aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorxgoff <antipotroast@gmail.com>2012-08-03 13:26:16 -0500
committerxgoff <antipotroast@gmail.com>2012-08-03 13:26:16 -0500
commit4c9176ee3ab1f434d9bccb2955f852df105c1702 (patch)
treebbb600dff6d6eff24471fd19dce5e90691cb4a0b
parent0a822c648427155c2649665f5f083ee299644ab9 (diff)
downloadlove2d-fakecanvas-4c9176ee3ab1f434d9bccb2955f852df105c1702.tar.gz
love2d-fakecanvas-4c9176ee3ab1f434d9bccb2955f852df105c1702.tar.bz2
love2d-fakecanvas-4c9176ee3ab1f434d9bccb2955f852df105c1702.tar.xz
love2d-fakecanvas-4c9176ee3ab1f434d9bccb2955f852df105c1702.zip
add setOption and getOption functions.
to start off, there is an option "vflip", used for potential performance increase, at the cost of incorrect texture coordinates
-rw-r--r--README.md9
-rw-r--r--fakecanvas.lua36
2 files changed, 37 insertions, 8 deletions
diff --git a/README.md b/README.md
index 87749b7..22f5e2c 100644
--- a/README.md
+++ b/README.md
@@ -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