Browse Source

Replace GLEW with gl3w, improve CMakeLists, fix some name conflicts and
use MessageBox on Windows

Samuel Čavoj 3 years ago
parent
commit
23825284a8
11 changed files with 80 additions and 36 deletions
  1. 3 0
      .gitmodules
  2. 47 15
      CMakeLists.txt
  3. 1 0
      deps/gl3w
  4. 2 2
      include/asteroid_field.h
  5. 1 1
      include/game.h
  6. 1 1
      include/shaders.h
  7. 9 9
      include/types.h
  8. 1 1
      src/asteroid_field.cc
  9. 3 5
      src/game.cc
  10. 11 1
      src/main.cc
  11. 1 1
      src/shaders.cc

+ 3 - 0
.gitmodules

@@ -7,3 +7,6 @@
 [submodule "deps/SDL_GameControllerDB"]
 	path = deps/SDL_GameControllerDB
 	url = https://github.com/gabomdq/SDL_GameControllerDB.git
+[submodule "deps/gl3w"]
+	path = deps/gl3w
+	url = https://github.com/skaslev/gl3w.git

+ 47 - 15
CMakeLists.txt

@@ -1,30 +1,62 @@
 cmake_minimum_required(VERSION 3.11)
 project(Asteroids)
 
-set (CMAKE_CXX_STANDARD 14)
+# setup deps
+set(GLFW_ROOT "${CMAKE_SOURCE_DIR}/deps/glfw")
+set(GLFW_INC "${GLFW_ROOT}/include")
+
+set(GL3W_ROOT "${CMAKE_SOURCE_DIR}/deps/gl3w")
+set(GL3W_INC "${GL3W_ROOT}/include")
+set(GL3W_SRC "${GL3W_ROOT}/src/gl3w.c")
+
+set(GLM_ROOT "${CMAKE_SOURCE_DIR}/deps/glm")
+set(GLM_INC "${GLM_ROOT}")
+
+if (NOT EXISTS ${GL3W_ROOT} OR NOT EXISTS ${GLFW_ROOT}
+        OR NOT EXISTS ${GLM_ROOT})
+    message(STATUS "Updating submodules")
+    execute_process(COMMAND git submodule init)
+    execute_process(COMMAND git submodule update)
+endif()
+
+if (NOT EXISTS ${GL3W_SRC})
+    message(STATUS "Generating gl3w source files")
+    execute_process(COMMAND python gl3w_gen.py
+                    WORKING_DIRECTORY ${GL3W_ROOT})
+endif()
+
+
+# asteroids target
+file(GLOB CCSOURCES src/*.cc)
+add_executable(asteroids ${CCSOURCES} ${GL3W_SRC})
+
+
+# c++ flags
+set(CMAKE_CXX_STANDARD 14)
+
+if(MINGW)
+    set(CMAKE_EXE_LINKER_FLAGS "-static -static-libgcc -static-libstdc++")
+    set_target_properties(asteroids PROPERTIES LINK_SEARCH_END_STATIC 1)
+endif()
 
-# opengl
-find_package(OpenGL)
 
 # 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(deps/glfw)
-include_directories(deps/glfw/include)
+add_subdirectory(${GLFW_ROOT})
 
-# glew
-find_package(GLEW REQUIRED)
-include_directories(${GLEW_INCLUDE_DIRS})
-
-# glm
-include_directories(deps/glm)
+find_package(OpenGL REQUIRED)
 
 # asteroids
-file(GLOB CCSOURCES src/*.cc)
-include_directories(include)
-add_executable(asteroids ${CCSOURCES})
-target_link_libraries(asteroids glfw ${OPENGL_LIBRARY} ${GLEW_LIBRARIES})
+include_directories(
+    include
+    ${GLFW_INC}
+    ${GL3W_INC}
+    ${GLM_INC}
+)
+target_link_libraries(asteroids glfw ${OPENGL_gl_LIBRARY})
+
 
 add_custom_target(copy_resources ALL
     COMMAND ${CMAKE_COMMAND} -E copy_directory

+ 1 - 0
deps/gl3w

@@ -0,0 +1 @@
+Subproject commit 8f7f459df8725c9614136b49a96023de276219f2

+ 2 - 2
include/asteroid_field.h

@@ -5,7 +5,7 @@
 #include <glm/vec2.hpp>
 #include <glm/vec3.hpp>
 #include <glm/vec4.hpp>
-#include <GL/glew.h>
+#include <GL/gl3w.h>
 
 #include "types.h"
 #include "shaders.h"
@@ -64,7 +64,7 @@ namespace Asteroids {
         }
         const float acceleration = .5;
         const float angular_acceleration = 4.0;
-        input_mode_t input_source = INPUT_KEYBOARD;
+        input_mode_t input_source = INPUT_MODE_KEYBOARD;
         void update(float dt);
         bool is_dead() const {
             return false;

+ 1 - 1
include/game.h

@@ -1,6 +1,6 @@
 #pragma once
 
-#include <GL/glew.h>
+#include <GL/gl3w.h>
 #include <GLFW/glfw3.h>
 
 #include "asteroid_field.h"

+ 1 - 1
include/shaders.h

@@ -2,7 +2,7 @@
 
 #include <string>
 
-#include <GL/glew.h>
+#include <GL/gl3w.h>
 
 
 namespace Asteroids {

+ 9 - 9
include/types.h

@@ -6,15 +6,15 @@ namespace Asteroids {
     };
 
     enum input_mode_t {
-        INPUT_KEYBOARD,
-        INPUT_GP1,
-        INPUT_GP2,
-        INPUT_GP3,
-        INPUT_GP4,
-        INPUT_GP5,
-        INPUT_GP6,
-        INPUT_GP7,
-        INPUT_GP8
+        INPUT_MODE_KEYBOARD,
+        INPUT_MODE_GP1,
+        INPUT_MODE_GP2,
+        INPUT_MODE_GP3,
+        INPUT_MODE_GP4,
+        INPUT_MODE_GP5,
+        INPUT_MODE_GP6,
+        INPUT_MODE_GP7,
+        INPUT_MODE_GP8
     };
 
     struct input_state_t {

+ 1 - 1
src/asteroid_field.cc

@@ -3,7 +3,7 @@
 
 #include <glm/geometric.hpp>
 
-#include <GL/glew.h>
+#include <GL/gl3w.h>
 #include <GLFW/glfw3.h>
 
 #include "asteroid_field.h"

+ 3 - 5
src/game.cc

@@ -72,7 +72,7 @@ void Game::key_cb(GLFWwindow *window, int key,
 }
 
 input_state_t Game::get_input(input_mode_t im) {
-    if (im == INPUT_KEYBOARD) {
+    if (im == INPUT_MODE_KEYBOARD) {
         return this->keyboard_inp;
     } else {
         int jsid = im - 1;
@@ -122,10 +122,8 @@ Game::Game() {
 
     load_controller_mappings();
 
-    glewExperimental = GL_TRUE;
-    GLenum glew_status = glewInit();
-    if (glew_status != GLEW_OK)
-        throw(std::runtime_error("Failed to intialize GLEW."));
+    if (gl3wInit())
+        throw(std::runtime_error("Failed to intialize gl3w."));
 
     glfwSetFramebufferSizeCallback(window, framebuffer_size_cb);
     glfwSetKeyCallback(window, key_cb);

+ 11 - 1
src/main.cc

@@ -1,15 +1,25 @@
 #include <stdexcept>
-#include <iostream>
 #include <memory>
 
 #include "game.h"
 
+#if defined(_WIN32)
+#include <windows.h>
+#else
+#include <iostream>
+#endif
+
 int main() {
     try {
         std::unique_ptr<Asteroids::Game> game(new Asteroids::Game());
         game->run();
     } catch (std::runtime_error &e) {
+#if defined(_WIN32)
+        MessageBox(NULL, e.what(), NULL, MB_OK);
+        return 1;
+#else
         std::cerr << e.what() << std::endl;
         return 1;
+#endif
     }
 }

+ 1 - 1
src/shaders.cc

@@ -5,7 +5,7 @@
 #include <cstdlib>
 #include <cstring>
 #include <cerrno>
-#include <GL/glew.h>
+#include <GL/gl3w.h>
 
 #include "file_util.h"
 #include "shaders.h"