218 lines
7.3 KiB
C
218 lines
7.3 KiB
C
#include "game.h"
|
|
#include <math.h>
|
|
#include <SDL2/SDL_image.h>
|
|
#include <SDL2/SDL_mixer.h>
|
|
#include <SDL2/SDL_surface.h>
|
|
#include "scaller.h"
|
|
#include "scene.h"
|
|
#include "text.h"
|
|
|
|
#define SLIDER_SPEED 1.f/500
|
|
#define GRAVITY 10000.f
|
|
|
|
static Mix_Music * music;
|
|
|
|
Scene_t gameScene;
|
|
|
|
static bool isSpaceKeypressed = false;
|
|
static float deltaAccu = 0;
|
|
static bool crashed = false;
|
|
static const float startPersonPositionX = CAMERA_WIDTH * 0.23f;
|
|
static const float startPersonPositionY = CAMERA_HEIGHT * 0.4f;
|
|
static float personPositionX = startPersonPositionX;
|
|
static float personPositionY = startPersonPositionY;
|
|
|
|
static const float startPersonVelocityX = 200;
|
|
static float personVelocityX = 200;
|
|
static float personVelocityY = 0;
|
|
|
|
float jetons = 0.f;
|
|
|
|
static RenderObject_t person;
|
|
static RenderId_t personId;
|
|
RenderObject_t scoreText;
|
|
static RenderId_t scoreTextId = -1;
|
|
|
|
static RenderObject_t strengthBar;
|
|
static RenderId_t strengthBarId;
|
|
static float strength = 0.f;
|
|
static bool increaseStrength = true;
|
|
|
|
|
|
static float adameStrength = 1.f; // this value is not const as it will be possible to improve the strenght of Adame.
|
|
|
|
static RenderObject_t strengthBarSlider;
|
|
static float strengthBarSliderDistance = 0.f;
|
|
static RenderId_t strengthBarSliderId;
|
|
static bool increaseStrengthBarSlider = true;
|
|
|
|
|
|
|
|
|
|
static void playMusic(){
|
|
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;
|
|
// }
|
|
}
|
|
|
|
int game_staticRender(void){
|
|
playMusic();
|
|
|
|
MonitorSize_t size = renderer_getMonitorSize();
|
|
float aspectRatio = (float)size.width / size.height;
|
|
|
|
SDL_Surface* gameBackgroundSurface = IMG_Load("./assets/img/game_bg.png");
|
|
if(gameBackgroundSurface == NULL){
|
|
printf("cannot load background");
|
|
return -1;
|
|
}
|
|
|
|
|
|
RenderObject_t gameBackground = EMPTY_RENDER_OBJECT;
|
|
strcpy(gameBackground.name, "gameBackground");
|
|
gameBackground.z = 0;
|
|
gameBackground.surface = gameBackgroundSurface;
|
|
scaller(gameBackground.surface, 1, &gameBackground.width, &gameBackground.height);
|
|
gameBackground.height = 1;
|
|
scene_addRenderObject(&gameScene, gameBackground, 0, 0);
|
|
|
|
SDL_Surface* buildingSurface = IMG_Load("./assets/img/building.png");
|
|
RenderObject_t building = EMPTY_RENDER_OBJECT;
|
|
strcpy(building.name, "building");
|
|
building.z = 9;
|
|
building.surface = buildingSurface;
|
|
building.width = 0.25;
|
|
building.height = 1;
|
|
scene_addRenderObject(&gameScene, building, 0, 0);
|
|
|
|
SDL_Surface* personSurface = IMG_Load("./assets/img/person.png");
|
|
strcpy(person.name, "person");
|
|
person.z = 9;
|
|
person.surface = personSurface;
|
|
person.width = 0.25f;
|
|
scaller(person.surface, 0.05f, &person.width, &person.height);
|
|
personId = scene_addRenderObject(&gameScene, person, personPositionX, personPositionY);
|
|
|
|
scoreText = EMPTY_RENDER_OBJECT;
|
|
strcpy(scoreText.name, "scoreText");
|
|
SDL_Color black = {0, 0, 0, 255};
|
|
text_renderText(&scoreText, 50, 0.5f, black, "./assets/fonts/Quinquefive.ttf", 40, "jetons: %d", (unsigned)jetons);
|
|
scoreTextId = scene_addRenderObject(&gameScene, scoreText, gameScene.x + 2, 0);
|
|
|
|
|
|
SDL_Surface* strengthBarSurface = IMG_Load("./assets/img/strengthBar.png");
|
|
strcpy(strengthBar.name, "strengthBar");
|
|
strengthBar.z = 100;
|
|
strengthBar.surface = strengthBarSurface;
|
|
strengthBar.width = 0.50f;
|
|
strengthBar.height = 0.025f;
|
|
strengthBarId = scene_addRenderObject(&gameScene, strengthBar, 0.30f * CAMERA_WIDTH, 0.9f * CAMERA_HEIGHT);
|
|
|
|
|
|
SDL_Surface* strengthBarSliderSurface = IMG_Load("./assets/img/building.png");
|
|
strcpy(strengthBarSlider.name, "strengthBarSlider");
|
|
strengthBarSlider.z = 101;
|
|
strengthBarSlider.surface = strengthBarSliderSurface;
|
|
strengthBarSlider.width = 0.005f;
|
|
strengthBarSlider.height = 0.05f;
|
|
strengthBarSliderId = scene_addRenderObject(&gameScene, strengthBarSlider, 0.30f * CAMERA_WIDTH, 0.89f * CAMERA_HEIGHT);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int game_render([[maybe_unused]] float delta){
|
|
deltaAccu += delta;
|
|
|
|
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)
|
|
increaseStrengthBarSlider = false;
|
|
else if(increaseStrengthBarSlider){
|
|
strengthBarSliderDistance += 0.005f;
|
|
scene_moveRenderObject(&gameScene, strengthBarSliderId, 0.30f * CAMERA_WIDTH + strengthBarSliderDistance * CAMERA_WIDTH, 0.89f * CAMERA_HEIGHT);
|
|
}
|
|
else if(!increaseStrengthBarSlider && strengthBarSliderDistance <= 0.0001f)
|
|
increaseStrengthBarSlider = true;
|
|
else{
|
|
strengthBarSliderDistance -= 0.005f;
|
|
scene_moveRenderObject(&gameScene, strengthBarSliderId, 0.30f * CAMERA_WIDTH + strengthBarSliderDistance * CAMERA_WIDTH, 0.89f * CAMERA_HEIGHT);
|
|
}
|
|
|
|
deltaAccu = 0;
|
|
}
|
|
// if(!crashed && deltaAccu >= SPEED && isSpaceKeypressed){
|
|
if(!crashed && isSpaceKeypressed){
|
|
const float distance = personVelocityX * delta;
|
|
personPositionX += distance;
|
|
gameScene.x = personPositionX - startPersonPositionX;
|
|
|
|
personVelocityY += GRAVITY * delta;
|
|
|
|
personPositionY += personVelocityY * delta;
|
|
jetons += distance;
|
|
scene_moveRenderObject(&gameScene, scoreTextId, gameScene.x + 2, 0);
|
|
|
|
if(personPositionY + CAMERA_HEIGHT * person.height >= CAMERA_HEIGHT - 2){
|
|
crashed = true;
|
|
scene_removeRenderObject(&gameScene, scoreTextId);
|
|
SDL_Color black = {0, 0, 0, 255};
|
|
text_renderText(&scoreText, 50, 0.5f, black, "./assets/fonts/Quinquefive.ttf", 40, "jetons: %d", (unsigned)jetons);
|
|
scoreTextId = scene_addRenderObject(&gameScene, scoreText, gameScene.x + 2, 0);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
scene_moveRenderObject(&gameScene, personId, personPositionX, personPositionY);
|
|
|
|
|
|
return 0;
|
|
}
|
|
|
|
void game_remove(void){
|
|
scene_free(&gameScene);
|
|
Mix_FreeMusic(music);
|
|
}
|
|
|
|
|
|
void game_handleSpaceKeyInput(void){
|
|
if(crashed){
|
|
crashed = false;
|
|
personPositionX = startPersonPositionX;
|
|
personPositionY = startPersonPositionY;
|
|
personVelocityX = startPersonVelocityX;
|
|
personVelocityY = 0;
|
|
isSpaceKeypressed = false;
|
|
gameScene.x = 0;
|
|
gameScene.y = 0;
|
|
strengthBarSliderDistance = 0.f;
|
|
scene_moveRenderObject(&gameScene, scoreTextId, gameScene.x + 2, 0);
|
|
scene_moveRenderObject(&gameScene, strengthBarId, 0.30f * CAMERA_WIDTH, 0.9f * CAMERA_HEIGHT);
|
|
}
|
|
else{
|
|
isSpaceKeypressed = true;
|
|
strength = - 32 * powf(fabs(strengthBarSliderDistance - 0.5f/2), 2) + 2;
|
|
personVelocityX *= strength * adameStrength;
|
|
scene_moveRenderObject(&gameScene, strengthBarId, 0, CAMERA_HEIGHT * 2);
|
|
scene_moveRenderObject(&gameScene, strengthBarSliderId, 0, CAMERA_HEIGHT * 2);
|
|
|
|
}
|
|
}
|