Skip to content

Commit

Permalink
Add basic main initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed May 10, 2024
1 parent 2765b25 commit 4319d11
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 20 deletions.
9 changes: 6 additions & 3 deletions source/core/inc/tactile/core/tactile_editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
namespace tactile {

class IWindow;
class IRenderer;

/**
* Represents the Tactile editor application.
*/
class TactileEditor final : public IEngineApp {
class TactileEditor final : public IEngineApp
{
public:
explicit TactileEditor(IWindow* window);
TactileEditor(IWindow* window, IRenderer* renderer);

void on_startup() override;

Expand All @@ -25,7 +27,8 @@ class TactileEditor final : public IEngineApp {
void on_framebuffer_scale_changed(float framebuffer_scale) override;

private:
IWindow* m_window;
IWindow* mWindow;
IRenderer* mRenderer;
};

} // namespace tactile
30 changes: 25 additions & 5 deletions source/core/src/tactile/core/tactile_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,43 @@

#include "tactile/core/tactile_editor.hpp"

#include <cstdlib> // malloc, free

#include <imgui.h>

#include "tactile/base/int.hpp"
#include "tactile/core/debug/validation.hpp"
#include "tactile/render/renderer.hpp"
#include "tactile/render/window.hpp"

namespace tactile {

TactileEditor::TactileEditor(IWindow* window)
: m_window {require_not_null(window, "null window")}
{}
TactileEditor::TactileEditor(IWindow* window, IRenderer* renderer)
: mWindow {require_not_null(window, "null window")},
mRenderer {require_not_null(renderer, "null renderer")}
{
// NOLINTBEGIN(*-no-malloc)
ImGui::SetAllocatorFunctions(
[](const usize size, void*) { return std::malloc(size); },
[](void* ptr, void*) { std::free(ptr); });
ImGui::SetCurrentContext(mRenderer->get_imgui_context());
// NOLINTEND(*-no-malloc)
}

void TactileEditor::on_startup()
{
m_window->show();
auto& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;

mWindow->show();
mWindow->maximize();
}

void TactileEditor::on_shutdown()
{
m_window->hide();
mWindow->hide();
ImGui::SetCurrentContext(nullptr);
}

void TactileEditor::on_update()
Expand Down
88 changes: 76 additions & 12 deletions source/main/src/tactile/main.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,89 @@
// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0)

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

#include "tactile/base/prelude.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/sdl_context.hpp"
#include "tactile/core/platform/win32.hpp"
#include "tactile/core/tactile_editor.hpp"
#include "tactile/core/util/scope_guard.hpp"

#ifdef TACTILE_HAS_OPENGL_RENDERER
#include "tactile/opengl/opengl_renderer.hpp"
#endif

auto main(const int, char*[]) -> int
{
const auto binary_dir = std::filesystem::current_path();
try {
const auto startup_time = tactile::SteadyClock::now();
std::set_terminate(&tactile::on_terminate);

tactile::win32_enable_virtual_terminal_processing();

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

tactile::Logger logger {};
logger.set_reference_instant(startup_time);
logger.set_min_level(tactile::LogLevel::kTrace);
logger.add_sink(std::move(terminal_sink));

tactile::set_default_logger(&logger);
const tactile::ScopeGuard logger_guard {
[] { tactile::set_default_logger(nullptr); }};

const tactile::SetLogScope log_scope {"General"};

TACTILE_LOG_INFO("Tactile " TACTILE_VERSION_STRING);

tactile::init_random_number_generator();

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

#ifdef TACTILE_HAS_OPENGL_RENDERER
TACTILE_LOG_TRACE("Initializing OpenGL renderer");
auto renderer = tactile::OpenGLRenderer::make();
#else
#error "No renderer is available"
#endif

if (!renderer.has_value()) {
TACTILE_LOG_FATAL("Could not create renderer: {}",
renderer.error().message());
return EXIT_FAILURE;
}

tactile::TactileEditor editor {renderer->get_window(), &renderer.value()};
tactile::Engine engine {&editor, &renderer.value()};

tactile::Logger logger {};
logger.set_reference_instant(tactile::SteadyClock::now());
logger.set_min_level(tactile::LogLevel::kTrace);
engine.run();

auto terminal_log_sink = std::make_unique<tactile::TerminalLogSink>();
logger.add_sink(std::move(terminal_log_sink));
return EXIT_SUCCESS;
}
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");
}

TACTILE_LOG_DEBUG("Tactile " TACTILE_VERSION_STRING);
return EXIT_SUCCESS;
return EXIT_FAILURE;
}

0 comments on commit 4319d11

Please sign in to comment.