dingusppc/devices/video/display_sdl.cpp
Mihai Parparita 1f7edfdb3b Make Emscripten build not depend on SDL2 or cubeb
While Emscripten has an SDL compabtility layer, it assumes that the
code is executing in the main browser process (and thus has access to
them DOM). The Infinite Mac project runs emulators in a worker thread
(for better performance) and has a custom API for the display, sound,
input, etc. Similarly, it does not need the cross-platform sound support
from cubeb, there there is a sound API as well.

This commit makes SDL (*_sdl.cpp) and cubeb-based (*_cubeb.cpp) code be
skipped when targeting Emscripten, and instead *_js.cpp files are used
instead (this is the cross-platform convention used by Chromium[^1], and
could be extended for other targets).

For hostevents.cpp and soundserver.cpp the entire file was replaced,
whereas for videoctrl.cpp there was enough shared logic that it was
kept, and the platform-specific bits were moved behind a Display class
that can have per-platform implementations. For cases where we need
additional private fields in the platform-specific classes, we use
a PIMPL pattern.

The *_js.cpp files with implementations are not included in this
commit, since they are closely tied to the Infinite Mac project, and
will live in its fork of DingusPPC.

[^1]: https://www.chromium.org/developers/design-documents/conventions-and-patterns-for-multi-platform-development/
2023-10-25 22:25:53 -07:00

158 lines
4.3 KiB
C++

#include <devices/video/display.h>
#include <SDL.h>
#include <loguru.hpp>
class Display::Impl {
public:
bool resizing = false;
uint32_t disp_wnd_id = 0;
SDL_Window* display_wnd = 0;
SDL_Renderer* renderer = 0;
SDL_Texture* disp_texture = 0;
SDL_Texture* cursor_texture = 0;
SDL_Rect cursor_rect; // destination rectangle for cursor drawing
};
Display::Display(): impl(std::make_unique<Impl>())
{
}
Display::~Display()
{
if (impl->cursor_texture) {
SDL_DestroyTexture(impl->cursor_texture);
}
if (impl->disp_texture) {
SDL_DestroyTexture(impl->disp_texture);
}
if (impl->renderer) {
SDL_DestroyRenderer(impl->renderer);
}
if (impl->display_wnd) {
SDL_DestroyWindow(impl->display_wnd);
}
}
bool Display::configure(int width, int height)
{
bool is_initialization = false;
if (!impl->display_wnd) { // create display window
impl->display_wnd = SDL_CreateWindow(
"DingusPPC Display",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
width, height,
SDL_WINDOW_OPENGL
);
impl->disp_wnd_id = SDL_GetWindowID(impl->display_wnd);
if (impl->display_wnd == NULL) {
ABORT_F("Display: SDL_CreateWindow failed with %s", SDL_GetError());
}
impl->renderer = SDL_CreateRenderer(impl->display_wnd, -1, SDL_RENDERER_ACCELERATED);
if (impl->renderer == NULL) {
ABORT_F("Display: SDL_CreateRenderer failed with %s", SDL_GetError());
}
is_initialization = true;
} else { // resize display window
SDL_SetWindowSize(impl->display_wnd, width, height);
impl->resizing = true;
}
if (impl->disp_texture) {
SDL_DestroyTexture(impl->disp_texture);
}
impl->disp_texture = SDL_CreateTexture(
impl->renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
width, height
);
if (impl->disp_texture == NULL) {
ABORT_F("Display: SDL_CreateTexture failed with %s", SDL_GetError());
}
return is_initialization;
}
void Display::handle_events(const WindowEvent& wnd_event)
{
if (wnd_event.sub_type == SDL_WINDOWEVENT_SIZE_CHANGED &&
wnd_event.window_id == impl->disp_wnd_id)
impl->resizing = false;
}
void Display::blank()
{
SDL_SetRenderDrawColor(impl->renderer, 0, 0, 0, 255);
SDL_RenderClear(impl->renderer);
SDL_RenderPresent(impl->renderer);
}
void Display::update(std::function<void(uint8_t *dst_buf, int dst_pitch)> convert_fb_cb, bool draw_hw_cursor, int cursor_x, int cursor_y)
{
if (impl->resizing)
return;
uint8_t* dst_buf;
int dst_pitch;
SDL_LockTexture(impl->disp_texture, NULL, (void **)&dst_buf, &dst_pitch);
// texture update callback to get ARGB data from guest framebuffer
convert_fb_cb(dst_buf, dst_pitch);
SDL_UnlockTexture(impl->disp_texture);
SDL_RenderClear(impl->renderer);
SDL_RenderCopy(impl->renderer, impl->disp_texture, NULL, NULL);
// draw HW cursor if enabled
if (draw_hw_cursor) {
impl->cursor_rect.x = cursor_x;
impl->cursor_rect.y = cursor_y;
SDL_RenderCopy(impl->renderer, impl->cursor_texture, NULL, &impl->cursor_rect);
}
SDL_RenderPresent(impl->renderer);
}
void Display::setup_hw_cursor(std::function<void(uint8_t *dst_buf, int dst_pitch)> draw_hw_cursor, int cursor_width, int cursor_height)
{
uint8_t* dst_buf;
int dst_pitch;
if (impl->cursor_texture) {
SDL_DestroyTexture(impl->cursor_texture);
}
impl->cursor_texture = SDL_CreateTexture(
impl->renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
cursor_width, cursor_height
);
if (impl->cursor_texture == NULL) {
ABORT_F("SDL_CreateTexture for HW cursor failed with %s", SDL_GetError());
}
SDL_LockTexture(impl->cursor_texture, NULL, (void **)&dst_buf, &dst_pitch);
SDL_SetTextureBlendMode(impl->cursor_texture, SDL_BLENDMODE_BLEND);
draw_hw_cursor(dst_buf, dst_pitch);
SDL_UnlockTexture(impl->cursor_texture);
impl->cursor_rect.x = 0;
impl->cursor_rect.y = 0;
impl->cursor_rect.w = cursor_width;
impl->cursor_rect.h = cursor_height;
}