Adds a basic version of the SDL2 test tool.
The tool is used to test parts of the new SDL2 code, without the need to directly modify the main Wesnoth code.
This commit is contained in:
parent
d20c12019b
commit
525cd6ea42
3 changed files with 205 additions and 0 deletions
|
@ -78,6 +78,7 @@ option(ENABLE_GAME "Enable compilation of the game" ON)
|
|||
option(ENABLE_CAMPAIGN_SERVER "Enable compilation of campaign server")
|
||||
option(ENABLE_SERVER "Enable compilation of server" ON)
|
||||
option(ENABLE_TOOLS "Enable building and installation of tools for artists and WML maintainers")
|
||||
option(ENABLE_SDL2_TOOLS "Enable building and installation of tools for testing with SDL2" OFF)
|
||||
option(ENABLE_TESTS "Build unit tests")
|
||||
option(ENABLE_NLS "Enable building of translations" ON)
|
||||
option(ENABLE_LOW_MEM "Reduce memory usage by removing extra functionality" OFF)
|
||||
|
|
|
@ -1202,6 +1202,35 @@ set_target_properties(wesmage PROPERTIES OUTPUT_NAME ${BINARY_PREFIX}wesmage${BI
|
|||
install(TARGETS wesmage DESTINATION ${BINDIR})
|
||||
|
||||
endif(ENABLE_TOOLS)
|
||||
|
||||
|
||||
########### SDL2 test tool ###############
|
||||
|
||||
if(ENABLE_SDL2_TOOLS)
|
||||
|
||||
add_executable(sdl2
|
||||
tools/sdl2/sdl2.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(sdl2
|
||||
wesnoth-sdl
|
||||
wesnoth-core
|
||||
wesnoth-game
|
||||
wesnoth-gui_types
|
||||
wesnoth-gui_event
|
||||
wesnoth-gui_iterator
|
||||
wesnoth-gui_placer
|
||||
wesnoth-gui_tooltip
|
||||
wesnoth-gui_widget
|
||||
wesnoth-gui_widget_definition
|
||||
wesnoth-gui1_widgets
|
||||
wesnoth-schema_validator
|
||||
wesnoth-lua
|
||||
${game-external-libs}
|
||||
)
|
||||
|
||||
endif()
|
||||
|
||||
########### Unit tests ###############
|
||||
|
||||
if(ENABLE_TESTS)
|
||||
|
|
175
src/tools/sdl2/sdl2.cpp
Normal file
175
src/tools/sdl2/sdl2.cpp
Normal file
|
@ -0,0 +1,175 @@
|
|||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Tool to test SDL 2 functions.
|
||||
*/
|
||||
|
||||
#include "sdl/texture.hpp"
|
||||
#include "sdl/window.hpp"
|
||||
#include "text.hpp"
|
||||
|
||||
#include <SDL.h>
|
||||
|
||||
#include <boost/foreach.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <deque>
|
||||
|
||||
struct texit
|
||||
{
|
||||
};
|
||||
|
||||
struct tsdl
|
||||
{
|
||||
tsdl()
|
||||
{
|
||||
if(SDL_Init(SDL_INIT_VIDEO) != 0) {
|
||||
std::cerr << "Unable to initialise SDL with error »"
|
||||
<< SDL_GetError() << "«.\n";
|
||||
|
||||
throw texit();
|
||||
}
|
||||
}
|
||||
|
||||
~tsdl()
|
||||
{
|
||||
|
||||
SDL_Quit();
|
||||
}
|
||||
};
|
||||
|
||||
struct ttext_input
|
||||
{
|
||||
ttext_input()
|
||||
{
|
||||
SDL_StartTextInput();
|
||||
}
|
||||
|
||||
~ttext_input()
|
||||
{
|
||||
SDL_StopTextInput();
|
||||
}
|
||||
};
|
||||
|
||||
static void draw_text(sdl::twindow& window,
|
||||
const std::string& string,
|
||||
const int x,
|
||||
const int y)
|
||||
{
|
||||
if(string.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
static font::ttext text;
|
||||
text.set_text(string, false);
|
||||
sdl::ttexture texture
|
||||
= window.create_texture(SDL_TEXTUREACCESS_STATIC, text.render());
|
||||
|
||||
window.draw(texture, x, y);
|
||||
}
|
||||
|
||||
static void draw_command_line(sdl::twindow& window, const std::string& command)
|
||||
{
|
||||
draw_text(window, command, 15, 575);
|
||||
}
|
||||
|
||||
static void draw_command_history(sdl::twindow& window,
|
||||
const std::deque<std::string>& history)
|
||||
{
|
||||
unsigned offset = 535;
|
||||
BOOST_REVERSE_FOREACH(const std::string & item, history)
|
||||
{
|
||||
draw_text(window, item, 25, offset);
|
||||
offset -= 25;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
try
|
||||
{
|
||||
const tsdl sdl;
|
||||
bool dirty = false;
|
||||
std::string line;
|
||||
|
||||
std::deque<std::string> history;
|
||||
|
||||
SDL_Event event;
|
||||
sdl::twindow window("SDL2 test application",
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
800,
|
||||
600,
|
||||
0,
|
||||
SDL_RENDERER_TARGETTEXTURE);
|
||||
|
||||
SDL_Rect rect = { 25, 575, 750, 25 };
|
||||
SDL_SetTextInputRect(&rect);
|
||||
const ttext_input text_input;
|
||||
|
||||
while(true) {
|
||||
if(dirty) {
|
||||
window.clear();
|
||||
draw_command_line(window, line);
|
||||
draw_command_history(window, history);
|
||||
dirty = false;
|
||||
}
|
||||
window.render();
|
||||
if(SDL_WaitEvent(&event) == 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
switch(event.type) {
|
||||
case SDL_TEXTINPUT:
|
||||
utf8::insert(line, utf8::size(line), event.text.text);
|
||||
dirty = true;
|
||||
break;
|
||||
|
||||
case SDL_TEXTEDITING:
|
||||
/* Seems not to be called. */
|
||||
break;
|
||||
|
||||
case SDL_KEYDOWN:
|
||||
if(event.key.keysym.sym == SDLK_BACKSPACE) {
|
||||
if(!line.empty()) {
|
||||
utf8::erase(line, utf8::size(line) - 1);
|
||||
dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case SDL_KEYUP:
|
||||
if(event.key.keysym.sym == SDLK_RETURN) {
|
||||
history.push_back(line);
|
||||
if(history.size() > 22) {
|
||||
history.pop_front();
|
||||
}
|
||||
line.clear();
|
||||
dirty = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case SDL_QUIT:
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue