#include #include #include #include "sector.h" #include "common.h" int SW = 1200; int SH = 800; char *name = "Thingy"; SDL_Surface *surface = NULL; void vline(int x, int y1, int y2, uint32_t col) { y1 = clamp(y1, 0, SH-1); y2 = clamp(y2, 0, SH-1); uint32_t *pixels = (uint32_t*)surface->pixels; for(int y = min(y1, y2); y <= max(y1, y2); y++) { pixels[y * SW + x] = SDL_MapRGB(surface->format, color(col)); } } int main(void) { // set logging priority #ifdef DEBUG SDL_LogSetAllPriority(SDL_LOG_PRIORITY_DEBUG); #else SDL_LogSetAllPriority(SDL_LOG_PRIORITY_ERROR); #endif // ----------------- START SDL ----------------- // int ret = 1; SDL_Window *window = NULL; map_t map = {0}; if(SDL_Init(SDL_INIT_VIDEO) != 0) { log_critical(LOG_VIDEO, "SDL_Init: %s", SDL_GetError()); goto exit; } window = SDL_CreateWindow(name, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SW, SH, 0); if(window == NULL) { log_critical(LOG_VIDEO, "SDL_CreateWindow: %s", SDL_GetError()); goto exit; } surface = SDL_GetWindowSurface(window); if(surface == NULL) { log_critical(LOG_VIDEO, "SDL_GetWindowSurface %s", SDL_GetError()); goto exit; } // --------------------------------------------- // if(map_load(&map, "files/map_clear.txt")) return 1; bool quit = false; int wasd[4] = {0}; while(!quit) { SDL_Event event; while(SDL_PollEvent(&event)) { // ----------------- EVENTS ----------------- // switch(event.type) { case SDL_QUIT: quit = true; break; case SDL_KEYDOWN: switch(event.key.keysym.sym) { case 'w': wasd[0] = 1; break; case 'a': wasd[1] = 1; break; case 's': wasd[2] = 1; break; case 'd': wasd[3] = 1; break; } break; case SDL_KEYUP: switch(event.key.keysym.sym) { case 'w': wasd[0] = 0; break; case 'a': wasd[1] = 0; break; case 's': wasd[2] = 0; break; case 'd': wasd[3] = 0; break; } break; default: // log_debug(LOG_APPLICATION, "event: %d", event.type); break; } // ------------------------------------------ // } // ----------------- GAME LOOP ----------------- // int x,y; SDL_GetRelativeMouseState(&x,&y); map.player.angle += x * 0.03f; float move_vec[2] = {0.f, 0.f}; if(wasd[0]) { move_vec[0] += map.player.anglecos*0.2f; move_vec[1] += map.player.anglesin*0.2f; } if(wasd[2]) { move_vec[0] -= map.player.anglecos*0.2f; move_vec[1] -= map.player.anglesin*0.2f; } if(wasd[1]) { move_vec[0] += map.player.anglesin*0.2f; move_vec[1] -= map.player.anglecos*0.2f; } if(wasd[3]) { move_vec[0] -= map.player.anglesin*0.2f; move_vec[1] += map.player.anglecos*0.2f; } int pushing = wasd[0] || wasd[1] || wasd[2] || wasd[3]; float acceleration = pushing ? 0.4 : 0.2; map.player.pos.x += move_vec[0] * acceleration; map.player.pos.y += move_vec[1] * acceleration; player_update(&map.player); for(int i = 0; i < SW-1; i++) vline(i, 0, SH-1, COLOR_LIGHTWHITE); map_draw(&map, SW, SH); // --------------------------------------------- // if(SDL_UpdateWindowSurface(window) != 0) { log_error(LOG_VIDEO, "SDL_UpdateWindowSurface: %s", SDL_GetError()); } } ret = 0; exit: map_unload(&map); if(window != NULL) SDL_DestroyWindow(window); SDL_Quit(); return ret; }