1 /*
2 * Kuklomenos
3 * Copyright (C) 2008-2009 Martin Bays <mbays@sdf.lonestar.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 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see http://www.gnu.org/licenses/.
17 */
18
19 #include "overlay.h"
20 #include "geom.h"
21 #include "data.h"
22 #include "SDL_gfxPrimitivesDirty.h"
23
24 #include <string>
25 #include <vector>
26 #include <SDL/SDL.h>
27 using namespace std;
28
drawstr(SDL_Surface * surface,string str,Uint8 alpha)29 void Overlay::drawstr(SDL_Surface* surface, string str, Uint8 alpha)
30 {
31 clear();
32 push_back(str);
33 draw(surface);
34 clear();
35 }
36
draw(SDL_Surface * surface,Uint8 alpha)37 void Overlay::draw(SDL_Surface* surface, Uint8 alpha)
38 {
39 if (!drawWithFont(surface, fontBig, 10, 20, alpha))
40 drawWithFont(surface, fontSmall, 7, 13, alpha, true);
41 }
42
drawWithFont(SDL_Surface * surface,const void * fontdata,int cw,int ch,Uint8 alpha,bool force)43 bool Overlay::drawWithFont(SDL_Surface* surface, const void *fontdata, int cw,
44 int ch, Uint8 alpha, bool force)
45 {
46 const unsigned int maxlen = (2*3*screenGeom.rad/5)/cw;
47
48 vector<string> splitStrings;
49
50 for (vector<string>::iterator it = begin();
51 it != end(); it++)
52 {
53 if (!force && it->length() > maxlen)
54 return false;
55
56 if (!splitStrings.empty() &&
57 it->length() + splitStrings.back().length() < maxlen)
58 splitStrings.back() += " " + *it;
59 else
60 splitStrings.push_back(*it);
61 }
62
63 gfxPrimitivesSetFont(fontdata, cw, ch);
64
65 const int ystart = screenGeom.centre.y + int(screenGeom.rad*offy) -
66 ( splitStrings.size() * ch - ch/2 +
67 (splitStrings.size() - 1) * ch/2 )/2;
68 const int yinc = ch + ch/2;
69
70 int i = 0;
71 for (vector<string>::iterator it = splitStrings.begin();
72 it != splitStrings.end();
73 it++, i++)
74 {
75 const int strx = screenGeom.centre.x - it->length()*cw/2;
76 const int stry = ystart + i*yinc;
77 stringColor(surface, strx, stry, it->c_str(),
78 (alpha == 0) ? colour
79 : (colour >> 8 << 8) + alpha);
80 }
81 return true;
82 }
83