yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
window.cc
Go to the documentation of this file.
1#include "app/core/window.h"
2
3#include "absl/status/status.h"
4#include "absl/strings/str_format.h"
7#include "app/gui/style.h"
8#include "imgui/backends/imgui_impl_sdl2.h"
9#include "imgui/backends/imgui_impl_sdlrenderer2.h"
10#include "imgui/imgui.h"
11
12namespace yaze {
13namespace core {
14
15absl::Status CreateWindow(Window& window, int flags) {
16 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) != 0) {
17 return absl::InternalError(
18 absl::StrFormat("SDL_Init: %s\n", SDL_GetError()));
19 }
20
21 SDL_DisplayMode display_mode;
22 SDL_GetCurrentDisplayMode(0, &display_mode);
23 int screen_width = display_mode.w * 0.8;
24 int screen_height = display_mode.h * 0.8;
25
26 window.window_ = std::unique_ptr<SDL_Window, SDL_Deleter>(
27 SDL_CreateWindow("Yet Another Zelda3 Editor", SDL_WINDOWPOS_UNDEFINED,
28 SDL_WINDOWPOS_UNDEFINED, screen_width, screen_height,
29 flags),
30 SDL_Deleter());
31 if (window.window_ == nullptr) {
32 return absl::InternalError(
33 absl::StrFormat("SDL_CreateWindow: %s\n", SDL_GetError()));
34 }
35
36 RETURN_IF_ERROR(Renderer::Get().CreateRenderer(window.window_.get()));
37
38 IMGUI_CHECKVERSION();
39 ImGui::CreateContext();
40 ImGuiIO& io = ImGui::GetIO();
41 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
42 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
43
44 ImGui_ImplSDL2_InitForSDLRenderer(window.window_.get(),
45 Renderer::Get().renderer());
46 ImGui_ImplSDLRenderer2_Init(Renderer::Get().renderer());
47
49
51
52 ImGui_ImplSDLRenderer2_NewFrame();
53 ImGui_ImplSDL2_NewFrame();
54
55 const int audio_frequency = 48000;
56 SDL_AudioSpec want, have;
57 SDL_memset(&want, 0, sizeof(want));
58 want.freq = audio_frequency;
59 want.format = AUDIO_S16;
60 want.channels = 2;
61 want.samples = 2048;
62 want.callback = NULL; // Uses the queue
63 window.audio_device_ = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
64 if (window.audio_device_ == 0) {
65 throw std::runtime_error(
66 absl::StrFormat("Failed to open audio: %s\n", SDL_GetError()));
67 }
68 window.audio_buffer_ = std::make_shared<int16_t>(audio_frequency / 50 * 4);
69 SDL_PauseAudioDevice(window.audio_device_, 0);
70
71 return absl::OkStatus();
72}
73
74absl::Status ShutdownWindow(Window& window) {
75 SDL_PauseAudioDevice(window.audio_device_, 1);
76 SDL_CloseAudioDevice(window.audio_device_);
77 ImGui_ImplSDL2_Shutdown();
78 ImGui_ImplSDLRenderer2_Shutdown();
79 ImGui::DestroyContext();
80 SDL_DestroyRenderer(Renderer::Get().renderer());
81 SDL_DestroyWindow(window.window_.get());
82 SDL_Quit();
83 return absl::OkStatus();
84}
85
86absl::Status HandleEvents(Window &window) {
87 SDL_Event event;
88 ImGuiIO &io = ImGui::GetIO();
89 SDL_WaitEvent(&event);
90 ImGui_ImplSDL2_ProcessEvent(&event);
91 switch (event.type) {
92 case SDL_KEYDOWN:
93 case SDL_KEYUP: {
94 io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
95 io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
96 io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
97 io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
98 break;
99 }
100 case SDL_WINDOWEVENT:
101 switch (event.window.event) {
102 case SDL_WINDOWEVENT_CLOSE:
103 window.active_ = false;
104 break;
105 case SDL_WINDOWEVENT_SIZE_CHANGED:
106 io.DisplaySize.x = static_cast<float>(event.window.data1);
107 io.DisplaySize.y = static_cast<float>(event.window.data2);
108 break;
109 }
110 break;
111 }
112 int mouseX;
113 int mouseY;
114 const int buttons = SDL_GetMouseState(&mouseX, &mouseY);
115
116 io.DeltaTime = 1.0f / 60.0f;
117 io.MousePos = ImVec2(static_cast<float>(mouseX), static_cast<float>(mouseY));
118 io.MouseDown[0] = buttons & SDL_BUTTON(SDL_BUTTON_LEFT);
119 io.MouseDown[1] = buttons & SDL_BUTTON(SDL_BUTTON_RIGHT);
120 io.MouseDown[2] = buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE);
121
122 int wheel = 0;
123 io.MouseWheel = static_cast<float>(wheel);
124 return absl::OkStatus();
125}
126
127} // namespace core
128} // namespace yaze
static Renderer & Get()
Definition window.h:37
#define RETURN_IF_ERROR(expression)
Definition macro.h:51
absl::Status ShutdownWindow(Window &window)
Definition window.cc:74
absl::Status CreateWindow(Window &window, int flags)
Definition window.cc:15
absl::Status LoadPackageFonts()
absl::Status HandleEvents(Window &window)
Definition window.cc:86
void ColorsYaze()
Definition style.cc:132
Main namespace for the application.
Definition controller.cc:12
Deleter for SDL_Window and SDL_Renderer.
Definition sdl_deleter.h:12
std::shared_ptr< SDL_Window > window_
Definition window.h:17
std::shared_ptr< int16_t > audio_buffer_
Definition window.h:19
SDL_AudioDeviceID audio_device_
Definition window.h:18