Mana
Loading...
Searching...
No Matches
graphics.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "graphics.h"
23
24#include "gui/truetypefont.h"
25#include "resources/theme.h"
26
27#include <guichan/exception.hpp>
28
29
30void Graphics::updateSize(int width, int height, float /*scale*/)
31{
32 mWidth = width;
33 mHeight = height;
34}
35
36bool Graphics::drawImage(const Image *image, int x, int y)
37{
38 if (!image)
39 return false;
40
41 return drawImage(image, 0, 0, x, y, image->getWidth(), image->getHeight());
42}
43
44bool Graphics::drawImageF(const Image *image, float x, float y)
45{
46 if (!image)
47 return false;
48
49 return drawImageF(image, 0, 0, x, y, image->getWidth(), image->getHeight());
50}
51
52bool Graphics::drawRescaledImage(const Image *image, int x, int y, int width, int height)
53{
54 if (!image)
55 return false;
56
57 return drawRescaledImage(image, 0, 0, x, y, image->getWidth(), image->getHeight(), width, height);
58}
59
61 int srcX, int srcY,
62 float dstX, float dstY,
63 int width, int height,
64 float desiredWidth, float desiredHeight,
65 bool useColor)
66{
67 return drawRescaledImage(image,
68 srcX, srcY,
69 static_cast<int>(dstX),
70 static_cast<int>(dstY),
71 width, height,
72 static_cast<int>(desiredWidth),
73 static_cast<int>(desiredHeight),
74 useColor);
75}
76
77bool Graphics::drawImage(const Image *image,
78 int srcX, int srcY,
79 int dstX, int dstY,
80 int width, int height,
81 bool useColor)
82{
83 return drawRescaledImage(image,
84 srcX, srcY,
85 dstX, dstY,
86 width, height,
87 width, height, useColor);
88}
89
90bool Graphics::drawImageF(const Image *image, int srcX, int srcY, float dstX, float dstY, int width, int height, bool useColor)
91{
92 return drawRescaledImageF(image,
93 srcX, srcY,
94 dstX, dstY,
95 width, height,
96 width, height, useColor);
97}
98
99void Graphics::drawImagePattern(const Image *image, int x, int y, int w, int h)
100{
101 if (!image)
102 return;
103
104 drawRescaledImagePattern(image, x, y, w, h,
105 image->getWidth(), image->getHeight());
106}
107
109 int x, int y,
110 int w, int h,
111 int scaledWidth, int scaledHeight)
112{
114 0, 0,
115 image->getWidth(),
116 image->getHeight(),
117 x, y,
118 w, h,
119 scaledWidth,
120 scaledHeight);
121}
122
123void Graphics::drawImageRect(const ImageRect &imgRect, int x, int y, int w, int h)
124{
125 const int srcGridX[4] = {0,
126 imgRect.left,
127 imgRect.image->getWidth() - imgRect.right,
128 imgRect.image->getWidth()};
129 const int srcGridY[4] = {0,
130 imgRect.top,
131 imgRect.image->getHeight() - imgRect.bottom,
132 imgRect.image->getHeight()};
133
134 const int dstGridX[4] = {x, x + imgRect.left, x + w - imgRect.right, x + w};
135 const int dstGridY[4] = {y, y + imgRect.top, y + h - imgRect.bottom, y + h};
136
137 for (unsigned ix = 0; ix < 3; ix++)
138 {
139 for (unsigned iy = 0; iy < 3; iy++)
140 {
141 const int srcW = srcGridX[ix + 1] - srcGridX[ix];
142 const int srcH = srcGridY[iy + 1] - srcGridY[iy];
143
144 const int dstW = dstGridX[ix + 1] - dstGridX[ix];
145 const int dstH = dstGridY[iy + 1] - dstGridY[iy];
146
147 if (srcW <= 0 || srcH <= 0 || dstW <= 0 || dstH <= 0)
148 continue;
149
150 switch (imgRect.fillMode)
151 {
153 drawRescaledImage(imgRect.image.get(),
154 srcGridX[ix],
155 srcGridY[iy],
156 dstGridX[ix],
157 dstGridY[iy],
158 srcW, srcH,
159 dstW, dstH);
160 break;
161 case FillMode::Repeat:
162 drawRescaledImagePattern(imgRect.image.get(),
163 srcGridX[ix],
164 srcGridY[iy],
165 srcW, srcH,
166 dstGridX[ix],
167 dstGridY[iy],
168 dstW, dstH,
169 srcW, srcH);
170 break;
171 }
172 }
173 }
174}
175
176void Graphics::drawText(const std::string &text,
177 int x, int y,
178 gcn::Graphics::Alignment alignment,
179 const gcn::Color &color,
180 gcn::Font *font,
181 bool outline,
182 bool shadow,
183 const std::optional<gcn::Color> &outlineColor,
184 const std::optional<gcn::Color> &shadowColor)
185{
186 switch (alignment)
187 {
188 case gcn::Graphics::LEFT:
189 break;
190 case gcn::Graphics::CENTER:
191 x -= font->getWidth(text) / 2;
192 break;
193 case gcn::Graphics::RIGHT:
194 x -= font->getWidth(text);
195 break;
196 default:
197 throw GCN_EXCEPTION("Unknown alignment.");
198 }
199
200 auto realOutlineColor = outlineColor;
201 auto realShadowColor = shadowColor;
202
203 if (shadow && !realShadowColor)
204 {
206 sc.a = color.a / 2;
207 realShadowColor = sc;
208 }
209
210 if (outline && !realOutlineColor)
211 {
213 oc.a = color.a;
214 realOutlineColor = oc;
215 }
216
217 setColor(color);
218 static_cast<TrueTypeFont*>(font)->drawString(graphics, text, x, y,
219 realOutlineColor,
220 realShadowColor);
221}
222
223void Graphics::drawText(const std::string &text,
224 int x,
225 int y,
226 gcn::Graphics::Alignment align,
227 gcn::Font *font,
228 const TextFormat &format)
229{
230 drawText(text,
231 x,
232 y,
233 align,
234 format.color,
235 font,
236 format.outlineColor.has_value(),
237 format.shadowColor.has_value(),
238 format.outlineColor,
239 format.shadowColor);
240}
241
243{
244 pushClipArea(gcn::Rectangle(0, 0, mWidth, mHeight));
245}
246
248{
249 popClipArea();
250}
251
252void Graphics::pushClipRect(const gcn::Rectangle &rect)
253{
254 const gcn::ClipRectangle &carea = mClipStack.top();
255 mClipRects.emplace(rect.x + carea.xOffset,
256 rect.y + carea.yOffset,
257 rect.width,
258 rect.height);
259
261}
262
264{
265 mClipRects.pop();
267}
int mWidth
Definition graphics.h:271
virtual void drawImagePattern(const Image *image, int x, int y, int w, int h)
Definition graphics.cpp:99
std::stack< gcn::Rectangle > mClipRects
Definition graphics.h:277
bool drawImageF(const Image *image, float x, float y)
Blits an image onto the screen.
Definition graphics.cpp:44
void drawImageRect(const ImageRect &imgRect, int x, int y, int w, int h)
Draws a rectangle using images.
Definition graphics.cpp:123
bool drawRescaledImage(const Image *image, int x, int y, int width, int height)
Draws a rescaled version of the image.
Definition graphics.cpp:52
void _endDraw() override
Definition graphics.cpp:247
void drawRescaledImagePattern(const Image *image, int x, int y, int w, int h, int scaledWidth, int scaledHeight)
Draw a pattern based on a rescaled version of the given image.
Definition graphics.cpp:108
void drawText(const std::string &text, int x, int y, gcn::Graphics::Alignment alignment, const gcn::Color &color, gcn::Font *font, bool outline=false, bool shadow=false, const std::optional< gcn::Color > &outlineColor={}, const std::optional< gcn::Color > &shadowColor={})
Definition graphics.cpp:176
int mHeight
Definition graphics.h:272
void setColor(const gcn::Color &color) override
Definition graphics.h:255
virtual void updateClipRect()=0
bool drawImage(const Image *image, int x, int y)
Blits an image onto the screen.
Definition graphics.cpp:36
void pushClipRect(const gcn::Rectangle &rect)
Definition graphics.cpp:252
void _beginDraw() override
Definition graphics.cpp:242
virtual void updateSize(int width, int height, float scale)
Called when the window size or scale has changed.
Definition graphics.cpp:30
void popClipRect()
Definition graphics.cpp:263
virtual bool drawRescaledImageF(const Image *image, int srcX, int srcY, float dstX, float dstY, int width, int height, float desiredWidth, float desiredHeight, bool useColor=false)
Draws a rescaled version of the image.
Definition graphics.cpp:60
Defines a class for loading and storing images.
Definition image.h:45
int getHeight() const
Returns the height of the image.
Definition image.h:89
int getWidth() const
Returns the width of the image.
Definition image.h:83
static const gcn::Color & getThemeColor(int type)
Gets the color associated with the type in the default palette (0).
Definition theme.cpp:313
@ SHADOW
Definition theme.h:226
@ OUTLINE
Definition theme.h:227
A wrapper around SDL_ttf for allowing the use of TrueType fonts.
Graphics * graphics
Definition client.cpp:104
An image reference along with the margins specifying how to render this image at different sizes.
Definition graphics.h:62
int top
Definition graphics.h:64
int right
Definition graphics.h:67
FillMode fillMode
Definition graphics.h:68
std::unique_ptr< Image > image
Definition graphics.h:63
int bottom
Definition graphics.h:66
int left
Definition graphics.h:65
std::optional< gcn::Color > shadowColor
Definition theme.h:129
std::optional< gcn::Color > outlineColor
Definition theme.h:128
gcn::Color color
Definition theme.h:127