Coolkid's typedef on structs.

booyah
This commit is contained in:
KikooDX 2021-03-14 00:25:24 +01:00
parent bfa9385841
commit 8bcce2348d
2 changed files with 27 additions and 22 deletions

View File

@ -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);

View File

@ -2,10 +2,10 @@
#include <gint/keyboard.h>
#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;
}