Adding current renderer from gamejam project
This commit is contained in:
33
src/backend/RaylibInput.cpp
Normal file
33
src/backend/RaylibInput.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "RaylibInput.h"
|
||||
#include <raylib.h>
|
||||
|
||||
namespace {
|
||||
int GetRmlKeyModifiers() {
|
||||
int mods = 0;
|
||||
if (IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT))
|
||||
mods |= Rml::Input::KM_SHIFT;
|
||||
if (IsKeyDown(KEY_LEFT_CONTROL) || IsKeyDown(KEY_RIGHT_CONTROL))
|
||||
mods |= Rml::Input::KM_CTRL;
|
||||
if (IsKeyDown(KEY_LEFT_ALT) || IsKeyDown(KEY_RIGHT_ALT))
|
||||
mods |= Rml::Input::KM_ALT;
|
||||
return mods;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void ForwardInputToRml(Rml::Context *context) {
|
||||
Vector2 mouse = GetMousePosition();
|
||||
context->ProcessMouseMove((int)mouse.x, (int)mouse.y, GetRmlKeyModifiers());
|
||||
|
||||
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||
context->ProcessMouseButtonDown(0, GetRmlKeyModifiers());
|
||||
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
|
||||
context->ProcessMouseButtonUp(0, GetRmlKeyModifiers());
|
||||
if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT))
|
||||
context->ProcessMouseButtonDown(1, GetRmlKeyModifiers());
|
||||
if (IsMouseButtonReleased(MOUSE_BUTTON_RIGHT))
|
||||
context->ProcessMouseButtonUp(1, GetRmlKeyModifiers());
|
||||
|
||||
float wheel = GetMouseWheelMove();
|
||||
if (wheel != 0.0f)
|
||||
context->ProcessMouseWheel(-wheel, GetRmlKeyModifiers());
|
||||
}
|
||||
4
src/backend/RaylibInput.h
Normal file
4
src/backend/RaylibInput.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
#include <RmlUi/Core/Context.h>
|
||||
|
||||
void ForwardInputToRml(Rml::Context *context);
|
||||
116
src/backend/RaylibRenderInterface.cpp
Normal file
116
src/backend/RaylibRenderInterface.cpp
Normal file
@@ -0,0 +1,116 @@
|
||||
#include "RaylibRenderInterface.h"
|
||||
|
||||
#include <raylib.h>
|
||||
#include <rlgl.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
struct GeometryData {
|
||||
std::vector<Rml::Vertex> vertices;
|
||||
std::vector<int> indices;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
Rml::CompiledGeometryHandle
|
||||
RaylibRenderInterface::CompileGeometry(Rml::Span<const Rml::Vertex> vertices,
|
||||
Rml::Span<const int> indices) {
|
||||
auto *geo = new GeometryData();
|
||||
geo->vertices.assign(vertices.begin(), vertices.end());
|
||||
geo->indices.assign(indices.begin(), indices.end());
|
||||
return reinterpret_cast<Rml::CompiledGeometryHandle>(geo);
|
||||
}
|
||||
|
||||
void RaylibRenderInterface::RenderGeometry(Rml::CompiledGeometryHandle handle,
|
||||
Rml::Vector2f translation,
|
||||
Rml::TextureHandle texture) {
|
||||
auto *geo = reinterpret_cast<GeometryData *>(handle);
|
||||
|
||||
if (texture) {
|
||||
Texture2D *tex = reinterpret_cast<Texture2D *>(texture);
|
||||
rlSetTexture(tex->id);
|
||||
} else {
|
||||
rlSetTexture(rlGetTextureIdDefault());
|
||||
}
|
||||
|
||||
rlPushMatrix();
|
||||
rlTranslatef(translation.x, translation.y, 0.0f);
|
||||
|
||||
rlBegin(RL_TRIANGLES);
|
||||
for (size_t i = 0; i + 2 < geo->indices.size(); i += 3) {
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
const Rml::Vertex &v = geo->vertices[geo->indices[i + j]];
|
||||
rlColor4ub(v.colour.red, v.colour.green, v.colour.blue, v.colour.alpha);
|
||||
rlTexCoord2f(v.tex_coord.x, v.tex_coord.y);
|
||||
rlVertex2f(v.position.x, v.position.y);
|
||||
}
|
||||
}
|
||||
rlEnd();
|
||||
|
||||
rlPopMatrix();
|
||||
rlSetTexture(0);
|
||||
}
|
||||
|
||||
void RaylibRenderInterface::ReleaseGeometry(
|
||||
Rml::CompiledGeometryHandle handle) {
|
||||
delete reinterpret_cast<GeometryData *>(handle);
|
||||
}
|
||||
|
||||
Rml::TextureHandle
|
||||
RaylibRenderInterface::LoadTexture(Rml::Vector2i &texture_dimensions,
|
||||
const Rml::String &source) {
|
||||
Texture2D tex = ::LoadTexture(source.c_str());
|
||||
if (tex.id == 0) {
|
||||
TraceLog(LOG_WARNING, "[RmlUi] failed to load texture: %s", source.c_str());
|
||||
return 0;
|
||||
}
|
||||
texture_dimensions = Rml::Vector2i(tex.width, tex.height);
|
||||
return reinterpret_cast<Rml::TextureHandle>(new Texture2D(tex));
|
||||
}
|
||||
|
||||
Rml::TextureHandle
|
||||
RaylibRenderInterface::GenerateTexture(Rml::Span<const Rml::byte> source_data,
|
||||
Rml::Vector2i source_dimensions) {
|
||||
Image img{};
|
||||
img.width = source_dimensions.x;
|
||||
img.height = source_dimensions.y;
|
||||
img.mipmaps = 1;
|
||||
img.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
|
||||
|
||||
size_t byte_count = static_cast<size_t>(img.width) * img.height * 4;
|
||||
img.data = RL_MALLOC(byte_count);
|
||||
std::memcpy(img.data, source_data.data(), byte_count);
|
||||
|
||||
Texture2D tex = LoadTextureFromImage(img);
|
||||
UnloadImage(img); // frees img.data
|
||||
|
||||
return reinterpret_cast<Rml::TextureHandle>(new Texture2D(tex));
|
||||
}
|
||||
|
||||
void RaylibRenderInterface::ReleaseTexture(Rml::TextureHandle handle) {
|
||||
Texture2D *tex = reinterpret_cast<Texture2D *>(handle);
|
||||
UnloadTexture(*tex);
|
||||
delete tex;
|
||||
}
|
||||
|
||||
void RaylibRenderInterface::EnableScissorRegion(bool enable) {
|
||||
scissor_enabled = enable;
|
||||
if (enable)
|
||||
ApplyScissor();
|
||||
else
|
||||
EndScissorMode();
|
||||
}
|
||||
|
||||
void RaylibRenderInterface::SetScissorRegion(Rml::Rectanglei region) {
|
||||
scissor_region = region;
|
||||
if (scissor_enabled)
|
||||
ApplyScissor();
|
||||
}
|
||||
|
||||
void RaylibRenderInterface::ApplyScissor() {
|
||||
BeginScissorMode(scissor_region.Left(), scissor_region.Top(),
|
||||
scissor_region.Width(), scissor_region.Height());
|
||||
}
|
||||
38
src/backend/RaylibRenderInterface.h
Normal file
38
src/backend/RaylibRenderInterface.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
#include <RmlUi/Core/RenderInterface.h>
|
||||
|
||||
// NOTE: RmlUi's RenderInterface signature has shifted across versions
|
||||
// (raw pointer+count vs. Rml::Span<>). If this fails to compile against
|
||||
// a different RmlUi commit, check RmlUi/Core/RenderInterface.h and adjust
|
||||
// CompileGeometry/GenerateTexture's parameter types, internal logic
|
||||
// stays the same.
|
||||
|
||||
class RaylibRenderInterface : public Rml::RenderInterface {
|
||||
public:
|
||||
Rml::CompiledGeometryHandle
|
||||
CompileGeometry(Rml::Span<const Rml::Vertex> vertices,
|
||||
Rml::Span<const int> indices) override;
|
||||
|
||||
void RenderGeometry(Rml::CompiledGeometryHandle handle,
|
||||
Rml::Vector2f translation,
|
||||
Rml::TextureHandle texture) override;
|
||||
|
||||
void ReleaseGeometry(Rml::CompiledGeometryHandle handle) override;
|
||||
|
||||
Rml::TextureHandle LoadTexture(Rml::Vector2i &texture_dimensions,
|
||||
const Rml::String &source) override;
|
||||
|
||||
Rml::TextureHandle GenerateTexture(Rml::Span<const Rml::byte> source_data,
|
||||
Rml::Vector2i source_dimensions) override;
|
||||
|
||||
void ReleaseTexture(Rml::TextureHandle handle) override;
|
||||
|
||||
void EnableScissorRegion(bool enable) override;
|
||||
void SetScissorRegion(Rml::Rectanglei region) override;
|
||||
|
||||
private:
|
||||
void ApplyScissor();
|
||||
|
||||
bool scissor_enabled = false;
|
||||
Rml::Rectanglei scissor_region;
|
||||
};
|
||||
15
src/backend/RaylibSystemInterface.cpp
Normal file
15
src/backend/RaylibSystemInterface.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "RaylibSystemInterface.h"
|
||||
#include <raylib.h>
|
||||
|
||||
double RaylibSystemInterface::GetElapsedTime() { return GetTime(); }
|
||||
|
||||
bool RaylibSystemInterface::LogMessage(Rml::Log::Type type,
|
||||
const Rml::String &message) {
|
||||
TraceLogLevel level = LOG_INFO;
|
||||
if (type == Rml::Log::LT_WARNING)
|
||||
level = LOG_WARNING;
|
||||
if (type == Rml::Log::LT_ERROR || type == Rml::Log::LT_ASSERT)
|
||||
level = LOG_ERROR;
|
||||
TraceLog(level, "[RmlUi] %s", message.c_str());
|
||||
return true;
|
||||
}
|
||||
8
src/backend/RaylibSystemInterface.h
Normal file
8
src/backend/RaylibSystemInterface.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
#include <RmlUi/Core/SystemInterface.h>
|
||||
|
||||
class RaylibSystemInterface : public Rml::SystemInterface {
|
||||
public:
|
||||
double GetElapsedTime() override;
|
||||
bool LogMessage(Rml::Log::Type type, const Rml::String &message) override;
|
||||
};
|
||||
0
src/main.cpp
Normal file
0
src/main.cpp
Normal file
Reference in New Issue
Block a user