💿🐜 Antkeeper source code https://antkeeper.com
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.

747 lines
19 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "animation/frame-scheduler.hpp"
  20. #include "application.hpp"
  21. #include "debug/logger.hpp"
  22. #include "debug/performance-sampler.hpp"
  23. #include "event/event-dispatcher.hpp"
  24. #include "event/window-events.hpp"
  25. #include "input/scancode.hpp"
  26. #include "input/sdl-game-controller-tables.hpp"
  27. #include "input/sdl-scancode-table.hpp"
  28. #include "resources/image.hpp"
  29. #include <SDL2/SDL.h>
  30. #include <glad/glad.h>
  31. #include <stdexcept>
  32. #include <utility>
  33. #include <thread>
  34. #include <stb/stb_image_write.h>
  35. #include <iostream>
  36. #include <iomanip>
  37. application::application():
  38. closed(false),
  39. exit_status(EXIT_SUCCESS),
  40. current_state{std::string(), nullptr, nullptr},
  41. queued_state{std::string(), nullptr, nullptr},
  42. update_callback(nullptr),
  43. render_callback(nullptr),
  44. fullscreen(true),
  45. vsync(true),
  46. cursor_visible(true),
  47. display_dimensions({0, 0}),
  48. window_dimensions({0, 0}),
  49. viewport_dimensions({0, 0}),
  50. mouse_position({0, 0}),
  51. update_rate(60.0),
  52. logger(nullptr),
  53. sdl_window(nullptr),
  54. sdl_gl_context(nullptr)
  55. {
  56. // Setup logging
  57. logger = new debug::logger();
  58. // Get SDL compiled version
  59. SDL_version sdl_compiled_version;
  60. SDL_VERSION(&sdl_compiled_version);
  61. std::string sdl_compiled_version_string = std::to_string(sdl_compiled_version.major) + "." + std::to_string(sdl_compiled_version.minor) + "." + std::to_string(sdl_compiled_version.patch);
  62. logger->log("Compiled against SDL " + sdl_compiled_version_string);
  63. // Get SDL linked version
  64. SDL_version sdl_linked_version;
  65. SDL_GetVersion(&sdl_linked_version);
  66. std::string sdl_linked_version_string = std::to_string(sdl_linked_version.major) + "." + std::to_string(sdl_linked_version.minor) + "." + std::to_string(sdl_linked_version.patch);
  67. logger->log("Linking against SDL " + sdl_linked_version_string);
  68. // Init SDL
  69. logger->push_task("Initializing SDL");
  70. if (SDL_Init(SDL_INIT_VIDEO) != 0)
  71. {
  72. logger->pop_task(EXIT_FAILURE);
  73. throw std::runtime_error("Failed to initialize SDL");
  74. }
  75. else
  76. {
  77. logger->pop_task(EXIT_SUCCESS);
  78. }
  79. // Load default OpenGL library
  80. logger->push_task("Loading OpenGL library");
  81. if (SDL_GL_LoadLibrary(nullptr) != 0)
  82. {
  83. logger->pop_task(EXIT_FAILURE);
  84. }
  85. else
  86. {
  87. logger->pop_task(EXIT_SUCCESS);
  88. }
  89. // Set window creation hints
  90. SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
  91. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  92. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  93. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  94. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  95. // Get display dimensions
  96. SDL_DisplayMode sdl_desktop_display_mode;
  97. if (SDL_GetDesktopDisplayMode(0, &sdl_desktop_display_mode) != 0)
  98. {
  99. logger->error("Failed to get desktop display mode: \"" + std::string(SDL_GetError()) + "\"");
  100. throw std::runtime_error("Failed to detect desktop display mode");
  101. }
  102. else
  103. {
  104. logger->log("Detected " + std::to_string(sdl_desktop_display_mode.w) + "x" + std::to_string(sdl_desktop_display_mode.h) + " display");
  105. display_dimensions = {sdl_desktop_display_mode.w, sdl_desktop_display_mode.h};
  106. }
  107. // Create a hidden fullscreen window
  108. logger->push_task("Creating " + std::to_string(display_dimensions[0]) + "x" + std::to_string(display_dimensions[1]) + " window");
  109. sdl_window = SDL_CreateWindow
  110. (
  111. "",
  112. SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  113. display_dimensions[0], display_dimensions[1],
  114. SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_HIDDEN
  115. );
  116. if (!sdl_window)
  117. {
  118. logger->pop_task(EXIT_FAILURE);
  119. throw std::runtime_error("Failed to create SDL window");
  120. }
  121. else
  122. {
  123. logger->pop_task(EXIT_SUCCESS);
  124. }
  125. // Create OpenGL context
  126. logger->push_task("Creating OpenGL 3.3 context");
  127. sdl_gl_context = SDL_GL_CreateContext(sdl_window);
  128. if (!sdl_gl_context)
  129. {
  130. logger->pop_task(EXIT_FAILURE);
  131. throw std::runtime_error("Failed to create OpenGL context");
  132. }
  133. else
  134. {
  135. logger->pop_task(EXIT_SUCCESS);
  136. }
  137. // Update window size and viewport size
  138. SDL_GetWindowSize(sdl_window, &window_dimensions[0], &window_dimensions[1]);
  139. SDL_GL_GetDrawableSize(sdl_window, &viewport_dimensions[0], &viewport_dimensions[1]);
  140. // Load OpenGL functions via GLAD
  141. logger->push_task("Loading OpenGL functions");
  142. if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress))
  143. {
  144. logger->pop_task(EXIT_FAILURE);
  145. throw std::runtime_error("Failed to load OpenGL functions");
  146. }
  147. else
  148. {
  149. logger->pop_task(EXIT_SUCCESS);
  150. }
  151. // Set v-sync mode
  152. int swap_interval = (vsync) ? 1 : 0;
  153. logger->push_task((swap_interval) ? "Enabling v-sync" : "Disabling v-sync");
  154. if (SDL_GL_SetSwapInterval(swap_interval) != 0)
  155. {
  156. logger->pop_task(EXIT_FAILURE);
  157. }
  158. else
  159. {
  160. logger->pop_task(EXIT_SUCCESS);
  161. }
  162. // Init SDL joystick and gamepad subsystems
  163. logger->push_task("Initializing SDL Joystick and Game Controller subsystems");
  164. if (SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) != 0)
  165. {
  166. logger->pop_task(EXIT_FAILURE);
  167. }
  168. else
  169. {
  170. logger->pop_task(EXIT_SUCCESS);
  171. }
  172. // Load SDL gamepad mappings
  173. /*
  174. logger->push_task("Loading SDL gamepad mappings from database");
  175. std::string gamecontrollerdb_path = data_path + "controls/gamecontrollerdb.txt";
  176. if (SDL_GameControllerAddMappingsFromFile(gamecontrollerdb_path.c_str()) == -1)
  177. {
  178. logger->pop_task(EXIT_FAILURE);
  179. }
  180. else
  181. {
  182. logger->pop_task(EXIT_SUCCESS);
  183. }
  184. */
  185. // Setup rasterizer
  186. rasterizer = new gl::rasterizer();
  187. // Setup events
  188. event_dispatcher = new ::event_dispatcher();
  189. // Setup input
  190. keyboard = new input::keyboard();
  191. keyboard->set_event_dispatcher(event_dispatcher);
  192. mouse = new input::mouse();
  193. mouse->set_event_dispatcher(event_dispatcher);
  194. // Connect gamepads
  195. translate_sdl_events();
  196. // Setup frame scheduler
  197. frame_scheduler = new ::frame_scheduler();
  198. frame_scheduler->set_update_callback(std::bind(&application::update, this, std::placeholders::_1, std::placeholders::_2));
  199. frame_scheduler->set_render_callback(std::bind(&application::render, this, std::placeholders::_1));
  200. frame_scheduler->set_update_rate(update_rate);
  201. frame_scheduler->set_max_frame_duration(0.25);
  202. // Setup performance sampling
  203. performance_sampler = new debug::performance_sampler();
  204. performance_sampler->set_sample_size(15);
  205. }
  206. application::~application()
  207. {
  208. // Destroy the OpenGL context
  209. SDL_GL_DeleteContext(sdl_gl_context);
  210. // Destroy the SDL window
  211. SDL_DestroyWindow(sdl_window);
  212. // Shutdown SDL
  213. SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
  214. SDL_Quit();
  215. }
  216. void application::close(int status)
  217. {
  218. closed = true;
  219. exit_status = status;
  220. }
  221. int application::execute(bootloader_type bootloader)
  222. {
  223. try
  224. {
  225. // Execute bootloader
  226. if (bootloader)
  227. {
  228. exit_status = bootloader(this);
  229. if (exit_status != EXIT_SUCCESS)
  230. {
  231. return exit_status;
  232. }
  233. }
  234. // Show window
  235. SDL_ShowWindow(sdl_window);
  236. // Clear window
  237. rasterizer->clear_framebuffer(true, false, false);
  238. SDL_GL_SwapWindow(sdl_window);
  239. // Perform initial update
  240. update(0.0, 0.0);
  241. // Reset frame scheduler
  242. frame_scheduler->reset();
  243. // Schedule frames until closed
  244. while (!closed)
  245. {
  246. // Tick frame scheduler
  247. frame_scheduler->tick();
  248. // Sample frame duration
  249. performance_sampler->sample(frame_scheduler->get_frame_duration());
  250. }
  251. // Exit current state
  252. change_state({std::string(), nullptr, nullptr});
  253. }
  254. catch (const std::exception& e)
  255. {
  256. // Print exception to logger
  257. logger->error(std::string("Unhandled exception: \"") + e.what() + std::string("\""));
  258. // Show error message box with unhandled exception
  259. SDL_ShowSimpleMessageBox
  260. (
  261. SDL_MESSAGEBOX_ERROR,
  262. "Unhandled Exception",
  263. e.what(),
  264. sdl_window
  265. );
  266. // Set exit status to failure
  267. exit_status = EXIT_FAILURE;
  268. }
  269. return exit_status;
  270. }
  271. void application::change_state(const application::state& next_state)
  272. {
  273. // Exit current state
  274. if (current_state.exit)
  275. {
  276. logger->push_task("Exiting application state \"" + current_state.name + "\"");
  277. try
  278. {
  279. current_state.exit();
  280. }
  281. catch (...)
  282. {
  283. logger->pop_task(EXIT_FAILURE);
  284. throw;
  285. }
  286. logger->pop_task(EXIT_SUCCESS);
  287. }
  288. current_state = next_state;
  289. // Enter next state
  290. if (current_state.enter)
  291. {
  292. logger->push_task("Entering application state \"" + current_state.name + "\"");
  293. try
  294. {
  295. current_state.enter();
  296. }
  297. catch (...)
  298. {
  299. logger->pop_task(EXIT_FAILURE);
  300. throw;
  301. }
  302. logger->pop_task(EXIT_SUCCESS);
  303. }
  304. // Enter queued state (if any)
  305. if (queued_state.enter != nullptr || queued_state.exit != nullptr)
  306. {
  307. // Make a copy of the queued state
  308. application::state queued_state_copy = queued_state;
  309. // Clear the queued state
  310. queued_state = {std::string(), nullptr, nullptr};
  311. // Enter the queued state
  312. change_state(queued_state_copy);
  313. }
  314. }
  315. void application::queue_state(const application::state& next_state)
  316. {
  317. queued_state = next_state;
  318. logger->log("Queued application state \"" + queued_state.name + "\"");
  319. }
  320. std::shared_ptr<image> application::capture_frame() const
  321. {
  322. int w = viewport_dimensions[0];
  323. int h = viewport_dimensions[1];
  324. std::shared_ptr<image> frame = std::make_shared<image>();
  325. frame->format(3, false);
  326. frame->resize(w, h);
  327. // Read pixel data from framebuffer into image
  328. glReadBuffer(GL_BACK);
  329. glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, frame->get_pixels());
  330. return std::move(frame);
  331. }
  332. void application::save_frame(const std::string& path) const
  333. {
  334. logger->push_task("Saving screenshot to \"" + path + "\"");
  335. auto frame = capture_frame();
  336. std::thread
  337. (
  338. [frame, path]
  339. {
  340. stbi_flip_vertically_on_write(1);
  341. stbi_write_png(path.c_str(), frame->get_width(), frame->get_height(), frame->get_channel_count(), frame->get_pixels(), frame->get_width() * frame->get_channel_count());
  342. }
  343. ).detach();
  344. logger->pop_task(EXIT_SUCCESS);
  345. }
  346. void application::set_update_callback(const update_callback_type& callback)
  347. {
  348. update_callback = callback;
  349. }
  350. void application::set_render_callback(const render_callback_type& callback)
  351. {
  352. render_callback = callback;
  353. }
  354. void application::set_update_rate(double frequency)
  355. {
  356. update_rate = frequency;
  357. frame_scheduler->set_update_rate(update_rate);
  358. }
  359. void application::set_title(const std::string& title)
  360. {
  361. SDL_SetWindowTitle(sdl_window, title.c_str());
  362. }
  363. void application::set_cursor_visible(bool visible)
  364. {
  365. SDL_ShowCursor((visible) ? SDL_ENABLE : SDL_DISABLE);
  366. cursor_visible = visible;
  367. }
  368. void application::set_relative_mouse_mode(bool enabled)
  369. {
  370. if (enabled)
  371. {
  372. SDL_GetMouseState(&mouse_position[0], &mouse_position[1]);
  373. SDL_ShowCursor(SDL_DISABLE);
  374. SDL_SetRelativeMouseMode(SDL_TRUE);
  375. }
  376. else
  377. {
  378. SDL_SetRelativeMouseMode(SDL_FALSE);
  379. SDL_WarpMouseInWindow(sdl_window, mouse_position[0], mouse_position[1]);
  380. if (cursor_visible)
  381. SDL_ShowCursor(SDL_ENABLE);
  382. }
  383. }
  384. void application::resize_window(int width, int height)
  385. {
  386. int x = (display_dimensions[0] >> 1) - (width >> 1);
  387. int y = (display_dimensions[1] >> 1) - (height >> 1);
  388. // Resize and center window
  389. SDL_SetWindowPosition(sdl_window, x, y);
  390. SDL_SetWindowSize(sdl_window, width, height);
  391. window_resized();
  392. }
  393. void application::set_fullscreen(bool fullscreen)
  394. {
  395. if (this->fullscreen != fullscreen)
  396. {
  397. this->fullscreen = fullscreen;
  398. if (fullscreen)
  399. {
  400. SDL_HideWindow(sdl_window);
  401. SDL_SetWindowFullscreen(sdl_window, SDL_WINDOW_FULLSCREEN_DESKTOP);
  402. SDL_ShowWindow(sdl_window);
  403. }
  404. else
  405. {
  406. SDL_SetWindowFullscreen(sdl_window, 0);
  407. SDL_SetWindowBordered(sdl_window, SDL_TRUE);
  408. SDL_SetWindowResizable(sdl_window, SDL_TRUE);
  409. }
  410. }
  411. }
  412. void application::set_vsync(bool vsync)
  413. {
  414. if (this->vsync != vsync)
  415. {
  416. this->vsync = vsync;
  417. SDL_GL_SetSwapInterval((vsync) ? 1 : 0);
  418. }
  419. }
  420. void application::set_window_opacity(float opacity)
  421. {
  422. SDL_SetWindowOpacity(sdl_window, opacity);
  423. }
  424. void application::swap_buffers()
  425. {
  426. SDL_GL_SwapWindow(sdl_window);
  427. }
  428. void application::update(double t, double dt)
  429. {
  430. translate_sdl_events();
  431. event_dispatcher->update(t);
  432. if (update_callback)
  433. {
  434. update_callback(t, dt);
  435. }
  436. /*
  437. static int frame = 0;
  438. if (frame % 60 == 0)
  439. {
  440. std::cout << std::fixed;
  441. std::cout << std::setprecision(2);
  442. std::cout << performance_sampler->mean_frame_duration() * 1000.0 << "\n";
  443. }
  444. ++frame;
  445. */
  446. }
  447. void application::render(double alpha)
  448. {
  449. /*
  450. std::cout << std::fixed;
  451. std::cout << std::setprecision(2);
  452. std::cout << performance_sampler->mean_frame_duration() * 1000.0 << std::endl;
  453. */
  454. if (render_callback)
  455. {
  456. render_callback(alpha);
  457. }
  458. SDL_GL_SwapWindow(sdl_window);
  459. }
  460. void application::translate_sdl_events()
  461. {
  462. // Mouse motion event accumulators
  463. bool mouse_motion = false;
  464. int mouse_x;
  465. int mouse_y;
  466. int mouse_dx = 0;
  467. int mouse_dy = 0;
  468. SDL_Event sdl_event;
  469. while (SDL_PollEvent(&sdl_event))
  470. {
  471. switch (sdl_event.type)
  472. {
  473. case SDL_KEYDOWN:
  474. case SDL_KEYUP:
  475. {
  476. if (sdl_event.key.repeat == 0)
  477. {
  478. input::scancode scancode = input::scancode::unknown;
  479. if (sdl_event.key.keysym.scancode <= SDL_SCANCODE_APP2)
  480. {
  481. scancode = input::sdl_scancode_table[sdl_event.key.keysym.scancode];
  482. }
  483. if (sdl_event.type == SDL_KEYDOWN)
  484. keyboard->press(scancode);
  485. else
  486. keyboard->release(scancode);
  487. }
  488. break;
  489. }
  490. case SDL_MOUSEMOTION:
  491. {
  492. // More than one mouse motion event is often generated per frame, and may be a source of lag.
  493. // Mouse events are accumulated here to prevent excess function calls and allocations
  494. mouse_motion = true;
  495. mouse_x = sdl_event.motion.x;
  496. mouse_y = sdl_event.motion.y;
  497. mouse_dx += sdl_event.motion.xrel;
  498. mouse_dy += sdl_event.motion.yrel;
  499. break;
  500. }
  501. case SDL_MOUSEBUTTONDOWN:
  502. {
  503. mouse->press(sdl_event.button.button, sdl_event.button.x, sdl_event.button.y);
  504. break;
  505. }
  506. case SDL_MOUSEBUTTONUP:
  507. {
  508. mouse->release(sdl_event.button.button, sdl_event.button.x, sdl_event.button.y);
  509. break;
  510. }
  511. case SDL_MOUSEWHEEL:
  512. {
  513. int direction = (sdl_event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED) ? -1 : 1;
  514. mouse->scroll(sdl_event.wheel.x * direction, sdl_event.wheel.y * direction);
  515. break;
  516. }
  517. case SDL_CONTROLLERBUTTONDOWN:
  518. {
  519. if (sdl_event.cbutton.button != SDL_CONTROLLER_BUTTON_INVALID)
  520. {
  521. if (auto it = gamepad_map.find(sdl_event.cdevice.which); it != gamepad_map.end())
  522. {
  523. input::gamepad_button button = input::sdl_button_table[sdl_event.cbutton.button];
  524. it->second->press(button);
  525. }
  526. }
  527. break;
  528. }
  529. case SDL_CONTROLLERBUTTONUP:
  530. {
  531. if (sdl_event.cbutton.button != SDL_CONTROLLER_BUTTON_INVALID)
  532. {
  533. if (auto it = gamepad_map.find(sdl_event.cdevice.which); it != gamepad_map.end())
  534. {
  535. input::gamepad_button button = input::sdl_button_table[sdl_event.cbutton.button];
  536. it->second->release(button);
  537. }
  538. }
  539. break;
  540. }
  541. case SDL_CONTROLLERAXISMOTION:
  542. {
  543. if (sdl_event.caxis.axis != SDL_CONTROLLER_AXIS_INVALID)
  544. {
  545. if (auto it = gamepad_map.find(sdl_event.cdevice.which); it != gamepad_map.end())
  546. {
  547. input::gamepad_axis axis = input::sdl_axis_table[sdl_event.caxis.axis];
  548. float value = sdl_event.caxis.value;
  549. static const float min = static_cast<float>(std::numeric_limits<std::int16_t>::min());
  550. static const float max = static_cast<float>(std::numeric_limits<std::int16_t>::max());
  551. value /= (value < 0.0f) ? -min : max;
  552. it->second->move(axis, value);
  553. }
  554. }
  555. break;
  556. }
  557. case SDL_CONTROLLERDEVICEADDED:
  558. {
  559. if (SDL_IsGameController(sdl_event.cdevice.which))
  560. {
  561. SDL_GameController* sdl_controller = SDL_GameControllerOpen(sdl_event.cdevice.which);
  562. std::string controller_name = SDL_GameControllerNameForIndex(sdl_event.cdevice.which);
  563. if (sdl_controller)
  564. {
  565. if (auto it = gamepad_map.find(sdl_event.cdevice.which); it != gamepad_map.end())
  566. {
  567. logger->log("Reconnected gamepad \"" + controller_name + "\"");
  568. it->second->connect(true);
  569. }
  570. else
  571. {
  572. // Get gamepad GUID
  573. SDL_Joystick* sdl_joystick = SDL_GameControllerGetJoystick(sdl_controller);
  574. SDL_JoystickGUID sdl_guid = SDL_JoystickGetGUID(sdl_joystick);
  575. char guid_buffer[33];
  576. std::memset(guid_buffer, '\0', 33);
  577. SDL_JoystickGetGUIDString(sdl_guid, &guid_buffer[0], 33);
  578. std::string guid(guid_buffer);
  579. logger->log("Connected gamepad \"" + controller_name + "\" with GUID: " + guid + "");
  580. // Create new gamepad
  581. input::gamepad* gamepad = new input::gamepad();
  582. gamepad->set_event_dispatcher(event_dispatcher);
  583. gamepad->set_guid(guid);
  584. // Add gamepad to list and map
  585. gamepads.push_back(gamepad);
  586. gamepad_map[sdl_event.cdevice.which] = gamepad;
  587. // Send controller connected event
  588. gamepad->connect(false);
  589. }
  590. }
  591. else
  592. {
  593. logger->error("Failed to connected gamepad \"" + controller_name + "\"");
  594. }
  595. }
  596. break;
  597. }
  598. case SDL_CONTROLLERDEVICEREMOVED:
  599. {
  600. SDL_GameController* sdl_controller = SDL_GameControllerFromInstanceID(sdl_event.cdevice.which);
  601. if (sdl_controller)
  602. {
  603. SDL_GameControllerClose(sdl_controller);
  604. logger->log("Disconnected gamepad");
  605. if (auto it = gamepad_map.find(sdl_event.cdevice.which); it != gamepad_map.end())
  606. {
  607. it->second->disconnect();
  608. }
  609. }
  610. break;
  611. }
  612. case SDL_WINDOWEVENT:
  613. {
  614. if (sdl_event.window.event == SDL_WINDOWEVENT_RESIZED)
  615. {
  616. window_resized();
  617. }
  618. break;
  619. }
  620. case SDL_QUIT:
  621. {
  622. close(EXIT_SUCCESS);
  623. break;
  624. }
  625. }
  626. }
  627. // Process accumulated mouse motion events
  628. if (mouse_motion)
  629. {
  630. mouse->move(mouse_x, mouse_y, mouse_dx, mouse_dy);
  631. }
  632. }
  633. void application::window_resized()
  634. {
  635. // Update window size and viewport size
  636. SDL_GetWindowSize(sdl_window, &window_dimensions[0], &window_dimensions[1]);
  637. SDL_GL_GetDrawableSize(sdl_window, &viewport_dimensions[0], &viewport_dimensions[1]);
  638. rasterizer->context_resized(viewport_dimensions[0], viewport_dimensions[1]);
  639. window_resized_event event;
  640. event.w = window_dimensions[0];
  641. event.h = window_dimensions[1];
  642. event_dispatcher->queue(event);
  643. }