add table

This commit is contained in:
Mylloon 2022-11-14 23:55:36 +01:00
parent 8fff1eb907
commit 1c45bc9e8d
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 137 additions and 19 deletions

24
includes/main.h Normal file
View file

@ -0,0 +1,24 @@
#ifndef OTHELLO_MAIN_H
#define OTHELLO_MAIN_H 1
#include <GL4D/gl4dp.h>
#include <GL4D/gl4duw_SDL2.h>
#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

40
includes/plateau.h Normal file
View file

@ -0,0 +1,40 @@
#ifndef OTHELLO_PLATEAU_H
#define OTHELLO_PLATEAU_H 1
#include <GL4D/gl4dp.h>
#include <GL4D/gl4du.h>
#include <stdio.h>
#include <stdlib.h>
// 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

View file

@ -1,29 +1,32 @@
#include <GL4D/gl4dp.h> #include "../includes/main.h"
#include <GL4D/gl4duw_SDL2.h> #include <assert.h>
#include <SDL2/SDL_video.h>
static void dessin(void); int main(int argc, char *argv[]) {
static void job(void); assert(GL_TRUE == gl4duwCreateWindow(argc, argv, "Othello", 10, 10, 800,
800, GL4DW_SHOWN));
int main(int argc, char **argv) { init();
if (!gl4duwCreateWindow(argc, argv, "GL4Dummies' Hello World", 10, 10, 320,
240, GL4DW_SHOWN)) {
return 1;
}
gl4dpInitScreen();
gl4duwDisplayFunc(dessin);
gl4duwDisplayFunc(draw);
gl4duwMainLoop(); 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(); gl4dpClearScreen();
job(); job();
gl4dpUpdateScreen(NULL); gl4dpUpdateScreen(NULL);
} }
void job(void) {} void job(void) { draw_table(table); }

51
src/plateau.c Normal file
View file

@ -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));
}
}