From 6f82bba8833f187abebab53375ac87905487cf8c Mon Sep 17 00:00:00 2001 From: Joshua Grams Date: Sun, 2 Jan 2005 20:51:19 +0000 Subject: [PATCH] * Switched over to using SFont 2.03, which doesn't have tons of extra cruft. * Moved all things to do with displaying/editing the score out to score.c/score.h --- Makefile | 4 +- SFont.c | 332 +++++++++++++++++++++----------------------------------------- SFont.h | 89 ++++++++++++----- config.h | 1 + file.c | 2 +- main.c | 187 +++++++---------------------------- score.c | 155 +++++++++++++++++++++++++++++ score.h | 44 +++++++++ 8 files changed, 411 insertions(+), 403 deletions(-) create mode 100644 score.c create mode 100644 score.h diff --git a/Makefile b/Makefile index 6ffba09..0b8ef80 100644 --- a/Makefile +++ b/Makefile @@ -16,9 +16,9 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ldflags := $(shell sdl-config --libs) -lSDL_image -lSDL_mixer -cflags := -g $(shell sdl-config --cflags) -Wall +cflags := $(shell sdl-config --cflags) -Wall -my_objects := sound.o file.o main.o +my_objects := file.o score.o sound.o main.o libs := SFont.o objects := $(libs) $(my_objects) diff --git a/SFont.c b/SFont.c index e77f01b..7e750bf 100644 --- a/SFont.c +++ b/SFont.c @@ -1,28 +1,47 @@ -#include +/* SFont: a simple font library that uses special images as fonts + Copyright (C) 2003 Karl Bartel + + License: GPL or LGPL (at your choice) + WWW: http://www.linux-games.com/sfont/ + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Karl Bartel + Cecilienstr. 14 + 12307 Berlin + GERMANY + karlb@gmx.net +*/ +#include + +#include #include #include "SFont.h" -#include "string.h" -SFont_FontInfo InternalFont; -int alpha=127; -int font_height; - -Uint32 +static Uint32 GetPixel(SDL_Surface *Surface, Sint32 X, Sint32 Y) { - Uint8 *bits; Uint32 Bpp; - if (X<0) puts("SFONT ERROR: x too small in GetPixel. Report this to "); - if (X>=Surface->w) puts("SFONT ERROR: x too big in GetPixel. Report this to "); + assert(X>=0); + assert(Xw); Bpp = Surface->format->BytesPerPixel; - bits = ((Uint8 *)Surface->pixels)+Y*Surface->pitch+X*Bpp; - // Paint the walls with the fresh blood of your enemies - // Get the pixel switch(Bpp) { case 1: @@ -44,253 +63,122 @@ GetPixel(SDL_Surface *Surface, Sint32 X, Sint32 Y) break; } - return -1; + return -1; } -void -InitFont2(SFont_FontInfo *Font) +SFont_Font * +SFont_InitFont(SDL_Surface* Surface) { int x = 0, i = 0; + Uint32 pixel; + SFont_Font* Font; + Uint32 pink; - if ( Font->Surface==NULL ) { - printf("The font has not been loaded!\n"); - exit(1); - } + if (Surface == NULL) + return NULL; + + Font = (SFont_Font *) malloc(sizeof(SFont_Font)); + Font->Surface = Surface; - while ( x < Font->Surface->w ) { - if(GetPixel(Font->Surface,x,0)==SDL_MapRGB(Font->Surface->format,255,0,255)) { + SDL_LockSurface(Surface); + + pink = SDL_MapRGB(Surface->format, 255, 0, 255); + while (x < Surface->w) { + if (GetPixel(Surface, x, 0) == pink) { Font->CharPos[i++]=x; - while (( x < Font->Surface->w-1) && (GetPixel(Font->Surface,x,0)==SDL_MapRGB(Font->Surface->format,255,0,255))) + while((x < Surface->w) && (GetPixel(Surface, x, 0)== pink)) x++; Font->CharPos[i++]=x; } x++; } + Font->MaxPos = x-1; + + pixel = GetPixel(Surface, 0, Surface->h-1); + SDL_UnlockSurface(Surface); + SDL_SetColorKey(Surface, SDL_SRCCOLORKEY, pixel); - Font->h=Font->Surface->h; - font_height = Font->h; - SDL_SetColorKey(Font->Surface, SDL_SRCCOLORKEY, GetPixel(Font->Surface, 0, Font->Surface->h-1)); + return Font; } void -InitFont(SDL_Surface *Font) +SFont_FreeFont(SFont_Font* FontInfo) { - InternalFont.Surface=Font; - InitFont2(&InternalFont); -} - -int -SFont_wide(char *text) { - int i=0,xwide=0; - int ofs; - SFont_FontInfo *Font = &InternalFont; - - while (text[i]) { - if (text[i]==' ') { - xwide += (int)(Font->CharPos[2]-Font->CharPos[1]); - } else { - ofs = (text[i]-33)*2+1; - xwide += (int)(Font->CharPos[ofs+1]-Font->CharPos[ofs]); - } - i++; - } - return xwide; -} - -int -SFont_height() { - return InternalFont.Surface->h-1; + SDL_FreeSurface(FontInfo->Surface); + free(FontInfo); } void -PutString2(SDL_Surface *Surface, SFont_FontInfo *Font, int x, int y, char *text) +SFont_Write(SDL_Surface *Surface, const SFont_Font *Font, + int x, int y, const char *text) { - int ofs; - int i=0; - SDL_Rect srcrect,dstrect; - - while (text[i]) { - if (text[i]==' ') { - x+=Font->CharPos[2]-Font->CharPos[1]; + const char* c; + int charoffset; + SDL_Rect srcrect, dstrect; + + if(text == NULL) + return; + + // these values won't change in the loop + srcrect.y = 1; + dstrect.y = y; + srcrect.h = dstrect.h = Font->Surface->h - 1; + + for(c = text; *c != '\0' && x <= Surface->w ; c++) { + charoffset = ((int) (*c - 33)) * 2 + 1; + // skip spaces and nonprintable characters + if (*c == ' ' || charoffset < 0 || charoffset > Font->MaxPos) { + x += Font->CharPos[2]-Font->CharPos[1]; + continue; } - else { -// printf("-%c- %c - %u\n",228,text[i],text[i]); - ofs=(text[i]-33)*2+1; - //printf("printing %c %d\n",text[i],ofs); - srcrect.w = dstrect.w = (Font->CharPos[ofs+2]+Font->CharPos[ofs+1])/2-(Font->CharPos[ofs]+Font->CharPos[ofs-1])/2; - srcrect.h = dstrect.h = Font->Surface->h-1; - srcrect.x = (Font->CharPos[ofs]+Font->CharPos[ofs-1])/2; - srcrect.y = 1; - dstrect.x = x-(float)(Font->CharPos[ofs]-Font->CharPos[ofs-1])/2; - dstrect.y = y; - - //SDL_SetAlpha ( Font->Surface, SDL_SRCALPHA, 127); - SDL_BlitSurface( Font->Surface, &srcrect, Surface, &dstrect); - - x+=Font->CharPos[ofs+1]-Font->CharPos[ofs]; - } - i++; - } -} -// Return a new surface, with the text on it. -// This surface is new, fresh, and must eventually be freed. -// Create the new surface with the same colour system as a parent surface. -SDL_Surface * -new_Surface_PutString(SDL_Surface *parent, char *text) { + srcrect.w = dstrect.w = + (Font->CharPos[charoffset+2] + Font->CharPos[charoffset+1])/2 - + (Font->CharPos[charoffset] + Font->CharPos[charoffset-1])/2; + srcrect.x = (Font->CharPos[charoffset]+Font->CharPos[charoffset-1])/2; + dstrect.x = x - (float)(Font->CharPos[charoffset] + - Font->CharPos[charoffset-1])/2; - Uint32 rmask = parent->format->Rmask; - Uint32 gmask = parent->format->Gmask; - Uint32 bmask = parent->format->Bmask; - Uint32 amask = parent->format->Amask; - Uint32 bytesperpixel = parent->format->BytesPerPixel; + SDL_BlitSurface(Font->Surface, &srcrect, Surface, &dstrect); - return SDL_CreateRGBSurface( - SDL_SWSURFACE, - SFont_wide(text), - SFont_height(), - bytesperpixel, rmask, gmask, bmask, amask - ); -} - -void -PutString(SDL_Surface *Surface, int x, int y, char *text) { - PutString2(Surface, &InternalFont, x, y, text); -} - -int -TextWidth2(SFont_FontInfo *Font, char *text) -{ - int x=0,i=0; - unsigned char ofs = 0; - while (text[i]!='\0') { - if (text[i]==' ') { - x+=Font->CharPos[2]-Font->CharPos[1]; - } else { - ofs=(text[i]-33)*2+1; - x+=Font->CharPos[ofs+1]-Font->CharPos[ofs]; - } - i++; + x += Font->CharPos[charoffset+1] - Font->CharPos[charoffset]; } - return x+Font->CharPos[ofs+2]-Font->CharPos[ofs+1]; } int -TextWidth(char *text) -{ - return TextWidth2(&InternalFont, text); -} - -void -TextAlpha(int a) { - alpha = a; -} - -void -XCenteredString2(SDL_Surface *Surface, SFont_FontInfo *Font, int y, char *text) +SFont_TextWidth(const SFont_Font *Font, const char *text) { - PutString2(Surface, &InternalFont, Surface->w/2-TextWidth(text)/2, y, text); -} + const char* c; + int charoffset=0; + int width = 0; -void -XCenteredString(SDL_Surface *Surface, int y, char *text) -{ - XCenteredString2(Surface, &InternalFont, y, text); -} - -SDL_Surface *Back; -char tmp[1024]; + if(text == NULL) + return 0; -void -clearBuffer() { - SDL_Event event; + for(c = text; *c != '\0'; c++) { + charoffset = ((int) *c - 33) * 2 + 1; + // skip spaces and nonprintable characters + if (*c == ' ' || charoffset < 0 || charoffset > Font->MaxPos) { + width += Font->CharPos[2]-Font->CharPos[1]; + continue; + } + + width += Font->CharPos[charoffset+1] - Font->CharPos[charoffset]; + } - // Delete the event buffer - while (SDL_PollEvent(&event)) - ; - // start input - SDL_EnableUNICODE(1); + return width; } int -SFont_Input2( SDL_Surface *Dest, SFont_FontInfo *Font, int x, int y, int PixelWidth, char *text) +SFont_TextHeight(const SFont_Font* Font) { - SDL_Event event; - int ch; - SDL_Rect rect; - int ofs=(text[0]-33)*2+1; - int leftshift; - - if (ofs<0) { - leftshift = 0; - } - else { - leftshift = (Font->CharPos[ofs]-Font->CharPos[ofs-1])/2; - } - - rect.x=x-leftshift; - rect.y=y; - rect.w=PixelWidth; - rect.h=Font->Surface->h; - - //SDL_SetAlpha (Dest, SDL_SRCALPHA, 127); - - SDL_BlitSurface(Dest, &rect, Back, NULL); - sprintf(tmp,"%s_",text); - PutString2(Dest,Font,x,y,tmp); - SDL_UpdateRect(Dest, x-leftshift, y, PixelWidth, Font->h); - - while (SDL_PollEvent(&event) && event.type==SDL_KEYDOWN) { - - // Find the character pressed - ch=event.key.keysym.unicode; - - // If backspace and the length of the text > 0, reduce the string by 1 character - if (ch=='\b') { - if (strlen(text)>0) { - text[strlen(text)-1]='\0'; - } - } - else { - sprintf(text,"%s%c",text,ch); - } - - // If the new character would exceed the allowed width - if (TextWidth2(Font,text)>PixelWidth) { - text[strlen(text)-1]='\0'; - } - - //SDL_SetAlpha (Back, SDL_SRCALPHA, 127); - SDL_BlitSurface( Back, NULL, Dest, &rect); - PutString2(Dest, Font, x, y, text); - if (ofs>0) { - SDL_UpdateRect(Dest, x-(Font->CharPos[ofs]-Font->CharPos[ofs-1])/2, y, PixelWidth, Font->Surface->h); - } - - } - //text[strlen(text)-1]='\0'; - if (ch==SDLK_RETURN) { - SDL_FreeSurface(Back); - return 1; - } - else - return 0; + return Font->Surface->h - 1; } -int -SFont_Input( SDL_Surface *Dest, int x, int y, int PixelWidth, char *text) +void +SFont_WriteCenter(SDL_Surface *Surface, const SFont_Font *Font, + int y, const char *text) { - - Back = SDL_AllocSurface(Dest->flags, - PixelWidth, - InternalFont.h, - Dest->format->BitsPerPixel, - Dest->format->Rmask, - Dest->format->Gmask, - Dest->format->Bmask, 0); - - return SFont_Input2( Dest, &InternalFont, x, y, PixelWidth, text); - // ph: - // Returns 1 when the return key is pressed - // Returns 0 when nothing exceptional happened + SFont_Write(Surface, Font, Surface->w/2 - SFont_TextWidth(Font, text)/2, + y, text); } diff --git a/SFont.h b/SFont.h index 46fdc1b..1ba77da 100644 --- a/SFont.h +++ b/SFont.h @@ -1,48 +1,83 @@ +/* SFont: a simple font-library that uses special bitmaps as fonts + Copyright (C) 2003 Karl Bartel + + License: GPL or LGPL (at your choice) + WWW: http://www.linux-games.com/sfont/ + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Karl Bartel + Cecilienstr. 14 + 12307 Berlin + GERMANY + karlb@gmx.net +*/ + /************************************************************************ -* SFONT - SDL Font Library by Karl Bartel * +* SFONT - SDL Font Library by Karl Bartel * * * -* All functions are explained below. There are two versions of each * -* funtction. The first is the normal one, the function with the * -* 2 at the end can be used when you want to handle more than one font * -* in your program. * +* All functions are explained below. For further information, take a * +* look at the example files, the links at the SFont web site, or * +* contact me, if you problem isn' addressed anywhere. * * * ************************************************************************/ +#ifndef SFONT_H +#define SFONT_H + +#include -#include +#ifdef __cplusplus +extern "C" { +#endif // Delcare one variable of this type for each font you are using. // To load the fonts, load the font image into YourFont->Surface -// and call InitFont( YourFont ); +// and call SFont_InitFont(YourFont); typedef struct { SDL_Surface *Surface; int CharPos[512]; - int h; -} SFont_FontInfo; + int MaxPos; +} SFont_Font; // Initializes the font // Font: this contains the suface with the font. +// The Surface must be loaded before calling this function +SFont_Font* SFont_InitFont (SDL_Surface *Font); + +// Frees the font +// Font: The font to free // The font must be loaded before using this function. -void InitFont (SDL_Surface *Font); -void InitFont2(SFont_FontInfo *Font); +void SFont_FreeFont(SFont_Font* Font); // Blits a string to a surface // Destination: the suface you want to blit to // text: a string containing the text you want to blit. -void PutString (SDL_Surface *Surface, int x, int y, char *text); -void PutString2(SDL_Surface *Surface, SFont_FontInfo *Font, int x, int y, char *text); +void SFont_Write(SDL_Surface *Surface, const SFont_Font *Font, int x, int y, + const char *text); // Returns the width of "text" in pixels -int TextWidth(char *text); -int TextWidth2(SFont_FontInfo *Font, char *text); -int SFont_wide(char *text); - -// Blits a string to with centered x position -void XCenteredString (SDL_Surface *Surface, int y, char *text); -void XCenteredString2(SDL_Surface *Surface, SFont_FontInfo *Font, int y, char *text); - -// Allows the user to enter text -// Width: What is the maximum width of the text (in pixels) -// text: This string contains the text which was entered by the user -// ph: nonblocking -int SFont_Input ( SDL_Surface *Destination, int x, int y, int Width, char *text); -int SFont_Input2( SDL_Surface *Destination, SFont_FontInfo *Font, int x, int y, int Width, char *text); +int SFont_TextWidth(const SFont_Font* Font, const char *text); +// Returns the height of "text" in pixels (which is always equal to Font->Surface->h) +int SFont_TextHeight(const SFont_Font* Font); + +// Blits a string to Surface with centered x position +void SFont_WriteCenter(SDL_Surface *Surface, const SFont_Font* Font, int y, + const char *text); + +#ifdef __cplusplus +} +#endif + +#endif /* SFONT_H */ diff --git a/config.h b/config.h index ba41151..3bbb662 100644 --- a/config.h +++ b/config.h @@ -2,6 +2,7 @@ #define VOR_CONFIG_H #define VERSION "0.1" +#define DATA_PREFIX "/usr/share/vor" // screen size #define XSIZE 640 diff --git a/file.c b/file.c index b9bc7e2..a8f9ff4 100644 --- a/file.c +++ b/file.c @@ -61,7 +61,7 @@ find_data_dir(void) char *data_options[3] = { "./data", getenv("VOR_DATA"), - "/usr/share/vor" + DATA_PREFIX }; for(i=0; i<3; i++) { diff --git a/main.c b/main.c index 78c2ca0..e06a010 100644 --- a/main.c +++ b/main.c @@ -21,11 +21,9 @@ #undef DEBUG -extern int font_height; -void clearBuffer(); - #include "config.h" #include "file.h" +#include "score.h" #include "sound.h" #include @@ -81,21 +79,6 @@ 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 @@ -112,6 +95,8 @@ SDL_Surface *surf_deadrock[NROCKS], // THE DEAD ROCKS *surf_font_big; // The big font +SFont_Font *g_font; + // Structure global variables struct enginedots edot[MAXENGINEDOTS], *dotptr = edot; struct rock_struct rock[MAXROCKS], *rockptr = rock; @@ -182,58 +167,6 @@ 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() { return (float)random()/(float)RAND_MAX; @@ -504,7 +437,7 @@ create_engine_dots2(int newdots, int m) { void drawdots(SDL_Surface *s) { - int m, scorepos, n; + int m; SDL_LockSurface(s); // Draw the background stars aka space dots @@ -514,10 +447,8 @@ 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); } @@ -526,8 +457,7 @@ 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++) { @@ -609,7 +539,7 @@ init(int fullscreen) { NULLERROR(surf_b_over = SDL_DisplayFormat(temp)); surf_font_big = IMG_Load(load_file(BIG_FONT_FILE)); - InitFont(surf_font_big); + g_font = SFont_InitFont(surf_font_big); // Load the spaceship graphic. NULLERROR(temp = IMG_Load(load_file("sprites/ship.png"))); @@ -775,62 +705,33 @@ draw() { 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); + x = (XSIZE-SFont_TextWidth(g_font,text))/2 + sin(fadetimer/4.5)*10; + SFont_Write(surf_screen,g_font,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); + x = (XSIZE-SFont_TextWidth(g_font,text))/2 + cos(fadetimer/4.5)*10; + SFont_Write(surf_screen,g_font,x,YSIZE-100 + cos(fadetimer/3)*5,text); break; case HIGH_SCORE_ENTRY: + play_tune(2); + if(!process_score_input()) { // done inputting name - 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 { + // 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); } @@ -906,45 +807,29 @@ 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; diff --git a/score.c b/score.c new file mode 100644 index 0000000..ac619dd --- /dev/null +++ b/score.c @@ -0,0 +1,155 @@ +#include "config.h" + +#include "SFont.h" +#include "file.h" +#include "score.h" + +#include +#include +#include +#include +#include +#include + +// High score table +struct highscore g_scores[N_SCORES] = { + {13000,"Pad"}, + {12500,"Pad"}, + {6500,"Pad"}, + {5000,"Pad"}, + {3000,"Pad"}, + {2500,"Pad"}, + {2000,"Pad"}, + {1500,"Pad"} +}; + +extern SFont_Font *g_font; + +int cur_score; // which score we're currently entering. + +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 g_scores[i].score) return i; + } + return -1; +} + +int +new_high_score(int score) +{ + int i; + + cur_score = -1; // assume not a new high score + if(score <= g_scores[LOW_SCORE].score) return false; + read_high_score_table(); + cur_score = score_rank(score); + if(cur_score < 0) return false; + + // Move all lower scores down a notch, losing lowest score forever. + for(i=LOW_SCORE; i>cur_score; i--) g_scores[i] = g_scores[i-1]; + + // initialize new score entry. + g_scores[cur_score].score = score; + for(i=0; i<32; i++) g_scores[cur_score].name[i] = 0; + return true; +} + +int +snprintscore(char *s, size_t n, int score) +{ + int min = score/60000; + int sec = score/1000%60; + int tenths = score%1000/100; + if(min) { + return snprintf(s, n, "%2d:%.2d.%d", min, sec, tenths); + } else { + return snprintf(s, n, "%2d.%d", sec, tenths); + } +} + +int +snprintscore_line(char *s, size_t n, int score) +{ + int r = snprintf(s, n, "Time: "); + return r + snprintscore(s+r, n-r, score); +} + +void +display_scores(SDL_Surface *s, uint32_t x, uint32_t y) +{ + char t[1024]; + int i,h = SFont_TextHeight(g_font); + + SFont_Write(s,g_font,x+30,y,"High scores"); + y += h; + for(i = 0; i 0) name[n--]=0; + else if(e.key.keysym.sym == SDLK_RETURN) { + SDL_EnableUNICODE(0); + return false; + } + else name[n++] = c; + } + return true; +} diff --git a/score.h b/score.h new file mode 100644 index 0000000..478afca --- /dev/null +++ b/score.h @@ -0,0 +1,44 @@ +/* Variations on RockDodger + * Copyright (C) 2004 Joshua Grams + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef VOR_SCORE_H +#define VOR_SCORE_H + +#include +#include +#include + +#define N_SCORES 8 +#define LOW_SCORE (N_SCORES-1) + +struct highscore { + int score; + char name[32]; +}; + +extern struct highscore g_scores[N_SCORES]; + +void read_high_score_table(void); +void write_high_score_table(void); +int snprintscore(char *s, size_t n, int score); +int snprintscore_line(char *s, size_t n, int score); +void display_scores(SDL_Surface *s, uint32_t x, uint32_t y); +int new_high_score(int score); +int process_score_input(void); + +#endif // VOR_SCORE_H -- 1.7.10.4