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 Jul 9, 2017
1 parent bbc0f25 commit 441acda
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 2 deletions.
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 @@ -78,6 +79,8 @@
<Add library="libboost_timer-mgw51-mt-1_64" />
<Add library="libboost_zlib-mgw51-mt-1_64" />
<Add library="libcrypto-1_1" />
<Add library="libglew32" />
<Add library="opengl32" />
<Add directory="./" />
</Linker>
<Unit filename="../../packaging/windows/wesnoth.rc">
Expand Down Expand Up @@ -720,6 +723,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 @@ -886,6 +891,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 @@ -15,6 +15,7 @@

#include "editor/controller/editor_controller.hpp"
#include "editor/editor_display.hpp"
#include "ogl/utils.hpp"
#include "reports.hpp"
#include "team.hpp"
#include "terrain/builder.hpp"
Expand Down Expand Up @@ -59,7 +60,11 @@ editor_display::editor_display(editor_controller& controller, CVideo& video, rep
, 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"

Expand Down Expand Up @@ -661,7 +662,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 @@ -83,7 +84,11 @@ game_display::game_display(game_board& board, CVideo& video, std::weak_ptr<wb::m
needs_rebuild_(false)
{
replace_overlay_map(&overlay_map_);
#ifdef USE_GL_RENDERING
gl::clear_screen();
#else
video.clear_screen();
#endif
}

game_display* game_display::create_dummy_display(CVideo& video)
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 @@ -36,6 +36,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 @@ -276,7 +277,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
10 changes: 8 additions & 2 deletions 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 All @@ -62,6 +64,7 @@ window::window(const std::string& title,
fill(0,0,0);

render();
#endif
}

window::~window()
Expand Down Expand Up @@ -120,7 +123,11 @@ void window::fill(Uint8 r, Uint8 g, Uint8 b, Uint8 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 Expand Up @@ -159,4 +166,3 @@ window::operator SDL_Renderer*()
}

} // namespace sdl

15 changes: 15 additions & 0 deletions src/video.cpp
Expand Up @@ -18,6 +18,7 @@
#include "floating_label.hpp"
#include "image.hpp"
#include "log.hpp"
#include "ogl/utils.hpp"
#include "preferences/general.hpp"
#include "sdl/render_utils.hpp"
#include "sdl/texture.hpp"
Expand Down Expand Up @@ -85,6 +86,9 @@ void trigger_full_redraw() {

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

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

if(preferences::fullscreen()) {
video_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
Expand All @@ -225,6 +232,14 @@ void CVideo::init_window()
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 @@ -22,6 +22,7 @@

#include <SDL_render.h>

#include "ogl/context.hpp"
#include "sdl/window.hpp"

class surface;
Expand Down Expand Up @@ -174,6 +175,11 @@ class CVideo {
static CVideo* singleton_;

std::unique_ptr<sdl::window> window;

#ifdef USE_GL_RENDERING
std::unique_ptr<gl::context> gl_context;
#endif

class video_event_handler : public events::sdl_handler {
public:
virtual void handle_event(const SDL_Event &) {}
Expand Down

0 comments on commit 441acda

Please sign in to comment.