Skip to content

Commit

Permalink
Add the initial SDL_Window wrapper code.
Browse files Browse the repository at this point in the history
With this wrapper in place, Wesnoth shows a main window, all black and a
lot of errors on the console.
  • Loading branch information
mordante committed Mar 8, 2014
1 parent cb4c167 commit ff3588c
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -358,6 +358,7 @@ endif()

set(wesnoth-sdl_SRC
sdl/alpha.cpp
sdl/window.cpp
)

add_library(wesnoth-sdl
Expand Down
3 changes: 3 additions & 0 deletions src/SConscript
Expand Up @@ -150,6 +150,7 @@ libcampaignd = env.Library("campaignd", libcampaignd_sources, OBJPREFIX = "campa
libwesnoth_sdl_sources = Split("""
sdl_utils.cpp
sdl/alpha.cpp
sdl/window.cpp
tracer.cpp
""")
libwesnoth_sdl = client_env.Library("wesnoth_sdl", libwesnoth_sdl_sources)
Expand Down Expand Up @@ -623,6 +624,7 @@ wesmage_sources = Split("""
tools/exploder_utils.cpp
sdl_utils.cpp
sdl/alpha.cpp
sdl/window.cpp
tracer.cpp
loadscreen_empty.cpp
""")
Expand Down Expand Up @@ -666,6 +668,7 @@ create_images_sources = Split("""
tools/exploder_utils.cpp
sdl_utils.cpp
sdl/alpha.cpp
sdl/window.cpp
tracer.cpp
loadscreen_empty.cpp
""")
Expand Down
78 changes: 78 additions & 0 deletions src/sdl/window.cpp
@@ -0,0 +1,78 @@
/*
Copyright (C) 2014 by Mark de Wever <koraq@xs4all.nl>
Part of 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.
*/

#include "sdl/window.hpp"

#if SDL_VERSION_ATLEAST(2, 0, 0)

#include "log.hpp"
#include "wml_exception.hpp"

#include <SDL_render.h>

static lg::log_domain log_display("display");
#define ERR_DP LOG_STREAM(err, log_display)

namespace sdl
{

twindow::twindow(const std::string& title,
const int x,
const int y,
const int w,
const int h,
const Uint32 window_flags,
const Uint32 render_flags)
: window_(SDL_CreateWindow(title.c_str(), x, y, w, h, window_flags))
{
if(!window_) {
ERR_DP << "Failed to create a SDL_Window object with error»"
<< SDL_GetError() << "«.\n";

throw game::error("");
}

if(!SDL_CreateRenderer(window_, -1, render_flags)) {
ERR_DP << "Failed to create a SDL_Window object with error»"
<< SDL_GetError() << "«.\n";

throw game::error("");
}
}

twindow::~twindow()
{
if(window_) {
SDL_DestroyWindow(window_);
}
}

void twindow::set_size(const int w, const int h)
{
SDL_SetWindowSize(window_, w, h);
}

void twindow::full_screen()
{
/** @todo Implement. */
}

twindow::operator SDL_Window*()
{
return window_;
}

} // namespace sdl

#endif
114 changes: 114 additions & 0 deletions src/sdl/window.hpp
@@ -0,0 +1,114 @@
/*
Copyright (C) 2014 by Mark de Wever <koraq@xs4all.nl>
Part of 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.
*/

#ifndef SDL_WINDOW_HPP_INCLUDED
#define SDL_WINDOW_HPP_INCLUDED

/**
* @file
* Contains a wrapper class for the @ref SDL_Window class.
*/

#include <SDL_version.h>

#if SDL_VERSION_ATLEAST(2, 0, 0)

#include <boost/noncopyable.hpp>

#include <SDL_video.h>

#include <string>

namespace sdl
{

/**
* The wrapper class for the @ref SDL_Window class.
*
* At the moment of writing it is not certain yet how many windows will be
* created. At least one as main window, but maybe the GUI dialogues will have
* their own window. Once that is known it might be a good idea to evaluate
* whether the class should become a singleton or not.
*
* The class also wraps several functions operating on @ref SDL_Window objects.
* For functions not wrapped the class offers an implicit conversion operator
* to a pointer to the @ref SDL_Window object it owns.
*/
class twindow : private boost::noncopyable
{
public:
/***** ***** ***** Constructor and destructor. ***** ***** *****/

/**
* Constructor.
*
* The function calls @ref SDL_CreateWindow and @ref SDL_CreateRenderer.
*
* @param title Used as title for @ref SDL_CreateWindow.
* @param x Used as x for @ref SDL_CreateWindow.
* @param y Used as y for @ref SDL_CreateWindow.
* @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.
*/
twindow(const std::string& title,
const int x,
const int y,
const int w,
const int h,
const Uint32 window_flags,
const Uint32 render_flags);

~twindow();


/***** ***** ***** Operations. ***** ***** *****/

/**
* Wrapper for @ref SDL_SetWindowSize.
*
* @param w Used as w for @ref SDL_SetWindowSize.
* @param h Used as x for @ref SDL_SetWindowSize.
*/
void set_size(const int w, const int h);

/**
* Dummy function for setting the screen to full screen mode.
*
* @todo Implement this function properly.
*/
void full_screen();


/***** ***** ***** Conversion operators. ***** ***** *****/

/**
* Conversion operator to a SDL_Window*.
*/
operator SDL_Window*();


private:
/***** ***** ***** Members. ***** ***** *****/

/** The @ref SDL_Window we own. */
SDL_Window* window_;
};

} // namespace sdl

#endif

#endif
42 changes: 39 additions & 3 deletions src/video.cpp
Expand Up @@ -25,6 +25,7 @@
#include "preferences.hpp"
#include "preferences_display.hpp"
#include "sdl_utils.hpp"
#include "sdl/window.hpp"
#include "video.hpp"

#include <boost/foreach.hpp>
Expand Down Expand Up @@ -217,6 +218,9 @@ namespace {

surface frameBuffer = NULL;
bool fake_interactive = false;
#if SDL_VERSION_ATLEAST(2, 0, 0)
sdl::twindow* main_window = NULL;
#endif
}

bool non_interactive()
Expand Down Expand Up @@ -340,6 +344,9 @@ CVideo::~CVideo()
{
LOG_DP << "calling SDL_Quit()\n";
SDL_Quit();
#if SDL_VERSION_ATLEAST(2, 0, 0)
delete main_window;
#endif
LOG_DP << "called SDL_Quit()\n";
}

Expand Down Expand Up @@ -403,11 +410,40 @@ int CVideo::modePossible( int x, int y, int bits_per_pixel, int flags, bool curr
#endif
}

#if SDL_VERSION_ATLEAST(2, 0, 0)
int CVideo::setMode( int x, int y, int bits_per_pixel, int flags )
{
#if SDL_VERSION_ATLEAST(2, 0, 0)
return 0;
update_rects.clear();
if (fake_screen_) return 0;
mode_changed_ = true;

flags = get_flags(flags);

fullScreen = (flags & FULL_SCREEN) != 0;

if(!main_window) {
main_window
= new sdl::twindow("", 0, 0, x, y, flags, SDL_RENDERER_SOFTWARE);
} else {
if(fullScreen) {
main_window->full_screen();
} else {
main_window->set_size(x, y);
}
}

frameBuffer = SDL_GetWindowSurface(*main_window);

if(frameBuffer != NULL) {
image::set_pixel_format(frameBuffer->format);
return bits_per_pixel;
} else {
return 0;
}
}
#else
int CVideo::setMode( int x, int y, int bits_per_pixel, int flags )
{
update_rects.clear();
if (fake_screen_) return 0;
mode_changed_ = true;
Expand All @@ -425,8 +461,8 @@ int CVideo::setMode( int x, int y, int bits_per_pixel, int flags )
image::set_pixel_format(frameBuffer->format);
return bits_per_pixel;
} else return 0;
#endif
}
#endif

bool CVideo::modeChanged()
{
Expand Down

0 comments on commit ff3588c

Please sign in to comment.