basic implementation of gamestates handling

This commit is contained in:
Sebastian Krzyszkowiak 2012-12-25 00:22:03 +01:00
parent e29382b5dc
commit b36c5e8ed4
5 changed files with 186 additions and 17 deletions

View file

@ -25,18 +25,18 @@ void InitConfig(struct Game *game) {
ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_USER_SETTINGS_PATH);
ALLEGRO_PATH *data = al_create_path("SuperDerpy.ini");
al_join_paths(path, data);
game->config.config = al_load_config_file(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP));
if (!game->config.config) game->config.config=al_create_config();
game->_priv.config = al_load_config_file(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP));
if (!game->_priv.config) game->_priv.config=al_create_config();
al_destroy_path(path);
al_destroy_path(data);
}
void SetConfigOption(struct Game *game, char* section, char* name, char* value) {
al_set_config_value(game->config.config, section, name, value);
al_set_config_value(game->_priv.config, section, name, value);
}
const char* GetConfigOption(struct Game *game, char* section, char* name) {
return al_get_config_value(game->config.config, section, name);
return al_get_config_value(game->_priv.config, section, name);
}
const char* GetConfigOptionDefault(struct Game *game, char* section, char* name, const char* def) {
@ -49,8 +49,8 @@ void DeinitConfig(struct Game *game) {
ALLEGRO_PATH *data = al_create_path("SuperDerpy.ini");
al_make_directory(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP));
al_join_paths(path, data);
al_save_config_file(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP), game->config.config);
al_save_config_file(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP), game->_priv.config);
al_destroy_path(path);
al_destroy_path(data);
al_destroy_config(game->config.config);
al_destroy_config(game->_priv.config);
}

View file

@ -20,29 +20,139 @@
*/
#include "utils.h"
#include "gamestate.h"
struct Gamestate* AddNewGamestate(struct Game *game) {
struct Gamestate *tmp = game->_priv.gamestate_list;
if (!tmp) {
game->_priv.gamestate_list = malloc(sizeof(struct Gamestate));
tmp = game->_priv.gamestate_list;
} else {
while (tmp->next) {
tmp = tmp->next;
}
tmp->next = malloc(sizeof(struct Gamestate));
tmp = tmp->next;
}
tmp->name = NULL;
tmp->fade = false;
tmp->fade_counter = 0;
tmp->handle = NULL;
tmp->loaded = false;
tmp->paused = false;
tmp->started = false;
tmp->pending_load = false;
tmp->pending_start = false;
tmp->after = NULL;
tmp->next = NULL;
return tmp;
}
struct Gamestate* FindGamestate(struct Game *game, const char* name) {
struct Gamestate *tmp = game->_priv.gamestate_list;
while (tmp) {
if (!strcmp(name, tmp->name)) {
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
void LoadGamestate(struct Game *game, const char* name) {
PrintConsole(game, "load finished %s", name);
struct Gamestate *gs = FindGamestate(game, name);
if (gs) {
if (!gs->loaded) {
PrintConsole(game, "Gamestate %s already loaded.", name);
return;
}
gs->pending_load = true;
} else {
gs = AddNewGamestate(game);
gs->name = strdup(name);
gs->fade = true;
gs->fade_counter = 0;
gs->pending_load = true;
}
PrintConsole(game, "Gamestate %s marked to be LOADED.", name);
}
void UnloadGamestate(struct Game *game, const char* name) {
PrintConsole(game, "unload finished %s", name);
struct Gamestate *gs = FindGamestate(game, name);
if (gs) {
if (!gs->loaded) {
PrintConsole(game, "Gamestate %s already unloaded.", name);
return;
}
gs->pending_load = true;
PrintConsole(game, "Gamestate %s marked to be UNLOADED.", name);
} else {
PrintConsole(game, "Tried to unload nonexisitent gamestate %s", name);
}
}
void StartGamestate(struct Game *game, const char* name) {
PrintConsole(game, "start finished %s", name);
struct Gamestate *gs = FindGamestate(game, name);
if (gs) {
if (gs->started) {
PrintConsole(game, "Gamestate %s already started.", name);
return;
}
gs->pending_start = true;
PrintConsole(game, "Gamestate %s marked to be STARTED.", name);
} else {
PrintConsole(game, "Tried to start nonexisitent gamestate %s", name);
}
}
void StopGamestate(struct Game *game, const char* name) {
PrintConsole(game, "stop finished %s", name);
struct Gamestate *gs = FindGamestate(game, name);
if (gs) {
if (!gs->started) {
PrintConsole(game, "Gamestate %s already stopped.", name);
return;
}
gs->pending_start = true;
PrintConsole(game, "Gamestate %s marked to be STOPPED.", name);
} else {
PrintConsole(game, "Tried to stop nonexisitent gamestate %s", name);
}
}
void PauseGamestate(struct Game *game, const char* name) {
PrintConsole(game, "pause finished %s", name);
struct Gamestate *gs = FindGamestate(game, name);
if (gs) {
if (!gs->started) {
PrintConsole(game, "Tried to pause gamestate %s which is not started.", name);
return;
}
if (gs->paused) {
PrintConsole(game, "Gamestate %s already paused.", name);
return;
}
gs->paused = true;
PrintConsole(game, "Gamestate %s paused.", name);
} else {
PrintConsole(game, "Tried to pause nonexisitent gamestate %s", name);
}
}
void ResumeGamestate(struct Game *game, const char* name) {
PrintConsole(game, "resume finished %s", name);
struct Gamestate *gs = FindGamestate(game, name);
if (gs) {
if (!gs->started) {
PrintConsole(game, "Tried to resume gamestate %s which is not started.", name);
return;
}
if (!gs->paused) {
PrintConsole(game, "Gamestate %s already resumed.", name);
return;
}
gs->paused = false;
PrintConsole(game, "Gamestate %s resumed.", name);
} else {
PrintConsole(game, "Tried to resume nonexisitent gamestate %s", name);
}
}
void SwitchGamestate(struct Game *game, const char* current, const char* n) {

View file

@ -18,6 +18,8 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef GAMESTATE_H
#define GAMESTATE_H
struct Game;
@ -40,3 +42,5 @@ void StopGamestate(struct Game *game, const char* name);
void PauseGamestate(struct Game *game, const char* name);
void ResumeGamestate(struct Game *game, const char* name);
void SwitchGamestate(struct Game *game, const char* current, const char* n);
#endif

View file

@ -48,12 +48,37 @@ void DrawConsole(struct Game *game) {
game->_priv.fps_count.frames_done++;
}
int counter=0;
void DrawGamestates(struct Game *game) {
if (counter<0) {
PrintConsole(game, "logiced %d", abs(counter));
counter=0;
}
counter++;
al_clear_to_color(al_map_rgb(0,0,0));
struct Gamestate *tmp = game->_priv.gamestate_list;
while (tmp) {
if ((tmp->loaded) && (tmp->started)) {
//PrintConsole(game, "drawing %s", tmp->name);
}
tmp = tmp->next;
}
}
void LogicGamestates(struct Game *game) {
return;
if (counter>0) {
PrintConsole(game, "drawed %d", abs(counter));
counter=0;
}
counter--;
struct Gamestate *tmp = game->_priv.gamestate_list;
while (tmp) {
if ((tmp->loaded) && (tmp->started) && (!tmp->paused)) {
//PrintConsole(game, "logic %s", tmp->name);
}
tmp = tmp->next;
}
}
void SetupViewport(struct Game *game) {
@ -213,6 +238,9 @@ int main(int argc, char **argv){
PrintConsole(&game, "Viewport %dx%d", game.viewport.width, game.viewport.height);
game._priv.gamestate = NULL;
game._priv.gamestate_list = NULL;
game._priv.event_queue = al_create_event_queue();
if(!game._priv.event_queue) {
fprintf(stderr, "failed to create event_queue!\n");
@ -240,13 +268,13 @@ int main(int argc, char **argv){
al_flip_display();
al_clear_to_color(al_map_rgb(0,0,0));
al_wait_for_vsync();
game._priv.timer = al_create_timer(ALLEGRO_BPS_TO_SECS(60)); // logic timer
if(!game._priv.timer) {
fprintf(stderr, "failed to create timer!\n");
return -1;
}
al_register_event_source(game._priv.event_queue, al_get_timer_event_source(game._priv.timer));
al_wait_for_vsync();
al_start_timer(game._priv.timer);
setlocale(LC_NUMERIC, "C");
@ -274,14 +302,39 @@ int main(int argc, char **argv){
StartGamestate(&game, gamestate);
//free(gamestate);
bool redraw = false;
while(1) {
ALLEGRO_EVENT ev;
if (al_is_event_queue_empty(game._priv.event_queue)) {
if (redraw && al_is_event_queue_empty(game._priv.event_queue)) {
struct Gamestate *tmp = game._priv.gamestate_list;
while (tmp) {
if ((tmp->pending_load) && (!tmp->loaded)) {
PrintConsole(&game, "Loading gamestate %s...", tmp->name);
tmp->loaded = true;
tmp->pending_load = false;
} else if ((tmp->pending_load) && (tmp->loaded)) {
PrintConsole(&game, "Unloading gamestate %s...", tmp->name);
tmp->loaded = false;
tmp->pending_load = false;
tmp->handle = NULL;
} else if ((tmp->pending_start) && (!tmp->started)) {
PrintConsole(&game, "Starting gamestate %s...", tmp->name);
tmp->started = true;
tmp->pending_start = false;
} else if ((tmp->pending_start) && (tmp->started)) {
PrintConsole(&game, "Stopping gamestate %s...", tmp->name);
tmp->started = false;
tmp->pending_start = false;
}
tmp=tmp->next;
}
// TODO: process gamestates
DrawGamestates(&game);
DrawConsole(&game);
al_flip_display();
redraw = false;
} else {
@ -289,6 +342,7 @@ int main(int argc, char **argv){
if ((ev.type == ALLEGRO_EVENT_TIMER) && (ev.timer.source == game._priv.timer)) {
LogicGamestates(&game);
redraw = true;
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
break;

View file

@ -51,7 +51,6 @@ struct Game {
int fps; /*!< FPS limit */
int width; /*!< Width of window as being set in configuration. */
int height; /*!< Height of window as being set in configuration. */
ALLEGRO_CONFIG *config;
} config;
struct {
@ -77,6 +76,8 @@ struct Game {
int frames_done;
} fps_count;
ALLEGRO_CONFIG *config;
} _priv; /*!< Private resources. Do not use in gamestates! */
bool shuttingdown; /*!< If true then shut down of the game is pending. */