don't store the rows on the heap; it works just fine on the stack

This commit is contained in:
2025-04-16 14:28:08 +02:00
parent 271d4e47d4
commit 17a7502003
2 changed files with 4 additions and 13 deletions

View File

@@ -9,9 +9,7 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include "../error.h"
#include "../window/audio.h" #include "../window/audio.h"
#include "../window/colour/colour8.h"
#include "./tetromino/shapes.h" #include "./tetromino/shapes.h"
#include "gametime.h" #include "gametime.h"
#include "tetromino/placing.h" #include "tetromino/placing.h"
@@ -54,6 +52,7 @@ void game_init(gamedata* const dat) {
audiodevice* ad = audio_device_init(32000, AUDIO_S16, 1, 4096); audiodevice* ad = audio_device_init(32000, AUDIO_S16, 1, 4096);
*dat = (gamedata){ *dat = (gamedata){
{0}, // rowdat
{0}, // row {0}, // row
gt, // time gt, // time
ad, // audio_device ad, // audio_device
@@ -72,10 +71,7 @@ void game_init(gamedata* const dat) {
// initialize the rows within the game data // initialize the rows within the game data
for (int8_t i = 0; i < ROWS; i++) { for (int8_t i = 0; i < ROWS; i++) {
dat->rows[i] = calloc(COLUMNS, sizeof(colour8)); dat->rows[i] = dat->rowdat + (i * COLUMNS);
if (dat->rows[i] == NULL)
fatal(ERROR_STD_MEMORY_INIT, __FILE_NAME__, __LINE__, "something went wrong when allocating memory for row %i", i);
} }
// set the shape data in each slot to it's corrsponding ID // set the shape data in each slot to it's corrsponding ID
@@ -142,12 +138,6 @@ void game_free(gamedata* const dat) {
audio_wav_unload(&dat->place_sfx); audio_wav_unload(&dat->place_sfx);
audio_device_free(dat->audio_device); audio_device_free(dat->audio_device);
// clear each row
for (int8_t i = 0; i < ROWS; i++) {
free(dat->rows[i]);
dat->rows[i] = NULL;
}
// zero-out the rest of the data // zero-out the rest of the data
*dat = (gamedata){0}; *dat = (gamedata){0};
} }

View File

@@ -23,7 +23,8 @@ typedef colour8 const* const row_const;
typedef colour8* row; typedef colour8* row;
typedef struct { typedef struct {
row rows[ROWS]; colour8 rowdat[ROWS * COLUMNS];
colour8* rows[ROWS];
struct gametime time; struct gametime time;
audiodevice* audio_device; audiodevice* audio_device;
audiodata music; audiodata music;