add table
This commit is contained in:
parent
8fff1eb907
commit
1c45bc9e8d
4 changed files with 137 additions and 19 deletions
24
includes/main.h
Normal file
24
includes/main.h
Normal 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
40
includes/plateau.h
Normal 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
|
41
src/main.c
41
src/main.c
|
@ -1,29 +1,32 @@
|
|||
#include <GL4D/gl4dp.h>
|
||||
#include <GL4D/gl4duw_SDL2.h>
|
||||
#include <SDL2/SDL_video.h>
|
||||
#include "../includes/main.h"
|
||||
#include <assert.h>
|
||||
|
||||
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); }
|
||||
|
|
51
src/plateau.c
Normal file
51
src/plateau.c
Normal 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));
|
||||
}
|
||||
}
|
Reference in a new issue