forked from gecube/opencaesar3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
picture.cpp
238 lines (198 loc) · 4.4 KB
/
picture.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// This file is part of openCaesar3.
//
// openCaesar3 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 3 of the License, or
// (at your option) any later version.
//
// openCaesar3 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with openCaesar3. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2012-2013 Gregoire Athanase, gathanase@gmail.com
#include "picture.hpp"
#include <sdl_facade.hpp>
#include <iostream>
#include <sstream>
#include <sdl_facade.hpp>
#include "exception.hpp"
#include "oc3_positioni.h"
Picture::Picture()
{
_surface = NULL;
_xoffset = 0;
_yoffset = 0;
_glTextureID = 0;
_width = 0;
_height = 0;
_name = "";
}
void Picture::init(SDL_Surface *surface, const int xoffset, const int yoffset)
{
_surface = surface;
_xoffset = xoffset;
_yoffset = yoffset;
_width = _surface->w;
_height = _surface->h;
}
void Picture::set_offset(const int xoffset, const int yoffset)
{
_xoffset = xoffset;
_yoffset = yoffset;
}
void Picture::set_offset( const Point& offset )
{
_xoffset = offset.getX();
_yoffset = offset.getY();
}
void Picture::add_offset(const int dx, const int dy)
{
_xoffset += dx;
_yoffset += dy;
}
SDL_Surface* Picture::get_surface() const
{
return _surface;
}
int Picture::get_xoffset() const
{
return _xoffset;
}
int Picture::get_yoffset() const
{
return _yoffset;
}
int Picture::get_width() const
{
return _width;
}
int Picture::get_height() const
{
return _height;
}
void Picture::set_name(std::string &name)
{
_name = name;
}
std::string Picture::get_name()
{
return _name;
}
void Animation::init(const std::vector<Picture*> &pictures)
{
_pictures = pictures;
_animIndex = 0;
}
std::vector<Picture*>& Animation::get_pictures()
{
return _pictures;
}
const std::vector<Picture*>& Animation::get_pictures() const
{
return _pictures;
}
void Animation::nextFrame()
{
_animIndex += 1;
}
Picture* Animation::get_current_picture()
{
if (_pictures.size() == 0)
{
return NULL;
}
if (_animIndex >= _pictures.size())
{
_animIndex = 0;
}
return _pictures[_animIndex];
}
Font::Font(TTF_Font &ttfFont, SDL_Color &color)
{
_ttfFont = &ttfFont;
_color = color;
}
TTF_Font& Font::getTTF()
{
return *_ttfFont;
}
SDL_Color& Font::getColor()
{
return _color;
}
std::list<std::string> Font::split_text(const std::string &text, const int width)
{
std::list<std::string> res;
std::istringstream iss(text);
int w;
int h;
SdlFacade &sdlFacade = SdlFacade::instance();
std::string currentLine = "";
while (iss)
{
std::string word;
iss >> word;
sdlFacade.getTextSize(*this, currentLine + " " + word, w, h);
if (word == "")
{
// end of text
break;
}
else if (currentLine == "")
{
// first word
currentLine = word;
}
else if (w <= width)
{
// write on current line
currentLine += " " + word;
}
else
{
// too long, need another line
res.push_back(currentLine);
currentLine = word;
}
}
if (currentLine != "")
{
res.push_back(currentLine);
}
return res;
}
FontCollection* FontCollection::_instance = NULL;
FontCollection& FontCollection::instance()
{
if (_instance == NULL)
{
_instance = new FontCollection();
}
return *_instance;
}
FontCollection::FontCollection()
{ }
Font& FontCollection::getFont(const int key)
{
std::map<int, Font>::iterator it = _collection.find(key);
if (it == _collection.end())
{
THROW("Error, font is not initialized, key=" << key);
}
return (*it).second;
}
void FontCollection::setFont(const int key, Font& font)
{
std::pair<std::map<int, Font>::iterator, bool> ret = _collection.insert(std::pair<int, Font>(key, font));
if (ret.second == false)
{
// no insert font (already exists)
THROW("Error, font already exists, key=" << key);
}
// // insert font
// std::cout << "Registered font with key=" << key << std::endl;
}