From eba20320bf8d542a46dc3fd84ec401057a25b9da Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sun, 28 Dec 2025 12:58:22 -0800 Subject: swgfx no longer allocates --- src/main.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 1c017f0..e0b54c1 100644 --- a/src/main.c +++ b/src/main.c @@ -14,10 +14,9 @@ #include #include -static constexpr int BufferWidth = 160; -static constexpr int BufferHeight = 120; -static constexpr sgVec2i BufferDims = (sgVec2i){.x = BufferWidth, .y = BufferHeight}; -static constexpr R Aspect = (R)BufferWidth / (R)BufferHeight; +static constexpr int BufferWidth = 160; +static constexpr int BufferHeight = 120; +static constexpr R Aspect = (R)BufferWidth / (R)BufferHeight; static const char* WindowTitle = "GAME"; // Window dimensions must be an integer scaling of buffer dimensions. @@ -63,8 +62,8 @@ typedef struct Camera { typedef struct State { SDL_Window* window; + void* gfx_mem; swgfx* gfx; - sgPixel* colour; Model* model; Camera camera; CameraController camera_controller; @@ -146,7 +145,7 @@ static void RenderIndexedModel(swgfx* gfx, const IndexedModel* model) { assert(gfx); assert(model); const sgTriIdx* tris = (const sgTriIdx*)(model->data + model->offsetTris); - const sgVec3* positions = (const sgVec3*)(model->data + model->offsetPositions); + const sgVec3* positions = (const sgVec3*) (model->data + model->offsetPositions); sgTrianglesIndexedNonUniform(gfx, model->numTris, tris, positions); } @@ -206,7 +205,6 @@ static bool Render(State* state) { const Camera* cam = &state->camera; - sgColourBuffer(state->gfx, BufferDims, state->colour); sgClear(state->gfx); sgViewport(state->gfx, 0, 0, BufferWidth, BufferHeight); sgCheck(state->gfx); @@ -263,18 +261,17 @@ static bool Initialize(State* state) { fprintf(stderr, "SDL_CreateWindow failed\n"); return false; } - - if (!(state->gfx = sgNew())) { - fprintf(stderr, "sgNew failed\n"); + + const size_t sg_mem_size = sgMem(BufferWidth, BufferHeight); + if (!(state->gfx_mem = calloc(1, sg_mem_size))) { + fprintf(stderr, "Failed to allocate memory for graphics\n"); return false; } - if (!(state->colour = SG_ALIGN_ALLOC(BufferWidth * BufferHeight, sgPixel))) { - fprintf(stderr, "Failed to allocate colour buffer\n"); + if (!(state->gfx = sgNew(BufferWidth, BufferHeight, state->gfx_mem))) { + fprintf(stderr, "sgNew failed\n"); return false; } - - sgColourBuffer(state->gfx, BufferDims, state->colour); const char* model_path = "/home/jeanne/blender/box.mdl"; if (!(state->model = read_file(model_path))) { @@ -308,13 +305,14 @@ static void Shutdown(State* state) { state->model = nullptr; } - if (state->colour) { - SG_FREE(&state->colour); - } - if (state->gfx) { sgDel(&state->gfx); } + + if (state->gfx_mem) { + free(state->gfx_mem); + state->gfx_mem = nullptr; + } if (state->window) { SDL_DestroyWindow(state->window); -- cgit v1.2.3