Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding backend sources for SDL3 #556

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 199 additions & 0 deletions Backends/RmlUi_Backend_SDL3_SDLrenderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
* This source file is part of RmlUi, the HTML/CSS Interface Middleware
*
* For the latest information, see http://github.com/mikke89/RmlUi
*
* Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
* Copyright (c) 2019-2023 The RmlUi Team, and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

#include "RmlUi_Backend.h"
#include "RmlUi_Platform_SDL.h"
#include "RmlUi_Renderer_SDL.h"
#include <RmlUi/Core/Context.h>
#include <RmlUi/Core/Core.h>
#include <RmlUi/Core/Log.h>
#include <SDL.h>

/**
Global data used by this backend.

Lifetime governed by the calls to Backend::Initialize() and Backend::Shutdown().
*/
struct BackendData {
BackendData(SDL_Renderer* renderer) : render_interface(renderer) {}

SystemInterface_SDL system_interface;
RenderInterface_SDL render_interface;

SDL_Window* window = nullptr;
SDL_Renderer* renderer = nullptr;

bool running = true;
};
static Rml::UniquePtr<BackendData> data;

bool Backend::Initialize(const char* window_name, int width, int height, bool allow_resize)
{
RMLUI_ASSERT(!data);

if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0)
return false;

// Submit click events when focusing the window.
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");

const Uint32 window_flags = (allow_resize ? SDL_WINDOW_RESIZABLE : 0);
SDL_Window* window = SDL_CreateWindow(window_name, width, height, window_flags);
if (!window)
{
Rml::Log::Message(Rml::Log::LT_ERROR, "SDL error on create window: %s\n", SDL_GetError());
return false;
}

/*
* Force a specific back-end
SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1");
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software");
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengles2");
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "direct3d");
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
*/

SDL_Renderer* renderer = SDL_CreateRenderer(window, nullptr, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer)
return false;

data = Rml::MakeUnique<BackendData>(renderer);

data->window = window;
data->renderer = renderer;

data->system_interface.SetWindow(window);

SDL_RendererInfo renderer_info;
if (SDL_GetRendererInfo(renderer, &renderer_info) == 0)
{
data->system_interface.LogMessage(Rml::Log::LT_INFO, Rml::CreateString(128, "Using SDL renderer: %s\n", renderer_info.name));
}

return true;
}

void Backend::Shutdown()
{
RMLUI_ASSERT(data);

SDL_DestroyRenderer(data->renderer);
SDL_DestroyWindow(data->window);

data.reset();

SDL_Quit();
}

Rml::SystemInterface* Backend::GetSystemInterface()
{
RMLUI_ASSERT(data);
return &data->system_interface;
}

Rml::RenderInterface* Backend::GetRenderInterface()
{
RMLUI_ASSERT(data);
return &data->render_interface;
}

bool Backend::ProcessEvents(Rml::Context* context, KeyDownCallback key_down_callback, bool power_save)
{
RMLUI_ASSERT(data && context);

bool result = data->running;
SDL_Event ev;

int has_event = 0;
if (power_save)
has_event = SDL_WaitEventTimeout(&ev, static_cast<int>(Rml::Math::Min(context->GetNextUpdateDelay(), 10.0) * 1000));
else
has_event = SDL_PollEvent(&ev);
while (has_event)
{
switch (ev.type)
{
case SDL_EVENT_QUIT:
{
result = false;
}
break;
case SDL_EVENT_KEY_DOWN:
{
const Rml::Input::KeyIdentifier key = RmlSDL::ConvertKey(ev.key.keysym.sym);
const int key_modifier = RmlSDL::GetKeyModifierState();
const float native_dp_ratio = 1.f;

// See if we have any global shortcuts that take priority over the context.
if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, true))
break;
// Otherwise, hand the event over to the context by calling the input handler as normal.
if (!RmlSDL::InputEventHandler(context, ev))
break;
// The key was not consumed by the context either, try keyboard shortcuts of lower priority.
if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, false))
break;
}
break;
default:
{
RmlSDL::InputEventHandler(context, ev);
}
break;
}
has_event = SDL_PollEvent(&ev);
}

return result;
}

void Backend::RequestExit()
{
RMLUI_ASSERT(data);

data->running = false;
}

void Backend::BeginFrame()
{
RMLUI_ASSERT(data);

SDL_SetRenderDrawColor(data->renderer, 0, 0, 0, 0);
SDL_RenderClear(data->renderer);

data->render_interface.BeginFrame();
}

void Backend::PresentFrame()
{
RMLUI_ASSERT(data);

data->render_interface.EndFrame();
SDL_RenderPresent(data->renderer);
}
Loading