add font support

This commit is contained in:
Mylloon 2023-05-03 17:24:54 +02:00
parent b7b8738f8a
commit 1ec21b64dc
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
7 changed files with 121 additions and 6 deletions

View file

@ -37,7 +37,7 @@ MSVCSRC = $(patsubst %,<ClCompile Include=\"%\\\" \\/>,$(SOURCES))
OBJ = $(SOURCES:.c=.o) OBJ = $(SOURCES:.c=.o)
DOXYFILE = documentation/Doxyfile DOXYFILE = documentation/Doxyfile
VSCFILES = $(PROGNAME).vcxproj $(PROGNAME).sln VSCFILES = $(PROGNAME).vcxproj $(PROGNAME).sln
EXTRAFILES = COPYING $(wildcard shaders/*.?s images/*.png) $(VSCFILES) EXTRAFILES = COPYING $(wildcard shaders/*.?s images/*.png fonts/*.ttf) $(VSCFILES)
DISTFILES = $(SOURCES) Makefile $(HEADERS) $(DOXYFILE) $(EXTRAFILES) DISTFILES = $(SOURCES) Makefile $(HEADERS) $(DOXYFILE) $(EXTRAFILES)
# API 8 # API 8

BIN
fonts/Instrument.ttf (Stored with Git LFS) Normal file

Binary file not shown.

View file

@ -3,6 +3,7 @@
#include <GL4D/gl4dh.h> #include <GL4D/gl4dh.h>
#include <GL4D/gl4duw_SDL2.h> #include <GL4D/gl4duw_SDL2.h>
#include <SDL_ttf.h>
#include "audio.h" #include "audio.h"

2
main.c
View file

@ -29,7 +29,7 @@ int main(int argc, char *argv[]) {
// Animations // Animations
GL4DHanime animations[] = {{7000, lights, NULL, NULL}, GL4DHanime animations[] = {{7000, lights, NULL, NULL},
{3000, credits, NULL, NULL}, {10000, credits, NULL, NULL},
{0, NULL, NULL, NULL}}; {0, NULL, NULL, NULL}};
gl4dhInit(animations, _dims[0], _dims[1], NULL); gl4dhInit(animations, _dims[0], _dims[1], NULL);

9
shaders/credits.fs Normal file
View file

@ -0,0 +1,9 @@
#version 330
uniform sampler2D tex;
in vec2 vsoTexCoord;
out vec4 fragColor;
void main(void) {
vec4 c = texture(tex, vsoTexCoord);
fragColor = vec4(c.rgb, length(c.rgb) > 0.0 ? 1.0 : 0.0);
}

16
shaders/credits.vs Normal file
View file

@ -0,0 +1,16 @@
#version 330
layout(location = 0) in vec3 vsiPosition;
layout(location = 2) in vec2 vsiTexCoord;
uniform int inv;
uniform mat4 modview, proj;
out vec2 vsoTexCoord;
void main(void) {
gl_Position = proj * modview * vec4(vsiPosition, 1.0);
if(inv != 0) {
vsoTexCoord = vec2(vsiTexCoord.s, 1.0 - vsiTexCoord.t);
} else {
vsoTexCoord = vec2(vsiTexCoord.s, vsiTexCoord.t);
}
}

View file

@ -1,10 +1,14 @@
#include "../includes/animations.h" #include "../includes/animations.h"
static GLuint _pId = 0;
static GLuint _textTexId = 0;
static GLuint _quadId = 0;
// Initialize // Initialize
static void init(); static void init(void);
// Draw // Draw
static void draw(); static void draw(void);
void credits(int state) { void credits(int state) {
switch (state) { switch (state) {
@ -21,6 +25,88 @@ void credits(int state) {
} }
} }
static void init() {} static void init(void) {
_pId = gl4duCreateProgram("<vs>shaders/credits.vs", "<fs>shaders/credits.fs",
NULL);
gl4duGenMatrix(GL_FLOAT, "modview");
gl4duGenMatrix(GL_FLOAT, "proj");
_quadId = gl4dgGenQuadf();
static void draw() {} // Font
char *text = " CRÉDITS\n\n"
"Concours API8 — 7 édition\n"
"Font: fontesk et fontsquirrel\n"
"Librairies: GL4D, SDL2 et extensions";
char *fontName = "fonts/Instrument.ttf";
SDL_Color c = {255, 255, 255, 255};
SDL_Surface *d, *s;
TTF_Font *font = NULL;
if (TTF_Init() == -1) {
fprintf(stderr, "Erreur TTF : %s\n", TTF_GetError());
exit(2);
}
glGenTextures(1, &_textTexId);
glBindTexture(GL_TEXTURE_2D, _textTexId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
if (!(font = TTF_OpenFont(fontName, 100))) {
fprintf(stderr, "Erreur TTF : %s\n", TTF_GetError());
exit(2);
}
if ((d = TTF_RenderUTF8_Blended_Wrapped(font, text, c, 0)) == NULL) {
TTF_CloseFont(font);
fprintf(stderr, "Erreur TTF : RenderText\n");
exit(2);
}
assert((s = SDL_CreateRGBSurface(0, d->w, d->h, 32, R_MASK, G_MASK, B_MASK,
A_MASK)));
SDL_BlitSurface(d, NULL, s, NULL);
SDL_FreeSurface(d);
glBindTexture(GL_TEXTURE_2D, _textTexId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, s->w, s->h, 0, GL_RGBA,
GL_UNSIGNED_BYTE, s->pixels);
SDL_FreeSurface(s);
TTF_CloseFont(font);
glBindTexture(GL_TEXTURE_2D, 0);
}
static void draw(void) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
static Uint32 t0 = -1;
GLfloat t, d;
if (t0 < 0.0f)
t0 = SDL_GetTicks();
t = (GLfloat)(SDL_GetTicks() - t0) / 1000.f;
d = -1.1f + .25f * t;
GLfloat ratio = (GLfloat)_dims[0] / (GLfloat)_dims[1];
gl4duBindMatrix("proj");
gl4duLoadIdentityf();
gl4duFrustumf(-ratio, ratio, -1, 1, 2, 100);
gl4duBindMatrix("modview");
gl4duLoadIdentityf();
gl4duTranslatef(0.f, d, -2.f);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _textTexId);
gl4duScalef(.5f, .1f, .5f);
glUseProgram(_pId);
gl4duSendMatrices();
glUniform1i(glGetUniformLocation(_pId, "inv"), 1);
glUniform1i(glGetUniformLocation(_pId, "tex"), 0);
gl4dgDraw(_quadId);
glUseProgram(0);
}