diff options
| author | 3gg <3gg@shellblade.net> | 2025-12-28 12:58:22 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-12-28 12:58:22 -0800 |
| commit | eba20320bf8d542a46dc3fd84ec401057a25b9da (patch) | |
| tree | 2a9d16ca00ba791fe81f53ffd83c507710fb89b0 /src/main.c | |
| parent | 5a079a2d114f96d4847d1ee305d5b7c16eeec50e (diff) | |
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 34 |
1 files changed, 16 insertions, 18 deletions
| @@ -14,10 +14,9 @@ | |||
| 14 | #include <stdio.h> | 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> | 15 | #include <stdlib.h> |
| 16 | 16 | ||
| 17 | static constexpr int BufferWidth = 160; | 17 | static constexpr int BufferWidth = 160; |
| 18 | static constexpr int BufferHeight = 120; | 18 | static constexpr int BufferHeight = 120; |
| 19 | static constexpr sgVec2i BufferDims = (sgVec2i){.x = BufferWidth, .y = BufferHeight}; | 19 | static constexpr R Aspect = (R)BufferWidth / (R)BufferHeight; |
| 20 | static constexpr R Aspect = (R)BufferWidth / (R)BufferHeight; | ||
| 21 | 20 | ||
| 22 | static const char* WindowTitle = "GAME"; | 21 | static const char* WindowTitle = "GAME"; |
| 23 | // Window dimensions must be an integer scaling of buffer dimensions. | 22 | // Window dimensions must be an integer scaling of buffer dimensions. |
| @@ -63,8 +62,8 @@ typedef struct Camera { | |||
| 63 | 62 | ||
| 64 | typedef struct State { | 63 | typedef struct State { |
| 65 | SDL_Window* window; | 64 | SDL_Window* window; |
| 65 | void* gfx_mem; | ||
| 66 | swgfx* gfx; | 66 | swgfx* gfx; |
| 67 | sgPixel* colour; | ||
| 68 | Model* model; | 67 | Model* model; |
| 69 | Camera camera; | 68 | Camera camera; |
| 70 | CameraController camera_controller; | 69 | CameraController camera_controller; |
| @@ -146,7 +145,7 @@ static void RenderIndexedModel(swgfx* gfx, const IndexedModel* model) { | |||
| 146 | assert(gfx); | 145 | assert(gfx); |
| 147 | assert(model); | 146 | assert(model); |
| 148 | const sgTriIdx* tris = (const sgTriIdx*)(model->data + model->offsetTris); | 147 | const sgTriIdx* tris = (const sgTriIdx*)(model->data + model->offsetTris); |
| 149 | const sgVec3* positions = (const sgVec3*)(model->data + model->offsetPositions); | 148 | const sgVec3* positions = (const sgVec3*) (model->data + model->offsetPositions); |
| 150 | sgTrianglesIndexedNonUniform(gfx, model->numTris, tris, positions); | 149 | sgTrianglesIndexedNonUniform(gfx, model->numTris, tris, positions); |
| 151 | } | 150 | } |
| 152 | 151 | ||
| @@ -206,7 +205,6 @@ static bool Render(State* state) { | |||
| 206 | 205 | ||
| 207 | const Camera* cam = &state->camera; | 206 | const Camera* cam = &state->camera; |
| 208 | 207 | ||
| 209 | sgColourBuffer(state->gfx, BufferDims, state->colour); | ||
| 210 | sgClear(state->gfx); | 208 | sgClear(state->gfx); |
| 211 | sgViewport(state->gfx, 0, 0, BufferWidth, BufferHeight); | 209 | sgViewport(state->gfx, 0, 0, BufferWidth, BufferHeight); |
| 212 | sgCheck(state->gfx); | 210 | sgCheck(state->gfx); |
| @@ -263,18 +261,17 @@ static bool Initialize(State* state) { | |||
| 263 | fprintf(stderr, "SDL_CreateWindow failed\n"); | 261 | fprintf(stderr, "SDL_CreateWindow failed\n"); |
| 264 | return false; | 262 | return false; |
| 265 | } | 263 | } |
| 266 | 264 | ||
| 267 | if (!(state->gfx = sgNew())) { | 265 | const size_t sg_mem_size = sgMem(BufferWidth, BufferHeight); |
| 268 | fprintf(stderr, "sgNew failed\n"); | 266 | if (!(state->gfx_mem = calloc(1, sg_mem_size))) { |
| 267 | fprintf(stderr, "Failed to allocate memory for graphics\n"); | ||
| 269 | return false; | 268 | return false; |
| 270 | } | 269 | } |
| 271 | 270 | ||
| 272 | if (!(state->colour = SG_ALIGN_ALLOC(BufferWidth * BufferHeight, sgPixel))) { | 271 | if (!(state->gfx = sgNew(BufferWidth, BufferHeight, state->gfx_mem))) { |
| 273 | fprintf(stderr, "Failed to allocate colour buffer\n"); | 272 | fprintf(stderr, "sgNew failed\n"); |
| 274 | return false; | 273 | return false; |
| 275 | } | 274 | } |
| 276 | |||
| 277 | sgColourBuffer(state->gfx, BufferDims, state->colour); | ||
| 278 | 275 | ||
| 279 | const char* model_path = "/home/jeanne/blender/box.mdl"; | 276 | const char* model_path = "/home/jeanne/blender/box.mdl"; |
| 280 | if (!(state->model = read_file(model_path))) { | 277 | if (!(state->model = read_file(model_path))) { |
| @@ -308,13 +305,14 @@ static void Shutdown(State* state) { | |||
| 308 | state->model = nullptr; | 305 | state->model = nullptr; |
| 309 | } | 306 | } |
| 310 | 307 | ||
| 311 | if (state->colour) { | ||
| 312 | SG_FREE(&state->colour); | ||
| 313 | } | ||
| 314 | |||
| 315 | if (state->gfx) { | 308 | if (state->gfx) { |
| 316 | sgDel(&state->gfx); | 309 | sgDel(&state->gfx); |
| 317 | } | 310 | } |
| 311 | |||
| 312 | if (state->gfx_mem) { | ||
| 313 | free(state->gfx_mem); | ||
| 314 | state->gfx_mem = nullptr; | ||
| 315 | } | ||
| 318 | 316 | ||
| 319 | if (state->window) { | 317 | if (state->window) { |
| 320 | SDL_DestroyWindow(state->window); | 318 | SDL_DestroyWindow(state->window); |
