FifeGUI 0.2.0
A C++ GUI library designed for games.
graphics.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2004 - 2008 Olof Naessén and Per Larsson
3// SPDX-FileCopyrightText: 2013 - 2026 Fifengine contributors
4
5#include "fifechan/graphics.hpp"
6
7#include <memory>
8#include <string>
9
10#include "fifechan/cliprectangle.hpp"
11#include "fifechan/exception.hpp"
12#include "fifechan/font.hpp"
13#include "fifechan/image.hpp"
14#include "fifechan/rectangle.hpp"
15
16namespace fcn
17{
19 {
20 // Ignore area with a negative width or height by pushing
21 // an empty clip area to the stack.
22 if (area.isEmpty()) {
23 ClipRectangle const clip_rect;
24 mClipStack.push(clip_rect);
25 return true;
26 }
27
28 if (mClipStack.empty()) {
29 ClipRectangle clip_rect;
30 clip_rect.x = area.x;
31 clip_rect.y = area.y;
32 clip_rect.width = area.width;
33 clip_rect.height = area.height;
34 clip_rect.xOffset = area.x;
35 clip_rect.yOffset = area.y;
36 mClipStack.push(clip_rect);
37 return true;
38 }
39
40 ClipRectangle const & top = mClipStack.top();
41 ClipRectangle clip_rect;
42 clip_rect = area;
43 clip_rect.xOffset = top.xOffset + clip_rect.x;
44 clip_rect.yOffset = top.yOffset + clip_rect.y;
45 clip_rect.x += top.xOffset;
46 clip_rect.y += top.yOffset;
47
48 clip_rect = top.intersection(clip_rect);
49
50 mClipStack.push(clip_rect);
51
52 return !clip_rect.isEmpty();
53 }
54
56 {
57
58 if (mClipStack.empty()) {
59 throwException("Tried to pop clip area from empty stack.");
60 }
61
62 mClipStack.pop();
63 }
64
66 {
67 if (mClipStack.empty()) {
68 throwException("The clip area stack is empty.");
69 }
70
71 return mClipStack.top();
72 }
73
74 void Graphics::drawImage(Image const * image, int dstX, int dstY)
75 {
76 drawImage(image, 0, 0, dstX, dstY, image->getWidth(), image->getHeight());
77 }
78
80 {
81 mFont = font;
82 }
83
84 std::shared_ptr<Font> Graphics::createFont(std::string const & filename, int size)
85 {
86 (void)filename;
87 (void)size;
88 return nullptr;
89 }
90
91 void Graphics::drawText(std::string const & text, int x, int y, Alignment alignment)
92 {
93 if (mFont == nullptr) {
94 throwException("No font set.");
95 }
96
97 switch (alignment) {
98 case Alignment::Left:
99 mFont->drawString(this, text, x, y);
100 break;
101 case Alignment::Center:
102 mFont->drawString(this, text, x - (mFont->getWidth(text) / 2), y);
103 break;
104 case Alignment::Right:
105 mFont->drawString(this, text, x - mFont->getWidth(text), y);
106 break;
107 default:
108 throwException("Unknown alignment.");
109 }
110 }
111} // namespace fcn
A rectangle specifically used for clipping rendering regions.
int xOffset
Holds the x offset of the x coordinate.
int yOffset
Holds the y offset of the y coordinate.
Abstract interface for font rendering.
Definition font.hpp:24
Font * mFont
Holds the current font.
Definition graphics.hpp:387
Alignment
Alignments for text drawing.
Definition graphics.hpp:63
virtual void popClipArea()
Removes the top most clip area from the stack.
Definition graphics.cpp:55
virtual void drawImage(Image const *image, int srcX, int srcY, int dstX, int dstY, int width, int height)=0
Draws a part of an image.
void drawText(std::string const &text, int x, int y)
Draws text with a default left alignment.
Definition graphics.hpp:362
virtual ClipRectangle const & getCurrentClipArea()
Gets the current clip area.
Definition graphics.cpp:65
virtual bool pushClipArea(Rectangle area)
Pushes a clip area onto the stack.
Definition graphics.cpp:18
virtual void setFont(Font *font)
Sets the font to use when drawing text.
Definition graphics.cpp:79
virtual std::shared_ptr< Font > createFont(std::string const &filename, int size)
Creates a font for this graphics backend.
Definition graphics.cpp:84
std::stack< ClipRectangle > mClipStack
Holds the clip area stack.
Definition graphics.hpp:382
Abstract holder for image data.
Definition image.hpp:32
virtual int getHeight() const =0
Gets the height of the image.
virtual int getWidth() const =0
Gets the width of the image.
Represents a rectangular area (X, Y, Width, Height).
Definition rectangle.hpp:20
int width
Holds the width of the rectangle.
Rectangle intersection(Rectangle const &rh) const
Gets the intersection between two rectangles.
int y
Holds the x coordinate of the rectangle.
int x
Holds the x coordinate of the rectangle.
int height
Holds the height of the rectangle.
bool isEmpty() const
Checks whether the rectangle is empty or not.
Definition rectangle.cpp:71