From bfa938584185452097d8eb3e680425f6d0feb61d Mon Sep 17 00:00:00 2001 From: KikooDX Date: Sun, 14 Mar 2021 00:18:11 +0100 Subject: [PATCH 1/3] Add typedef for tiles, use TwoValues in functions, add weird comments. --- .gitignore | 2 ++ include/main.h | 19 +++++++++++++++---- src/levels.c | 8 ++++++-- src/main.c | 34 ++++++++++++++++++---------------- 4 files changed, 41 insertions(+), 22 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2b70265 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# build files are ): +build-cg diff --git a/include/main.h b/include/main.h index 80b02d3..963ef3c 100644 --- a/include/main.h +++ b/include/main.h @@ -12,7 +12,18 @@ struct Player { struct TwoValues spawn; }; -struct TwoValues search(int x, int level[16][16]); -int collide_pixel(int x, int y, int obj, int level[16][16]); -int collide(int x, int y, int h, int obj, int level[16][16]); -struct Player level_reset(struct Player player); \ No newline at end of file +/* using a typedef is what cool kids do at least i think + * you might no remember what `int x` is :( + * if x is declared with `tile_t x` you know x is some tile :) + * knowledge is power and cool + * as bonus it looks cool in function declaration + * cool stuff told u :D */ +typedef int tile_t; + +struct TwoValues search(tile_t x, tile_t level[16][16]); +/* here u can see i replaced x and y arguments with your twovalues struct + * u made smthing use it + * it will be handy in the future and makes better looking code */ +int collide_pixel(struct TwoValues pos, tile_t obj, tile_t level[16][16]); +int collide(struct TwoValues pos, int h, tile_t obj, tile_t level[16][16]); +struct Player level_reset(struct Player player); diff --git a/src/levels.c b/src/levels.c index 3d2ef9b..65e98dc 100644 --- a/src/levels.c +++ b/src/levels.c @@ -1,4 +1,8 @@ -const int level[16][16] = { +#include "main.h" + +/* see use tile_t cause these stuff are tiles + * would you believe it? */ +const tile_t level[16][16] = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 3, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1}, @@ -15,4 +19,4 @@ const int level[16][16] = { {1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 3, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 3, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} -}; \ No newline at end of file +}; diff --git a/src/main.c b/src/main.c index 6f9ede8..d543e89 100644 --- a/src/main.c +++ b/src/main.c @@ -2,7 +2,7 @@ #include #include "main.h" -struct TwoValues search(int x, int level[16][16]) { +struct TwoValues search(tile_t x, tile_t level[16][16]) { // Search for x in a given matrix. // If x is found, return it coordinates struct TwoValues coordinates = {0, 0}; @@ -19,9 +19,9 @@ struct TwoValues search(int x, int level[16][16]) { return coordinates; } -int collide_pixel(int x, int y, int obj, int level[LEVEL_SIZE][LEVEL_SIZE]) { +int collide_pixel(struct TwoValues pos, tile_t obj, tile_t level[LEVEL_SIZE][LEVEL_SIZE]) { // Check if there's something in (x, y) - if(obj == level[x][y]) { + if(obj == level[pos.x][pos.y]) { return 1; } else { @@ -29,15 +29,18 @@ int collide_pixel(int x, int y, int obj, int level[LEVEL_SIZE][LEVEL_SIZE]) { } } -int collide(int x, int y, int h, int obj, int level[LEVEL_SIZE][LEVEL_SIZE]) { +int collide(struct TwoValues pos, int h, tile_t obj, tile_t level[LEVEL_SIZE][LEVEL_SIZE]) { + const int x = pos.x; + const int y = pos.y; // Check if there's something in // the square (x + 1, y + 1, x + 11, y + 11) // The size of the hitbox changes with h - if( collide_pixel((x + h) / TILE_SIZE, (y + h) / TILE_SIZE, obj, level) || - collide_pixel((x + h) / TILE_SIZE, (y + TILE_SIZE - h - 1) / TILE_SIZE, obj, level) || - collide_pixel((x + TILE_SIZE - h - 1) / TILE_SIZE, (y + h) / TILE_SIZE, obj, level) || - collide_pixel((x + TILE_SIZE - h - 1) / TILE_SIZE, (y + TILE_SIZE - h - 1) / TILE_SIZE, obj, level) - ) { + if( collide_pixel((struct TwoValues){(x + h) / TILE_SIZE, (y + h) / TILE_SIZE}, obj, level) || + collide_pixel((struct TwoValues){(x + h) / TILE_SIZE, (y + TILE_SIZE - h - 1) / TILE_SIZE}, obj, level) || + collide_pixel((struct TwoValues){(x + TILE_SIZE - h - 1) / TILE_SIZE, (y + h) / TILE_SIZE}, obj, level) || + collide_pixel((struct TwoValues){(x + TILE_SIZE - h - 1) / TILE_SIZE, (y + TILE_SIZE - h - 1) / TILE_SIZE}, obj, level) + ) + { return 1; } return 0; @@ -48,13 +51,12 @@ struct Player level_reset(struct Player player) { return player; } -int main(void) -{ +int main(void) { extern bopti_image_t img_player; extern bopti_image_t img_wall; extern bopti_image_t img_spike; - extern int level[LEVEL_SIZE][LEVEL_SIZE]; + extern tile_t level[LEVEL_SIZE][LEVEL_SIZE]; int running = 1; @@ -96,20 +98,20 @@ int main(void) clearevents(); // trying to move the player >w< - if(!collide(player.pos.x + mov_x, player.pos.y, 0, 1, level)) { + if(!collide((struct TwoValues){player.pos.x + mov_x, player.pos.y}, 0, 1, level)) { player.pos.x += mov_x; } - if(!collide(player.pos.x, player.pos.y + mov_y, 0, 1, level)) { + if(!collide((struct TwoValues){player.pos.x, player.pos.y + mov_y}, 0, 1, level)) { player.pos.y += mov_y; } // d i e - if(collide(player.pos.x + mov_x, player.pos.y + mov_y, 2, 3, level)) { + if(collide(player.pos, 2, 3, level)) { player = level_reset(player); } - if(keydown(KEY_EXIT)) {running = 0;} + if(keydown(KEY_EXIT)) running = 0; } return 1; From 8bcce2348d1680b2d48d4fda106c0de7ea2d88ea Mon Sep 17 00:00:00 2001 From: KikooDX Date: Sun, 14 Mar 2021 00:25:24 +0100 Subject: [PATCH 2/3] Coolkid's typedef on structs. booyah --- include/main.h | 25 +++++++++++++++---------- src/main.c | 24 ++++++++++++------------ 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/include/main.h b/include/main.h index 963ef3c..2bad117 100644 --- a/include/main.h +++ b/include/main.h @@ -3,14 +3,19 @@ #define LEVEL_SIZE 16 #define TILE_SIZE 12 -struct TwoValues { +/* see we use a typedef like bellow + * its lazy and allow you to type + * Vec2 instead of struct TwoValues */ +typedef struct Vec2 { int x, y; -}; +} Vec2; -struct Player { - struct TwoValues pos; - struct TwoValues spawn; -}; +/* we do da same with player cause laziness never killed nobody + * wait */ +typedef struct Player { + Vec2 pos; + Vec2 spawn; +} Player; /* using a typedef is what cool kids do at least i think * you might no remember what `int x` is :( @@ -20,10 +25,10 @@ struct Player { * cool stuff told u :D */ typedef int tile_t; -struct TwoValues search(tile_t x, tile_t level[16][16]); +Vec2 search(tile_t x, tile_t level[16][16]); /* here u can see i replaced x and y arguments with your twovalues struct * u made smthing use it * it will be handy in the future and makes better looking code */ -int collide_pixel(struct TwoValues pos, tile_t obj, tile_t level[16][16]); -int collide(struct TwoValues pos, int h, tile_t obj, tile_t level[16][16]); -struct Player level_reset(struct Player player); +int collide_pixel(Vec2 pos, tile_t obj, tile_t level[16][16]); +int collide(Vec2 pos, int h, tile_t obj, tile_t level[16][16]); +Player level_reset(Player player); diff --git a/src/main.c b/src/main.c index d543e89..1fc8b31 100644 --- a/src/main.c +++ b/src/main.c @@ -2,10 +2,10 @@ #include #include "main.h" -struct TwoValues search(tile_t x, tile_t level[16][16]) { +Vec2 search(tile_t x, tile_t level[16][16]) { // Search for x in a given matrix. // If x is found, return it coordinates - struct TwoValues coordinates = {0, 0}; + Vec2 coordinates = {0, 0}; for(int m = 0; m < LEVEL_SIZE; ++m) { for(int n = 0; n < LEVEL_SIZE; ++n) { if(level[m][n] == x) { @@ -19,7 +19,7 @@ struct TwoValues search(tile_t x, tile_t level[16][16]) { return coordinates; } -int collide_pixel(struct TwoValues pos, tile_t obj, tile_t level[LEVEL_SIZE][LEVEL_SIZE]) { +int collide_pixel(Vec2 pos, tile_t obj, tile_t level[LEVEL_SIZE][LEVEL_SIZE]) { // Check if there's something in (x, y) if(obj == level[pos.x][pos.y]) { return 1; @@ -29,16 +29,16 @@ int collide_pixel(struct TwoValues pos, tile_t obj, tile_t level[LEVEL_SIZE][LEV } } -int collide(struct TwoValues pos, int h, tile_t obj, tile_t level[LEVEL_SIZE][LEVEL_SIZE]) { +int collide(Vec2 pos, int h, tile_t obj, tile_t level[LEVEL_SIZE][LEVEL_SIZE]) { const int x = pos.x; const int y = pos.y; // Check if there's something in // the square (x + 1, y + 1, x + 11, y + 11) // The size of the hitbox changes with h - if( collide_pixel((struct TwoValues){(x + h) / TILE_SIZE, (y + h) / TILE_SIZE}, obj, level) || - collide_pixel((struct TwoValues){(x + h) / TILE_SIZE, (y + TILE_SIZE - h - 1) / TILE_SIZE}, obj, level) || - collide_pixel((struct TwoValues){(x + TILE_SIZE - h - 1) / TILE_SIZE, (y + h) / TILE_SIZE}, obj, level) || - collide_pixel((struct TwoValues){(x + TILE_SIZE - h - 1) / TILE_SIZE, (y + TILE_SIZE - h - 1) / TILE_SIZE}, obj, level) + if( collide_pixel((Vec2){(x + h) / TILE_SIZE, (y + h) / TILE_SIZE}, obj, level) || + collide_pixel((Vec2){(x + h) / TILE_SIZE, (y + TILE_SIZE - h - 1) / TILE_SIZE}, obj, level) || + collide_pixel((Vec2){(x + TILE_SIZE - h - 1) / TILE_SIZE, (y + h) / TILE_SIZE}, obj, level) || + collide_pixel((Vec2){(x + TILE_SIZE - h - 1) / TILE_SIZE, (y + TILE_SIZE - h - 1) / TILE_SIZE}, obj, level) ) { return 1; @@ -46,7 +46,7 @@ int collide(struct TwoValues pos, int h, tile_t obj, tile_t level[LEVEL_SIZE][LE return 0; } -struct Player level_reset(struct Player player) { +Player level_reset(Player player) { player.pos = player.spawn; return player; } @@ -61,7 +61,7 @@ int main(void) { int running = 1; // player - struct Player player = { + Player player = { .pos = {0, 0}, .spawn = {0, 0} }; @@ -98,11 +98,11 @@ int main(void) { clearevents(); // trying to move the player >w< - if(!collide((struct TwoValues){player.pos.x + mov_x, player.pos.y}, 0, 1, level)) { + if(!collide((Vec2){player.pos.x + mov_x, player.pos.y}, 0, 1, level)) { player.pos.x += mov_x; } - if(!collide((struct TwoValues){player.pos.x, player.pos.y + mov_y}, 0, 1, level)) { + if(!collide((Vec2){player.pos.x, player.pos.y + mov_y}, 0, 1, level)) { player.pos.y += mov_y; } From 5c60f8d65ccbdb51dd4fb645cb8289b7f627bbd2 Mon Sep 17 00:00:00 2001 From: KikooDX Date: Sun, 14 Mar 2021 00:40:14 +0100 Subject: [PATCH 3/3] Using more Vec2 to make lazy boi happy. --- .gitignore | 3 +++ src/main.c | 34 ++++++++++++++++++++-------------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 2b70265..ff228cf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ # build files are ): build-cg + +# go away binary, everyone hates you >:( +*.g3a diff --git a/src/main.c b/src/main.c index 1fc8b31..0133e20 100644 --- a/src/main.c +++ b/src/main.c @@ -21,7 +21,7 @@ Vec2 search(tile_t x, tile_t level[16][16]) { int collide_pixel(Vec2 pos, tile_t obj, tile_t level[LEVEL_SIZE][LEVEL_SIZE]) { // Check if there's something in (x, y) - if(obj == level[pos.x][pos.y]) { + if(obj == level[pos.x / TILE_SIZE][pos.y / TILE_SIZE]) { return 1; } else { @@ -30,16 +30,18 @@ int collide_pixel(Vec2 pos, tile_t obj, tile_t level[LEVEL_SIZE][LEVEL_SIZE]) { } int collide(Vec2 pos, int h, tile_t obj, tile_t level[LEVEL_SIZE][LEVEL_SIZE]) { - const int x = pos.x; - const int y = pos.y; + /* tl = top left + * br = bottom right + * avoid repetition later on ezier to work on */ + const Vec2 pos_tl = (Vec2){pos.x + h, pos.y + h}; + const Vec2 pos_br = (Vec2){pos.x + TILE_SIZE - h - 1, pos.y + TILE_SIZE - h - 1}; // Check if there's something in // the square (x + 1, y + 1, x + 11, y + 11) // The size of the hitbox changes with h - if( collide_pixel((Vec2){(x + h) / TILE_SIZE, (y + h) / TILE_SIZE}, obj, level) || - collide_pixel((Vec2){(x + h) / TILE_SIZE, (y + TILE_SIZE - h - 1) / TILE_SIZE}, obj, level) || - collide_pixel((Vec2){(x + TILE_SIZE - h - 1) / TILE_SIZE, (y + h) / TILE_SIZE}, obj, level) || - collide_pixel((Vec2){(x + TILE_SIZE - h - 1) / TILE_SIZE, (y + TILE_SIZE - h - 1) / TILE_SIZE}, obj, level) - ) + if(collide_pixel(pos_tl, obj, level) || + collide_pixel(pos_br, obj, level) || + collide_pixel((Vec2){pos_tl.x, pos_br.y}, obj, level) || + collide_pixel((Vec2){pos_br.x, pos_tl.y}, obj, level)) { return 1; } @@ -93,17 +95,21 @@ int main(void) { dimage(player.pos.x, player.pos.y, &img_player); dupdate(); - int mov_x = keydown(KEY_RIGHT) - keydown(KEY_LEFT); - int mov_y = keydown(KEY_DOWN) - keydown(KEY_UP); + + /* if something has x and y, you probably want to use your Vec2 struct */ + Vec2 mov = { + .x = keydown(KEY_RIGHT) - keydown(KEY_LEFT), + .y = keydown(KEY_DOWN) - keydown(KEY_UP) + }; clearevents(); // trying to move the player >w< - if(!collide((Vec2){player.pos.x + mov_x, player.pos.y}, 0, 1, level)) { - player.pos.x += mov_x; + if(!collide((Vec2){player.pos.x + mov.x, player.pos.y}, 0, 1, level)) { + player.pos.x += mov.x; } - if(!collide((Vec2){player.pos.x, player.pos.y + mov_y}, 0, 1, level)) { - player.pos.y += mov_y; + if(!collide((Vec2){player.pos.x, player.pos.y + mov.y}, 0, 1, level)) { + player.pos.y += mov.y; } // d i e