6#include <fifechan/backends/opengl/graphics.hpp>
9#include "fifechan/platform.hpp"
16 #include <OpenGL/gl.h>
22#include <fifechan/backends/opengl/image.hpp>
23#include <fifechan/exception.hpp>
24#include <fifechan/image.hpp>
41 Graphics::~Graphics() =
default;
46 GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT | GL_FOG_BIT | GL_LIGHTING_BIT |
47 GL_LINE_BIT | GL_POINT_BIT | GL_POLYGON_BIT | GL_SCISSOR_BIT | GL_STENCIL_BUFFER_BIT | GL_TEXTURE_BIT |
50 glMatrixMode(GL_MODELVIEW);
54 glMatrixMode(GL_TEXTURE);
58 glMatrixMode(GL_PROJECTION);
62 glOrtho(0.0,
static_cast<double>(
mWidth),
static_cast<double>(
mHeight), 0.0, -1.0, 1.0);
64 glDisable(GL_LIGHTING);
65 glDisable(GL_CULL_FACE);
66 glDisable(GL_DEPTH_TEST);
67 glDisable(GL_TEXTURE_2D);
69 glEnable(GL_SCISSOR_TEST);
73 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
75 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
82 glMatrixMode(GL_MODELVIEW);
85 glMatrixMode(GL_TEXTURE);
88 glMatrixMode(GL_PROJECTION);
132 auto const * srcImage =
dynamic_cast<OpenGLImage const *
>(image);
134 if (srcImage ==
nullptr) {
135 throwException(
"Trying to draw an image of unknown format, must be an OpenGLImage.");
140 "The clip stack is empty, perhaps you called a draw function outside of _beginDraw() and _endDraw()?");
149 float const texX1 = srcX /
static_cast<float>(srcImage->getTextureWidth());
150 float const texY1 = srcY /
static_cast<float>(srcImage->getTextureHeight());
151 float const texX2 = (srcX + width) /
static_cast<float>(srcImage->getTextureWidth());
152 float const texY2 = (srcY + height) /
static_cast<float>(srcImage->getTextureHeight());
154 glBindTexture(GL_TEXTURE_2D, srcImage->getTextureHandle());
156 glEnable(GL_TEXTURE_2D);
158 GLboolean
const blendWasEnabled = glIsEnabled(GL_BLEND);
159 if (blendWasEnabled == 0U) {
165 glTexCoord2f(texX1, texY1);
166 glVertex3i(dstX, dstY, 0);
168 glTexCoord2f(texX1, texY2);
169 glVertex3i(dstX, dstY + height, 0);
171 glTexCoord2f(texX2, texY2);
172 glVertex3i(dstX + width, dstY + height, 0);
174 glTexCoord2f(texX2, texY1);
175 glVertex3i(dstX + width, dstY, 0);
177 glDisable(GL_TEXTURE_2D);
179 if (blendWasEnabled == 0U) {
188 "The clip stack is empty, perhaps you called a draw function outside of _beginDraw() and _endDraw()?");
205 "The clip stack is empty, perhaps you called a draw function outside of _beginDraw() and _endDraw()?");
216 glVertex2f(x1 + 0.375F, y1 + 0.375F);
217 glVertex2f(x2 + 1.0F - 0.375F, y2 + 1.0F - 0.375F);
221 glVertex2f(x2 + 1.0F - 0.375F, y2 + 1.0F - 0.375F);
225 glVertex2f(x1 + 0.375F, y1 + 0.375F);
248 "The clip stack is empty, perhaps you called a draw function outside of _beginDraw() and _endDraw()?");
253 glBegin(GL_LINE_LOOP);
265 "The clip stack is empty, perhaps you called a draw function outside of _beginDraw() and _endDraw()?");
302 static_cast<GLubyte
>(color.
r),
303 static_cast<GLubyte
>(color.
g),
304 static_cast<GLubyte
>(color.
b),
305 static_cast<GLubyte
>(color.
a));
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.
uint8_t a
Alpha color component (0-255).
uint8_t b
Blue color component (0-255).
uint8_t g
Green color component (0-255).
uint8_t r
Red color component (0-255).
virtual void popClipArea()
Removes the top most clip area from the stack.
virtual bool pushClipArea(Rectangle area)
Pushes a clip area onto the stack.
std::stack< ClipRectangle > mClipStack
Holds the clip area stack.
Abstract holder for image data.
Represents a 2D coordinate (X, Y).
Represents a rectangular area (X, Y, Width, Height).
int width
Holds the width of the rectangle.
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.
void setColor(Color const &color) override
Sets the color to use when drawing.
void drawFillCircle(Point const &p, unsigned int radius) override
Draws a filled circle.
void drawCircleSegment(Point const &p, unsigned int radius, int sangle, int eangle) override
Draws a simple, non-filled circle segment with a one pixel width.
void _beginDraw() override
Initializes drawing.
void drawLine(int x1, int y1, int x2, int y2) override
Draws a line.
void drawCircle(Point const &p, unsigned int radius) override
Draws a simple, non-filled circle with a one pixel width.
bool mAlpha
Whether alpha blending is enabled.
void drawBezier(PointVector const &points, int steps, unsigned int width) override
Draws a bezier curve.
void drawRectangle(Rectangle const &rectangle) override
Draws a simple, non-filled rectangle with a one pixel width.
void drawFillCircleSegment(Point const &p, unsigned int radius, int sangle, int eangle) override
Draws a filled circle segment.
void popClipArea() override
Removes the top most clip area from the stack.
void drawPoint(int x, int y) override
Draws a single point/pixel.
Color mColor
Current drawing color.
void _endDraw() override
Deinitializes the drawing process.
void fillRectangle(Rectangle const &rectangle) override
Draws a filled rectangle.
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.
int mWidth
Width of the logical target plane.
void drawPolyLine(PointVector const &points, unsigned int width) override
Draws lines between points with given width.
void setTargetPlane(int width, int height)
Sets the target plane on where to draw.
virtual int getTargetPlaneWidth() const
Gets the target plane width.
virtual void setTargetPlaneImpl(int width, int height)
Backend hook that actually applies the logical target plane size.
int mHeight
Height of the logical target plane.
virtual int getTargetPlaneHeight() const
Gets the target plane height.
Color const & getColor() const override
Gets the color to use when drawing.
bool pushClipArea(Rectangle area) override
Pushes a clip area onto the stack.
std::vector< Point > PointVector
A list of points.
void throwException(std::string const &message, std::source_location location=std::source_location::current())
Throw an Exception capturing the current source location.
fcn::opengl::Image OpenGLImage
Alias for the OpenGL image implementation.