From 695c8eebe813991cd7404defea5fd74afe8bf491 Mon Sep 17 00:00:00 2001 From: Jyrki Vesterinen Date: Tue, 27 Mar 2018 21:33:37 +0300 Subject: [PATCH] Integrate GLEW to the Visual Studio project I also removed USE_GL_RENDERING (OpenGL rendering is now used unconditionally), moved initialization of gl::context out of the constructor, and added some light logging about OpenGL (so that, in the future with hardware-specific issues, we have a way to know which hardware is affected). This commit breaks build with anything except Visual Studio due to requiring OpenGL and GLEW but not linking to them. --- projectfiles/VC14/wesnoth.vcxproj | 20 ++++++------ src/editor/editor_display.cpp | 4 --- src/events.cpp | 4 --- src/game_display.cpp | 5 --- src/ogl/context.cpp | 53 +++++++++++++++++++++++-------- src/ogl/context.hpp | 19 +++-------- src/ogl/utils.cpp | 6 +--- src/ogl/utils.hpp | 4 --- src/playsingle_controller.cpp | 4 --- src/sdl/window.cpp | 38 +--------------------- src/sdl/window.hpp | 4 +-- src/video.cpp | 14 +++----- src/video.hpp | 4 +-- 13 files changed, 62 insertions(+), 117 deletions(-) diff --git a/projectfiles/VC14/wesnoth.vcxproj b/projectfiles/VC14/wesnoth.vcxproj index 0758fca871fe..1369694fa29e 100644 --- a/projectfiles/VC14/wesnoth.vcxproj +++ b/projectfiles/VC14/wesnoth.vcxproj @@ -119,7 +119,7 @@ Disabled ..\..\src;..\..\..\external\include;%(AdditionalIncludeDirectories) - WIN32;_WINSOCK_DEPRECATED_NO_WARNINGS;BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE;_DEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;_WIN32_WINNT=_WIN32_WINNT_WIN7;HAVE_PYTHON;USE_GZIP;NO_HAVE_FRIBIDI;HAVE_LIBPNG;NOMINMAX;_SCL_SECURE_NO_WARNINGS;DEBUG_LUA;%(PreprocessorDefinitions) + WIN32;GLEW_STATIC;_WINSOCK_DEPRECATED_NO_WARNINGS;BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE;_DEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;_WIN32_WINNT=_WIN32_WINNT_WIN7;HAVE_PYTHON;USE_GZIP;NO_HAVE_FRIBIDI;HAVE_LIBPNG;NOMINMAX;_SCL_SECURE_NO_WARNINGS;DEBUG_LUA;%(PreprocessorDefinitions) UninitializedLocalUsageCheck MultiThreadedDebugDLL false @@ -136,7 +136,7 @@ /SAFESEH:NO %(AdditionalOptions) - SDL2main.lib;SDL2.lib;SDL2_image.lib;SDL2_ttf.lib;SDL2_mixer.lib;libcrypto.lib;cairo.lib;winmm.lib;ws2_32.lib;pango-1.0.lib;pangocairo-1.0.lib;gobject-2.0.lib;glib-2.0.lib;libpng.lib;$(IntDir)liblua.lib;Shlwapi.lib;%(AdditionalDependencies) + SDL2main.lib;SDL2.lib;SDL2_image.lib;opengl32.lib;glew32s.lib;SDL2_ttf.lib;SDL2_mixer.lib;libcrypto.lib;cairo.lib;winmm.lib;ws2_32.lib;pango-1.0.lib;pangocairo-1.0.lib;gobject-2.0.lib;glib-2.0.lib;libpng.lib;$(IntDir)liblua.lib;Shlwapi.lib;%(AdditionalDependencies) ..\..\..\external\lib;%(AdditionalLibraryDirectories) MSVCR90;MSVCRT;%(IgnoreSpecificDefaultLibraries) DebugFastLink @@ -156,7 +156,7 @@ true false ..\..\src;..\..\..\external\include;%(AdditionalIncludeDirectories) - WIN32;_WINSOCK_DEPRECATED_NO_WARNINGS;BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE;_WINDOWS;_CRT_SECURE_NO_WARNINGS;_WIN32_WINNT=_WIN32_WINNT_WIN7;HAVE_PYTHON;USE_GZIP;NO_HAVE_FRIBIDI;HAVE_LIBPNG;NOMINMAX;_SCL_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + WIN32;GLEW_STATIC;_WINSOCK_DEPRECATED_NO_WARNINGS;BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE;_WINDOWS;_CRT_SECURE_NO_WARNINGS;_WIN32_WINNT=_WIN32_WINNT_WIN7;HAVE_PYTHON;USE_GZIP;NO_HAVE_FRIBIDI;HAVE_LIBPNG;NOMINMAX;_SCL_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) MultiThreadedDLL false true @@ -175,7 +175,7 @@ true - SDL2main.lib;SDL2.lib;SDL2_image.lib;SDL2_mixer.lib;libcrypto.lib;cairo.lib;winmm.lib;ws2_32.lib;pango-1.0.lib;pangocairo-1.0.lib;gobject-2.0.lib;glib-2.0.lib;libpng.lib;$(IntDir)liblua.lib;Shlwapi.lib;%(AdditionalDependencies) + SDL2main.lib;SDL2.lib;SDL2_image.lib;opengl32.lib;glew32s.lib;SDL2_mixer.lib;libcrypto.lib;cairo.lib;winmm.lib;ws2_32.lib;pango-1.0.lib;pangocairo-1.0.lib;gobject-2.0.lib;glib-2.0.lib;libpng.lib;$(IntDir)liblua.lib;Shlwapi.lib;%(AdditionalDependencies) ..\..\..\external\lib;%(AdditionalLibraryDirectories) false Windows @@ -199,7 +199,7 @@ Disabled ..\..\src;..\..\..\external\include;%(AdditionalIncludeDirectories) - WIN32;_WINSOCK_DEPRECATED_NO_WARNINGS;BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE;_DEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;_WIN32_WINNT=_WIN32_WINNT_WIN7;HAVE_PYTHON;USE_GZIP;NO_HAVE_FRIBIDI;HAVE_LIBPNG;NOMINMAX;_SCL_SECURE_NO_WARNINGS;DEBUG_LUA;MSVC_DO_UNIT_TESTS;BOOST_TEST_DYN_LINK;%(PreprocessorDefinitions) + WIN32;GLEW_STATIC;_WINSOCK_DEPRECATED_NO_WARNINGS;BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE;_DEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;_WIN32_WINNT=_WIN32_WINNT_WIN7;HAVE_PYTHON;USE_GZIP;NO_HAVE_FRIBIDI;HAVE_LIBPNG;NOMINMAX;_SCL_SECURE_NO_WARNINGS;DEBUG_LUA;MSVC_DO_UNIT_TESTS;BOOST_TEST_DYN_LINK;%(PreprocessorDefinitions) EnableFastChecks MultiThreadedDebugDLL false @@ -215,7 +215,7 @@ _MSC_VER;%(PreprocessorDefinitions) - SDL2main.lib;SDL2.lib;SDL2_image.lib;SDL2_ttf.lib;SDL2_mixer.lib;libcrypto.lib;cairo.lib;winmm.lib;ws2_32.lib;pango-1.0.lib;pangocairo-1.0.lib;gobject-2.0.lib;glib-2.0.lib;libpng.lib;Debug\liblua.lib;Shlwapi.lib;%(AdditionalDependencies) + SDL2main.lib;SDL2.lib;SDL2_image.lib;opengl32.lib;glew32s.lib;SDL2_ttf.lib;SDL2_mixer.lib;libcrypto.lib;cairo.lib;winmm.lib;ws2_32.lib;pango-1.0.lib;pangocairo-1.0.lib;gobject-2.0.lib;glib-2.0.lib;libpng.lib;Debug\liblua.lib;Shlwapi.lib;%(AdditionalDependencies) $(OutDir)test.exe ..\..\..\external\lib;%(AdditionalLibraryDirectories) DebugFastLink @@ -235,7 +235,7 @@ true false ..\..\src;..\..\..\external\include;%(AdditionalIncludeDirectories) - WIN32;_WINSOCK_DEPRECATED_NO_WARNINGS;BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE;_WINDOWS;_CRT_SECURE_NO_WARNINGS;_WIN32_WINNT=_WIN32_WINNT_WIN7;HAVE_PYTHON;USE_GZIP;NO_HAVE_FRIBIDI;HAVE_LIBPNG;NOMINMAX;_SCL_SECURE_NO_WARNINGS;MSVC_DO_UNIT_TESTS;BOOST_TEST_DYN_LINK;%(PreprocessorDefinitions) + WIN32;GLEW_STATIC;_WINSOCK_DEPRECATED_NO_WARNINGS;BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE;_WINDOWS;_CRT_SECURE_NO_WARNINGS;_WIN32_WINNT=_WIN32_WINNT_WIN7;HAVE_PYTHON;USE_GZIP;NO_HAVE_FRIBIDI;HAVE_LIBPNG;NOMINMAX;_SCL_SECURE_NO_WARNINGS;MSVC_DO_UNIT_TESTS;BOOST_TEST_DYN_LINK;%(PreprocessorDefinitions) MultiThreadedDLL false true @@ -254,7 +254,7 @@ true - SDL2main.lib;SDL2.lib;SDL2_image.lib;SDL2_ttf.lib;SDL2_mixer.lib;libcrypto.lib;cairo.lib;winmm.lib;ws2_32.lib;pango-1.0.lib;pangocairo-1.0.lib;gobject-2.0.lib;glib-2.0.lib;libpng.lib;Release\liblua.lib;Shlwapi.lib;%(AdditionalDependencies) + SDL2main.lib;SDL2.lib;SDL2_image.lib;opengl32.lib;glew32s.lib;SDL2_ttf.lib;SDL2_mixer.lib;libcrypto.lib;cairo.lib;winmm.lib;ws2_32.lib;pango-1.0.lib;pangocairo-1.0.lib;gobject-2.0.lib;glib-2.0.lib;libpng.lib;Release\liblua.lib;Shlwapi.lib;%(AdditionalDependencies) $(OutDir)test.exe ..\..\..\external\lib;%(AdditionalLibraryDirectories) false @@ -281,7 +281,7 @@ true false ..\..\src;..\..\..\external\include;%(AdditionalIncludeDirectories) - WIN32;_WINSOCK_DEPRECATED_NO_WARNINGS;BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE;_WINDOWS;_CRT_SECURE_NO_WARNINGS;_WIN32_WINNT=_WIN32_WINNT_WIN7;HAVE_PYTHON;USE_GZIP;NO_HAVE_FRIBIDI;HAVE_LIBPNG;NOMINMAX;_SCL_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + WIN32;GLEW_STATIC;_WINSOCK_DEPRECATED_NO_WARNINGS;BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE;_WINDOWS;_CRT_SECURE_NO_WARNINGS;_WIN32_WINNT=_WIN32_WINNT_WIN7;HAVE_PYTHON;USE_GZIP;NO_HAVE_FRIBIDI;HAVE_LIBPNG;NOMINMAX;_SCL_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) MultiThreadedDLL false true @@ -300,7 +300,7 @@ true - SDL2main.lib;SDL2.lib;SDL2_image.lib;SDL2_ttf.lib;SDL2_mixer.lib;libcrypto.lib;cairo.lib;winmm.lib;ws2_32.lib;pango-1.0.lib;pangocairo-1.0.lib;gobject-2.0.lib;glib-2.0.lib;libpng.lib;$(IntDir)liblua.lib;Shlwapi.lib;%(AdditionalDependencies) + SDL2main.lib;SDL2.lib;SDL2_image.lib;opengl32.lib;glew32s.lib;SDL2_ttf.lib;SDL2_mixer.lib;libcrypto.lib;cairo.lib;winmm.lib;ws2_32.lib;pango-1.0.lib;pangocairo-1.0.lib;gobject-2.0.lib;glib-2.0.lib;libpng.lib;$(IntDir)liblua.lib;Shlwapi.lib;%(AdditionalDependencies) ..\..\..\external\lib;%(AdditionalLibraryDirectories) DebugFastLink Windows diff --git a/src/editor/editor_display.cpp b/src/editor/editor_display.cpp index faacf290bf95..e740378797a5 100644 --- a/src/editor/editor_display.cpp +++ b/src/editor/editor_display.cpp @@ -35,11 +35,7 @@ editor_display::editor_display(editor_controller& controller, const config& them , brush_locations_() , controller_(controller) { -#ifdef USE_GL_RENDERING gl::clear_screen(); -#else - video().clear_screen(); -#endif } void editor_display::add_brush_loc(const map_location& hex) diff --git a/src/events.cpp b/src/events.cpp index 1c010256fc32..890a526ec2bd 100644 --- a/src/events.cpp +++ b/src/events.cpp @@ -623,11 +623,7 @@ void run_event_loop() // CVideo& video = CVideo::get_singleton(); -#ifdef USE_GL_RENDERING gl::clear_screen(); -#else - video.clear_screen(); -#endif raise_draw_event(); diff --git a/src/game_display.cpp b/src/game_display.cpp index 0d1404e6c7a8..89414cfb8cd7 100644 --- a/src/game_display.cpp +++ b/src/game_display.cpp @@ -71,12 +71,7 @@ game_display::game_display(game_board& board, std::weak_ptr wb, , mode_(RUNNING) , needs_rebuild_(false) { -#ifdef USE_GL_RENDERING gl::clear_screen(); -#else - video().clear_screen(); -#endif - } game_display::~game_display() diff --git a/src/ogl/context.cpp b/src/ogl/context.cpp index 0aa6f2ad9d79..9104a14227ac 100644 --- a/src/ogl/context.cpp +++ b/src/ogl/context.cpp @@ -11,29 +11,58 @@ See the COPYING file for more details. */ -#ifdef USE_GL_RENDERING - #include "ogl/context.hpp" -#include +#include "log.hpp" + +#include +#include + +#include +#include + +static lg::log_domain log_opengl("opengl"); +#define LOG_GL LOG_STREAM(info, log_opengl) +#define ERR_GL LOG_STREAM(err, log_opengl) namespace gl { -context::context(sdl::window* window) - : gl_context_(SDL_GL_CreateContext(*window)) + +context::~context() +{ + SDL_GL_DeleteContext(gl_context_); +} + +void context::init(sdl::window* window) { // Set flags. set_context_flags(); + // Attempt to create the context. + gl_context_ = SDL_GL_CreateContext(*window); + if(gl_context_ == nullptr) { + ERR_GL << "Error creating OpenGL context: too old hardware/drivers?\n"; + ERR_GL << SDL_GetError() << std::endl; + throw std::runtime_error("error creating OpenGL context"); + } + // Initialize GLEW. - // TODO: should this be moved somewhere else? glewExperimental = GL_TRUE; - glewInit(); -} + GLenum result = glewInit(); + if(result != GLEW_OK) { + ERR_GL << "Error initializing GLEW\n"; + throw std::runtime_error("error initializing GLEW"); + } -context::~context() -{ - SDL_GL_DeleteContext(gl_context_); + // Print some information. + GLint profile; + glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &profile); + bool core_profile = profile & GL_CONTEXT_CORE_PROFILE_BIT; + + LOG_GL << "Using OpenGL " << (core_profile ? "core profile " : "") << + "version " << glGetString(GL_VERSION) << std::endl; + LOG_GL << "GPU: " << + glGetString(GL_VENDOR) << " " << glGetString(GL_RENDERER) << std::endl; } void context::set_context_flags() @@ -53,5 +82,3 @@ void context::set_context_flags() } } - -#endif // USE_GL_RENDERING diff --git a/src/ogl/context.hpp b/src/ogl/context.hpp index 11bd8e2e5399..697632c657ef 100644 --- a/src/ogl/context.hpp +++ b/src/ogl/context.hpp @@ -13,39 +13,28 @@ #pragma once -#ifdef USE_GL_RENDERING - #include "sdl/window.hpp" -#include -#include - namespace gl { /** Encapsulates the management of an OpenGL context for the current window. */ class context { public: + context() = default; context(const context&) = delete; context& operator=(const context&) = delete; - /** - * Constructor - * - * @param window The SDL window to attach a context to. - */ - context(sdl::window* window); - ~context(); + void init(sdl::window* window); + private: /** Sets any relevant flags for the GL context. */ void set_context_flags(); /** The window's OpenGL context. */ - SDL_GLContext gl_context_; + SDL_GLContext gl_context_ = nullptr; }; } // namespace gl - -#endif // USE_GL_RENDERING diff --git a/src/ogl/utils.cpp b/src/ogl/utils.cpp index 6cc2cc74fb74..8b25c33176d9 100644 --- a/src/ogl/utils.cpp +++ b/src/ogl/utils.cpp @@ -11,11 +11,9 @@ See the COPYING file for more details. */ -#ifdef USE_GL_RENDERING - #include "ogl/utils.hpp" -#include +#include namespace gl { @@ -27,5 +25,3 @@ void clear_screen() } } // namespace gl - -#endif // USE_GL_RENDERING diff --git a/src/ogl/utils.hpp b/src/ogl/utils.hpp index 60c8d91513ca..952950a61de0 100644 --- a/src/ogl/utils.hpp +++ b/src/ogl/utils.hpp @@ -13,13 +13,9 @@ #pragma once -#ifdef USE_GL_RENDERING - namespace gl { /** Fills the screen with fully-transparent black. */ void clear_screen(); } // namespace gl - -#endif // USE_GL_RENDERING diff --git a/src/playsingle_controller.cpp b/src/playsingle_controller.cpp index f8331a19d79c..93fd935ff3ec 100644 --- a/src/playsingle_controller.cpp +++ b/src/playsingle_controller.cpp @@ -269,11 +269,7 @@ LEVEL_RESULT playsingle_controller::play_scenario(const config& level) const bool is_victory = get_end_level_data_const().is_victory; if(gamestate().gamedata_.phase() <= game_data::PRESTART) { -#ifdef USE_GL_RENDERING gl::clear_screen(); -#else - gui_->video().clear_screen(); -#endif } ai_testing::log_game_end(); diff --git a/src/sdl/window.cpp b/src/sdl/window.cpp index 484e10c5d827..59850d8037d8 100644 --- a/src/sdl/window.cpp +++ b/src/sdl/window.cpp @@ -27,45 +27,13 @@ window::window(const std::string& title, const int y, const int w, const int h, - const uint32_t window_flags, - const uint32_t render_flags) + const uint32_t window_flags) : window_(SDL_CreateWindow(title.c_str(), x, y, w, h, window_flags)) , info_() { if(!window_) { throw exception("Failed to create a SDL_Window object.", true); } - -#ifndef USE_GL_RENDERING - if(!SDL_CreateRenderer(window_, -1, render_flags)) { - throw exception("Failed to create a SDL_Renderer object.", true); - } - - if(SDL_GetRendererInfo(*this, &info_) != 0) { - throw exception("Failed to retrieve the information of the renderer.", - true); - } - - if(info_.num_texture_formats == 0) { - throw exception("The renderer has no texture information available.\n", - false); - } - - if(!(info_.flags & SDL_RENDERER_TARGETTEXTURE)) { - throw exception("Render-to-texture not supported or enabled!", false); - } - - // Set default blend mode to blend. - SDL_SetRenderDrawBlendMode(*this, SDL_BLENDMODE_BLEND); - - // In fullscreen mode, do not minimize on focus loss. - // Minimizing was reported as bug #1606 with blocker priority. - SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0"); - - fill(0,0,0); - - render(); -#endif } window::~window() @@ -132,11 +100,7 @@ void window::fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) void window::render() { -#ifdef USE_GL_RENDERING SDL_GL_SwapWindow(*this); -#else - SDL_RenderPresent(*this); -#endif } void window::set_title(const std::string& title) diff --git a/src/sdl/window.hpp b/src/sdl/window.hpp index db9ae8fc4d46..e040d38f1750 100644 --- a/src/sdl/window.hpp +++ b/src/sdl/window.hpp @@ -61,15 +61,13 @@ class window * @param w Used as w for @ref SDL_CreateWindow. * @param h Used as x for @ref SDL_CreateWindow. * @param window_flags Used as flags for @ref SDL_CreateWindow. - * @param render_flags Used as flags for @ref SDL_CreateRenderer. */ window(const std::string& title, const int x, const int y, const int w, const int h, - const uint32_t window_flags, - const uint32_t render_flags); + const uint32_t window_flags); ~window(); diff --git a/src/video.cpp b/src/video.cpp index 411f75f679d7..59646270ca7b 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -43,9 +43,7 @@ bool interactive = true; CVideo::CVideo(FAKE_TYPES type) : window() -#ifdef USE_GL_RENDERING - , gl_context() -#endif + , gl_context_() , fake_screen_(false) , fake_size_(0u, 0u) , updated_locked_(0) @@ -133,12 +131,10 @@ void CVideo::init_window() // Add any more default flags here window_flags |= SDL_WINDOW_RESIZABLE; + window_flags |= SDL_WINDOW_OPENGL; #ifdef __APPLE__ window_flags |= SDL_WINDOW_ALLOW_HIGHDPI; #endif -#ifdef USE_GL_RENDERING - video_flags |= SDL_WINDOW_OPENGL; -#endif if(preferences::fullscreen()) { window_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; @@ -147,19 +143,17 @@ void CVideo::init_window() } // Initialize window - window.reset(new sdl::window("", x, y, w, h, window_flags, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE)); + window.reset(new sdl::window("", x, y, w, h, window_flags)); std::cerr << "Setting mode to " << w << "x" << h << std::endl; window->set_minimum_size(preferences::min_window_width, preferences::min_window_height); -#ifdef USE_GL_RENDERING // Initialize an OpenGL context for the window. - gl_context.reset(new gl::context(window.get())); + gl_context_.init(window.get()); gl::clear_screen(); render_screen(); -#endif event_handler_.join_global(); } diff --git a/src/video.hpp b/src/video.hpp index 64d14c130b5f..adba40f39661 100644 --- a/src/video.hpp +++ b/src/video.hpp @@ -217,10 +217,8 @@ class CVideo /** The SDL window object. */ std::unique_ptr window; -#ifdef USE_GL_RENDERING /** The OpenGL context attached to the SDL window. */ - std::unique_ptr gl_context; -#endif + gl::context gl_context_; /** Initializes the SDL video subsystem. */ void initSDL();