JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fade new ships in,
[vor.git] / main.c
diff --git a/main.c b/main.c
index ae52bd7..5ac2b5e 100644 (file)
--- a/main.c
+++ b/main.c
@@ -21,8 +21,8 @@
 
 #include <argp.h>
 #include <math.h>
-#include <SDL/SDL.h>
-#include <SDL/SDL_image.h>
+#include <SDL.h>
+#include <SDL_image.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -66,7 +66,7 @@ struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
 char topline[1024];
 char *initerror = "";
 
-struct ship ship = { SHIP_SPRITE, NULL, XSIZE/2, YSIZE/2, SCREENDXMIN, 0.0 };
+struct ship ship = { SHIP, 0, NULL, XSIZE/2, YSIZE/2, SCREENDXMIN, 0.0 };
          
 float screendx = SCREENDXMIN, screendy = 0.0;
 float xscroll, yscroll;
@@ -77,6 +77,7 @@ float t_frame;  // length of this frame (in ticks = 1/20th second)
 int ms_frame;   // length of this frame (milliseconds)
 int ms_end;     // end of this frame (milliseconds)
 
+int bang = false;
 float bangx, bangy, bangdx, bangdy;
 
 int score;
@@ -203,7 +204,7 @@ draw_bang_dots(SDL_Surface *s)
                }
 
                // check collisions
-               if(pixel_hit_rocks(bdot[i].x, bdot[i].y)) { bdot[i].active = 0; continue; }
+               if(pixel_collides(bdot[i].x, bdot[i].y)) { bdot[i].active = 0; continue; }
 
                pixel = pixels + row_inc*(int)(bdot[i].y) + (int)(bdot[i].x);
                if(bdot[i].c) c = bdot[i].c; else c = heatcolor[(int)(bdot[i].life)*3];
@@ -268,7 +269,7 @@ draw_engine_dots(SDL_Surface *s) {
                        edot[i].active = 0;
                        continue;
                }
-               if(pixel_hit_rocks(edot[i].x, edot[i].y)) { edot[i].active = 0; continue; }
+               if(pixel_collides(edot[i].x, edot[i].y)) { edot[i].active = 0; continue; }
                heatindex = edot[i].life * 6;
                c = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
                pixels[row_inc*(int)(edot[i].y) + (int)(edot[i].x)] = c;
@@ -311,6 +312,12 @@ load_image(char *filename)
        return img;
 }
 
+void
+load_ship(void)
+{
+       load_sprite(SPRITE(&ship), "sprites/ship.png");
+}
+
 int
 init(void) {
 
@@ -367,15 +374,6 @@ init(void) {
        NULLERROR(surf_b_game = load_image("banners/game.png"));
        NULLERROR(surf_b_over = load_image("banners/over.png"));
 
-       // Load the spaceship graphic.
-       ship.shape = malloc(sizeof(struct shape));
-       if(!ship.shape) {
-               fprintf(stderr, "can't allocate ship shape.\n");
-               exit(1);
-       }
-       NULLERROR(ship.image = load_image("sprites/ship.png"));
-       get_shape(ship.image, ship.shape);
-
        // Load the life indicator (small ship) graphic.
        NULLERROR(surf_life = load_image("indicators/life.png"));
 
@@ -390,7 +388,8 @@ init(void) {
        init_engine_dots();
        init_dust();
 
-       init_rocks();
+       init_sprites();
+       add_sprite(SPRITE(&ship));
 
        // Remove the mouse cursor
 #ifdef SDL_DISABLE
@@ -400,43 +399,33 @@ init(void) {
        return 0;
 }
 
-int
-draw() {
+void
+show_lives(void)
+{
        int i;
        SDL_Rect dest;
-       int bang, x;
-       char *text;
-       float fadegame,fadeover;
-
-       bang = 0;
-
-       // Draw a fully black background
-       SDL_FillRect(surf_screen,NULL,0);
 
-       // Draw the background dots
-       drawdots(surf_screen);
-
-       // Draw ship
-       if(state == GAMEPLAY ) {
-               dest.x = ship.x;
-               dest.y = ship.y;
-               SDL_BlitSurface(ship.image,NULL,surf_screen,&dest);
+       for(i=0; i<ship.lives-1; i++) {
+               dest.x = (i + 1)*(surf_life->w + 10);
+               dest.y = 20;
+               SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
        }
+}
 
-       draw_rocks();
+void
+draw(void) {
+       SDL_Rect dest;
+       int x;
+       char *text;
+       float fadegame,fadeover;
 
-       // Draw the life indicators.
-       if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER) {
-               for(i = 0; i<ship.lives-1; i++) {
-                       dest.x = (i + 1)*(surf_life->w + 10);
-                       dest.y = 20;
-                       SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
-               }
-       }
+       SDL_FillRect(surf_screen,NULL,0);  // black background
+       drawdots(surf_screen);             // background dots
+       draw_sprite(SPRITE(&ship));
+       draw_rocks();
 
-       // Draw the score
-       snprintscore_line(topline, 50, score);
-       SFont_Write(surf_screen, g_font, XSIZE-250, 0, topline);
+       show_lives();
+       show_score();
 
        // If it's game over, show the game over graphic in the dead centre
        switch (state) {
@@ -516,43 +505,51 @@ draw() {
                        ; // no action necessary
        }
 
-       if(state == GAMEPLAY) {
-               bang = hit_rocks(ship.x, ship.y, ship.shape);
-       }
+       collisions();
 
        ms_frame = SDL_GetTicks() - ms_end;
        ms_end += ms_frame;
-       if(ms_frame>200 || ms_frame<0) {
-               // We won't run at all below 5 frames per second.
-               // This also happens if we were paused, grr.
-               t_frame = 0;
-               ms_frame = 0;
-       } else {
-               t_frame = opt_gamespeed * ms_frame / 50;
-               if(state == GAMEPLAY) score += ms_frame;
-       }
+       t_frame = opt_gamespeed * ms_frame / 50;
+       if(state == GAMEPLAY) score += ms_frame;
 
        // Update the surface
        SDL_Flip(surf_screen);
+}
 
+static inline void
+kill_ship(Sprite *ship)
+{
+       ship->flags = MOVE|DRAW;
+       SDL_SetAlpha(ship->image, SDL_SRCALPHA, 0);
+       bang = true;
+}
 
-       return bang;
+void
+do_collision(Sprite *a, Sprite *b)
+{
+       if(a->type == SHIP) kill_ship(a);
+       else if (b->type == SHIP) kill_ship(b);
+       else bounce(a, b);
 }
 
-int
+void
 gameloop() {
        Uint8 *keystate = SDL_GetKeyState(NULL);
        float tmp;
 
 
        for(;;) {
+               SDL_PumpEvents();
+               keystate = SDL_GetKeyState(NULL);
+
                if(!paused) {
                        // Count down the game loop timer, and change state when it gets to zero or less;
 
                        if((state_timeout -= t_frame*3) < 0) {
                                switch(state) {
                                        case DEAD_PAUSE:
-                                               // Create a new ship and start all over again
+                                               // Restore the ship and continue playing
+                                               ship.flags = DRAW|MOVE|COLLIDE;
                                                state = GAMEPLAY;
                                                play_tune(TUNE_GAMEPLAY);
                                                break;
@@ -564,7 +561,7 @@ gameloop() {
                                                        SDL_EnableUNICODE(1);
                                                        while(SDL_PollEvent(&e))
                                                                ;
-                                               } else if(!keystate[SDLK_SPACE]) {
+                                               } else {
                                                        state = HIGH_SCORE_DISPLAY;
                                                        state_timeout = 400;
                                                }
@@ -584,19 +581,16 @@ gameloop() {
                                }
                        } else {
                                if(state == DEAD_PAUSE) {
-                                       float blast_radius;
-                                       int fixonly;
-
-                                       if(state_timeout < DEAD_PAUSE_LENGTH - 20.0) {
-                                               blast_radius = BLAST_RADIUS * 1.3;
-                                               fixonly = 1;
-                                       } else {
+                                       float blast_radius, alpha;
+                                       if(state_timeout >= DEAD_PAUSE_LENGTH - 20.0) {
                                                blast_radius = BLAST_RADIUS * (DEAD_PAUSE_LENGTH - state_timeout) / 20.0;
-                                               fixonly = 0;
+                                               blast_rocks(bangx, bangy, blast_radius);
                                        }
-                                       blast_rocks(bangx, bangy, blast_radius, fixonly);
 
                                        if(bangx < 60) bangx = 60;
+
+                                       alpha = 255.0 * (DEAD_PAUSE_LENGTH - state_timeout) / DEAD_PAUSE_LENGTH;
+                                       SDL_SetAlpha(ship.image, SDL_SRCALPHA, (uint8_t)alpha);
                                }
                        }
 
@@ -619,37 +613,34 @@ gameloop() {
                        xscroll = screendx * t_frame;
                        yscroll = screendy * t_frame;
 
-                       ship.x += ship.dx*t_frame - xscroll;
-                       ship.y += ship.dy*t_frame - yscroll;
-
                        // move bang center
                        bangx += bangdx*t_frame - xscroll;
                        bangy += bangdy*t_frame - yscroll;
 
-                       move_rocks();
+                       move_sprites();
 
 
-                       // BOUNCE X
-                       if(ship.x<0 || ship.x>XSIZE-ship.image->w) {
-                               // BOUNCE from left and right wall
+                       // BOUNCE off left or right edge of screen
+                       if(ship.x < 0 || ship.x+ship.w > XSIZE) {
                                ship.x -= (ship.dx-screendx)*t_frame;
                                ship.dx = screendx - (ship.dx-screendx)*opt_bounciness;
                        }
 
-                       // BOUNCE Y
-                       if(ship.y<0 || ship.y>YSIZE-ship.image->h) {
-                               // BOUNCE from top and bottom wall
+                       // BOUNCE off top or bottom of screen
+                       if(ship.y < 0 || ship.y+ship.h > YSIZE) {
                                ship.y -= (ship.dy-screendy)*t_frame;
                                ship.dy = screendy - (ship.dy-screendy)*opt_bounciness;
                        }
 
+                       draw();
 
-                       if(draw() && state == GAMEPLAY) {
+                       if(state == GAMEPLAY && bang) {
                                // Died
+                               bang = false;
                                play_sound(SOUND_BANG); // Play the explosion sound
                                bangx = ship.x; bangy = ship.y; bangdx = ship.dx; bangdy = ship.dy;
-                                       new_bang_dots(ship.x,ship.y,ship.dx,ship.dy,ship.image);
-                                       ship.dx *= 0.5; ship.dy *= 0.5;
+                               new_bang_dots(ship.x,ship.y,ship.dx,ship.dy,ship.image);
+                               ship.dx *= 0.5; ship.dy *= 0.5;
                                if(ship.dx < SCREENDXMIN) ship.dx = SCREENDXMIN;
                                if(--ship.lives <= 0) {
                                        state = GAME_OVER;
@@ -657,38 +648,37 @@ gameloop() {
                                        state_timeout = 200.0;
                                        fadetimer = 0.0;
                                        faderate = t_frame;
-                               }
-                               else {
+                               } else {
                                        state = DEAD_PAUSE;
                                        state_timeout = DEAD_PAUSE_LENGTH;
                                }
                        }
 
-                       SDL_PumpEvents();
-                       keystate = SDL_GetKeyState(NULL);
-
                        // new game
-                       if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE)) {
-
+                       if(keystate[SDLK_SPACE]
+                          && (state == HIGH_SCORE_DISPLAY
+                              || state == TITLE_PAGE
+                              || state == GAME_OVER)) {
+                               reset_sprites();
                                reset_rocks();
 
+                               ship.x = XSIZE/2.2; ship.y = YSIZE/2;
+                               ship.dx = screendx; ship.dy = screendy;
                                ship.lives = 4;
+                               ship.flags = MOVE|DRAW|COLLIDE;
+                               SDL_SetAlpha(ship.image, SDL_SRCALPHA, SDL_ALPHA_OPAQUE);
+                               add_sprite(SPRITE(&ship));
+
                                score = 0;
 
                                state = GAMEPLAY;
                                play_tune(TUNE_GAMEPLAY);
-
-                               ship.x = XSIZE/2.2; ship.y = YSIZE/2;
-                               ship.dx = screendx; ship.dy = screendy;
                        }
 
                        ship.jets = 0;
-               } else {
-                       SDL_PumpEvents();
-                       keystate = SDL_GetKeyState(NULL);
                }
 
-               if(state == GAMEPLAY) {
+               if(state == GAMEPLAY || state == DEAD_PAUSE) {
                        if(!paused) {
                                if(keystate[SDLK_LEFT]  | keystate[SDLK_h]) { ship.dx -= 1.5*t_frame; ship.jets |= 1<<0;}
                                if(keystate[SDLK_DOWN]  | keystate[SDLK_t]) { ship.dy += 1.5*t_frame; ship.jets |= 1<<1;}
@@ -701,19 +691,15 @@ gameloop() {
                                if(!pausedown) {
                                        paused = !paused;
                                        pausedown = 1;
+                                       if(!paused) ms_end = SDL_GetTicks();
                                }
                        } else {
                                pausedown = 0;
                        }
-               } else if(state == GAME_OVER) {
-                       if(keystate[SDLK_SPACE]) {
-                               state_timeout = -1;
-                       }
                }
 
-               if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
-                       return 0;
-               }
+               if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE]))
+                       return;
 
        }
 }