You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

243 lines
5.1 KiB

#include "Engine.hpp"
#include "Platform.hpp"
#include <cstdint>
#include <SDL2/SDL.h>
void constexpr SetBit(uint32_t& x, unsigned long bit)
{
x |= (1UL << bit);
}
void constexpr ClearBit(uint32_t& x, unsigned long bit)
{
x &= ~(1UL << bit);
}
static void HandleEvent(Platform& platform, SDL_Event& event);
PlatformStatus Platform_Init(Platform& platform, int width, int height)
{
int result = SDL_Init(SDL_INIT_VIDEO);
if (result < 0)
{
fprintf(stderr, "Error initializing SDL: %s\n", SDL_GetError());
return PlatformStatus::Error;
}
SDL_Window* window = SDL_CreateWindow(
"Soft 3D Engine",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
width, height,
SDL_WINDOW_SHOWN);
if (window == nullptr)
{
fprintf(stderr, "Error creating SDL window: %s\n", SDL_GetError());
return PlatformStatus::Error;
}
SDL_Surface* surface = SDL_GetWindowSurface(window);
if (surface == nullptr)
{
fprintf(stderr, "Error getting SDL window surface: %s\n", SDL_GetError());
return PlatformStatus::Error;
}
result = SDL_ShowCursor(SDL_DISABLE);
if (result < 0)
{
fprintf(stderr, "Error disabling cursor in SDL window: %s\n", SDL_GetError());
return PlatformStatus::Error;
}
platform.framerateMillis = (1000 / WINDOW_FPS);
platform.window = window;
platform.surface = surface;
return PlatformStatus::Ok;
}
PlatformStatus Platform_CheckForEvents(Platform& platform)
{
SDL_Event event;
while (SDL_PollEvent(&event) != 0)
{
if (event.type == SDL_QUIT)
{
return PlatformStatus::Quit;
}
else
{
HandleEvent(platform, event);
}
}
return PlatformStatus::Ok;
}
void Platform_ClearWindow(Platform& platform)
{
SDL_LockSurface(platform.surface);
SDL_FillRect(platform.surface, nullptr, 0);
}
void Platform_UpdateWindow(Platform& platform)
{
SDL_UnlockSurface(platform.surface);
SDL_UpdateWindowSurface(platform.window);
}
void Platform_GetFrameTime(Platform& platform)
{
platform.frameStartMillis = SDL_GetTicks();
}
void Platform_SyncToFramerate(Platform& platform)
{
uint32_t stopTimeMillis = SDL_GetTicks();
uint32_t framerateMillis = stopTimeMillis - platform.frameStartMillis;
// Delay if time to spare
if (framerateMillis < platform.framerateMillis)
{
uint32_t delayMillis = platform.framerateMillis - framerateMillis;
SDL_Delay(delayMillis);
}
}
void Platform_Shutdown(Platform& platform)
{
SDL_DestroyWindow(platform.window);
SDL_Quit();
}
// PRIVATE FUNCTIONS
static void HandleEvent(
Platform& platform, SDL_Event& event)
{
if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_w)
{
SetBit(platform.input, TRANSLATE_Z_POS);
}
else if (event.key.keysym.sym == SDLK_s)
{
SetBit(platform.input, TRANSLATE_Z_NEG);
}
else if (event.key.keysym.sym == SDLK_a)
{
SetBit(platform.input, TRANSLATE_X_NEG);
}
else if (event.key.keysym.sym == SDLK_d)
{
SetBit(platform.input, TRANSLATE_X_POS);
}
else if (event.key.keysym.sym == SDLK_q)
{
SetBit(platform.input, TRANSLATE_Y_POS);
}
else if (event.key.keysym.sym == SDLK_e)
{
SetBit(platform.input, TRANSLATE_Y_NEG);
}
else if (event.key.keysym.sym == SDLK_i)
{
SetBit(platform.input, ROTATE_X_POS);
}
else if (event.key.keysym.sym == SDLK_k)
{
SetBit(platform.input, ROTATE_X_NEG);
}
else if (event.key.keysym.sym == SDLK_j)
{
SetBit(platform.input, ROTATE_Y_POS);
}
else if (event.key.keysym.sym == SDLK_l)
{
SetBit(platform.input, ROTATE_Y_NEG);
}
else if (event.key.keysym.sym == SDLK_u)
{
SetBit(platform.input, ROTATE_Z_POS);
}
else if (event.key.keysym.sym == SDLK_o)
{
SetBit(platform.input, ROTATE_Z_NEG);
}
else if (event.key.keysym.sym == SDLK_UP)
{
SetBit(platform.input, SCALE_UP);
}
else if (event.key.keysym.sym == SDLK_DOWN)
{
SetBit(platform.input, SCALE_DOWN);
}
}
else if (event.type == SDL_KEYUP)
{
if (event.key.keysym.sym == SDLK_w)
{
ClearBit(platform.input, TRANSLATE_Z_POS);
}
else if (event.key.keysym.sym == SDLK_s)
{
ClearBit(platform.input, TRANSLATE_Z_NEG);
}
else if (event.key.keysym.sym == SDLK_a)
{
ClearBit(platform.input, TRANSLATE_X_NEG);
}
else if (event.key.keysym.sym == SDLK_d)
{
ClearBit(platform.input, TRANSLATE_X_POS);
}
else if (event.key.keysym.sym == SDLK_q)
{
ClearBit(platform.input, TRANSLATE_Y_POS);
}
else if (event.key.keysym.sym == SDLK_e)
{
ClearBit(platform.input, TRANSLATE_Y_NEG);
}
else if (event.key.keysym.sym == SDLK_i)
{
ClearBit(platform.input, ROTATE_X_POS);
}
else if (event.key.keysym.sym == SDLK_k)
{
ClearBit(platform.input, ROTATE_X_NEG);
}
else if (event.key.keysym.sym == SDLK_j)
{
ClearBit(platform.input, ROTATE_Y_POS);
}
else if (event.key.keysym.sym == SDLK_l)
{
ClearBit(platform.input, ROTATE_Y_NEG);
}
else if (event.key.keysym.sym == SDLK_u)
{
ClearBit(platform.input, ROTATE_Z_POS);
}
else if (event.key.keysym.sym == SDLK_o)
{
ClearBit(platform.input, ROTATE_Z_NEG);
}
else if (event.key.keysym.sym == SDLK_UP)
{
ClearBit(platform.input, SCALE_UP);
}
else if (event.key.keysym.sym == SDLK_DOWN)
{
ClearBit(platform.input, SCALE_DOWN);
}
}
}