yaze 0.2.2
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 core {
20
21absl::Status Controller::OnEntry(std::string filename) {
22#if defined(__APPLE__) && defined(__MACH__)
23#if TARGET_IPHONE_SIMULATOR == 1 || TARGET_OS_IPHONE == 1
25#elif TARGET_OS_MAC == 1
27#endif
28#elif defined(_WIN32)
30#elif defined(__linux__)
32#else
34#endif
39 editor_manager_.Initialize(filename);
40 active_ = true;
41 return absl::OkStatus();
42}
43
45 int wheel = 0;
46 SDL_Event event;
47 ImGuiIO &io = ImGui::GetIO();
48
49 while (SDL_PollEvent(&event)) {
50 ImGui_ImplSDL2_ProcessEvent(&event);
51 switch (event.type) {
52 case SDL_KEYDOWN:
53 case SDL_KEYUP: {
54 ImGuiIO &io = ImGui::GetIO();
55 io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
56 io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
57 io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
58 io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
59 break;
60 }
61 case SDL_WINDOWEVENT:
62 switch (event.window.event) {
63 case SDL_WINDOWEVENT_CLOSE:
64 active_ = false;
65 break;
66 case SDL_WINDOWEVENT_SIZE_CHANGED:
67 io.DisplaySize.x = static_cast<float>(event.window.data1);
68 io.DisplaySize.y = static_cast<float>(event.window.data2);
69 break;
70 default:
71 break;
72 }
73 break;
74 default:
75 break;
76 }
77 }
78
79 int mouseX;
80 int mouseY;
81 const int buttons = SDL_GetMouseState(&mouseX, &mouseY);
82
83 io.DeltaTime = 1.0f / 60.0f;
84 io.MousePos = ImVec2(static_cast<float>(mouseX), static_cast<float>(mouseY));
85 io.MouseDown[0] = buttons & SDL_BUTTON(SDL_BUTTON_LEFT);
86 io.MouseDown[1] = buttons & SDL_BUTTON(SDL_BUTTON_RIGHT);
87 io.MouseDown[2] = buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE);
88 io.MouseWheel = static_cast<float>(wheel);
89}
90
91absl::Status Controller::OnLoad() {
92 if (editor_manager_.quit()) {
93 active_ = false;
94 }
95#if TARGET_OS_IPHONE != 1
96 constexpr ImGuiWindowFlags kMainEditorFlags =
97 ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoCollapse |
98 ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_MenuBar |
99 ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoTitleBar;
100
101 const ImGuiIO &io = ImGui::GetIO();
102 ImGui_ImplSDLRenderer2_NewFrame();
103 ImGui_ImplSDL2_NewFrame();
104 ImGui::NewFrame();
105 ImGui::SetNextWindowPos(gui::kZeroPos);
106 ImVec2 dimensions(io.DisplaySize.x, io.DisplaySize.y);
107 ImGui::SetNextWindowSize(dimensions, ImGuiCond_Always);
108
109 if (!ImGui::Begin("##YazeMain", nullptr, kMainEditorFlags)) {
110 ImGui::End();
111 }
112#endif
114#if TARGET_OS_IPHONE != 1
115 ImGui::End();
116#endif
117 return absl::OkStatus();
118}
119
121 RETURN_IF_ERROR(test_editor_->Update());
122 return absl::OkStatus();
123}
124
126 ImGui::Render();
127 SDL_RenderClear(Renderer::GetInstance().renderer());
128 ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(),
130 SDL_RenderPresent(Renderer::GetInstance().renderer());
131}
132
134 SDL_PauseAudioDevice(audio_device_, 1);
135 SDL_CloseAudioDevice(audio_device_);
136 ImGui_ImplSDLRenderer2_Shutdown();
137 ImGui_ImplSDL2_Shutdown();
138 ImGui::DestroyContext();
139 SDL_Quit();
140}
141
143 auto sdl_flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
144
145 if (SDL_Init(sdl_flags) != 0) {
146 return absl::InternalError(
147 absl::StrFormat("SDL_Init: %s\n", SDL_GetError()));
148 }
149
150 SDL_DisplayMode display_mode;
151 SDL_GetCurrentDisplayMode(0, &display_mode);
152 int screen_width = display_mode.w * 0.8;
153 int screen_height = display_mode.h * 0.8;
154
155 window_ = std::unique_ptr<SDL_Window, core::SDL_Deleter>(
156 SDL_CreateWindow("Yet Another Zelda3 Editor", // window title
157 SDL_WINDOWPOS_UNDEFINED, // initial x position
158 SDL_WINDOWPOS_UNDEFINED, // initial y position
159 screen_width, // width, in pixels
160 screen_height, // height, in pixels
161 SDL_WINDOW_RESIZABLE),
163 if (window_ == nullptr) {
164 return absl::InternalError(
165 absl::StrFormat("SDL_CreateWindow: %s\n", SDL_GetError()));
166 }
167
168 return absl::OkStatus();
169}
170
174
176 IMGUI_CHECKVERSION();
177 ImGui::CreateContext();
178
179 ImGuiIO &io = ImGui::GetIO();
180 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
181
182 // Initialize ImGui based on the backend
183 ImGui_ImplSDL2_InitForSDLRenderer(window_.get(),
184 Renderer::GetInstance().renderer());
185 ImGui_ImplSDLRenderer2_Init(Renderer::GetInstance().renderer());
186
188
189 // Set the default style
191
192 // Build a new ImGui frame
193 ImGui_ImplSDLRenderer2_NewFrame();
194 ImGui_ImplSDL2_NewFrame();
195
196 return absl::OkStatus();
197}
198
199absl::Status Controller::LoadFontFamilies() const {
200 // LoadSystemFonts();
201 return LoadPackageFonts();
202}
203
205 SDL_AudioSpec want, have;
206 SDL_memset(&want, 0, sizeof(want));
207 want.freq = audio_frequency_;
208 want.format = AUDIO_S16;
209 want.channels = 2;
210 want.samples = 2048;
211 want.callback = NULL; // Uses the queue
212 audio_device_ = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
213 if (audio_device_ == 0) {
214 return absl::InternalError(
215 absl::StrFormat("Failed to open audio: %s\n", SDL_GetError()));
216 }
217 // audio_buffer_ = new int16_t[audio_frequency_ / 50 * 4];
218 audio_buffer_ = std::make_shared<int16_t>(audio_frequency_ / 50 * 4);
219 SDL_PauseAudioDevice(audio_device_, 0);
220 editor_manager_.emulator().set_audio_buffer(audio_buffer_.get());
221 editor_manager_.emulator().set_audio_device_id(audio_device_);
222 return absl::OkStatus();
223}
224
226 // Create and load a dotfile for the application
227 // This will store the user's preferences and settings
228 std::string config_directory = GetConfigDirectory(platform_);
229
230 // Create the directory if it doesn't exist
231 if (!std::filesystem::exists(config_directory)) {
232 if (!std::filesystem::create_directory(config_directory)) {
233 return absl::InternalError(absl::StrFormat(
234 "Failed to create config directory %s", config_directory));
235 }
236 }
237
238 // Check if the config file exists
239 std::string config_file = config_directory + "yaze.cfg";
240 if (!std::filesystem::exists(config_file)) {
241 // Create the file if it doesn't exist
242 std::ofstream file(config_file);
243 if (!file.is_open()) {
244 return absl::InternalError(
245 absl::StrFormat("Failed to create config file %s", config_file));
246 }
247 file.close();
248 }
249
250 return absl::OkStatus();
251}
252
253} // namespace core
254} // namespace yaze
absl::Status OnLoad()
Definition controller.cc:91
absl::Status CreateWindow()
absl::Status LoadAudioDevice()
SDL_AudioDeviceID audio_device_
Definition controller.h:67
absl::Status CreateGuiContext()
absl::Status LoadFontFamilies() const
absl::Status OnTestLoad()
auto renderer() -> SDL_Renderer *
Definition controller.h:50
absl::Status OnEntry(std::string filename="")
Definition controller.cc:21
editor::EditorManager editor_manager_
Definition controller.h:64
std::shared_ptr< SDL_Window > window_
Definition controller.h:69
absl::Status CreateRenderer()
editor::Editor * test_editor_
Definition controller.h:63
absl::Status LoadConfigFiles()
std::shared_ptr< int16_t > audio_buffer_
Definition controller.h:68
static Renderer & GetInstance()
Definition renderer.h:26
absl::Status CreateRenderer(SDL_Window *window)
Definition renderer.h:31
#define RETURN_IF_ERROR(expression)
Definition macro.h:62
absl::Status LoadPackageFonts()
std::string GetConfigDirectory(Platform platform)
constexpr ImVec2 kZeroPos
Definition input.h:22
void ColorsYaze()
Definition style.cc:117
Main namespace for the application.
Definition controller.cc:18
Deleter for SDL_Window and SDL_Renderer.
Definition sdl_deleter.h:12