- }
- else {
- // Initialise with video only
- CONDERROR(SDL_Init(SDL_INIT_VIDEO)!=0);
- atexit(SDL_Quit);
- }
-
- if (oss_sound_flag)
- play_tune(0);
-
- // Attempt to get the required video size
- flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
- if (fullscreen) flag |= SDL_FULLSCREEN;
- surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
-
- // Set the title bar text
- SDL_WM_SetCaption("Rock Dodgers", "rockdodgers");
-
- NULLERROR(surf_screen);
-
- // Set the heat color from the range 0 (cold) to 300 (blue-white)
- for (i=0; i<W*3; i++)
- heatcolor[i] = SDL_MapRGB(
- surf_screen->format,
- (i<W)?(i*M/W):(M),(i<W)?0:(i<2*W)?((i-W)*M/W):M,(i<2*W)?0:((i-W)*M/W) // Got that?
- );
-
- // Load the banners
- NULLERROR(temp = IMG_Load(load_file("banners/variations.png")));
- NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
-
- NULLERROR(temp = IMG_Load(load_file("banners/on.png")));
- NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
-
- NULLERROR(temp = IMG_Load(load_file("banners/rockdodger.png")));
- NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
-
- NULLERROR(temp = IMG_Load(load_file("banners/game.png")));
- NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
-
- NULLERROR(temp = IMG_Load(load_file("banners/over.png")));
- NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
-
- surf_font_big = IMG_Load(load_file(BIG_FONT_FILE));
- InitFont(surf_font_big);
-
- // Load the spaceship graphic.
- NULLERROR(temp = IMG_Load(load_file("sprites/ship.png")));
- NULLERROR(surf_ship = SDL_DisplayFormat(temp));
-
- // Load the life indicator (small ship) graphic.
- NULLERROR(temp = IMG_Load(load_file("indicators/life.png")));
- NULLERROR(surf_life = SDL_DisplayFormat(temp));
-
- // Create the array of black points;
- SDL_LockSurface(surf_ship);
- raw_pixels = (Uint16 *) surf_ship->pixels;
- for (i=0; i<surf_ship->w; i++)
- for (j=0; j<surf_ship->h; j++)
- if (raw_pixels[j*(surf_ship->pitch)/2+i] == 0) {
- blackptr->x = i;
- blackptr->y = j;
- blackptr++;
- }
- SDL_UnlockSurface(surf_ship);
-
- init_engine_dots();
- init_space_dots();
-
- // Load all our lovely rocks
- for (i=0; i<NROCKS; i++) {
- char a[100];
-
- sprintf(a,load_file("sprites/rock%d.png"),i);
- NULLERROR(temp = IMG_Load(a));
- NULLERROR(surf_rock[i] = SDL_DisplayFormat(temp));
-
- sprintf(a,load_file("sprites/deadrock%d.png"),i);
- NULLERROR(temp = IMG_Load(a));
- NULLERROR(surf_deadrock[i] = SDL_DisplayFormat(temp));
- }
-
- // Remove the mouse cursor
-#ifdef SDL_DISABLE
- SDL_ShowCursor(SDL_DISABLE);
-#endif
-
- return 0;
-}/*}}}*/
-int draw() {/*{{{*/
- int i,n;
- SDL_Rect src,dest;
- struct black_point_struct *p;
- Uint16 *raw_pixels;
- int bang, offset, x;
- char *text;
- float fadegame,fadeover;
-
- char *statedisplay, buf[1024];
-
- bang=0;
-
- src.x=0;
- src.y=0;
- dest.x=0;
- dest.y=0;
-
- // Draw a fully black background
- SDL_FillRect(surf_screen,NULL,0);
-
-
-#ifdef DEBUG
- // DEBUG {{{
- // Show the current state
- switch (state) {
- case TITLE_PAGE:
- statedisplay = "title_page";
- break;
- case GAMEPLAY:
- statedisplay = "gameplay";
- break;
- case DEAD_PAUSE:
- statedisplay = "dead_pause";
- break;
- case GAME_OVER:
- statedisplay = "game_over";
- break;
- case HIGH_SCORE_ENTRY:
- statedisplay = "high_score_entry";
- break;
- case HIGH_SCORE_DISPLAY:
- statedisplay = "high_score_display";
- break;
- case DEMO:
- statedisplay = "demo";
- break;
- }
- snprintf(buf,1024, "mode=%s", statedisplay);
- PutString(surf_screen,0,YSIZE-50,buf);
- // }}}
-#endif
-
- // Draw the background dots
- drawdots(surf_screen);
-
- // If it's firing, draw the laser
- if (laser)
- drawlaser();
-
- // Draw ship
- if (!gameover && (state==GAMEPLAY || state==DEMO) ) {
- src.w = surf_ship->w;
- src.h = surf_ship->h;
- dest.w = src.w;
- dest.h = src.h;
- dest.x = (int)xship;
- dest.y = (int)yship;
- SDL_BlitSurface(surf_ship,&src,surf_screen,&dest);
- }
-
- // Draw all the rocks, in all states
- for (i=0; i<MAXROCKS; i++) {
- if (rock[i].active) {
-
- src.w = rock[i].image->w;
- src.h = rock[i].image->h;
- dest.w = src.w;
- dest.h = src.h;
- dest.x = (int) rock[i].x;
- dest.y = (int) rock[i].y;
-
- // Draw the rock
- SDL_BlitSurface(rock[i].image,&src,surf_screen,&dest);
-
- // Draw the heated part of the rock, in an alpha which reflects the
- // amount of heat in the rock.
- if (rock[i].heat>0) {
- SDL_Surface *deadrock;
- deadrock = surf_deadrock[rock[i].type_number];
- SDL_SetAlpha(deadrock,SDL_SRCALPHA,rock[i].heat*255/rock[i].image->h);
- dest.x = (int) rock[i].x; // kludge
- SDL_BlitSurface(deadrock,&src,surf_screen,&dest);
- if (rnd()<0.3) rock[i].heat-=movementrate;
- }
-
- // If the rock is heated past a certain point, the water content of
- // the rock flashes to steam, releasing enough energy to destroy
- // the rock in spectacular fashion.
- if (rock[i].heat>rock[i].image->h) {
- rock[i].active=0;
- play_sound(1+(int)(rnd()*3));
- makebangdots(rock[i].x,rock[i].y,rock[i].xvel,rock[i].yvel,rock[i].image,10);
- }
-
- }
- }
-
- // If it's game over, show the game over graphic in the dead centre
- switch (state) {
-
- case GAME_OVER:
-
- if (fadetimer<3.0/faderate)
- fadegame=fadetimer/(3.0/faderate);
- else
- fadegame=1.0;
-
- if (fadetimer<3.0/faderate)
- fadeover=0.0;
- else
- if (fadetimer<6.0/faderate)
- fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
- else
- fadeover = 1.0;
-
- src.w = surf_b_game->w;
- src.h = surf_b_game->h;
- dest.w = src.w;
- dest.h = src.h;
- dest.x = (XSIZE-src.w)/2;
- dest.y = (YSIZE-src.h)/2-40;
- SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200+55*cos(fadetimer+=movementrate/1.0))));
- SDL_BlitSurface(surf_b_game,&src,surf_screen,&dest);
-
- src.w = surf_b_over->w;
- src.h = surf_b_over->h;
- dest.w = src.w;
- dest.h = src.h;
- dest.x = (XSIZE-src.w)/2;
- dest.y = (YSIZE-src.h)/2+40;
- SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200+55*sin(fadetimer))));
- SDL_BlitSurface(surf_b_over,&src,surf_screen,&dest);
- break;
-
- case TITLE_PAGE:
- src.w = surf_b_variations->w;
- src.h = surf_b_variations->h;
- dest.w = src.w;
- dest.h = src.h;
- dest.x = (XSIZE-src.w)/2 + cos(fadetimer/6.5)*10;
- dest.y = (YSIZE/2-src.h)/2 + sin(fadetimer/5.0)*10;
- SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200+55*sin(fadetimer+=movementrate/2.0)));
- SDL_BlitSurface(surf_b_variations,&src,surf_screen,&dest);
-
- src.w = surf_b_on->w;
- src.h = surf_b_on->h;
- dest.w = src.w;
- dest.h = src.h;
- dest.x = (XSIZE-src.w)/2 + cos((fadetimer+1.0)/6.5)*10;
- dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + 20 + sin((fadetimer+1.0)/5.0)*10;
- SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200+55*sin(fadetimer-1.0)));
- SDL_BlitSurface(surf_b_on,&src,surf_screen,&dest);
-
- src.w = surf_b_rockdodger->w;
- src.h = surf_b_rockdodger->h;
- dest.w = src.w;
- dest.h = src.h;
- dest.x = (XSIZE-src.w)/2 + cos((fadetimer+2.0)/6.5)*10;
- dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer+2.0)/5)*10;
- SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200+55*sin(fadetimer-2.0)));
- SDL_BlitSurface(surf_b_rockdodger,&src,surf_screen,&dest);
-
- text = "Version " VERSION;
- x = (XSIZE-SFont_wide(text))/2 + sin(fadetimer/4.5)*10;
- PutString(surf_screen,x,YSIZE-50+sin(fadetimer/2)*5,text);
-
- text = sequence[(int)(fadetimer/40)%NSEQUENCE];
- //text = "Press SPACE to start!";
- x = (XSIZE-SFont_wide(text))/2 + cos(fadetimer/4.5)*10;
- PutString(surf_screen,x,YSIZE-100+cos(fadetimer/3)*5,text);
- break;
-
- case HIGH_SCORE_ENTRY:
-
- if (score >= high[7].score) {
- play_tune(2);
- if (SFont_Input (surf_screen, 330, 50+(scorerank+2)*font_height, 300, name)) {
- // Insert name into high score table
-
- // Lose the lowest name forever (loser!)
- //if (high[7].allocated)
- // free(high[7].name); // THIS WAS CRASHING SO I REMOVED IT
-
- // Insert new high score
- high[scorerank].score = score;
- high[scorerank].name = strdup(name); // MEMORY NEVER FREED!
- high[scorerank].allocated = 1;
-
- // Set the global name string to "", ready for the next winner
- name[0]=0;
-
- // Change state to briefly show high scores page
- state = HIGH_SCORE_DISPLAY;
- state_timeout=200;
-
- // Write the high score table to the file
- write_high_score_table();
-
- // Play the title page tune
- play_tune(0);
- }
- }
- else {
- state = HIGH_SCORE_DISPLAY;
- state_timeout=400;
- }
-
- case HIGH_SCORE_DISPLAY:
- // Display de list o high scores mon.
- PutString(surf_screen,180,50,"High scores");
- for (i=0; i<8; i++) {
- char s[1024];
- sprintf(s, "#%1d",i+1);
- PutString(surf_screen, 150, 50+(i+2)*font_height,s);
- snprintscore(s, 1024, high[i].score);
- PutString(surf_screen, 200, 50+(i+2)*font_height,s);
- sprintf(s, "%3s", high[i].name);
- PutString(surf_screen, 330, 50+(i+2)*font_height,s);
- }
-
- }
-
- if (!gameover && state==GAMEPLAY) {
- // Show the freaky shields
- SDL_LockSurface(surf_screen);
- raw_pixels = (Uint16 *) surf_screen->pixels;
- if (initialshield>0 || shieldsup && shieldlevel>0) {
- int x,y,l;
- Uint16 c;
-
- if (initialshield>0) {
- initialshield-=movementrate;
- c = SDL_MapRGB(surf_screen->format,0,255,255);
- }
- else {
- c = heatcolor[(int)shieldlevel];
- shieldlevel-=movementrate;
- }
-
- shieldpulse += 0.2;
- for (p=black_point; p<blackptr; p++) {
- x = p->x + (int)xship + (rnd()+rnd()-1)*sin(shieldpulse)*4 + 1;
- y = p->y + (int)yship + (rnd()+rnd()-1)*sin(shieldpulse)*4 + 1;
- if (x>0 && y>0 && x<XSIZE && y<YSIZE) {
- offset = surf_screen->pitch/2 * y + x;
- raw_pixels[offset] = c;
- }
- }
- }
- else {
- // When the shields are off, check that the black points
- // on the ship are still black, and not covered up by rocks
- for (p=black_point; p<blackptr; p++) {
- offset = surf_screen->pitch/2 * (p->y + (int)yship) + p->x + (int)xship;
- if (raw_pixels[offset]) {
- // Set the bang flag
- bang = 1;
- }
- }
- }
- SDL_UnlockSurface(surf_screen);
- }