From f0f6a723b35ae12d080cbdb52c75d9200e5b0048 Mon Sep 17 00:00:00 2001 From: 0xdeadbeer <64986162+0xdeadbeer@users.noreply.github.com> Date: Thu, 23 Nov 2023 19:58:37 +0100 Subject: [PATCH] 04: add boilerplate map generation code --- 04-collision/client/README.md | 3 - .../client/assets/player/idle_down.png | Bin 1028 -> 0 bytes .../client/assets/player/idle_left.png | Bin 878 -> 0 bytes .../client/assets/player/idle_right.png | Bin 921 -> 0 bytes 04-collision/client/assets/player/idle_up.png | Bin 725 -> 0 bytes .../client/assets/player/move_down.png | Bin 1555 -> 0 bytes .../client/assets/player/move_left.png | Bin 1659 -> 0 bytes .../client/assets/player/move_right.png | Bin 1651 -> 0 bytes 04-collision/client/assets/player/move_up.png | Bin 1080 -> 0 bytes 04-collision/client/assets/player/tilemap.png | Bin 6736 -> 0 bytes 04-collision/client/src/format.c | 62 ---- 04-collision/client/src/format.h | 9 - 04-collision/client/src/main.c | 278 ------------------ 04-collision/server/README.md | 3 - 04-collision/server/src/main.c | 216 -------------- 04-collision/server/src/structures.h | 9 - {04-collision => 04-map}/.imgs/showcase.png | Bin 04-map/README.md | 7 + {04-collision => 04-map}/client/Makefile | 0 04-map/client/README.md | 7 + 04-map/client/assets/floor/cobble.png | Bin 0 -> 945 bytes 04-map/client/assets/floor/grass.png | Bin 0 -> 839 bytes 04-map/client/assets/floor/lava.png | Bin 0 -> 1119 bytes 04-map/client/assets/player/idle.png | Bin 0 -> 1438 bytes 04-map/client/assets/player/jump.png | Bin 0 -> 1034 bytes 04-map/client/assets/player/land.png | Bin 0 -> 1500 bytes 04-map/client/assets/player/run.png | Bin 0 -> 3254 bytes {04-collision => 04-map}/client/src/defs.h | 14 +- 04-map/client/src/format.c | 40 +++ 04-map/client/src/format.h | 10 + 04-map/client/src/main.c | 241 +++++++++++++++ {04-collision => 04-map}/client/src/object.c | 20 +- {04-collision => 04-map}/client/src/structs.h | 4 +- {04-collision => 04-map}/server/Makefile | 0 04-map/server/README.md | 3 + {04-collision => 04-map}/server/src/defs.h | 9 +- .../server/src/hashmap.c | 10 +- 04-map/server/src/hashmap.h | 9 + 04-map/server/src/main.c | 118 ++++++++ 04-map/server/src/map.c | 29 ++ {04-collision => 04-map}/server/src/structs.h | 16 +- 41 files changed, 510 insertions(+), 607 deletions(-) delete mode 100644 04-collision/client/README.md delete mode 100644 04-collision/client/assets/player/idle_down.png delete mode 100644 04-collision/client/assets/player/idle_left.png delete mode 100644 04-collision/client/assets/player/idle_right.png delete mode 100644 04-collision/client/assets/player/idle_up.png delete mode 100644 04-collision/client/assets/player/move_down.png delete mode 100644 04-collision/client/assets/player/move_left.png delete mode 100644 04-collision/client/assets/player/move_right.png delete mode 100644 04-collision/client/assets/player/move_up.png delete mode 100644 04-collision/client/assets/player/tilemap.png delete mode 100644 04-collision/client/src/format.c delete mode 100644 04-collision/client/src/format.h delete mode 100644 04-collision/client/src/main.c delete mode 100644 04-collision/server/README.md delete mode 100644 04-collision/server/src/main.c delete mode 100644 04-collision/server/src/structures.h rename {04-collision => 04-map}/.imgs/showcase.png (100%) create mode 100644 04-map/README.md rename {04-collision => 04-map}/client/Makefile (100%) create mode 100644 04-map/client/README.md create mode 100644 04-map/client/assets/floor/cobble.png create mode 100644 04-map/client/assets/floor/grass.png create mode 100644 04-map/client/assets/floor/lava.png create mode 100644 04-map/client/assets/player/idle.png create mode 100644 04-map/client/assets/player/jump.png create mode 100644 04-map/client/assets/player/land.png create mode 100644 04-map/client/assets/player/run.png rename {04-collision => 04-map}/client/src/defs.h (73%) create mode 100644 04-map/client/src/format.c create mode 100644 04-map/client/src/format.h create mode 100644 04-map/client/src/main.c rename {04-collision => 04-map}/client/src/object.c (77%) rename {04-collision => 04-map}/client/src/structs.h (92%) rename {04-collision => 04-map}/server/Makefile (100%) create mode 100644 04-map/server/README.md rename {04-collision => 04-map}/server/src/defs.h (77%) rename 04-collision/server/src/structures.c => 04-map/server/src/hashmap.c (81%) create mode 100644 04-map/server/src/hashmap.h create mode 100644 04-map/server/src/main.c create mode 100644 04-map/server/src/map.c rename {04-collision => 04-map}/server/src/structs.h (51%) diff --git a/04-collision/client/README.md b/04-collision/client/README.md deleted file mode 100644 index 07cdb1d..0000000 --- a/04-collision/client/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Collision - Client - -Client-side code for 04-collision diff --git a/04-collision/client/assets/player/idle_down.png b/04-collision/client/assets/player/idle_down.png deleted file mode 100644 index 3241ca64c658aa46b3572d508a213818e3a432d5..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1028 zcmV+f1pE7mP)Px&zDYzuRCt{2nO|sIRUF4Zx7A8-Vz5hsGn=V1w7NevWSv~m>4Q2al^H5**t(a& z=uoNkMf&2Ohe7u;knLscVLlDk2Qd#L6IS%uIw2IsriBhe+iWGNTW6DQCF|Kkn!DaE z=_)k8tMz=~axUk0F6Vc?=Xd^I&}cLojYgx<{68_=s5BG|sA{%MHYe)SW0I9Ysqrra zR;o6jBt;NaI}{A4SDMX7mir}L5Q_vc0aohyCQrjdc~TWO=idAncpyY57!VObY7S6;{(K53 zHNLMqPG5K2QJ|bx7H6@1)&0U+AszvJ-ElfU?WeW1l_MXVwDbE zDFKXzw*4v;4Dc?{($d1#eI0xoJ;3jK-r>cei)`K3LCc1}fKrq+wCx9hWm<9>G&0#7 z@o+s%53~2!QD!D4Ieq*vGZT~SJ$4j;_rvw&vA@1HLQvWaJ-zu^&K-Q2jk|X6#Kvv5 z?e2{$-1zM>U1u*bH8C!>6BL~TGTEGw$>!(+bq;{b4tv+xOQcdMR}N^BsM&l$QJUc$ z&ysvGq_@IqHeUe1ABh2Q{rWX$0i{4#1Syn15>vDJg5A?c&RlS$OKq$}5tKg?vqfOB zk*{`t=@#AK>#o=}5!JzZ3UCLIBm<{|x}S18@AJ4;K~|pvH57D-n<{%mGke zSLa9zb9cmj5fvbO{#VKBR>i;KPS8zUy%L+5{oBpz0U-enZNA0$h12-9v^nz6U(Hl3 zKlJ(Qj3-awdp=ro-^7Q2KN4f?i^BkX_E87%-P?)p-fr6m6G?XVo~-uynj;~Av9tFC z0GoEb&GcBxHl)*O07gbeDt>tTTIKxzDi7`VdwO~lpe)O>ZG(yA@?&yM5gh`{_xjZs zOe7gh$XtxNx(rTDO#v`GJPef12_JU8C+dUPe}SdF@Aa4r3=B95Se9kKmql2Ng7vo* zu7+h<4k09PqVe_<-M%vgm@qtR$I8jVJy(P%W9HRL|u1Aed!f?q8F0000Px&C`m*?RCt{2nZZjGQ5?s=E@rhWh8VOF9)<;JI+%-f2Z0zw={Tx>(f_>hM2 zAGJCw0AOnUEdZdUTR>Bz$eWgLO{m8;fKm&uu)DE|-HlD0y4VT8nfbhF!JHXkaw9+# zMIqbJiME!AwwA~?bg~Vd%$X4aK<3N{d;SB=+fGGMXyM*1Uz<8P??r$*InVABJBp%s zt3D29-i?Bat0AD}*->xMd;3M&+b>coon;k)bt`-TCGJmS_U1XLVgkj|50uO00Nik3 z-vwdOr%a(T2y!U%;zNG3FN#Nuqe1R}uN$~*Lq%~hTKP{?tqBNk^#19kyA zVsS*w_rA7dLc@cHj}Sh41_1EMSY-1jkO*jRYXbm$nYs(WNr4y79wU{`virg=z}Jr} z+$N7DkO-(H0u|u=lb1)@xL=8ZM?fZ*$M8TuO#L~o+9p0{98tJZC2#>;R55{~wT8A> z4|dHZHoG+;!1C-UHb1-x#CG5U3=i~s_tCxq0Klbdw}CnZhN`AOt@tL9NF)-8L?V$$B$B%07f>)$KFH*)(f|Me07*qoM6N<$ Eg43Lj&j0`b diff --git a/04-collision/client/assets/player/idle_right.png b/04-collision/client/assets/player/idle_right.png deleted file mode 100644 index 66a019baf47b4245c047cf03afff0377fd6bc342..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 921 zcmV;K17`e*P)Px&Q%OWYRCt{2nLTI|VHn5%J*qTG3Dy>kNDFD85EmQlA{FF7GuSRI(ksrH{GdZC zP9jAVBwa);4qb&D2yti}s**ul3M$nsXOJ4DEd;A*q)9c%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mvxQ}mq3C_(5h{bFR|palKm}{&`H+!KO^GsRR10mpmuPC#1qE`zk0|O; z(AN1S|Mdp7sUJe!DAA*2HUw#?x(geK%Fs<*k2BC}tEd>RAx6(gXqv%+CwCxG!9oHF z8o5dyS*^GLBi{C^&(xY^C{VYF7En{;-PqW0R*&ckr5L+v4^L<@Y%hE-xnOgufBQH# zIH?CpWIo30;sqQCfh`NP7X|8}>xTuNM{Sz&8ng2~v(=tgBXFHK?7Y0+>U}_SHIeT$+WI8`s#>1u#J;*>X?r~O(`^8{uAFS7EL6N}BI za60)dF`)4#58So!Kr|V7bmSkb%2)BYfg%2FN7E8tUR0ZSOM{8Rj9vZE3spJguz-HW zym94Dp{j=m@EefHUB6xWh4-gs=h`nt4(GmOxwDCrpwdFV`J-DEVion2cx_*9Gi)@jOiczD50_e_>ar+7z8D(zhK((A#zf%=zS zx#9aCQejCZq?>Fc-WalHfP zE~dkkUVdLz5Al+O#*C-|iPxwG&b9_(`peAA0pRTH?8dTJ!f>bB5@eLdi<=jpefFY_ zD2sBXwVBGjwXn_{x6z$ksj{%1;iI8&sfHp=h2JYL`@SW>D#YKf4!qSj&mW9F>h$rp zI4^Yy!-@fhfszzVIK=jjsGSUa1x)EJ624VZW!^$=-VBGyxq6!jUTA?g=)@qj9=y`S zD2+H)aF@~QmZ{g;I6PrIDi;Q?wRfgF;OkgI3j%*8#Nv0GEiP4~PyLF}8|mmT)s1IjiP#&8N{1A!ILkJU1||0DVhc zjAMS6qFlSOFzeG%WD~-h|2~$?{h3-iclHXH?j(RY%w}FukE-DAQ|%#Aze)~zJE76z zsy)73rM&@UXfd-=B?{8&Y0J97y&Byl zJH3}~%}u|SPuyhA6>0h-Bj-6!7dQIca0z-szt}ygocsItGGSc5pVaI} gvj2Z8B*dwm^^)yQNUe!B<-Y=8sAsHOr44)aKMh*|cK`qY diff --git a/04-collision/client/src/format.c b/04-collision/client/src/format.c deleted file mode 100644 index afbe628..0000000 --- a/04-collision/client/src/format.c +++ /dev/null @@ -1,62 +0,0 @@ -#include -#include "defs.h" -#include "structs.h" -#include "format.h" - -extern SDL_Texture *idle_down_animation; -extern struct object *create_object(SDL_Texture *texture, int scale, int resolution); - -int handle_player_connect(int *message, struct object ***map, int *slots) { - int object_id = message[1]; - - if (object_id >= *slots) { - *slots = object_id+1; - *map = realloc(*map, sizeof(struct object *)*(*slots)); - } - - if (*map == NULL) - return MEMERR; - - struct object *new_object = create_object(idle_down_animation, 4, 64); - if (new_object == NULL) - return MEMERR; - - new_object->id = object_id; - new_object->x = message[2]; - new_object->y = message[3]; - new_object->colliding = message[4]; - new_object->force = message[5]; - new_object->state = message[6]; - (*map)[object_id] = new_object; - - return STDOK; -} - -int handle_object_properties(int *message, struct object ***map, int *slots) { - int object_id = message[1]; - - if (object_id >= *slots) { - *slots = object_id+1; - *map = realloc(*map, sizeof(struct object *)*(*slots)); - } - - if (*map == NULL) - return MEMERR; - - if ((*map)[object_id] == NULL) { - struct object *new_object = create_object(idle_down_animation, 4, 64); - if (new_object == NULL) - return MEMERR; - - new_object->id = object_id; - (*map)[object_id] = new_object; - } - - (*map)[object_id]->x = message[2]; - (*map)[object_id]->y = message[3]; - (*map)[object_id]->colliding = message[4]; - (*map)[object_id]->force = message[5]; - (*map)[object_id]->state = message[6]; - - return STDOK; -} diff --git a/04-collision/client/src/format.h b/04-collision/client/src/format.h deleted file mode 100644 index f84065a..0000000 --- a/04-collision/client/src/format.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef FORMAT_H -#define FORMAT_H - -#include - -int handle_player_connect(int *message, struct object ***map, int *slots); -int handle_object_properties(int *message, struct object ***map, int *slots); - -#endif diff --git a/04-collision/client/src/main.c b/04-collision/client/src/main.c deleted file mode 100644 index 9914a22..0000000 --- a/04-collision/client/src/main.c +++ /dev/null @@ -1,278 +0,0 @@ -#include -#include -#include -#include -#include "structs.h" -#include "defs.h" -#include "format.h" - -#define DEBUG 1 - -int game_running = 1; -struct game game; -int movement_speed = 10; -double delta_time; -double last_frame; - -// network -TCPsocket server_socket; - -// textures -SDL_Texture *idle_animation; -SDL_Texture *move_left_animation; -SDL_Texture *move_right_animation; -SDL_Texture *move_up_animation; -SDL_Texture *move_down_animation; -SDL_Texture *idle_left_animation; -SDL_Texture *idle_right_animation; -SDL_Texture *idle_up_animation; -SDL_Texture *idle_down_animation; - -// scene -int objects_count = 0; -struct object ** objects_map; - -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_D) - game.left = !game.left; - if (event->keysym.scancode == SDL_SCANCODE_F) - game.right = !game.right; - if (event->keysym.scancode == SDL_SCANCODE_K) - game.up = !game.up; - if (event->keysym.scancode == SDL_SCANCODE_J) - game.down = !game.down; -} - -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 present_scene(void) { - SDL_RenderPresent(game.renderer); -} - -int new_key_position(void **map, int slots) { - int slot = 0; - - if (slots <= 0) - return slot; - - if (map == NULL) - return slot; - - while (slot <= slots) { - if (map[slots] == NULL) - break; - - slot++; - } - - return slot; -} - -int handle_server(void *data) { - for (;;) { - int message[CLIENT_MESSAGE_LEN]; - int recv_len = SDLNet_TCP_Recv(server_socket, message, sizeof(int)*CLIENT_MESSAGE_LEN); - - if (!recv_len) { - fprintf(stderr, "Error: failed receiving message from server\n%s\n", SDLNet_GetError()); - break; - } - - if (DEBUG) - fprintf(stdout, "Notice: received '%d' bytes from server\n", recv_len); - - if (message[0] == PLAYER_CONNECT_FORMAT) { - if (DEBUG) - fprintf(stdout, "DEBUG: PLAYER CONNECT MESSAGE\n"); - - int ret = handle_player_connect(message, &objects_map, &objects_count); - if (ret == MEMERR) { - fprintf(stderr, "MEMERR: Failed handling memory for player connect\n"); - return MEMERR; - } - - continue; - } - - if (message[0] == OBJECT_PROPERTIES_FORMAT) { - if (DEBUG) - fprintf(stdout, "DEBUG: OBJECT PROPERTIES MESSAGE FOR ID %d\n", message[1]); - - int ret = handle_object_properties(message, &objects_map, &objects_count); - if (ret == MEMERR) { - fprintf(stderr, "MEMERR: Failed handling memory for new object properties\n"); - return MEMERR; - } - - continue; - } - - if (message[0] == PLAYER_DISCONNECT_FORMAT) { - if (DEBUG) - fprintf(stdout, "DEBUG: PLAYER DISCONNECT MESSAGE\n"); - - int object_id = message[1]; - - free(objects_map[object_id]); - objects_map[object_id] = NULL; - - continue; - } - } - - return 0; -} - -int connect_to_server(void) { - IPaddress ip; - if (SDLNet_ResolveHost(&ip, SERVER_ADDR, SERVER_PORT) != 0) { - fprintf(stderr, "Error: resolving host of server\n%s\n", SDLNet_GetError()); - return -1; - } - - server_socket = SDLNet_TCP_Open(&ip); - if (!server_socket) { - fprintf(stderr, "Error: failed opening socket to '%s' at '%d'\n%s\n", SERVER_ADDR, SERVER_PORT, SDLNet_GetError()); - return -1; - } - - SDL_CreateThread(handle_server, "server", NULL); - - return 0; -} - -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; - } - - if (SDLNet_Init() != 0) { - fprintf(stderr, "Error: could not initialize SDL net\n%s\n", SDL_GetError()); - return -1; - } - - game.window = SDL_CreateWindow("03-network", 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 - move_left_animation = load_texture("assets/player/move_left.png"); - move_right_animation = load_texture("assets/player/move_right.png"); - move_up_animation = load_texture("assets/player/move_up.png"); - move_down_animation = load_texture("assets/player/move_down.png"); - idle_left_animation = load_texture("assets/player/idle_left.png"); - idle_right_animation = load_texture("assets/player/idle_right.png"); - idle_up_animation = load_texture("assets/player/idle_up.png"); - idle_down_animation = load_texture("assets/player/idle_down.png"); - idle_animation = idle_down_animation; - - if (connect_to_server() != 0) - return -1; - - // game loop - while (game_running) { - double current_frame = SDL_GetTicks(); - delta_time = current_frame - last_frame; - last_frame = current_frame; - - prepare_scene(); - handle_input(); - - uint8_t message[SERVER_MESSAGE_LEN]; - message[0] = (game.left << 7) | - (game.right << 6) | - (game.up << 5) | - (game.down << 4); - - SDLNet_TCP_Send(server_socket, message, sizeof(uint8_t)*SERVER_MESSAGE_LEN); - - for (int iter = 0; iter < objects_count; iter++) { - struct object *obj = objects_map[iter]; - if (obj == NULL) - continue; - - if (obj->state & LEFT_MOVEMENT) { - switch_animation(obj, move_left_animation); - idle_animation = idle_left_animation; - } - - if (obj->state & RIGHT_MOVEMENT) { - switch_animation(obj, move_right_animation); - idle_animation = idle_right_animation; - } - - if (obj->state & UP_MOVEMENT) { - switch_animation(obj, move_up_animation); - idle_animation = idle_up_animation; - } - - if (obj->state & DOWN_MOVEMENT) { - switch_animation(obj, move_down_animation); - idle_animation = idle_down_animation; - } - - if (obj->state == NO_MOVEMENT) - switch_animation(obj, idle_animation); - - obj->animation_speed = 6; - - draw_object(&game, obj); - } - - present_scene(); - - SDL_Delay(16); - } - - SDL_DestroyWindow(game.window); - SDL_Quit(); - - return 0; -} diff --git a/04-collision/server/README.md b/04-collision/server/README.md deleted file mode 100644 index c3ea17a..0000000 --- a/04-collision/server/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Collision - Server - -Server-side code for 04-collision diff --git a/04-collision/server/src/main.c b/04-collision/server/src/main.c deleted file mode 100644 index 531f7f8..0000000 --- a/04-collision/server/src/main.c +++ /dev/null @@ -1,216 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "defs.h" -#include "structs.h" -#include "structures.h" - -#define DEBUG 0x0010 -#define PORT 9080 - -struct object **objects_map; -int objects_map_size = 0; -int objects_count = 0; - -struct connection **connections_map; -int connections_map_size = 0; -int connections_count = 0; - -void handle_player_input(struct object *obj, char *message) { - uint8_t action = message[0]; - - if (action & LEFT_MOVEMENT) - obj->x -= MOVEMENT_SPEED; - - if (action & RIGHT_MOVEMENT) - obj->x += MOVEMENT_SPEED; - - if (action & UP_MOVEMENT) - obj->y -= MOVEMENT_SPEED; - - if (action & DOWN_MOVEMENT) - obj->y += MOVEMENT_SPEED; - - obj->state = action; -} - -void handle_player_physics(struct object *obj) { -} - -int broadcast_event(int format, int object_id) { - struct object *obj = objects_map[object_id]; - - for (int iter = 0; iter < connections_map_size; iter++) { - struct connection *con = connections_map[iter]; - if (con == NULL) - continue; - - int message[] = { - format, - obj->id, - obj->x, - obj->y, - obj->colliding, - obj->force, - obj->state - }; - - SDLNet_TCP_Send(con->socket, message, sizeof(int)*CLIENT_MESSAGE_LEN); - } - - return 0; -} - -int handle_player(void *data) { - struct connection *connection_data = (struct connection *) data; - struct object *obj = objects_map[connection_data->obj_id]; - - SDLNet_SocketSet set = SDLNet_AllocSocketSet(1); - if (set == NULL) { - fprintf(stderr, "Error: cannot allocate memory for a new socket set\n"); - return -1; - } - - int ret = SDLNet_TCP_AddSocket(set, connection_data->socket); - if (ret == -1) { - fprintf(stderr, "Error: max socket count in socket set reached\n"); - return -1; - } - - broadcast_event(PLAYER_CONNECT_FORMAT, connection_data->obj_id); - - for (;;) { - char message[SERVER_MESSAGE_LEN]; - int ready_sockets = SDLNet_CheckSockets(set, 100); - if (ready_sockets == -1) { - fprintf(stderr, "Error: cannot call select() system call with SDLNet_CheckSockets()\n"); - continue; - } - - if (ready_sockets == 0) - goto update_client; - - int recv_len = SDLNet_TCP_Recv(connection_data->socket, message, SERVER_MESSAGE_LEN); - if (!recv_len) { - // player disconnected - fprintf(stderr, "Error: failed receiving message\n%s\n", SDLNet_GetError()); - break; - } - - if (DEBUG & GENERAL_DEBUG) - fprintf(stdout, "Notice: received '%s' and '%d' bytes\n", message, recv_len); - - handle_player_input(obj, message); - - update_client: - handle_player_physics(obj); - broadcast_event(OBJECT_PROPERTIES_FORMAT, connection_data->obj_id); - } - - // Communicate client disconnect - broadcast_event(PLAYER_DISCONNECT_FORMAT, connection_data->obj_id); - - SDLNet_FreeSocketSet(set); - SDLNet_TCP_Close(connection_data->socket); - - objects_map[connection_data->obj_id] = NULL; - objects_count--; - - connections_map[connection_data->id] = NULL; - connections_count--; - - free(obj); - free(data); - - return 0; -} - -void catch_alarm(int sig) { - fprintf(stdout, "Notice: force stopping server...\n"); - exit(EXIT_SUCCESS); -} - -int new_key_position(void **map, int slots) { - int slot = 0; - - if (slots <= 0) - return slot; - - if (map == NULL) - return slot; - - while (slot <= slots) { - if (map[slot] == NULL) - break; - - slot++; - } - - return slot; -} - -int main(int argc, char *argv[]) { - signal(SIGINT, catch_alarm); - - if (SDL_Init(SDL_INIT_VIDEO) != 0) { - fprintf(stderr, "Error: could not initialize SDL\n%s\n", SDL_GetError()); - return -1; - } - - if (SDLNet_Init() != 0) { - fprintf(stderr, "Error: could not initialize SDL net\n%s\n", SDL_GetError()); - return -1; - } - - IPaddress ip; - if (SDLNet_ResolveHost(&ip, NULL, PORT) == -1) { - fprintf(stderr, "Error: failed resolving host \n%s\n", SDL_GetError()); - return -1; - } - - TCPsocket server = SDLNet_TCP_Open(&ip); - if (!server) { - fprintf(stderr, "Error: failed opening socket at %d\n%s\n", PORT, SDL_GetError()); - return -1; - } - - for (;;) { - TCPsocket client = SDLNet_TCP_Accept(server); - if (!client) { - SDL_Delay(100); - continue; - } - - fprintf(stdout, "Notice: accepted a connection from client!\n"); - - int new_object_slot = map_allocate_value((void ***) &objects_map, &objects_map_size, &objects_count, sizeof(struct object)); - if (new_object_slot == MEMERR) { - fprintf(stderr, "MEMERR: failed allocating memory for new object\n"); - return STDERR; - } - - int new_connection_slot = map_allocate_value((void ***) &connections_map, &connections_map_size, &connections_count, sizeof(struct connection)); - if (new_connection_slot == MEMERR) { - fprintf(stderr, "MEMERR: failed allocating memory for new connection\n"); - return STDERR; - } - - struct object *new_object = objects_map[new_object_slot]; - struct connection *new_connection = connections_map[new_connection_slot]; - - new_object->id = new_object_slot; - new_connection->id = new_connection_slot; - new_connection->socket = client; - new_connection->obj_id = new_object_slot; - - if (DEBUG & PLAYER_DEBUG) - fprintf(stdout, "Created connection with id '%d' and object '%d'\n", new_connection->id, new_object->id); - - SDL_CreateThread(handle_player, "client", new_connection); - } - - return 0; -} diff --git a/04-collision/server/src/structures.h b/04-collision/server/src/structures.h deleted file mode 100644 index d216630..0000000 --- a/04-collision/server/src/structures.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef STRUCTURES_H -#define STRUCTURES_H - -#include "structs.h" - -int map_find_empty_slot(void **map, int slots); -int map_allocate_value(void ***map, int *total_slots, int *used_slots, int bytes); - -#endif diff --git a/04-collision/.imgs/showcase.png b/04-map/.imgs/showcase.png similarity index 100% rename from 04-collision/.imgs/showcase.png rename to 04-map/.imgs/showcase.png diff --git a/04-map/README.md b/04-map/README.md new file mode 100644 index 0000000..fae3826 --- /dev/null +++ b/04-map/README.md @@ -0,0 +1,7 @@ + + + + +# Map + +My first attempt to recreate map generation in SDL. Both the server and the client run off of the SDL2 library. (alongside some extensions) diff --git a/04-collision/client/Makefile b/04-map/client/Makefile similarity index 100% rename from 04-collision/client/Makefile rename to 04-map/client/Makefile diff --git a/04-map/client/README.md b/04-map/client/README.md new file mode 100644 index 0000000..c78419b --- /dev/null +++ b/04-map/client/README.md @@ -0,0 +1,7 @@ + + + + +# Map - Client + +Client-side code for 04-map diff --git a/04-map/client/assets/floor/cobble.png b/04-map/client/assets/floor/cobble.png new file mode 100644 index 0000000000000000000000000000000000000000..3fc1dbc594abebb3e75ee6e0dde2b8aeeae98cc3 GIT binary patch literal 945 zcmV;i15W&jP)< z7$c=rYh{WhYb`T7j$@2*-#4!8^JI5lYn4(kEv59{Q%a>2Gn;doS*>->$<*^a%Ypr|l=Xuszd+*olwbpX?Qi>e07z0M;oB*10rj+*H08O7q zzZnQ-_IkaDgE<&rN^rimfeJn%aF~2=z>s3Z8gz1zQi3aC-+R|ukK-tx2YNxkHymVl zXC*`v9pvCtp}-Z`@s0sYu!l6#hLay*L;$FTx1@zgBE|fIS7MXFaU6)v%rd~Cm_4;+ zDFq;r;=MQP(U?A3Yo!!m5%)U>qK^;hLE3fqIVWLhZ%D%curg?^0SIqd5QLWv082np zYVRGcMT>L=t0aR=porXBlS1&Q;uJu@QZb;Bzz9f66EXzWd(UtO1Zcra#3im6XGG;9 z`AZZ`wbt(Y&PoVI#Kl|1qAg(zgS~faZLLM`f(DpWLAZOyh-qyTC3syIe%+mZ_djYa zC*kg{!ukgCXejm&_e_;T-EbjP+VrQ!vyK@&?-a>7^=mm0HV8%I?^AM^q_fh$4GYH+H|sB56XlFnfg7*!`N5g+LV-;dgh3f;`^ z`;K=RAyVMVr>-zO{Kmoek;z%116i3=G8xzTu3iiFcpi)~;#r$@6vsnV2Qmx9{&5^; z)>;FBPGjkkrV#G{|8!F&mJ5=uElS2$9Vk_5pX&DH$=&mpTJQUg$6V%^C47}W)6_Oz=A6CvxNEFi-K-=H zEPL;nMlF8S7@|hS%oodXiLM&j_`0q+X9g)KXz{v0vVjgrK3`9>m1rP{=5D0-#2!&8cAA~{(gg`KaFz9`VH(o2g^-RZ!EK9rkYi>+@0hoZj z?%4BQ-~ac_;0{Um$NK|-8HoBTRUzvBF&V%!>238n+t6?SVgCaUroq8l-@^=SX5^FD zOm@$0Y3&9`@7u9I&d^BcN&+O=T%C*2$8-x<=dz)*EDyL5d*0 za<1V>my^Qus=WiiLd%y{=@ri~Z^_8W!DIlvxVHoX6=87XFEfbbOb)fXbJxvFAcCIg zqzDK#rr@%fYlJX;=_oalyF-C+IVwUagc;6t;7qdfFr!=2^IiJ71KgNu1V)3$!MH3B zBmwurET-%E54u9WpUh+6=*2a+k@P2XCMSg!!P(?bvr$3bPX^9J&wpkt1DTV|hmF^e zGtbch?kD0GeSSA%Az-uZ#ojZ4Vt38islPX1M-4o=j?vtX-q)CpS2z5wqo5Ug1$jRm z_{A4oT2qXjtrxEtE_QdJ?<}p-?%&TMng$A)Md4a#>VL`B`Wz;RfEPy_ilz5H^nE=gdC(W+lm1gp zLu(y??B+luN4|@(zq@ledef&eXc1ki%$$R2z!AEmDapjtS*u=3(ow45h^{Y7!4ZMg z4#S=cmSJ#W+)UNv5^1(002ovPDHLk RV1fVu002ovPDHLkV1ny;g>?V` literal 0 HcmV?d00001 diff --git a/04-map/client/assets/floor/lava.png b/04-map/client/assets/floor/lava.png new file mode 100644 index 0000000000000000000000000000000000000000..38b8b28372c48a39b8328d0916ad1d0c17ccb3d6 GIT binary patch literal 1119 zcmV-l1fctgP)42GX%3Lp?%1_^?=48ou}24%2b2FIW&gJWs`s4%gl})Tb5pZ z+q(qM1n2{pfZ661|23m&z`;I%9QLLRbM5nrkMVpC=YB}22wIpEhygxqj36Sd4zxP* z)BeHc5!hZr{@UWhHdc-7a5V;xuqW`EPX+!Jr*B3t8Uc==_gO;h;KQcHh-TZ*w)Y4E z1)z#Y9krI5lJU!o=C8o+3D3Lzx!HkZU@o&?f>zfsR@C(M^7`{V4db z(J+O*g%NB6b_t$`a4@D|G(Wii{ur~jz(`7n?nbx5yU|BJQlbKT`8=CInJ3x0qCV4o zS;Z)O=43{5{{}t{?YVTYilG~;XS)S5JSF^MOoSfXmk&I+1O&Rf=JIN0@oV(J)Eqpe z0roP-l6@1#Y*6}%!7a?^(%p6m!W-S}6}RLEZlQ$Nl)b9csg9=1xX&MW^h=`_zL}ne zccX6xQuGN}Y6n^a-y{IlG&q;e=x=tD3RDYo2@&&5um9)bmWdS=;BFeJvWm}MzOMAt zn`{4$-v|+)Ls)Bgl#5R@0AFGKY#=G_jzkDT=o|02kE7N?uGEWKWO`wm>)3q6cgp?G zTwa569|gobZH(=9rlbD~f^WEA=jvc%>I%4o>+EFn-OcU;=M2B915g%7o6pQzmX&%M z1+wBj|5^y~#E=Djpg)5>(eU+}8e7~6CTYvQHx6EEYtBRsz=m-U+i z3ciHrcr$v@W_m11;I@!ye=*JWM`Lg_;Zy0_J^~n(=Vqp>VXpN9quG^R0&+1$2*T$I zKXgD^<#aoY+hJqr@>JNNaIseR$~&QKX+p<PPbOTniO`IMSe35?gbfeKKlF>sTpd_r)I2c;8h9{9MUMh-4R6E7Yb~D&>!anT zF<2^Vib-3Y(~%p{vOmMr ztEVz{L}{nFv^*JO36jvn_&UCq-tg}DV`qICMYLyF?erFy#O5M;k;)3X7phxst)VmG l@gK9;-Zj3)guws+002ovPDHLkV1fVu002ovPDHLkV1oD*DJuW~ literal 0 HcmV?d00001 diff --git a/04-map/client/assets/player/idle.png b/04-map/client/assets/player/idle.png new file mode 100644 index 0000000000000000000000000000000000000000..4cf0c430a8cdd6b2418cdb6c35eb528bf38bc103 GIT binary patch literal 1438 zcmZ`(eK?Z~6n~fR$}K7LSefyMqO1=^O@_?3iKmA7EMhnAaFLYjmFTWibxW#4gw0sial6{GMohW0g#y_ax$RPQqBq^C++qK&QfaxVk>aaDD-71{#s$7ZntMM?z}#YF$cJ_4(ME=5W)oA}K7 zKcRapdwadyCJKkC(iu_{CJI>>JjCs7Vx{OzkU& z>I7{?8rW`yRIqeHYFI+baL;FniXFfLnu551J;P4((<+IFVKEGVNrE?K$x}vRjS8N4 zW~ZZr8qRW%)G;M8NM|twDU}N?VZO<46Ykh>5<;gVy=Ar;!&^Z)kmJ;QdBGt#4wqyH z_UxAQ5Oo3-l>w$|cD{Z(6kVbo=~Sc!I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aKHl+8zHKVzWGp)VWSBDvJ-9nWcDR_kj1y73x#o9vS?jEPbL!E zC-Q11@w)sm?2~FutFt^j`I~dUCCN|s0~Wam-|BB-J)6m&)7Gyub1_jq$N^8Ks^f_B zG4ayxW2?#p^YbLa#vXQhYR)a;gt4hu90y1e1cL8TX?D4*=ow-8u{LQVISfmC*~Aeh zO600g?G$ut5L*)=9n`mCx^2CNfeHH%VT!kOp^^al51BJYu&<_ zd3BNjmEo}7p3TiqK}7Q|Z70)kXDr_zJ7Or!$XoQZ~kYxLu?(@_xT#dDwzVc3n=W^8ylyaMDlb zU2c$pm(XK1UK42TKTk5v8%yXN5+8>59J`N}m7O{n=;UA38v!*Poeg&GiPPr=CIoHn zl+f*^uer=Ylf|#^MZbS0gu+wg$J-oycQRlC$^4?!q4%iVEn!j#o!l`}DLh~lk4t5A urjx%H1FaTxt0ohAmlSPm_kZleXTci)B8?P-SwXx1Fr!?3Tv|`!vi<>k{PDv8 literal 0 HcmV?d00001 diff --git a/04-map/client/assets/player/run.png b/04-map/client/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`NQgHF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$Rn0erKWM@Qsb9P3X6xaYOJmwT#sssEo99csmuY{c3wo6 zu|y|?%vQ&+GRSpl_+^5T2IcPT*{VA!B{0k+Otm|5>aa(6^s}9ZQfc!t`_z84KmS6V zGilLzn%Tj{ov&SWLqIjhTmhn$0+tLkZ&4S-mRc*=+$o>>FJpaT2O2VwJn45!tVWnc zFk=qhMbievp{sw#&eC}*-E0u}tscUVC36s1&wyTx2q>1F!yoq7lSo==V1Xsk&t(yl zXIXbzva35=cSG{8Cm#SwHp8x^alwME8UF%Z?(K<~6+pxYZkv18j2N$AU zZA__X?$D707k^1yY&hL0iuMlKs>*Mn9#dryR=O%+7e^%uwb2Feh0RG~;BBXR2p9Jm z(cj)Hvgg)MeJKVog}xt8KdBqYOaDc*&&$7}xLU(uG~Y|^dhvzY6OaU&mXF3(w*wPC zYuP-$8Z=ViK2Ci-UG{bc%@CY z-dS^D0J5!T;IwXb)Nt983^P|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 "defs.h" +#include "structs.h" +#include "format.h" + +extern SDL_Texture *grass_texture; +extern SDL_Texture *cobble_texture; +extern SDL_Texture *lava_texture; +extern struct object *create_object(SDL_Texture *texture, int scale, int resolution); + +int handle_block_creation(int id, int type, struct object ***map, int *slots) { + (*slots)++; + *map = realloc(*map, sizeof(struct object *)*(*slots)); + if (*map == NULL) + return MEMERR; + + SDL_Texture *texture; + switch (type) { + case 0: + texture = grass_texture; + break; + case 1: + texture = cobble_texture; + break; + case 2: + texture = lava_texture; + break; + default: + texture = grass_texture; + break; + } + + struct object *new_block = create_object(texture, 1, 32); + if (new_block == NULL) + return MEMERR; + + (*map)[id] = new_block; + + return STDOK; +} diff --git a/04-map/client/src/format.h b/04-map/client/src/format.h new file mode 100644 index 0000000..3b9203b --- /dev/null +++ b/04-map/client/src/format.h @@ -0,0 +1,10 @@ +#ifndef FORMAT_H +#define FORMAT_H + +#include + +int handle_block_creation(int id, int type, struct object ***map, int *slots); +// int handle_player_connect(int *message, struct object ***map, int *slots); +// int handle_object_properties(int *message, struct object ***map, int *slots); + +#endif diff --git a/04-map/client/src/main.c b/04-map/client/src/main.c new file mode 100644 index 0000000..0aeeedd --- /dev/null +++ b/04-map/client/src/main.c @@ -0,0 +1,241 @@ +#include +#include +#include +#include +#include +#include +#include "structs.h" +#include "format.h" +#include "defs.h" + +#define DEBUG 1 +#define MAP_WIDTH 100 +#define MAP_HEIGHT 100 + +int original_width = 1000; +int original_height = 480; +int screen_width = 1000; +int screen_height = 480; +int game_running = 1; +struct game game; +int movement_speed = 10; +double delta_time; +double last_frame; + +// network +TCPsocket server_socket; + +// Textures +SDL_Texture *grass_texture; +SDL_Texture *cobble_texture; +SDL_Texture *lava_texture; + +// scene +int objects_count = 0; +struct object ** objects_map; +SDL_mutex *objects_map_mutex; + +void prepare_scene(void) { + SDL_SetRenderDrawColor(game.renderer, 0, 0, 0, 0); + SDL_RenderClear(game.renderer); +} + +void key(SDL_KeyboardEvent *event) { + if (event->repeat != 0) + return; + + if (event->keysym.scancode == SDL_SCANCODE_J) + game.left = !game.left; + if (event->keysym.scancode == SDL_SCANCODE_K) + 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)) { + 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 present_scene(void) { + SDL_RenderPresent(game.renderer); +} + +int new_key_position(void **map, int slots) { + int slot = 0; + + if (slots <= 0) + return slot; + + if (map == NULL) + return slot; + + while (slot <= slots) { + if (map[slots] == NULL) + break; + + slot++; + } + + return slot; +} + +int handle_server(void *data) { + int message[MAP_WIDTH*MAP_HEIGHT]; + int recv_len = SDLNet_TCP_Recv(server_socket, message, sizeof(int)*MAP_WIDTH*MAP_HEIGHT); + + if (!recv_len) { + fprintf(stderr, "Error: failed receiving message from server\n%s\n", SDLNet_GetError()); + return MEMERR; + } + + if (DEBUG) + fprintf(stdout, "Notice: received '%d' bytes from server\n", recv_len); + + SDL_LockMutex(objects_map_mutex); + + int blocks_num = recv_len / sizeof(int); + + for (int y = 0; y < MAP_HEIGHT; y++) { + for (int x = 0; x < MAP_WIDTH; x++) { + + int iter = (y*MAP_WIDTH)+x; + int ret = handle_block_creation(iter, message[iter], &objects_map, &objects_count); + if (ret == MEMERR) { + fprintf(stderr, "Error: failed creating new block\n"); + return MEMERR; + } + + struct object *block = objects_map[iter]; + block->x = x * block->resolution * block->scale; + block->y = y * block->resolution * block->scale; + } + } + + SDL_UnlockMutex(objects_map_mutex); + + return 0; +} + +int connect_to_server(void) { + IPaddress ip; + if (SDLNet_ResolveHost(&ip, SERVER_ADDR, SERVER_PORT) != 0) { + fprintf(stderr, "Error: resolving host of server\n%s\n", SDLNet_GetError()); + return -1; + } + + server_socket = SDLNet_TCP_Open(&ip); + if (!server_socket) { + fprintf(stderr, "Error: failed opening socket to '%s' at '%d'\n%s\n", SERVER_ADDR, SERVER_PORT, SDLNet_GetError()); + return -1; + } + + SDL_CreateThread(handle_server, "server", NULL); + + return 0; +} + +void catch_alarm(int sig) { + exit(EXIT_FAILURE); +} + +void handle_events() { + SDL_Event event; + + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_WINDOWEVENT: + if (event.window.event == SDL_WINDOWEVENT_RESIZED) + SDL_GetWindowSize(game.window, &screen_width, &screen_height); + break; + case SDL_QUIT: + exit(EXIT_SUCCESS); + break; + } + } +} + + +int main(int argc, char *argv[]) { + signal(SIGINT, catch_alarm); + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + fprintf(stderr, "Error: could not initialize SDL\n%s\n", SDL_GetError()); + return -1; + } + + if (SDLNet_Init() != 0) { + fprintf(stderr, "Error: could not initialize SDL net\n%s\n", SDL_GetError()); + return -1; + } + + game.window = SDL_CreateWindow("03-network", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_width, screen_height, WINDOW_FLAGS); + SDL_SetWindowResizable(game.window, 1); + + 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 textures + grass_texture = load_texture("assets/floor/grass.png"); + cobble_texture = load_texture("assets/floor/cobble.png"); + lava_texture = load_texture("assets/floor/lava.png"); + + // create mutexes + objects_map_mutex = SDL_CreateMutex(); + + if (connect_to_server() != 0) + return -1; + + while (game_running) { + prepare_scene(); + + handle_events(); + + int lock = SDL_TryLockMutex(objects_map_mutex); + if (lock == 0) { + for (int iter = 0; iter < objects_count; iter++) { + struct object *object = objects_map[iter]; + if (object == NULL) + continue; + + object->x = object->x - (object->scale * object->resolution * (original_width - original_width)); + object->y = object->y - (object->scale * object->resolution * (original_height - original_height)); + + draw_object(&game, object); + } + + SDL_UnlockMutex(objects_map_mutex); + } + + present_scene(); + + SDL_Delay(100); + } + + SDL_DestroyWindow(game.window); + SDL_Quit(); + + return 0; +} diff --git a/04-collision/client/src/object.c b/04-map/client/src/object.c similarity index 77% rename from 04-collision/client/src/object.c rename to 04-map/client/src/object.c index 6be16b5..7019603 100644 --- a/04-collision/client/src/object.c +++ b/04-map/client/src/object.c @@ -16,7 +16,7 @@ struct object *create_object(SDL_Texture *texture, int scale, int resolution) { return object; } -void draw_object(struct game *game, struct object *object) { +void frame_draw_object(struct game *game, struct object *object) { int texture_width; SDL_Rect src; SDL_Rect dest; @@ -45,6 +45,24 @@ void draw_object(struct game *game, struct object *object) { } } +void draw_object(struct game *game, struct object *object) { + SDL_Rect src; + SDL_Rect dest; + + src.x = 0; + 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, SDL_FLIP_NONE); +} + + void switch_animation(struct object *object, SDL_Texture *animation) { if (object->texture == animation) return; diff --git a/04-collision/client/src/structs.h b/04-map/client/src/structs.h similarity index 92% rename from 04-collision/client/src/structs.h rename to 04-map/client/src/structs.h index fa37e25..64ca29e 100644 --- a/04-collision/client/src/structs.h +++ b/04-map/client/src/structs.h @@ -9,8 +9,7 @@ struct game { int left; int right; - int up; - int down; + int jump; }; struct animation { @@ -43,6 +42,7 @@ struct object { struct object *create_object(SDL_Texture *texture, int scale, int resolution); void draw_object(struct game *game, struct object *object); +void frame_draw_object(struct game *game, struct object *object); void switch_animation(struct object *object, SDL_Texture *animation); #endif diff --git a/04-collision/server/Makefile b/04-map/server/Makefile similarity index 100% rename from 04-collision/server/Makefile rename to 04-map/server/Makefile diff --git a/04-map/server/README.md b/04-map/server/README.md new file mode 100644 index 0000000..51f75f5 --- /dev/null +++ b/04-map/server/README.md @@ -0,0 +1,3 @@ +# Map - Server + +Server-side code for 04-map diff --git a/04-collision/server/src/defs.h b/04-map/server/src/defs.h similarity index 77% rename from 04-collision/server/src/defs.h rename to 04-map/server/src/defs.h index 3294e62..fedd149 100644 --- a/04-collision/server/src/defs.h +++ b/04-map/server/src/defs.h @@ -21,11 +21,16 @@ // MCSA: Message Communication Standard Actions #define LEFT_MOVEMENT 0b10000000 #define RIGHT_MOVEMENT 0b01000000 -#define UP_MOVEMENT 0b00100000 -#define DOWN_MOVEMENT 0b00010000 +#define JUMP_MOVEMENT 0b00100000 +#define CROUCH_MOVEMENT 0b00010000 // OC: Object Constants #define GRAVITY 2 #define MOVEMENT_SPEED 10 +// BS: Block Standard +#define GRASS_BLOCK 1 +#define COBBLE_BLOCK 2 +#define LAVA_BLOCK 3 + #endif diff --git a/04-collision/server/src/structures.c b/04-map/server/src/hashmap.c similarity index 81% rename from 04-collision/server/src/structures.c rename to 04-map/server/src/hashmap.c index 60e0cf7..a458183 100644 --- a/04-collision/server/src/structures.c +++ b/04-map/server/src/hashmap.c @@ -1,9 +1,9 @@ #include #include -#include "structures.h" +#include "hashmap.h" #include "defs.h" -int map_find_empty_slot(void **map, int slots) { +int find_empty_slot(void **map, int slots) { int slot = 0; if (slots <= 0) @@ -25,12 +25,12 @@ int map_find_empty_slot(void **map, int slots) { // If there's Nx fewer used slots than total slots in the map, find the first possible one. // Else reallocate the map and use the end slot // Then allocate an object of size BYTES and add it into the map. -int map_allocate_value(void ***map, int *total_slots, int *used_slots, int bytes) { +int allocate_value(void ***map, int *total_slots, int *used_slots, int bytes) { int half_slots = (*total_slots) / 2; int empty_slot = 0; if (*used_slots < half_slots) { - empty_slot = map_find_empty_slot(*map, *used_slots); + empty_slot = find_empty_slot(*map, *used_slots); goto jump; } @@ -51,5 +51,3 @@ jump: return empty_slot; } - - diff --git a/04-map/server/src/hashmap.h b/04-map/server/src/hashmap.h new file mode 100644 index 0000000..5beed26 --- /dev/null +++ b/04-map/server/src/hashmap.h @@ -0,0 +1,9 @@ +#ifndef HASHMAP_H +#define HASHMAP_H + +#include "structs.h" + +int find_empty_slot(void **map, int slots); +int allocate_value(void ***map, int *total_slots, int *used_slots, int bytes); + +#endif diff --git a/04-map/server/src/main.c b/04-map/server/src/main.c new file mode 100644 index 0000000..c6af2f2 --- /dev/null +++ b/04-map/server/src/main.c @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include +#include +#include +#include "defs.h" +#include "structs.h" +#include "hashmap.h" + +#define DEBUG 0x0010 +#define PORT 9080 +#define MAP_WIDTH 100 +#define MAP_HEIGHT 100 + +struct connection **connections_map; +int connections_map_size = 0; +int connections_count = 0; + +int handle_connection(void *data) { + struct connection *connection_data = (struct connection *) data; + + struct map *new_map = generate_map(MAP_WIDTH, MAP_HEIGHT); + if (new_map == NULL) { + fprintf(stderr, "Erorr: failed generating map\n"); + goto end; + } + + SDLNet_TCP_Send(connection_data->socket, new_map->blocks, new_map->width*new_map->height*sizeof(int)); +end: + + SDLNet_TCP_Close(connection_data->socket); + connections_map[connection_data->id] = NULL; + connections_count--; + free(data); + + return 0; +} + +void catch_alarm(int sig) { + fprintf(stdout, "Notice: force stopping server...\n"); + exit(EXIT_SUCCESS); +} + +int new_key_position(void **map, int slots) { + int slot = 0; + + if (slots <= 0) + return slot; + + if (map == NULL) + return slot; + + while (slot <= slots) { + if (map[slot] == NULL) + break; + + slot++; + } + + return slot; +} + +int main(int argc, char *argv[]) { + signal(SIGINT, catch_alarm); + srand(time(NULL)); + + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + fprintf(stderr, "Error: could not initialize SDL\n%s\n", SDL_GetError()); + return -1; + } + + if (SDLNet_Init() != 0) { + fprintf(stderr, "Error: could not initialize SDL net\n%s\n", SDL_GetError()); + return -1; + } + + IPaddress ip; + if (SDLNet_ResolveHost(&ip, NULL, PORT) == -1) { + fprintf(stderr, "Error: failed resolving host \n%s\n", SDL_GetError()); + return -1; + } + + TCPsocket server = SDLNet_TCP_Open(&ip); + if (!server) { + fprintf(stderr, "Error: failed opening socket at %d\n%s\n", PORT, SDL_GetError()); + return -1; + } + + for (;;) { + TCPsocket client = SDLNet_TCP_Accept(server); + if (!client) { + SDL_Delay(100); + continue; + } + + fprintf(stdout, "Notice: accepted a connection from client!\n"); + + int new_connection_slot = allocate_value((void ***) &connections_map, &connections_map_size, &connections_count, sizeof(struct connection)); + if (new_connection_slot == MEMERR) { + fprintf(stderr, "MEMERR: failed allocating memory for new connection\n"); + return STDERR; + } + + struct connection *new_connection = connections_map[new_connection_slot]; + + new_connection->id = new_connection_slot; + new_connection->socket = client; + + if (DEBUG & PLAYER_DEBUG) + fprintf(stdout, "Created connection with id '%d'\n", new_connection->id); + + SDL_CreateThread(handle_connection, "client", new_connection); + } + + return 0; +} diff --git a/04-map/server/src/map.c b/04-map/server/src/map.c new file mode 100644 index 0000000..b5911e7 --- /dev/null +++ b/04-map/server/src/map.c @@ -0,0 +1,29 @@ +#include +#include "structs.h" + +struct map *generate_map(int width, int height) { + struct map *new_map = (struct map *) calloc(1, sizeof(struct map)); + if (new_map == NULL) + return NULL; + + int *map_blocks = (int *) calloc(width*height, sizeof(int)); + if (map_blocks == NULL) + return NULL; + + new_map->width = width; + new_map->height = height; + new_map->blocks = map_blocks; + + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + double scale = ((double) rand() / RAND_MAX); + int block = scale * 3; + int index = (y*width)+x; + map_blocks[index] = block; + } + } + + fprintf(stdout, "Value of first block -> %d\n", map_blocks[0]); + + return new_map; +} diff --git a/04-collision/server/src/structs.h b/04-map/server/src/structs.h similarity index 51% rename from 04-collision/server/src/structs.h rename to 04-map/server/src/structs.h index aeb32d0..0db270b 100644 --- a/04-collision/server/src/structs.h +++ b/04-map/server/src/structs.h @@ -5,19 +5,15 @@ struct connection { int id; - int obj_id; TCPsocket socket; }; -struct object { - int id; - int x; - int y; - - int colliding; - int force; - - uint8_t state; +struct map { + int width; + int height; + int *blocks; }; +struct map *generate_map(int width, int height); + #endif