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:
Alex Szpakowski
2016-12-30 18:36:54 -04:00
parent d94b363b39
commit f12596dd19
38 changed files with 1603 additions and 1431 deletions
+45 -316
View File
@@ -38,10 +38,7 @@
#include "video/VideoStream.h"
#include "graphics/Font.h"
#include "Image.h"
#include "graphics/Quad.h"
#include "graphics/Texture.h"
#include "SpriteBatch.h"
#include "ParticleSystem.h"
#include "Canvas.h"
@@ -53,8 +50,6 @@
namespace love
{
class Reference;
namespace graphics
{
namespace opengl
@@ -64,321 +59,81 @@ class Graphics final : public love::graphics::Graphics
{
public:
typedef void (*ScreenshotCallback)(love::image::ImageData *i, Reference *ref, void *ud);
struct ScreenshotInfo
{
ScreenshotCallback callback;
Reference *ref;
};
Graphics();
virtual ~Graphics();
// Implements Module.
const char *getName() const override;
Image *newImage(const std::vector<love::image::ImageData *> &data, const Image::Settings &settings);
Image *newImage(const std::vector<love::image::CompressedImageData *> &cdata, const Image::Settings &settings);
love::graphics::Font *newFont(love::font::Rasterizer *data, const Texture::Filter &filter = Texture::defaultFilter) override;
SpriteBatch *newSpriteBatch(Texture *texture, int size, vertex::Usage usage);
ParticleSystem *newParticleSystem(Texture *texture, int size);
love::graphics::Canvas *newCanvas(int width, int height, const Canvas::Settings &settings) override;
love::graphics::Shader *newShader(const Shader::ShaderSource &source) override;
Mesh *newMesh(const std::vector<Vertex> &vertices, Mesh::DrawMode drawmode, vertex::Usage usage);
Mesh *newMesh(int vertexcount, Mesh::DrawMode drawmode, vertex::Usage usage);
Mesh *newMesh(const std::vector<Mesh::AttribFormat> &vertexformat, int vertexcount, Mesh::DrawMode drawmode, vertex::Usage usage);
Mesh *newMesh(const std::vector<Mesh::AttribFormat> &vertexformat, const void *data, size_t datasize, Mesh::DrawMode drawmode, vertex::Usage usage);
Text *newText(love::graphics::Font *font, const std::vector<Font::ColoredString> &text = {});
Video *newVideo(love::video::VideoStream *stream, float pixeldensity);
void setViewportSize(int width, int height, int pixelwidth, int pixelheight) override;
bool setMode(int width, int height, int pixelwidth, int pixelheight) override;
void unSetMode() override;
void setActive(bool active) override;
bool isActive() const override;
void flushStreamDraws() override;
/**
* Resets the current color, background color,
* line style, and so forth. (This will be called
* when the game reloads.
**/
void reset();
void clear(Colorf color) override;
void clear(const std::vector<OptionalColorf> &colors) override;
void clear(Colorf color);
void clear(const std::vector<OptionalColorf> &colors);
void discard(const std::vector<bool> &colorbuffers, bool depthstencil) override;
void discard(const std::vector<bool> &colorbuffers, bool depthstencil);
void present(void *screenshotCallbackData) override;
bool isCanvasActive() const override;
bool isCanvasActive(Canvas *canvas) const;
void setColor(Colorf c) override;
/**
* Flips buffers. (Rendered geometry is presented on screen).
**/
void present(void *screenshotCallbackData);
void setCanvas(const std::vector<love::graphics::Canvas *> &canvases) override;
void setCanvas() override;
int getWidth() const override;
int getHeight() const override;
int getPixelWidth() const;
int getPixelHeight() const;
void setScissor(const Rect &rect) override;
void setScissor() override;
double getCurrentPixelDensity() const;
double getScreenPixelDensity() const;
void drawToStencilBuffer(StencilAction action, int value) override;
void stopDrawToStencilBuffer() override;
/**
* True if a graphics viewport is set.
**/
bool isCreated() const;
void setStencilTest(CompareMode compare, int value) override;
void setStencilTest() override;
void setCanvas(Canvas *canvas);
void setCanvas(const std::vector<Canvas *> &canvases);
void setCanvas(const std::vector<StrongRef<Canvas>> &canvases);
void setCanvas();
void clearStencil() override;
std::vector<Canvas *> getCanvas() const;
void setColorMask(ColorMask mask) override;
/**
* 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.
**/
void setScissor(const Rect &rect);
void intersectScissor(const Rect &rect);
void setBlendMode(BlendMode mode, BlendAlpha alphamode) override;
/**
* Clears any scissor that has been created.
**/
void setScissor();
void setPointSize(float size) override;
/**
* 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.
**/
void drawToStencilBuffer(StencilAction action, int value);
void stopDrawToStencilBuffer();
/**
* Sets whether stencil testing is enabled.
**/
void setStencilTest(CompareMode compare, int value);
void setStencilTest();
void getStencilTest(CompareMode &compare, int &value);
/**
* Clear the stencil buffer in the active Canvas(es.)
**/
void clearStencil();
/**
* Creates an Image object with padding and/or optimization.
**/
Image *newImage(const std::vector<love::image::ImageData *> &data, const Image::Settings &settings);
Image *newImage(const std::vector<love::image::CompressedImageData *> &cdata, const Image::Settings &settings);
Quad *newQuad(Quad::Viewport v, double sw, double sh);
/**
* Creates a Font object.
**/
graphics::Font *newFont(love::font::Rasterizer *data, const Texture::Filter &filter = Texture::getDefaultFilter());
SpriteBatch *newSpriteBatch(Texture *texture, int size, Mesh::Usage usage);
ParticleSystem *newParticleSystem(Texture *texture, int size);
Canvas *newCanvas(int width, int height, const Canvas::Settings &settings);
Shader *newShader(const Shader::ShaderSource &source);
Mesh *newMesh(const std::vector<Vertex> &vertices, Mesh::DrawMode drawmode, Mesh::Usage usage);
Mesh *newMesh(int vertexcount, Mesh::DrawMode drawmode, Mesh::Usage usage);
Mesh *newMesh(const std::vector<Mesh::AttribFormat> &vertexformat, int vertexcount, Mesh::DrawMode drawmode, Mesh::Usage usage);
Mesh *newMesh(const std::vector<Mesh::AttribFormat> &vertexformat, const void *data, size_t datasize, Mesh::DrawMode drawmode, Mesh::Usage usage);
Text *newText(graphics::Font *font, const std::vector<Font::ColoredString> &text = {});
Video *newVideo(love::video::VideoStream *stream, float pixeldensity);
bool isGammaCorrect() const;
/**
* Sets the current constant color.
**/
void setColor(Colorf c);
/**
* Gets current color.
**/
Colorf getColor() const override;
/**
* Sets the background Color.
**/
void setBackgroundColor(Colorf c);
/**
* Gets the current background color.
**/
Colorf getBackgroundColor() const;
void setFont(love::graphics::Font *font);
love::graphics::Font *getFont();
void setShader(Shader *shader);
void setShader();
Shader *getShader() const;
/**
* Sets the enabled color components when rendering.
**/
void setColorMask(ColorMask mask);
/**
* Gets the current color mask.
**/
ColorMask getColorMask() const;
/**
* Sets the current blend mode.
**/
void setBlendMode(BlendMode mode, BlendAlpha alphamode);
/**
* 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);
/**
* Sets the line style.
* @param style LINE_ROUGH or LINE_SMOOTH.
**/
void setLineStyle(LineStyle style);
/**
* Sets the line join mode.
**/
void setLineJoin(LineJoin style);
float getLineWidth() const override;
LineStyle getLineStyle() const override;
LineJoin getLineJoin() const override;
/**
* Sets the size of points.
**/
void setPointSize(float size);
/**
* 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.
**/
void setWireframe(bool enable);
/**
* Gets whether wireframe drawing mode is enabled.
**/
bool isWireframe() const;
/**
* 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);
void captureScreenshot(const ScreenshotInfo &info);
/**
* Returns system-dependent renderer information.
* Returned strings can vary greatly between systems! Do not rely on it for
* anything!
**/
RendererInfo getRendererInfo() const;
/**
* Returns performance-related statistics.
**/
Stats getStats() const;
void setWireframe(bool enable) override;
bool isSupported(Feature feature) const override;
double getSystemLimit(SystemLimit limittype) const override;
/**
* Gets whether a graphics feature is supported on this system.
**/
bool isSupported(Feature feature) const;
void push(StackType type = STACK_TRANSFORM);
void pop();
RendererInfo getRendererInfo() const override;
Stats getStats() const override;
private:
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();
// Stencil.
CompareMode stencilCompare = COMPARE_ALWAYS;
int stencilTestValue = 0;
StrongRef<love::graphics::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 CachedRenderbuffer
{
int w;
@@ -388,44 +143,18 @@ private:
GLuint renderbuffer;
};
void restoreState(const DisplayState &s);
void restoreStateChecked(const DisplayState &s);
void endPass();
void bindCachedFBO(const std::vector<Canvas *> &canvases);
void bindCachedFBO(const std::vector<love::graphics::Canvas *> &canvases);
void discard(OpenGL::FramebufferTarget target, const std::vector<bool> &colorbuffers, bool depthstencil);
GLuint attachCachedStencilBuffer(int w, int h, int samples);
void setDebug(bool enable);
void checkSetDefaultFont();
StrongRef<love::graphics::Font> defaultFont;
std::vector<ScreenshotInfo> pendingScreenshotCallbacks;
std::unordered_map<uint32, GLuint> framebufferObjects;
std::vector<CachedRenderbuffer> stencilBuffers;
QuadIndices *quadIndices;
int width;
int height;
int pixelWidth;
int pixelHeight;
bool created;
bool active;
bool writingToStencil;
std::vector<DisplayState> states;
std::vector<StackType> stackTypes; // Keeps track of the pushed stack types.
int canvasSwitchCount;
static const size_t MAX_USER_STACK_DEPTH = 64;
}; // Graphics
} // opengl