FifeGUI 0.3.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// Corresponding header include
6#include "fifechan/graphics.hpp"
7
8// Standard library includes
9#include <cassert>
10#include <memory>
11#include <string>
12
13// Platform config include
14#include "fifechan/platform.hpp"
15
16// Project headers (subdirs before local)
17#include "fifechan/cliprectangle.hpp"
18#include "fifechan/exception.hpp"
19#include "fifechan/font.hpp"
20#include "fifechan/image.hpp"
21#include "fifechan/rectangle.hpp"
22
23namespace fcn
24{
26 {
27 // Ignore area with a negative width or height by pushing
28 // an empty clip area to the stack.
29 assert("Width must be non-negative" && area.width >= 0);
30 assert("Height must be non-negative" && area.height >= 0);
31
32 if (area.isEmpty()) {
33 ClipRectangle const clip_rect;
34 mClipStack.push(clip_rect);
35 return true;
36 }
37
38 if (mClipStack.empty()) {
39 ClipRectangle clip_rect;
40 clip_rect.x = area.x;
41 clip_rect.y = area.y;
42 clip_rect.width = area.width;
43 clip_rect.height = area.height;
44 clip_rect.xOffset = area.x;
45 clip_rect.yOffset = area.y;
46 mClipStack.push(clip_rect);
47 return true;
48 }
49
50 ClipRectangle const & top = mClipStack.top();
51 ClipRectangle clip_rect;
52 clip_rect = area;
53 clip_rect.xOffset = top.xOffset + clip_rect.x;
54 clip_rect.yOffset = top.yOffset + clip_rect.y;
55 clip_rect.x += top.xOffset;
56 clip_rect.y += top.yOffset;
57
58 clip_rect = top.intersection(clip_rect);
59
60 mClipStack.push(clip_rect);
61
62 return !clip_rect.isEmpty();
63 }
64
66 {
67 if (mClipStack.empty()) {
68 throwException("Tried to pop clip area from empty stack.");
69 }
70
71 mClipStack.pop();
72 }
73
75 {
76 if (mClipStack.empty()) {
77 throwException("The clip area stack is empty.");
78 }
79
80 return mClipStack.top();
81 }
82
83 void Graphics::drawImage(Image const * image, int dstX, int dstY)
84 {
85 if (image == nullptr) {
86 throwException("Image must not be null");
87 }
88 drawImage(image, 0, 0, dstX, dstY, image->getWidth(), image->getHeight());
89 }
90
92 {
93 mFont = font;
94 }
95
96 std::shared_ptr<Font> Graphics::createFont(std::string const & filename, int size)
97 {
98 (void)filename;
99 (void)size;
100 return nullptr;
101 }
102
103 void Graphics::drawText(std::string const & text, int x, int y, Alignment alignment)
104 {
105 if (mFont == nullptr) {
106 throwException("No font set.");
107 }
108
109 switch (alignment) {
110 case Alignment::Left:
111 mFont->drawString(this, text, x, y);
112 break;
113 case Alignment::Center:
114 mFont->drawString(this, text, x - (mFont->getWidth(text) / 2), y);
115 break;
116 case Alignment::Right:
117 mFont->drawString(this, text, x - mFont->getWidth(text), y);
118 break;
119 default:
120 throwException("Unknown alignment.");
121 }
122 }
123} // 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:26
Font * mFont
Holds the current font.
Definition graphics.hpp:426
Alignment
Horizontal alignments for text drawing.
Definition graphics.hpp:64
virtual void popClipArea()
Removes the top most clip area from the stack.
Definition graphics.cpp:65
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:401
virtual ClipRectangle const & getCurrentClipArea()
Gets the current clip area.
Definition graphics.cpp:74
virtual bool pushClipArea(Rectangle area)
Pushes a clip area onto the stack.
Definition graphics.cpp:25
virtual void setFont(Font *font)
Sets the font to use when drawing text.
Definition graphics.cpp:91
virtual std::shared_ptr< Font > createFont(std::string const &filename, int size)
Creates a font for this graphics backend.
Definition graphics.cpp:96
std::stack< ClipRectangle > mClipStack
Holds the clip area stack.
Definition graphics.hpp:421
Abstract holder for image data.
Definition image.hpp:34
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:22
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:82
Used replacement tokens by configure_file():
void throwException(std::string const &message, std::source_location location=std::source_location::current())
Throw an Exception capturing the current source location.