pass pointer to data structure to/from gamestates functions

This commit is contained in:
Sebastian Krzyszkowiak 2012-12-26 13:24:34 +01:00
parent 232cc6a204
commit 3daa3c714f
4 changed files with 49 additions and 52 deletions

View file

@ -35,20 +35,21 @@ struct Gamestate {
unsigned char fade_counter;
char** after; // TODO: and this one too?
struct Gamestate* next;
void* data;
struct {
void (*Gamestate_Draw)(struct Game *game);
void (*Gamestate_Logic)(struct Game *game);
void (*Gamestate_Draw)(struct Game *game, void* data);
void (*Gamestate_Logic)(struct Game *game, void* data);
void (*Gamestate_Load)(struct Game *game, void (*progress)(struct Game *game));
void (*Gamestate_Start)(struct Game *game);
void (*Gamestate_Pause)(struct Game *game);
void (*Gamestate_Resume)(struct Game *game);
void (*Gamestate_Stop)(struct Game *game);
void (*Gamestate_Unload)(struct Game *game);
void* (*Gamestate_Load)(struct Game *game, void (*progress)(struct Game *game));
void (*Gamestate_Start)(struct Game *game, void* data);
void (*Gamestate_Pause)(struct Game *game, void* data);
void (*Gamestate_Resume)(struct Game *game, void* data);
void (*Gamestate_Stop)(struct Game *game, void* data);
void (*Gamestate_Unload)(struct Game *game, void* data);
void (*Gamestate_ProcessEvent)(struct Game *game, ALLEGRO_EVENT *ev);
void (*Gamestate_Keydown)(struct Game *game, ALLEGRO_EVENT *ev); // TODO: rly?
void (*Gamestate_Reload)(struct Game *game);
void (*Gamestate_ProcessEvent)(struct Game *game, void* data, ALLEGRO_EVENT *ev);
void (*Gamestate_Keydown)(struct Game *game, void* data, ALLEGRO_EVENT *ev); // TODO: rly?
void (*Gamestate_Reload)(struct Game *game, void* data);
int *Gamestate_ProgressCount;
} api;

View file

@ -18,31 +18,28 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
//#include <stdio.h>
#include "../utils.h"
#include "disclaimer.h"
struct {
ALLEGRO_FONT *font, *font_small;
} res;
// FIXME: bad bad bad bad!
int Gamestate_ProgressCount = 0;
void Gamestate_Logic(struct Game *game) {
}
void Gamestate_Logic(struct Game *game, struct Disclaimer_Resources* data) {}
void Gamestate_Draw(struct Game *game) {
void Gamestate_Draw(struct Game *game, struct Disclaimer_Resources* data) {
al_clear_to_color(al_map_rgb(0,0,0));
DrawTextWithShadow(res.font, al_map_rgb(255,255,255), game->viewport.width/2, game->viewport.height*0.3, ALLEGRO_ALIGN_CENTRE, "This is an early development preview of the game.");
DrawTextWithShadow(res.font, al_map_rgb(255,255,255), game->viewport.width/2, game->viewport.height*0.4, ALLEGRO_ALIGN_CENTRE, "It's not supposed to be complete!");
DrawTextWithShadow(res.font, al_map_rgb(255,255,255), game->viewport.width/2, game->viewport.height*0.5, ALLEGRO_ALIGN_CENTRE, "Keep in mind that everything may be changed");
DrawTextWithShadow(res.font, al_map_rgb(255,255,255), game->viewport.width/2, game->viewport.height*0.6, ALLEGRO_ALIGN_CENTRE, "and many things surely will change.");
DrawTextWithShadow(res.font_small, al_map_rgb(255,255,255), game->viewport.width/2, game->viewport.height*0.9, ALLEGRO_ALIGN_CENTRE, "Press any key to continue...");
DrawTextWithShadow(data->font, al_map_rgb(255,255,255), game->viewport.width/2, game->viewport.height*0.3, ALLEGRO_ALIGN_CENTRE, "This is an early development preview of the game.");
DrawTextWithShadow(data->font, al_map_rgb(255,255,255), game->viewport.width/2, game->viewport.height*0.4, ALLEGRO_ALIGN_CENTRE, "It's not supposed to be complete!");
DrawTextWithShadow(data->font, al_map_rgb(255,255,255), game->viewport.width/2, game->viewport.height*0.5, ALLEGRO_ALIGN_CENTRE, "Keep in mind that everything may be changed");
DrawTextWithShadow(data->font, al_map_rgb(255,255,255), game->viewport.width/2, game->viewport.height*0.6, ALLEGRO_ALIGN_CENTRE, "and many things surely will change.");
DrawTextWithShadow(data->font_small, al_map_rgb(255,255,255), game->viewport.width/2, game->viewport.height*0.9, ALLEGRO_ALIGN_CENTRE, "Press any key to continue...");
}
void Gamestate_Start(struct Game *game) {
void Gamestate_Start(struct Game *game, struct Disclaimer_Resources* data) {
FadeGamestate(game, true);
}
int Gamestate_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
int Gamestate_Keydown(struct Game *game, struct Disclaimer_Resources* data, ALLEGRO_EVENT *ev) {
StopGamestate(game, "disclaimer");
UnloadGamestate(game, "disclaimer");
LoadGamestate(game, "intro");
@ -50,26 +47,27 @@ int Gamestate_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
return 0;
}
void Gamestate_Load(struct Game *game, void (*progress)(struct Game*)) {
res.font_small = al_load_ttf_font(GetDataFilePath("fonts/ShadowsIntoLight.ttf"),game->viewport.height*0.05,0 );
res.font = al_load_ttf_font(GetDataFilePath("fonts/ShadowsIntoLight.ttf"),game->viewport.height*0.065,0 );
void* Gamestate_Load(struct Game *game, void (*progress)(struct Game*)) {
struct Disclaimer_Resources *data = malloc(sizeof(struct Disclaimer_Resources));
data->font_small = al_load_ttf_font(GetDataFilePath("fonts/ShadowsIntoLight.ttf"),game->viewport.height*0.05,0 );
data->font = al_load_ttf_font(GetDataFilePath("fonts/ShadowsIntoLight.ttf"),game->viewport.height*0.065,0 );
if (progress) (*progress)(game);
//PrintConsole(game, "Preloading GAMESTATE_INTRO...");
//Intro_Preload(game, progress);
return data;
}
void Gamestate_Stop(struct Game *game) {
void Gamestate_Stop(struct Game *game, struct Disclaimer_Resources* data) {
FadeGamestate(game, false);
}
void Gamestate_Unload(struct Game *game) {
al_destroy_font(res.font);
al_destroy_font(res.font_small);
void Gamestate_Unload(struct Game *game, struct Disclaimer_Resources* data) {
al_destroy_font(data->font);
al_destroy_font(data->font_small);
}
void Gamestate_Reload(struct Game *game) {}
void Gamestate_Reload(struct Game *game, struct Disclaimer_Resources* data) {}
void Gamestate_Resume(struct Game *game) {}
void Gamestate_Pause(struct Game *game) {}
void Gamestate_ProcessEvent(struct Game *game, ALLEGRO_EVENT *ev) {}
int Gamestate_ProgressCount = 0;
void Gamestate_Resume(struct Game *game, struct Disclaimer_Resources* data) {}
void Gamestate_Pause(struct Game *game, struct Disclaimer_Resources* data) {}
void Gamestate_ProcessEvent(struct Game *game, struct Disclaimer_Resources* data, ALLEGRO_EVENT *ev) {}

View file

@ -19,8 +19,6 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
void Gamestate_Draw(struct Game *game);
void Gamestate_Preload(struct Game *game, void (*progress)(struct Game*, float));
void Gamestate_Unload(struct Game *game);
void Gamestate_Load(struct Game *game);
int Gamestate_Keydown(struct Game *game, ALLEGRO_EVENT *ev);
struct Disclaimer_Resources {
ALLEGRO_FONT *font, *font_small;
};

View file

@ -62,7 +62,7 @@ void DrawGamestates(struct Game *game) {
while (tmp) {
if ((tmp->loaded) && (tmp->started)) {
//PrintConsole(game, "drawing %s", tmp->name);
(*tmp->api.Gamestate_Draw)(game);
(*tmp->api.Gamestate_Draw)(game, tmp->data);
}
tmp = tmp->next;
}
@ -78,7 +78,7 @@ void LogicGamestates(struct Game *game) {
while (tmp) {
if ((tmp->loaded) && (tmp->started) && (!tmp->paused)) {
//PrintConsole(game, "logic %s", tmp->name);
(*tmp->api.Gamestate_Logic)(game);
(*tmp->api.Gamestate_Logic)(game, tmp->data);
}
tmp = tmp->next;
}
@ -88,7 +88,7 @@ void KeydownGamestates(struct Game *game, ALLEGRO_EVENT *ev) {
struct Gamestate *tmp = game->_priv.gamestates;
while (tmp) {
if ((tmp->loaded) && (tmp->started) && (!tmp->paused)) {
(*tmp->api.Gamestate_Keydown)(game, ev);
(*tmp->api.Gamestate_Keydown)(game, tmp->data, ev);
}
tmp = tmp->next;
}
@ -98,7 +98,7 @@ void EventGamestates(struct Game *game, ALLEGRO_EVENT *ev) {
struct Gamestate *tmp = game->_priv.gamestates;
while (tmp) {
if ((tmp->loaded) && (tmp->started) && (!tmp->paused)) {
(*tmp->api.Gamestate_ProcessEvent)(game, ev);
(*tmp->api.Gamestate_ProcessEvent)(game, tmp->data, ev);
}
tmp = tmp->next;
}
@ -365,14 +365,14 @@ int main(int argc, char **argv){
if (!(tmp->api.Gamestate_ProgressCount = dlsym(tmp->handle, "Gamestate_ProgressCount"))) { gs_error(); continue; }
(*tmp->api.Gamestate_Load)(&game, NULL);
tmp->data = (*tmp->api.Gamestate_Load)(&game, NULL);
tmp->loaded = true;
tmp->pending_load = false;
}
} else if ((tmp->pending_start) && (tmp->started)) {
PrintConsole(&game, "Stopping gamestate %s...", tmp->name);
(*tmp->api.Gamestate_Stop)(&game);
(*tmp->api.Gamestate_Stop)(&game, tmp->data);
tmp->started = false;
tmp->pending_start = false;
}
@ -380,7 +380,7 @@ int main(int argc, char **argv){
PrintConsole(&game, "Unloading gamestate %s...", tmp->name);
tmp->loaded = false;
tmp->pending_load = false;
(*tmp->api.Gamestate_Unload)(&game);
(*tmp->api.Gamestate_Unload)(&game, tmp->data);
dlclose(tmp->handle);
tmp->handle = NULL;
} else if ((tmp->pending_start) && (!tmp->started)) {
@ -389,7 +389,7 @@ int main(int argc, char **argv){
tmp->pending_start = false;
} else {
PrintConsole(&game, "Starting gamestate %s...", tmp->name);
(*tmp->api.Gamestate_Start)(&game);
(*tmp->api.Gamestate_Start)(&game, tmp->data);
tmp->started = true;
tmp->pending_start = false;
}