World data structure

This commit is contained in:
2025-09-08 19:39:12 +02:00
parent f2be16de9c
commit a8e4e36996
4 changed files with 118 additions and 3 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
/build/ /build/
/wschod /librefront
/perf.data* /perf.data*
/massif.out* /massif.out*
+11 -2
View File
@@ -6,8 +6,11 @@
#include "util.h" #include "util.h"
SDL_Window *sdl_window; #include "world.h"
SDL_Renderer *sdl_renderer;
static SDL_Window *sdl_window;
static SDL_Renderer *sdl_renderer;
/* static World *world; */
SDL_AppResult SDL_AppResult
SDL_AppInit (void **appstate, int argc, char **argv) SDL_AppInit (void **appstate, int argc, char **argv)
@@ -24,6 +27,8 @@ SDL_AppInit (void **appstate, int argc, char **argv)
SDL_SetAppMetadata ("LibreFront", "0.1", SDL_SetAppMetadata ("LibreFront", "0.1",
"https://git.cfpi-fpsi.eu:8443/chopin-interactive/librefront"); "https://git.cfpi-fpsi.eu:8443/chopin-interactive/librefront");
g_info ("Welcome to LibreFront");
if (!SDL_Init (SDL_INIT_VIDEO)) if (!SDL_Init (SDL_INIT_VIDEO))
{ {
g_error ("Couldn't initialize SDL: %s", SDL_GetError ()); g_error ("Couldn't initialize SDL: %s", SDL_GetError ());
@@ -43,6 +48,8 @@ SDL_AppInit (void **appstate, int argc, char **argv)
g_error ("Couldn't create renderer: %s", SDL_GetError ()); g_error ("Couldn't create renderer: %s", SDL_GetError ());
} }
/* world = World_random (128, 128); */
return SDL_APP_CONTINUE; return SDL_APP_CONTINUE;
} }
@@ -64,7 +71,9 @@ SDL_AppIterate (void *appstate)
{ {
UNUSED (appstate); UNUSED (appstate);
/* World_update (world); */
SDL_RenderClear (sdl_renderer); SDL_RenderClear (sdl_renderer);
/* World_render (world, sdl_renderer); */
SDL_RenderPresent (sdl_renderer); SDL_RenderPresent (sdl_renderer);
return SDL_APP_CONTINUE; return SDL_APP_CONTINUE;
+65
View File
@@ -0,0 +1,65 @@
#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
View File
@@ -0,0 +1,41 @@
#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);