diff --git a/src/plateau.c b/src/plateau.c index 7f1bf8e..c170eb1 100644 --- a/src/plateau.c +++ b/src/plateau.c @@ -1,4 +1,5 @@ #include "../includes/plateau.h" +#include Table *generate_table(GLuint width, GLuint height, Pos origin, Uint32 color) { Table *table = malloc(sizeof(Table)); @@ -10,15 +11,23 @@ Table *generate_table(GLuint width, GLuint height, Pos origin, Uint32 color) { table->nb_columns = table->nb_row = 7; - // FIX: Allocate too much memory here - table->lines = malloc(table->nb_row * table->nb_columns * 4 * sizeof(int)); + GLuint wPas = table->width / table->nb_columns, + hPas = table->height / table->nb_row, wMax = table->width - wPas, + hMax = table->height - hPas; + + // Calcul l'espace neccessaire + int space_needed = 0; + for (GLuint i = wPas; i < wMax; i += wPas, space_needed += 4) { + for (GLuint j = hPas; j < hMax; j += hPas, space_needed += 4) { + } + } + + table->lines = malloc(space_needed * sizeof(int)); assert(table->lines != NULL); int *tmp = table->lines; - for (GLuint wPas = table->width / table->nb_columns, i = wPas; - i < table->width - wPas; i += wPas, tmp += 4) { - for (GLuint hPas = table->height / table->nb_row, j = hPas; - j < table->height - hPas; j += hPas, tmp += 4) { + for (GLuint i = wPas; i < wMax; i += wPas, tmp += 4) { + for (GLuint j = hPas; j < hMax; j += hPas, tmp += 4) { *tmp = table->width; *(tmp + 1) = j; *(tmp + 2) = origin.x;