From 1c45bc9e8d4b697e1b6a93da4609eb0fd31138a9 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 14 Nov 2022 23:55:36 +0100 Subject: [PATCH] add table --- includes/main.h | 24 ++++++++++++++++++++++ includes/plateau.h | 40 ++++++++++++++++++++++++++++++++++++ src/main.c | 41 ++++++++++++++++++++----------------- src/plateau.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 137 insertions(+), 19 deletions(-) create mode 100644 includes/main.h create mode 100644 includes/plateau.h create mode 100644 src/plateau.c diff --git a/includes/main.h b/includes/main.h new file mode 100644 index 0000000..e419fbc --- /dev/null +++ b/includes/main.h @@ -0,0 +1,24 @@ +#ifndef OTHELLO_MAIN_H +#define OTHELLO_MAIN_H 1 + +#include +#include + +#include "../includes/plateau.h" + +// Plateau +static Table *table; + +// Initialise le programme +static void init(void); + +// Série d'évènement pendant chaque rendu de frame +static void job(void); + +// Dessine dans la fenêtre +static void draw(void); + +// Quitte le programme +static void quit(void); + +#endif diff --git a/includes/plateau.h b/includes/plateau.h new file mode 100644 index 0000000..7ce2c47 --- /dev/null +++ b/includes/plateau.h @@ -0,0 +1,40 @@ +#ifndef OTHELLO_PLATEAU_H +#define OTHELLO_PLATEAU_H 1 + +#include +#include + +#include +#include + +// Position d'un point x, y +struct pos { + GLuint x, y; +}; +typedef struct pos Pos; + +// Plateau +struct table { + GLuint width; + GLuint height; + Pos origin; + + int nb_row; + int nb_columns; + + int *lines; + + Uint32 color; +}; +typedef struct table Table; + +// Génère une structure de plateau +Table *generate_table(GLuint width, GLuint height, Pos origin, Uint32 color); + +// Libère le plateau en mémoire +void free_table(Table *table); + +/* Dessine le plateau */ +void draw_table(Table *); + +#endif diff --git a/src/main.c b/src/main.c index dfeeb41..2765aad 100644 --- a/src/main.c +++ b/src/main.c @@ -1,29 +1,32 @@ -#include -#include -#include +#include "../includes/main.h" +#include -static void dessin(void); -static void job(void); - -int main(int argc, char **argv) { - - if (!gl4duwCreateWindow(argc, argv, "GL4Dummies' Hello World", 10, 10, 320, - 240, GL4DW_SHOWN)) { - return 1; - } - - gl4dpInitScreen(); - - gl4duwDisplayFunc(dessin); +int main(int argc, char *argv[]) { + assert(GL_TRUE == gl4duwCreateWindow(argc, argv, "Othello", 10, 10, 800, + 800, GL4DW_SHOWN)); + init(); + gl4duwDisplayFunc(draw); gl4duwMainLoop(); - return 0; + + return EXIT_SUCCESS; } -void dessin(void) { +void init(void) { + gl4dpInitScreen(); + + table = generate_table(gl4dpGetWidth(), gl4dpGetHeight(), (Pos){0, 0}, + RGB(255, 255, 255)); + + atexit(quit); +} + +void quit(void) { free_table(table); } + +void draw(void) { gl4dpClearScreen(); job(); gl4dpUpdateScreen(NULL); } -void job(void) {} +void job(void) { draw_table(table); } diff --git a/src/plateau.c b/src/plateau.c new file mode 100644 index 0000000..be645f2 --- /dev/null +++ b/src/plateau.c @@ -0,0 +1,51 @@ +#include "../includes/plateau.h" + +Table *generate_table(GLuint width, GLuint height, Pos origin, Uint32 color) { + Table *table = malloc(sizeof(Table *)); + assert(table != NULL); + + table->width = width; + table->height = height; + // TODO: Use origin + table->origin = origin; + table->color = 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)); + 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) { + *tmp = table->width; + *(tmp + 1) = j; + *(tmp + 2) = 0; + *(tmp + 3) = j; + } + + *tmp = i; + *(tmp + 1) = 0; + *(tmp + 2) = i; + *(tmp + 3) = table->height; + } + *(tmp) = -1; + + return table; +} + +void free_table(Table *table) { + free(table->lines); + free(table); +} + +void draw_table(Table *table) { + gl4dpSetColor(table->color); + + for (int *ptr = table->lines; *(ptr) != -1; ptr += 4) { + gl4dpLine(*ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3)); + } +}