From d25b6de1b5906da325f35c8cd39be0d623209ff7 Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Sun, 22 Oct 2023 11:47:13 +0200 Subject: [PATCH] Create window on Haiku --- CMakeLists.txt | 20 ++++ config.hpp.in | 1 + src/game/renderer/gl/gl_context.cpp | 2 +- src/game/renderer/gl/haiku_gl_context.cpp | 40 ++++++++ src/game/renderer/gl/haiku_gl_context.hpp | 46 +++++++++ .../legacy_gl/legacy_gl_master_renderer.cpp | 6 +- .../legacy_gl/legacy_gl_master_renderer.hpp | 1 + src/game/window/haiku_window.cpp | 93 +++++++++++++++++++ src/game/window/haiku_window.hpp | 61 ++++++++++++ src/game/window/window.cpp | 10 ++ src/game/window/window.hpp | 3 +- 11 files changed, 279 insertions(+), 4 deletions(-) create mode 100644 src/game/renderer/gl/haiku_gl_context.cpp create mode 100644 src/game/renderer/gl/haiku_gl_context.hpp create mode 100644 src/game/window/haiku_window.cpp create mode 100644 src/game/window/haiku_window.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5570a23..d1148f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,6 +101,12 @@ if(SERENITYOS) add_definitions("-D__serenityos__") endif() option(WINDOW_SERENITYOS "Enable SerenityOS window creation" ${WINDOW_SERENITYOS_DEFAULT}) +if(HAIKU) + set(WINDOW_HAIKU_DEFAULT ON) +else() + set(WINDOW_HAIKU_DEFAULT OFF) +endif() +option(WINDOW_HAIKU "Enable Haiku window creation" ${WINDOW_HAIKU_DEFAULT}) option(ENABLE_BACKTRACE_PRINTING "Enable backtrace printing" OFF) if(ENABLE_BACKTRACE_PRINTING) @@ -235,6 +241,16 @@ if(BUILD_CLIENT) ) endif() endif() + if(WINDOW_HAIKU) + list(APPEND CLIENT_SOURCES + src/game/window/haiku_window.cpp + ) + if(RENDERER_GL OR RENDERER_LEGACY_GL) + list(APPEND CLIENT_SOURCES + src/game/renderer/gl/haiku_gl_context.cpp + ) + endif() + endif() endif() if(BUILD_SERVER) file(GLOB_RECURSE SERVER_SOURCES "src/server/**.cpp") @@ -348,6 +364,10 @@ if(BUILD_CLIENT) target_link_libraries(${PROJECT_NAME} ${XKBCommon_LIBRARY}) endif() + if(WINDOW_HAIKU) + target_link_libraries(${PROJECT_NAME} "be" "game") + endif() + if(NOT GLM_INCLUDE_DIRS) find_package(GLM REQUIRED) endif() diff --git a/config.hpp.in b/config.hpp.in index 9f4a896..323d65a 100644 --- a/config.hpp.in +++ b/config.hpp.in @@ -32,6 +32,7 @@ SOFTWARE. #cmakedefine WINDOW_X11 #cmakedefine WINDOW_WAYLAND #cmakedefine WINDOW_SERENITYOS +#cmakedefine WINDOW_HAIKU #cmakedefine ENABLE_BACKTRACE_PRINTING diff --git a/src/game/renderer/gl/gl_context.cpp b/src/game/renderer/gl/gl_context.cpp index 0b6b3f6..5e8cd9b 100644 --- a/src/game/renderer/gl/gl_context.cpp +++ b/src/game/renderer/gl/gl_context.cpp @@ -71,8 +71,8 @@ GLContext* GLContext::create_context(window::Window* window) { break; #endif case window::WindowType::WINDOW_TYPE_WAYLAND: - return nullptr; case window::WindowType::WINDOW_TYPE_SERENITYOS: + case window::WindowType::WINDOW_TYPE_HAIKU: return nullptr; } return nullptr; diff --git a/src/game/renderer/gl/haiku_gl_context.cpp b/src/game/renderer/gl/haiku_gl_context.cpp new file mode 100644 index 0000000..a19594a --- /dev/null +++ b/src/game/renderer/gl/haiku_gl_context.cpp @@ -0,0 +1,40 @@ +/* +PolyGun + +Copyright (c) 2023 mrkubax10 + +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 "game/renderer/gl/haiku_gl_context.hpp" + +#include "game/window/haiku_window.hpp" + +using namespace polygun::renderer; + +HaikuGLContext::HaikuGLContext(window::HaikuWindow* window) : + GLContext(), + BGLView(window->Bounds(), "glView", B_FOLLOW_ALL_SIDES, 0, BGL_RGB|BGL_DEPTH|BGL_DOUBLE) +{ + LockGL(); +} + +void* HaikuGLContext::get_proc_address(const std::string& name) { + return nullptr; +} diff --git a/src/game/renderer/gl/haiku_gl_context.hpp b/src/game/renderer/gl/haiku_gl_context.hpp new file mode 100644 index 0000000..b34bfd4 --- /dev/null +++ b/src/game/renderer/gl/haiku_gl_context.hpp @@ -0,0 +1,46 @@ +/* +PolyGun + +Copyright (c) 2023 mrkubax10 + +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. +*/ + +#ifndef POLYGUN_RENDERER_HAIKU_GL_CONTEXT_HPP +#define POLYGUN_RENDERER_HAIKU_GL_CONTEXT_HPP + +#include "game/renderer/gl/gl_context.hpp" +#include + +namespace polygun::window { + class HaikuWindow; +} + +namespace polygun::renderer { + class HaikuGLContext final : public GLContext, public BGLView { + public: + HaikuGLContext(window::HaikuWindow* window); + +#if defined(RENDERER_GL) + virtual void* get_proc_address(const std::string& name) override; +#endif + }; +} + +#endif // POLYGUN_RENDERER_HAIKU_GL_CONTEXT_HPP diff --git a/src/game/renderer/legacy_gl/legacy_gl_master_renderer.cpp b/src/game/renderer/legacy_gl/legacy_gl_master_renderer.cpp index d15bbbf..8c0f606 100644 --- a/src/game/renderer/legacy_gl/legacy_gl_master_renderer.cpp +++ b/src/game/renderer/legacy_gl/legacy_gl_master_renderer.cpp @@ -38,7 +38,8 @@ using namespace polygun::renderer; LegacyGLMasterRenderer::LegacyGLMasterRenderer(window::Window* window) : MasterRenderer(), - m_context(nullptr) + m_context(nullptr), + m_should_delete_gl_context(!window->window_creates_gl_context()) { if(window->window_creates_gl_context()) m_context = window->get_window_gl_context(); @@ -52,7 +53,8 @@ LegacyGLMasterRenderer::LegacyGLMasterRenderer(window::Window* window) : } LegacyGLMasterRenderer::~LegacyGLMasterRenderer() { - delete m_context; + if(m_should_delete_gl_context) + delete m_context; } void LegacyGLMasterRenderer::clear_screen(float r, float g, float b) { diff --git a/src/game/renderer/legacy_gl/legacy_gl_master_renderer.hpp b/src/game/renderer/legacy_gl/legacy_gl_master_renderer.hpp index 8cbff66..4bb1254 100644 --- a/src/game/renderer/legacy_gl/legacy_gl_master_renderer.hpp +++ b/src/game/renderer/legacy_gl/legacy_gl_master_renderer.hpp @@ -44,6 +44,7 @@ namespace polygun::renderer { private: GLContext* m_context; + bool m_should_delete_gl_context; }; } diff --git a/src/game/window/haiku_window.cpp b/src/game/window/haiku_window.cpp new file mode 100644 index 0000000..1cc5217 --- /dev/null +++ b/src/game/window/haiku_window.cpp @@ -0,0 +1,93 @@ +/* +PolyGun + +Copyright (c) 2023 mrkubax10 + +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 "game/window/haiku_window.hpp" + +#include + +#include "common/logger.hpp" + +using namespace polygun::window; + +static BApplication* g_app = nullptr; + +HaikuWindow::HaikuWindow(const std::string& title, unsigned width, unsigned height, renderer::RendererType renderer_type) : + BDirectWindow(BRect(10, 10, width, height), title.c_str(), B_TITLED_WINDOW, 0), + Window() +{ + m_type = WindowType::WINDOW_TYPE_HAIKU; + switch(renderer_type) { + case renderer::RendererType::RENDERER_TYPE_GL: + case renderer::RendererType::RENDERER_TYPE_LEGACY_GL: +#if defined(RENDERER_GL) || defined(RENDERER_LEGACY_GL) + m_gl_context = new renderer::HaikuGLContext(this); + AddChild(m_gl_context); +#else + LOG_FATAL("Request to setup HaikuGLContext while OpenGL renderer is not compiled in"); +#endif + break; + } + Show(); +} + +HaikuWindow::~HaikuWindow() { +#if defined(RENDERER_GL) || defined(RENDERER_LEGACY_GL) + if(m_gl_context) { + m_gl_context->UnlockGL(); + //delete m_gl_context; + } +#endif +} + +void HaikuWindow::finish_frame() { +#if defined(RENDERER_GL) || defined(RENDERER_LEGACY_GL) + if(m_gl_context) + m_gl_context->SwapBuffers(); +#endif +} + +void HaikuWindow::grab_mouse(bool grab) { + +} + +bool HaikuWindow::QuitRequested() { + Event new_event; + new_event.m_type = EventType::EVENT_TYPE_WINDOW_CLOSE; + push_event(new_event); + return true; +} + +void HaikuWindow::MessageReceived(BMessage* msg) { + msg->PrintToStream(); + switch(msg->what) { + case B_KYD: + LOG_VERBOSE("Key down!"); + default: + BDirectWindow::MessageReceived(msg); + } +} + +void HaikuWindow::init_application() { + g_app = new BApplication("application/x-vnd.PolyGun-PolyGun"); +} diff --git a/src/game/window/haiku_window.hpp b/src/game/window/haiku_window.hpp new file mode 100644 index 0000000..9f0a112 --- /dev/null +++ b/src/game/window/haiku_window.hpp @@ -0,0 +1,61 @@ +/* +PolyGun + +Copyright (c) 2023 mrkubax10 + +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. +*/ + +#ifndef POLYGUN_WINDOW_HAIKU_WINDOW_HPP +#define POLYGUN_WINDOW_HAIKU_WINDOW_HPP + +#include "game/window/window.hpp" +#include + +#if defined(RENDERER_GL) || defined(RENDERER_LEGACY_GL) +#include "game/renderer/gl/haiku_gl_context.hpp" +#endif + +namespace polygun::window { + class HaikuWindow final : public BDirectWindow, public Window { + public: + HaikuWindow(const std::string& title, unsigned width, unsigned height, renderer::RendererType renderer_type); + virtual ~HaikuWindow() override; + + virtual void finish_frame() override; + virtual void grab_mouse(bool grab) override; +#if defined(RENDERER_GL) || defined(RENDERER_LEGACY_GL) + virtual bool window_creates_gl_context() const override { return true; } + virtual renderer::GLContext* get_window_gl_context() override { return m_gl_context; } +#endif + // BDirectWindow methods + virtual bool QuitRequested() override; + virtual void MessageReceived(BMessage* msg) override; + + static void init_application(); + + private: +#if defined(RENDERER_GL) || defined(RENDERER_LEGACY_GL) + renderer::HaikuGLContext* m_gl_context; +#endif + }; +} + + +#endif // POLYGUN_WINDOW_HAIKU_WINDOW_HPP diff --git a/src/game/window/window.cpp b/src/game/window/window.cpp index e629b27..0cc8f96 100644 --- a/src/game/window/window.cpp +++ b/src/game/window/window.cpp @@ -37,6 +37,9 @@ SOFTWARE. #if defined(WINDOW_SERENITYOS) #include "game/window/serenityos_window.hpp" #endif +#if defined(WINDOW_HAIKU) +#include "game/window/haiku_window.hpp" +#endif #include "common/logger.hpp" using namespace polygun; @@ -122,6 +125,13 @@ window::Window* window::Window::create_window(const std::string& title, unsigned LOG_FATAL("Failed to create X11 window: not compiled in"); #endif } +#elif defined(__HAIKU__) +#if defined(WINDOW_HAIKU) + HaikuWindow::init_application(); + return new HaikuWindow(title, width, height, renderer_type); +#else + LOG_FATAL("Failed to create Haiku window: not compiled in"); +#endif #endif LOG_FATAL("Failed to determine which window type should be created. You may have to set it manually"); return nullptr; diff --git a/src/game/window/window.hpp b/src/game/window/window.hpp index a8f9c2d..7978d54 100644 --- a/src/game/window/window.hpp +++ b/src/game/window/window.hpp @@ -46,7 +46,8 @@ namespace polygun::window { WINDOW_TYPE_WIN32, WINDOW_TYPE_X11, WINDOW_TYPE_WAYLAND, - WINDOW_TYPE_SERENITYOS + WINDOW_TYPE_SERENITYOS, + WINDOW_TYPE_HAIKU }; class Window : public utils::ThreadSafe{