mirror of
https://github.com/love2d/love.git
synced 2026-08-16 00:02:12 +02:00
More code restructuring.
Move all love.graphics functionality that doesn’t call OpenGL out of the OpenGL backend Graphics class. --HG-- branch : minor
This commit is contained in:
+302
-10
@@ -30,7 +30,12 @@
|
||||
#include "vertex.h"
|
||||
#include "Color.h"
|
||||
#include "Texture.h"
|
||||
#include "Canvas.h"
|
||||
#include "Font.h"
|
||||
#include "Shader.h"
|
||||
#include "Quad.h"
|
||||
#include "math/Transform.h"
|
||||
#include "font/Rasterizer.h"
|
||||
|
||||
// C++
|
||||
#include <string>
|
||||
@@ -38,6 +43,9 @@
|
||||
|
||||
namespace love
|
||||
{
|
||||
|
||||
class Reference;
|
||||
|
||||
namespace graphics
|
||||
{
|
||||
|
||||
@@ -283,12 +291,43 @@ public:
|
||||
Graphics *gfx;
|
||||
};
|
||||
|
||||
typedef void (*ScreenshotCallback)(love::image::ImageData *i, Reference *ref, void *ud);
|
||||
|
||||
struct ScreenshotInfo
|
||||
{
|
||||
ScreenshotCallback callback;
|
||||
Reference *ref;
|
||||
};
|
||||
|
||||
Graphics();
|
||||
virtual ~Graphics();
|
||||
|
||||
// Implements Module.
|
||||
virtual ModuleType getModuleType() const { return M_GRAPHICS; }
|
||||
|
||||
Quad *newQuad(Quad::Viewport v, double sw, double sh);
|
||||
|
||||
virtual Font *newFont(love::font::Rasterizer *data, const Texture::Filter &filter = Texture::defaultFilter) = 0;
|
||||
|
||||
virtual Canvas *newCanvas(int width, int height, const Canvas::Settings &settings) = 0;
|
||||
|
||||
virtual Shader *newShader(const Shader::ShaderSource &source) = 0;
|
||||
|
||||
/**
|
||||
* Resets the current color, background color, line style, and so forth.
|
||||
**/
|
||||
void reset();
|
||||
|
||||
virtual void clear(Colorf color) = 0;
|
||||
virtual void clear(const std::vector<OptionalColorf> &colors) = 0;
|
||||
|
||||
virtual void discard(const std::vector<bool> &colorbuffers, bool depthstencil) = 0;
|
||||
|
||||
/**
|
||||
* Flips buffers. (Rendered geometry is presented on screen).
|
||||
**/
|
||||
virtual void present(void *screenshotCallbackData) = 0;
|
||||
|
||||
/**
|
||||
* Sets the current graphics display viewport dimensions.
|
||||
**/
|
||||
@@ -319,25 +358,190 @@ public:
|
||||
* Normally the module will always be active as long as a window exists, it
|
||||
* may be different on some platforms (especially mobile ones.)
|
||||
**/
|
||||
virtual bool isActive() const = 0;
|
||||
bool isActive() const;
|
||||
|
||||
virtual int getWidth() const = 0;
|
||||
virtual int getHeight() const = 0;
|
||||
/**
|
||||
* True if a graphics viewport is set.
|
||||
**/
|
||||
bool isCreated() const;
|
||||
|
||||
virtual bool isCanvasActive() const = 0;
|
||||
int getWidth() const;
|
||||
int getHeight() const;
|
||||
int getPixelWidth() const;
|
||||
int getPixelHeight() const;
|
||||
|
||||
virtual void flushStreamDraws() = 0;
|
||||
StreamVertexData requestStreamDraw(const StreamDrawRequest &request);
|
||||
double getCurrentPixelDensity() const;
|
||||
double getScreenPixelDensity() const;
|
||||
|
||||
virtual Colorf getColor() const = 0;
|
||||
/**
|
||||
* Sets the current constant color.
|
||||
**/
|
||||
virtual void setColor(Colorf c) = 0;
|
||||
|
||||
virtual float getLineWidth() const = 0;
|
||||
virtual LineStyle getLineStyle() const = 0;
|
||||
virtual LineJoin getLineJoin() const = 0;
|
||||
/**
|
||||
* Gets current color.
|
||||
**/
|
||||
Colorf getColor() const;
|
||||
|
||||
/**
|
||||
* Sets the background Color.
|
||||
**/
|
||||
void setBackgroundColor(Colorf c);
|
||||
|
||||
/**
|
||||
* Gets the current background color.
|
||||
**/
|
||||
Colorf getBackgroundColor() const;
|
||||
|
||||
void setFont(Font *font);
|
||||
Font *getFont();
|
||||
|
||||
void setShader(Shader *shader);
|
||||
void setShader();
|
||||
|
||||
Shader *getShader() const;
|
||||
|
||||
void setCanvas(Canvas *canvas);
|
||||
virtual void setCanvas(const std::vector<Canvas *> &canvases) = 0;
|
||||
void setCanvas(const std::vector<StrongRef<Canvas>> &canvases);
|
||||
virtual void setCanvas() = 0;
|
||||
|
||||
std::vector<Canvas *> getCanvas() const;
|
||||
bool isCanvasActive() const;
|
||||
bool isCanvasActive(Canvas *canvas) const;
|
||||
|
||||
/**
|
||||
* Scissor defines a box such that everything outside that box is discarded
|
||||
* and not drawn. Scissoring is automatically enabled.
|
||||
* @param rect The rectangle defining the scissor area.
|
||||
**/
|
||||
virtual void setScissor(const Rect &rect) = 0;
|
||||
void intersectScissor(const Rect &rect);
|
||||
|
||||
/**
|
||||
* Clears any scissor that has been created.
|
||||
**/
|
||||
virtual void setScissor() = 0;
|
||||
|
||||
/**
|
||||
* Gets the current scissor box.
|
||||
* @return Whether the scissor is enabled.
|
||||
*/
|
||||
bool getScissor(Rect &rect) const;
|
||||
|
||||
/**
|
||||
* Enables or disables drawing to the stencil buffer. When enabled, the
|
||||
* color buffer is disabled.
|
||||
**/
|
||||
virtual void drawToStencilBuffer(StencilAction action, int value) = 0;
|
||||
virtual void stopDrawToStencilBuffer() = 0;
|
||||
|
||||
/**
|
||||
* Sets whether stencil testing is enabled.
|
||||
**/
|
||||
virtual void setStencilTest(CompareMode compare, int value) = 0;
|
||||
virtual void setStencilTest() = 0;
|
||||
void getStencilTest(CompareMode &compare, int &value);
|
||||
|
||||
/**
|
||||
* Clear the stencil buffer in the active Canvas(es.)
|
||||
**/
|
||||
virtual void clearStencil() = 0;
|
||||
|
||||
/**
|
||||
* Sets the enabled color components when rendering.
|
||||
**/
|
||||
virtual void setColorMask(ColorMask mask) = 0;
|
||||
|
||||
/**
|
||||
* Gets the current color mask.
|
||||
**/
|
||||
ColorMask getColorMask() const;
|
||||
|
||||
/**
|
||||
* Sets the current blend mode.
|
||||
**/
|
||||
virtual void setBlendMode(BlendMode mode, BlendAlpha alphamode) = 0;
|
||||
|
||||
/**
|
||||
* Gets the current blend mode.
|
||||
**/
|
||||
BlendMode getBlendMode(BlendAlpha &alphamode) const;
|
||||
|
||||
/**
|
||||
* Sets the default filter for images, canvases, and fonts.
|
||||
**/
|
||||
void setDefaultFilter(const Texture::Filter &f);
|
||||
|
||||
/**
|
||||
* Gets the default filter for images, canvases, and fonts.
|
||||
**/
|
||||
const Texture::Filter &getDefaultFilter() const;
|
||||
|
||||
/**
|
||||
* Default Image mipmap filter mode and sharpness values.
|
||||
**/
|
||||
void setDefaultMipmapFilter(Texture::FilterMode filter, float sharpness);
|
||||
void getDefaultMipmapFilter(Texture::FilterMode *filter, float *sharpness) const;
|
||||
|
||||
/**
|
||||
* Sets the line width.
|
||||
* @param width The new width of the line.
|
||||
**/
|
||||
void setLineWidth(float width);
|
||||
float getLineWidth() const;
|
||||
|
||||
/**
|
||||
* Sets the line style.
|
||||
* @param style LINE_ROUGH or LINE_SMOOTH.
|
||||
**/
|
||||
void setLineStyle(LineStyle style);
|
||||
LineStyle getLineStyle() const;
|
||||
|
||||
/**
|
||||
* Sets the line join mode.
|
||||
**/
|
||||
void setLineJoin(LineJoin style);
|
||||
LineJoin getLineJoin() const;
|
||||
|
||||
/**
|
||||
* Sets the size of points.
|
||||
**/
|
||||
virtual void setPointSize(float size) = 0;
|
||||
|
||||
/**
|
||||
* Gets the point size.
|
||||
**/
|
||||
float getPointSize() const;
|
||||
|
||||
/**
|
||||
* Sets whether graphics will be drawn as wireframe lines instead of filled
|
||||
* triangles (has no effect for drawn points.)
|
||||
* This should only be used as a debugging tool. The wireframe lines do not
|
||||
* behave the same as regular love.graphics lines.
|
||||
**/
|
||||
virtual void setWireframe(bool enable) = 0;
|
||||
|
||||
/**
|
||||
* Gets whether wireframe drawing mode is enabled.
|
||||
**/
|
||||
bool isWireframe() const;
|
||||
|
||||
void captureScreenshot(const ScreenshotInfo &info);
|
||||
|
||||
void draw(Drawable *drawable, const Matrix4 &m);
|
||||
void draw(Texture *texture, Quad *quad, const Matrix4 &m);
|
||||
|
||||
/**
|
||||
* Draws text at the specified coordinates
|
||||
**/
|
||||
void print(const std::vector<Font::ColoredString> &str, const Matrix4 &m);
|
||||
|
||||
/**
|
||||
* Draws formatted text on screen at the specified coordinates.
|
||||
**/
|
||||
void printf(const std::vector<Font::ColoredString> &str, float wrap, Font::AlignMode align, const Matrix4 &m);
|
||||
|
||||
/**
|
||||
* Draws a point at (x,y).
|
||||
* @param x Point along x-axis.
|
||||
@@ -420,11 +624,31 @@ public:
|
||||
**/
|
||||
void polygon(DrawMode mode, const float *coords, size_t count);
|
||||
|
||||
/**
|
||||
* Gets whether a graphics feature is supported on this system.
|
||||
**/
|
||||
virtual bool isSupported(Feature feature) const = 0;
|
||||
|
||||
/**
|
||||
* Gets the system-dependent numeric limit for the specified parameter.
|
||||
**/
|
||||
virtual double getSystemLimit(SystemLimit limittype) const = 0;
|
||||
|
||||
/**
|
||||
* Returns system-dependent renderer information.
|
||||
* Returned strings can vary greatly between systems! Do not rely on it for
|
||||
* anything!
|
||||
**/
|
||||
virtual RendererInfo getRendererInfo() const = 0;
|
||||
|
||||
/**
|
||||
* Returns performance-related statistics.
|
||||
**/
|
||||
virtual Stats getStats() const = 0;
|
||||
|
||||
void push(StackType type = STACK_TRANSFORM);
|
||||
void pop();
|
||||
|
||||
const Matrix4 &getTransform() const;
|
||||
const Matrix4 &getProjection() const;
|
||||
|
||||
@@ -440,6 +664,9 @@ public:
|
||||
Vector transformPoint(Vector point);
|
||||
Vector inverseTransformPoint(Vector point);
|
||||
|
||||
virtual void flushStreamDraws() = 0;
|
||||
StreamVertexData requestStreamDraw(const StreamDrawRequest &request);
|
||||
|
||||
template <typename T>
|
||||
T *getScratchBuffer(size_t count)
|
||||
{
|
||||
@@ -484,8 +711,48 @@ public:
|
||||
static bool getConstant(const char *in, StackType &out);
|
||||
static bool getConstant(StackType in, const char *&out);
|
||||
|
||||
// Default shader code (a shader is always required internally.)
|
||||
static Shader::ShaderSource defaultShaderCode[RENDERER_MAX_ENUM][2];
|
||||
static Shader::ShaderSource defaultVideoShaderCode[RENDERER_MAX_ENUM][2];
|
||||
|
||||
protected:
|
||||
|
||||
struct DisplayState
|
||||
{
|
||||
Colorf color = Colorf(1.0, 1.0, 1.0, 1.0);
|
||||
Colorf gammaCorrectedColor = Colorf(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
Colorf backgroundColor = Colorf(0.0, 0.0, 0.0, 1.0);
|
||||
|
||||
BlendMode blendMode = BLEND_ALPHA;
|
||||
BlendAlpha blendAlphaMode = BLENDALPHA_MULTIPLY;
|
||||
|
||||
float lineWidth = 1.0f;
|
||||
LineStyle lineStyle = LINE_SMOOTH;
|
||||
LineJoin lineJoin = LINE_JOIN_MITER;
|
||||
|
||||
float pointSize = 1.0f;
|
||||
|
||||
bool scissor = false;
|
||||
Rect scissorRect = Rect();
|
||||
|
||||
CompareMode stencilCompare = COMPARE_ALWAYS;
|
||||
int stencilTestValue = 0;
|
||||
|
||||
StrongRef<Font> font;
|
||||
StrongRef<Shader> shader;
|
||||
|
||||
std::vector<StrongRef<Canvas>> canvases;
|
||||
|
||||
ColorMask colorMask = ColorMask(true, true, true, true);
|
||||
|
||||
bool wireframe = false;
|
||||
|
||||
Texture::Filter defaultFilter = Texture::Filter();
|
||||
|
||||
Texture::FilterMode defaultMipmapFilter = Texture::FILTER_NEAREST;
|
||||
float defaultMipmapSharpness = 0.0f;
|
||||
};
|
||||
|
||||
struct StreamBufferState
|
||||
{
|
||||
StreamBuffer *vb[2];
|
||||
@@ -498,10 +765,27 @@ protected:
|
||||
int indexCount;
|
||||
};
|
||||
|
||||
void restoreState(const DisplayState &s);
|
||||
void restoreStateChecked(const DisplayState &s);
|
||||
|
||||
void pushTransform();
|
||||
void pushIdentityTransform();
|
||||
void popTransform();
|
||||
|
||||
int width;
|
||||
int height;
|
||||
int pixelWidth;
|
||||
int pixelHeight;
|
||||
|
||||
bool created;
|
||||
bool active;
|
||||
|
||||
bool writingToStencil;
|
||||
|
||||
StrongRef<love::graphics::Font> defaultFont;
|
||||
|
||||
std::vector<ScreenshotInfo> pendingScreenshotCallbacks;
|
||||
|
||||
StreamBufferState streamBufferState;
|
||||
|
||||
std::vector<Matrix4> transformStack;
|
||||
@@ -509,8 +793,16 @@ protected:
|
||||
|
||||
std::vector<double> pixelScaleStack;
|
||||
|
||||
std::vector<DisplayState> states;
|
||||
std::vector<StackType> stackTypeStack;
|
||||
|
||||
int canvasSwitchCount;
|
||||
|
||||
static const size_t MAX_USER_STACK_DEPTH = 64;
|
||||
|
||||
private:
|
||||
|
||||
void checkSetDefaultFont();
|
||||
int calculateEllipsePoints(float rx, float ry) const;
|
||||
|
||||
std::vector<uint8> scratchBuffer;
|
||||
|
||||
Reference in New Issue
Block a user