The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
window.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 #include "tools/sdl2/window.hpp"
16 
17 #include "tools/sdl2/sdl2.hpp"
18 
19 #include "sdl/window.hpp"
21 
22 #include <boost/lexical_cast.hpp>
23 
24 static void create(std::string& command, std::string::const_iterator begin)
25 {
26  std::string title;
27  int x = SDL_WINDOWPOS_CENTERED;
28  int y = SDL_WINDOWPOS_CENTERED;
29  int w = 800;
30  int h = 600;
31 
32  while(begin != command.end()) {
33  const std::string argument = get_token(command, begin, ' ');
34 
35  if(argument.find("x=") == 0) {
36  x = boost::lexical_cast<int>(argument.substr(2));
37  } else if(argument.find("y=") == 0) {
38  y = boost::lexical_cast<int>(argument.substr(2));
39  } else if(argument.find("w=") == 0) {
40  w = boost::lexical_cast<int>(argument.substr(2));
41  } else if(argument.find("h=") == 0) {
42  h = boost::lexical_cast<int>(argument.substr(2));
43  } else if(argument.find("title=") == 0) {
44  title = argument.substr(6); /* todo allow spaces. */
45  } else {
46  utf8::insert(command, utf8::size(command), " -> Unknown argument");
47  }
48  }
49 
50  windows.push_back(new sdl::twindow(
51  title.c_str(), x, y, w, h, 0, SDL_RENDERER_TARGETTEXTURE));
52 
53  utf8::insert(command,
54  utf8::size(command),
55  " -> OK ID "
56  + std::to_string(windows.size() - 1));
57 }
58 
59 static void modify(std::string& command, std::string::const_iterator begin)
60 {
61  std::size_t id
62  = boost::lexical_cast<std::size_t>(get_token(command, begin, ' '));
63 
64  if(id >= windows.size()) {
65  utf8::insert(command, utf8::size(command), " -> ID out of range");
66  } else if(windows[id] == nullptr) {
67  utf8::insert(command, utf8::size(command), " -> ID destroyed");
68  } else {
69 
70  std::string title = SDL_GetWindowTitle(*windows[id]);
71  int w;
72  int h;
73  SDL_GetWindowSize(*windows[id], &w, &h);
74 
75  while(begin != command.end()) {
76  const std::string argument = get_token(command, begin, ' ');
77 
78  if(argument.find("w=") == 0) {
79  w = boost::lexical_cast<int>(argument.substr(2));
80  } else if(argument.find("h=") == 0) {
81  h = boost::lexical_cast<int>(argument.substr(2));
82  } else if(argument.find("title=") == 0) {
83  title = argument.substr(6); /* todo allow spaces. */
84  } else {
86  command, utf8::size(command), " -> Unknown argument");
87  }
88  }
89 
90  windows[id]->set_title(title);
91  windows[id]->set_size(w, h);
92  utf8::insert(command, utf8::size(command), " -> OK");
93  }
94 }
95 
96 static void destroy(std::string& command, std::string::const_iterator begin)
97 {
98  std::size_t id
99  = boost::lexical_cast<std::size_t>(get_token(command, begin, ' '));
100 
101  if(id >= windows.size()) {
102  utf8::insert(command, utf8::size(command), " -> ID out of range");
103  } else if(windows[id] == nullptr) {
104  utf8::insert(command, utf8::size(command), " -> ID already destroyed");
105  } else {
106  delete windows[id];
107  windows[id] = nullptr;
108  utf8::insert(command, utf8::size(command), " -> OK");
109  }
110 }
111 
112 void execute_window(std::string& command, std::string::const_iterator begin)
113 {
114  const std::string action = get_token(command, begin, ' ');
115 
116  if(action == "create") {
117  create(command, begin);
118  } else if(action == "modify") {
119  modify(command, begin);
120  } else if(action == "destroy") {
121  destroy(command, begin);
122  } else {
123  utf8::insert(command, utf8::size(command), " -> Unknown action");
124  }
125 }
static void create(std::string &command, std::string::const_iterator begin)
Definition: window.cpp:24
static std::map< std::string, twindow_builder > windows
Map with all known windows, (the builder class builds a window).
Definition: settings.cpp:459
#define h
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
To lexical_cast(From value)
Lexical cast converts one type to another.
token get_token(iterator &i1, const iterator i2)
Definition: tokenizer.cpp:38
GLuint id
Definition: glew.h:1647
The wrapper class for the SDL_Window class.
Definition: window.hpp:52
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1858
static void destroy(std::string &command, std::string::const_iterator begin)
Definition: window.cpp:96
static void modify(std::string &command, std::string::const_iterator begin)
Definition: window.cpp:59
size_t size(const utf8::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:88
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:5910
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
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
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void execute_window(std::string &command, std::string::const_iterator begin)
Executes a window command.
Definition: window.cpp:112