start game physics

This commit is contained in:
2023-01-22 13:09:38 +01:00
parent 5c6b6513c3
commit b7ca43006a
9 changed files with 154 additions and 39 deletions
+82 -17
View File
@@ -1,24 +1,31 @@
#include "game.h"
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_surface.h>
#include "scaller.h"
#define SPEED 1.f/10000000000
#define GRAVITY 10000.f
static RenderId_t menuObjects[10];
static unsigned menuObjectsSize = 0;
static Mix_Music * music;
Scene_t gameScene;
int game_staticRender(void){
MonitorSize_t size = renderer_getMonitorSize();
float aspectRatio = (float)size.width / size.height;
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;
SDL_Surface* ppSurface = IMG_Load("./assets/img/pp.png");
if(ppSurface == NULL){
printf("cannot load background");
return -1;
}
static const float personVelocityX = 200;
static float personVelocityY = 0;
static void playMusic(){
music = Mix_LoadMUS("./assets/music/jojo arab.mp3");
if(music == NULL) {
fprintf(stderr, "Prout not found!\n");
@@ -29,23 +36,76 @@ int game_staticRender(void){
printf("Music sound could not be played!\n"
"SDL_Error: %s\n", Mix_GetError());
Mix_FreeMusic(music);
running = false;
running = false;
}
}
RenderObject_t pp;
strcpy(pp.name, "pp");
pp.z = 10;
pp.surface = ppSurface;
pp.width = 1;
pp.height = 1;
scaller(pp.surface, 1, &pp.width, &pp.height);
menuObjects[menuObjectsSize] = scene_addRenderObject(&gameScene, pp, CAMERA_WIDTH * 0.35f, 0);
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;
strcpy(gameBackground.name, "gameBackground");
gameBackground.z = 0;
gameBackground.surface = gameBackgroundSurface;
scaller(gameBackground.surface, 1, &gameBackground.width, &gameBackground.height);
gameBackground.height = 1;
menuObjects[menuObjectsSize] = scene_addRenderObject(&gameScene, gameBackground, 0, 0);
renderer_setScene(&gameScene);
++menuObjectsSize;
return 0;
}
int game_render([[maybe_unused]] float delta){
for(unsigned i = 2; i < menuObjectsSize; ++i){
scene_removeRenderObject(&gameScene, menuObjects[i]);
}
menuObjectsSize = 2;
deltaAccu += delta;
SDL_Surface* buildingSurface = IMG_Load("./assets/img/building.png");
RenderObject_t building;
strcpy(building.name, "building");
building.z = 9;
building.surface = buildingSurface;
building.width = 0.25;
building.height = 1;
menuObjects[menuObjectsSize] = scene_addRenderObject(&gameScene, building, 0, 0);
++menuObjectsSize;
SDL_Surface* personSurface = IMG_Load("./assets/img/person.png");
RenderObject_t person;
strcpy(person.name, "person");
person.z = 9;
person.surface = personSurface;
person.width = 0.25f;
scaller(person.surface, 0.05f, &person.width, &person.height);
// if(!crashed && deltaAccu >= SPEED && isSpaceKeypressed){
if(!crashed && isSpaceKeypressed){
personPositionX += personVelocityX * delta;
gameScene.x = personPositionX - startPersonPositionX;
personVelocityY += GRAVITY * delta;
personPositionY += personVelocityY * delta;
if(personPositionY + CAMERA_HEIGHT * person.height >= CAMERA_HEIGHT - 2) crashed = true;
}
menuObjects[menuObjectsSize] = scene_addRenderObject(&gameScene, person, personPositionX, personPositionY);
++menuObjectsSize;
return 0;
}
@@ -54,3 +114,8 @@ void game_remove(void){
Mix_FreeMusic(music);
menuObjectsSize = 0;
}
void game_handleSpaceKeyInput(void){
isSpaceKeypressed = true;
}