2015-03-15 20:51:56 +01:00
|
|
|
/*! \file gamestate.c
|
|
|
|
* \brief Gamestate management.
|
2012-12-24 19:41:12 +01:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Copyright (c) Sebastian Krzyszkowiak <dos@dosowisko.net>
|
|
|
|
*
|
|
|
|
* 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
|
2017-07-22 18:22:12 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2012-12-24 19:41:12 +01:00
|
|
|
* (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
|
2017-07-22 18:22:12 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-12-24 19:41:12 +01:00
|
|
|
*/
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
#include "gamestate.h"
|
2016-09-08 00:32:21 +02:00
|
|
|
#include "internal.h"
|
2012-12-24 19:41:12 +01:00
|
|
|
#include "utils.h"
|
2012-12-25 00:22:03 +01:00
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
static struct Gamestate* AddNewGamestate(struct Game* game, const char* name) {
|
|
|
|
struct Gamestate* tmp = game->_priv.gamestates;
|
2012-12-25 00:22:03 +01:00
|
|
|
if (!tmp) {
|
2017-09-09 00:11:43 +02:00
|
|
|
game->_priv.gamestates = AllocateGamestate(game, name);
|
2012-12-26 02:25:56 +01:00
|
|
|
tmp = game->_priv.gamestates;
|
2012-12-25 00:22:03 +01:00
|
|
|
} else {
|
|
|
|
while (tmp->next) {
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
2017-09-09 00:11:43 +02:00
|
|
|
tmp->next = AllocateGamestate(game, name);
|
2012-12-25 00:22:03 +01:00
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
static struct Gamestate* FindGamestate(struct Game* game, const char* name) {
|
|
|
|
struct Gamestate* tmp = game->_priv.gamestates;
|
2012-12-25 00:22:03 +01:00
|
|
|
while (tmp) {
|
|
|
|
if (!strcmp(name, tmp->name)) {
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-12-24 19:41:12 +01:00
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_EXPORT void RegisterGamestate(struct Game* game, const char* name, struct Gamestate_API* api) {
|
|
|
|
struct Gamestate* gs = FindGamestate(game, name);
|
2016-08-20 03:02:39 +02:00
|
|
|
if (!gs) {
|
|
|
|
gs = AddNewGamestate(game, name);
|
|
|
|
}
|
|
|
|
if (gs->api) {
|
|
|
|
PrintConsole(game, "Trying to register already registered gamestate \"%s\"!", name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gs->api = api;
|
|
|
|
PrintConsole(game, "Gamestate \"%s\" registered.", name);
|
|
|
|
}
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_EXPORT void LoadGamestate(struct Game* game, const char* name) {
|
|
|
|
struct Gamestate* gs = FindGamestate(game, name);
|
2012-12-25 00:22:03 +01:00
|
|
|
if (gs) {
|
2016-08-13 20:03:58 +02:00
|
|
|
if (gs->loaded && !gs->pending_unload) {
|
2012-12-27 21:05:28 +01:00
|
|
|
PrintConsole(game, "Gamestate \"%s\" already loaded.", name);
|
2012-12-25 00:22:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
gs->pending_load = true;
|
|
|
|
} else {
|
2016-08-16 18:01:12 +02:00
|
|
|
gs = AddNewGamestate(game, name);
|
2012-12-25 00:22:03 +01:00
|
|
|
gs->pending_load = true;
|
2015-03-15 05:38:15 +01:00
|
|
|
gs->showLoading = true;
|
2012-12-25 00:22:03 +01:00
|
|
|
}
|
2012-12-27 21:05:28 +01:00
|
|
|
PrintConsole(game, "Gamestate \"%s\" marked to be LOADED.", name);
|
2016-08-16 18:41:50 +02:00
|
|
|
game->_priv.gamestate_scheduled = true;
|
2012-12-24 19:41:12 +01:00
|
|
|
}
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_EXPORT void UnloadGamestate(struct Game* game, const char* name) {
|
|
|
|
struct Gamestate* gs = FindGamestate(game, name);
|
2012-12-25 00:22:03 +01:00
|
|
|
if (gs) {
|
2016-08-13 20:03:58 +02:00
|
|
|
if (gs->pending_load) {
|
|
|
|
gs->pending_load = false;
|
|
|
|
PrintConsole(game, "Canceling loading of gamestate \"%s\".", name);
|
|
|
|
return;
|
|
|
|
}
|
2012-12-25 00:22:03 +01:00
|
|
|
if (!gs->loaded) {
|
2012-12-27 21:05:28 +01:00
|
|
|
PrintConsole(game, "Gamestate \"%s\" already unloaded.", name);
|
2012-12-25 00:22:03 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-09-10 21:35:14 +02:00
|
|
|
if (gs->started) { gs->pending_stop = true; }
|
2016-08-13 20:03:58 +02:00
|
|
|
gs->pending_unload = true;
|
2012-12-27 21:05:28 +01:00
|
|
|
PrintConsole(game, "Gamestate \"%s\" marked to be UNLOADED.", name);
|
2012-12-25 00:22:03 +01:00
|
|
|
} else {
|
2012-12-27 21:05:28 +01:00
|
|
|
PrintConsole(game, "Tried to unload nonexisitent gamestate \"%s\"", name);
|
2012-12-25 00:22:03 +01:00
|
|
|
}
|
2016-08-16 18:41:50 +02:00
|
|
|
game->_priv.gamestate_scheduled = true;
|
2012-12-24 19:41:12 +01:00
|
|
|
}
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_EXPORT void StartGamestate(struct Game* game, const char* name) {
|
|
|
|
struct Gamestate* gs = FindGamestate(game, name);
|
2012-12-25 00:22:03 +01:00
|
|
|
if (gs) {
|
2016-08-13 20:03:58 +02:00
|
|
|
if (gs->started && !gs->pending_stop) {
|
2012-12-27 21:05:28 +01:00
|
|
|
PrintConsole(game, "Gamestate \"%s\" already started.", name);
|
2012-12-25 00:22:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
gs->pending_start = true;
|
2012-12-27 21:05:28 +01:00
|
|
|
PrintConsole(game, "Gamestate \"%s\" marked to be STARTED.", name);
|
2012-12-25 00:22:03 +01:00
|
|
|
} else {
|
2012-12-27 21:05:28 +01:00
|
|
|
PrintConsole(game, "Tried to start nonexisitent gamestate \"%s\"", name);
|
2012-12-25 00:22:03 +01:00
|
|
|
}
|
2016-08-16 18:41:50 +02:00
|
|
|
game->_priv.gamestate_scheduled = true;
|
2012-12-24 19:41:12 +01:00
|
|
|
}
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_EXPORT void StopGamestate(struct Game* game, const char* name) {
|
|
|
|
struct Gamestate* gs = FindGamestate(game, name);
|
2012-12-25 00:22:03 +01:00
|
|
|
if (gs) {
|
2016-08-13 20:03:58 +02:00
|
|
|
if (gs->pending_start) {
|
|
|
|
gs->pending_start = false;
|
|
|
|
PrintConsole(game, "Canceling starting of gamestate \"%s\".", name);
|
|
|
|
return;
|
|
|
|
}
|
2012-12-25 00:22:03 +01:00
|
|
|
if (!gs->started) {
|
2012-12-27 21:05:28 +01:00
|
|
|
PrintConsole(game, "Gamestate \"%s\" already stopped.", name);
|
2012-12-25 00:22:03 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-08-13 20:03:58 +02:00
|
|
|
gs->pending_stop = true;
|
2012-12-27 21:05:28 +01:00
|
|
|
PrintConsole(game, "Gamestate \"%s\" marked to be STOPPED.", name);
|
2012-12-25 00:22:03 +01:00
|
|
|
} else {
|
2012-12-27 21:05:28 +01:00
|
|
|
PrintConsole(game, "Tried to stop nonexisitent gamestate \"%s\"", name);
|
2012-12-25 00:22:03 +01:00
|
|
|
}
|
2016-08-16 18:41:50 +02:00
|
|
|
game->_priv.gamestate_scheduled = true;
|
2012-12-24 19:41:12 +01:00
|
|
|
}
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_EXPORT void PauseGamestate(struct Game* game, const char* name) {
|
|
|
|
struct Gamestate* gs = FindGamestate(game, name);
|
2012-12-25 00:22:03 +01:00
|
|
|
if (gs) {
|
|
|
|
if (!gs->started) {
|
2012-12-27 21:05:28 +01:00
|
|
|
PrintConsole(game, "Tried to pause gamestate \"%s\" which is not started.", name);
|
2012-12-25 00:22:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (gs->paused) {
|
2012-12-27 21:05:28 +01:00
|
|
|
PrintConsole(game, "Gamestate \"%s\" already paused.", name);
|
2012-12-25 00:22:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
gs->paused = true;
|
2016-11-11 20:17:02 +01:00
|
|
|
game->_priv.current_gamestate = gs;
|
|
|
|
(*gs->api->Gamestate_Pause)(game, gs->data);
|
2012-12-27 21:05:28 +01:00
|
|
|
PrintConsole(game, "Gamestate \"%s\" paused.", name);
|
2012-12-25 00:22:03 +01:00
|
|
|
} else {
|
2012-12-27 21:05:28 +01:00
|
|
|
PrintConsole(game, "Tried to pause nonexisitent gamestate \"%s\"", name);
|
2012-12-25 00:22:03 +01:00
|
|
|
}
|
2012-12-24 19:41:12 +01:00
|
|
|
}
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_EXPORT void ResumeGamestate(struct Game* game, const char* name) {
|
|
|
|
struct Gamestate* gs = FindGamestate(game, name);
|
2012-12-25 00:22:03 +01:00
|
|
|
if (gs) {
|
|
|
|
if (!gs->started) {
|
2012-12-27 21:05:28 +01:00
|
|
|
PrintConsole(game, "Tried to resume gamestate \"%s\" which is not started.", name);
|
2012-12-25 00:22:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!gs->paused) {
|
2012-12-27 21:05:28 +01:00
|
|
|
PrintConsole(game, "Gamestate \"%s\" already resumed.", name);
|
2012-12-25 00:22:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
gs->paused = false;
|
2016-11-11 20:17:02 +01:00
|
|
|
game->_priv.current_gamestate = gs;
|
|
|
|
(*gs->api->Gamestate_Resume)(game, gs->data);
|
2012-12-27 21:05:28 +01:00
|
|
|
PrintConsole(game, "Gamestate \"%s\" resumed.", name);
|
2012-12-25 00:22:03 +01:00
|
|
|
} else {
|
2012-12-27 21:05:28 +01:00
|
|
|
PrintConsole(game, "Tried to resume nonexisitent gamestate \"%s\"", name);
|
2012-12-25 00:22:03 +01:00
|
|
|
}
|
2012-12-24 19:41:12 +01:00
|
|
|
}
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_EXPORT void UnloadAllGamestates(struct Game* game) {
|
|
|
|
struct Gamestate* tmp = game->_priv.gamestates;
|
2016-08-13 21:35:26 +02:00
|
|
|
while (tmp) {
|
|
|
|
UnloadGamestate(game, tmp->name);
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_EXPORT void PauseAllGamestates(struct Game* game) {
|
|
|
|
struct Gamestate* tmp = game->_priv.gamestates;
|
2016-08-21 21:57:43 +02:00
|
|
|
while (tmp) {
|
|
|
|
if (tmp->started || !tmp->paused) {
|
|
|
|
PauseGamestate(game, tmp->name);
|
|
|
|
}
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_EXPORT void ResumeAllGamestates(struct Game* game) {
|
|
|
|
struct Gamestate* tmp = game->_priv.gamestates;
|
2016-08-21 21:57:43 +02:00
|
|
|
while (tmp) {
|
|
|
|
if (tmp->paused) {
|
|
|
|
ResumeGamestate(game, tmp->name);
|
|
|
|
}
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_EXPORT void SwitchGamestate(struct Game* game, const char* current, const char* n) {
|
2012-12-24 19:41:12 +01:00
|
|
|
StopGamestate(game, current);
|
|
|
|
UnloadGamestate(game, current);
|
|
|
|
LoadGamestate(game, n);
|
|
|
|
StartGamestate(game, n);
|
|
|
|
}
|
2016-08-16 18:41:50 +02:00
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_EXPORT void ChangeGamestate(struct Game* game, const char* current, const char* n) {
|
2016-08-20 03:32:09 +02:00
|
|
|
StopGamestate(game, current);
|
|
|
|
LoadGamestate(game, n);
|
|
|
|
StartGamestate(game, n);
|
|
|
|
}
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_EXPORT void SwitchCurrentGamestate(struct Game* game, const char* n) {
|
2016-08-16 18:41:50 +02:00
|
|
|
SwitchGamestate(game, game->_priv.current_gamestate->name, n);
|
|
|
|
}
|
2016-08-20 03:32:09 +02:00
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_EXPORT void ChangeCurrentGamestate(struct Game* game, const char* n) {
|
2016-08-20 03:32:09 +02:00
|
|
|
ChangeGamestate(game, game->_priv.current_gamestate->name, n);
|
|
|
|
}
|
2016-08-21 21:57:43 +02:00
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_EXPORT void StopCurrentGamestate(struct Game* game) {
|
2016-08-21 21:57:43 +02:00
|
|
|
StopGamestate(game, game->_priv.current_gamestate->name);
|
|
|
|
}
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_EXPORT void PauseCurrentGamestate(struct Game* game) {
|
2016-08-21 21:57:43 +02:00
|
|
|
PauseGamestate(game, game->_priv.current_gamestate->name);
|
|
|
|
}
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_EXPORT void UnloadCurrentGamestate(struct Game* game) {
|
2016-08-21 21:57:43 +02:00
|
|
|
UnloadGamestate(game, game->_priv.current_gamestate->name);
|
|
|
|
}
|