]> git.vomp.tv Git - vompclient.git/blob - box.cc
Update for windows
[vompclient.git] / box.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "box.h"
22
23 char Box::numBoxes = 0;
24
25 Box::Box()
26 {
27   area.x = 0;
28   area.y = 0;
29   area.w = 0;
30   area.h = 0;
31
32   offsetX = 0;
33   offsetY = 0;
34
35   gap = 6; // default gap for drawPara
36
37   numBoxes++;
38   Log::getInstance()->log("Box", Log::DEBUG, "Construct, now %u", numBoxes);
39
40   surface = NULL;
41 }
42
43 Box::~Box()
44 {
45   numBoxes--;
46   Log::getInstance()->log("Box", Log::DEBUG, "Destruct, now %u", numBoxes);
47 }
48
49 void Box::draw()
50 {
51 }
52
53 void Box::setScreenPos(int x, int y)
54 {
55   area.x = x;
56   area.y = y;
57 }
58
59 void Box::setSurfaceOffset(UINT x, UINT y)
60 {
61   offsetX = x;
62   offsetY = y;
63 }
64
65 void Box::setGap(UINT tgap)
66 {
67   gap = tgap;
68 }
69
70 void Box::blt(Region& r)
71 {
72 //  Log::getInstance()->log("Box", Log::DEBUG, "Show region %p %u %u %u %u", surface, r.x, r.y, r.w, r.h);
73 //  surface->updateToScreen(area.x, area.y, area.w, area.h);
74
75   /* surface update to screen needs:
76   source x distance into this surface
77   source y distance into this surface
78   width of update
79   height of update
80   destination x on screen
81   destination y on screen
82   */
83
84   surface->updateToScreen(r.x - area.x, r.y - area.y, r.w, r.h, r.x, r.y);
85 }
86
87 int Box::getScreenX()
88 {
89   return area.x;
90 }
91
92 int Box::getScreenY()
93 {
94   return area.y;
95 }
96
97 int Box::getWidth()
98 {
99   return area.w;
100 }
101
102 int Box::getHeight()
103 {
104   return area.h;
105 }
106
107 // Level 1 drawing functions
108
109 void Box::fillColour(Colour& colour)
110 {
111   rectangle(0, 0, area.w, area.h, colour);
112 }
113
114 void Box::drawPara(char* text, int x, int y, Colour& colour)
115 {
116   char line[256];
117   int lineHeight = surface->getFontHeight() + gap;
118
119   int lineWidth;
120   int thisCharWidth;
121   int textPos;
122   int linePos;
123   int ypos;
124   int printLine;
125
126   textPos = 0;
127   ypos = y;
128
129   while(1)
130   {
131     linePos = 0;
132     lineWidth = 0;
133     while(1)
134     {
135       printLine = 0;
136
137       if (text[textPos] == '\0') break;
138
139       if (text[textPos] == '\n')
140       {
141         textPos++; // ignore the \n
142         printLine = 1;
143         break;
144       }
145
146       thisCharWidth = surface->getCharWidth(text[textPos]);
147       if ((lineWidth + thisCharWidth) > (int)(area.w - (2 * paraMargin)))
148       {
149         // this character would break the right margin
150         if (text[textPos] == ' ')
151         {
152           // this char is a space, ignore and break
153           textPos++;
154           break;
155         }
156         else
157         {
158           // Need to go back to the last space in the line
159           while ((text[textPos] != ' ') && (linePos >= 0))
160           {
161             textPos--;
162             linePos--;
163           }
164           // Now take the space we just found
165           textPos++;
166           break;
167         }
168       }
169       line[linePos++] = text[textPos];
170       lineWidth += thisCharWidth;
171       textPos++;
172     }
173
174 //    line[linePos++] = '\0';
175     if (linePos>=0) line[linePos++] = '\0'; //Here is the change
176
177     if (printLine || (linePos > 1)) // if some text was put in line
178     {
179       drawText(line, x, ypos, colour);
180       ypos += lineHeight;
181       if (ypos > (int)(area.h - lineHeight)) break;
182     }
183     else
184     {
185       break;
186     }
187   }
188 }
189
190 // Level 0 drawing functions
191
192 void Box::rectangle(int x1, int y1, int w, int h, Colour colour)
193 {
194   surface->fillblt(offsetX + x1, offsetY + y1, w, h, colour.rgba());
195 }
196
197 void Box::rectangle(Region& region, Colour& colour)
198 {
199   surface->fillblt(offsetX + region.x, offsetY + region.y, region.w, region.h, colour.rgba());
200 }
201
202 void Box::drawText(char* text, int x, int y, Colour& colour)
203 {
204   surface->drawText(text, offsetX + x, offsetY + y, colour.rgba());
205 }
206
207 void Box::drawTextRJ(char* text, int x, int y, Colour& colour)
208 {
209   surface->drawTextRJ(text, offsetX + x, offsetY + y, colour.rgba());
210 }
211
212 void Box::drawTextCentre(char* text, int x, int y, Colour& colour)
213 {
214   surface->drawTextCentre(text, offsetX + x, offsetY + y, colour.rgba());
215 }
216
217 void Box::drawPixel(UINT x, UINT y, Colour& colour)
218 {
219   int c = (  (0xFF000000        )
220              | (colour.red  << 16)
221              | (colour.green  <<  8)
222              | (colour.blue     ) );
223
224
225   surface->drawPixel(offsetX + x, offsetY + y, c);
226 }