5#include <fifechan/backends/opengl/graphics.hpp>
6#include <fifechan/backends/opengl/image.hpp>
7#include <fifechan/exception.hpp>
8#include <fifechan/image.hpp>
15 #include <OpenGL/gl.h>
32 Graphics::~Graphics() =
default;
37 GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT | GL_FOG_BIT | GL_LIGHTING_BIT |
38 GL_LINE_BIT | GL_POINT_BIT | GL_POLYGON_BIT | GL_SCISSOR_BIT | GL_STENCIL_BUFFER_BIT | GL_TEXTURE_BIT |
41 glMatrixMode(GL_MODELVIEW);
45 glMatrixMode(GL_TEXTURE);
49 glMatrixMode(GL_PROJECTION);
53 glOrtho(0.0,
static_cast<double>(
mWidth),
static_cast<double>(
mHeight), 0.0, -1.0, 1.0);
55 glDisable(GL_LIGHTING);
56 glDisable(GL_CULL_FACE);
57 glDisable(GL_DEPTH_TEST);
58 glDisable(GL_TEXTURE_2D);
60 glEnable(GL_SCISSOR_TEST);
64 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
66 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
73 glMatrixMode(GL_MODELVIEW);
76 glMatrixMode(GL_TEXTURE);
79 glMatrixMode(GL_PROJECTION);
123 auto const * srcImage =
dynamic_cast<OpenGLImage
const *
>(image);
125 if (srcImage ==
nullptr) {
126 throwException(
"Trying to draw an image of unknown format, must be an OpenGLImage.");
131 "The clip stack is empty, perhaps you called a draw function outside of _beginDraw() and _endDraw()?");
140 float const texX1 = srcX /
static_cast<float>(srcImage->getTextureWidth());
141 float const texY1 = srcY /
static_cast<float>(srcImage->getTextureHeight());
142 float const texX2 = (srcX + width) /
static_cast<float>(srcImage->getTextureWidth());
143 float const texY2 = (srcY + height) /
static_cast<float>(srcImage->getTextureHeight());
145 glBindTexture(GL_TEXTURE_2D, srcImage->getTextureHandle());
147 glEnable(GL_TEXTURE_2D);
149 GLboolean
const blendWasEnabled = glIsEnabled(GL_BLEND);
150 if (blendWasEnabled == 0U) {
156 glTexCoord2f(texX1, texY1);
157 glVertex3i(dstX, dstY, 0);
159 glTexCoord2f(texX1, texY2);
160 glVertex3i(dstX, dstY + height, 0);
162 glTexCoord2f(texX2, texY2);
163 glVertex3i(dstX + width, dstY + height, 0);
165 glTexCoord2f(texX2, texY1);
166 glVertex3i(dstX + width, dstY, 0);
168 glDisable(GL_TEXTURE_2D);
170 if (blendWasEnabled == 0U) {
179 "The clip stack is empty, perhaps you called a draw function outside of _beginDraw() and _endDraw()?");
196 "The clip stack is empty, perhaps you called a draw function outside of _beginDraw() and _endDraw()?");
207 glVertex2f(x1 + 0.375F, y1 + 0.375F);
208 glVertex2f(x2 + 1.0F - 0.375F, y2 + 1.0F - 0.375F);
212 glVertex2f(x2 + 1.0F - 0.375F, y2 + 1.0F - 0.375F);
216 glVertex2f(x1 + 0.375F, y1 + 0.375F);
239 "The clip stack is empty, perhaps you called a draw function outside of _beginDraw() and _endDraw()?");
244 glBegin(GL_LINE_LOOP);
256 "The clip stack is empty, perhaps you called a draw function outside of _beginDraw() and _endDraw()?");
293 static_cast<GLubyte
>(color.
r),
294 static_cast<GLubyte
>(color.
g),
295 static_cast<GLubyte
>(color.
b),
296 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 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.
virtual void setTargetPlane(int width, int height)
Sets the target plane on where to draw.
virtual int getTargetPlaneWidth() const
Gets the target plane width.
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.