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
Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

+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;
}
+2
View File
@@ -10,4 +10,6 @@ int game_render(float delta);
void game_remove(void);
void game_handleSpaceKeyInput(void);
#endif
+55 -17
View File
@@ -5,42 +5,80 @@
#include <stdbool.h>
keyEvent_t * events[SDL_NUM_SCANCODES];
int event_counts[SDL_NUM_SCANCODES];
keyEvent_t * eventsKeyDown[SDL_NUM_SCANCODES];
int eventKeyDown_counts[SDL_NUM_SCANCODES];
keyEvent_t * eventsKeyUp[SDL_NUM_SCANCODES];
int eventKeyUp_counts[SDL_NUM_SCANCODES];
void input_init(void) {
memset(events, 0, SDL_NUM_SCANCODES * sizeof(keyEvent_t *));
memset(event_counts, 0, SDL_NUM_SCANCODES * sizeof(int));
memset(eventsKeyDown, 0, SDL_NUM_SCANCODES * sizeof(keyEvent_t *));
memset(eventKeyDown_counts, 0, SDL_NUM_SCANCODES * sizeof(int));
memset(eventsKeyUp, 0, SDL_NUM_SCANCODES * sizeof(keyEvent_t *));
memset(eventKeyUp_counts, 0, SDL_NUM_SCANCODES * sizeof(int));
}
void input_poll(SDL_Scancode code) {
void input_pollKeyDown(SDL_Scancode code) {
printf("Scancode: %d\n", code);
keyEvent_t * handlers = events[code];
for(int i = 0; i < event_counts[code]; i++) {
keyEvent_t * handlers = eventsKeyDown[code];
for(int i = 0; i < eventKeyDown_counts[code]; i++) {
handlers[i]();
}
}
void input_pollKeyUp(SDL_Scancode code){
printf("Scancode: %d\n", code);
keyEvent_t * handlers = eventsKeyUp[code];
for(int i = 0; i < eventKeyUp_counts[code]; i++) {
handlers[i]();
}
}
void input_listen(SDL_Scancode code, keyEvent_t handler) {
event_counts[code]++;
events[code] = (keyEvent_t *)realloc(events[code], event_counts[code] * sizeof(keyEvent_t));
events[code][event_counts[code] - 1] = handler;
void input_listenKeyDown(SDL_Scancode code, keyEvent_t handler) {
eventKeyDown_counts[code]++;
eventsKeyDown[code] = (keyEvent_t *)realloc(eventsKeyDown[code], eventKeyDown_counts[code] * sizeof(keyEvent_t));
eventsKeyDown[code][eventKeyDown_counts[code] - 1] = handler;
}
void input_removeListener(SDL_Scancode code, keyEvent_t handler) {
void input_listenKeyUp(SDL_Scancode code, keyEvent_t handler) {
eventKeyUp_counts[code]++;
eventsKeyUp[code] = (keyEvent_t *)realloc(eventsKeyUp[code], eventKeyUp_counts[code] * sizeof(keyEvent_t));
eventsKeyUp[code][eventKeyUp_counts[code] - 1] = handler;
}
void input_removeKeyDownListener(SDL_Scancode code, keyEvent_t handler) {
bool found = false;
for(int i = 0; i < event_counts[code]; i++) {
if(events[code][i] == handler) {
for(int i = 0; i < eventKeyDown_counts[code]; i++) {
if(eventsKeyDown[code][i] == handler) {
found = true;
break;
}
if(found) {
if(i == event_counts[code] - 1) {
if(i == eventKeyDown_counts[code] - 1) {
break;
}
events[code][i] = events[code][i + 1];
eventsKeyDown[code][i] = eventsKeyDown[code][i + 1];
}
}
event_counts[code]--;
eventKeyDown_counts[code]--;
}
void input_removeKeyUpListener(SDL_Scancode code, keyEvent_t handler) {
bool found = false;
for(int i = 0; i < eventKeyUp_counts[code]; i++) {
if(eventsKeyUp[code][i] == handler) {
found = true;
break;
}
if(found) {
if(i == eventKeyUp_counts[code] - 1) {
break;
}
eventsKeyUp[code][i] = eventsKeyUp[code][i + 1];
}
}
eventKeyUp_counts[code]--;
}
+7 -3
View File
@@ -7,10 +7,14 @@ typedef void(*keyEvent_t)(void);
//renderer facing
void input_init(void);
void input_poll(SDL_Scancode);
void input_pollKeyDown(SDL_Scancode);
void input_pollKeyUp(SDL_Scancode);
//user facing
void input_listen(SDL_Scancode code, keyEvent_t);
void input_removeListener(SDL_Scancode code, keyEvent_t handler);
void input_listenKeyDown(SDL_Scancode code, keyEvent_t);
void input_removeKeyDownListener(SDL_Scancode code, keyEvent_t handler);
void input_listenKeyUp(SDL_Scancode code, keyEvent_t);
void input_removeKeyUpListener(SDL_Scancode code, keyEvent_t handler);
#endif
+4 -1
View File
@@ -28,6 +28,9 @@ static void space(void) {
game_staticRender();
renderer_setScene(&gameScene);
}
else {
game_handleSpaceKeyInput();
}
}
Scene_t demo;
@@ -41,7 +44,7 @@ static void game_setup(void) {
printf("error");
exit(1);
}
input_listen(SDL_SCANCODE_SPACE, space);
input_listenKeyDown(SDL_SCANCODE_SPACE, space);
}
static void game_loop(void) {
+4 -1
View File
@@ -66,7 +66,10 @@ void renderer_main(void(*game_setup)(void), void(*game_loop)(void)) {
case SDL_KEYDOWN:
// keyboard API for key pressed
input_poll(event.key.keysym.scancode);
input_pollKeyDown(event.key.keysym.scancode);
break;
case SDL_KEYUP:
input_pollKeyUp(event.key.keysym.scancode);
break;
}
}