diff --git a/assets/img/building.png b/assets/img/building.png new file mode 100644 index 0000000..6e4635f Binary files /dev/null and b/assets/img/building.png differ diff --git a/assets/img/game_bg.png b/assets/img/game_bg.png new file mode 100644 index 0000000..16fd3c0 Binary files /dev/null and b/assets/img/game_bg.png differ diff --git a/assets/img/person.png b/assets/img/person.png new file mode 100644 index 0000000..395a2cf Binary files /dev/null and b/assets/img/person.png differ diff --git a/src/game.c b/src/game.c index 7ffbbbd..b72d021 100644 --- a/src/game.c +++ b/src/game.c @@ -1,24 +1,31 @@ #include "game.h" #include #include +#include #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; +} diff --git a/src/game.h b/src/game.h index 79b1438..69e481d 100644 --- a/src/game.h +++ b/src/game.h @@ -10,4 +10,6 @@ int game_render(float delta); void game_remove(void); +void game_handleSpaceKeyInput(void); + #endif diff --git a/src/input.c b/src/input.c index 3797c6c..16de9a3 100644 --- a/src/input.c +++ b/src/input.c @@ -5,42 +5,80 @@ #include -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]--; +} + diff --git a/src/input.h b/src/input.h index 8fe9b11..06fe899 100644 --- a/src/input.h +++ b/src/input.h @@ -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 diff --git a/src/main.c b/src/main.c index a28b76a..78d5300 100644 --- a/src/main.c +++ b/src/main.c @@ -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) { diff --git a/src/renderer.c b/src/renderer.c index 650639a..08279e0 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -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; } }