* usage of gl4dhAnimeManager
* basic impl of music (MID)
* includes and src folders
* move light to animation
This commit is contained in:
Mylloon 2023-05-03 12:22:42 +02:00
parent 747b4d1e86
commit f45432f0ef
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
7 changed files with 193 additions and 75 deletions

View file

@ -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 %,<ClCompile Include=\"%\\\" \\/>,$(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)

18
includes/animations.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef DEMO_ANIMATION_HPP
#define DEMO_ANIMATION_HPP 1
#include <GL4D/gl4dh.h>
#include <GL4D/gl4duw_SDL2.h>
#include "audio.h"
// Dimensions de la fenêtre
extern GLuint _dims[];
// Lumière
void lights(int);
// Crédits de fin
void credits(int);
#endif

15
includes/audio.h Normal file
View file

@ -0,0 +1,15 @@
#ifndef DEMO_AUDIO_HPP
#define DEMO_AUDIO_HPP 1
#include <SDL_mixer.h>
// 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

104
main.c
View file

@ -1,86 +1,46 @@
#include <GL4D/gl4duw_SDL2.h>
#include <stdio.h>
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("<vs>shaders/lights.vs", "<fs>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); }

39
src/audio.c Normal file
View file

@ -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();
}

17
src/credits.c Normal file
View file

@ -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;
}
}

69
src/light.c Normal file
View file

@ -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("<vs>shaders/lights.vs", "<fs>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;
}
}