diff --git a/Makefile b/Makefile index 601551c..18b5004 100644 --- a/Makefile +++ b/Makefile @@ -37,7 +37,7 @@ MSVCSRC = $(patsubst %,,$(SOURCES)) OBJ = $(SOURCES:.c=.o) DOXYFILE = documentation/Doxyfile 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) # API 8 diff --git a/fonts/Instrument.ttf b/fonts/Instrument.ttf new file mode 100644 index 0000000..200c138 --- /dev/null +++ b/fonts/Instrument.ttf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f14c103282c1d54da23beb0dc116a90a93a270a4bad9d8d2a7fd9350645d33ad +size 12496 diff --git a/includes/animations.h b/includes/animations.h index 21c35c0..2d417ba 100644 --- a/includes/animations.h +++ b/includes/animations.h @@ -3,6 +3,7 @@ #include #include +#include #include "audio.h" diff --git a/main.c b/main.c index 309f469..e088b3b 100644 --- a/main.c +++ b/main.c @@ -29,7 +29,7 @@ int main(int argc, char *argv[]) { // Animations GL4DHanime animations[] = {{7000, lights, NULL, NULL}, - {3000, credits, NULL, NULL}, + {10000, credits, NULL, NULL}, {0, NULL, NULL, NULL}}; gl4dhInit(animations, _dims[0], _dims[1], NULL); diff --git a/shaders/credits.fs b/shaders/credits.fs new file mode 100644 index 0000000..8ed5eae --- /dev/null +++ b/shaders/credits.fs @@ -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); +} diff --git a/shaders/credits.vs b/shaders/credits.vs new file mode 100644 index 0000000..f0cf83c --- /dev/null +++ b/shaders/credits.vs @@ -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); + } +} diff --git a/src/credits.c b/src/credits.c index 97c1a65..a0714e0 100644 --- a/src/credits.c +++ b/src/credits.c @@ -1,10 +1,14 @@ #include "../includes/animations.h" +static GLuint _pId = 0; +static GLuint _textTexId = 0; +static GLuint _quadId = 0; + // Initialize -static void init(); +static void init(void); // Draw -static void draw(); +static void draw(void); void credits(int state) { switch (state) { @@ -21,6 +25,88 @@ void credits(int state) { } } -static void init() {} +static void init(void) { + _pId = gl4duCreateProgram("shaders/credits.vs", "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); +}