diff --git a/src/game_state.c b/src/game_state.c new file mode 100644 index 0000000..8179e75 --- /dev/null +++ b/src/game_state.c @@ -0,0 +1 @@ +#include "game_state.h" diff --git a/src/game_state.h b/src/game_state.h new file mode 100644 index 0000000..edfca31 --- /dev/null +++ b/src/game_state.h @@ -0,0 +1,58 @@ +#pragma once +/* This houses the state of an actual, running game; your player, the + other players, the world map, buildings. TODO: move some stuff to + separate files. */ + +#include +#include + +typedef struct Player +{ + unsigned int id; + const char *name; + SDL_Color color; /* 4 bytes */ + + unsigned int power; + unsigned int population, max_population; + + gboolean is_alive : 1, is_human : 1; +} Player; + +typedef enum TerrainType + { + TERRAIN_WATER, + TERRAIN_FLAT, + TERRAIN_HILL, + TERRAIN_MOUNTAIN + } TerrainType; + +typedef struct MapTile +{ + unsigned int x, y; + unsigned int owner_id; + TerrainType terrain_type; + + gboolean is_defended : 1, is_radiated : 1; +} MapTile; + +typedef struct Building +{ + MapTile *parent_tile; + unsigned int level; +} Building; + +typedef struct World +{ + GArray *tiles; + GArray *buildings; + unsigned int width, height; +} World; + +typedef struct GameState +{ + World world; + + GList *players; + Player *me; +} GameState; + diff --git a/src/main.c b/src/main.c index deaf2fb..e710d1b 100644 --- a/src/main.c +++ b/src/main.c @@ -6,10 +6,8 @@ #include "util.h" #include "gfx_context.h" -#include "stage/stage_manager.h" static GfxContext *gfx_ctx; -static StageManager *stageman; SDL_AppResult SDL_AppInit (void **appstate, int argc, char **argv) @@ -33,11 +31,8 @@ SDL_AppInit (void **appstate, int argc, char **argv) g_error ("Couldn't initialize SDL: %s", SDL_GetError ()); } - stageman = StageManager_new (); gfx_ctx = GfxContext_new (); - StageManager_switch (stageman, "main_menu"); - return SDL_APP_CONTINUE; } @@ -51,8 +46,6 @@ SDL_AppEvent (void *appstate, SDL_Event *event) return SDL_APP_SUCCESS; } - StageManager_on_SDL_Event (stageman, event); - return SDL_APP_CONTINUE; } @@ -61,9 +54,7 @@ SDL_AppIterate (void *appstate) { UNUSED (appstate); - StageManager_update (stageman); GfxContext_prerender (gfx_ctx); - StageManager_render (stageman, gfx_ctx); GfxContext_postrender (gfx_ctx); return SDL_APP_CONTINUE; @@ -75,5 +66,4 @@ SDL_AppQuit (void *appstate, SDL_AppResult result) UNUSED (appstate, result); GfxContext_free (gfx_ctx); - StageManager_free (stageman); } diff --git a/src/stage/stage.c b/src/stage/stage.c deleted file mode 100644 index 6c980e3..0000000 --- a/src/stage/stage.c +++ /dev/null @@ -1,48 +0,0 @@ -#include "stage.h" - -Stage * -Stage_new () -{ - Stage *g = g_new0 (Stage, 1); - return g; -} - -void -Stage_add_event_handler (Stage *self, int priority, unsigned int on_event, - gboolean (*predicate) (SDL_Event *sdl_event, void *userdata), void *predicate_data, - gboolean (*callback) (SDL_Event *sdl_event, void *userdata), void *callback_data) -{ - EventHandler *h = g_new (EventHandler, 1); - h->on_event = on_event; - h->priority = priority; - h->predicate_data = predicate_data; - h->callback_data = callback_data; - h->predicate = predicate; - h->callback = callback; - - GList *iter = self->event_handlers; - while (iter && ((EventHandler *) iter->data)->priority >= priority) - { - iter = iter->next; - } - - self->event_handlers = g_list_insert_before (self->event_handlers, iter, h); -} - -void -Stage_handle_SDL_Event (Stage *self, SDL_Event *event) -{ - for (GList *l = self->event_handlers; l != NULL; l = l->next) - { - EventHandler *h = (EventHandler *) l->data; - - if ((h->on_event == 0 || h->on_event == event->type) - && (!h->predicate || h->predicate (event, h->predicate_data))) - { - if (h->callback && h->callback (event, h->callback_data)) - { - break; - } - } - } -} diff --git a/src/stage/stage.h b/src/stage/stage.h deleted file mode 100644 index c937ea2..0000000 --- a/src/stage/stage.h +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once - -#include -#include - -#include "../util.h" -#include "../gfx_context.h" - -typedef struct { - unsigned int on_event; - int priority; - - void *predicate_data, *callback_data; - gboolean (*predicate) (SDL_Event *sdl_event, void *userdata); - gboolean (*callback) (SDL_Event *sdl_event, void *userdata); -} EventHandler; - -typedef struct Stage -{ - /* There should be like an UI context here, the World, and so - on. Both could be NULL. */ - - GList *event_handlers; - - void (*on_enter) (struct Stage *); - void (*on_exit) (struct Stage *); - void (*on_update) (struct Stage *); - void (*on_render) (struct Stage *, GfxContext *); -} Stage; - -Stage *Stage_new (); -void Stage_add_event_handler (Stage *self, int priority, unsigned int on_event, - gboolean (*predicate) (SDL_Event *, void *), void *predicate_data, - gboolean (*callback) (SDL_Event *, void *), void *callback_data); -void Stage_handle_SDL_Event (Stage *self, SDL_Event *event); - -gboolean predicate_key (SDL_Event *event, void *data); - -gboolean callback_switch_stage (SDL_Event *event, void *data); diff --git a/src/stage/stage_main_menu.c b/src/stage/stage_main_menu.c deleted file mode 100644 index 5cf6058..0000000 --- a/src/stage/stage_main_menu.c +++ /dev/null @@ -1,25 +0,0 @@ -#include "stage_main_menu.h" - -static void -_on_enter (Stage *self) -{ - UNUSED (self); -} - -static void -_on_render (Stage *self, GfxContext *gfx_ctx) -{ - UNUSED (self); - - SDL_SetRenderDrawColor (gfx_ctx->renderer, 255, 0, 0, 255); -} - -Stage * -Stage_main_menu_new () -{ - Stage *self = Stage_new (); - self->on_enter = _on_enter; - self->on_render = _on_render; - - return self; -} diff --git a/src/stage/stage_main_menu.h b/src/stage/stage_main_menu.h deleted file mode 100644 index 751f0b0..0000000 --- a/src/stage/stage_main_menu.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -#include "stage.h" - -Stage *Stage_main_menu_new (); diff --git a/src/stage/stage_manager.c b/src/stage/stage_manager.c deleted file mode 100644 index 47a7937..0000000 --- a/src/stage/stage_manager.c +++ /dev/null @@ -1,70 +0,0 @@ -#include "stage_manager.h" - -StageManager * -StageManager_new () -{ - StageManager *self = g_new0 (StageManager, 1); - self->stages = g_hash_table_new (g_str_hash, g_str_equal); - - StageManager_add (self, "main_menu", Stage_main_menu_new ()); - - return self; -} - -void -StageManager_add (StageManager *self, const char *name, Stage *stage) -{ - g_hash_table_replace (self->stages, g_strdup (name), stage); -} - -void -StageManager_switch (StageManager *self, const char *name) -{ - Stage *next_stage = g_hash_table_lookup (self->stages, name); - - if (self->current && self->current->on_exit) - { - self->current->on_exit (self->current); - } - - if (next_stage && next_stage->on_enter) - { - next_stage->on_enter (next_stage); - self->current = next_stage; - } -} - -void -StageManager_free (StageManager *self) -{ - g_free (self->stages); - g_free (self); -} - -void -StageManager_update (StageManager *self) -{ - if (self->current && self->current->on_update) - { - self->current->on_update (self->current); - } -} - -void -StageManager_on_SDL_Event (StageManager *self, SDL_Event *event) -{ - if (self->current && self->current->event_handlers) - { - Stage_handle_SDL_Event (self->current, event); - } -} - -void -StageManager_render (StageManager *self, GfxContext *ctx) -{ - if (self->current && self->current->on_render) - { - self->current->on_render (self->current, ctx); - } -} - diff --git a/src/stage/stage_manager.h b/src/stage/stage_manager.h deleted file mode 100644 index 01269c4..0000000 --- a/src/stage/stage_manager.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include -#include - -#include "stage_main_menu.h" - -typedef struct StageManager -{ - GHashTable *stages; - Stage *current, *global; -} StageManager; - -StageManager *StageManager_new (); -void StageManager_add (StageManager *self, const char *name, Stage *stage); -void StageManager_switch (StageManager *self, const char *name); -void StageManager_free (StageManager *self); - -void StageManager_update (StageManager *self); -void StageManager_on_SDL_Event (StageManager *self, SDL_Event *event); -void StageManager_render (StageManager *self, GfxContext *ctx); diff --git a/src/world.c b/src/world.c deleted file mode 100644 index aa0c9c5..0000000 --- a/src/world.c +++ /dev/null @@ -1,65 +0,0 @@ -#include "world.h" - -World * -World_random (unsigned int width, unsigned int height) -{ - World *w = g_new0 (World, 1); - w->width = width; - w->height = height; - w->tiles = g_array_sized_new (FALSE, TRUE, sizeof (MapTile), width * height); - - for (unsigned int i = 0; i < w->tiles->len; ++i) - { - /* This should actually be allocated on the heap and be a - pointer, because it's gonna be modified a lot. Just change - all the MapTile things in g_array_* functions to MapTile*, - and allocate with g_new0/g_new here. */ - MapTile mt; - mt.terrain_type = rand () > 0.5 ? TERRAIN_FLAT : TERRAIN_HILL; - g_array_append_val (w->tiles, mt); - } - - return w; -} - -void -World_update (World *w) -{ - UNUSED (w); -} - -void -World_render (World *w, SDL_Renderer *renderer) -{ - UNUSED (w); - - for (unsigned int y = 0; y < w->height; ++y) - { - for (unsigned int x = 0; x < w->width; ++x) - { - unsigned int i = y * w->width + x; - MapTile mt = g_array_index (w->tiles, MapTile, i); - - switch (mt.terrain_type) - { - case TERRAIN_HILL: - SDL_SetRenderDrawColor (renderer, 255, 0, 0, 255); - break; - default: - SDL_SetRenderDrawColor (renderer, 0, 255, 0, 255); - break; - } - - /* Very unoptimized --- for now. */ - SDL_FRect pixel = { - .x = x, - .y = y, - .w = 1, - .h = 1 - }; - - SDL_RenderFillRect (renderer, &pixel); - } - } -} - diff --git a/src/world.h b/src/world.h deleted file mode 100644 index 2a093f8..0000000 --- a/src/world.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once - -#include -#include - -#include "util.h" - -typedef enum TerrainType - { - TERRAIN_FLAT, - TERRAIN_HILL, - TERRAIN_MOUNTAIN - } TerrainType; - -typedef enum BuildingType - { - BUILDING_NONE, - BUILDING_PORT, - BUILDING_CITY, - BUILDING_FORT - } BuildingType; - -typedef struct MapTile -{ - int player_id; - TerrainType terrain_type; - BuildingType building_type; - - gboolean is_radiated; - gboolean is_defended; -} MapTile; - -typedef struct World -{ - unsigned int width, height; - GArray *tiles; -} World; - -World *World_random (unsigned int width, unsigned int height); -void World_update (World *w); -void World_render (World *w, SDL_Renderer *renderer);