From 64c0a2bb741557ab89e887058945779373af2259 Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 19 Mar 2018 08:08:29 +0000 Subject: [PATCH] Start of title screen. --- src/game/title.c | 74 ++++++++++++++++++++++++++++++++++++++++++++ src/game/title.h | 18 ++++++++++- src/main.c | 11 +++++-- src/main.h | 1 + src/structs.h | 1 + src/system/io.c | 9 ++++++ src/system/widgets.c | 24 ++++++++++++-- 7 files changed, 132 insertions(+), 6 deletions(-) diff --git a/src/game/title.c b/src/game/title.c index fa681ee..69e37c7 100644 --- a/src/game/title.c +++ b/src/game/title.c @@ -22,17 +22,91 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. static void logic(void); static void draw(void); +static int getRecentSave(void); + +static Texture *atlasTexture; +static Atlas *title; +static int recentSaveSlot; +static Widget *newGame; +static Widget *loadGame; +static Widget *continueGame; +static Widget *options; +static Widget *credits; +static Widget *quit; void initTitle(void) { + startSectionTransition(); + + atlasTexture = getTexture("gfx/atlas/atlas.png"); + + title = getImageFromAtlas("gfx/main/title.png"); + + newGame = getWidget("new", "title"); + loadGame = getWidget("load", "title"); + continueGame = getWidget("continue", "title"); + options = getWidget("options", "title"); + credits = getWidget("credits", "title"); + quit = getWidget("exit", "title"); + + recentSaveSlot = getRecentSave(); + + showWidgetGroup("title"); + + if (recentSaveSlot != -1) + { + setSelectedWidget("continue", "title"); + } + else + { + loadGame->disabled = 1; + continueGame->disabled = 1; + } + app.delegate.logic = &logic; app.delegate.draw = &draw; + + endSectionTransition(); } static void logic(void) { + doWidgets(); } static void draw(void) { + blitRect(atlasTexture->texture, SCREEN_WIDTH / 2, 175, &title->rect, 1); + + drawText(10, SCREEN_HEIGHT - 30, 18, TA_LEFT, colors.white, "Copyright 2014, 2018 Parallel Realities"); + drawText(SCREEN_WIDTH - 10, SCREEN_HEIGHT - 30, 18, TA_RIGHT, colors.white, "Version %.2f.%d", VERSION, REVISION); + + drawWidgets(); +} + +static int getRecentSave(void) +{ + char filename[MAX_FILENAME_LENGTH]; + int i, slot, curModTime, modTime; + + slot = -1; + modTime = 0; + + for (i = 0 ; i < MAX_SAVE_SLOTS ; i++) + { + sprintf(filename, "%s/%d/game.json", app.saveDir, i); + + if (fileExists(filename)) + { + curModTime = getFileModTime(filename); + + if (curModTime > modTime) + { + modTime = curModTime; + slot = i; + } + } + } + + return slot; } diff --git a/src/game/title.h b/src/game/title.h index 20d7193..5d390fa 100644 --- a/src/game/title.h +++ b/src/game/title.h @@ -20,5 +20,21 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "../common.h" -extern App app; +extern void blitRect(SDL_Texture *texture, int x, int y, SDL_Rect *srcRect, int center); +extern void doWidgets(void); +extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...); +extern void drawWidgets(void); +extern void endSectionTransition(void); +extern Atlas *getImageFromAtlas(char *filename); +extern Texture *getTexture(const char *filename); +extern Widget *getWidget(char *name, char *group); +extern void playSound(int snd, int ch); +extern void saveConfig(void); +extern void showWidgetGroup(char *group); +extern void startSectionTransition(void); +extern long getFileModTime(char *filename); +extern int fileExists(const char *filename); +extern void setSelectedWidget(char *name, char *group); +extern App app; +extern Colors colors; diff --git a/src/main.c b/src/main.c index 2edb02b..908b10f 100644 --- a/src/main.c +++ b/src/main.c @@ -183,6 +183,13 @@ static void handleCommandLine(int argc, char *argv[]) return; } } - - initWorldTest(worldId); + + if (worldId != NULL) + { + initWorldTest(worldId); + } + else + { + initTitle(); + } } diff --git a/src/main.h b/src/main.h index c6cfeae..9ba51a4 100644 --- a/src/main.h +++ b/src/main.h @@ -38,6 +38,7 @@ extern void prepareScene(void); extern void presentScene(void); extern void saveScreenshot(char *name); extern void saveTrophyScreenshot(void); +extern void initTitle(void); App app; Camera camera; diff --git a/src/structs.h b/src/structs.h index c2bbbb3..3be175c 100644 --- a/src/structs.h +++ b/src/structs.h @@ -494,6 +494,7 @@ struct Widget { int minValue; int maxValue; int visible; + int disabled; int numOptions; char **options; void (*action)(void); diff --git a/src/system/io.c b/src/system/io.c index a634f14..5540e60 100644 --- a/src/system/io.c +++ b/src/system/io.c @@ -29,6 +29,15 @@ int fileExists(const char *filename) return (stat(filename, &buffer) == 0); } +long getFileModTime(const char *filename) +{ + struct stat buffer; + + stat(filename, &buffer); + + return buffer.st_mtime; +} + const char *getFileLocation(const char *filename) { static char path[MAX_FILENAME_LENGTH]; diff --git a/src/system/widgets.c b/src/system/widgets.c index d42800f..11cae5c 100644 --- a/src/system/widgets.c +++ b/src/system/widgets.c @@ -208,6 +208,7 @@ void drawWidgets(void) x = w->x + w->w + 25; drawRect(x, w->y, 200, w->h, 0, 0, 0, 255); drawOutlineRect(x, w->y, 200, w->h, 0, outline, 0, 255); + if (app.awaitingWidgetInput && w == selectedWidget) { drawText(x + 100, w->y + 2, 24, TA_CENTER, colors.white, "..."); @@ -226,6 +227,11 @@ void drawWidgets(void) } break; } + + if (w->disabled) + { + drawRect(w->x, w->y, w->w, w->h, 0, 0, 0, 160); + } } } } @@ -239,7 +245,10 @@ void drawWidgetFrame(void) static void selectWidget(int dir) { - int oldWidgetIndex = widgetIndex; + int oldWidgetIndex, valid; + + oldWidgetIndex = widgetIndex; + valid = 0; do { @@ -256,8 +265,10 @@ static void selectWidget(int dir) } selectedWidget = &widgets[widgetIndex]; + + valid = selectedWidget->visible && !selectedWidget->disabled; - } while (!selectedWidget->visible); + } while (!valid); if (oldWidgetIndex != widgetIndex) { @@ -286,6 +297,11 @@ Widget *getWidget(char *name, char *group) return NULL; } +void setSelectedWidget(char *name, char *group) +{ + selectedWidget = getWidget(name, group); +} + Widget *selectWidgetAt(int x, int y) { Widget *w; @@ -396,7 +412,7 @@ static void loadWidgetGroup(char *filename) for (node = root->child ; node != NULL ; node = node->next) { - if (++numWidgets >= MAX_WIDGETS) + if (numWidgets >= MAX_WIDGETS) { SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Out of widget space."); exit(1); @@ -435,6 +451,8 @@ static void loadWidgetGroup(char *filename) default: break; } + + numWidgets++; } cJSON_Delete(root);