X-Git-Url: https://jasonwoof.com/gitweb/?p=vor.git;a=blobdiff_plain;f=main.c;h=4942aa2c5ac0f0f721fb0316d0ebdd57b432bb40;hp=51d8e9f14233742aaa5849c39583f1e3ee6b7891;hb=e10d6c0b95e2f4fe7acd75733370dae71bb3b73f;hpb=a2af96e68b210b7c3db90b7ec474498c5611a3a7 diff --git a/main.c b/main.c index 51d8e9f..4942aa2 100644 --- a/main.c +++ b/main.c @@ -21,12 +21,9 @@ #undef DEBUG -extern int font_height; -void clearBuffer(); - -// includes {{{ #include "config.h" #include "file.h" +#include "score.h" #include "sound.h" #include @@ -39,13 +36,9 @@ void clearBuffer(); #include #include "SFont.h" -// }}} -// constants {{{ -// }}} -// macros {{{ + #define CONDERROR(a) if((a)) {initerror = strdup(SDL_GetError());return 1;} #define NULLERROR(a) CONDERROR((a) == NULL) -// }}} // ************************************* STRUCTS struct rock_struct { @@ -86,25 +79,9 @@ struct spacedot { float x,y,dx; Uint16 color; }; -// High score table {{{ -struct highscore { - int score; - char *name; - int allocated; -} high[] = { - {13000,"Pad",0}, - {12500,"Pad",0}, - {6500,"Pad",0}, - {5000,"Pad",0}, - {3000,"Pad",0}, - {2500,"Pad",0}, - {2000,"Pad",0}, - {1500,"Pad",0} -}; -// }}} // ************************************* VARS -// SDL_Surface global variables {{{ +// SDL_Surface global variables SDL_Surface *surf_screen, // Screen *surf_b_variations, // "variations" banner @@ -117,23 +94,24 @@ SDL_Surface *surf_rock[NROCKS], // THE ROCKS *surf_deadrock[NROCKS], // THE DEAD ROCKS *surf_font_big; // The big font -// }}} -// Structure global variables {{{ + +SFont_Font *g_font; + +// Structure global variables struct enginedots edot[MAXENGINEDOTS], *dotptr = edot; struct rock_struct rock[MAXROCKS], *rockptr = rock; struct black_point_struct black_point[MAXBLACKPOINTS], *blackptr = black_point; struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot; struct spacedot sdot[MAXSPACEDOTS]; -// }}} -// Other global variables {{{ + +// Other global variables char topline[1024]; char *initerror = ""; -char name[1024], debug1[1024]; float xship,yship = 240.0; // X position, 0..XSIZE float xvel,yvel; // Change in X position per tick. float rockrate,rockspeed; -float movementrate; +float movementrate; // this controls the speed of everything that moves. float yscroll; float scrollvel; @@ -144,16 +122,13 @@ int maneuver = 0; int sound_flag = 1, music_flag = 0; int tail_plume = 0; // display big engine at the back? int friction = 0; // should there be friction? -int scorerank; -float fadetimer = 0,faderate; +float fadetimer = 0, faderate; -int pausedown = 0,paused = 0; +int pausedown = 0, paused = 0; // bangdot start (bd1) and end (bd2) position: int bd1 = 0, bd2 = 0; -int xoffset[NROCKS][MAXROCKHEIGHT]; - enum states { TITLE_PAGE, GAMEPLAY, @@ -166,8 +141,6 @@ enum states { enum states state = TITLE_PAGE; float state_timeout = 600.0; -const int fakesin[] = {0,1,0,-1}; -const int fakecos[] = {1,0,-1,0}; #define NSEQUENCE 2 char *sequence[] = { "Press SPACE to start", @@ -180,7 +153,6 @@ Uint16 heatcolor[W*3]; char *data_dir; extern char *optarg; extern int optind, opterr, optopt; -// }}} float dist_sq(float x1, float y1, float x2, float y2) { @@ -189,63 +161,21 @@ float dist_sq(float x1, float y1, float x2, float y2) // ************************************* FUNCS -void read_high_score_table() { - FILE *f; - int i; - - f = open_score_file("r"); - if(f) { - // If the file exists, read from it - for(i = 0; i<8; i++) { - char s[1024]; - int highscore; - if(fscanf (f, "%d %[^\n]", &highscore, s) != 2) { - break; - } - if(high[i].allocated) { - free(high[i].name); - } - high[i].name = strdup(s); - high[i].score = highscore; - high[i].allocated = 1; - } - fclose(f); - } -} - -void write_high_score_table() { - FILE *f; - int i; - - f = open_score_file("w"); - if(f) { - // If the file exists, write to it - for(i = 0; i<8; i++) { - fprintf (f, "%d %s\n", high[i].score, high[i].name); - } - fclose(f); - } -} -void snprintscore(char *s, size_t n, int score) { - int min = score/60000; - int sec = score/1000%60; - int tenths = score%1000/100; - if(min) { - snprintf(s, n, "%2d:%.2d.%d", min, sec, tenths); - } else { - snprintf(s, n, " %2d.%d", sec, tenths); - } -} -float rnd() { +float +rnd() { return (float)random()/(float)RAND_MAX; } -void init_engine_dots() { + +void +init_engine_dots() { int i; for(i = 0; ipixels; @@ -385,7 +318,8 @@ void draw_space_dots(SDL_Surface *s) { } } -void draw_engine_dots(SDL_Surface *s) { +void +draw_engine_dots(SDL_Surface *s) { int i; Uint16 *rawpixel; rawpixel = (Uint16 *) s->pixels; @@ -408,7 +342,8 @@ void draw_engine_dots(SDL_Surface *s) { } } -void create_engine_dots(int newdots) { +void +create_engine_dots(int newdots) { int i; double theta,r,dx,dy; @@ -438,7 +373,8 @@ void create_engine_dots(int newdots) { } } -void create_engine_dots2(int newdots, int m) { +void +create_engine_dots2(int newdots, int m) { int i; double theta, theta2, dx, dy, adx, ady; @@ -493,8 +429,9 @@ void create_engine_dots2(int newdots, int m) { } } -void drawdots(SDL_Surface *s) { - int m, scorepos, n; +void +drawdots(SDL_Surface *s) { + int m; SDL_LockSurface(s); // Draw the background stars aka space dots @@ -504,10 +441,8 @@ void drawdots(SDL_Surface *s) { if(1 || state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER ) { SDL_UnlockSurface(s); - scorepos = XSIZE-250; - n = snprintf(topline, 50, "Time: "); - snprintscore(topline + n, 50-n, score); - PutString(s,scorepos,0,topline); + snprintscore_line(topline, 50, score); + SFont_Write(s,g_font,XSIZE-250,0,topline); SDL_LockSurface(s); } @@ -516,8 +451,7 @@ void drawdots(SDL_Surface *s) { draw_engine_dots(s); // Create more engine dots comin out da back - if(!gameover) - create_engine_dots(200); + if(!gameover) create_engine_dots(200); // Create engine dots out the side we're moving from for(m = 0; m<4; m++) { @@ -533,7 +467,8 @@ void drawdots(SDL_Surface *s) { SDL_UnlockSurface(s); } -int init(int fullscreen) { +int +init(int fullscreen) { int i,j; SDL_Surface *temp; @@ -582,30 +517,30 @@ int init(int fullscreen) { } // Load the banners - NULLERROR(temp = IMG_Load(load_file("banners/variations.png"))); + NULLERROR(temp = IMG_Load(add_path("banners/variations.png"))); NULLERROR(surf_b_variations = SDL_DisplayFormat(temp)); - NULLERROR(temp = IMG_Load(load_file("banners/on.png"))); + NULLERROR(temp = IMG_Load(add_path("banners/on.png"))); NULLERROR(surf_b_on = SDL_DisplayFormat(temp)); - NULLERROR(temp = IMG_Load(load_file("banners/rockdodger.png"))); + NULLERROR(temp = IMG_Load(add_path("banners/rockdodger.png"))); NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp)); - NULLERROR(temp = IMG_Load(load_file("banners/game.png"))); + NULLERROR(temp = IMG_Load(add_path("banners/game.png"))); NULLERROR(surf_b_game = SDL_DisplayFormat(temp)); - NULLERROR(temp = IMG_Load(load_file("banners/over.png"))); + NULLERROR(temp = IMG_Load(add_path("banners/over.png"))); NULLERROR(surf_b_over = SDL_DisplayFormat(temp)); - surf_font_big = IMG_Load(load_file(BIG_FONT_FILE)); - InitFont(surf_font_big); + surf_font_big = IMG_Load(add_path(BIG_FONT_FILE)); + g_font = SFont_InitFont(surf_font_big); // Load the spaceship graphic. - NULLERROR(temp = IMG_Load(load_file("sprites/ship.png"))); + NULLERROR(temp = IMG_Load(add_path("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(temp = IMG_Load(add_path("indicators/life.png"))); NULLERROR(surf_life = SDL_DisplayFormat(temp)); // Create the array of black points; @@ -630,11 +565,11 @@ int init(int fullscreen) { for(i = 0; i= 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 { + // Change state to briefly show high scores page state = HIGH_SCORE_DISPLAY; - state_timeout = 400; - } - // FALL THROUGH + state_timeout = 200; + // Write the high score table to the file + write_high_score_table(); + + // Play the title page tune + play_tune(0); + } + // FALL THROUGH TO 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); - } + display_scores(surf_screen, 150,50); } @@ -908,7 +784,8 @@ int draw() { return bang; } -int gameloop() { +int +gameloop() { int i = 0; Uint8 *keystate; @@ -924,45 +801,29 @@ int gameloop() { state = GAMEPLAY; play_tune(1); xship -= 50; - break; + break; case GAME_OVER: state = HIGH_SCORE_ENTRY; - clearBuffer(); - name[0] = 0; state_timeout = 5.0e6; - - if(score >= high[7].score) { - // Read the high score table from the storage file - read_high_score_table(); - - // Find ranking of this score, store as scorerank - for(i = 0; i<8; i++) { - if(high[i].score <= score) { - scorerank = i; - break; - } - } - - // Move all lower scores down a notch - for(i = 7; i >= scorerank; i--) - high[i] = high[i-1]; - - // Insert blank high score - high[scorerank].score = score; - high[scorerank].name = ""; - high[scorerank].allocated = 0; + if(new_high_score(score)) { + SDL_Event e; + SDL_EnableUNICODE(1); + while(SDL_PollEvent(&e)) + ; + } else { + state = HIGH_SCORE_DISPLAY; + state_timeout = 400; } - - break; + break; case HIGH_SCORE_DISPLAY: state = TITLE_PAGE; state_timeout = 500.0; - break; + break; case HIGH_SCORE_ENTRY: // state = TITLE_PAGE; // play_tune(1); // state_timeout = 100.0; - break; + break; case TITLE_PAGE: state = HIGH_SCORE_DISPLAY; state_timeout = 200.0;