From edde9fac2fdc592435679614d7dba641f78b3162 Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 19 Feb 2018 22:32:14 +0000 Subject: [PATCH] Integrate trophy display. --- src/game/stats.c | 2 +- src/game/stats.h | 1 - src/game/trophies.c | 119 +++++++++++++++++++++++++++++++++++++++++++- src/game/trophies.h | 10 ++++ src/hub/hub.c | 19 ++++--- src/hub/hub.h | 2 + src/system/atlas.h | 2 - src/util/util.c | 13 +++++ src/util/util.h | 1 + src/world/world.c | 21 ++++++-- src/world/world.h | 2 + 11 files changed, 178 insertions(+), 14 deletions(-) diff --git a/src/game/stats.c b/src/game/stats.c index 12390c8..e01e8e0 100644 --- a/src/game/stats.c +++ b/src/game/stats.c @@ -70,7 +70,7 @@ void doStats(void) if (isControl(CONTROL_RIGHT) || app.keyboard[SDL_SCANCODE_RIGHT]) { page = limit(page + 1, 0, maxPages - 1); - app.keyboard[SDL_SCANCODE_LEFT] = 0; + app.keyboard[SDL_SCANCODE_RIGHT] = 0; clearControl(CONTROL_RIGHT); } diff --git a/src/game/stats.h b/src/game/stats.h index 50e6a3b..9a5d592 100644 --- a/src/game/stats.h +++ b/src/game/stats.h @@ -31,7 +31,6 @@ extern float limit(float i, float low, float high); extern int isControl(int type); extern void clearControl(int type); extern Atlas *getImageFromAtlas(char *filename); -extern void blitRectScaled(SDL_Texture *texture, int x, int y, int w, int h, SDL_Rect *srcRect, int center); extern Texture *getTexture(const char *filename); extern void blitRect(SDL_Texture *texture, int x, int y, SDL_Rect *srcRect, int center); extern void doWidgets(void); diff --git a/src/game/trophies.c b/src/game/trophies.c index 91b698c..27864ce 100644 --- a/src/game/trophies.c +++ b/src/game/trophies.c @@ -35,6 +35,10 @@ static Atlas *sparkle; static Atlas *alertSphere; static Texture *atlasTexture; static int awarded; +static Atlas *left; +static Atlas *right; +static int page; +static float maxPages; void initTrophies(void) { @@ -47,6 +51,8 @@ void initTrophies(void) trophyIcons[TROPHY_UNEARNED] = getImageFromAtlas("gfx/trophies/unearned.png"); sparkle = getImageFromAtlas("gfx/trophies/sparkle.png"); alertSphere = getImageFromAtlas("gfx/trophies/alertSphere.png"); + left = getImageFromAtlas("gfx/ui/left.png"); + right = getImageFromAtlas("gfx/ui/right.png"); alertRect.h = 90; alertRect.y = 10; @@ -56,12 +62,124 @@ void initTrophies(void) awarded = 0; sparkleAngle = 0; + + page = 0; + + maxPages = STAT_TIME_PLAYED; + maxPages /= TROPHIES_PER_PAGE; + maxPages = ceil(maxPages); loadTrophyData(); resetAlert(); } +void doTrophies(void) +{ + if (isControl(CONTROL_LEFT) || app.keyboard[SDL_SCANCODE_LEFT]) + { + page = limit(page - 1, 0, maxPages - 1); + app.keyboard[SDL_SCANCODE_LEFT] = 0; + clearControl(CONTROL_LEFT); + } + + if (isControl(CONTROL_RIGHT) || app.keyboard[SDL_SCANCODE_RIGHT]) + { + page = limit(page + 1, 0, maxPages - 1); + app.keyboard[SDL_SCANCODE_RIGHT] = 0; + clearControl(CONTROL_RIGHT); + } + + doWidgets(); +} + +void drawTrophies(void) +{ + Trophy *t; + SDL_Rect r; + int start, end, i, x, y; + + drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0, 128); + + r.w = 600; + r.h = 650; + r.x = (SCREEN_WIDTH / 2) - r.w / 2; + r.y = (SCREEN_HEIGHT / 2) - r.h / 2; + + r.y += 15; + + drawRect(r.x, r.y, r.w, r.h, 0, 0, 0, 192); + + drawOutlineRect(r.x, r.y, r.w, r.h, 200, 200, 200, 255); + + drawText(SCREEN_WIDTH / 2, 60, 28, TA_CENTER, colors.white, "Trophies"); + + drawText(SCREEN_WIDTH / 2, 100, 16, TA_CENTER, colors.lightGrey, "Page %d / %d", page + 1, (int)maxPages); + + if (page > 0) + { + blitRect(atlasTexture->texture, SCREEN_WIDTH / 2 - 100, 110, &left->rect, 1); + } + + if (page < maxPages - 1) + { + blitRect(atlasTexture->texture, SCREEN_WIDTH / 2 + 100, 110, &right->rect, 1); + } + + x = r.x + 15; + y = 160; + start = page * TROPHIES_PER_PAGE; + end = start + TROPHIES_PER_PAGE; + i = 0; + + for (t = game.trophyHead.next ; t != NULL ; t = t->next) + { + if (i >= start && i < end) + { + if (t->awardDate) + { + setSparkleColor(t); + blitRectRotated(atlasTexture->texture, x + 32, y + 32, &sparkle->rect, sparkleAngle); + blitRectRotated(atlasTexture->texture, x + 32, y + 32, &sparkle->rect, -sparkleAngle); + + blitRectScaled(atlasTexture->texture, x, y, 64, 64, &trophyIcons[t->value]->rect, 0); + drawText(x + 85, y - 10, 20, TA_LEFT, colors.yellow, t->title); + drawText(x + 85, y + 20, 18, TA_LEFT, colors.white, t->description); + + if (strlen(t->awardDateStr) == 0) + { + STRNCPY(t->awardDateStr, timeToDate(t->awardDate), MAX_NAME_LENGTH); + } + + drawText(x + 85, y + 48, 18, TA_LEFT, colors.white, t->awardDateStr); + } + else + { + blitRectScaled(atlasTexture->texture, x, y, 64, 64, &trophyIcons[TROPHY_UNEARNED]->rect, 0); + + if (!t->hidden) + { + drawText(x + 85, y - 10, 20, TA_LEFT, colors.lightGrey, t->title); + drawText(x + 85, y + 20, 18, TA_LEFT, colors.darkGrey, t->description); + drawText(x + 85, y + 48, 18, TA_LEFT, colors.darkGrey, "-"); + } + else + { + drawText(x + 85, y + 20, 20, TA_LEFT, colors.darkGrey, "Hidden"); + } + } + + y += 120; + } + + i++; + } + + SDL_SetTextureColorMod(atlasTexture->texture, 255, 255, 255); + + drawWidgets(); +} + void awardTrophy(char *id) { Trophy *t; @@ -211,7 +329,6 @@ void drawTrophyAlert(void) blitRectRotated(atlasTexture->texture, x + 24, y + 24, &sparkle->rect, sparkleAngle); blitRectRotated(atlasTexture->texture, x + 24, y + 24, &sparkle->rect, -sparkleAngle); blitRectScaled(atlasTexture->texture, x, y, 48, 48, &trophyIcons[alertTrophy->value]->rect, 0); - SDL_SetTextureColorMod(atlasTexture->texture, 255, 255, 255); } } diff --git a/src/game/trophies.h b/src/game/trophies.h index ba91022..2be82de 100644 --- a/src/game/trophies.h +++ b/src/game/trophies.h @@ -22,6 +22,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "../json/cJSON.h" #include +#define TROPHIES_PER_PAGE 4 + extern char *readFile(const char *filename); extern long lookup(const char *name); extern void textSize(char *text, int size, int *w, int *h); @@ -36,6 +38,14 @@ extern void blitRectRotated(SDL_Texture *texture, int x, int y, SDL_Rect *srcRec extern Texture *getTexture(const char *filename); extern void blitRectScaled(SDL_Texture *texture, int x, int y, int w, int h, SDL_Rect *srcRect, int center); extern void saveScreenshot(char *name); +extern void drawWidgets(void); +extern char *timeToString(int seconds, int showHours); +extern float limit(float i, float low, float high); +extern int isControl(int type); +extern void clearControl(int type); +extern void doWidgets(void); +extern char *timeToDate(long millis); +extern App app; extern Colors colors; extern Game game; diff --git a/src/hub/hub.c b/src/hub/hub.c index 20fb216..e1fa79a 100644 --- a/src/hub/hub.c +++ b/src/hub/hub.c @@ -38,7 +38,7 @@ static void options(void); static void stats(void); static void trophies(void); static void quit(void); -static void returnFromStats(void); +static void returnFromTrophyStats(void); static void doCursor(void); static void doMissionSelect(void); static void doMissionInfo(void); @@ -104,7 +104,8 @@ void initHub(void) getWidget("trophies", "hub")->action = trophies; getWidget("quit", "hub")->action = quit; - getWidget("ok", "stats")->action = returnFromStats; + getWidget("ok", "stats")->action = returnFromTrophyStats; + getWidget("ok", "trophies")->action = returnFromTrophyStats; loadMissions(); @@ -238,15 +239,19 @@ static void logic(void) break; case SHOW_STATS: - drawStats(); doStats(); if (app.keyboard[SDL_SCANCODE_ESCAPE]) { - returnFromStats(); + returnFromTrophyStats(); } break; case SHOW_TROPHIES: + doTrophies(); + if (app.keyboard[SDL_SCANCODE_ESCAPE]) + { + returnFromTrophyStats(); + } break; default: @@ -361,6 +366,7 @@ static void draw(void) break; case SHOW_TROPHIES: + drawTrophies(); break; } } @@ -637,7 +643,8 @@ static void stats(void) static void trophies(void) { - + showing = SHOW_TROPHIES; + showWidgetGroup("trophies"); } static void quit(void) @@ -645,7 +652,7 @@ static void quit(void) } -static void returnFromStats(void) +static void returnFromTrophyStats(void) { showWidgetGroup("hub"); showing = SHOW_WIDGETS; diff --git a/src/hub/hub.h b/src/hub/hub.h index 7dcbbc1..9699206 100644 --- a/src/hub/hub.h +++ b/src/hub/hub.h @@ -65,6 +65,8 @@ extern void doWidgets(void); extern void drawStats(void); extern void doStats(void); extern void awardTrophy(char *id); +extern void doTrophies(void); +extern void drawTrophies(void); extern App app; extern Colors colors; diff --git a/src/system/atlas.h b/src/system/atlas.h index b5c37e3..4e9f922 100644 --- a/src/system/atlas.h +++ b/src/system/atlas.h @@ -21,8 +21,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "../common.h" #include "../json/cJSON.h" -extern Texture *getTexture(const char *filename); extern char *readFile(const char *filename); extern unsigned long hashcode(const char *str); -extern Dev dev; diff --git a/src/util/util.c b/src/util/util.c index 4035822..0a6ca1a 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -81,6 +81,19 @@ char *timeToString(int seconds, int showHours) return TIME_STRING; } +char *timeToDate(long millis) +{ + static char DATE[MAX_NAME_LENGTH]; + + struct tm *timeinfo; + + timeinfo = localtime(&millis); + + strftime(DATE, MAX_NAME_LENGTH, "%d %b %Y, %I:%M%p", timeinfo); + + return DATE; +} + void *resize(void *array, int oldSize, int newSize) { void **newArray; diff --git a/src/util/util.h b/src/util/util.h index 8ad0dc9..68290cd 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -19,3 +19,4 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "../common.h" +#include "time.h" diff --git a/src/world/world.c b/src/world/world.c index 3824136..73925fd 100644 --- a/src/world/world.c +++ b/src/world/world.c @@ -42,7 +42,7 @@ static void options(void); static void stats(void); static void trophies(void); static void quit(void); -static void returnFromStats(void); +static void returnFromTrophyStats(void); int getMissionStatus(void); static Texture *background; @@ -191,6 +191,10 @@ static void draw(void) case SHOW_STATS: drawStats(); break; + + case SHOW_TROPHIES: + drawTrophies(); + break; } } @@ -350,7 +354,16 @@ static void doWorldInProgress(void) if (app.keyboard[SDL_SCANCODE_ESCAPE]) { - returnFromStats(); + returnFromTrophyStats(); + } + } + else if (showing == SHOW_TROPHIES) + { + doTrophies(); + + if (app.keyboard[SDL_SCANCODE_ESCAPE]) + { + returnFromTrophyStats(); } } } @@ -699,13 +712,15 @@ static void stats(void) static void trophies(void) { + showing = SHOW_TROPHIES; + showWidgetGroup("trophies"); } static void quit(void) { } -static void returnFromStats(void) +static void returnFromTrophyStats(void) { showWidgetGroup("hub"); showing = SHOW_WIDGETS; diff --git a/src/world/world.h b/src/world/world.h index d9e4fc7..af735f4 100644 --- a/src/world/world.h +++ b/src/world/world.h @@ -99,6 +99,8 @@ extern void initPostMission(void); extern void addKeysFromStash(void); extern void drawStats(void); extern void doStats(void); +extern void doTrophies(void); +extern void drawTrophies(void); extern App app; extern Colors colors;