24 while (SDL_PollEvent(&e)) {
26 case SDL_EVENT_WINDOW_RESIZED: {
28 SDL_Point window_size;
32 SDL_Point render_size;
34 &render_size.y,
nullptr);
36 const auto x_scale =
static_cast<float>(window_size.x) /
37 static_cast<float>(render_size.x);
39 const auto y_scale =
static_cast<float>(window_size.y) /
40 static_cast<float>(render_size.y);
42 const auto scale = std::min(x_scale, y_scale);
45 static_cast<float>(render_size.x) * scale,
46 static_cast<float>(render_size.y) * scale);
50 case SDL_EVENT_KEY_DOWN: {
52 keyboard.pressed[e.key.scancode] =
true;
53 keyboard.down[e.key.scancode] =
true;
54 keyboard.anyPressed =
true;
55 keyboard.lastPressed = e.key.scancode;
60 case SDL_EVENT_KEY_UP: {
62 keyboard.released[e.key.scancode] =
true;
63 keyboard.down[e.key.scancode] =
false;
68 case SDL_EVENT_MOUSE_BUTTON_DOWN: {
69 const auto button =
static_cast<int>(e.button.button);
70 mouse.pressed[button] =
true;
71 mouse.down[button] =
true;
72 mouse.anyPressed =
true;
73 mouse.lastPressed = button;
77 case SDL_EVENT_MOUSE_BUTTON_UP: {
78 auto button =
static_cast<int>(e.button.button);
79 mouse.released[button] =
true;
80 mouse.down[button] =
false;
84 case SDL_EVENT_MOUSE_MOTION: {
87 mouse.xChange = e.motion.xrel;
88 mouse.yChange = e.motion.yrel;
94 case SDL_EVENT_MOUSE_WHEEL: {
99 case SDL_EVENT_GAMEPAD_AXIS_MOTION: {
104 auto motion = e.gaxis.value / 32768.0f;
105 auto& current = controller[e.gaxis.which];
107 if (motion > current.deadZone) {
108 current.axis[e.gaxis.axis] = motion;
109 }
else if (motion < -current.deadZone) {
110 current.axis[e.gaxis.axis] = motion;
112 current.axis[e.gaxis.axis] = 0.0f;
118 case SDL_EVENT_GAMEPAD_BUTTON_DOWN: {
123 auto button =
static_cast<int>(e.gbutton.button);
124 controller[e.gbutton.which].pressed[button] =
true;
125 controller[e.gbutton.which].down[button] =
true;
126 controller[e.gbutton.which].anyPressed =
true;
127 controller[e.gbutton.which].lastPressed = button;
131 case SDL_EVENT_GAMEPAD_BUTTON_UP: {
136 auto button =
static_cast<int>(e.gbutton.button);
137 controller[e.gbutton.which].released[button] =
true;
138 controller[e.gbutton.which].down[button] =
false;
142 case SDL_EVENT_GAMEPAD_ADDED: {
144 !SDL_IsGamepad(e.gdevice.which) ||
145 SDL_OpenGamepad(e.gdevice.which) ==
nullptr) {
150 SDL_OpenGamepad(e.gdevice.which);
155 case SDL_EVENT_GAMEPAD_REMOVED: {
160 auto* controller = SDL_GetGamepadFromID(e.gdevice.which);
163 SDL_CloseGamepad(controller);
169 case SDL_EVENT_QUIT: {
181 if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMEPAD)) {
191 spec.format = SDL_AUDIO_S16LE;
195 if (!Mix_OpenAudio(0, &spec)) {
200 SDL_CreateWindow(
"", width * scale, height * scale, SDL_WINDOW_RESIZABLE);
207 SDL_SetHint(SDL_HINT_RENDER_VSYNC,
"1");
213 SDL_LOGICAL_PRESENTATION_LETTERBOX);