diff options
| author | esquizo <esquizo+noreply@esquizo.net> | 2026-04-24 17:34:02 -0300 |
|---|---|---|
| committer | esquizo <esquizo+noreply@esquizo.net> | 2026-04-24 17:34:18 -0300 |
| commit | c027a6ebdb3e1cba203ea067720cca3a4c620974 (patch) | |
| tree | f5a821999e3ca26a4f1c67511d4bb095942f63ee | |
| parent | 8e53990f306a5f746a82793b98c9410e3cde8810 (diff) | |
estrutura body
| -rw-r--r-- | sdl3_main.c | 87 |
1 files changed, 48 insertions, 39 deletions
diff --git a/sdl3_main.c b/sdl3_main.c index 1bb4a89..b4988bc 100644 --- a/sdl3_main.c +++ b/sdl3_main.c @@ -12,21 +12,24 @@ static void render(void); static void update(float delta); static void process_events(void); -static int checkcollision(float pos1[2], float size1[2], float pos2[2], float size2[2]); +static int checkcollision(float pos1[2], float size1[2], float pos2[2], float size2[2]); typedef struct { - float x, y; + float pos[2]; + float vel[2]; +} Body; + +typedef struct { + Body body; } Player; typedef struct { - float x, y; - float vx, vy; + Body body; bool active; } Ghost; typedef struct { - float x, y; - float vx, vy; + Body body; float time; bool active; } Bullet; @@ -63,8 +66,8 @@ main() for(int i = 0; i < 10; i++) { ghosts[i].active = 1; - ghosts[i].x = rand() % 800; - ghosts[i].y = rand() % 600; + ghosts[i].body.pos[0] = rand() % 800; + ghosts[i].body.pos[1] = rand() % 600; } running = true; @@ -93,8 +96,8 @@ render(void) { float plx, ply, minx, miny, maxx, maxy; - plx = player.x - camera_x; - ply = player.y - camera_y; + plx = player.body.pos[0] - camera_x; + ply = player.body.pos[1] - camera_y; minx = plx - 80; miny = ply - 80; @@ -133,8 +136,8 @@ render(void) SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); SDL_RenderRect(renderer, &(SDL_FRect){ - .x = player.x - ENTITY_SIZE - camera_x, - .y = player.y - ENTITY_SIZE - camera_y, + .x = player.body.pos[0] - ENTITY_SIZE - camera_x, + .y = player.body.pos[1] - ENTITY_SIZE - camera_y, .w = ENTITY_SIZE * 2.0, .h = ENTITY_SIZE * 2.0 }); @@ -145,8 +148,8 @@ render(void) SDL_SetRenderDrawColor(renderer, 0xAA, 0xAA, 0xAA, 0xFF); SDL_RenderRect(renderer, &(SDL_FRect){ - .x = ghosts[i].x - ENTITY_SIZE - camera_x, - .y = ghosts[i].y - ENTITY_SIZE - camera_y, + .x = ghosts[i].body.pos[0] - ENTITY_SIZE - camera_x, + .y = ghosts[i].body.pos[1] - ENTITY_SIZE - camera_y, .w = ENTITY_SIZE * 2.0, .h = ENTITY_SIZE * 2.0 }); @@ -158,8 +161,8 @@ render(void) SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0x00, 0xFF); SDL_RenderRect(renderer, &(SDL_FRect){ - .x = bullet[i].x - BULLET_SIZE - camera_x, - .y = bullet[i].y - BULLET_SIZE - camera_y, + .x = bullet[i].body.pos[0] - BULLET_SIZE - camera_x, + .y = bullet[i].body.pos[1] - BULLET_SIZE - camera_y, .w = BULLET_SIZE * 2.0, .h = BULLET_SIZE * 2.0 }); @@ -172,21 +175,24 @@ update(float delta) # define SPEED 100 const _Bool *kstate = SDL_GetKeyboardState(NULL); + player.body.vel[0] = 0; + player.body.vel[1] = 0; + if(kstate[SDL_SCANCODE_W]) - player.y -= delta * SPEED; + player.body.vel[1] -= SPEED; if(kstate[SDL_SCANCODE_A]) - player.x -= delta * SPEED; + player.body.vel[0] -= SPEED; if(kstate[SDL_SCANCODE_S]) - player.y += delta * SPEED; + player.body.vel[1] += SPEED; if(kstate[SDL_SCANCODE_D]) - player.x += delta * SPEED; + player.body.vel[0] += SPEED; if(shot) { shot = false; int i; - float dx = shotx - player.x; - float dy = shoty - player.y; + float dx = shotx - player.body.pos[0]; + float dy = shoty - player.body.pos[1]; float dd = sqrtf(dx * dx + dy * dy); dx /= dd; @@ -197,15 +203,14 @@ update(float delta) if(i < 1024) { bullet[i].time = 0.5; - bullet[i].vx = dx * SPEED * 5.0; - bullet[i].vy = dy * SPEED * 5.0; - bullet[i].x = player.x; - bullet[i].y = player.y; + bullet[i].body.vel[0] = dx * SPEED * 5.0; + bullet[i].body.vel[1] = dy * SPEED * 5.0; + bullet[i].body.pos[0] = player.body.pos[0]; + bullet[i].body.pos[1] = player.body.pos[1]; bullet[i].active = true; } } - for(int i = 0; i < 1024; i++) { if(!bullet[i].active) continue; @@ -215,8 +220,8 @@ update(float delta) continue; int r = checkcollision( - (float[2]){ bullet[i].x, bullet[i].y }, (float[2]){ BULLET_SIZE, BULLET_SIZE }, - (float[2]){ ghosts[j].x, ghosts[j].y }, (float[2]){ ENTITY_SIZE, ENTITY_SIZE } + bullet[i].body.pos, (float[2]){ BULLET_SIZE, BULLET_SIZE }, + ghosts[j].body.pos, (float[2]){ ENTITY_SIZE, ENTITY_SIZE } ); if(r) { @@ -232,8 +237,8 @@ update(float delta) float vx = 0, vy = 0; - float dx = player.x - ghosts[i].x; - float dy = player.y - ghosts[i].y; + float dx = player.body.pos[0] - ghosts[i].body.pos[0]; + float dy = player.body.pos[1] - ghosts[i].body.pos[1]; float dd = sqrtf(dx * dx + dy * dy); dx /= dd; @@ -246,8 +251,8 @@ update(float delta) if(!ghosts[j].active || i == j) continue; - float dx = ghosts[j].x - ghosts[i].x; - float dy = ghosts[j].y - ghosts[i].y; + float dx = ghosts[j].body.pos[0] - ghosts[i].body.pos[0]; + float dy = ghosts[j].body.pos[1] - ghosts[i].body.pos[1]; float dd = sqrtf(dx * dx + dy * dy); dx /= dd; @@ -259,16 +264,19 @@ update(float delta) } } - ghosts[i].vx = vx; - ghosts[i].vy = vy; + ghosts[i].body.vel[0] = vx; + ghosts[i].body.vel[1] = vy; } + player.body.pos[0] += player.body.vel[0] * delta; + player.body.pos[1] += player.body.vel[1] * delta; + for(int i = 0; i < 1024; i++) { if(!ghosts[i].active) continue; - ghosts[i].x += ghosts[i].vx * delta; - ghosts[i].y += ghosts[i].vy * delta; + ghosts[i].body.pos[0] += ghosts[i].body.vel[0] * delta; + ghosts[i].body.pos[1] += ghosts[i].body.vel[1] * delta; } for(int i = 0; i < 1024; i++) { @@ -281,8 +289,8 @@ update(float delta) continue; } - bullet[i].x += delta * bullet[i].vx; - bullet[i].y += delta * bullet[i].vy; + bullet[i].body.pos[0] += delta * bullet[i].body.vel[0]; + bullet[i].body.pos[1] += delta * bullet[i].body.vel[1]; } } @@ -324,3 +332,4 @@ checkcollision(float p1[2], float s1[2], float p2[2], float s2[2]) return minx < 0 && maxx > 0 && miny < 0 && maxy > 0; } + |
