make Gamestate structure private and add API for accessing it

This commit is contained in:
Sebastian Krzyszkowiak 2019-03-05 03:20:06 +01:00
parent 66e0319691
commit c93c22cf04
No known key found for this signature in database
GPG key ID: E8F235CF3BDBC3FF
7 changed files with 75 additions and 46 deletions

View file

@ -32,19 +32,8 @@ static struct Gamestate* AddNewGamestate(struct Game* game, const char* name) {
return tmp;
}
static struct Gamestate* FindGamestate(struct Game* game, const char* name) {
struct Gamestate* tmp = game->_priv.gamestates;
while (tmp) {
if (!strcmp(name, tmp->name)) {
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
SYMBOL_EXPORT void RegisterGamestate(struct Game* game, const char* name, struct GamestateAPI* api) {
struct Gamestate* gs = FindGamestate(game, name);
struct Gamestate* gs = GetGamestate(game, name);
if (!gs) {
gs = AddNewGamestate(game, name);
}
@ -58,7 +47,7 @@ SYMBOL_EXPORT void RegisterGamestate(struct Game* game, const char* name, struct
}
SYMBOL_EXPORT void LoadGamestate(struct Game* game, const char* name) {
struct Gamestate* gs = FindGamestate(game, name);
struct Gamestate* gs = GetGamestate(game, name);
if (gs) {
if (gs->loaded && !gs->pending_unload) {
PrintConsole(game, "Gamestate \"%s\" already loaded.", name);
@ -74,7 +63,7 @@ SYMBOL_EXPORT void LoadGamestate(struct Game* game, const char* name) {
}
SYMBOL_EXPORT void UnloadGamestate(struct Game* game, const char* name) {
struct Gamestate* gs = FindGamestate(game, name);
struct Gamestate* gs = GetGamestate(game, name);
if (gs) {
if (gs->pending_load) {
gs->pending_load = false;
@ -95,7 +84,7 @@ SYMBOL_EXPORT void UnloadGamestate(struct Game* game, const char* name) {
}
SYMBOL_EXPORT void StartGamestate(struct Game* game, const char* name) {
struct Gamestate* gs = FindGamestate(game, name);
struct Gamestate* gs = GetGamestate(game, name);
if (gs) {
if (gs->started && !gs->pending_stop) {
PrintConsole(game, "Gamestate \"%s\" already started.", name);
@ -114,7 +103,7 @@ SYMBOL_EXPORT void StartGamestate(struct Game* game, const char* name) {
}
SYMBOL_EXPORT void StopGamestate(struct Game* game, const char* name) {
struct Gamestate* gs = FindGamestate(game, name);
struct Gamestate* gs = GetGamestate(game, name);
if (gs) {
if (gs->pending_start) {
gs->pending_start = false;
@ -134,7 +123,7 @@ SYMBOL_EXPORT void StopGamestate(struct Game* game, const char* name) {
}
SYMBOL_EXPORT void PauseGamestate(struct Game* game, const char* name) {
struct Gamestate* gs = FindGamestate(game, name);
struct Gamestate* gs = GetGamestate(game, name);
if (gs) {
if (!gs->started) {
PrintConsole(game, "Tried to pause gamestate \"%s\" which is not started.", name);
@ -156,7 +145,7 @@ SYMBOL_EXPORT void PauseGamestate(struct Game* game, const char* name) {
}
SYMBOL_EXPORT void ResumeGamestate(struct Game* game, const char* name) {
struct Gamestate* gs = FindGamestate(game, name);
struct Gamestate* gs = GetGamestate(game, name);
if (gs) {
if (!gs->started) {
PrintConsole(game, "Tried to resume gamestate \"%s\" which is not started.", name);
@ -237,3 +226,36 @@ SYMBOL_EXPORT void PauseCurrentGamestate(struct Game* game) {
SYMBOL_EXPORT void UnloadCurrentGamestate(struct Game* game) {
UnloadGamestate(game, game->_priv.current_gamestate->name);
}
SYMBOL_EXPORT struct Gamestate* GetCurrentGamestate(struct Game* game) {
return game->_priv.current_gamestate;
}
SYMBOL_EXPORT struct Gamestate* GetGamestate(struct Game* game, const char* name) {
struct Gamestate* tmp = game->_priv.gamestates;
if (!name) {
return game->_priv.loading.gamestate;
}
while (tmp) {
if (!strcmp(name, tmp->name)) {
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
SYMBOL_EXPORT ALLEGRO_BITMAP* GetGamestateFramebuffer(struct Game* game, struct Gamestate* gamestate) {
return gamestate->fb;
}
SYMBOL_EXPORT struct Gamestate* GetNextGamestate(struct Game* game, struct Gamestate* gamestate) {
if (!gamestate) {
return game->_priv.gamestates;
}
return gamestate->next;
}
SYMBOL_EXPORT bool IsGamestateVisible(struct Game* game, struct Gamestate* gamestate) {
return (gamestate->loaded) && (gamestate->started);
}

View file

@ -41,22 +41,7 @@ struct GamestateAPI {
int* progress_count;
};
struct Gamestate {
char* name;
void* handle;
bool loaded, pending_load, pending_unload;
bool started, pending_start, pending_stop;
bool frozen;
bool show_loading;
bool paused;
bool fromlib;
bool open;
struct Gamestate* next;
struct GamestateAPI* api;
ALLEGRO_BITMAP* fb;
int progress_count;
void* data;
};
struct Gamestate;
void LoadGamestate(struct Game* game, const char* name);
void UnloadGamestate(struct Game* game, const char* name);
@ -75,6 +60,11 @@ void ChangeCurrentGamestate(struct Game* game, const char* n);
void StopCurrentGamestate(struct Game* game);
void PauseCurrentGamestate(struct Game* game);
void UnloadCurrentGamestate(struct Game* game);
struct Gamestate* GetCurrentGamestate(struct Game* game);
struct Gamestate* GetGamestate(struct Game* game, const char* name);
ALLEGRO_BITMAP* GetGamestateFramebuffer(struct Game* game, struct Gamestate* gamestate);
struct Gamestate* GetNextGamestate(struct Game* game, struct Gamestate* gamestate);
bool IsGamestateVisible(struct Game* game, struct Gamestate* gamestate);
// Gamestate API

View file

@ -21,17 +21,17 @@
#include "3rdparty/valgrind.h"
#include <dlfcn.h>
SYMBOL_INTERNAL void SimpleCompositor(struct Game* game, struct Gamestate* gamestates, ALLEGRO_BITMAP* loading_fb) {
struct Gamestate* tmp = gamestates;
SYMBOL_INTERNAL void SimpleCompositor(struct Game* game) {
struct Gamestate* tmp = GetNextGamestate(game, NULL);
ClearToColor(game, al_map_rgb(0, 0, 0));
while (tmp) {
if ((tmp->loaded) && (tmp->started)) {
al_draw_bitmap(tmp->fb, game->clip_rect.x, game->clip_rect.y, 0);
if (IsGamestateVisible(game, tmp)) {
al_draw_bitmap(GetGamestateFramebuffer(game, tmp), game->clip_rect.x, game->clip_rect.y, 0);
}
tmp = tmp->next;
tmp = GetNextGamestate(game, tmp);
}
if (game->loading.shown) {
al_draw_bitmap(loading_fb, game->clip_rect.x, game->clip_rect.y, 0);
al_draw_bitmap(GetGamestateFramebuffer(game, GetGamestate(game, NULL)), game->clip_rect.x, game->clip_rect.y, 0);
}
}
@ -77,7 +77,7 @@ SYMBOL_INTERNAL void DrawGamestates(struct Game* game) {
al_reset_clipping_rectangle();
if (game->_priv.params.handlers.compositor) {
game->_priv.params.handlers.compositor(game, game->_priv.gamestates, game->_priv.loading.gamestate->fb);
game->_priv.params.handlers.compositor(game);
}
if (game->_priv.params.handlers.postdraw) {

View file

@ -93,7 +93,24 @@ struct ScreenshotThreadData {
ALLEGRO_BITMAP* bitmap;
};
void SimpleCompositor(struct Game* game, struct Gamestate* gamestates, ALLEGRO_BITMAP* loading_fb);
struct Gamestate {
char* name;
void* handle;
bool loaded, pending_load, pending_unload;
bool started, pending_start, pending_stop;
bool frozen;
bool show_loading;
bool paused;
bool fromlib;
bool open;
struct Gamestate* next;
struct GamestateAPI* api;
ALLEGRO_BITMAP* fb;
int progress_count;
void* data;
};
void SimpleCompositor(struct Game* game);
void DrawGamestates(struct Game* game);
void LogicGamestates(struct Game* game, double delta);
void EventGamestates(struct Game* game, ALLEGRO_EVENT* ev);

View file

@ -112,7 +112,7 @@ int _libsuperderpy_main(int argc, char** argv);
struct Handlers {
bool (*event)(struct Game* game, ALLEGRO_EVENT* ev);
void (*destroy)(struct Game* game);
void (*compositor)(struct Game* game, struct Gamestate* gamestates, ALLEGRO_BITMAP* loading_fb);
void (*compositor)(struct Game* game);
void (*prelogic)(struct Game* game, double delta);
void (*postlogic)(struct Game* game, double delta);
void (*predraw)(struct Game* game);

View file

@ -127,7 +127,7 @@ SYMBOL_EXPORT void DrawCenteredTintedScaled(ALLEGRO_BITMAP* bitmap, ALLEGRO_COLO
SYMBOL_EXPORT void ClearToColor(struct Game* game, ALLEGRO_COLOR color) {
ALLEGRO_BITMAP* target = al_get_target_bitmap();
if (GetFramebuffer(game) == target && al_get_parent_bitmap(target) == al_get_backbuffer(game->display)) {
if (game->_priv.current_gamestate && GetFramebuffer(game) == target && al_get_parent_bitmap(target) == al_get_backbuffer(game->display)) {
al_set_target_backbuffer(game->display);
}
int x, y, w, h;
@ -652,7 +652,7 @@ SYMBOL_EXPORT ALLEGRO_BITMAP* CreateNotPreservedBitmap(int width, int height) {
return bitmap;
}
SYMBOL_EXPORT void EnableCompositor(struct Game* game, void compositor(struct Game* game, struct Gamestate* gamestates, ALLEGRO_BITMAP* loading_fb)) {
SYMBOL_EXPORT void EnableCompositor(struct Game* game, void compositor(struct Game* game)) {
PrintConsole(game, "Compositor enabled.");
game->_priv.params.handlers.compositor = compositor ? compositor : SimpleCompositor;
ResizeGamestates(game);

View file

@ -82,7 +82,7 @@ void PopTransform(struct Game* game);
ALLEGRO_BITMAP* CreateNotPreservedBitmap(int width, int height);
void EnableCompositor(struct Game* game, void compositor(struct Game* game, struct Gamestate* gamestates, ALLEGRO_BITMAP* loading_fb));
void EnableCompositor(struct Game* game, void compositor(struct Game* game));
void DisableCompositor(struct Game* game);
char* StrToLower(struct Game* game, char* text);