diff options
| author | esquizo <esquizo+noreply@esquizo.net> | 2026-05-02 14:12:34 -0300 |
|---|---|---|
| committer | esquizo <esquizo+noreply@esquizo.net> | 2026-05-02 14:12:43 -0300 |
| commit | 34acd612061792a38aa13e376b20f4b5d4b54302 (patch) | |
| tree | 1b0afe88f88f9f83c4e979383c8ccbbf4d91b3b1 /sdl3_main.c | |
| parent | b2a58e06b885c5801814943669d383f0141ddabb (diff) | |
movida a logica do jogo pro game.c
Diffstat (limited to 'sdl3_main.c')
| -rw-r--r-- | sdl3_main.c | 193 |
1 files changed, 5 insertions, 188 deletions
diff --git a/sdl3_main.c b/sdl3_main.c index ffd086f..709a37c 100644 --- a/sdl3_main.c +++ b/sdl3_main.c @@ -1,50 +1,23 @@ -#include <math.h> #include <stdbool.h> #include <stdio.h> -#include <stdlib.h> #include <SDL3/SDL.h> -#include <string.h> #include "dat.h" -#define SPEED 100 - static void render(void); static void update(float delta); static void process_events(void); -static Entity *allocentity(void); -static void freeentity(Entity *); - -static EntityCollisionProc bulletcollproc; - -static EntityUpdateProc playerproc; -static EntityUpdateProc ghostproc; - -static EntityCollisionProc *collisionProcs[LASTEntity] = { - [EBULLET] = bulletcollproc, -}; - -static EntityUpdateProc *updateprocs[LASTEntity] = { - [EPLAYER] = playerproc, - [EGHOST] = ghostproc, -}; - static SDL_Window *window; static SDL_Renderer *renderer; static bool running; static Uint64 old_ticks, new_ticks; -static Entity *player; - -Entity entitybuffer[2048]; -int ebufi; - static float camera_x, camera_y; -static bool shot; -static int shotx, shoty; +bool shot; +int shotx, shoty; int main() @@ -59,21 +32,10 @@ main() return -1; } - player = allocentity(); - player->type = EPLAYER; - player->body.size[0] = ENTITY_SIZE; - player->body.size[1] = ENTITY_SIZE; - for(int i = 0; i < 10; i++) { - Entity *e = allocentity(); - e->type = EGHOST; - e->body.pos[0] = rand() % 800; - e->body.pos[1] = rand() % 600; - e->body.size[0] = ENTITY_SIZE; - e->body.size[1] = ENTITY_SIZE; - } - + entinit(); running = true; old_ticks = SDL_GetTicksNS(); + while(running) { double delta; process_events(); @@ -178,32 +140,8 @@ render(void) void update(float delta) { - const _Bool *kstate = SDL_GetKeyboardState(NULL); - Entity *bullet, *other; - EntityCollision *coll; - - float vx, vy; - entphysics(delta); - while((coll = deqcoll())) { - Entity *self = coll->self; - Entity *target = coll->target; - - if(collisionProcs[self->type]) - collisionProcs[self->type](self, target); - - if(collisionProcs[target->type]) - collisionProcs[target->type](target, self); - } - - for(int ei = 0; ei < ebufi; ei++) { - Entity *e = entitybuffer + ei; - if(!e->active) - continue; - - if(updateprocs[e->type]) - updateprocs[e->type](e, delta); - } + entupdate(delta); } void @@ -224,124 +162,3 @@ process_events() } } } - -Entity * -allocentity(void) -{ - Entity *e; - - for(e = entitybuffer; e < entitybuffer + ebufi; e++) { - if(!e->active) - break; - } - - if(e >= entitybuffer + ebufi) { - if(ebufi >= 2048) - return NULL; - e = entitybuffer + ebufi; - ebufi++; - } - - memset(e, 0, sizeof(*e)); - e->active = true; - - return e; -} - -void -freeentity(Entity *e) -{ - e->active = false; -} - -void -bulletcollproc(Entity *self, Entity *target) -{ - if(target->type == EGHOST) { - self->active = false; - target->active = false; - } -} - -void -playerproc(Entity *e, float delta) -{ - Entity *bullet; - const _Bool *kstate = SDL_GetKeyboardState(NULL); - - e->body.vel[0] = 0; - e->body.vel[1] = 0; - - if(kstate[SDL_SCANCODE_W]) - e->body.vel[1] -= SPEED; - if(kstate[SDL_SCANCODE_A]) - e->body.vel[0] -= SPEED; - if(kstate[SDL_SCANCODE_S]) - e->body.vel[1] += SPEED; - if(kstate[SDL_SCANCODE_D]) - e->body.vel[0] += SPEED; - - if(shot) { - shot = false; - int i; - - float dx = shotx - e->body.pos[0]; - float dy = shoty - e->body.pos[1]; - float dd = sqrtf(dx * dx + dy * dy); - - dx /= dd; - dy /= dd; - - bullet = allocentity(); - bullet->type = EBULLET; - bullet->time = 0.5; - bullet->body.vel[0] = dx * SPEED * 5.0; - bullet->body.vel[1] = dy * SPEED * 5.0; - bullet->body.pos[0] = e->body.pos[0]; - bullet->body.pos[1] = e->body.pos[1]; - bullet->body.size[0] = BULLET_SIZE; - bullet->body.size[1] = BULLET_SIZE; - bullet->active = true; - } -} - -void -ghostproc(Entity *e, float delta) -{ - Entity *other; - float vx, vy; - - vx = 0; - vy = 0; - - float dx = player->body.pos[0] - e->body.pos[0]; - float dy = player->body.pos[1] - e->body.pos[1]; - float dd = sqrtf(dx * dx + dy * dy); - - dx /= dd; - dy /= dd; - - vx += dx * SPEED; - vy += dy * SPEED; - - for(int j = 0; j < 1024; j++) { - other = entitybuffer + j; - if(!other->active || other == e || other->type != EGHOST) - continue; - - float dx = other->body.pos[0] - e->body.pos[0]; - float dy = other->body.pos[1] - e->body.pos[1]; - float dd = sqrtf(dx * dx + dy * dy); - - dx /= dd; - dy /= dd; - - if(dd < 40) { - vx -= dx * 100; - vy -= dy * 100; - } - } - - e->body.vel[0] = vx; - e->body.vel[1] = vy; -} |
