diff --git a/CMakeLists.txt b/CMakeLists.txt index 2880593f..4c2e90af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,7 @@ set(BUILD_UTIL TRUE CACHE BOOL "Selects if the util module should be built") set(BUILD_SFMLBACKENDS FALSE CACHE BOOL "Selects if the SFML backends should be built") set(BUILD_SDLBACKENDS FALSE CACHE BOOL "Selects if the SDL backends should be built") set(BUILD_SDL2BACKENDS FALSE CACHE BOOL "Selects if the SDL2 backends should be built") +set(BUILD_GLFWBACKENDS FALSE CACHE BOOL "Selects if the GLFW backends should be built") set(BUILD_JSON TRUE CACHE BOOL "Selects if the json (de)serialization functions should be built") @@ -487,6 +488,29 @@ if(BUILD_UI) ${ui_sdl2_backend_header_files}) target_link_libraries(${project_name}-sdl2 ${SDL2_LIBRARY} ${project_name}-ui) endif() + + if(${BUILD_GLFWBACKENDS}) + find_package(GLFW REQUIRED) + if(GLFW_FOUND) + include_directories(${GLFW_INCLUDE_DIR}) + endif(GLFW_FOUND) + + set(ui_glfw_backend_source_files + src/ui/glfw3windowbackend.cpp + #src/ui/contextsettings.cpp + src/ui/glfw3inputbackend.cpp) + + set(ui_glfw_backend_header_files + include/fea/ui/glfw3windowbackend.hpp + include/fea/ui/glfw3inputbackend.hpp) + + set(BUILT_TARGETS ${BUILT_TARGETS} ${project_name}-glfw) + + add_library(${project_name}-glfw ${SHARED_OR_STATIC} + ${ui_glfw_backend_source_files} + ${ui_glfw_backend_header_files}) + target_link_libraries(${project_name}-glfw ${GLFW_LIBRARY} ${project_name}-ui) + endif() endif() if(BUILD_UTIL) @@ -554,6 +578,9 @@ if(INSTALL_PKGCONFIG_FILES) if(BUILD_SDL2BACKENDS) set(PKGMODULE_SDL2 fea-sdl2) endif() + if(BUILD_GLFWBACKENDS) + set(PKGMODULE_SDL2 fea-glfw) + endif() if(BUILD_JSON) set(PKGREQ_JSONCPP jsoncpp) endif() diff --git a/cmake/modules/FindGLFW.cmake b/cmake/modules/FindGLFW.cmake new file mode 100644 index 00000000..a4574173 --- /dev/null +++ b/cmake/modules/FindGLFW.cmake @@ -0,0 +1,83 @@ +# + +# Try to find GLFW library and include path. +# Once done this will define +# +# GLFW_FOUND +# GLFW_INCLUDE_DIR +# GLFW_LIBRARY +# + +include(FindPackageHandleStandardArgs) + +if (WIN32) + find_path( GLFW_INCLUDE_DIR + NAMES + GLFW/glfw3.h + PATHS + ${PROJECT_SOURCE_DIR}/shared_external/glfw/include + ${PROJECT_SOURCE_DIR}/../shared_external/glfw/include + ${GLFW_LOCATION}/include + $ENV{GLFW_LOCATION}/include + $ENV{PROGRAMFILES}/GLFW/include + ${GLFW_LOCATION} + $ENV{GLFW_LOCATION} + DOC "The directory where GLFW/glfw3.h resides" ) + if(ARCH STREQUAL "x86") + find_library( GLFW_LIBRARY + NAMES + glfw3 + PATHS + ${GLFW_LOCATION}/lib + $ENV{GLFW_LOCATION}/lib + $ENV{PROGRAMFILES}/GLFW/lib + DOC "The GLFW library") + else() + find_library( GLFW_LIBRARY + NAMES + glfw3 + PATHS + ${GLFW_LOCATION}/lib + $ENV{GLFW_LOCATION}/lib + $ENV{PROGRAMFILES}/GLFW/lib + DOC "The GLFW library") + endif() +endif () + +if (${CMAKE_HOST_UNIX}) + find_path( GLFW_INCLUDE_DIR + NAMES + GLFW/glfw3.h + PATHS + ${GLFW_LOCATION}/include + $ENV{GLFW_LOCATION}/include + /usr/include + /usr/local/include + /sw/include + /opt/local/include + NO_DEFAULT_PATH + DOC "The directory where GLFW/glfw3.h resides" + ) + find_library( GLFW_LIBRARY + NAMES + glfw3 glfw + PATHS + ${GLFW_LOCATION}/lib + $ENV{GLFW_LOCATION}/lib + /usr/lib64 + /usr/lib + /usr/local/lib64 + /usr/local/lib + /sw/lib + /opt/local/lib + /usr/lib/x86_64-linux-gnu + NO_DEFAULT_PATH + DOC "The GLFW library") +endif () + +find_package_handle_standard_args(GLFW DEFAULT_MSG + GLFW_INCLUDE_DIR + GLFW_LIBRARY +) + +mark_as_advanced( GLFW_FOUND ) diff --git a/include/fea/ui/glfw3inputbackend.hpp b/include/fea/ui/glfw3inputbackend.hpp new file mode 100644 index 00000000..7e5e4e4b --- /dev/null +++ b/include/fea/ui/glfw3inputbackend.hpp @@ -0,0 +1,34 @@ +#pragma once +#include +#define NO_SDL_GLEXT +#include + +namespace fea +{ + class FEA_API GLFW3InputBackend : public InputBackend + { + public: + GLFW3InputBackend(); + + std::queue fetchEvents() override; + + bool isKeyPressed(Keyboard::Code code) override; + + bool isMouseButtonPressed(Mouse::Button b) override; + Vec2I getMouseGlobalPosition() override; //not supported + Vec2I getMouseWindowPosition() override; + void setMouseGlobalPosition(int32_t x, int32_t y) override; //not supported + void setMouseWindowPosition(int32_t x, int32_t y) override; //not supported + + bool isGamepadConnected(uint32_t id) override; //not supported + uint32_t getGamepadButtonCount(uint32_t id) override; //not supported + bool isGamepadButtonPressed(uint32_t id, uint32_t button) override; //not supported + bool gamepadHasAxis(uint32_t id, Gamepad::Axis axis) override; //not supported + float getGamepadAxisPosition(uint32_t id, Gamepad::Axis axis) override; //not supported + + void setGamepadThreshold(float threshold) override; //not supported + void setKeyRepeatEnabled(bool enabled) override; //not supported + private: + bool mKeyRepeat; + }; +} diff --git a/include/fea/ui/glfw3windowbackend.hpp b/include/fea/ui/glfw3windowbackend.hpp new file mode 100644 index 00000000..bb2c5bb7 --- /dev/null +++ b/include/fea/ui/glfw3windowbackend.hpp @@ -0,0 +1,35 @@ +#pragma once +#include +#include + +namespace fea{ +class GLFW3WindowBackend : public WindowBackend +{ +private: + GLFWwindow *window; + GLFWimage *images[2]; +public: + void open(VideoMode mode, const std::string& title, uint32_t style, const ContextSettings& settings) override; + void close(); + bool isOpen() const; + const ContextSettings getSettings() const override; //not supported + Vec2I getPosition() const; + void setPosition(int32_t x, int32_t y); + Vec2I getSize() const; + void setSize(int32_t w, int32_t h); + void setTitle(const std::string& title); + void setIcon(uint32_t width, uint32_t height, const uint8_t* pixels); + void setVisible(bool visible); + void setVSyncEnabled(bool enabled); + void setMouseCursorVisible(bool visible); + void setFramerateLimit(uint32_t limit); + bool setRenderingActive(bool active) const override; + void swapBuffers(); + void lockCursor(bool lock); + ~GLFW3WindowBackend(); + + + +}; + +} diff --git a/pkg-config/fea-glfw.pc.in b/pkg-config/fea-glfw.pc.in new file mode 100644 index 00000000..8fd3b417 --- /dev/null +++ b/pkg-config/fea-glfw.pc.in @@ -0,0 +1,12 @@ +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=${prefix} +libdir=${exec_prefix}/lib +includedir=${prefix}/include + +Name: fea-glfw +Description: Feather Kit GLFW backends +Version: @fea_VERSION@ +URL: http://featherkit.therocode.net/ +Libs: -L${libdir} -lfea-glfw +Cflags: -I${includedir} +Requires.private: glfw diff --git a/src/ui/glfw3inputbackend.cpp b/src/ui/glfw3inputbackend.cpp new file mode 100644 index 00000000..55de7b86 --- /dev/null +++ b/src/ui/glfw3inputbackend.cpp @@ -0,0 +1,110 @@ +#include + +namespace fea +{ + GLFW3InputBackend::GLFW3InputBackend(): + mKeyRepeat(true) + { + } + + std::queue GLFW3InputBackend::fetchEvents() + { + std::queue result; + glfwSetKeyCallback(window, key_callback); + + void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) + { + switch(action){ + case GLFW_PRESS: + switch(key) + { + case GLFW_KEY_E: + KeyEvent event; + Code code(4); + event = key_press(code,false,false,false,false); + break; + + case GLFW_KEY_A: + KeyEvent event; + Code code(0); + event = key_press(code,false,false,false,false); + break; + + case GLFW_KEY_B: + KeyEvent event; + Code code(1); + event = key_press(code,false,false,false,false); + break; + + } + break; + } + + } + } + return event; + } + + bool GLFW3InputBackend::isKeyPressed(Keyboard::Code code) + { + return false; + } + + bool GLFW3InputBackend::isMouseButtonPressed(Mouse::Button b) + { + return false; + } + + Vec2I GLFW3InputBackend::getMouseGlobalPosition() + { + return {}; + } + + Vec2I GLFW3InputBackend::getMouseWindowPosition() + { + return {}; + } + + void GLFW3InputBackend::setMouseGlobalPosition(int32_t x, int32_t y) + { + } + + void GLFW3InputBackend::setMouseWindowPosition(int32_t x, int32_t y) + { + } + + bool GLFW3InputBackend::isGamepadConnected(uint32_t id) + { + return true; + } + + uint32_t GLFW3InputBackend::getGamepadButtonCount(uint32_t id) + { + return 0; + } + + bool GLFW3InputBackend::isGamepadButtonPressed(uint32_t id, uint32_t button) + { + return false; + } + + bool GLFW3InputBackend::gamepadHasAxis(uint32_t id, Gamepad::Axis axis) + { + return false; + } + + float GLFW3InputBackend::getGamepadAxisPosition(uint32_t id, Gamepad::Axis axis) + { + return 0.0f; + } + + void GLFW3InputBackend::setGamepadThreshold(float threshold) + { + (void) threshold; + } + + void GLFW3InputBackend::setKeyRepeatEnabled(bool enabled) + { + (void) enabled; + } +} diff --git a/src/ui/glfw3windowbackend.cpp b/src/ui/glfw3windowbackend.cpp new file mode 100644 index 00000000..2c4a592e --- /dev/null +++ b/src/ui/glfw3windowbackend.cpp @@ -0,0 +1,135 @@ +#include +#include + +namespace fea{ + + int key_press(KeyBoard::Code pkey_code ,bool psystem,bool pcontrol,bool pshift,bool palt ) + { + KeyEvent Event; + Event.code =pKey_code; + Event.shift = pshift; + Event.control = pcontrol; + Event.alt = palt; + Event.system = psystem; + + return Event; + } + + void GLFW3WindowBackend::open(VideoMode mode, const std::string& title, uint32_t style, const ContextSettings& settings) + { + glfwInit(); + window = glfwCreateWindow(mode.mWidth, mode.mHeight, title.c_str(), NULL, NULL); + } + + void GLFW3WindowBackend::close() + { + glfwDestroyWindow(window); + } + + + bool GLFW3WindowBackend::isOpen() const + { + return window != nullptr; + } + + const ContextSettings GLFW3WindowBackend::getSettings() const + { + + // still looking on how to get and set context setings + + } + + Vec2I GLFW3WindowBackend::getPosition() const + { + Vec2I s; + s.x = 0; + s.y = 0; + + if(isOpen()) + glfwGetWindowPos(window, &s.x , &s.y ); + + return s; + } + + void GLFW3WindowBackend::setPosition(int32_t x, int32_t y) + { + if(isOpen()) + { + glfwSetWindowPos(window , x , y); + } + } + + Vec2I GLFW3WindowBackend::getSize() const + { + Vec2I s; + s.x = 0; + s.y = 0; + + if(isOpen()) + glfwGetWindowSize(window, &s.x, &s.y); + + return s; + } + + void GLFW3WindowBackend::setSize(int32_t w, int32_t h) + { + glfwSetWindowSize(window, w, h); + } + + void GLFW3WindowBackend::setTitle(const std::string& title) + { + glfwSetWindowTitle(window, title.c_str()); + } + + //void GLFW3WindowBackend::setIcon(const std::string &path_small , const std::string &path_big ) + void GLFW3WindowBackend::setIcon(uint32_t width, uint32_t height, const uint8_t* pixels) + { + //images[0] = load_icon(path_big.c_str()); + //images[1] = load_icon(path_small.c_str()); + + //glfwSetWindowIcon(window, 2 ,images) + } + + void GLFW3WindowBackend::setVisible(bool visible) + { + // still working on it + } + + void GLFW3WindowBackend::setVSyncEnabled(bool enabled) + { + // still woriking on it + } + + void GLFW3WindowBackend::setMouseCursorVisible(bool visible) + { + // still working on it + } + + void GLFW3WindowBackend::setFramerateLimit(uint32_t limit) + { + //still woriking on it + } + + bool GLFW3WindowBackend::setRenderingActive(bool active) const + { + // still working on it + } + + void GLFW3WindowBackend::swapBuffers() + { + glfwSwapBuffers(window); + } + + void GLFW3WindowBackend::lockCursor(bool lock) + { + if(isOpen()) + { + // still working on it + } + } + + GLFW3WindowBackend::~GLFW3WindowBackend() + { + close(); + } +}