diff --git a/Makefile b/Makefile index 4d9abf3..8f0496d 100644 --- a/Makefile +++ b/Makefile @@ -23,8 +23,8 @@ PACKNAME = api8_2023 PROGNAME = demo VERSION = 0.1 distdir = $(PACKNAME)_$(PROGNAME)-$(VERSION) -HEADERS = -SOURCES = main.c +HEADERS = $(wildcard includes/*.h) +SOURCES = main.c $(wildcard src/*.c) MSVCSRC = $(patsubst %,,$(SOURCES)) OBJ = $(SOURCES:.c=.o) DOXYFILE = documentation/Doxyfile @@ -54,7 +54,7 @@ else endif CPPFLAGS += $(shell sdl2-config --cflags) -LDFLAGS += -lGL4Dummies $(shell sdl2-config --libs) +LDFLAGS += -lGL4Dummies $(shell sdl2-config --libs) -lSDL2_mixer -lSDL2_image all: $(PROGNAME) $(PROGNAME): $(OBJ) diff --git a/includes/animations.h b/includes/animations.h new file mode 100644 index 0000000..21c35c0 --- /dev/null +++ b/includes/animations.h @@ -0,0 +1,18 @@ +#ifndef DEMO_ANIMATION_HPP +#define DEMO_ANIMATION_HPP 1 + +#include +#include + +#include "audio.h" + +// Dimensions de la fenêtre +extern GLuint _dims[]; + +// Lumière +void lights(int); + +// Crédits de fin +void credits(int); + +#endif diff --git a/includes/audio.h b/includes/audio.h new file mode 100644 index 0000000..71cc13b --- /dev/null +++ b/includes/audio.h @@ -0,0 +1,15 @@ +#ifndef DEMO_AUDIO_HPP +#define DEMO_AUDIO_HPP 1 + +#include + +// Charge et lance la musique +void initMusic(Mix_Music *, const char *); + +// Renvoie une moyenne de l'intensité du son +double averageMusic(void); + +// Libère la musique en mémoire +void freeMusic(Mix_Music *); + +#endif diff --git a/main.c b/main.c index 19412fd..07ea3e0 100644 --- a/main.c +++ b/main.c @@ -1,86 +1,46 @@ -#include -#include -static void init(void); -static void resize(int, int); -static void draw(void); -static void sortie(void); +#include "includes/animations.h" -static GLuint _wW = 1280, _wH = 720; -static GLuint _quadId = 0; -static GLuint _pId = 0; +// Son de fond +// static Mix_Music *_ambiance = NULL; -int main(int argc, char **argv) { +// Dimensions initiales de la fenêtre +GLuint _dims[] = {1280, 720}; + +// Comportement à la fermeture du programme +static void closure(void) { + // freeMusic(_ambiance); + gl4duClean(GL4DU_ALL); +} + +// Comportement quand la fenêtre est redimensionnée +static void resize(int w, int h) { + _dims[0] = w; + _dims[1] = h; + glViewport(0, 0, _dims[0], _dims[1]); +} + +int main(int argc, char *argv[]) { if (!gl4duwCreateWindow(argc, argv, "Demo API8 2023", GL4DW_POS_CENTERED, - GL4DW_POS_CENTERED, _wW, _wH, + GL4DW_POS_CENTERED, _dims[0], _dims[1], GL4DW_OPENGL | GL4DW_SHOWN)) { fprintf(stderr, "Erreur lors de la création de la fenêtre.\n"); return 1; } - init(); - atexit(sortie); + // initMusic(_ambiance, "audio/ambiance.mid"); + + // Animations + GL4DHanime animations[] = {{7000, lights, NULL, NULL}, + {3000, credits, NULL, NULL}, + {0, NULL, NULL, NULL}}; + + gl4dhInit(animations, _dims[0], _dims[1], NULL); + atexit(closure); + gl4duwResizeFunc(resize); - gl4duwDisplayFunc(draw); + gl4duwDisplayFunc(gl4dhDraw); gl4duwMainLoop(); return 0; } - -void init(void) { - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); - _quadId = gl4dgGenQuadf(); - _pId = gl4duCreateProgram("shaders/lights.vs", "shaders/lights.fs", - NULL); - glEnable(GL_DEPTH_TEST); - gl4duGenMatrix(GL_FLOAT, "proj"); - gl4duGenMatrix(GL_FLOAT, "mod"); - gl4duGenMatrix(GL_FLOAT, "view"); - resize(_wW, _wH); -} - -static void resize(int width, int height) { - GLfloat ratio; - _wW = width; - _wH = height; - glViewport(0, 0, _wW, _wH); - ratio = (GLfloat)_wW / (GLfloat)_wH; - gl4duBindMatrix("proj"); - gl4duLoadIdentityf(); - gl4duFrustumf(-1 * ratio, 1 * ratio, -1, 1, 2, 100); -} - -void draw(void) { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - // Vue - gl4duBindMatrix("view"); - gl4duLoadIdentityf(); - gl4duLookAtf(0, 0, 4.f, 0, 0, 0, 0.f, 1.f, 0); - - // Scene - // clang-format off - GLfloat rouge[] = {1.f, 0.f, 0.f, 1.f}, - blanc[] = {1.f, 1.f, 1.f, 1.f}, - lumpos0[] = {0.f, 0.f, 1.1f, 1.f}, - lumpos[4], - *mat; - // clang-format on - - static GLdouble x = 0.; - lumpos0[0] = 4.f * (float)sin(x); - gl4duBindMatrix("view"); - mat = (GLfloat *)gl4duGetMatrixData(); - MMAT4XVEC4(lumpos, mat, lumpos0); - gl4duBindMatrix("mod"); - gl4duLoadIdentityf(); - glUseProgram(_pId); - gl4duSendMatrices(); - glUniform4fv(glGetUniformLocation(_pId, "scolor"), 1, rouge); - glUniform4fv(glGetUniformLocation(_pId, "lcolor"), 1, blanc); - glUniform4fv(glGetUniformLocation(_pId, "lumpos"), 1, lumpos); - gl4dgDraw(_quadId); - glUseProgram(0); -} - -void sortie(void) { gl4duClean(GL4DU_ALL); } diff --git a/src/audio.c b/src/audio.c new file mode 100644 index 0000000..98866bc --- /dev/null +++ b/src/audio.c @@ -0,0 +1,39 @@ +#include "../includes/audio.h" + +// Stocke les données à propos de la musique +static double average; + +double averageMusic(void) { return average; } + +void callback(void *_, Uint8 *stream, int size) { + int i = 0; + double tmp = 0.; + for (signed short *stream16 = (signed short *)stream; i < size / 2; ++i) { + tmp += stream16[i] / ((1 << 15) - 1.); + } + average = fabs(tmp / i); +} + +void initMusic(Mix_Music *music, const char *filename) { + Mix_Init(MIX_INIT_MID); + Mix_OpenAudio(44100, AUDIO_S16LSB, 2, 1024); + music = Mix_LoadMUS(filename); + + Mix_VolumeMusic(MIX_MAX_VOLUME / 10); + Mix_SetPostMix(callback, NULL); + + Mix_PlayMusic(music, 1); +} + +void freeMusic(Mix_Music *music) { + if (music) { + if (Mix_PlayingMusic()) { + Mix_HaltMusic(); + } + Mix_FreeMusic(music); + music = NULL; + } + + Mix_CloseAudio(); + Mix_Quit(); +} diff --git a/src/credits.c b/src/credits.c new file mode 100644 index 0000000..2053e4b --- /dev/null +++ b/src/credits.c @@ -0,0 +1,17 @@ +#include "../includes/animations.h" + +void credits(int state) { + switch (state) { + case GL4DH_INIT: + break; + + case GL4DH_DRAW: + break; + + case GL4DH_FREE: + break; + + default: + break; + } +} diff --git a/src/light.c b/src/light.c new file mode 100644 index 0000000..4a4f84e --- /dev/null +++ b/src/light.c @@ -0,0 +1,69 @@ +#include "../includes/animations.h" + +void cube(int, int); + +void lights(int state) { + static GLuint quadId = 0; + static GLuint pId = 0; + + const GLuint wW = _dims[0]; + const GLuint wH = _dims[1]; + + switch (state) { + case GL4DH_INIT: + quadId = gl4dgGenQuadf(); + pId = gl4duCreateProgram("shaders/lights.vs", "shaders/lights.fs", + NULL); + + gl4duGenMatrix(GL_FLOAT, "proj"); + gl4duGenMatrix(GL_FLOAT, "mod"); + gl4duGenMatrix(GL_FLOAT, "view"); + + glEnable(GL_DEPTH_TEST); + break; + + case GL4DH_DRAW: + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + static GLdouble lumloc = 0.; + // clang-format off + GLfloat rouge[] = {1.f, 0.f, 0.f, 1.f}, + blanc[] = {1.f, 1.f, 1.f, 1.f}, + lumpos0[] = {4.f * (float)sin(lumloc), 0.f, 1.1f, 1.f}, + lumpos[4], + *mat, + ratio = (GLfloat)_dims[0] / (GLfloat)_dims[1]; + // clang-format on + + // Setup + gl4duBindMatrix("proj"); + gl4duLoadIdentityf(); // TODO: ça bug?? + gl4duFrustumf(-ratio, ratio, -1, 1, 2, 100); + + // Scene + gl4duBindMatrix("view"); + gl4duLoadIdentityf(); + gl4duLookAtf(0, 0, 4.0f, 0, 0, 0, 0.0f, 1.0f, 0); + + mat = (GLfloat *)gl4duGetMatrixData(); + MMAT4XVEC4(lumpos, mat, lumpos0); + + gl4duBindMatrix("mod"); + gl4duLoadIdentityf(); + + glUseProgram(pId); + gl4duSendMatrices(); + + glUniform4fv(glGetUniformLocation(pId, "scolor"), 1, rouge); + glUniform4fv(glGetUniformLocation(pId, "lcolor"), 1, blanc); + glUniform4fv(glGetUniformLocation(pId, "lumpos"), 1, lumpos); + gl4dgDraw(quadId); + + glUseProgram(0); + break; + + default: + break; + } +}