From 8f5248856745169607c6f12d92a15b4d32d8b3ed Mon Sep 17 00:00:00 2001 From: 0xdeadbeer <64986162+0xdeadbeer@users.noreply.github.com> Date: Sun, 12 Nov 2023 21:32:30 +0100 Subject: [PATCH] 02: add simple jumping demo --- 01-introduction/src/main.c | 16 ++- 01-introduction/src/main.o | Bin 7072 -> 7320 bytes 02-jumping/.imgs/showcase.png | Bin 0 -> 3115 bytes 02-jumping/Makefile | 28 ++++ 02-jumping/README.md | 3 + 02-jumping/assets/player/idle.png | Bin 0 -> 1438 bytes 02-jumping/assets/player/jump.png | Bin 0 -> 1021 bytes 02-jumping/assets/player/land.png | Bin 0 -> 1500 bytes 02-jumping/assets/player/run.png | Bin 0 -> 3254 bytes 02-jumping/src/main.c | 207 ++++++++++++++++++++++++++++++ 02-jumping/src/main.o | Bin 0 -> 7704 bytes 02-jumping/src/structs.h | 40 ++++++ 12 files changed, 290 insertions(+), 4 deletions(-) create mode 100644 02-jumping/.imgs/showcase.png create mode 100644 02-jumping/Makefile create mode 100644 02-jumping/README.md create mode 100644 02-jumping/assets/player/idle.png create mode 100644 02-jumping/assets/player/jump.png create mode 100644 02-jumping/assets/player/land.png create mode 100644 02-jumping/assets/player/run.png create mode 100644 02-jumping/src/main.c create mode 100644 02-jumping/src/main.o create mode 100644 02-jumping/src/structs.h diff --git a/01-introduction/src/main.c b/01-introduction/src/main.c index 1ed90aa..05194c8 100644 --- a/01-introduction/src/main.c +++ b/01-introduction/src/main.c @@ -60,6 +60,14 @@ SDL_Texture *load_texture(const char *path) { return texture; } +void switch_animation(struct object *object, SDL_Texture *animation) { + if (object->texture == animation) + return; + + object->animation_slide = 0; + object->texture = animation; +} + struct object *create_object(SDL_Texture *texture, int x, int y, int scale, int resolution) { struct object *object = (struct object *) calloc(1, sizeof(struct object)); @@ -70,7 +78,7 @@ struct object *create_object(SDL_Texture *texture, int x, int y, int scale, int object->texture = texture; object->resolution = resolution; - object->animation_speed = 15; + object->animation_speed = 13; object->scale = scale; object->x = x; object->y = y; @@ -149,15 +157,15 @@ int main(int argc, char *argv[]) { if (game.left) { player->x -= movement_speed; player->flip = SDL_FLIP_HORIZONTAL; - player->texture = run_animation; + switch_animation(player, run_animation); } if (game.right) { player->x += movement_speed; player->flip = SDL_FLIP_NONE; - player->texture = run_animation; + switch_animation(player, run_animation); } if (!game.right && !game.left) { - player->texture = idle_animation; + switch_animation(player, idle_animation); } player->y += gravity; diff --git a/01-introduction/src/main.o b/01-introduction/src/main.o index 243fdc03b30f04414a8b98d5cac234d5bbd96fa8..8e6797fd39be83f0f74990ecf3fa9b0f72e52eef 100644 GIT binary patch delta 1875 zcmZ`(ZHP=^6u$4>nc2C_?9R;2%)NI8Gs9vL*&4=5mXPsLNLxOVo%~>bpk+2BO{=JF z{_Hx^_{Eldlt`1JBpb3RSzB20v6LvqDzs@?=iK|=I&JN#_uTtF=RD7OzdGkLrH0Nr z)i7_&E{b^_$A+^VZA01Os^M%=$u_JS${RF!v~0eDX5&pm*^a3u)8b8EkcEsF*^WnFNTlc{f8t&~4PMW*MdQZXCOesJ zHQ8d*;20U$>}eMV;kjpyP|ANjK_Oa3ObyBr4E3jEYzVav)`x&}X{JSGBa7vLrDliG+|Mlnau0iP&iKK=^dA z!17DThgQhUb*i0F5U|ZR6{6AWK1aT4`0d;_FHy4l8}^y^&m1K1SO>z}bGU&$+p(;8At^B3F*>A8O_TFatd zxe;0kKc#Ah6LmcfvV_FxGaZMs(@hYp(ao(aI)g=9#oy93NJVtB%oM7a!YydW z=v~I8w?urhr|=z8U?(VkkNrbcC*W}2L0aN;YzD&@vwW)L=&XcbR4;5F zhqZ^XCNj1AEPpb?dl{Z&ct68uF?_(rLkew!x~#K}&(Iy!#dmlX)%`yic@Dgd!WyS} z4kCk$*fgA0iT%|w2Y31;HD59ZAB1lGy%>ddBcsiiCsKkQBcm>>gg?eou@M@~M)XI} zABNjzqk7tlJuwP_Se+(%Yq0QBI@x)}tLAmW)wlI|aE0+n|R TKA1Jo95=M{bX#b75V6F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LCGD|7MHzkCIZ@gRVjmEQ7U} zLs5iSL^ysQndjn_Z_)cK!3y?8`13c+Y_To1lzKf`rD(zM^$i(L=C%6^9Hexzmp9TP7J z3x2D_5a!4t4@Lw;++IjPL?TE)*M!XouFOAKKtr?u-6Zv(YkUXdi)sf zak0Mh>nO11+UfNJza4o$n<0Rk1Z07%>MV|KH|Cnd*}PH={w!JoU_k}q;T@2{2S2OR zD+a#QyBe@rHR$i1USRL^qA`5(r#B^2wb1c{XQEaxHcV>?NY(}bx#42)8SNHPEAhx+ zwgbTUmm?2ozQJTIB3(a3CTazbJxT1)Zea|u@~8|2{NOX;WZ4432(zCtQ7d>^3MYza z`pO61;cUJh#UB&2?YppT6D78R|4^6h|6#?IDsW4&75XzcI( zI|u+6TJ`se!ZF#4lOpqh+0S5p*beUt&(-{yaUWs}2=GNDGqzgZXbL{yCx91`EdrAH za5aCX+;6jxRN?`E{`@|6r*cN^Po;%Do_#4@uM~^*CnFzWVt7lNu6Os9ze}qfq(y&z zAN~3LRNk#6AiixSnJ2yu4LOwnk>Ay7)+Q(!7+wzKMJ61R2SEE47DYJB3DHoS125zG z=NfjWa)C7$d|>gvJRb?fU-ZQUv9Iw_V7=DGio;H>%!gN%iq-8`MjGM)49-BT&x$2f zCLY@o5T)$9hFE?0!P#ULO;mw=BX5G;-Lu&+QdMkLz^zYNPI6;W(H&45@yQ*hzGvh%)2y re59^(3$vL{@`WG>f*=TjXb=AZ>!Ac3Jx|zZ00000NkvXXu0mjfKxWs7 literal 0 HcmV?d00001 diff --git a/02-jumping/assets/player/land.png b/02-jumping/assets/player/land.png new file mode 100644 index 0000000000000000000000000000000000000000..2c24d6f4357ea83410f70d7fd04f5f77d36e323c GIT binary patch literal 1500 zcmZ`(c{J2}6rbS{W2x*>ae5=$WV|G@{V3aTh>@|yj1p0{q{&!jBIjj&mhfcI3rEw# zSfVC0<(ZPPq%vihC>}BlW-y?f66-h0knKKI`5_j_}FecV-Gx-bX?qJr{p zK|>(WHmPo*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?3Tv|`!vi<>k{PDv8 literal 0 HcmV?d00001 diff --git a/02-jumping/assets/player/run.png b/02-jumping/assets/player/run.png new file mode 100644 index 0000000000000000000000000000000000000000..615b2229f81fa53516db0910b73d045122d5c768 GIT binary patch literal 3254 zcma)9cQo8v*Z$26qem}^9+^am7J`JUh0Kksgy>ubLG%`8^dY(+l4w`7j25Cq8AMIg ztB#0Hh&Fl&Vld^M_pk5!@B7x;>zuvL+Gp+kJo`E8teyDS5YEDQjS&C zP4(sIq12Txq$+~CfdWk6+CcTFz!m^7`##dqGDW@HduQ`ZVESr51)k(xKbOJvSGshi zAIB{}Ki@TREIlkr@m0Je?^`8UEtjNTf&uaAKOM36l2W-^w?hU_t)FDj2x4+O4Fp4BEA!S+t zEjF6}j}8zFphbTpuuQDV#iadD`sJ8LXrTb+ z_yMs51Sn8GL+K5VegyFLOk7plSa#H2*t_*!Y`=pOc?p5N1eT|Ol~1_zUVC(>t55!X z+F5?9h)iA2H@6jU3KCc+792Z_^;^oD60(8wd|Z{La;>rq1W@6xlT+M0ihZ`NQgHF9*^o$f7gUWD9&fA=8P&(S3mw3CjKjZcqGMUfIrydgu~*8T9d|eN=*naTLFCO$EgZX5eDYoFkr%s zq7M*YAX8{!$X_JID(|Z`p}Hv1c@IT z+^UHM;PdG!2Wv?X*zyB>0`hhbQ-?J+>zelv+drm8JE&T0=su)JnA5q{cszk69}9kY zdo@9<2vaTCq?GBUet0rwh_F*O{a|B3RFYWuTfuVTk6~+_b+4ZsIKILd)Eoz&cc(y? zVOz22__yH*@zV4)C^8uEE;dZgMi)xhX%#Fb!CxcY}6MF zHUM?c7VKNxP|JfU>7WG)eKz%#>u^laYuicxz7kje8dh!M%$74fJ#NX$W&W-|P6KC^ zWuCSI{!NQ#7)Knh(!pg`le<}++?=~l*Hoo~SASkM#XDMZTRQ*Ln%0GT?5j>ffg#JP z76v$yA_%A8|OkzAVk_oU}^u>N@>qxChJ#Yf#E~e}Ny{BV53lyfE zeex4aNxW>ZDt4u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X`rYkzm&sts90va$6Zy0@N^@gr9l?vY`!cPS$*qSe z_1p6p-GkJ7N=-N$w;&!X;ekzwVrzwgr^=jB{+@W!{P*Z`j>6YV)9gbl&c}JSGB4Br zg7Gm($H!#8NG90e3*!Q9f2Xwdbhdm*Y*bXnP?^Y~&8U^@?aGX;yk72I?tXAuDO-5A z2vah9VP*x}eSO3cDPGGxlSY;C14Va-$`1_KtV`#6!CO;IYpz{3T`=(z9a zkKxl=*T2`F-wxbqw^k1c8VMn-S;i$|8myl@0(=9*xZmG0X0ISK!vzxZ2y}_FXRF!3 zhdDZd6QJN?tQwH7<`!%elSEIVT!ip+@kD`G>!hf{#xZ9WC00`a? zF~`KDz4G@40^5+KKTN&h8D3)a(IWqcU4Keu-4_%}Sw +#include +#include +#include "structs.h" + +const int SCREEN_WIDTH = 1000; +const int SCREEN_HEIGHT = 480; +const Uint32 WINDOW_FLAGS = SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL; +const Uint32 RENDERER_FLAGS = SDL_RENDERER_ACCELERATED; +int GAME_RUNNING = 1; +struct game game; +int movement_speed = 10; +int gravity = 8; +double delta_time; +double last_frame; + +void prepare_scene(void) { + SDL_SetRenderDrawColor(game.renderer, 96, 128, 255, 255); + SDL_RenderClear(game.renderer); +} + +void key(SDL_KeyboardEvent *event) { + if (event->repeat != 0) + return; + + if (event->keysym.scancode == SDL_SCANCODE_A) + game.left = !game.left; + if (event->keysym.scancode == SDL_SCANCODE_D) + game.right = !game.right; + if (event->keysym.scancode == SDL_SCANCODE_SPACE) + game.jump = !game.jump; +} + +void handle_input(void) { + SDL_Event event; + + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_QUIT: + GAME_RUNNING = 0; + break; + case SDL_KEYDOWN: + case SDL_KEYUP: + key(&event.key); + break; + default: + break; + } + if (event.type == SDL_QUIT) { + GAME_RUNNING = 0; + } + } +} + +SDL_Texture *load_texture(const char *path) { + SDL_Texture *texture; + + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", path); + + texture = IMG_LoadTexture(game.renderer, path); + + return texture; +} + +void switch_animation(struct object *object, SDL_Texture *animation) { + if (object->texture == animation) + return; + + object->animation_slide = 0; + object->texture = animation; +} + +struct object *create_object(SDL_Texture *texture, int x, int y, int scale, int resolution) { + struct object *object = (struct object *) calloc(1, sizeof(struct object)); + + if (object == NULL) { + fprintf(stderr, "Error: failed allocating memory for new object\n"); + return NULL; + } + + object->texture = texture; + object->resolution = resolution; + object->animation_speed = 13; + object->scale = scale; + object->x = x; + object->y = y; + + return object; +} + +void draw_object(struct object *object) { + int texture_width; + SDL_Rect src; + SDL_Rect dest; + + src.x = object->animation_slide * object->resolution; + src.y = 0; + + src.w = object->resolution; + src.h = object->resolution; + + dest.x = object->x; + dest.y = object->y; + dest.w = object->resolution * object->scale; + dest.h = object->resolution * object->scale; + + SDL_RenderCopyEx(game.renderer, object->texture, &src, &dest, 0, NULL, object->flip); + + // update animation slide + SDL_QueryTexture(object->texture, NULL, NULL, &texture_width, NULL); + + object->animation_clock += object->animation_speed*(delta_time/1000); + + if (object->animation_clock >= 1) { + object->animation_clock = 0; + object->animation_slide = (object->animation_slide+1) % (texture_width / object->resolution); // clock arithmetic: jump back to first animation slide + } +} + +void present_scene(void) { + SDL_RenderPresent(game.renderer); +} + +int main(int argc, char *argv[]) { + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + fprintf(stderr, "Error: could not initialize SDL\n%s\n", SDL_GetError()); + return -1; + } + + game.window = SDL_CreateWindow("02-jumping", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, WINDOW_FLAGS); + + if (game.window == NULL) { + fprintf(stderr, "Error: could not create window\n%s\n", SDL_GetError()); + return -1; + } + + game.renderer = SDL_CreateRenderer(game.window, -1, RENDERER_FLAGS); + + if (game.renderer == NULL) { + fprintf(stderr, "Error: could not create renderer\n%s\n", SDL_GetError()); + return -1; + } + + // load player animations + SDL_Texture *idle_animation = load_texture("assets/player/idle.png"); + SDL_Texture *run_animation = load_texture("assets/player/run.png"); + SDL_Texture *jump_animation = load_texture("assets/player/jump.png"); + + struct object *player = create_object(idle_animation, SCREEN_WIDTH/2, 0, 4, 48); + + // game loop + while (GAME_RUNNING) { + double current_frame = SDL_GetTicks(); + delta_time = current_frame - last_frame; + last_frame = current_frame; + + prepare_scene(); + handle_input(); + + // The reason why I don't do + // that much game development + // in my free time. Damn youuu ifs + if (player->colliding) { + if (game.left) + switch_animation(player, run_animation); + if (game.right) + switch_animation(player, run_animation); + if (!game.left && !game.right) + switch_animation(player, idle_animation); + if (game.jump) { + switch_animation(player, jump_animation); + player->force -= movement_speed * 3; + player->y += player->force; + player->colliding = 0; + } + } + + if (game.left) { + player->x -= movement_speed; + player->flip = SDL_FLIP_HORIZONTAL; + } + if (game.right) { + player->x += movement_speed; + player->flip = SDL_FLIP_NONE; + } + + if (player->y < 200) { + player->force += 2; + } else { + player->colliding = 1; + player->force = 0; + } + + player->y += player->force; + + draw_object(player); + + present_scene(); + + SDL_Delay(16); + } + + SDL_DestroyWindow(game.window); + SDL_Quit(); + + return 0; +} diff --git a/02-jumping/src/main.o b/02-jumping/src/main.o new file mode 100644 index 0000000000000000000000000000000000000000..de82e66558a6b7847e4c81c450cacbb64540e132 GIT binary patch literal 7704 zcmbtYeQX@X6`ykg2_eZj5J(7tEFysS zFOE@CP=iai*Qi1L5LHoI0aB%^Ql++{CMwC9G!B?_Eh|p>O8J4Mt9pb#WRdhwz#)j^z&G{+*q00$YxwR)|pzN|P|@ zl!dd^p7fWaFq*7`B87KMIzg>DAa!seC|wLX#)8%hO@V#>RFc@I5e~{WtQZ){167{_r|P(xaq07fqm7ZbwaAt`&IhUa$X$xp%8p)&FG;RdRTqNPQkAyPbG`v^q($ z2@O-nTbdNSa}ummc$q938pnbK%B0p#4^-_PU|x!U^US6A*G4YI9~)MrB}rx{En+VM zt2@47xL7l>35@ReClw5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?E3O_7u|hcHt%;!uu)zE_Q+d+0VrLr8Z>ISJKK4%DC?VCI`ws~=zE0$ zqwUTTnNl)Or&xA4dwKs{t#Op&^n8wQ0h0SP9_DX0_=r2^KOW-qet#eWKN7+@hQAkq zzYxOt-tg-Pe8Rx7_fM&AUHy;7(cdNXu>ZBRz98tY$;W;O0sm%tn7`G);S1v(h8}E9 zJPtX7kG+cdMT2k7%eM{wGJ`*E;42LL--e#s4SX>=M1lnVtVDt2R*j=yz}TOML;R(z zSMRf^HvL>-;8MTl->Y%xzthm)YT&C3yvNXkF=KxU29E4%t^da%{C| zH~qhC@b5D0-U0SOH1w=C^n7OUP5-YLIQ)N^9>`+(wXf#a^naDcF^}M%1)1btgO58fkMpJwe+*=j zZ4rDs#Q&F~i`EGKQ3Jn?xNKK__A38zmu38np&x%Z7=PEm&Gq%3ft&n)7Afc_`~=Q4L$1({CNY%-GKQP7DA%*?n?*=~K8JB?yW}-OI>u2^+>U%2P&pD5-TnSt}MWQR?XGCIOu_zK= zK@~a+y|^cf1pNha6Z8+{0eNrL<(_;g(dQNVT#qi#38~55uW!xD!!P%?s5!c-!>Xz* zsdF#sdE^Q`EL@al7h*11MO0=|2kc`AB9a^FL0esvYa94I8pqyg%0+FL*9q6bmMQmB z0XoC&*-)KHro0s@Z=^E(!hFsU?Pz_d!|XqEw^9a_u$_7=5)!^YsgmL%&uYJF + +struct game { + SDL_Window *window; + SDL_Renderer *renderer; + + int left; + int right; + int jump; +}; + +struct animation { + SDL_Texture *tilemap; + int resolution; + + int skippable; +}; + +struct object { + SDL_Texture *texture; + int resolution; // of the texture/every tile + + int x; + int y; + int scale; + Uint32 flip; + + // animation + double animation_clock; // everytime it reaches 1, the texture switches to the next slide + double animation_speed; // how fast will the clock reach 1 with respect to delta_time + int animation_slide; // the current slide of the animation + + int colliding; + int force; +}; + +#endif