better text impl

This commit is contained in:
Mylloon 2022-11-15 23:44:29 +01:00
parent cbc804efd2
commit 8abd8aafa1
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 20 additions and 15 deletions

View file

@ -9,7 +9,8 @@
TTF_Font *load_font(int quality);
// Écrit du texte à une certaine position
void write_text(TTF_Font *font, GLfloat x, GLfloat y, char *text);
void write_text(TTF_Font *font, char *text, GLfloat *s, GLfloat *t,
Uint32 color);
// Décharge la police
void unload_font(TTF_Font *font);

View file

@ -29,12 +29,15 @@ void quit(void) {
}
void draw(void) {
gl4dpClearScreenWith(RGB(0, 0, 0));
job();
gl4dpUpdateScreen(NULL);
}
void job(void) {
write_text(font, 1.2, 10., "Othello");
char title[21];
sprintf(title, "Othello - tour des %c", 'B');
write_text(font, title, (GLfloat[2]){.6, -.1}, (GLfloat[2]){.3, 10.},
RGB(255, 255, 255));
draw_table(table);
}

View file

@ -8,7 +8,7 @@ Table *generate_table(GLuint width, GLuint height, Pos origin, Uint32 color) {
table->height = height;
table->color = color;
table->nb_columns = table->nb_row = 7;
table->nb_columns = table->nb_row = 8;
GLuint wPas = table->width / table->nb_columns,
hPas = table->height / table->nb_row, wMax = table->width - wPas,
@ -16,8 +16,8 @@ Table *generate_table(GLuint width, GLuint height, Pos origin, Uint32 color) {
// Calcul l'espace neccessaire
int space_needed = 1;
for (GLuint i = wPas; i < wMax; i += wPas, space_needed += 4) {
for (GLuint j = hPas; j < hMax; j += hPas, space_needed += 4) {
for (GLuint i = wPas; i <= wMax; i += wPas, space_needed += 4) {
for (GLuint j = hPas; j <= hMax; j += hPas, space_needed += 4) {
}
}
assert(space_needed != 0);
@ -26,8 +26,8 @@ Table *generate_table(GLuint width, GLuint height, Pos origin, Uint32 color) {
assert(table->lines != NULL);
int *tmp = table->lines;
for (GLuint i = wPas; i < wMax; i += wPas, tmp += 4) {
for (GLuint j = hPas; j < hMax; 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;

View file

@ -1,6 +1,6 @@
#include "../includes/text.h"
#include "SDL_ttf.h"
#include <GL4D/gl4dp.h>
#include <GL4D/gl4duw_SDL2.h>
TTF_Font *load_font(int quality) {
if (TTF_Init() == -1) {
@ -11,14 +11,15 @@ TTF_Font *load_font(int quality) {
return TTF_OpenFont("fonts/Hubot-Sans-Regular.ttf", quality);
}
void write_text(TTF_Font *font, GLfloat x, GLfloat y, char *text) {
void write_text(TTF_Font *font, char *text, GLfloat *s, GLfloat *t,
Uint32 color) {
SDL_Surface *surface = TTF_RenderText_Solid(
font, text,
(SDL_Color){RED(color), GREEN(color), BLUE(color), ALPHA(color)});
SDL_Surface *surface =
TTF_RenderText_Solid(font, text, (SDL_Color){255, 255, 255, 255});
gl4dpCopyFromSDLSurfaceWithTransforms(surface, s, t);
// TODO: Make scale depend of the text length
gl4dpCopyFromSDLSurfaceWithTransforms(surface, (GLfloat[2]){.3, -.1},
(GLfloat[2]){x, y});
SDL_FreeSurface(surface);
}
void unload_font(TTF_Font *font) { TTF_CloseFont(font); }