diff --git a/cmake-build-debug/CMakeFiles/nhf.dir/C.includecache b/cmake-build-debug/CMakeFiles/nhf.dir/C.includecache index 02fc3247091ca04fc709d04232ad90bcd6c19e82..35b33f6b338166ab307d40330b2f09baede540b3 100644 --- a/cmake-build-debug/CMakeFiles/nhf.dir/C.includecache +++ b/cmake-build-debug/CMakeFiles/nhf.dir/C.includecache @@ -6,18 +6,16 @@ #IncludeRegexTransform: -/home/blint/projects/nhf/main.c -view.h -/home/blint/projects/nhf/view.h +/home/blint/projects/nhf/move.c move.h /home/blint/projects/nhf/move.h -SDL2/SDL_image.h -- -SDL2/SDL2_gfxPrimitives.h +view.h +/home/blint/projects/nhf/view.h +SDL.h - stdbool.h - -time.h +math.h - /home/blint/projects/nhf/move.h @@ -25,6 +23,8 @@ SDL.h - stdbool.h - +math.h +- /home/blint/projects/nhf/view.h SDL2/SDL.h diff --git a/cmake-build-debug/CMakeFiles/nhf.dir/main.c.o b/cmake-build-debug/CMakeFiles/nhf.dir/main.c.o index 4412b56eb15dbb28531e4f01a134ebed894420c2..57729bf1d1f120f7d10af9e495fdfcaf5e11b066 100644 Binary files a/cmake-build-debug/CMakeFiles/nhf.dir/main.c.o and b/cmake-build-debug/CMakeFiles/nhf.dir/main.c.o differ diff --git a/cmake-build-debug/CMakeFiles/nhf.dir/move.c.o b/cmake-build-debug/CMakeFiles/nhf.dir/move.c.o index 8398855cb108592e767563cc8c946b5f37ca55f4..cc0422c7fcdba817f07df543f633d5576b9d058f 100644 Binary files a/cmake-build-debug/CMakeFiles/nhf.dir/move.c.o and b/cmake-build-debug/CMakeFiles/nhf.dir/move.c.o differ diff --git a/cmake-build-debug/nhf b/cmake-build-debug/nhf index 63ebd209c07ee5c4110b312dade39ae93ed06c39..80e49ec71ed2bd6d4af6eb31def774258b4ed613 100755 Binary files a/cmake-build-debug/nhf and b/cmake-build-debug/nhf differ diff --git a/main.c b/main.c index 9114f58647091273969dd73b0a82952b29ff852a..fd9ffd79352a46ed97eef05dcaa51f52c73f7de2 100644 --- a/main.c +++ b/main.c @@ -7,7 +7,7 @@ typedef struct Palyaelem{ int x, y; - int hossz; + int hossz, magassag; struct Palyaelem *kov; }Palyaelem; @@ -23,6 +23,7 @@ Palyaelem *gen_elem() elem->hossz = rand() % (W * 2); elem->x = rand() % W; elem->y = rand() % (H - (owl * 2)); + elem->magassag = 20; elem->kov = NULL; return elem; } @@ -33,7 +34,7 @@ void palyaelem_kirajzolo(Palyaelem *elem, SDL_Renderer *prenderer) r.x = elem->x; r.y = elem->y; r.w = elem->hossz; - r.h = 20; + r.h = elem->magassag; SDL_SetRenderDrawColor( renderer, 0, 255, 0, 255 ); SDL_RenderFillRect( prenderer, &r ); @@ -41,9 +42,21 @@ void palyaelem_kirajzolo(Palyaelem *elem, SDL_Renderer *prenderer) void palyakirajzol(SDL_Renderer *prenderer) { + Palyaelem alap; + alap.x = 0; + alap.y = 550; + alap.hossz = 900; + alap.magassag = 50; + palyaelem_kirajzolo(&alap, prenderer); } +Uint32 idozit(Uint32 ms, void *param) { + SDL_Event ev; + ev.type = SDL_USEREVENT; + SDL_PushEvent(&ev); + return ms; /* ujabb varakozas */ +} int main(int argc, char *argv[]) { @@ -53,15 +66,18 @@ int main(int argc, char *argv[]) { SDL_Texture *bagoly = kep_betolt(renderer, "infoc.png"); + SDL_TimerID timer_id = SDL_AddTimer(20, idozit, NULL); + SDL_RenderPresent(renderer); bool quit = false; bool moving = false; int elozo_x = 0; - int elozo_y = 0; + int elozo_y = 550 - owl; Palyaelem *elem = gen_elem(); + Palyaelem *mozgo = elem; while (!quit) { @@ -71,12 +87,13 @@ int main(int argc, char *argv[]) { SDL_RenderClear(renderer); - palyaelem_kirajzolo(elem, renderer); + palyaelem_kirajzolo(mozgo, renderer); + palyakirajzol(renderer); keywatcher(ev, bagoly, elozo_x, elozo_y, &quit, &moving, renderer); if (moving){ - move_draw(ev, bagoly, renderer, &elozo_x, &elozo_y); + move_draw(ev, bagoly, renderer, &elozo_x, &elozo_y, timer_id); } SDL_RenderPresent(renderer); diff --git a/move.c b/move.c index e95b1b5cc939bff5f50934135e6dd902c1a314f4..b0c8d8bbba94c54684d29bb6e929e32632f6f15a 100644 --- a/move.c +++ b/move.c @@ -6,55 +6,88 @@ #include "view.h" #include <SDL.h> #include <stdbool.h> +#include <math.h> -int v = 5; -int vx = 0; -int vy = 0; +double v = 5.0; +double vx = 0; +double vy = 0; +double g = 9.82; +double pi = 3.14159; +int t = 0; +bool jump = false; +bool jumping = false; -static void move(SDL_Event ev) +static void move(SDL_Event ev, SDL_TimerID timer) { + double y_tmp = 0; switch (ev.type) { case SDL_KEYDOWN: - if (ev.key.keysym.sym == SDLK_a) { - vx = -1; - vx *= v; - break; - } + switch (ev.key.keysym.sym) { + case SDLK_LEFT: + vx = -1; + vx *= v; + break; - if (ev.key.keysym.sym == SDLK_d) { - vx = 1; - vx *= v; - break; - } + case SDLK_RIGHT: + vx = 1; + vx *= v; + break; - if (ev.key.keysym.sym == SDLK_w) { - vy = -1; - vy *= v; - break; - } + case SDLK_SPACE: + t = 0; + if (vx == 0) { + y_tmp = v - (g * t); + if (y_tmp >= 0) + vy = -1; + else + vy = 1; + } + else + { + y_tmp = v * sin(pi/4) - g*t; + if (y_tmp >= 0) + vy = -1; + else + vy = 1; + } + vy *= v; + jump = true; + jumping = true; + break; - if (ev.key.keysym.sym == SDLK_s) { - vy = 1; - vy *= v; - break; + /*case SDLK_w: + vy = -1; + vy *= v; + break; + + case SDLK_s: + vy = 1; + vy *= v; + break; + */ } case SDL_KEYUP: switch (ev.key.keysym.sym) { - case SDLK_a: + case SDLK_LEFT: if (vx < 0) vx = 0; break; - case SDLK_d: + case SDLK_RIGHT: if (vx > 0) vx = 0; break; - case SDLK_w: + /*case SDLK_SPACE: + + jump = false; + break; + + /*case SDLK_w: if (vy < 0) vy = 0; break; @@ -62,11 +95,15 @@ static void move(SDL_Event ev) case SDLK_s: if (vy > 0) vy = 0; - break; + break;*/ default: break; } break; + case SDL_USEREVENT: + if (jumping) + t++; + break; } } @@ -82,6 +119,10 @@ void keywatcher(SDL_Event ev, SDL_Texture *kep, int x, int y, bool *quit, bool * case SDLK_ESCAPE: *quit = true; break; + + case SDLK_r: + kirajzol(renderer, kep, 0, 550 - owl); + break; } break; @@ -91,11 +132,14 @@ void keywatcher(SDL_Event ev, SDL_Texture *kep, int x, int y, bool *quit, bool * } } -void move_draw(SDL_Event ev, SDL_Texture *kep, SDL_Renderer *renderer, int *x, int *y) +void move_draw(SDL_Event ev, SDL_Texture *kep, SDL_Renderer *renderer, int *x, int *y, SDL_TimerID timer) { - move(ev); - *x += vx; - *y += vy; + move(ev, timer); + *x += (int) vx; + *y += (int) vy; + + if (*y == (550 - owl) && !jump) + jumping = false; kirajzol(renderer, kep, *x, *y); } \ No newline at end of file diff --git a/move.h b/move.h index 04b953ccec7e96f1c00cc3d06d40fd9b1bf101d3..3ca504f4407ca926014b1b66fc2ab3a01c76e016 100644 --- a/move.h +++ b/move.h @@ -7,15 +7,21 @@ #include <SDL.h> #include <stdbool.h> +#include <math.h> -extern int v; -extern int vx; -extern int vy; +extern double v; +extern double vx; +extern double vy; +extern double g; +extern double pi; +extern int t; +extern bool jump; +extern bool jumping; -static void move(SDL_Event ev); +static void move(SDL_Event ev, SDL_TimerID timer); void keywatcher(SDL_Event ev, SDL_Texture *kep, int x, int y, bool *quit, bool *moving, SDL_Renderer *renderer); -void move_draw(SDL_Event ev, SDL_Texture *kep, SDL_Renderer *renderer, int *x, int *y); +void move_draw(SDL_Event ev, SDL_Texture *kep, SDL_Renderer *renderer, int *x, int *y, SDL_TimerID timer); #endif //NHF_MOVE_H \ No newline at end of file