aboutsummaryrefslogtreecommitdiffstats
path: root/README.md
blob: 027fff78b139f09982934843714719d6e71d7710 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
##fakecanvas

fakecanvas is an attempt at emulating the functionality of canvases (render to texture) for hardware that does not support them. it was meant to answer a question: can you do it? the answer is yes, but at a cost...

it is more or less a drop-in library, in that all you need to do is call `require 'fakecanvas'` (preferably inside `love.load`) to use it. fakecanvas' own functions will only be used if `love.graphics.isSupported "canvas"` is false, unless this behavior is overridden (see below). note: you must be using LÖVE 0.8

it uses (a few) screenshots in order to isolate drawing operations, which means there are some amusing drawbacks:

* `setCanvas()` is fairly expensive
* clearing a canvas is really expensive
* each `setCanvas()` call will allocate several megs of ram, this isn't a huge deal if you draw to canvases only occasionally. draw to one every frame, though...
* 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
 
not to mention:

* it's apparently impossible to seamlessly impersonate real canvases, so functions that involve canvases (specifically those that need the image data) need to be wrapped. some of these have probably been missed.
* it uses the debug library, which may or may not be available
* the above is used to poke around LÖVE's internals (currently, to wrap pixeleffects' `send()` method) which is especially dodgy
* ??? 

TODOs and/or untested:

* consecutive `setCanvas()` calls with a canvas argument, eg:

```lua
love.graphics.setCanvas(canvas1)
-- drawing here
love.graphics.setCanvas(canvas2)
-- drawing here
love.graphics.setCanvas(canvas3)
-- drawing here
```
the background gets saved when `setCanvas()` is called with an argument, but it doesn't get restored until it's called without an argument. currently only one state is saved so it's possible the above code would trash the background. this isn't a problem if you only ever use `renderTo()` because that automatically handles the `setCanvas()` calls
* will changing modes during canvas drawing destroy the contents?

beware: i have done only minimal testing of this library so it's possible there are cases where it doesn't work correctly or at all, or that the entire idea is ultimately unworkable!

so, consider this more of a proof-of-concept than as an actual alternative. get better drivers/hardware dammit >:(

###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 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
 * `false`: disable canvas functions even if real canvases are supported
 * `nil` (or no argument): **default**. fakecanvas will only use its own functions if real canvases are not supported

* `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
local c
function love.load () 
  local fc = require 'fakecanvas'.enable(true) -- force usage of fake canvases
  
  c = love.graphics.newCanvas(fc.getMaxCanvasSize(256, 256))
  
  c:renderTo(function () 
    love.graphics.circle("fill", 128, 128, 96)
    love.graphics.setColor(0, 0, 0, 255)
    love.graphics.printf("did it work?", 0, 128, 256, "center")
    love.graphics.setColor(255, 255, 255, 255)
  end)
  
end

function love.draw ()
  love.graphics.draw (c, 0, 0)
end
```