yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
backend.h
Go to the documentation of this file.
1#ifndef YAZE_APP_CORE_PLATFORM_BACKEND_H
2#define YAZE_APP_CORE_PLATFORM_BACKEND_H
3
4#include <SDL.h>
5
6#include <memory>
7#include <stdexcept>
8
11#include "imgui/backends/imgui_impl_sdl2.h"
12#include "imgui/backends/imgui_impl_sdlrenderer2.h"
13
14namespace yaze {
15namespace core {
16
17template <typename Derived>
19 public:
20 PlatformBackend() = default;
21 ~PlatformBackend() = default;
22
23 void init() { static_cast<Derived *>(this)->init(); }
24 void init_audio() { static_cast<Derived *>(this)->init_audio(); }
25 void shutdown_audio() { static_cast<Derived *>(this)->shutdown_audio(); }
26 void shutdown() { static_cast<Derived *>(this)->shutdown(); }
27 void new_frame() { static_cast<Derived *>(this)->new_frame(); }
28 void render() { static_cast<Derived *>(this)->render(); }
29};
30
31class Sdl2Backend : public PlatformBackend<Sdl2Backend> {
32 public:
33 Sdl2Backend() = default;
34 ~Sdl2Backend() = default;
35
36 void init() {
37 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) !=
38 0) {
39 throw std::runtime_error("Failed to initialize SDL2");
40 }
41
42 SDL_DisplayMode display_mode;
43 SDL_GetCurrentDisplayMode(0, &display_mode);
44 int screen_width = display_mode.w * 0.8;
45 int screen_height = display_mode.h * 0.8;
46
47 window = std::unique_ptr<SDL_Window, core::SDL_Deleter>(
48 SDL_CreateWindow("Yet Another Zelda3 Editor", // window title
49 SDL_WINDOWPOS_UNDEFINED, // initial x position
50 SDL_WINDOWPOS_UNDEFINED, // initial y position
51 screen_width, // width, in pixels
52 screen_height, // height, in pixels
53 SDL_WINDOW_RESIZABLE),
55 if (!window) {
56 throw std::runtime_error("Failed to create window");
57 }
58
59 if (!Renderer::GetInstance().CreateRenderer(window.get()).ok()) {
60 throw std::runtime_error("Failed to create renderer");
61 }
62
63 ImGui_ImplSDL2_InitForSDLRenderer(window.get(),
64 Renderer::GetInstance().renderer());
65 ImGui_ImplSDLRenderer2_Init(Renderer::GetInstance().renderer());
66 }
67
68 void init_audio() {
69 const int audio_frequency = 48000;
70 SDL_AudioSpec want, have;
71 SDL_memset(&want, 0, sizeof(want));
72 want.freq = audio_frequency;
73 want.format = AUDIO_S16;
74 want.channels = 2;
75 want.samples = 2048;
76 want.callback = NULL; // Uses the queue
77 audio_device_ = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
78 if (audio_device_ == 0) {
79 throw std::runtime_error(
80 absl::StrFormat("Failed to open audio: %s\n", SDL_GetError()));
81 }
82 audio_buffer_ = std::make_shared<int16_t>(audio_frequency / 50 * 4);
83 SDL_PauseAudioDevice(audio_device_, 0);
84 }
85
87 SDL_PauseAudioDevice(audio_device_, 1);
88 SDL_CloseAudioDevice(audio_device_);
89 }
90
91 void shutdown() {
92 ImGui_ImplSDL2_Shutdown();
93 ImGui_ImplSDLRenderer2_Shutdown();
94 SDL_DestroyRenderer(Renderer::GetInstance().renderer());
95 SDL_DestroyWindow(window.get());
96 SDL_Quit();
97 }
98
99 void new_frame() {
100 ImGui_ImplSDLRenderer2_NewFrame();
101 ImGui_ImplSDL2_NewFrame();
102 }
103
104 void render() {
105 ImGui::Render();
106 SDL_RenderClear(Renderer::GetInstance().renderer());
107 ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(),
108 Renderer::GetInstance().renderer());
109 SDL_RenderPresent(Renderer::GetInstance().renderer());
110 }
111
112 auto window_ptr() -> SDL_Window * { return window.get(); }
113 auto audio_device() -> SDL_AudioDeviceID { return audio_device_; }
114 auto audio_buffer() -> std::shared_ptr<int16_t> { return audio_buffer_; }
115
116 private:
117 SDL_AudioDeviceID audio_device_;
118 std::shared_ptr<int16_t> audio_buffer_;
119 std::unique_ptr<SDL_Window, SDL_Deleter> window;
120};
121
122} // namespace core
123} // namespace yaze
124
125#endif // YAZE_APP_CORE_PLATFORM_BACKEND_H
static Renderer & GetInstance()
Definition renderer.h:26
auto audio_buffer() -> std::shared_ptr< int16_t >
Definition backend.h:114
SDL_AudioDeviceID audio_device_
Definition backend.h:117
std::unique_ptr< SDL_Window, SDL_Deleter > window
Definition backend.h:119
auto audio_device() -> SDL_AudioDeviceID
Definition backend.h:113
std::shared_ptr< int16_t > audio_buffer_
Definition backend.h:118
auto window_ptr() -> SDL_Window *
Definition backend.h:112
Main namespace for the application.
Definition controller.cc:18
Deleter for SDL_Window and SDL_Renderer.
Definition sdl_deleter.h:12