Skip to content

Commit

Permalink
Adapt OpenGL renderer to runtime target
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Jul 8, 2024
1 parent 00e972d commit 8cf5e35
Show file tree
Hide file tree
Showing 23 changed files with 646 additions and 271 deletions.
1 change: 0 additions & 1 deletion source/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ target_sources(tactile-base
"${PROJECT_SOURCE_DIR}/inc/tactile/base/io/byte_stream.hpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/base/numeric/saturate_cast.hpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/base/numeric/sign_cast.hpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/base/plugin/plugin.hpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/base/render/renderer.hpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/base/render/texture.hpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/base/render/window.hpp"
Expand Down
6 changes: 0 additions & 6 deletions source/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ target_sources(tactile-core
"${PROJECT_SOURCE_DIR}/inc/tactile/core/numeric/vec_common.hpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/core/numeric/vec_format.hpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/core/numeric/vec_stream.hpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/core/persist/protobuf_context.hpp"
"${PROJECT_SOURCE_DIR}/src/tactile/core/persist/protobuf_context.cpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/core/platform/bits.hpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/core/platform/dynamic_library.hpp"
"${PROJECT_SOURCE_DIR}/src/tactile/core/platform/dynamic_library.cpp"
Expand All @@ -144,8 +142,6 @@ target_sources(tactile-core
"${PROJECT_SOURCE_DIR}/src/tactile/core/platform/file_dialog.cpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/core/platform/filesystem.hpp"
"${PROJECT_SOURCE_DIR}/src/tactile/core/platform/filesystem.cpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/core/platform/sdl_context.hpp"
"${PROJECT_SOURCE_DIR}/src/tactile/core/platform/sdl_context.cpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/core/platform/win32.hpp"
"${PROJECT_SOURCE_DIR}/src/tactile/core/platform/win32.cpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/core/tile/animation.hpp"
Expand Down Expand Up @@ -227,8 +223,6 @@ target_sources(tactile-core
"${PROJECT_SOURCE_DIR}/inc/tactile/core/ui/fonts.hpp"
"${PROJECT_SOURCE_DIR}/src/tactile/core/ui/fonts.cpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/core/ui/imgui_compat.hpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/core/ui/imgui_context.hpp"
"${PROJECT_SOURCE_DIR}/src/tactile/core/ui/imgui_context.cpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/core/ui/shortcuts.hpp"
"${PROJECT_SOURCE_DIR}/src/tactile/core/ui/shortcuts.cpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/core/ui/theme.hpp"
Expand Down
19 changes: 0 additions & 19 deletions source/core/inc/tactile/core/ui/imgui_context.hpp

This file was deleted.

14 changes: 0 additions & 14 deletions source/core/src/tactile/core/ui/imgui_context.cpp

This file was deleted.

2 changes: 0 additions & 2 deletions source/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ target_include_directories(tactile PUBLIC "${PROJECT_SOURCE_DIR}/inc")
target_link_libraries(tactile
PRIVATE
tactile::base
tactile::core
tactile::runtime
SDL2::SDL2
SDL2::SDL2main
)
174 changes: 3 additions & 171 deletions source/main/src/tactile/main.cpp
Original file line number Diff line number Diff line change
@@ -1,178 +1,10 @@
// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0)

#include <cstdlib> // EXIT_SUCCESS, EXIT_FAILURE, malloc, free
#include <exception> // exception, set_terminate
#include <memory> // make_unique
#include <utility> // move

#include <SDL2/SDL_main.h>
#include <imgui.h>

#include "tactile/base/int.hpp"
#include "tactile/base/prelude.hpp"
#include "tactile/base/render/renderer.hpp"
#include "tactile/base/util/chrono.hpp"
#include "tactile/core/debug/exception.hpp"
#include "tactile/core/debug/terminate.hpp"
#include "tactile/core/engine/engine.hpp"
#include "tactile/core/log/logger.hpp"
#include "tactile/core/log/set_log_scope.hpp"
#include "tactile/core/log/terminal_log_sink.hpp"
#include "tactile/core/numeric/random.hpp"
#include "tactile/core/persist/protobuf_context.hpp"
#include "tactile/core/platform/dynamic_library.hpp"
#include "tactile/core/platform/sdl_context.hpp"
#include "tactile/core/platform/win32.hpp"
#include "tactile/core/tactile_editor.hpp"
#include "tactile/core/ui/common/style.hpp"
#include "tactile/core/ui/imgui_context.hpp"
#include "tactile/core/util/scope_guard.hpp"
#include "tactile/runtime/window.hpp"

namespace tactile {

struct RendererFunctions final
{
using prepare_renderer_t = void(uint32*);
using make_renderer_t = IRenderer*(IWindow*, ImGuiContext*);
using free_renderer_t = void(IRenderer*);

Unique<IDynamicLibrary> lib;
prepare_renderer_t* prepare_renderer;
make_renderer_t* make_renderer;
free_renderer_t* free_renderer;
};

using UniqueRenderer = Unique<IRenderer, RendererFunctions::free_renderer_t*>;

[[nodiscard]]
auto _load_renderer_functions(const Path& libpath) -> Maybe<RendererFunctions>
{
RendererFunctions functions {};

functions.lib = load_library(libpath);
if (!functions.lib) {
TACTILE_LOG_ERROR("Could not load renderer library");
return kNone;
}

functions.prepare_renderer =
find_symbol<RendererFunctions::prepare_renderer_t>(
*functions.lib,
"tactile_prepare_renderer");
functions.make_renderer =
find_symbol<RendererFunctions::make_renderer_t>(*functions.lib,
"tactile_make_renderer");
functions.free_renderer =
find_symbol<RendererFunctions::free_renderer_t>(*functions.lib,
"tactile_free_renderer");

if (!functions.prepare_renderer || !functions.make_renderer ||
!functions.free_renderer) {
TACTILE_LOG_ERROR("Could not load renderer functions");
return kNone;
}

return functions;
}

[[nodiscard]]
auto _make_logger() -> Logger
{
win32_enable_virtual_terminal_processing();

auto terminal_sink = std::make_unique<TerminalLogSink>();
terminal_sink->use_ansi_colors(true);

Logger logger {};

logger.set_reference_instant(SteadyClock::now());
logger.set_min_level(LogLevel::kTrace);
logger.add_sink(std::move(terminal_sink));

return logger;
}

[[nodiscard]]
auto _run() -> int
{
std::set_terminate(&on_terminate);

auto logger = _make_logger();
set_default_logger(&logger);
const ScopeGuard logger_guard {[] { set_default_logger(nullptr); }};
const SetLogScope log_scope {"General"};

TACTILE_LOG_INFO("Tactile " TACTILE_VERSION_STRING);

init_random_number_generator();

const ProtobufContext protobuf_context {};
const SDLContext sdl_context {};

ui::UniqueImGuiContext imgui_context {ImGui::CreateContext()};

// NOLINTBEGIN(*-no-malloc)
ImGui::SetAllocatorFunctions(
[](const usize size, void*) { return std::malloc(size); },
[](void* ptr, void*) { std::free(ptr); });
ImGui::SetCurrentContext(imgui_context.get());
// NOLINTEND(*-no-malloc)

auto& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;

ui::apply_custom_style(ImGui::GetStyle());
#include "tactile/runtime/launcher.hpp"

const auto renderer_functions =
_load_renderer_functions("tactile-opengl" TACTILE_DLL_EXT);
if (!renderer_functions.has_value()) {
return EXIT_FAILURE;
}

uint32 window_flags = 0;
renderer_functions->prepare_renderer(&window_flags);

auto window = Window::create(window_flags);
if (!window.has_value()) {
return EXIT_FAILURE;
}

UniqueRenderer renderer {
renderer_functions->make_renderer(&window.value(), imgui_context.get()),
renderer_functions->free_renderer};
if (!renderer) {
TACTILE_LOG_ERROR("Could not create renderer");
return EXIT_FAILURE;
}

TactileEditor editor {&window.value(), renderer.get()};

Engine engine {&editor, renderer.get()};
engine.run();

return EXIT_SUCCESS;
}

} // namespace tactile

auto main(const int, char*[]) -> int
auto main(const int argc, char* argv[]) -> int
{
try {
return tactile::_run();
}
catch (const tactile::Exception& exception) {
TACTILE_LOG_FATAL("Unhandled exception: {}\n{}",
exception.what(),
exception.trace());
}
catch (const std::exception& exception) {
TACTILE_LOG_FATAL("Unhandled exception: {}", exception.what());
}
catch (...) {
TACTILE_LOG_FATAL("Unhandled exception");
}

return EXIT_FAILURE;
return tactile::launch(argc, argv);
}
3 changes: 3 additions & 0 deletions source/opengl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ target_sources(tactile-opengl
"${PROJECT_SOURCE_DIR}/src/tactile/opengl/opengl_imgui.cpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/opengl/opengl_renderer.hpp"
"${PROJECT_SOURCE_DIR}/src/tactile/opengl/opengl_renderer.cpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/opengl/opengl_renderer_plugin.hpp"
"${PROJECT_SOURCE_DIR}/src/tactile/opengl/opengl_renderer_plugin.cpp"
"${PROJECT_SOURCE_DIR}/inc/tactile/opengl/opengl_texture.hpp"
"${PROJECT_SOURCE_DIR}/src/tactile/opengl/opengl_texture.cpp"
)
Expand All @@ -31,6 +33,7 @@ target_include_directories(tactile-opengl
target_link_libraries(tactile-opengl
PUBLIC
tactile::base
tactile::runtime

PRIVATE
OpenGL::GL
Expand Down
13 changes: 0 additions & 13 deletions source/opengl/inc/tactile/opengl/opengl_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,4 @@ class TACTILE_OPENGL_API OpenGLRenderer final : public IRenderer
OpenGLRenderer();
};

extern "C"
{
TACTILE_OPENGL_API
void tactile_prepare_renderer(uint32* window_flags);

TACTILE_OPENGL_API
auto tactile_make_renderer(IWindow* window,
ImGuiContext* context) -> IRenderer*;

TACTILE_OPENGL_API
void tactile_free_renderer(IRenderer* renderer);
}

} // namespace tactile
34 changes: 34 additions & 0 deletions source/opengl/inc/tactile/opengl/opengl_renderer_plugin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0)

#pragma once

#include "tactile/base/container/maybe.hpp"
#include "tactile/base/prelude.hpp"
#include "tactile/opengl/api.hpp"
#include "tactile/opengl/opengl_renderer.hpp"
#include "tactile/runtime/plugin.hpp"

namespace tactile {

/**
* Manages the OpenGL renderer plugin.
*/
class TACTILE_OPENGL_API OpenGLRendererPlugin final : public IPlugin
{
public:
void load(Runtime& runtime) override;

void unload(Runtime& runtime) override;

private:
Optional<OpenGLRenderer> mRenderer {};
};

extern "C"
{
TACTILE_OPENGL_API auto tactile_make_plugin() -> IPlugin*;

TACTILE_OPENGL_API void tactile_free_plugin(IPlugin* plugin);
}

} // namespace tactile
36 changes: 1 addition & 35 deletions source/opengl/src/tactile/opengl/opengl_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "tactile/opengl/opengl_error.hpp"
#include "tactile/opengl/opengl_imgui.hpp"
#include "tactile/opengl/opengl_texture.hpp"
#include "tactile/runtime/runtime.hpp"

namespace tactile {

Expand Down Expand Up @@ -190,39 +191,4 @@ auto OpenGLRenderer::get_window() const -> const IWindow*
return mData->window;
}

void tactile_prepare_renderer(uint32* window_flags)
{
if (window_flags != nullptr) {
*window_flags = SDL_WINDOW_OPENGL;
}

if constexpr (kOnMacos) {
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS,
SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
}

SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, SDL_TRUE);
}

auto tactile_make_renderer(IWindow* window, ImGuiContext* context) -> IRenderer*
{
if (auto renderer = OpenGLRenderer::make(window, context)) {
return new (std::nothrow) OpenGLRenderer {std::move(*renderer)};
}

return nullptr;
}

void tactile_free_renderer(IRenderer* renderer)
{
delete renderer;
}

} // namespace tactile
Loading

0 comments on commit 8cf5e35

Please sign in to comment.