tbftss/src/main.c

243 lines
4.2 KiB
C
Raw Normal View History

2015-10-20 13:51:49 +02:00
/*
Copyright (C) 2015-2019,2022 Parallel Realities
2015-10-20 13:51:49 +02:00
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 <time.h>
2022-07-31 11:43:20 +02:00
#include "common.h"
#include "game/credits.h"
2022-07-31 11:43:20 +02:00
#include "game/load.h"
#include "game/save.h"
#include "game/title.h"
#include "game/trophies.h"
2022-07-31 11:43:20 +02:00
#include "main.h"
#include "plat/win32/win32Init.h"
#include "system/controls.h"
#include "system/dev.h"
2022-07-31 11:43:20 +02:00
#include "system/draw.h"
#include "system/init.h"
2022-07-31 11:43:20 +02:00
#include "system/input.h"
#include "system/io.h"
#include "system/lookup.h"
#include "system/modalDialog.h"
#include "test/testMission.h"
2022-07-31 11:43:20 +02:00
App app;
Battle battle;
Colors colors;
Dev dev;
Entity *player;
Entity *self;
2022-07-31 11:43:20 +02:00
Game game;
2015-10-20 13:51:49 +02:00
2016-05-15 09:19:26 +02:00
static void handleMissionArgs(int argc, char *argv[]);
static void handleLoggingArgs(int argc, char *argv[]);
2018-07-07 15:46:51 +02:00
static void capFrameRate(long *then, float *remainder);
2015-12-20 13:25:20 +01:00
2015-10-20 13:51:49 +02:00
int main(int argc, char *argv[])
{
2022-07-31 11:43:20 +02:00
long then, lastFrameTime, frames;
2018-07-07 15:46:51 +02:00
float remainder;
2019-11-07 09:13:57 +01:00
2015-10-20 13:51:49 +02:00
memset(&app, 0, sizeof(App));
2015-12-18 11:12:37 +01:00
memset(&dev, 0, sizeof(Dev));
2019-11-07 09:13:57 +01:00
2016-05-15 09:19:26 +02:00
handleLoggingArgs(argc, argv);
2019-11-07 09:13:57 +01:00
2015-10-20 13:51:49 +02:00
atexit(cleanup);
srand(time(NULL));
2019-11-07 09:13:57 +01:00
init18N(argc, argv);
2019-11-07 09:13:57 +01:00
2016-03-03 19:03:07 +01:00
initLookups();
2015-10-20 13:51:49 +02:00
initSDL(argc, argv);
2019-11-07 09:13:57 +01:00
2015-10-20 13:51:49 +02:00
initGameSystem();
2019-11-07 09:13:57 +01:00
createScreenshotFolder();
2019-11-07 09:13:57 +01:00
2016-05-15 09:19:26 +02:00
if (fileExists(getSaveFilePath(SAVE_FILENAME)))
{
loadGame();
}
2019-11-07 09:13:57 +01:00
2016-05-15 09:19:26 +02:00
handleMissionArgs(argc, argv);
2019-11-07 09:13:57 +01:00
2018-12-06 09:37:19 +01:00
remainder = 0;
dev.fps = frames = 0;
2015-10-20 13:51:49 +02:00
then = SDL_GetTicks();
lastFrameTime = SDL_GetTicks() + 1000;
2019-11-07 09:13:57 +01:00
2015-10-20 13:51:49 +02:00
while (1)
{
2018-07-07 15:46:51 +02:00
capFrameRate(&then, &remainder);
2019-11-07 09:13:57 +01:00
2018-12-06 17:52:13 +01:00
doInput();
2019-11-07 09:13:57 +01:00
if (app.modalDialog.type != MD_NONE)
{
doModalDialog();
}
2019-11-07 09:13:57 +01:00
/* let the delegate decide during logic() */
app.doTrophyAlerts = 0;
2019-11-07 09:13:57 +01:00
app.delegate.logic();
2019-11-07 09:13:57 +01:00
if (app.doTrophyAlerts)
{
doTrophyAlerts();
}
2019-11-07 09:13:57 +01:00
game.stats[STAT_TIME]++;
2019-11-07 09:13:57 +01:00
/* always zero the mouse motion */
app.mouse.dx = app.mouse.dy = 0;
2019-11-07 09:13:57 +01:00
prepareScene();
2015-10-20 13:51:49 +02:00
app.delegate.draw();
2019-11-07 09:13:57 +01:00
if (app.doTrophyAlerts)
{
drawTrophyAlert();
}
2019-11-07 09:13:57 +01:00
2016-02-23 08:19:31 +01:00
if (app.modalDialog.type != MD_NONE)
{
drawModalDialog();
}
2019-11-07 09:13:57 +01:00
presentScene();
2019-11-07 09:13:57 +01:00
2015-12-18 11:12:37 +01:00
doDevKeys();
2019-11-07 09:13:57 +01:00
2015-10-20 13:51:49 +02:00
frames++;
2019-11-07 09:13:57 +01:00
2015-10-20 13:51:49 +02:00
if (SDL_GetTicks() > lastFrameTime)
{
2015-12-18 11:12:37 +01:00
dev.fps = frames;
2015-10-20 13:51:49 +02:00
frames = 0;
lastFrameTime = SDL_GetTicks() + 1000;
2019-11-07 09:13:57 +01:00
2015-12-18 11:12:37 +01:00
if (dev.takeScreenshots)
{
saveScreenshot();
}
2015-10-20 13:51:49 +02:00
}
2019-11-07 09:13:57 +01:00
2016-04-23 18:46:47 +02:00
if (isControl(CONTROL_SCREENSHOT))
{
saveScreenshot();
2019-11-07 09:13:57 +01:00
2016-04-23 18:46:47 +02:00
clearControl(CONTROL_SCREENSHOT);
}
2019-11-07 09:13:57 +01:00
/* don't save more than once per request, and not in the middle of battle */
if (app.saveGame && battle.status != MS_IN_PROGRESS)
2016-05-15 09:19:26 +02:00
{
saveGame();
app.saveGame = 0;
}
2015-10-20 13:51:49 +02:00
SDL_Delay(1);
}
return 0;
}
2015-12-20 13:25:20 +01:00
2018-07-07 15:46:51 +02:00
static void capFrameRate(long *then, float *remainder)
{
long wait;
2019-11-07 09:13:57 +01:00
2018-07-07 15:46:51 +02:00
wait = 16 + *remainder;
2019-11-07 09:13:57 +01:00
2018-07-07 15:46:51 +02:00
*remainder -= (int)*remainder;
2019-11-07 09:13:57 +01:00
2018-07-07 15:46:51 +02:00
wait -= (SDL_GetTicks() - *then);
2019-11-07 09:13:57 +01:00
2018-07-07 15:46:51 +02:00
if (wait < 1)
{
2018-07-07 15:46:51 +02:00
wait = 1;
}
2019-11-07 09:13:57 +01:00
2018-07-07 15:46:51 +02:00
SDL_Delay(wait);
2019-11-07 09:13:57 +01:00
*remainder += 0.666667;
2019-11-07 09:13:57 +01:00
2018-07-07 15:46:51 +02:00
*then = SDL_GetTicks();
}
2016-05-15 09:19:26 +02:00
static void handleLoggingArgs(int argc, char *argv[])
{
int i;
2019-11-07 09:13:57 +01:00
2018-12-06 17:52:13 +01:00
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN);
2019-11-07 09:13:57 +01:00
2022-07-31 11:43:20 +02:00
for (i = 1; i < argc; i++)
2016-05-15 09:19:26 +02:00
{
if (strcmp(argv[i], "-debug") == 0)
{
dev.debug = 1;
2019-11-07 09:13:57 +01:00
2016-05-15 09:19:26 +02:00
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG);
}
2019-11-07 09:13:57 +01:00
2016-05-15 09:19:26 +02:00
if (strcmp(argv[i], "-info") == 0)
{
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
}
}
}
static void handleMissionArgs(int argc, char *argv[])
2015-12-20 13:25:20 +01:00
{
2016-05-29 10:38:05 +02:00
int i, testingMission, showCredits;
2019-11-07 09:13:57 +01:00
2016-05-29 10:38:05 +02:00
showCredits = testingMission = 0;
2019-11-07 09:13:57 +01:00
2022-07-31 11:43:20 +02:00
for (i = 1; i < argc; i++)
2015-12-20 13:25:20 +01:00
{
/* assume this is filename for testing */
if (strcmp(argv[i], "-mission") == 0)
2015-12-20 13:25:20 +01:00
{
loadTestMission(argv[++i]);
2019-11-07 09:13:57 +01:00
2015-12-20 13:25:20 +01:00
testingMission = 1;
}
2019-11-07 09:13:57 +01:00
2016-05-29 10:38:05 +02:00
if (strcmp(argv[i], "-credits") == 0)
{
showCredits = 1;
}
2015-12-20 13:25:20 +01:00
}
2019-11-07 09:13:57 +01:00
2016-05-29 10:38:05 +02:00
if (showCredits)
{
initCredits();
}
else if (!testingMission)
2015-12-20 13:25:20 +01:00
{
initTitle();
}
}