1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-22 02:12:45 +01:00

rwgame: add imgui demo window

This commit is contained in:
Anonymous Maarten 2018-12-20 20:31:14 +01:00 committed by Daniel Evans
parent 0e5f1e5b5f
commit e77a7caf74
7 changed files with 233 additions and 4 deletions

View File

@ -15,6 +15,9 @@ add_library(librwgame STATIC
GameWindow.hpp
GameWindow.cpp
RWImGui.cpp
RWImGui.hpp
HUDDrawer.hpp
HUDDrawer.cpp
MenuSystem.hpp
@ -68,6 +71,18 @@ target_link_libraries(librwgame
SDL2::SDL2
)
if(ENABLE_IMGUI)
target_compile_definitions(librwgame
PUBLIC
RW_IMGUI
)
target_link_libraries(librwgame
PUBLIC
imgui::sdl_gl3
)
endif()
add_executable(rwgame
main.cpp
)

View File

@ -20,7 +20,7 @@ GameBase::GameBase(Logger &inlog, const std::optional<RWArgConfigLayer> &args) :
bool fullscreen = config.fullscreen();
size_t w = config.width(), h = config.height();
if (SDL_Init(SDL_INIT_VIDEO) < 0)
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0)
throw std::runtime_error("Failed to initialize SDL2!");
window.create(kWindowTitle + " [" + kBuildStr + "]", w, h, fullscreen);

View File

@ -5,8 +5,9 @@
#include <SDL_video.h>
#include <glm/vec2.hpp>
#include <string>
#include <tuple>
#include <SDL.h>
class GameWindow {
SDL_Window* window = nullptr;
@ -30,6 +31,10 @@ public:
bool isOpen() const {
return !!window;
}
std::tuple<SDL_Window *, SDL_GLContext> getSDLContext() {
return std::make_tuple(window, glcontext);
}
};
#endif

View File

@ -2,6 +2,7 @@
#include <glm/gtx/norm.hpp>
#include "RWImGui.hpp"
#include "GameInput.hpp"
#include "State.hpp"
#include "StateManager.hpp"
@ -28,7 +29,6 @@
#include <functional>
#include <iomanip>
#include <iostream>
#include <algorithm>
#ifdef _MSC_VER
#pragma warning(disable : 4305 5033)
@ -54,7 +54,8 @@ constexpr float kMaxPhysicsSubSteps = 2;
RWGame::RWGame(Logger& log, const std::optional<RWArgConfigLayer> &args)
: GameBase(log, args)
, data(&log, config.gamedataPath())
, renderer(&log, &data) {
, renderer(&log, &data)
, imgui(*this) {
RW_PROFILE_THREAD("Main");
RW_TIMELINE_ENTER("Startup", MP_YELLOW);
@ -76,6 +77,8 @@ RWGame::RWGame(Logger& log, const std::optional<RWArgConfigLayer> &args)
config.gamedataPath());
}
imgui.init();
data.load();
for (const auto& [specialModel, fileName, name] : kSpecialModels) {
@ -480,6 +483,9 @@ bool RWGame::updateInput() {
RW_PROFILE_SCOPE(__func__);
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (imgui.process_event(event)) {
continue;
}
switch (event.type) {
case SDL_QUIT:
return false;
@ -669,6 +675,8 @@ void RWGame::render(float alpha, float time) {
RW_PROFILE_SCOPE("state");
stateManager.draw(renderer);
}
imgui.tick();
}
void RWGame::renderDebugView(float time, ViewCamera &viewCam) {

View File

@ -3,6 +3,8 @@
#include "GameBase.hpp"
#include "HUDDrawer.hpp"
#include "RWConfig.hpp"
#include "RWImGui.hpp"
#include "StateManager.hpp"
#include "game.hpp"
@ -22,6 +24,7 @@
class RWGame final : public GameBase {
GameData data;
GameRenderer renderer;
RWImGui imgui;
DebugDraw debug;
GameState state;
HUDDrawer hudDrawer{};

177
rwgame/RWImGui.cpp Normal file
View File

@ -0,0 +1,177 @@
#include "RWImGui.hpp"
#ifdef RW_IMGUI
#include "RWGame.hpp"
#include <imgui.h>
#include <imgui_impl_sdl.h>
#include <imgui_impl_opengl3.h>
#include <gl/gl_core_3_3.h>
RWImGui::RWImGui(RWGame &game)
: _game(game) {
}
RWImGui::~RWImGui() {
destroy();
}
void RWImGui::init() {
IMGUI_CHECKVERSION();
_context = ImGui::CreateContext();
auto [window, context] = _game.getWindow().getSDLContext();
ImGui_ImplSDL2_InitForOpenGL(window, context);
ImGui_ImplOpenGL3_Init("#version 150");
}
void RWImGui::destroy() {
if (!_context) {
return;
}
ImGui::SetCurrentContext(_context);
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
_context = nullptr;
}
bool RWImGui::process_event(SDL_Event &event) {
if (!_context) {
return false;
}
ImGui::SetCurrentContext(_context);
ImGui_ImplSDL2_ProcessEvent(&event);
auto& io = ImGui::GetIO();
return io.WantCaptureMouse || io.WantCaptureKeyboard;
}
void RWImGui::tick() {
if (!_context) {
return;
}
ImGui::SetCurrentContext(_context);
auto& io = ImGui::GetIO();
SDL_Window *window;
std::tie(window, std::ignore) = _game.getWindow().getSDLContext();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame(window);
ImGui::NewFrame();
static float f = 0.0f;
ImGui::Begin("Hello, world!");
ImGui::Text("Hello, world!");
ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", static_cast<double>(1000.0f / io.Framerate), static_cast<double>(io.Framerate)); ImGui::End();
static bool show_demo_window = true;
if (show_demo_window) {
ImGui::ShowDemoWindow(&show_demo_window);
}
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
static ImGuiIO *g_io;
static SDL_Window *g_sdl_window;
static SDL_GLContext g_sdl_gl_context;
void imgui_init(SDL_Window *sdl_window, SDL_GLContext sdl_gl_context) {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
g_io = &ImGui::GetIO();
g_sdl_window = sdl_window;
g_sdl_gl_context = sdl_gl_context;
ImGui_ImplSDL2_InitForOpenGL(sdl_window, sdl_gl_context);
ImGui_ImplOpenGL3_Init("#version 150");
// ImGui::StyleColorsDark();
// ImGui::StyleColorsClassic();
ImGui::StyleColorsLight();
// Build atlas
unsigned char* tex_pixels = NULL;
int tex_w, tex_h;
g_io->Fonts->GetTexDataAsRGBA32(&tex_pixels, &tex_w, &tex_h);
}
void imgui_process_event(SDL_Event *event, bool *mouse, bool *keyboard) {
ImGui_ImplSDL2_ProcessEvent(event);
*mouse = g_io->WantCaptureMouse;
*keyboard = g_io->WantCaptureKeyboard;
}
static bool show_demo_window = true;
void imgui_tick() {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame(g_sdl_window);
ImGui::NewFrame();
// g_io->DisplaySize = ImVec2(1920, 1080);
// g_io->DeltaTime = 1.0f / 60.0f;
static float f = 0.0f;
ImGui::Begin("Hello, world!");
ImGui::Text("Hello, world!");
ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", static_cast<double>(1000.0f / g_io->Framerate), static_cast<double>(g_io->Framerate));
ImGui::End();
if (show_demo_window) {
ImGui::ShowDemoWindow(&show_demo_window);
}
// ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
ImGui::Render();
// SDL_GL_MakeCurrent(g_sdl_window, g_sdl_gl_context);
// glViewport(0, 0, static_cast<int>(g_io->DisplaySize.x), static_cast<int>(g_io->DisplaySize.y));
// glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
// glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// SDL_GL_SwapWindow(g_sdl_window);
}
void imgui_destroy() {
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
g_io = nullptr;
g_sdl_gl_context = nullptr;
g_sdl_window = nullptr;
}
#else
RWImGui::RWImGui(RWGame &game)
: _game(game) {
}
RWImGui::~RWImGui() {
destroy();
}
RWImGui::init(SDL_Window *window, SDL_GLContext context) {
}
RWImGui::destroy() {
}
RWImGui::process_event(SDL_Event &event) {
}
#endif

21
rwgame/RWImGui.hpp Normal file
View File

@ -0,0 +1,21 @@
#ifndef RWGAME_RWIMGUI_HPP
#define RWGAME_RWIMGUI_HPP
#include <SDL.h>
class RWGame;
struct ImGuiContext;
class RWImGui {
RWGame &_game;
ImGuiContext *_context = nullptr;
public:
RWImGui(RWGame &game);
~RWImGui();
void init();
void destroy();
bool process_event(SDL_Event &event);
void tick();
};
#endif // RWGAME_RWIMGUI_HPP