blobwarsAttrition/src/game/title.c

353 lines
6.3 KiB
C
Raw Normal View History

2018-01-21 10:31:38 +01:00
/*
Copyright (C) 2018 Parallel Realities
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.
*/
#include "title.h"
static void logic(void);
static void draw(void);
2018-03-19 09:08:29 +01:00
static int getRecentSave(void);
2018-03-19 23:51:37 +01:00
static void populateSaveSlotWidgets(void);
static void doNewGame(void);
static void doLoadGame(void);
static void doContinueGame(void);
static void doOptions(void);
static void doCredits(void);
static void doQuit(void);
static void doSaveSlot(void);
static void doLoadCancel(void);
static void doOK(void);
static void doCancel(void);
2018-03-21 08:26:11 +01:00
void returnToTitle(void);
2018-03-19 09:08:29 +01:00
static Texture *atlasTexture;
static Atlas *title;
static int recentSaveSlot;
2018-03-19 23:51:37 +01:00
static int saveAction;
2018-03-20 08:29:05 +01:00
static Widget *startNewGame;
2018-03-19 23:51:37 +01:00
static Widget *load;
2018-03-19 09:08:29 +01:00
static Widget *continueGame;
static Widget *options;
static Widget *credits;
static Widget *quit;
2018-03-19 23:51:37 +01:00
static Widget *save[MAX_SAVE_SLOTS];
static Widget *loadCancel;
static Widget *ok;
static Widget *cancel;
static float titleAlpha;
2018-01-21 10:31:38 +01:00
void initTitle(void)
{
2018-03-19 09:08:29 +01:00
startSectionTransition();
atlasTexture = getTexture("gfx/atlas/atlas.png");
title = getImageFromAtlas("gfx/main/title.png");
2018-03-20 08:29:05 +01:00
startNewGame = getWidget("new", "title");
startNewGame->action = &doNewGame;
2018-03-19 23:51:37 +01:00
load = getWidget("load", "title");
load->action = &doLoadGame;
2018-03-19 09:08:29 +01:00
continueGame = getWidget("continue", "title");
2018-03-19 23:51:37 +01:00
continueGame->action = &doContinueGame;
2018-03-19 09:08:29 +01:00
options = getWidget("options", "title");
2018-03-19 23:51:37 +01:00
options->action = &doOptions;
2018-03-19 09:08:29 +01:00
credits = getWidget("credits", "title");
2018-03-19 23:51:37 +01:00
credits->action = &doCredits;
2018-03-19 09:08:29 +01:00
quit = getWidget("exit", "title");
2018-03-19 23:51:37 +01:00
quit->action = &doQuit;
populateSaveSlotWidgets();
loadCancel = getWidget("cancel", "load");
loadCancel->action = doLoadCancel;
ok = getWidget("ok", "delete");
2018-03-19 23:51:37 +01:00
ok->action = doOK;
cancel = getWidget("cancel", "delete");
2018-03-19 23:51:37 +01:00
cancel->action = doCancel;
2018-03-19 09:08:29 +01:00
titleAlpha = 0;
2018-03-19 09:08:29 +01:00
recentSaveSlot = getRecentSave();
showWidgetGroup("title");
if (recentSaveSlot != -1)
{
setSelectedWidget("continue", "title");
}
else
{
2018-03-19 23:51:37 +01:00
load->disabled = 1;
2018-03-19 09:08:29 +01:00
continueGame->disabled = 1;
}
2018-04-21 10:40:38 +02:00
saveAction = SA_NONE;
2018-01-21 10:31:38 +01:00
app.delegate.logic = &logic;
app.delegate.draw = &draw;
2018-03-19 09:08:29 +01:00
loadMusic("music/Watching - DJ Sjors.ogg");
playMusic(0);
2018-03-19 09:08:29 +01:00
endSectionTransition();
2018-01-21 10:31:38 +01:00
}
static void logic(void)
{
2018-03-19 09:08:29 +01:00
doWidgets();
if (titleAlpha < 255)
{
titleAlpha++;
}
2018-03-21 08:26:11 +01:00
if (app.keyboard[SDL_SCANCODE_ESCAPE])
{
doLoadCancel();
app.keyboard[SDL_SCANCODE_ESCAPE] = 0;
}
2018-01-21 10:31:38 +01:00
}
static void draw(void)
{
SDL_SetTextureAlphaMod(atlasTexture->texture, titleAlpha);
2018-03-19 09:08:29 +01:00
blitRect(atlasTexture->texture, SCREEN_WIDTH / 2, 175, &title->rect, 1);
SDL_SetTextureAlphaMod(atlasTexture->texture, 255);
2018-03-19 09:08:29 +01:00
2018-03-19 23:51:37 +01:00
drawText(10, SCREEN_HEIGHT - 30, 16, TA_LEFT, colors.white, "Copyright 2014, 2018 Parallel Realities");
drawText(SCREEN_WIDTH - 10, SCREEN_HEIGHT - 30, 16, TA_RIGHT, colors.white, "Version %.2f.%d", VERSION, REVISION);
2018-03-19 09:08:29 +01:00
drawWidgets();
2018-03-21 08:26:11 +01:00
if (saveAction == SA_NEW)
{
2018-04-01 18:41:45 +02:00
drawText(SCREEN_WIDTH / 2, 275, 24, TA_CENTER, colors.white, app.strings[ST_CHOOSE_SAVE]);
2018-03-21 08:26:11 +01:00
}
else if (saveAction == SA_LOAD)
{
2018-04-01 18:41:45 +02:00
drawText(SCREEN_WIDTH / 2, 275, 24, TA_CENTER, colors.white, app.strings[ST_LOAD]);
2018-03-21 08:26:11 +01:00
}
else if (saveAction == SA_DELETE)
{
2018-04-01 18:41:45 +02:00
drawText(SCREEN_WIDTH / 2, 350, 24, TA_CENTER, colors.white, app.strings[ST_OVERWRITE_1]);
drawText(SCREEN_WIDTH / 2, 400, 22, TA_CENTER, colors.white, app.strings[ST_OVERWRITE_2]);
}
2018-03-19 09:08:29 +01:00
}
static int getRecentSave(void)
{
char filename[MAX_PATH_LENGTH];
2018-03-19 09:08:29 +01:00
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;
2018-01-21 10:31:38 +01:00
}
2018-03-19 23:51:37 +01:00
static void populateSaveSlotWidgets(void)
{
int i;
char name[MAX_NAME_LENGTH], filename[MAX_PATH_LENGTH];
2018-03-19 23:51:37 +01:00
for (i = 0 ; i < MAX_SAVE_SLOTS ; i++)
{
sprintf(name, "save%d", i);
save[i] = getWidget(name, "saveSlot");
sprintf(filename, "%s/%d/game.json", app.saveDir, i);
if (fileExists(filename))
{
strcpy(save[i]->label, getSaveWidgetLabel(filename));
save[i]->value[0] = 1;
}
else
{
strcpy(save[i]->label, "(empty)");
save[i]->value[0] = 0;
}
save[i]->value[1] = i;
save[i]->action = &doSaveSlot;
}
}
static void doNewGame(void)
{
2018-03-20 08:29:05 +01:00
int i;
saveAction = SA_NEW;
2018-03-19 23:51:37 +01:00
2018-03-20 08:29:05 +01:00
showWidgetGroup("saveSlot");
for (i = 0 ; i < MAX_SAVE_SLOTS ; i++)
{
save[i]->disabled = 0;
}
loadCancel->visible = 1;
2018-03-19 23:51:37 +01:00
destroyGame();
}
static void doLoadGame(void)
{
2018-03-20 08:29:05 +01:00
int i;
2018-03-19 23:51:37 +01:00
saveAction = SA_LOAD;
showWidgetGroup("saveSlot");
2018-03-20 08:29:05 +01:00
for (i = 0 ; i < MAX_SAVE_SLOTS ; i++)
{
save[i]->disabled = save[i]->value[0] == 0;
}
2018-03-19 23:51:37 +01:00
loadCancel->visible = 1;
}
static void doContinueGame(void)
{
stopMusic();
2018-03-19 23:51:37 +01:00
loadGame();
2018-03-19 23:51:37 +01:00
initHub();
game.saveSlot = continueGame->value[1];
2018-03-19 23:51:37 +01:00
}
static void doOptions(void)
{
2018-03-21 08:26:11 +01:00
initOptions(returnToTitle);
2018-03-19 23:51:37 +01:00
}
static void doCredits(void)
{
2018-03-21 08:26:11 +01:00
initCredits(0);
2018-03-19 23:51:37 +01:00
}
static void doQuit(void)
{
stopMusic();
2018-03-19 23:51:37 +01:00
exit(1);
}
static void doSaveSlot(void)
{
Widget *w;
w = getSelectedWidget();
game.saveSlot = w->value[1];
2018-03-19 23:51:37 +01:00
if (saveAction == SA_LOAD)
{
stopMusic();
2018-03-19 23:51:37 +01:00
loadGame();
initHub();
}
else if (saveAction == SA_NEW)
2018-03-19 23:51:37 +01:00
{
if (!w->value[0])
{
doOK();
}
else
{
saveAction = SA_DELETE;
showWidgetGroup("delete");
setSelectedWidget("cancel", "delete");
}
2018-03-19 23:51:37 +01:00
}
}
static void doLoadCancel(void)
{
showWidgetGroup("title");
2018-03-21 08:26:11 +01:00
saveAction = SA_NONE;
2018-03-19 23:51:37 +01:00
}
static void doOK(void)
{
int saveSlot;
saveSlot = game.saveSlot;
2018-03-21 19:25:29 +01:00
deleteSaveSlot(saveSlot);
stopMusic();
newGame();
initHub();
game.saveSlot = saveSlot;
saveGame();
2018-03-19 23:51:37 +01:00
}
static void doCancel(void)
{
doNewGame();
2018-03-19 23:51:37 +01:00
}
2018-03-20 08:29:05 +01:00
2018-03-21 08:26:11 +01:00
void returnToTitle(void)
2018-03-20 08:29:05 +01:00
{
app.delegate.logic = &logic;
app.delegate.draw = &draw;
showWidgetGroup("title");
}