From 977b8f16d7ef43101b6ef588f9610f34285fa7e6 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Wed, 20 Sep 2017 23:01:12 +0100 Subject: Replacing SDL by glfw --- .dir-locals.el | 6 ++++++ .gitmodules | 4 ++++ CMakeLists.txt | 9 ++------ README.md | 2 +- lib/CMakeLists.txt | 17 ++++++++++----- lib/glfw | 1 + tests/yagetest.cpp | 8 +++++-- yage/CMakeLists.txt | 7 +++--- yage/base/imageloader.cpp | 7 ++---- yage/base/sprite.cpp | 2 -- yage/base/sprite.h | 2 +- yage/base/window.cpp | 55 +++++++++++------------------------------------ yage/base/window.h | 9 +++++--- yage/yage.cpp | 18 +++++++++++++--- yage/yage.h | 17 ++++++--------- 15 files changed, 78 insertions(+), 86 deletions(-) create mode 160000 lib/glfw diff --git a/.dir-locals.el b/.dir-locals.el index e0f76bc1..bb978ba8 100644 --- a/.dir-locals.el +++ b/.dir-locals.el @@ -12,6 +12,9 @@ (setq company-clang-arguments (delete-dups (append company-clang-arguments (list (concat "-I" (projectile-project-root) "lib/rapidjson/include"))))) + (setq company-clang-arguments (delete-dups (append + company-clang-arguments + (list (concat "-I" (projectile-project-root) "lib/glfw/include"))))) (setq flycheck-clang-include-path (delete-dups (append flycheck-clang-include-path (list (concat (projectile-project-root) "yage"))))) @@ -21,6 +24,9 @@ (setq flycheck-clang-include-path (delete-dups (append flycheck-clang-include-path (list (concat (projectile-project-root) "lib/googletest/googletest/include"))))) + (setq flycheck-clang-include-path (delete-dups (append + flycheck-clang-include-path + (list (concat (projectile-project-root) "lib/glfw/include"))))) (setq flycheck-clang-include-path (delete-dups (append flycheck-clang-include-path (list (concat (projectile-project-root) "lib/rapidjson/include")))))))))) diff --git a/.gitmodules b/.gitmodules index 4cfa82ed..e314176d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -14,3 +14,7 @@ path = lib/SDL2 url = https://github.com/spurious/SDL-mirror branch = release-2.0.5 +[submodule "lib/glfw"] + path = lib/glfw + url = https://github.com/glfw/glfw.git + branch = latest diff --git a/CMakeLists.txt b/CMakeLists.txt index 808bfc0c..a1f6fe9f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,11 +16,6 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}) # find libraries find_package(OpenGL REQUIRED) -find_package(GLEW REQUIRED) - -# todo: change this to find_package -include(FindPkgConfig) -pkg_search_module(SDL2 REQUIRED sdl2) # adding libraries add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/lib) @@ -28,10 +23,10 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/yage) include(cmake/cppcheck.cmake) -if(${ENABLE_TESTING}) +if($ENV{UNIT_TESTS}) # enable tests enable_testing() - set(SIMULATION_RUNS 10000) + set(SIMULATION_RUNS 100) function(make_test test_name cycles) add_executable(${test_name} ${YAGE_TEST_DIR}/${test_name}.cpp) diff --git a/README.md b/README.md index 86d2df6f..724e5e5a 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Introduction YAGE stands for Yet Another Game Engine. It is a game engine that I am developing for a game called [Arider](https://github.com/ymherklotz/Arider). -It uses OpenGL and SDL2 for the window creation and management and graphics. +It uses OpenGL and GLFW for the window creation and management and graphics. The full documentation can be seen [here](https://www.yannherklotz.com/YAGE). diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 0f07326f..93f1bd1c 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -5,22 +5,29 @@ # See file LICENSE for more details # ---------------------------------------------------------------------------- -if(${ENABLE_TESTING}) +if($ENV{UNIT_TESTS}) # Initializing google test # prevents overriding the parent project's compiler/linter settings on windows set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + set(BUILD_GTEST ON CACHE BOOL "" FORCE) + set(BUILD_GMOCK OFF CACHE BOOL "" FORCE) # Add googletest directly to our build. This defines # the gtest and gtest_main targets. - add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/googletest") + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/googletest) # The gtest/gtest_main targets carry header search path # dependencies automatically when using CMake 2.8.11 or # later. Otherwise we have to add them here ourselves. if (CMAKE_VERSION VERSION_LESS 2.8.11) - include_directories("${gtest_SOURCE_DIR}/include") + include_directories(${gtest_SOURCE_DIR}/include) endif() endif() -# include headers for rapidjson, no need to build it -include_directories("${CMAKE_CURRENT_SOURCE_DIR}/rapidjson/include") +include_directories(${rapidjson}/include) + +# setting up glfw +set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE) +set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE) +set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/glfw) diff --git a/lib/glfw b/lib/glfw new file mode 160000 index 00000000..999f3556 --- /dev/null +++ b/lib/glfw @@ -0,0 +1 @@ +Subproject commit 999f3556fdd80983b10051746264489f2cb1ef16 diff --git a/tests/yagetest.cpp b/tests/yagetest.cpp index 0b8615bc..38d00ba3 100644 --- a/tests/yagetest.cpp +++ b/tests/yagetest.cpp @@ -11,8 +11,12 @@ TEST(YAGE, InitQuit) { - ASSERT_TRUE(yage::init()); - yage::quit(); + try { + yage::init(); + yage::quit(); + } catch(std::runtime_error e) { + ASSERT_TRUE(false); + } } int main(int argc, char **argv) diff --git a/yage/CMakeLists.txt b/yage/CMakeLists.txt index 1de87757..e274fca5 100644 --- a/yage/CMakeLists.txt +++ b/yage/CMakeLists.txt @@ -23,10 +23,9 @@ add_library(${PROJECT_NAME} target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${OPENGL_INCLUDE_DIR} - ${GLEW_INCLUDE_DIR} - ${SDL2_INCLUDE_DIR}) + ${GLEW_INCLUDE_DIR}) target_link_libraries(${PROJECT_NAME} - ${OPENGL_LIBRARIES} + ${OPENGL_gl_LIBRARY} ${GLEW_LIBRARIES} - ${SDL2_LIBRARIES}) + glfw) diff --git a/yage/base/imageloader.cpp b/yage/base/imageloader.cpp index 983c350e..0d7d5df9 100644 --- a/yage/base/imageloader.cpp +++ b/yage/base/imageloader.cpp @@ -17,8 +17,6 @@ namespace yage Texture ImageLoader::loadPng(const std::string &file_path) { - Texture texture = {}; - std::vector in; std::vector out; unsigned long width, height; @@ -35,6 +33,8 @@ Texture ImageLoader::loadPng(const std::string &file_path) std::to_string(error_code)); } + Texture texture{0, (int)width, (int)height}; + glGenTextures(1, &texture.id); glBindTexture(GL_TEXTURE_2D, texture.id); @@ -51,9 +51,6 @@ Texture ImageLoader::loadPng(const std::string &file_path) glBindTexture(GL_TEXTURE_2D, 0); - texture.width = (int)width; - texture.height = (int)height; - return texture; } diff --git a/yage/base/sprite.cpp b/yage/base/sprite.cpp index 767e1000..9ac4dc55 100644 --- a/yage/base/sprite.cpp +++ b/yage/base/sprite.cpp @@ -15,8 +15,6 @@ namespace yage { -Sprite::Sprite() = default; - Sprite::~Sprite() { if (vbo_id_ != 0) { diff --git a/yage/base/sprite.h b/yage/base/sprite.h index 5b9baf91..ff893b53 100644 --- a/yage/base/sprite.h +++ b/yage/base/sprite.h @@ -34,7 +34,7 @@ private: Texture texture_; public: - Sprite(); + Sprite() = default; Sprite(const Sprite &) = delete; Sprite(Sprite &&) = delete; ~Sprite(); diff --git a/yage/base/window.cpp b/yage/base/window.cpp index a8b6fc02..67f32bb2 100644 --- a/yage/base/window.cpp +++ b/yage/base/window.cpp @@ -6,8 +6,7 @@ * ---------------------------------------------------------------------------- */ -#include -#include +#include "window.h" #include #include @@ -19,60 +18,30 @@ Window::Window() = default; Window::~Window() { - SDL_DestroyWindow(window_); + glfwDestroyWindow(window_); } void Window::create(const std::string &window_name, int width, int height, unsigned flags) { - Uint32 gl_window_states = 0; + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); - // set the correct input flags - if (flags & WindowFlags::SHOWN) { - gl_window_states |= SDL_WINDOW_OPENGL; - } - if (flags & WindowFlags::HIDDEN) { - gl_window_states |= SDL_WINDOW_HIDDEN; - } - if (flags & WindowFlags::FULLSCREEN) { - gl_window_states |= SDL_WINDOW_FULLSCREEN; - } - if (flags & WindowFlags::BORDERLESS) { - gl_window_states |= SDL_WINDOW_BORDERLESS; - } - - // SDL_GL options - - // SDL_GL_SetAttribute (SDL_GL_CONTEXT_MAJOR_VERSION, 4); - // SDL_GL_SetAttribute (SDL_GL_CONTEXT_MINOR_VERSION, 5); - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - - // create the SDL window - window_ = SDL_CreateWindow(window_name.c_str(), SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, width, height, - gl_window_states); + window_ = glfwCreateWindow(width, height, window_name.c_str(), nullptr, nullptr); if (window_ == nullptr) { - throw std::runtime_error("SDL_CreateWindow failed"); + throw std::runtime_error("GLFW Window creation failed"); } - // initialize the GL context in the window - SDL_GLContext gl_context = SDL_GL_CreateContext(window_); - if (gl_context == nullptr) { - throw std::runtime_error("SDL_GL_CreateContext failed"); - } - - // initialize glew - GLenum error = glewInit(); - if (error != GLEW_OK) { - throw std::runtime_error("glewInit failed"); - } + // initialize the gl context + glfwMakeContextCurrent(window_); // print out the current OpenGL version to debug std::cout << "*** OpenGL version: " << glGetString(GL_VERSION) << " ***\n"; - // set vsync on instead of custom fps limiting - SDL_GL_SetSwapInterval(1); + // set vsync on + glfwSwapInterval(1); + // set the clear color to black glClearColor(0.f, 0.5f, 0.f, 1.f); // set alpha blending @@ -83,7 +52,7 @@ void Window::create(const std::string &window_name, int width, int height, void Window::swapBuffer() { // swap the window buffer - SDL_GL_SwapWindow(window_); + glfwSwapBuffers(window_); } void Window::clearBuffer() diff --git a/yage/base/window.h b/yage/base/window.h index 8639e075..eb4cf005 100644 --- a/yage/base/window.h +++ b/yage/base/window.h @@ -6,10 +6,13 @@ * ---------------------------------------------------------------------------- */ +/** @file + */ + #ifndef WINDOW_H #define WINDOW_H -#include +#include #include @@ -24,12 +27,12 @@ enum WindowFlags : unsigned { BORDERLESS = 0x8, }; -// window wrapper around SDL_Window pointer +// window wrapper around GLFWwindow pointer class Window { private: /// window handle - SDL_Window *window_ = nullptr; + GLFWwindow *window_ = nullptr; public: Window(); diff --git a/yage/yage.cpp b/yage/yage.cpp index fa1e5c36..a141d9aa 100644 --- a/yage/yage.cpp +++ b/yage/yage.cpp @@ -8,17 +8,29 @@ #include "yage.h" +#include + +#include + namespace yage { -bool init() +void glfwErrorCallback(int, const char *description) +{ + fprintf(stderr, "ERROR: %s\n", description); +} + +void init() { - return SDL_Init(SDL_INIT_VIDEO); + glfwSetErrorCallback(glfwErrorCallback); + if (!glfwInit()) { + throw std::runtime_error("GLFW couldn't be initialised"); + } } void quit() { - SDL_Quit(); + glfwTerminate(); } } // namespace yage diff --git a/yage/yage.h b/yage/yage.h index f20aff06..4fce87dc 100644 --- a/yage/yage.h +++ b/yage/yage.h @@ -26,15 +26,10 @@ #include "base/vertex.h" #include "base/window.h" +#include "physics/body.h" +#include "physics/particlebody.h" #include "physics/rectanglecollider.h" #include "physics/rigidbody.h" -#include "physics/particlebody.h" -#include "physics/body.h" - - -#include - -#include /** Project namespace. * @@ -44,17 +39,19 @@ namespace yage { +extern void glfwErrorCallback(int, const char *); + /** Initializes yage. * - * This is only there to initialize SDL2. + * This is only there to initialize glfw. * * @return Returns true if the initialization was successful. */ -extern bool init(); +extern void init(); /** Quit and cleanup yage * - * SDL2 needs to clean itself up. + * glfw needs to clean itself up. */ extern void quit(); -- cgit