Completes #33 Create score screen

This commit is contained in:
Linus Probert 2018-08-12 19:43:33 +02:00
parent 254417271c
commit 369cfda70b
5 changed files with 184 additions and 39 deletions

View File

@ -44,18 +44,29 @@ DbQuery MIGRATE_COMMANDS[] = {
}; };
static int static int
load_hiscore_cb(void *unused, int count, char **values, char **colNames); load_hiscore_cb(void *, int count, char **values, char **colNames);
static static
DbQuery GET_COMMANDS[] = { DbQuery GET_TOP_10_COMMAND = {
{ "SELECT datetime(time, 'localtime') as time, gold, playerLevel, dungeonLevel "
"SELECT datetime(time, 'localtime') as time, gold, playerLevel, dungeonLevel " "FROM hiscore "
"FROM hiscore " "ORDER BY gold DESC "
"ORDER BY gold DESC " "LIMIT 10",
"LIMIT 10", load_hiscore_cb,
load_hiscore_cb, NULL NULL
}, };
{ NULL }
static int
load_top_gold_cb(void *, int count, char **values, char **colNames);
static
DbQuery GET_TOP_GOLD_COMMAND = {
"SELECT gold "
"FROM hiscore "
"ORDER BY gold DESC "
"LIMIT 1",
load_top_gold_cb,
NULL
}; };
static sqlite3 *db = NULL; static sqlite3 *db = NULL;
@ -152,18 +163,36 @@ LinkedList *
hiscore_get_top10(void) hiscore_get_top10(void)
{ {
LinkedList *scores = linkedlist_create(); LinkedList *scores = linkedlist_create();
for (unsigned int i = 0;; ++i) { DbQuery *query = &GET_TOP_10_COMMAND;
DbQuery *query = &GET_COMMANDS[i]; query->cb_arg = &scores;
query->cb_arg = &scores; db_execute(db, query);
if (query->stmt == NULL)
break;
db_execute(db, query);
}
return scores; return scores;
} }
static int
load_top_gold_cb(void *result, int count, char **values, char **colNames)
{
UNUSED(count);
UNUSED(colNames);
double *gold = result;
*gold = atof(values[0]);
return 0;
}
double
hiscore_get_top_gold(void)
{
double result;
DbQuery *query = &GET_TOP_GOLD_COMMAND;
query->cb_arg = &result;
db_execute(db, query);
return result;
}
void void
hiscore_close(void) hiscore_close(void)
{ {

View File

@ -38,6 +38,9 @@ hiscore_register(Player *p, unsigned int dungeonLevel);
LinkedList * LinkedList *
hiscore_get_top10(void); hiscore_get_top10(void);
double
hiscore_get_top_gold(void);
void void
hiscore_close(void); hiscore_close(void);

View File

@ -70,6 +70,7 @@ static Menu *inGameMenu = NULL;
static Timer *menuTimer = NULL; static Timer *menuTimer = NULL;
static Camera *gCamera = NULL; static Camera *gCamera = NULL;
static Screen *creditsScreen = NULL; static Screen *creditsScreen = NULL;
static Screen *scoreScreen = NULL;
static unsigned int cLevel = 1; static unsigned int cLevel = 1;
static float deltaTime = 1.0; static float deltaTime = 1.0;
static double renderScale = 1.0; static double renderScale = 1.0;
@ -321,11 +322,19 @@ viewCredits(void *unused)
gGameState = CREDITS; gGameState = CREDITS;
} }
static void
viewScoreScreen(void *unused)
{
UNUSED(unused);
gGameState = SCORE_SCREEN;
}
static void static void
initMainMenu(void) initMainMenu(void)
{ {
struct MENU_ITEM menu_items[] = { struct MENU_ITEM menu_items[] = {
{ "PLAY", startGame }, { "PLAY", startGame },
{ "SCORES", viewScoreScreen },
{ "CREDITS", viewCredits }, { "CREDITS", viewCredits },
{ "QUIT", exitGame }, { "QUIT", exitGame },
}; };
@ -335,9 +344,10 @@ initMainMenu(void)
gMap = map_lua_generator_single_room__run(cLevel, gRenderer); gMap = map_lua_generator_single_room__run(cLevel, gRenderer);
createMenu(&mainMenu, menu_items, 3); createMenu(&mainMenu, menu_items, 4);
mixer_play_music(MENU_MUSIC); mixer_play_music(MENU_MUSIC);
creditsScreen = screen_create_credits(gRenderer); creditsScreen = screen_create_credits(gRenderer);
scoreScreen = screen_create_hiscore(gRenderer);
} }
static void static void
@ -362,6 +372,10 @@ resetGame(void)
screen_destroy(creditsScreen); screen_destroy(creditsScreen);
creditsScreen = NULL; creditsScreen = NULL;
if (scoreScreen)
screen_destroy(scoreScreen);
scoreScreen = NULL;
if (!inGameMenu) if (!inGameMenu)
initInGameMenu(); initInGameMenu();
@ -414,6 +428,8 @@ handle_main_input(void)
if (gGameState == CREDITS && input_key_is_pressed(&input, KEY_ESC)) if (gGameState == CREDITS && input_key_is_pressed(&input, KEY_ESC))
gGameState = MENU; gGameState = MENU;
else if (gGameState == SCORE_SCREEN && input_key_is_pressed(&input, KEY_ESC))
gGameState = MENU;
else if (gGameState == MENU && input_key_is_pressed(&input, KEY_ESC)) else if (gGameState == MENU && input_key_is_pressed(&input, KEY_ESC))
gGameState = QUIT; gGameState = QUIT;
@ -602,12 +618,19 @@ run_game(void)
if (gGameState == PLAYING && is_player_dead()) { if (gGameState == PLAYING && is_player_dead()) {
camera_shake(VECTOR2D_RIGHT, 800); camera_shake(VECTOR2D_RIGHT, 800);
gui_log("The dungeon consumed you"); gui_log("The dungeon consumed you");
gui_log("You earned %.2f gold", gPlayer->gold);
gui_event_message("You died!"); gui_event_message("You died!");
gui_event_message("You earned %.2f gold", gPlayer->gold);
if (hiscore_get_top_gold() < gPlayer->gold) {
gui_event_message("NEW HIGHSCORE");
gui_log("NEW HIGHSCORE");
}
gui_event_message("Press ESC to open menu"); gui_event_message("Press ESC to open menu");
mixer_play_effect(SPLAT); mixer_play_effect(SPLAT);
gGameState = GAME_OVER; gGameState = GAME_OVER;
createInGameGameOverMenu(); createInGameGameOverMenu();
hiscore_register(gPlayer, cLevel); hiscore_register(gPlayer, cLevel);
} else { } else {
check_next_level(); check_next_level();
} }
@ -628,7 +651,7 @@ run_menu(void)
} }
menu_update(mainMenu, &input); menu_update(mainMenu, &input);
if (gGameState != MENU && gGameState != CREDITS) if (gGameState != MENU && gGameState != CREDITS && gGameState != SCORE_SCREEN)
return; return;
SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 0); SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 0);
@ -645,6 +668,8 @@ run_menu(void)
menu_render(mainMenu, gCamera); menu_render(mainMenu, gCamera);
else if (gGameState == CREDITS) else if (gGameState == CREDITS)
screen_render(creditsScreen, gCamera); screen_render(creditsScreen, gCamera);
else if (gGameState == SCORE_SCREEN)
screen_render(scoreScreen, gCamera);
pointer_render(gPointer, gCamera); pointer_render(gPointer, gCamera);
@ -676,6 +701,7 @@ void run(void)
break; break;
case MENU: case MENU:
case CREDITS: case CREDITS:
case SCORE_SCREEN:
run_menu(); run_menu();
break; break;
case QUIT: case QUIT:
@ -714,6 +740,8 @@ void close(void)
menu_destroy(mainMenu); menu_destroy(mainMenu);
if (creditsScreen) if (creditsScreen)
screen_destroy(creditsScreen); screen_destroy(creditsScreen);
if (scoreScreen)
screen_destroy(scoreScreen);
if (inGameMenu) if (inGameMenu)
menu_destroy(inGameMenu); menu_destroy(inGameMenu);

View File

@ -18,6 +18,7 @@
#include "screen.h" #include "screen.h"
#include "util.h" #include "util.h"
#include "hiscore.h"
static Screen * static Screen *
screen_create(void) screen_create(void)
@ -29,11 +30,23 @@ screen_create(void)
} }
static Sprite * static Sprite *
create_text_sprite(const char *msg, int x, int y, SDL_Renderer *renderer) credit_txt(const char *msg, SDL_Color color, int x, int y, SDL_Renderer *renderer)
{ {
Sprite *s = sprite_create(); Sprite *s = sprite_create();
sprite_load_text_texture(s, "GUI/SDS_8x8.ttf", 0, 14, 1); sprite_load_text_texture(s, "GUI/SDS_8x8.ttf", 0, 14, 1);
texture_load_from_text(s->textures[0], msg, C_WHITE, C_BLACK, renderer); texture_load_from_text(s->textures[0], msg, color, C_BLACK, renderer);
s->pos = (Position) { x, y };
s->fixed = true;
s->dim = s->textures[0]->dim;
return s;
}
static Sprite *
score_txt(const char *msg, SDL_Color color, int x, int y, SDL_Renderer *renderer)
{
Sprite *s = sprite_create();
sprite_load_text_texture(s, "GUI/SDS_8x8.ttf", 0, 10, 1);
texture_load_from_text(s->textures[0], msg, color, C_BLACK, renderer);
s->pos = (Position) { x, y }; s->pos = (Position) { x, y };
s->fixed = true; s->fixed = true;
s->dim = s->textures[0]->dim; s->dim = s->textures[0]->dim;
@ -48,37 +61,106 @@ screen_create_credits(SDL_Renderer *renderer)
unsigned int columnOffset = 160; unsigned int columnOffset = 160;
Screen *screen = screen_create(); Screen *screen = screen_create();
linkedlist_push(&screen->sprites, create_text_sprite("- Game -", x, y, renderer)); linkedlist_push(&screen->sprites, credit_txt("- Game -", C_BLUE, x, y, renderer));
y += 30; y += 30;
linkedlist_push(&screen->sprites, create_text_sprite("Code:", x, y, renderer)); linkedlist_push(&screen->sprites, credit_txt("Code:", C_YELLOW, x, y, renderer));
linkedlist_push(&screen->sprites, create_text_sprite("Linus Probert", x + columnOffset, y, renderer)); linkedlist_push(&screen->sprites, credit_txt("Linus Probert", C_WHITE, x + columnOffset, y, renderer));
y += 20; y += 20;
linkedlist_push(&screen->sprites, create_text_sprite("liquidityc.github.io", x + columnOffset, y, renderer)); linkedlist_push(&screen->sprites, credit_txt("liquidityc.github.io", C_WHITE, x + columnOffset, y, renderer));
y += 20; y += 20;
linkedlist_push(&screen->sprites, create_text_sprite("@LiquidityC", x + columnOffset, y, renderer)); linkedlist_push(&screen->sprites, credit_txt("@LiquidityC", C_WHITE, x + columnOffset, y, renderer));
y += 60; y += 60;
linkedlist_push(&screen->sprites, create_text_sprite(" - Graphics -", x, y, renderer)); linkedlist_push(&screen->sprites, credit_txt(" - Graphics -", C_BLUE, x, y, renderer));
y += 30; y += 30;
linkedlist_push(&screen->sprites, create_text_sprite("Palette:", x, y, renderer)); linkedlist_push(&screen->sprites, credit_txt("Palette:", C_YELLOW, x, y, renderer));
linkedlist_push(&screen->sprites, create_text_sprite("DawnBringer", x + columnOffset, y, renderer)); linkedlist_push(&screen->sprites, credit_txt("DawnBringer", C_WHITE, x + columnOffset, y, renderer));
y += 60; y += 60;
linkedlist_push(&screen->sprites, create_text_sprite(" - Music and Sound -", x, y, renderer)); linkedlist_push(&screen->sprites, credit_txt(" - Music and Sound -", C_BLUE, x, y, renderer));
y += 30; y += 30;
linkedlist_push(&screen->sprites, create_text_sprite("Music:", x, y, renderer)); linkedlist_push(&screen->sprites, credit_txt("Music:", C_YELLOW, x, y, renderer));
linkedlist_push(&screen->sprites, create_text_sprite("Eric Matyas", x + columnOffset, y, renderer)); linkedlist_push(&screen->sprites, credit_txt("Eric Matyas", C_WHITE, x + columnOffset, y, renderer));
y += 20; y += 20;
linkedlist_push(&screen->sprites, create_text_sprite("www.soundimage.org", x + columnOffset, y, renderer)); linkedlist_push(&screen->sprites, credit_txt("www.soundimage.org", C_WHITE, x + columnOffset, y, renderer));
y += 30; y += 30;
linkedlist_push(&screen->sprites, create_text_sprite("Sound:", x, y, renderer)); linkedlist_push(&screen->sprites, credit_txt("Sound:", C_YELLOW, x, y, renderer));
linkedlist_push(&screen->sprites, create_text_sprite("Eric Matyas", x + columnOffset, y, renderer)); linkedlist_push(&screen->sprites, credit_txt("Eric Matyas", C_WHITE, x + columnOffset, y, renderer));
y += 20; y += 20;
linkedlist_push(&screen->sprites, create_text_sprite("www.soundimage.org", x + columnOffset, y, renderer)); linkedlist_push(&screen->sprites, credit_txt("www.soundimage.org", C_WHITE, x + columnOffset, y, renderer));
y += 30; y += 30;
linkedlist_push(&screen->sprites, create_text_sprite("ArtisticDuded", x + columnOffset, y, renderer)); linkedlist_push(&screen->sprites, credit_txt("ArtisticDuded", C_WHITE, x + columnOffset, y, renderer));
y += 20; y += 20;
linkedlist_push(&screen->sprites, create_text_sprite("opengameart.org/users/artisticdude", x + columnOffset, y, renderer)); linkedlist_push(&screen->sprites, credit_txt("opengameart.org/users/artisticdude", C_WHITE, x + columnOffset, y, renderer));
return screen;
}
Screen *
screen_create_hiscore(SDL_Renderer *renderer)
{
Screen *screen = screen_create();
int y = 40;
int dateCol = 50;
int goldCol = 250;
int lvlCol = 400;
int dlvlCol = 550;
linkedlist_push(&screen->sprites, score_txt("Date",
C_GREEN,
dateCol,
y,
renderer));
linkedlist_push(&screen->sprites, score_txt("Gold",
C_GREEN,
goldCol,
y,
renderer));
linkedlist_push(&screen->sprites, score_txt("Level",
C_GREEN,
lvlCol,
y,
renderer));
linkedlist_push(&screen->sprites, score_txt("Depth",
C_GREEN,
dlvlCol,
y,
renderer));
LinkedList *scores = hiscore_get_top10();
while (scores) {
y += 30;
HiScore *score = linkedlist_pop(&scores);
char content[80];
m_sprintf(content, 80, "%s", score->timestamp);
linkedlist_push(&screen->sprites,
score_txt(content,
C_WHITE,
dateCol,
y,
renderer));
m_sprintf(content, 80, "%.2lf", score->gold);
linkedlist_push(&screen->sprites,
score_txt(content,
C_YELLOW,
goldCol,
y,
renderer));
m_sprintf(content, 80, "%d", score->playerLevel);
linkedlist_push(&screen->sprites,
score_txt(content,
C_BLUE,
lvlCol,
y,
renderer));
m_sprintf(content, 80, "%d", score->dungeonLevel);
linkedlist_push(&screen->sprites,
score_txt(content,
C_RED,
dlvlCol,
y,
renderer));
hiscore_destroy(score);
}
return screen; return screen;
} }

View File

@ -30,6 +30,9 @@ typedef struct Screen {
Screen * Screen *
screen_create_credits(SDL_Renderer*); screen_create_credits(SDL_Renderer*);
Screen *
screen_create_hiscore(SDL_Renderer*);
void void
screen_render(Screen *screen, Camera *cam); screen_render(Screen *screen, Camera *cam);