JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
we now return you to your regularly scheduled exploding ship.
[vor.git] / main.c
diff --git a/main.c b/main.c
index ae52bd7..a068033 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, 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,16 +399,14 @@ init(void) {
        return 0;
 }
 
-int
-draw() {
+void
+draw(void) {
        int i;
        SDL_Rect dest;
-       int bang, x;
+       int x;
        char *text;
        float fadegame,fadeover;
 
-       bang = 0;
-
        // Draw a fully black background
        SDL_FillRect(surf_screen,NULL,0);
 
@@ -417,11 +414,7 @@ draw() {
        drawdots(surf_screen);
 
        // Draw ship
-       if(state == GAMEPLAY ) {
-               dest.x = ship.x;
-               dest.y = ship.y;
-               SDL_BlitSurface(ship.image,NULL,surf_screen,&dest);
-       }
+       if(state == GAMEPLAY) draw_sprite(SPRITE(&ship));
 
        draw_rocks();
 
@@ -516,9 +509,7 @@ 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;
@@ -534,9 +525,18 @@ draw() {
 
        // Update the surface
        SDL_Flip(surf_screen);
+}
 
-
-       return bang;
+void
+do_collision(Sprite *a, Sprite *b)
+{
+       if(a->type == SHIP) {
+               a->type = -SHIP; bang = true;
+       } else if (b->type == SHIP) {
+               b->type = -SHIP; bang = true;
+       } else {
+               bounce(a, b);
+       }
 }
 
 int
@@ -553,6 +553,7 @@ gameloop() {
                                switch(state) {
                                        case DEAD_PAUSE:
                                                // Create a new ship and start all over again
+                                               ship.sprite_type = SHIP;
                                                state = GAMEPLAY;
                                                play_tune(TUNE_GAMEPLAY);
                                                break;
@@ -585,16 +586,10 @@ 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 {
+                                       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;
                                }
@@ -619,14 +614,11 @@ 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
@@ -643,13 +635,15 @@ gameloop() {
                                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,8 +651,7 @@ gameloop() {
                                        state_timeout = 200.0;
                                        fadetimer = 0.0;
                                        faderate = t_frame;
-                               }
-                               else {
+                               } else {
                                        state = DEAD_PAUSE;
                                        state_timeout = DEAD_PAUSE_LENGTH;
                                }
@@ -669,17 +662,18 @@ gameloop() {
 
                        // new game
                        if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE)) {
-
+                               reset_sprites();
                                reset_rocks();
 
+                               ship.x = XSIZE/2.2; ship.y = YSIZE/2;
+                               ship.dx = screendx; ship.dy = screendy;
                                ship.lives = 4;
+                               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;