diff options
-rw-r--r-- | README.md | 9 | ||||
-rw-r--r-- | fakecanvas.lua | 36 |
2 files changed, 37 insertions, 8 deletions
@@ -6,7 +6,7 @@ it is more or less a drop-in library, in that all you need to do is call `requir it uses (a few) screenshots in order to isolate drawing operations, which means there are some amusing drawbacks: -* it's slow +* `setCanvas()` is fairly expensive * possible drawing issues if you call `love.graphics.present()` yourself, specifically between `setCanvas()` calls * canvas width/height cannot exceed the window's width/height, and if your hardware lacks PO2 support, canvases will be further limited to that as well. ie: for an 800x600 display the max canvas size is 512x512. 1024x768 will limit you to 1024x512, and so on. * any other weirdness you might run across @@ -39,7 +39,7 @@ so, consider this more of a proof-of-concept than as an actual alternative. get ###API -fakecanvas directly replaces some of LÖVE's functions, so there are no special functions you need to use. however, the library itself consists of a couple extra functions: +fakecanvas directly replaces some of LÖVE's functions, so there are no special functions you need to use. however, the library itself consists of some extra functions: * `enable([state])`: control fakecanvas' usage directly by passing the `state` argument. returns the module, for your chaining pleasure. `state` can be one of: * `true`: force usage of fakecanvas' functions even if real canvases are supported @@ -48,6 +48,11 @@ fakecanvas directly replaces some of LÖVE's functions, so there are no special * `getMaxCanvasSize([hint_w, hint_h])`: returns the maximum size fakecanvas can use for fake canvases. real canvases can likely be made much larger, so this can be used to put an upper limit on their size if needed. for convenience, you can provide width and height hints to this function, which will be clamped if they exceed the maximum size, or returned unmodified if they don't. +* `setOption(name, value)`: sets the option `name` to `value`. current options are: + * `"vflip"`: use vertically-flipped texture coords for the canvas image, to match those of real canvases. visually this makes no difference, but does make a difference inside shaders. if your shaders are not sensitive to this information, you can disable this option and get a possible performance boost. by setting this option before calling `setCanvas()`, you can control this behavior on a per fake canvas basis. boolean: default `true` + +* `getOption(name)`: gets the current value of option `name`. + ### Example main.lua ```lua diff --git a/fakecanvas.lua b/fakecanvas.lua index 39482ce..b170896 100644 --- a/fakecanvas.lua +++ b/fakecanvas.lua @@ -39,6 +39,16 @@ local canvasmt = { __index = canvas } local canvases = setmetatable({ }, { __mode = "k" }) +local options = { + -- flip fake canvas images vertically (then unflip with quad) in order to + -- match real canvas orientation. this only makes a difference inside + -- shaders, when using the canvas' texture coordinates. if you do not need + -- to use this information, you can get a performance boost by disabling + -- this option. you can enable or disable vflipping for individual fake + -- canvases by placing setOption() calls around the newCanvas() call. + vflip = true, +} + function canvas:clear (...) -- other option is chucking out the imagedata and creating a new one, but i'd probably end up using mapPixel anyway local nargs = select("#", ...) @@ -115,8 +125,11 @@ local function Canvas (width, height) c._imagedata = love.image.newImageData(w, h) c._image = love.graphics.newImage(c._imagedata) c._quad = love.graphics.newQuad(0, 0, w, h, w, h) + c._vflip = options.vflip - c._quad:flip(false, true) -- flip vertically part 0 + if options.vflip then + c._quad:flip(false, true) -- flip vertically part 0 + end local p = newproxy(true) @@ -166,11 +179,14 @@ local function setCanvas (...) love.graphics.setBackgroundColor(0, 0, 0, 0) love.graphics.clear() - -- flip vertically (unfortunately) so it can later be drawn unflipped in order to match texcoords of real canvases. part 1 - local flipped = love.graphics.newImage(tempdata) - love.graphics.draw(flipped, 0, current_canvas._imagedata:getHeight(), 0, 1, -1) - - local newdata = love.graphics.newScreenshot() + local newdata = tempdata + if current_canvas._vflip then + -- flip vertically (unfortunately) so it can later be drawn unflipped in order to match texcoords of real canvases. part 1 + local flipped = love.graphics.newImage(tempdata) + love.graphics.draw(flipped, 0, current_canvas._imagedata:getHeight(), 0, 1, -1) + + newdata = love.graphics.newScreenshot() + end --newdata:encode("__canvas.png") @@ -284,6 +300,14 @@ function M.getMaxCanvasSize (hw, hh) end end +function M.setOption (name, value) + options[name] = value +end + +function M.getOption (name) + return options[name] +end + M.enable() return M
\ No newline at end of file |