Web support + compilable in mingw

This commit is contained in:
Marc
2023-01-23 01:40:29 +01:00
parent ade88dff84
commit a182c0f30a
21 changed files with 121 additions and 62 deletions
+1 -1
View File
@@ -20,7 +20,7 @@ void cinematic_staticRender(void) {
text_renderText(&AdamesLegend, 8, 0.8f, white, "./assets/fonts/Quinquefive.ttf", 40, "Adame est l'eleve modele de Polytech,\n Membre de plusieurs associations il est connu de tous.\n Lors de l'edition 21.2 de la Nantarena,\n Adame s'est retrouve a engeuler des joueurs pour leur demander de mettre leur putain de masque.\n\n L'edition finie, Adame a kidnappe l'ensemble des joueurs qui forcaient pour leur apprendre a respecter l'autorite a coup de coup de pied au cul.\n\n Press space to continue");
scene_addRenderObject(&cinematicScene, AdamesLegend, 10, 80);
music = Mix_LoadMUS("./assets/music/chinese rap.mp3");
music = Mix_LoadMUS("./assets/music/chinese rap.ogg");
if(music == NULL) {
fprintf(stderr, "Chinese rap not found!\n");
exit(1);
+16 -7
View File
@@ -12,6 +12,11 @@
#include "text.h"
#include "input.h"
//Web asembly
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#define GRAVITY 10000.f
@@ -84,21 +89,21 @@ RenderId_t steroidText;
static void playMusic(){
music = Mix_LoadMUS("./assets/music/game.wav");
music = Mix_LoadMUS("./assets/music/game.ogg");
if(music == NULL) {
fprintf(stderr, "Game audio not found!\n");
fprintf(stderr, "Game audio not found! %s\n", Mix_GetError());
exit(1);
}
petMusic = Mix_LoadWAV("./assets/music/pet.mp3");
petMusic = Mix_LoadWAV("./assets/music/pet.ogg");
if(petMusic == NULL){
fprintf(stderr, "Pet audio not found!\n");
fprintf(stderr, "Pet audio not found! %s\n", Mix_GetError());
exit(1);
}
koMusic = Mix_LoadWAV("./assets/music/ko-effect-gain.mp3");
koMusic = Mix_LoadWAV("./assets/music/ko-effect-gain.ogg");
if(koMusic == NULL){
fprintf(stderr, "ko effect audio not found!\n");
fprintf(stderr, "ko effect audio not found %s\n", Mix_GetError());
exit(1);
}
@@ -120,8 +125,12 @@ int game_staticRender(void){
SDL_Surface* gameBackgroundSurface = IMG_Load("./assets/img/game_bg.png");
if(gameBackgroundSurface == NULL){
printf("cannot load background");
printf("cannot load background\n");
#ifdef __EMSCRIPTEN__
emscripten_pause_main_loop();
#else
return -1;
#endif
}
SDL_Surface* gameBackgroundSurface2 = IMG_Load("./assets/img/game_bg.png");
+14 -7
View File
@@ -12,6 +12,12 @@
#include <unistd.h>
#include "cinematic.h"
#include "scaller.h"
#include "main.h"
//Web asembly
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
clock_t timecode;
long iter;
@@ -40,14 +46,10 @@ static void game_setup(void) {
RenderObject_t text = EMPTY_RENDER_OBJECT;
SDL_Color white = {255, 255, 255, 255};
TTF_Init();
if(text_renderText(&text, 1000, 1, white, "./assets/fonts/arial.ttf", 25, "Hello World") < 0){
printf("error");
exit(1);
}
input_listenKeyDown(SDL_SCANCODE_SPACE, space);
}
static void game_loop(void) {
void game_loop(void) {
float delta = ((float)(clock() - timecode)) / CLOCKS_PER_SEC;
//update time
@@ -62,7 +64,6 @@ static void game_loop(void) {
timecode = clock();
}
int main(int argc, char ** argv) {
TTF_Init();
if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) != 0){
@@ -75,9 +76,15 @@ int main(int argc, char ** argv) {
timecode = clock();
Mix_Init(MIX_INIT_MP3);
renderer_main(argc, argv, game_setup, game_loop);
renderer_main(argc, argv, game_setup);
Mix_CloseAudio();
text_freeFontCache();
return 0;
}
#ifdef __MINGW32__
int WinMain(int argc, char ** argv) {
main(argc, argv);
}
#endif
+6
View File
@@ -0,0 +1,6 @@
#ifndef __MAIN_H__
#define __MAIN_H__
void game_loop(void);
#endif
+14 -5
View File
@@ -20,21 +20,30 @@ static Mix_Music * music;
Scene_t menuScene = { NULL, 0, 0, 0, 0, 0 };
//Web asembly
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
int menu_staticRender(void){
MonitorSize_t size = renderer_getMonitorSize();
float aspectRatio = (float)size.width / size.height;
SDL_Surface* backgroundImageSurface = IMG_Load("./assets/img/menu_bg.png");
SDL_Surface* backgroundImageSurface = IMG_Load("assets/img/menu_bg.png");
if(backgroundImageSurface == NULL){
printf("cannot load background");
return -1;
printf("Could not load menu background\n");
#ifdef __EMSCRIPTEN__
emscripten_pause_main_loop();
#endif
return -1;
}
music = Mix_LoadMUS("./assets/music/menu.wav");
music = Mix_LoadMUS("assets/music/menu.ogg");
if(music == NULL) {
fprintf(stderr, "Menu music not found!\n");
fprintf(stderr, "Menu music not found!: %s\n", Mix_GetError());
exit(1);
}
+56 -36
View File
@@ -14,6 +14,12 @@
#include <math.h>
#include "input.h"
#include "scene.h"
#include "main.h"
//Web asembly
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
bool running;
MonitorSize_t monitorSize;
@@ -36,8 +42,50 @@ static void toggleFullscreen(void) {
}
}
static bool main_loop(void) {
SDL_RenderClear(renderer);
game_loop();
SDL_Event event;
// Events management
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
// handling of close button
running = false;
printf("Quitting my job!\n");
break;
case SDL_KEYDOWN:
// keyboard API for key pressed
input_pollKeyDown(event.key.keysym.scancode);
break;
case SDL_KEYUP:
input_pollKeyUp(event.key.keysym.scancode);
break;
}
}
if(!running)
return false;
if(scene != NULL)
scene_render(scene);
SDL_RenderPresent(renderer);
#ifndef __EMSCRIPTEN__
SDL_Delay(1000 / 60);
#endif
return true;
}
//int (*compar)(const void [.size], const void [.size], void *)
void renderer_main(int argc, char ** argv, void(*game_setup)(void), void(*game_loop)(void)) {
void renderer_main(int argc, char ** argv, void(*game_setup)(void)) {
// returns zero on success else non-zero
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf("error initializing SDL: %s\n", SDL_GetError());
@@ -75,44 +123,16 @@ void renderer_main(int argc, char ** argv, void(*game_setup)(void), void(*game_l
running = true;
game_setup();
while(running) {
SDL_RenderClear(renderer);
game_loop();
SDL_Event event;
// Events management
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
// handling of close button
running = false;
printf("Quitting my job!\n");
break;
case SDL_KEYDOWN:
// keyboard API for key pressed
input_pollKeyDown(event.key.keysym.scancode);
break;
case SDL_KEYUP:
input_pollKeyUp(event.key.keysym.scancode);
break;
}
}
//no need to make an extra render pass
if(!running) break;
if(scene != NULL)
scene_render(scene);
SDL_RenderPresent(renderer);
SDL_Delay(1000 / 400); //400fps or u a pussy
}
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop((em_callback_func)main_loop, 0, true);
#else
while(main_loop());
#endif
}
void renderer_setScene(const Scene_t * newScene) {
scene = newScene;
}
+1 -1
View File
@@ -16,7 +16,7 @@ typedef struct MonitorSize {
int width, height;
} MonitorSize_t;
void renderer_main(int argc, char ** argv, void(*game_setup)(void), void(*game_loop)(void));
void renderer_main(int argc, char ** argv, void(*game_loop)(void));
MonitorSize_t renderer_getMonitorSize(void);
void renderer_setScene(const Scene_t * newScene);
const Scene_t * renderer_getScene(void);
+3 -1
View File
@@ -5,9 +5,11 @@
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
#include <float.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
const RenderObject_t EMPTY_RENDER_OBJECT = { 0, 0.f, 0.f, 0.f, 0.f, 0.f, NULL, "Undefined" };
@@ -83,7 +85,7 @@ RenderId_t scene_addRenderObject(Scene_t * scene, RenderObject_t object, int x,
scene->objects[scene->objectCount - 1] = item;
//the objects are sorted
#ifdef __MINGW32__
#if defined(__MINGW32__) || defined(__EMSCRIPTEN__)
qsort(scene->objects, scene->objectCount, sizeof(SceneObject_t), (int (__cdecl *)(const void *,const void *))compartSort);
#else
qsort(scene->objects, scene->objectCount, sizeof(SceneObject_t), (__compar_fn_t)compartSort);