The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
sdl2.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Tool to test SDL 2 functions.
18  */
19 
20 #include "tools/sdl2/sdl2.hpp"
21 
22 #include "sdl/texture.hpp"
23 #include "sdl/window.hpp"
24 #include "tools/sdl2/window.hpp"
25 #include "text.hpp"
26 
27 #include <SDL.h>
28 
29 #include <iostream>
30 #include <deque>
31 #include <boost/range/adaptor/reversed.hpp>
32 
33 std::vector<sdl::twindow*> windows;
34 
35 struct texit
36 {
37 };
38 
39 struct tsdl
40 {
41  tsdl()
42  {
43  if(SDL_Init(SDL_INIT_VIDEO) != 0) {
44  std::cerr << "Unable to initialise SDL with error »"
45  << SDL_GetError() << "«.\n";
46 
47  throw texit();
48  }
49  }
50 
52  {
53 
54  SDL_Quit();
55  }
56 };
57 
59 {
61  {
62  SDL_StartTextInput();
63  }
64 
66  {
67  SDL_StopTextInput();
68  }
69 };
70 
71 static void draw_text(sdl::twindow& window,
72  const std::string& string,
73  const int x,
74  const int y)
75 {
76  if(string.empty()) {
77  return;
78  }
79 
80  static font::ttext text;
81  text.set_text(string, false);
82  sdl::ttexture texture
83  = window.create_texture(SDL_TEXTUREACCESS_STATIC, text.render());
84 
85  window.draw(texture, x, y);
86 }
87 
88 static void draw_command_line(sdl::twindow& window, const std::string& command)
89 {
90  draw_text(window, command, 15, 575);
91 }
92 
93 static void draw_command_history(sdl::twindow& window,
94  const std::deque<std::string>& history)
95 {
96  unsigned offset = 535;
97  for(const std::string & item : boost::adaptors::reverse(history))
98  {
99  draw_text(window, item, 25, offset);
100  offset -= 25;
101  }
102 }
103 
104 static bool execute_command(std::string& command_line)
105 {
106  std::string::const_iterator begin = command_line.begin();
107  const std::string command = get_token(command_line, begin, ' ');
108 
109  if(command == "quit") {
110 
111  utf8::insert(command_line, utf8::size(command_line), " -> Ok");
112  return false;
113  } else if(command == "window") {
114 
115  execute_window(command_line, begin);
116  }
117 
118  return true;
119 }
120 
122  std::string::const_iterator& begin,
123  const char separator)
124 {
125  const std::string::const_iterator end
126  = std::find(begin, string.end(), separator);
127 
128  std::string result = std::string(begin, end);
129 
130  begin = end;
131  if(begin != string.end()) {
132  ++begin;
133  }
134 
135  return result;
136 }
137 
138 int main()
139 {
140  try
141  {
142  const tsdl sdl;
143  bool dirty = false;
144  std::string line;
145 
146  std::deque<std::string> history;
147 
148  SDL_Event event;
149  sdl::twindow window("SDL2 test application",
150  SDL_WINDOWPOS_CENTERED,
151  SDL_WINDOWPOS_CENTERED,
152  800,
153  600,
154  0,
155  SDL_RENDERER_TARGETTEXTURE);
156 
157  SDL_Rect rect = { 25, 575, 750, 25 };
158  SDL_SetTextInputRect(&rect);
159  const ttext_input text_input;
160 
161  bool run = true;
162  while(run) {
163  if(dirty) {
164  window.fill(0, 0, 0, 0);
165  draw_command_line(window, line);
166  draw_command_history(window, history);
167  dirty = false;
168  }
169  window.render();
170  for(sdl::twindow * window : windows)
171  {
172  if(window) {
173  window->render();
174  }
175  }
176  if(SDL_WaitEvent(&event) == 0) {
177  return EXIT_FAILURE;
178  }
179 
180  switch(event.type) {
181  case SDL_TEXTINPUT:
182  utf8::insert(line, utf8::size(line), event.text.text);
183  dirty = true;
184  break;
185 
186  case SDL_TEXTEDITING:
187  /* Seems not to be called. */
188  break;
189 
190  case SDL_KEYDOWN:
191  if(event.key.keysym.sym == SDLK_BACKSPACE) {
192  if(!line.empty()) {
193  utf8::erase(line, utf8::size(line) - 1);
194  dirty = true;
195  }
196  }
197 
198  break;
199 
200  case SDL_KEYUP:
201  if(event.key.keysym.sym == SDLK_RETURN) {
202  run = execute_command(line);
203  history.push_back(line);
204  if(history.size() > 22) {
205  history.pop_front();
206  }
207  line.clear();
208  dirty = true;
209  SDL_RaiseWindow(window);
210  }
211  break;
212 
213  case SDL_QUIT:
214  return EXIT_SUCCESS;
215  }
216  }
217  }
218  catch(...)
219  {
220  return EXIT_FAILURE;
221  }
222 }
static void draw_text(sdl::twindow &window, const std::string &string, const int x, const int y)
Definition: sdl2.cpp:71
tsdl()
Definition: sdl2.cpp:41
GLenum GLenum GLuint texture
Definition: glew.h:3453
std::string get_token(const std::string &string, std::string::const_iterator &begin, const char separator)
Gets a token from a string.
Definition: sdl2.cpp:121
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
GLintptr offset
Definition: glew.h:1650
void render()
Renders the contents of the window.
Definition: window.cpp:108
GLuint GLuint end
Definition: glew.h:1221
GLuint64EXT * result
Definition: glew.h:10727
The wrapper class for the SDL_Window class.
Definition: window.hpp:52
static void draw_command_history(sdl::twindow &window, const std::deque< std::string > &history)
Definition: sdl2.cpp:93
std::vector< sdl::twindow * > windows
Contains the windows created by the user.
Definition: sdl2.cpp:33
int main()
Definition: sdl2.cpp:138
void fill(Uint8 r, Uint8 g, Uint8 b, Uint8 a=0)
Clears the contents of the window with a given color.
Definition: window.cpp:99
static bool execute_command(std::string &command_line)
Definition: sdl2.cpp:104
ttext_input()
Definition: sdl2.cpp:60
Definition: sdl2.cpp:39
size_t size(const utf8::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:88
This exception when throw should terminate the application.
Definition: sdl2.cpp:35
surface render() const
Returns the rendered text.
Definition: text.cpp:166
~ttext_input()
Definition: sdl2.cpp:65
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
bool find(E event, F functor)
Tests whether an event handler is available.
cl_event event
Definition: glew.h:3070
bool set_text(const std::string &text, const bool markedup)
Sets the text to render.
Definition: text.cpp:360
Contains a wrapper class for the SDL_Window class.
utf8::string & insert(utf8::string &str, const size_t pos, const utf8::string &insert)
Insert a UTF-8 string at the specified position.
Definition: unicode.cpp:101
~tsdl()
Definition: sdl2.cpp:51
static void draw_command_line(sdl::twindow &window, const std::string &command)
Definition: sdl2.cpp:88
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Text class.
Definition: text.hpp:66
void execute_window(std::string &command, std::string::const_iterator begin)
Executes a window command.
Definition: window.cpp:112
utf8::string & erase(utf8::string &str, const size_t start, const size_t len)
Erases a portion of a UTF-8 string.
Definition: unicode.cpp:106