Compare commits

..

12 Commits

Author SHA1 Message Date
Marc a182c0f30a Web support + compilable in mingw 2023-01-23 01:40:29 +01:00
Marc ade88dff84 Fix leak, rename and some non working mingw shit 2023-01-22 22:53:16 +01:00
Marc 9f0a8a2692 bug fix 2023-01-22 17:39:29 +01:00
Marc 6837f227d3 Fullscreen 2023-01-22 17:37:28 +01:00
Marc 50ec708f9c background fix 2023-01-22 17:21:24 +01:00
Marc 8b21274d49 Merge branch 'master' of ssh.marcbarbier.fr:Marc/gamejam-polytech 2023-01-22 17:19:10 +01:00
JamilHajjar 2f266c7949 add audio effects 2023-01-22 17:18:36 +01:00
Marc c4bb2e8eee Infinite background 2023-01-22 17:18:25 +01:00
Marc b5fda06f31 Merge branch 'master' of ssh.marcbarbier.fr:Marc/gamejam-polytech 2023-01-22 17:01:00 +01:00
Marc 1bea67f658 Strength 2023-01-22 16:59:08 +01:00
JamilHajjar 789873f263 Merge branch 'master' of ssh.marcbarbier.fr:Marc/gamejam-polytech 2023-01-22 16:42:45 +01:00
JamilHajjar f7a2c15717 press space to teach respect 2023-01-22 16:42:23 +01:00
21 changed files with 491 additions and 309 deletions
+24 -7
View File
@@ -8,15 +8,28 @@ if(CMAKE_BUILD_TYPE MATCHES DEBUG)
add_link_options(-fsanitize=address) add_link_options(-fsanitize=address)
endif() endif()
set(CMAKE_C_FLAGS "-Wall -Wextra -pedantic -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wmaybe-uninitialized -Wno-unused-variable") if(EMSCRIPTEN)
set(CMAKE_C_FLAGS_DEBUG "-g -Werror -O0") set(CMAKE_C_FLAGS "-s USE_SDL=2 -s USE_SDL_IMAGE=2 -s USE_SDL_TTF=2 -s USE_SDL_MIXER=2 -s SDL2_IMAGE_FORMATS='[\"png\"]' -s USE_OGG=1 -sALLOW_MEMORY_GROWTH --use-preload-plugins --preload-file assets")
set(CMAKE_C_FLAGS_RELEASE "") set(CMAKE_C_FLAGS_DEBUG "-g -O0")
set(CMAKE_C_FLAGS_RELEASE "-O2")
set(CMAKE_EXECUTABLE_SUFFIX_C ".html")
#help with clangd
add_definitions(-D__EMSCRIPTEN__ -DEXIT_RUNTIME=1 -DMUSIC_WAV)
elseif(MINGW)
set(CMAKE_C_FLAGS "-Wno-undef --static")
set(CMAKE_C_FLAGS_DEBUG "-g -O0")
set(CMAKE_C_FLAGS_RELEASE "-O2")
else()
set(CMAKE_C_FLAGS "-Wall -Wextra -pedantic -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wmaybe-uninitialized -Wno-unused-variable")
set(CMAKE_C_FLAGS_DEBUG "-g -Werror -O0")
set(CMAKE_C_FLAGS_RELEASE "")
endif()
# generate the compile_commands for vscode / clang # generate the compile_commands for vscode / clang
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "") set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")
# set the project name # set the project name
project(FLOPPE_FION) project(LEGEND_OF_ADAME)
set(SRC set(SRC
@@ -32,13 +45,17 @@ set(SRC
) )
# add the executable # add the executable
add_executable(FLOPPE_FION ${SRC}) add_executable(LEGEND_OF_ADAME ${SRC})
target_link_libraries(FLOPPE_FION SDL2 SDL2_image SDL2_ttf SDL2_mixer m) target_link_libraries(LEGEND_OF_ADAME SDL2 SDL2_image SDL2_ttf SDL2_mixer m)
set_property(TARGET FLOPPE_FION PROPERTY C_STANDARD 23) set_property(TARGET LEGEND_OF_ADAME PROPERTY C_STANDARD 23)
file(COPY assets DESTINATION ${CMAKE_BINARY_DIR}) file(COPY assets DESTINATION ${CMAKE_BINARY_DIR})
if(MINGW)
target_link_libraries(LEGEND_OF_ADAME ws2_32)
endif()
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+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"); 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); 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) { if(music == NULL) {
fprintf(stderr, "Chinese rap not found!\n"); fprintf(stderr, "Chinese rap not found!\n");
exit(1); exit(1);
+117 -29
View File
@@ -6,14 +6,24 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <limits.h>
#include "scaller.h" #include "scaller.h"
#include "scene.h" #include "scene.h"
#include "text.h" #include "text.h"
#include "input.h"
//Web asembly
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#define SLIDER_SPEED 1.f/5000
#define GRAVITY 10000.f #define GRAVITY 10000.f
static Mix_Music * music; static Mix_Music * music;
static Mix_Chunk * petMusic;
static Mix_Chunk * koMusic;
Scene_t gameScene; Scene_t gameScene;
@@ -36,6 +46,8 @@ RenderObject_t scoreText;
static RenderId_t scoreTextId = -1; static RenderId_t scoreTextId = -1;
static int victimHeight; static int victimHeight;
static RenderId_t pressSpaceShadowId;
static RenderId_t pressSpaceTextId;
#define VICTIME_COUNT 4 #define VICTIME_COUNT 4
#define VICTIME_SIZE 0.5f #define VICTIME_SIZE 0.5f
@@ -56,7 +68,6 @@ static RenderId_t strengthBarId;
static float strength = 0.f; static float strength = 0.f;
static bool increaseStrength = true; static bool increaseStrength = true;
static float adameStrength = 2.f; // this value is not const as it will be possible to improve the strenght of Adame. static float adameStrength = 2.f; // this value is not const as it will be possible to improve the strenght of Adame.
static RenderObject_t strengthBarSlider; static RenderObject_t strengthBarSlider;
@@ -64,26 +75,48 @@ static float strengthBarSliderDistance = 0.f;
static RenderId_t strengthBarSliderId; static RenderId_t strengthBarSliderId;
static bool increaseStrengthBarSlider = true; static bool increaseStrengthBarSlider = true;
#define STEROIDS_POSITION CAMERA_WIDTH * 0.4f
#define SLIDER_SPEED ((adameStrength *0.5f)/5000)
void game_handleFKeyInput(void);
static RenderId_t backgrounds[2];
static int currentBackground = 0;
static int backgroundOffset = 0;
static int backgroundStep;
RenderId_t steroidText;
static void playMusic(){ static void playMusic(){
music = Mix_LoadMUS("./assets/music/jojo arab.mp3"); music = Mix_LoadMUS("./assets/music/game.ogg");
if(music == NULL) { if(music == NULL) {
fprintf(stderr, "Jojo arab not found!\n"); fprintf(stderr, "Game audio not found! %s\n", Mix_GetError());
exit(1); exit(1);
} }
// if(Mix_PlayMusic(music, 0) != 0){ petMusic = Mix_LoadWAV("./assets/music/pet.ogg");
// printf("Music sound could not be played!\n" if(petMusic == NULL){
// "SDL_Error: %s\n", Mix_GetError()); fprintf(stderr, "Pet audio not found! %s\n", Mix_GetError());
// Mix_FreeMusic(music); exit(1);
// running = false; }
// }
koMusic = Mix_LoadWAV("./assets/music/ko-effect-gain.ogg");
if(koMusic == NULL){
fprintf(stderr, "ko effect audio not found %s\n", Mix_GetError());
exit(1);
}
if(Mix_PlayMusic(music, -1) != 0){
printf("Music sound could not be played!\n"
"SDL_Error: %s\n", Mix_GetError());
Mix_FreeMusic(music);
running = false;
}
} }
int game_staticRender(void){ int game_staticRender(void){
input_listenKeyDown(SDL_SCANCODE_F, game_handleFKeyInput);
playMusic(); playMusic();
srand(time(NULL)); srand(time(NULL));
@@ -92,10 +125,15 @@ int game_staticRender(void){
SDL_Surface* gameBackgroundSurface = IMG_Load("./assets/img/game_bg.png"); SDL_Surface* gameBackgroundSurface = IMG_Load("./assets/img/game_bg.png");
if(gameBackgroundSurface == NULL){ if(gameBackgroundSurface == NULL){
printf("cannot load background"); printf("cannot load background\n");
#ifdef __EMSCRIPTEN__
emscripten_pause_main_loop();
#else
return -1; return -1;
#endif
} }
SDL_Surface* gameBackgroundSurface2 = IMG_Load("./assets/img/game_bg.png");
RenderObject_t gameBackground = EMPTY_RENDER_OBJECT; RenderObject_t gameBackground = EMPTY_RENDER_OBJECT;
strcpy(gameBackground.name, "gameBackground"); strcpy(gameBackground.name, "gameBackground");
@@ -103,7 +141,10 @@ int game_staticRender(void){
gameBackground.surface = gameBackgroundSurface; gameBackground.surface = gameBackgroundSurface;
scaller(gameBackground.surface, 1, &gameBackground.width, &gameBackground.height); scaller(gameBackground.surface, 1, &gameBackground.width, &gameBackground.height);
gameBackground.height = 1; gameBackground.height = 1;
scene_addRenderObject(&gameScene, gameBackground, 0, 0); backgrounds[0] = scene_addRenderObject(&gameScene, gameBackground, 0, 0);
gameBackground.surface = gameBackgroundSurface2;
backgrounds[1] = scene_addRenderObject(&gameScene, gameBackground, gameBackground.width * CAMERA_WIDTH, 0);
backgroundStep = gameBackground.width * CAMERA_WIDTH;
SDL_Surface* buildingSurface = IMG_Load("./assets/img/building.png"); SDL_Surface* buildingSurface = IMG_Load("./assets/img/building.png");
RenderObject_t building = EMPTY_RENDER_OBJECT; RenderObject_t building = EMPTY_RENDER_OBJECT;
@@ -223,7 +264,7 @@ int game_staticRender(void){
scoreText = EMPTY_RENDER_OBJECT; scoreText = EMPTY_RENDER_OBJECT;
strcpy(scoreText.name, "scoreText"); strcpy(scoreText.name, "scoreText");
SDL_Color black = {0, 0, 0, 255}; SDL_Color black = {0, 0, 0, 255};
text_renderText(&scoreText, 50, 0.5f, black, "./assets/fonts/Quinquefive.ttf", 40, "jetons: %d", (unsigned)jetons); text_renderText(&scoreText, 50, 0.5f, black, "./assets/fonts/Quinquefive.ttf", 40, "jetons: %lu", (unsigned long)jetons);
scoreTextId = scene_addRenderObject(&gameScene, scoreText, gameScene.x + 2, 0); scoreTextId = scene_addRenderObject(&gameScene, scoreText, gameScene.x + 2, 0);
@@ -244,21 +285,34 @@ int game_staticRender(void){
strengthBarSlider.height = 0.05f; strengthBarSlider.height = 0.05f;
strengthBarSliderId = scene_addRenderObject(&gameScene, strengthBarSlider, 0.30f * CAMERA_WIDTH, 0.89f * CAMERA_HEIGHT); strengthBarSliderId = scene_addRenderObject(&gameScene, strengthBarSlider, 0.30f * CAMERA_WIDTH, 0.89f * CAMERA_HEIGHT);
text_renderText(&scoreText, 50, 1.f, black, "./assets/fonts/Quinquefive.ttf", 20, "Press F to buy steroids for 200J");
steroidText = scene_addRenderObject(&gameScene, scoreText, STEROIDS_POSITION, 0);
RenderObject_t pressSpaceShadow = EMPTY_RENDER_OBJECT;
strcpy(pressSpaceShadow.name, "pressSpaceShadow");
text_renderText(&pressSpaceShadow, 50, 1, black, "./assets/fonts/Quinquefive.ttf", 20, "Press space to teach respect");
pressSpaceShadowId = scene_addRenderObject(&gameScene, pressSpaceShadow, 0.30f * CAMERA_WIDTH, 0.8f * CAMERA_HEIGHT);
RenderObject_t pressSpaceText = EMPTY_RENDER_OBJECT;
strcpy(pressSpaceText.name, "pressSpaceText");
SDL_Color white = {255, 255, 255, 255};
text_renderText(&pressSpaceText, 50, 1, white, "./assets/fonts/Quinquefive.ttf", 20, "Press space to teach respect");
pressSpaceTextId = scene_addRenderObject(&gameScene, pressSpaceText, 0.295f * CAMERA_WIDTH, 0.82f * CAMERA_HEIGHT);
return 0; return 0;
} }
int game_render([[maybe_unused]] float delta){ int game_render(float delta){
deltaAccu += delta; deltaAccu += delta;
if(personPositionX - backgroundOffset > backgroundOffset * 0.1f) {
currentBackground = 1 - currentBackground;
backgroundOffset += backgroundStep;
scene_moveRenderObject(&gameScene, backgrounds[currentBackground], backgroundOffset, 0);
}
if(!isSpaceKeypressed && !crashed && deltaAccu >= SLIDER_SPEED){ if(!isSpaceKeypressed && !crashed && deltaAccu >= SLIDER_SPEED){
// if(increaseStrength && strength >= 2.f)
// increaseStrength = false;
// else if(increaseStrength)
// strength += 0.1;
// else if(!increaseStrength && strength <= 0.f)
// increaseStrength = true;
// else
// strength -= 0.1;
if(increaseStrengthBarSlider && strengthBarSliderDistance >= 0.50f) if(increaseStrengthBarSlider && strengthBarSliderDistance >= 0.50f)
increaseStrengthBarSlider = false; increaseStrengthBarSlider = false;
else if(increaseStrengthBarSlider){ else if(increaseStrengthBarSlider){
@@ -284,6 +338,20 @@ int game_render([[maybe_unused]] float delta){
scene_moveRenderObject(&gameScene, adame[0], ADAME_X, ADAME_Y); scene_moveRenderObject(&gameScene, adame[0], ADAME_X, ADAME_Y);
adame_id = 0; adame_id = 0;
kicked = true; kicked = true;
if(strength > 1.5){
if(Mix_PlayChannel(-1, koMusic, 0) != 0){
printf("Music sound could not be played!\n"
"SDL_Error: %s\n", Mix_GetError());
Mix_FreeChunk(koMusic);
}
}
else{
if(Mix_PlayChannel(-1, petMusic, 0) != 0){
printf("Music sound could not be played!\n"
"SDL_Error: %s\n", Mix_GetError());
Mix_FreeChunk(petMusic);
}
}
} else { } else {
adame_id++; adame_id++;
scene_moveRenderObject(&gameScene, adame[adame_id], ADAME_X, ADAME_Y); scene_moveRenderObject(&gameScene, adame[adame_id], ADAME_X, ADAME_Y);
@@ -302,22 +370,22 @@ int game_render([[maybe_unused]] float delta){
personPositionY += personVelocityY * delta; personPositionY += personVelocityY * delta;
jetons += distance; jetons += distance;
if(jetons > (float)ULONG_MAX)
jetons = (float)ULONG_MAX;
scene_moveRenderObject(&gameScene, scoreTextId, gameScene.x + 2, 0); scene_moveRenderObject(&gameScene, scoreTextId, gameScene.x + 2, 0);
if(personPositionY + CAMERA_HEIGHT * victimHeight >= CAMERA_HEIGHT - 18){ if(personPositionY + CAMERA_HEIGHT * victimHeight >= CAMERA_HEIGHT - 18){
crashed = true; crashed = true;
scene_removeRenderObject(&gameScene, scoreTextId); scene_removeRenderObject(&gameScene, scoreTextId);
SDL_Color black = {0, 0, 0, 255}; SDL_Color black = {0, 0, 0, 255};
text_renderText(&scoreText, 50, 0.5f, black, "./assets/fonts/Quinquefive.ttf", 40, "jetons: %d", (unsigned)jetons); text_renderText(&scoreText, 50, 0.5f, black, "./assets/fonts/Quinquefive.ttf", 40, "jetons: %lu", (unsigned long)jetons);
scoreTextId = scene_addRenderObject(&gameScene, scoreText, gameScene.x + 2, 0); scoreTextId = scene_addRenderObject(&gameScene, scoreText, gameScene.x + 2, 0);
} }
} }
scene_moveRenderObject(&gameScene, steroidText, STEROIDS_POSITION + gameScene.x, 0);
scene_moveRenderObject(&gameScene, personId, personPositionX, personPositionY); scene_moveRenderObject(&gameScene, personId, personPositionX, personPositionY);
return 0; return 0;
} }
@@ -326,8 +394,20 @@ void game_remove(void){
Mix_FreeMusic(music); Mix_FreeMusic(music);
} }
void game_handleFKeyInput(void) {
printf("coucou %f\n", jetons);
if(jetons > 200) {
jetons -= 200;
adameStrength += 0.5f;
scene_removeRenderObject(&gameScene, scoreTextId);
SDL_Color black = {0, 0, 0, 255};
text_renderText(&scoreText, 50, 0.5f, black, "./assets/fonts/Quinquefive.ttf", 40, "jetons: %lu", (unsigned long)jetons);
scoreTextId = scene_addRenderObject(&gameScene, scoreText, gameScene.x + 2, 0);
}
}
void game_handleSpaceKeyInput(void){ void game_handleSpaceKeyInput(void){
if(isSpaceKeypressed == true && !crashed)return;
if(crashed){ if(crashed){
crashed = false; crashed = false;
personPositionX = startPersonPositionX; personPositionX = startPersonPositionX;
@@ -343,13 +423,21 @@ void game_handleSpaceKeyInput(void){
scene_moveRenderObject(&gameScene, personId, 0, CAMERA_HEIGHT * 2); scene_moveRenderObject(&gameScene, personId, 0, CAMERA_HEIGHT * 2);
personId = victims[rand() % 4]; personId = victims[rand() % 4];
kicked=false; kicked=false;
}
else{ backgroundOffset = 0;
currentBackground = 0;
scene_moveRenderObject(&gameScene, backgrounds[0], 0, 0);
scene_moveRenderObject(&gameScene, backgrounds[1], backgroundStep, 0);
} else{
isSpaceKeypressed = true; isSpaceKeypressed = true;
strength = - 32 * powf(fabs(strengthBarSliderDistance - 0.5f/2), 2) + 2; strength = - 32 * powf(fabs(strengthBarSliderDistance - 0.5f/2), 2) + 2;
personVelocityX *= strength * adameStrength; personVelocityX *= strength * adameStrength;
scene_moveRenderObject(&gameScene, strengthBarId, 0, CAMERA_HEIGHT * 2); scene_moveRenderObject(&gameScene, strengthBarId, 0, CAMERA_HEIGHT * 2);
scene_moveRenderObject(&gameScene, strengthBarSliderId, 0, CAMERA_HEIGHT * 2); scene_moveRenderObject(&gameScene, strengthBarSliderId, 0, CAMERA_HEIGHT * 2);
scene_removeRenderObject(&gameScene, pressSpaceShadowId);
scene_removeRenderObject(&gameScene, pressSpaceTextId);
} }
} }
// press space to teach respect
+2 -1
View File
@@ -1,5 +1,6 @@
#include "input.h" #include "input.h"
#include <SDL2/SDL_scancode.h> #include <SDL2/SDL_scancode.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
@@ -23,6 +24,7 @@ void input_pollKeyDown(SDL_Scancode code) {
printf("Scancode: %d\n", code); printf("Scancode: %d\n", code);
keyEvent_t * handlers = eventsKeyDown[code]; keyEvent_t * handlers = eventsKeyDown[code];
for(int i = 0; i < eventKeyDown_counts[code]; i++) { for(int i = 0; i < eventKeyDown_counts[code]; i++) {
printf("Given\n");
handlers[i](); handlers[i]();
} }
} }
@@ -81,4 +83,3 @@ void input_removeKeyUpListener(SDL_Scancode code, keyEvent_t handler) {
} }
eventKeyUp_counts[code]--; eventKeyUp_counts[code]--;
} }
+16 -8
View File
@@ -12,6 +12,12 @@
#include <unistd.h> #include <unistd.h>
#include "cinematic.h" #include "cinematic.h"
#include "scaller.h" #include "scaller.h"
#include "main.h"
//Web asembly
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
clock_t timecode; clock_t timecode;
long iter; long iter;
@@ -40,14 +46,10 @@ static void game_setup(void) {
RenderObject_t text = EMPTY_RENDER_OBJECT; RenderObject_t text = EMPTY_RENDER_OBJECT;
SDL_Color white = {255, 255, 255, 255}; SDL_Color white = {255, 255, 255, 255};
TTF_Init(); 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); input_listenKeyDown(SDL_SCANCODE_SPACE, space);
} }
static void game_loop(void) { void game_loop(void) {
float delta = ((float)(clock() - timecode)) / CLOCKS_PER_SEC; float delta = ((float)(clock() - timecode)) / CLOCKS_PER_SEC;
//update time //update time
@@ -62,8 +64,7 @@ static void game_loop(void) {
timecode = clock(); timecode = clock();
} }
int main(int argc, char ** argv) {
int main(void) {
TTF_Init(); TTF_Init();
if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) != 0){ if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) != 0){
printf("Music sound could not be open!\n" printf("Music sound could not be open!\n"
@@ -75,8 +76,15 @@ int main(void) {
timecode = clock(); timecode = clock();
Mix_Init(MIX_INIT_MP3); Mix_Init(MIX_INIT_MP3);
renderer_main(game_setup, game_loop); renderer_main(argc, argv, game_setup);
Mix_CloseAudio(); Mix_CloseAudio();
text_freeFontCache(); 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
+13 -4
View File
@@ -20,21 +20,30 @@ static Mix_Music * music;
Scene_t menuScene = { NULL, 0, 0, 0, 0, 0 }; Scene_t menuScene = { NULL, 0, 0, 0, 0, 0 };
//Web asembly
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
int menu_staticRender(void){ int menu_staticRender(void){
MonitorSize_t size = renderer_getMonitorSize(); MonitorSize_t size = renderer_getMonitorSize();
float aspectRatio = (float)size.width / size.height; 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){ if(backgroundImageSurface == NULL){
printf("cannot load background"); printf("Could not load menu background\n");
#ifdef __EMSCRIPTEN__
emscripten_pause_main_loop();
#endif
return -1; return -1;
} }
music = Mix_LoadMUS("./assets/music/menu.wav"); music = Mix_LoadMUS("assets/music/menu.ogg");
if(music == NULL) { if(music == NULL) {
fprintf(stderr, "Menu music not found!\n"); fprintf(stderr, "Menu music not found!: %s\n", Mix_GetError());
exit(1); exit(1);
} }
+77 -29
View File
@@ -4,7 +4,9 @@
#include <SDL2/SDL_error.h> #include <SDL2/SDL_error.h>
#include <SDL2/SDL_rect.h> #include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h> #include <SDL2/SDL_render.h>
#include <SDL2/SDL_scancode.h>
#include <SDL2/SDL_timer.h> #include <SDL2/SDL_timer.h>
#include <SDL2/SDL_video.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -12,6 +14,12 @@
#include <math.h> #include <math.h>
#include "input.h" #include "input.h"
#include "scene.h" #include "scene.h"
#include "main.h"
//Web asembly
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
bool running; bool running;
MonitorSize_t monitorSize; MonitorSize_t monitorSize;
@@ -19,35 +27,22 @@ SDL_Window* win;
SDL_Renderer * renderer; SDL_Renderer * renderer;
const Scene_t * scene; const Scene_t * scene;
int scenesCount = 0; int scenesCount = 0;
bool fullscreen = false;
//int (*compar)(const void [.size], const void [.size], void *)
void renderer_main(void(*game_setup)(void), void(*game_loop)(void)) { static void toggleFullscreen(void) {
// returns zero on success else non-zero printf("Toggling\n");
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { if(!fullscreen) {
printf("error initializing SDL: %s\n", SDL_GetError()); SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN);
fullscreen = true;
} }
win = SDL_CreateWindow("The legend of ADAM", // creates a window else {
SDL_WINDOWPOS_CENTERED, SDL_SetWindowFullscreen(win, 0);
SDL_WINDOWPOS_CENTERED, fullscreen = false;
1280, 720, 0);
Uint32 render_flags = SDL_RENDERER_ACCELERATED;
// creates a renderer to render our images
renderer = SDL_CreateRenderer(win, -1, render_flags);
if(renderer == NULL) {
fprintf(stderr, "Could not create renderer\n");
} }
}
SDL_GetRendererOutputSize(renderer, &monitorSize.width, &monitorSize.height); static bool main_loop(void) {
input_init();
running = true;
game_setup();
while(running) {
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
game_loop(); game_loop();
@@ -74,17 +69,70 @@ void renderer_main(void(*game_setup)(void), void(*game_loop)(void)) {
} }
} }
//no need to make an extra render pass if(!running)
if(!running) break; return false;
if(scene != NULL) if(scene != NULL)
scene_render(scene); scene_render(scene);
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
SDL_Delay(1000 / 400); //400fps or u a pussy
} #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)) {
// returns zero on success else non-zero
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf("error initializing SDL: %s\n", SDL_GetError());
}
int width = 1280;
int height = 720;
if(argc > 2) {
width = atoi(argv[1]);
height = atoi(argv[2]);
}
win = SDL_CreateWindow("The legend of ADAM", // creates a window
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
width, height, 0);
Uint32 render_flags = SDL_RENDERER_ACCELERATED;
input_init();
input_listenKeyDown(SDL_SCANCODE_F11, toggleFullscreen);
// creates a renderer to render our images
renderer = SDL_CreateRenderer(win, -1, render_flags);
if(renderer == NULL) {
fprintf(stderr, "Could not create renderer\n");
}
SDL_GetRendererOutputSize(renderer, &monitorSize.width, &monitorSize.height);
running = true;
game_setup();
#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) { void renderer_setScene(const Scene_t * newScene) {
scene = newScene; scene = newScene;
} }
+1 -1
View File
@@ -16,7 +16,7 @@ typedef struct MonitorSize {
int width, height; int width, height;
} MonitorSize_t; } MonitorSize_t;
void renderer_main(void(*game_setup)(void), void(*game_loop)(void)); void renderer_main(int argc, char ** argv, void(*game_loop)(void));
MonitorSize_t renderer_getMonitorSize(void); MonitorSize_t renderer_getMonitorSize(void);
void renderer_setScene(const Scene_t * newScene); void renderer_setScene(const Scene_t * newScene);
const Scene_t * renderer_getScene(void); const Scene_t * renderer_getScene(void);
+10 -5
View File
@@ -5,8 +5,11 @@
#include <SDL2/SDL_image.h> #include <SDL2/SDL_image.h>
#include <SDL2/SDL_render.h> #include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h> #include <SDL2/SDL_surface.h>
#include <float.h>
#include <stdio.h> #include <stdio.h>
#include <string.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" }; const RenderObject_t EMPTY_RENDER_OBJECT = { 0, 0.f, 0.f, 0.f, 0.f, 0.f, NULL, "Undefined" };
@@ -62,8 +65,6 @@ RenderId_t scene_addRenderObject(Scene_t * scene, RenderObject_t object, int x,
scene->objectCount++; scene->objectCount++;
RenderId_t itemId = scene->lastId++; RenderId_t itemId = scene->lastId++;
printf("adding item: %s: %d\n", object.name, itemId);
if(scene->objectCount * sizeof(SceneObject_t) > scene->objectArraySize) { if(scene->objectCount * sizeof(SceneObject_t) > scene->objectArraySize) {
scene->objectArraySize = scene->objectCount * sizeof(SceneObject_t); scene->objectArraySize = scene->objectCount * sizeof(SceneObject_t);
scene->objects = realloc(scene->objects, scene->objectArraySize); scene->objects = realloc(scene->objects, scene->objectArraySize);
@@ -84,7 +85,11 @@ RenderId_t scene_addRenderObject(Scene_t * scene, RenderObject_t object, int x,
scene->objects[scene->objectCount - 1] = item; scene->objects[scene->objectCount - 1] = item;
//the objects are sorted //the objects are sorted
#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); qsort(scene->objects, scene->objectCount, sizeof(SceneObject_t), (__compar_fn_t)compartSort);
#endif
return itemId; return itemId;
} }
@@ -121,11 +126,11 @@ void scene_removeRenderObject(Scene_t * scene, RenderId_t id) {
if(index < 0) if(index < 0)
return; return;
printf("removing item: %s: %d\n", scene->objects[index].renderObj.name, id);
scene->objectCount--; scene->objectCount--;
SDL_DestroyTexture(scene->objects[index].cache);
SDL_FreeSurface(scene->objects[index].renderObj.surface);
for(int i = index; i < scene->objectCount; i++) { for(int i = index; i < scene->objectCount; i++) {
scene->objects[i] = scene->objects[i + 1]; scene->objects[i] = scene->objects[i + 1];
} }