33int main(
int argc,
char **argv) {
34 absl::InitializeSymbolizer(argv[0]);
36 absl::FailureSignalHandlerOptions options;
37 options.symbolize_stacktrace =
true;
38 options.alarm_on_failure_secs =
true;
39 absl::InstallFailureSignalHandler(options);
41 std::unique_ptr<SDL_Window, SDL_Deleter> window_;
42 std::unique_ptr<SDL_Renderer, SDL_Deleter> renderer_;
43 if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
46 SDL_DisplayMode displayMode;
47 SDL_GetCurrentDisplayMode(0, &displayMode);
48 window_ = std::unique_ptr<SDL_Window, SDL_Deleter>(
49 SDL_CreateWindow(
"Yaze Emulator",
50 SDL_WINDOWPOS_UNDEFINED,
51 SDL_WINDOWPOS_UNDEFINED,
54 SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI),
56 if (window_ ==
nullptr) {
61 renderer_ = std::unique_ptr<SDL_Renderer, SDL_Deleter>(
62 SDL_CreateRenderer(window_.get(), -1,
63 SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
65 if (renderer_ ==
nullptr) {
68 SDL_SetRenderDrawBlendMode(renderer_.get(), SDL_BLENDMODE_BLEND);
69 SDL_SetRenderDrawColor(renderer_.get(), 0x00, 0x00, 0x00, 0x00);
72 int audio_frequency_ = 48000;
73 SDL_AudioSpec want, have;
74 SDL_memset(&want, 0,
sizeof(want));
75 want.freq = audio_frequency_;
76 want.format = AUDIO_S16;
80 auto audio_device_ = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
81 if (audio_device_ == 0) {
84 auto audio_buffer_ =
new int16_t[audio_frequency_ / 50 * 4];
85 SDL_PauseAudioDevice(audio_device_, 0);
88 yaze_initialize_cocoa();
92 SDL_CreateTexture(renderer_.get(), SDL_PIXELFORMAT_RGBX8888,
93 SDL_TEXTUREACCESS_STREAMING, 512, 480);
94 if (ppu_texture_ == NULL) {
95 printf(
"Failed to create texture: %s\n", SDL_GetError());
101 std::vector<uint8_t> rom_data_;
105 auto count_frequency = SDL_GetPerformanceFrequency();
106 auto last_count = SDL_GetPerformanceCounter();
107 auto time_adder = 0.0;
108 int wanted_frames_ = 0;
109 int wanted_samples_ = 0;
112 if (!rom_.
LoadFromFile(
"inidisp_hammer_0f00.sfc").ok()) {
117 rom_data_ = rom_.
vector();
118 snes_.
Init(rom_data_);
119 wanted_frames_ = 1.0 / (snes_.
Memory().pal_timing() ? 50.0 : 60.0);
120 wanted_samples_ = 48000 / (snes_.
Memory().pal_timing() ? 50 : 60);
125 while (SDL_PollEvent(&event)) {
126 switch (event.type) {
130 rom_data_ = rom_.
vector();
131 snes_.
Init(rom_data_);
132 wanted_frames_ = 1.0 / (snes_.
Memory().pal_timing() ? 50.0 : 60.0);
133 wanted_samples_ = 48000 / (snes_.
Memory().pal_timing() ? 50 : 60);
136 SDL_free(event.drop.file);
142 case SDL_WINDOWEVENT:
143 switch (event.window.event) {
144 case SDL_WINDOWEVENT_CLOSE:
147 case SDL_WINDOWEVENT_SIZE_CHANGED:
158 uint64_t current_count = SDL_GetPerformanceCounter();
159 uint64_t delta = current_count - last_count;
160 last_count = current_count;
161 float seconds = delta / (float)count_frequency;
162 time_adder += seconds;
164 while (time_adder >= wanted_frames_ - 0.002) {
165 time_adder -= wanted_frames_;
170 snes_.
SetSamples(audio_buffer_, wanted_samples_);
171 if (SDL_GetQueuedAudioSize(audio_device_) <= wanted_samples_ * 4 * 6) {
172 SDL_QueueAudio(audio_device_, audio_buffer_, wanted_samples_ * 4);
177 if (SDL_LockTexture(ppu_texture_, NULL, &ppu_pixels_, &ppu_pitch_) !=
179 printf(
"Failed to lock texture: %s\n", SDL_GetError());
182 snes_.
SetPixels(
static_cast<uint8_t *
>(ppu_pixels_));
183 SDL_UnlockTexture(ppu_texture_);
187 SDL_RenderClear(renderer_.get());
188 SDL_RenderCopy(renderer_.get(), ppu_texture_, NULL, NULL);
189 SDL_RenderPresent(renderer_.get());
192 SDL_PauseAudioDevice(audio_device_, 1);
193 SDL_CloseAudioDevice(audio_device_);
194 delete audio_buffer_;