Skip to content

Commit

Permalink
Some initial code (disabled for now) for setting up an OGL context
Browse files Browse the repository at this point in the history
In the interests of getting this all done faster, I've decided to postpone work on the OGL implementation for now
and focus on the SDL_Renderer version. This contains some basic code (include-guarded) for setting up an OGL context
for the main SDL window.
  • Loading branch information
Vultraz committed Mar 13, 2018
1 parent ec7df68 commit 6094c89
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 1 deletion.
9 changes: 9 additions & 0 deletions projectfiles/CodeBlocks/wesnoth.cbp
Expand Up @@ -43,6 +43,7 @@
<Add option="-D_WIN32_WINDOWS" />
<Add option="-D_WIN32_WINNT=0x0501" />
<Add option="-D_WIN32_IE=0x0501" />
<Add option="-DGLEW_STATIC" />
<Add directory="../../src" />
</Compiler>
<Linker>
Expand Down Expand Up @@ -79,6 +80,8 @@
<Add library="libboost_zlib-mgw51-mt-1_64" />
<Add library="libcrypto-1_1" />
<Add library="winmm" />
<Add library="libglew32" />
<Add library="opengl32" />
<Add directory="./" />
</Linker>
<Unit filename="../../packaging/windows/wesnoth.rc">
Expand Down Expand Up @@ -736,6 +739,8 @@
<Unit filename="../../src/gui/widgets/list.hpp" />
<Unit filename="../../src/gui/widgets/listbox.cpp" />
<Unit filename="../../src/gui/widgets/listbox.hpp" />
<Unit filename="../../src/gui/widgets/map_viewer.cpp" />
<Unit filename="../../src/gui/widgets/map_viewer.hpp" />
<Unit filename="../../src/gui/widgets/matrix.cpp" />
<Unit filename="../../src/gui/widgets/matrix.hpp" />
<Unit filename="../../src/gui/widgets/menu_button.cpp" />
Expand Down Expand Up @@ -904,6 +909,10 @@
<Unit filename="../../src/multiplayer_error_codes.hpp" />
<Unit filename="../../src/network_asio.cpp" />
<Unit filename="../../src/network_asio.hpp" />
<Unit filename="../../src/ogl/context.cpp" />
<Unit filename="../../src/ogl/context.hpp" />
<Unit filename="../../src/ogl/utils.cpp" />
<Unit filename="../../src/ogl/utils.hpp" />
<Unit filename="../../src/overlay.hpp" />
<Unit filename="../../src/pathfind/astarsearch.cpp" />
<Unit filename="../../src/pathfind/pathfind.cpp" />
Expand Down
2 changes: 2 additions & 0 deletions source_lists/libwesnoth_sdl
@@ -1,3 +1,5 @@
ogl/context.cpp
ogl/utils.cpp
sdl/exception.cpp
sdl/rect.cpp
sdl/surface.cpp
Expand Down
5 changes: 5 additions & 0 deletions src/editor/editor_display.cpp
Expand Up @@ -16,6 +16,7 @@
#include "editor/controller/editor_controller.hpp"
#include "editor/editor_display.hpp"
#include "lexical_cast.hpp"
#include "ogl/utils.hpp"
#include "reports.hpp"
#include "team.hpp"
#include "terrain/builder.hpp"
Expand All @@ -32,7 +33,11 @@ editor_display::editor_display(editor_controller& controller, reports& reports_o
, 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)
Expand Down
5 changes: 5 additions & 0 deletions src/events.cpp
Expand Up @@ -16,6 +16,7 @@
#include "cursor.hpp"
#include "desktop/clipboard.hpp"
#include "log.hpp"
#include "ogl/utils.hpp"
#include "quit_confirmation.hpp"
#include "video.hpp"
#include "sdl/userevent.hpp"
Expand Down Expand Up @@ -658,7 +659,11 @@ void pump()
const bool is_draw_event = event.type == DRAW_EVENT || event.type == DRAW_ALL_EVENT;

if(is_draw_event) {
#ifdef USE_GL_RENDERING
gl::clear_screen();
#else
CVideo::get_singleton().clear_screen();
#endif
}

for(auto global_handler : event_contexts.front().handlers) {
Expand Down
5 changes: 5 additions & 0 deletions src/game_display.cpp
Expand Up @@ -33,6 +33,7 @@
#include "log.hpp"
#include "map/map.hpp"
#include "map/label.hpp"
#include "ogl/utils.hpp"
#include "font/standard_colors.hpp"
#include "reports.hpp"
#include "resources.hpp"
Expand Down Expand Up @@ -80,7 +81,11 @@ game_display::game_display(game_board& board, std::weak_ptr<wb::manager> wb,
needs_rebuild_(false)
{
replace_overlay_map(&overlay_map_);
#ifdef USE_GL_RENDERING
gl::clear_screen();
#else
video().clear_screen();
#endif
}

game_display::~game_display()
Expand Down
57 changes: 57 additions & 0 deletions src/ogl/context.cpp
@@ -0,0 +1,57 @@
/*
Copyright (C) 2017 by the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#ifdef USE_GL_RENDERING

#include "ogl/context.hpp"

#include <SDL_video.h>

namespace gl
{
context::context(sdl::window* window)
: gl_context_(SDL_GL_CreateContext(*window))
{
// Set flags.
set_context_flags();

// Initialize GLEW.
// TODO: should this be moved somewhere else?
glewExperimental = GL_TRUE;
glewInit();
}

context::~context()
{
SDL_GL_DeleteContext(gl_context_);
}

void context::set_context_flags()
{
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);

// Turn on double buffering.
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

// Enable VSync (sync buffer refresh with monitor refresh rate).
SDL_GL_SetSwapInterval(1);
}

}

#endif // USE_GL_RENDERING
51 changes: 51 additions & 0 deletions src/ogl/context.hpp
@@ -0,0 +1,51 @@
/*
Copyright (C) 2017 by the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#pragma once

#ifdef USE_GL_RENDERING

#include "sdl/window.hpp"

#include <GL/glew.h>
#include <GL/gl.h>

namespace gl
{
/** Encapsulates the management of an OpenGL context for the current window. */
class context
{
public:
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();

private:
/** Sets any relevant flags for the GL context. */
void set_context_flags();

/** The window's OpenGL context. */
SDL_GLContext gl_context_;
};

} // namespace gl

#endif // USE_GL_RENDERING
31 changes: 31 additions & 0 deletions src/ogl/utils.cpp
@@ -0,0 +1,31 @@
/*
Copyright (C) 2017 by the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#ifdef USE_GL_RENDERING

#include "ogl/utils.hpp"

#include <GL/gl.h>

namespace gl
{
void clear_screen()
{
// Fully alpha black
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
}

} // namespace gl

#endif // USE_GL_RENDERING
25 changes: 25 additions & 0 deletions src/ogl/utils.hpp
@@ -0,0 +1,25 @@
/*
Copyright (C) 2017 by the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#pragma once

#ifdef USE_GL_RENDERING

namespace gl
{
/** Fills the screen with fully-transparent black. */
void clear_screen();

} // namespace gl

#endif // USE_GL_RENDERING
5 changes: 5 additions & 0 deletions src/playsingle_controller.cpp
Expand Up @@ -35,6 +35,7 @@
#include "log.hpp"
#include "map/label.hpp"
#include "map/map.hpp"
#include "ogl/utils.hpp"
#include "playturn.hpp"
#include "random_deterministic.hpp"
#include "replay_helper.hpp"
Expand Down Expand Up @@ -273,7 +274,11 @@ 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();
Expand Down
9 changes: 8 additions & 1 deletion src/sdl/window.cpp
Expand Up @@ -12,9 +12,10 @@
See the COPYING file for more details.
*/

#include "sdl/surface.hpp"
#include "sdl/window.hpp"

#include "ogl/utils.hpp"
#include "sdl/surface.hpp"
#include "sdl/exception.hpp"

#include <SDL_render.h>
Expand All @@ -36,6 +37,7 @@ window::window(const std::string& title,
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);
}
Expand Down Expand Up @@ -66,6 +68,7 @@ window::window(const std::string& title,
fill(0,0,0);

render();
#endif
}

window::~window()
Expand Down Expand Up @@ -132,7 +135,11 @@ 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)
Expand Down
15 changes: 15 additions & 0 deletions src/video.cpp
Expand Up @@ -19,6 +19,7 @@
#include "font/sdl_ttf.hpp"
#include "image.hpp"
#include "log.hpp"
#include "ogl/utils.hpp"
#include "preferences/general.hpp"
#include "sdl/point.hpp"
#include "sdl/render_utils.hpp"
Expand Down Expand Up @@ -86,6 +87,9 @@ void trigger_full_redraw()

CVideo::CVideo(FAKE_TYPES type)
: window()
#ifdef USE_GL_RENDERING
, gl_context()
#endif
, fake_screen_(false)
, help_string_(0)
, updated_locked_(0)
Expand Down Expand Up @@ -200,6 +204,9 @@ void CVideo::init_window()

// Add any more default flags here
window_flags |= SDL_WINDOW_RESIZABLE;
#ifdef USE_GL_RENDERING
video_flags |= SDL_WINDOW_OPENGL;
#endif

if(preferences::fullscreen()) {
window_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
Expand All @@ -214,6 +221,14 @@ void CVideo::init_window()

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::clear_screen();
render_screen();
#endif

event_handler_.join_global();
}

Expand Down
6 changes: 6 additions & 0 deletions src/video.hpp
Expand Up @@ -17,6 +17,7 @@
#include "events.hpp"
#include "exceptions.hpp"
#include "lua_jailbreak_exception.hpp"
#include "ogl/context.hpp"

#include <SDL_render.h>

Expand Down Expand Up @@ -246,6 +247,11 @@ class CVideo
/** The SDL window object. */
std::unique_ptr<sdl::window> window;

#ifdef USE_GL_RENDERING
/** The OpenGL context attached to the SDL window. */
std::unique_ptr<gl::context> gl_context;
#endif

/** Initializes the SDL video subsystem. */
void initSDL();

Expand Down

0 comments on commit 6094c89

Please sign in to comment.