Scratch that, less abstractions
This commit is contained in:
@@ -0,0 +1 @@
|
||||
#include "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 <glib-2.0/glib.h>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
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;
|
||||
|
||||
-10
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <glib-2.0/glib.h>
|
||||
|
||||
#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);
|
||||
@@ -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;
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "stage.h"
|
||||
|
||||
Stage *Stage_main_menu_new ();
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <glib-2.0/glib.h>
|
||||
|
||||
#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);
|
||||
-65
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
-41
@@ -1,41 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <glib-2.0/glib.h>
|
||||
|
||||
#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);
|
||||
Reference in New Issue
Block a user