X-Git-Url: https://jasonwoof.com/gitweb/?p=vor.git;a=blobdiff_plain;f=score.c;fp=score.c;h=ac619dd58cac9138fd2222465c1ff75f7f67b391;hp=0000000000000000000000000000000000000000;hb=6f82bba8833f187abebab53375ac87905487cf8c;hpb=0ebbb01f731d0c9bb71140a93cfddd9abde509e5 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; +}