#include #include #include #include "dat.h" static void render(void); static void update(float delta); static void process_events(void); static SDL_Window *window; static SDL_Renderer *renderer; static bool running; static Uint64 old_ticks, new_ticks; static float camera_x, camera_y; bool shot; bool moveup, movedown, moveleft, moveright; int shotx, shoty; int main() { if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) { fprintf(stderr, "SDL_Init(): %s\n", SDL_GetError()); return 1; } if(!SDL_CreateWindowAndRenderer("window", 800, 600, SDL_WINDOW_OPENGL, &window, &renderer)) { fprintf(stderr, "SDL_CreateWindowAndRenderer: %s\n", SDL_GetError()); return -1; } entinit(); running = true; old_ticks = SDL_GetTicksNS(); while(running) { double delta; process_events(); new_ticks = SDL_GetTicksNS(); delta = (new_ticks - old_ticks) / 1000000000.0; old_ticks = new_ticks; update(delta); render(); SDL_RenderPresent(renderer); } SDL_DestroyWindow(window); SDL_Quit(); return 0; } void render(void) { { float plx, ply, minx, miny, maxx, maxy; plx = player->body.pos[0] - camera_x; ply = player->body.pos[1] - camera_y; minx = plx - 80; miny = ply - 80; maxx = plx + 80; maxy = ply + 80; if(minx < 0) camera_x += minx; if(miny < 0) camera_y += miny; if(maxx > 800) camera_x += (maxx - 800); if(maxy > 600) camera_y += (maxy - 600); } SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); SDL_RenderClear(renderer); SDL_SetRenderDrawColor(renderer, 0xAA, 0xAA, 0xAA, 0xFF); for(int y = 0; y < map_height; y++) for(int x = 0; x < map_width; x++) { if(!map_data[x + map_width * y]) continue; SDL_RenderFillRect(renderer, &(SDL_FRect) { .x = 2 * ENTITY_SIZE * x - ENTITY_SIZE - camera_x, .y = 2 * ENTITY_SIZE * y - ENTITY_SIZE - camera_y, .w = 2.0 * ENTITY_SIZE, .h = 2.0 * ENTITY_SIZE, }); } for(int i = 0; i < ebufi; i++) { Entity *e = entitybuffer + i; if(!e->active) continue; switch (e->type) { case EPLAYER: SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); SDL_RenderRect(renderer, &(SDL_FRect){ .x = e->body.pos[0] - ENTITY_SIZE - camera_x, .y = e->body.pos[1] - ENTITY_SIZE - camera_y, .w = ENTITY_SIZE * 2.0, .h = ENTITY_SIZE * 2.0 }); break; case EGHOST: SDL_SetRenderDrawColor(renderer, 0xAA, 0xAA, 0xAA, 0xFF); SDL_RenderRect(renderer, &(SDL_FRect){ .x = e->body.pos[0] - ENTITY_SIZE - camera_x, .y = e->body.pos[1] - ENTITY_SIZE - camera_y, .w = ENTITY_SIZE * 2.0, .h = ENTITY_SIZE * 2.0 }); break; case EBULLET: SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0x00, 0xFF); SDL_RenderRect(renderer, &(SDL_FRect){ .x = e->body.pos[0] - BULLET_SIZE - camera_x, .y = e->body.pos[1] - BULLET_SIZE - camera_y, .w = BULLET_SIZE * 2.0, .h = BULLET_SIZE * 2.0 }); break; } } } void update(float delta) { entphysics(delta); entupdate(delta); } void process_events() { SDL_Event event; while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_EVENT_QUIT: running = false; break; case SDL_EVENT_MOUSE_BUTTON_DOWN: shot = true; shotx = event.button.x + camera_x; shoty = event.button.y + camera_y; break; case SDL_EVENT_KEY_UP: case SDL_EVENT_KEY_DOWN: #define DEFINE_KEY(X, VAR) \ if(event.key.scancode == X) VAR = event.type == SDL_EVENT_KEY_DOWN DEFINE_KEY(SDL_SCANCODE_W, moveup); DEFINE_KEY(SDL_SCANCODE_A, moveleft); DEFINE_KEY(SDL_SCANCODE_S, movedown); DEFINE_KEY(SDL_SCANCODE_D, moveright); #undef DEFINE_KEY break; } } }