From 5c6b6513c317a213f0a136fabdf9dc32309bd5a8 Mon Sep 17 00:00:00 2001 From: Marc Date: Sat, 21 Jan 2023 19:17:51 +0100 Subject: [PATCH] perfection --- CMakeLists.txt | 1 + src/cinematic.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ src/cinematic.h | 11 ++++++++++ src/game.c | 17 +++++++++++++++- src/main.c | 26 +++++++++--------------- src/menu.c | 17 ++++++++++++++++ src/scene.c | 32 ++++++++++++----------------- src/scene.h | 2 +- src/text.c | 6 +++--- 9 files changed, 124 insertions(+), 41 deletions(-) create mode 100644 src/cinematic.c create mode 100644 src/cinematic.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 37b9f37..52be6ed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,7 @@ set(SRC src/scaller.c src/scene.c src/game.c + src/cinematic.c ) # add the executable diff --git a/src/cinematic.c b/src/cinematic.c new file mode 100644 index 0000000..bb27e05 --- /dev/null +++ b/src/cinematic.c @@ -0,0 +1,53 @@ +#include "cinematic.h" +#include "renderer.h" +#include "scene.h" +#include "text.h" +#include +#include + +Scene_t cinematicScene; + +#define SPEED 1.f/1000 + +static Mix_Music * music; + +float accu = 0; + +void cinematic_staticRender(void) { + RenderObject_t AdamesLegend; + strcpy(AdamesLegend.name, "Adame's Legend"); + SDL_Color black = {255, 255, 255, 255}; + text_renderText(&AdamesLegend, 8, 0.8f, black, "./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."); + scene_addRenderObject(&cinematicScene, AdamesLegend, 10, 80); + + music = Mix_LoadMUS("./assets/music/chinese rap.mp3"); + if(music == NULL) { + fprintf(stderr, "Prout not found!\n"); + exit(1); + } + + if(Mix_PlayMusic(music, 0) != 0){ + printf("Music sound could not be played!\n" + "SDL_Error: %s\n", Mix_GetError()); + Mix_FreeMusic(music); + running = false; + } +}RenderId_t id = -1; + + +void cinematic_render([[maybe_unused]]float delta) { + accu += delta; + + if(accu >= SPEED && cinematicScene.y < 90){ + printf("Scroll %f\n", cinematicScene.y); + cinematicScene.y += 0.1; + accu -= SPEED; + } + + renderer_setScene(&cinematicScene); +} + +void cinematic_remove(void) { + scene_free(&cinematicScene); + Mix_FreeMusic(music); +} diff --git a/src/cinematic.h b/src/cinematic.h new file mode 100644 index 0000000..bcf629a --- /dev/null +++ b/src/cinematic.h @@ -0,0 +1,11 @@ +#ifndef __CINEMATIC_H__ +#define __CINEMATIC_H__ +#include "scene.h" + +extern Scene_t cinematicScene; + +void cinematic_render(float delta); +void cinematic_remove(void); +void cinematic_staticRender(void); + +#endif diff --git a/src/game.c b/src/game.c index 9ef7a3e..7ffbbbd 100644 --- a/src/game.c +++ b/src/game.c @@ -1,10 +1,11 @@ #include "game.h" #include +#include #include "scaller.h" static RenderId_t menuObjects[10]; static unsigned menuObjectsSize = 0; - +static Mix_Music * music; Scene_t gameScene; @@ -18,6 +19,19 @@ int game_staticRender(void){ return -1; } + music = Mix_LoadMUS("./assets/music/jojo arab.mp3"); + if(music == NULL) { + fprintf(stderr, "Prout not found!\n"); + exit(1); + } + + if(Mix_PlayMusic(music, 0) != 0){ + printf("Music sound could not be played!\n" + "SDL_Error: %s\n", Mix_GetError()); + Mix_FreeMusic(music); + running = false; + } + RenderObject_t pp; strcpy(pp.name, "pp"); pp.z = 10; @@ -37,5 +51,6 @@ int game_render([[maybe_unused]] float delta){ void game_remove(void){ scene_free(&gameScene); + Mix_FreeMusic(music); menuObjectsSize = 0; } diff --git a/src/main.c b/src/main.c index 541a8ec..a28b76a 100644 --- a/src/main.c +++ b/src/main.c @@ -10,25 +10,23 @@ #include #include #include +#include "cinematic.h" #include "scaller.h" clock_t timecode; long iter; -Mix_Music *prout; static void space(void) { printf("space!\n"); - if(Mix_PlayMusic(prout, 0) != 0){ - printf("Music sound could not be played!\n" - "SDL_Error: %s\n", Mix_GetError()); - Mix_FreeMusic(prout); - running = false; - } if(renderer_getScene() == &menuScene){ menu_remove(); - renderer_setScene(&gameScene); + cinematic_staticRender(); + renderer_setScene(&cinematicScene); + } else if(renderer_getScene() == &cinematicScene) { + cinematic_remove(); game_staticRender(); + renderer_setScene(&gameScene); } } @@ -53,7 +51,9 @@ static void game_loop(void) { if(renderer_getScene() == &menuScene){ menu_render(delta); } - else{ + else if(renderer_getScene() == &cinematicScene){ + cinematic_render(delta); + } else { game_render(delta); } timecode = clock(); @@ -68,20 +68,12 @@ int main(void) { running = false; } - prout = Mix_LoadMUS("./assets/music/jojo arab.mp3"); - if(prout == NULL) { - fprintf(stderr, "Prout not found!\n"); - exit(1); - } - iter = 0; timecode = clock(); Mix_Init(MIX_INIT_MP3); renderer_main(game_setup, game_loop); - Mix_FreeMusic(prout); - Mix_CloseAudio(); text_freeFontCache(); } diff --git a/src/menu.c b/src/menu.c index 1ccbeec..0f818a4 100644 --- a/src/menu.c +++ b/src/menu.c @@ -1,5 +1,6 @@ #include "menu.h" #include +#include #include #include "renderer.h" #include "scaller.h" @@ -15,6 +16,7 @@ static float blinkDelta = 0; static bool increaseTextBrightness = false; static Uint8 textOpacity = 255; +static Mix_Music * music; Scene_t menuScene = { NULL, 0, 0, 0, 0, 0 }; @@ -30,6 +32,19 @@ int menu_staticRender(void){ return -1; } + music = Mix_LoadMUS("./assets/music/pet.mp3"); + if(music == NULL) { + fprintf(stderr, "Prout not found!\n"); + exit(1); + } + + if(Mix_PlayMusic(music, 0) != 0){ + printf("Music sound could not be played!\n" + "SDL_Error: %s\n", Mix_GetError()); + Mix_FreeMusic(music); + running = false; + } + RenderObject_t backgroundImage; strcpy(backgroundImage.name, "BackgroundImg"); backgroundImage.z = 0; @@ -94,5 +109,7 @@ int menu_render(float delta){ void menu_remove(void){ //scene_free(&menuScene); + Mix_FreeMusic(music); + menuObjectsSize = 0; } diff --git a/src/scene.c b/src/scene.c index 3f32de0..cc3b0af 100644 --- a/src/scene.c +++ b/src/scene.c @@ -13,30 +13,24 @@ static int compartSort(const SceneObject_t * a, const SceneObject_t * b) { void scene_render(const Scene_t * scene) { MonitorSize_t monitorSize = renderer_getMonitorSize(); - int minScanX = scene->x - CAMERA_WIDTH; - int minScanY = scene->y - CAMERA_HEIGHT; - int maxScanX = scene->x + CAMERA_WIDTH; - int maxScanY = scene->y + CAMERA_HEIGHT; for(int i = 0; i < scene->objectCount; i++) { const SceneObject_t * object = &scene->objects[i]; - if(object->x > minScanX && object->x < maxScanX && object->y > minScanY && object->y < maxScanY) { - float x = (object->x) / CAMERA_WIDTH; - float y = (object->y) / CAMERA_HEIGHT; - //the object is to be rendered + float x = (object->x - scene->x) / CAMERA_WIDTH; + float y = (object->y - scene->y) / CAMERA_HEIGHT; + //the object is to be rendered - SDL_Rect rect = { - round(x * monitorSize.width) , - round(y * monitorSize.height), - round(object->renderObj.width * monitorSize.width), - round(object->renderObj.height * monitorSize.height) - }; + SDL_Rect rect = { + round(x * monitorSize.width) , + round(y * monitorSize.height), + round(object->renderObj.width * monitorSize.width), + round(object->renderObj.height * monitorSize.height) + }; - int error = SDL_RenderCopyEx(renderer, object->cache, NULL, &rect, object->renderObj.angle_degre, NULL, SDL_FLIP_NONE); - if(error != 0) { - fprintf(stderr, "Render error in object %d with error %s\n", object->id, SDL_GetError()); - exit(1); - } + int error = SDL_RenderCopyEx(renderer, object->cache, NULL, &rect, object->renderObj.angle_degre, NULL, SDL_FLIP_NONE); + if(error != 0) { + fprintf(stderr, "Render error in object %d with error %s\n", object->id, SDL_GetError()); + exit(1); } } } diff --git a/src/scene.h b/src/scene.h index 469cba0..7f27567 100644 --- a/src/scene.h +++ b/src/scene.h @@ -28,7 +28,7 @@ typedef struct Scene { int objectCount; unsigned long objectArraySize; int lastId; - int x,y; + float x,y; } Scene_t; void scene_render(const Scene_t * scene); diff --git a/src/text.c b/src/text.c index 16075b2..41c3ff5 100644 --- a/src/text.c +++ b/src/text.c @@ -6,7 +6,7 @@ #include #define FONT_CACHE_SIZE 100 -#define MAX_TEXT_SIZE 100 +#define MAX_TEXT_SIZE 10000 typedef struct FontCacheObject { char* file; @@ -63,12 +63,12 @@ int text_renderText(RenderObject_t* renderObject, int z, float scale, SDL_Color vsprintf(text, format, args); va_end(args); - SDL_Surface* surface = TTF_RenderText_Blended(font, text, color); + MonitorSize_t monitor = renderer_getMonitorSize(); + SDL_Surface* surface = TTF_RenderText_Blended_Wrapped(font, text, color, monitor.width); int h, w; TTF_SizeText(font, text, &w, &h); - MonitorSize_t monitor = renderer_getMonitorSize(); renderObject->z = z; renderObject->angle_degre = 0;