yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
controller.cc
Go to the documentation of this file.
1#include "controller.h"
2
3#include <SDL.h>
4
5#include <filesystem>
6#include <memory>
7
8#include "absl/status/status.h"
9#include "absl/strings/str_format.h"
12#include "app/gui/style.h"
14#include "imgui/backends/imgui_impl_sdl2.h"
15#include "imgui/backends/imgui_impl_sdlrenderer2.h"
16#include "imgui/imgui.h"
17
18namespace yaze {
19namespace app {
20namespace core {
21
22namespace {
23
24constexpr ImGuiWindowFlags kMainEditorFlags =
25 ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoCollapse |
26 ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_MenuBar |
27 ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoTitleBar;
28
29using ImGui::Begin;
30using ImGui::End;
31using ImGui::GetIO;
32using ImGui::NewFrame;
33using ImGui::SetNextWindowPos;
34using ImGui::SetNextWindowSize;
35
37 const ImGuiIO &io = GetIO();
38 ImGui_ImplSDLRenderer2_NewFrame();
39 ImGui_ImplSDL2_NewFrame();
40 NewFrame();
41 SetNextWindowPos(gui::kZeroPos);
42 ImVec2 dimensions(io.DisplaySize.x, io.DisplaySize.y);
43 SetNextWindowSize(dimensions, ImGuiCond_Always);
44
45 if (!Begin("##YazeMain", nullptr, kMainEditorFlags)) {
46 End();
47 return;
48 }
49}
50
51} // namespace
52
53absl::Status Controller::OnEntry(std::string filename) {
54#if defined(__APPLE__) && defined(__MACH__)
55#if TARGET_IPHONE_SIMULATOR == 1 || TARGET_OS_IPHONE == 1
57#elif TARGET_OS_MAC == 1
59#endif
60#elif defined(_WIN32)
62#elif defined(__linux__)
64#else
66#endif
72 active_ = true;
73 return absl::OkStatus();
74}
75
77 int wheel = 0;
78 SDL_Event event;
79 ImGuiIO &io = ImGui::GetIO();
80
81 while (SDL_PollEvent(&event)) {
82 ImGui_ImplSDL2_ProcessEvent(&event);
83 switch (event.type) {
84 case SDL_KEYDOWN:
85 case SDL_KEYUP: {
86 ImGuiIO &io = ImGui::GetIO();
87 io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
88 io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
89 io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
90 io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
91 break;
92 }
93 case SDL_WINDOWEVENT:
94 switch (event.window.event) {
95 case SDL_WINDOWEVENT_CLOSE:
96 active_ = false;
97 break;
98 case SDL_WINDOWEVENT_SIZE_CHANGED:
99 io.DisplaySize.x = static_cast<float>(event.window.data1);
100 io.DisplaySize.y = static_cast<float>(event.window.data2);
101 break;
102 default:
103 break;
104 }
105 break;
106 default:
107 break;
108 }
109 }
110
111 int mouseX;
112 int mouseY;
113 const int buttons = SDL_GetMouseState(&mouseX, &mouseY);
114
115 io.DeltaTime = 1.0f / 60.0f;
116 io.MousePos = ImVec2(static_cast<float>(mouseX), static_cast<float>(mouseY));
117 io.MouseDown[0] = buttons & SDL_BUTTON(SDL_BUTTON_LEFT);
118 io.MouseDown[1] = buttons & SDL_BUTTON(SDL_BUTTON_RIGHT);
119 io.MouseDown[2] = buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE);
120 io.MouseWheel = static_cast<float>(wheel);
121}
122
123absl::Status Controller::OnLoad() {
124 if (editor_manager_.quit()) {
125 active_ = false;
126 }
127#if TARGET_OS_IPHONE != 1
128 if (platform_ != Platform::kiOS) {
129 NewMasterFrame();
130 }
131#endif
133#if TARGET_OS_IPHONE != 1
134 if (platform_ != Platform::kiOS) {
135 End();
136 }
137#endif
138 return absl::OkStatus();
139}
140
143 return absl::OkStatus();
144}
145
147 ImGui::Render();
148 SDL_RenderClear(Renderer::GetInstance().renderer());
149 ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(),
151 SDL_RenderPresent(Renderer::GetInstance().renderer());
152}
153
155 SDL_PauseAudioDevice(audio_device_, 1);
156 SDL_CloseAudioDevice(audio_device_);
157 ImGui_ImplSDLRenderer2_Shutdown();
158 ImGui_ImplSDL2_Shutdown();
159 ImGui::DestroyContext();
160 SDL_Quit();
161}
162
164 auto sdl_flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
165 if (flags()->kUseNewImGuiInput) {
166 sdl_flags |= SDL_INIT_GAMECONTROLLER;
167 }
168
169 if (SDL_Init(sdl_flags) != 0) {
170 return absl::InternalError(
171 absl::StrFormat("SDL_Init: %s\n", SDL_GetError()));
172 }
173
174 SDL_DisplayMode display_mode;
175 SDL_GetCurrentDisplayMode(0, &display_mode);
176 int screen_width = display_mode.w * 0.8;
177 int screen_height = display_mode.h * 0.8;
178
179 window_ = std::unique_ptr<SDL_Window, core::SDL_Deleter>(
180 SDL_CreateWindow("Yet Another Zelda3 Editor", // window title
181 SDL_WINDOWPOS_UNDEFINED, // initial x position
182 SDL_WINDOWPOS_UNDEFINED, // initial y position
183 screen_width, // width, in pixels
184 screen_height, // height, in pixels
185 SDL_WINDOW_RESIZABLE),
187 if (window_ == nullptr) {
188 return absl::InternalError(
189 absl::StrFormat("SDL_CreateWindow: %s\n", SDL_GetError()));
190 }
191
192 return absl::OkStatus();
193}
194
198
200 IMGUI_CHECKVERSION();
201 ImGui::CreateContext();
202
203 ImGuiIO &io = ImGui::GetIO();
204 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
205 if (flags()->kUseNewImGuiInput) {
206 io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
207 }
208
209 // Initialize ImGui based on the backend
210 ImGui_ImplSDL2_InitForSDLRenderer(window_.get(),
212 ImGui_ImplSDLRenderer2_Init(Renderer::GetInstance().renderer());
213
215
216 // Set the default style
218
219 // Build a new ImGui frame
220 ImGui_ImplSDLRenderer2_NewFrame();
221 ImGui_ImplSDL2_NewFrame();
222
223 return absl::OkStatus();
224}
225
226absl::Status Controller::LoadFontFamilies() const {
227 // LoadSystemFonts();
228 return LoadPackageFonts();
229}
230
232 SDL_AudioSpec want, have;
233 SDL_memset(&want, 0, sizeof(want));
234 want.freq = audio_frequency_;
235 want.format = AUDIO_S16;
236 want.channels = 2;
237 want.samples = 2048;
238 want.callback = NULL; // Uses the queue
239 audio_device_ = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
240 if (audio_device_ == 0) {
241 return absl::InternalError(
242 absl::StrFormat("Failed to open audio: %s\n", SDL_GetError()));
243 }
244 // audio_buffer_ = new int16_t[audio_frequency_ / 50 * 4];
245 audio_buffer_ = std::make_shared<int16_t>(audio_frequency_ / 50 * 4);
246 SDL_PauseAudioDevice(audio_device_, 0);
247 editor_manager_.emulator().set_audio_buffer(audio_buffer_.get());
248 editor_manager_.emulator().set_audio_device_id(audio_device_);
249 return absl::OkStatus();
250}
251
253 // Create and load a dotfile for the application
254 // This will store the user's preferences and settings
255 std::string config_directory = GetConfigDirectory(platform_);
256
257 // Create the directory if it doesn't exist
258 if (!std::filesystem::exists(config_directory)) {
259 if (!std::filesystem::create_directory(config_directory)) {
260 return absl::InternalError(absl::StrFormat(
261 "Failed to create config directory %s", config_directory));
262 }
263 }
264
265 // Check if the config file exists
266 std::string config_file = config_directory + "yaze.cfg";
267 if (!std::filesystem::exists(config_file)) {
268 // Create the file if it doesn't exist
269 std::ofstream file(config_file);
270 if (!file.is_open()) {
271 return absl::InternalError(
272 absl::StrFormat("Failed to create config file %s", config_file));
273 }
274 file.close();
275 }
276
277 return absl::OkStatus();
278}
279
280} // namespace core
281} // namespace app
282} // namespace yaze
SDL_AudioDeviceID audio_device_
Definition controller.h:67
absl::Status CreateGuiContext()
editor::EditorManager editor_manager_
Definition controller.h:64
absl::Status LoadFontFamilies() const
absl::Status LoadAudioDevice()
std::shared_ptr< SDL_Window > window_
Definition controller.h:69
absl::Status OnEntry(std::string filename="")
Definition controller.cc:53
absl::Status LoadConfigFiles()
editor::Editor * test_editor_
Definition controller.h:63
auto renderer() -> SDL_Renderer *
Definition controller.h:51
absl::Status CreateRenderer()
std::shared_ptr< int16_t > audio_buffer_
Definition controller.h:68
absl::Status CreateRenderer(SDL_Window *window)
Definition renderer.h:32
auto renderer() -> SDL_Renderer *
Definition renderer.h:44
static Renderer & GetInstance()
Definition renderer.h:27
void SetupScreen(std::string filename="")
auto emulator() -> emu::Emulator &
virtual absl::Status Update()=0
#define RETURN_IF_ERROR(expression)
Definition constants.h:62
std::string GetConfigDirectory(Platform platform)
Definition file_util.cc:56
absl::Status LoadPackageFonts()
void ColorsYaze()
Definition style.cc:431
constexpr ImVec2 kZeroPos
Definition input.h:28
Definition common.cc:22
Deleter for SDL_Window and SDL_Renderer.
Definition sdl_deleter.h:13