Mana
Loading...
Searching...
No Matches
graphics.h
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#pragma once
23
24#include "resources/image.h"
25
26#include <SDL.h>
27
28#include <guichan/color.hpp>
29#include <guichan/graphics.hpp>
30
31#include <memory>
32#include <optional>
33
34struct TextFormat;
35
36enum class FillMode
37{
38 Stretch,
39 Repeat,
40};
41
62{
63 std::unique_ptr<Image> image;
64 int top = 0;
65 int left = 0;
66 int bottom = 0;
67 int right = 0;
69
70 int minWidth() const { return left + right; }
71 int minHeight() const { return top + bottom; }
72};
73
77class Graphics : public gcn::Graphics
78{
79 public:
80 Graphics() = default;
81
85 virtual void setVSync(bool sync) = 0;
86
90 virtual void updateSize(int width, int height, float scale);
91
92 using gcn::Graphics::drawImage;
93
100 bool drawImage(const Image *image, int x, int y);
101
108 bool drawImageF(const Image *image, float x, float y);
109
113 bool drawRescaledImage(const Image *image, int x, int y, int width, int height);
114
118 virtual bool drawRescaledImage(const Image *image,
119 int srcX, int srcY,
120 int dstX, int dstY,
121 int width, int height,
122 int desiredWidth, int desiredHeight,
123 bool useColor = false) = 0;
124
128 virtual bool drawRescaledImageF(const Image *image, int srcX, int srcY,
129 float dstX, float dstY,
130 int width, int height,
131 float desiredWidth, float desiredHeight,
132 bool useColor = false);
133
140 virtual bool drawImage(const Image *image,
141 int srcX, int srcY,
142 int dstX, int dstY,
143 int width, int height,
144 bool useColor = false);
145
152 virtual bool drawImageF(const Image *image,
153 int srcX, int srcY,
154 float dstX, float dstY,
155 int width, int height,
156 bool useColor = false);
157
158 virtual void drawImagePattern(const Image *image,
159 int x, int y,
160 int w, int h);
161
165 void drawRescaledImagePattern(const Image *image,
166 int x, int y,
167 int w, int h,
168 int scaledWidth,
169 int scaledHeight);
170
174 virtual void drawRescaledImagePattern(const Image *image,
175 int srcX, int srcY,
176 int srcW, int srcH,
177 int dstX, int dstY,
178 int dstW, int dstH,
179 int scaledWidth,
180 int scaledHeight) = 0;
181
186 void drawImageRect(const ImageRect &imgRect, int x, int y, int w, int h);
187
188 void drawImageRect(const ImageRect &imgRect, const gcn::Rectangle &area)
189 {
190 drawImageRect(imgRect, area.x, area.y, area.width, area.height);
191 }
192
193 using gcn::Graphics::drawText;
194
195 void drawText(const std::string &text,
196 int x, int y,
197 gcn::Graphics::Alignment alignment,
198 const gcn::Color &color,
199 gcn::Font *font,
200 bool outline = false,
201 bool shadow = false,
202 const std::optional<gcn::Color> &outlineColor = {},
203 const std::optional<gcn::Color> &shadowColor = {});
204
205 void drawText(const std::string &text,
206 int x,
207 int y,
208 gcn::Graphics::Alignment align,
209 gcn::Font *font,
210 const TextFormat &format);
211
216 virtual void updateScreen() = 0;
217
221 int getWidth() const { return mWidth; }
222
226 int getHeight() const { return mHeight; }
227
231 float getScale() const { return mScale; }
232
237 virtual void windowToLogical(int windowX, int windowY,
238 float &logicalX, float &logicalY) const = 0;
239
240 void _beginDraw() override;
241 void _endDraw() override;
242
246 virtual SDL_Surface *getScreenshot() = 0;
247
248 gcn::Font *getFont() const { return mFont; }
249
250 void drawImage(const gcn::Image *image,
251 int srcX, int srcY,
252 int dstX, int dstY,
253 int width, int height) override {} // not used
254
255 void setColor(const gcn::Color &color) override
256 {
257 mColor = color;
258 }
259
260 const gcn::Color &getColor() const final
261 {
262 return mColor;
263 }
264
265 void pushClipRect(const gcn::Rectangle &rect);
266 void popClipRect();
267
268 protected:
269 virtual void updateClipRect() = 0;
270
271 int mWidth = 0;
272 int mHeight = 0;
273 float mScale = 1.0f;
274 gcn::Color mColor;
275
276 // Actual clipping rects. Clipping by gcn::Graphics::mClipStack is disabled.
277 std::stack<gcn::Rectangle> mClipRects;
278};
279
280extern Graphics *graphics;
A central point of control for graphics.
Definition graphics.h:78
virtual void updateScreen()=0
Updates the screen.
int mWidth
Definition graphics.h:271
virtual void drawImagePattern(const Image *image, int x, int y, int w, int h)
Definition graphics.cpp:99
float getScale() const
Returns the graphics scale.
Definition graphics.h:231
void drawImageRect(const ImageRect &imgRect, const gcn::Rectangle &area)
Definition graphics.h:188
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
gcn::Font * getFont() const
Definition graphics.h:248
virtual bool drawRescaledImage(const Image *image, int srcX, int srcY, int dstX, int dstY, int width, int height, int desiredWidth, int desiredHeight, bool useColor=false)=0
Draws a rescaled version of the image.
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
virtual SDL_Surface * getScreenshot()=0
Takes a screenshot and returns it as SDL surface.
gcn::Color mColor
Definition graphics.h:274
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
int getHeight() const
Returns the logical height of the screen.
Definition graphics.h:226
virtual void drawRescaledImagePattern(const Image *image, int srcX, int srcY, int srcW, int srcH, int dstX, int dstY, int dstW, int dstH, int scaledWidth, int scaledHeight)=0
Draw a pattern based on a rescaled version of the given image.
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
virtual void windowToLogical(int windowX, int windowY, float &logicalX, float &logicalY) const =0
Converts a window coordinate to a logical coordinate.
const gcn::Color & getColor() const final
Definition graphics.h:260
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
float mScale
Definition graphics.h:273
virtual void setVSync(bool sync)=0
Sets whether vertical refresh syncing is enabled.
void popClipRect()
Definition graphics.cpp:263
Graphics()=default
void drawImage(const gcn::Image *image, int srcX, int srcY, int dstX, int dstY, int width, int height) override
Definition graphics.h:250
int getWidth() const
Returns the logical width of the screen.
Definition graphics.h:221
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
FillMode
Definition graphics.h:37
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
int minHeight() const
Definition graphics.h:71
FillMode fillMode
Definition graphics.h:68
std::unique_ptr< Image > image
Definition graphics.h:63
int bottom
Definition graphics.h:66
int minWidth() const
Definition graphics.h:70
int left
Definition graphics.h:65