FifeGUI 0.2.0
A C++ GUI library designed for games.
backends/sdl2/graphics.hpp
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: 2016 - 2019 Gwilherm Baudic
4// SPDX-FileCopyrightText: 2013 - 2026 Fifengine contributors
5
6#ifndef INCLUDE_FIFECHAN_BACKENDS_SDL_GRAPHICS_HPP_
7#define INCLUDE_FIFECHAN_BACKENDS_SDL_GRAPHICS_HPP_
8
9#include <SDL2/SDL.h>
10
11#include <memory>
12#include <string>
13
14#include "fifechan/color.hpp"
15#include "fifechan/graphics.hpp"
16#include "fifechan/platform.hpp"
17
18namespace fcn
19{
20 class Image;
21 class Rectangle;
22} // namespace fcn
23
24namespace fcn::sdl2
25{
33 class FIFEGUI_EXT_API Graphics : public fcn::Graphics
34 {
35 public:
36 Graphics();
37
38 ~Graphics() override;
39
40 Graphics(Graphics const &) = delete;
41 Graphics& operator=(Graphics const &) = delete;
42 Graphics(Graphics&&) = delete;
43 Graphics& operator=(Graphics&&) = delete;
44
52 virtual void setTarget(SDL_Renderer* renderer, int width, int height);
53
59 virtual SDL_Renderer* getRenderTarget() const;
60
66 virtual void drawSDLTexture(SDL_Texture* texture, SDL_Rect source, SDL_Rect destination);
67
68 // Inherited from Graphics
69
70 void _beginDraw() override;
71 void _endDraw() override;
72 bool pushClipArea(fcn::Rectangle area) override;
73 void popClipArea() override;
74 void drawImage(
75 fcn::Image const * image, int srcX, int srcY, int dstX, int dstY, int width, int height) override;
76 void drawPoint(int x, int y) override;
77 void drawLine(int x1, int y1, int x2, int y2) override;
78 void drawLine(int x1, int y1, int x2, int y2, unsigned int width) override;
79 void drawRoundStroke(int x1, int y1, int x2, int y2, unsigned int width) override;
80
81 void drawRectangle(Rectangle const & rectangle) override;
82 void fillRectangle(Rectangle const & rectangle) override;
83
84 void drawPolyLine(PointVector const & points, unsigned int width) override;
85 void drawBezier(PointVector const & points, int segments, unsigned int width) override;
86
87 void drawCircle(Point const & center, unsigned int radius) override;
88 void drawFillCircle(Point const & center, unsigned int radius) override;
89 void drawCircleSegment(Point const & center, unsigned int radius, int startAngle, int endAngle) override;
90 void drawFillCircleSegment(Point const & center, unsigned int radius, int startAngle, int endAngle) override;
91
92 void setColor(Color const & color) override;
93 Color const & getColor() const override;
94 std::shared_ptr<Font> createFont(std::string const & filename, int size) override;
95
96 protected:
104 void drawHorizontalLine(int x1, int y, int x2);
105
113 void drawVerticalLine(int x, int y1, int y2);
114
119 void saveRenderColor();
120
124 void restoreRenderColor();
125
127 SDL_Renderer* mRenderTarget{};
128
130 int mWidth = 0;
131
133 int mHeight = 0;
134
137
140 Uint8 r{};
142 Uint8 g{};
144 Uint8 b{};
146 Uint8 a{};
147
149 bool mAlpha;
150 };
151} // namespace fcn::sdl2
152
153#endif // INCLUDE_FIFECHAN_BACKENDS_SDL_GRAPHICS_HPP_
Color.
Definition color.hpp:56
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:57
Abstract holder for image data.
Definition image.hpp:32
Represents a 2D coordinate (X, Y).
Definition point.hpp:29
Represents a rectangular area (X, Y, Width, Height).
Definition rectangle.hpp:20
Color mColor
Current drawing color.
void drawPolyLine(PointVector const &points, unsigned int width) override
Draws lines between points with given width.
Uint8 r
Cached renderer color components (previous renderer color).
Uint8 a
Previous alpha component from renderer.
virtual void drawSDLTexture(SDL_Texture *texture, SDL_Rect source, SDL_Rect destination)
Draws an SDL_Texture on the target surface.
void drawLine(int x1, int y1, int x2, int y2) override
Draws a line.
void popClipArea() override
Removes the top most clip area from the stack.
void drawFillCircleSegment(Point const &center, unsigned int radius, int startAngle, int endAngle) override
Draws a filled circle segment.
void restoreRenderColor()
Restore the rendering color after drawing.
void drawRoundStroke(int x1, int y1, int x2, int y2, unsigned int width) override
Draws a round brush stroke along the line segment.
void _beginDraw() override
Initializes drawing.
virtual void setTarget(SDL_Renderer *renderer, int width, int height)
Sets the target SDL_Renderer to use for drawing.
void drawHorizontalLine(int x1, int y, int x2)
Draws a horizontal line.
void saveRenderColor()
Save the current rendering color before drawing.
void drawFillCircle(Point const &center, unsigned int radius) override
Draws a filled circle.
void drawImage(fcn::Image const *image, int srcX, int srcY, int dstX, int dstY, int width, int height) override
Draws a part of an image.
std::shared_ptr< Font > createFont(std::string const &filename, int size) override
Creates a font for this graphics backend.
Uint8 g
Previous green component from renderer.
SDL_Renderer * mRenderTarget
The SDL_Renderer used for accelerated drawing.
bool pushClipArea(fcn::Rectangle area) override
Pushes a clip area onto the stack.
void drawPoint(int x, int y) override
Draws a single point/pixel.
Color const & getColor() const override
Gets the color to use when drawing.
void drawCircleSegment(Point const &center, unsigned int radius, int startAngle, int endAngle) override
Draws a simple, non-filled circle segment with a one pixel width.
Uint8 b
Previous blue component from renderer.
void setColor(Color const &color) override
Sets the color to use when drawing.
void drawVerticalLine(int x, int y1, int y2)
Draws a vertical line.
bool mAlpha
Whether alpha blending is enabled.
void drawRectangle(Rectangle const &rectangle) override
Draws a simple, non-filled rectangle with a one pixel width.
virtual SDL_Renderer * getRenderTarget() const
Gets the target SDL_Renderer.
void _endDraw() override
Deinitializes the drawing process.
void fillRectangle(Rectangle const &rectangle) override
Draws a filled rectangle.
void drawCircle(Point const &center, unsigned int radius) override
Draws a simple, non-filled circle with a one pixel width.
void drawBezier(PointVector const &points, int segments, unsigned int width) override
Draws a bezier curve.
Unified header for the SDL backend.