Scrupp SVN Manual
Create your own 2D games and applications

Cairo Support

Scrupp SVN contains version 1.2 of the Cairo binding lua-oocairo written by Geoff Richards.

By using this Cairo binding it is possible to generate images. Due to the runtime generation these images can be completely resolution independant.

Another possibility is the creation of PNG, PDF, PS and SVG files.

The original website of lua-oocairo (http://www.daizucms.org/lua/library/oocairo/) is no longer available on the web. That's why I have uploaded a copy on this website. It contains the documentation of lua-oocairo.

The doc directory of the Scrupp distribution contains the lua-oocairo documentation as well.

In order to ease the usage of Cairo from Scrupp, the binding was modified and new functions were added to Scrupp.

Additional or Modified Cairo Functions

Cairo.image_surface_create_from_png(file/filename)
This function was modified to support the virtual filesystem provided by PhysFS. That means it can load the png from a zip archive. Apart from that, it behaves exactly the way described in the lua-oocairo documentation.
Cairo.image_surface_create_from_file(filename)
Loads the image file with the specified filename and returns an image surface which can be used with Cairo. This function supports the same image formats as scrupp.addImage() and the virtual filesystem provided by PhysFS.
Note: It does not support images with an alpha channel! In this case, save the image as png and use Cairo.image_surface_create_from_png().
The Cairo example masking.slua from the Scrupp distribution shows how to use this function.

Additional Scrupp Functions

scrupp.addImageFromCairo(surface)
Returns a Scrupp image generated from the specified Cairo surface.

Example

-- load the Cairo library
local Cairo = require "oocairo"

-- create an image surface with a size of 200x200 pixel
local surface = Cairo.image_surface_create("rgb24", 200, 200)

-- create a Cairo context used for drawing
local cr = Cairo.context_create(surface)

-- create some arbitrary shapes
cr:set_source_rgb(1, 1, 1)
cr:paint()

cr:move_to(0, 0)
cr:line_to(190, 100)
cr:line_to(100, 185)
cr:line_to(200, 200)
cr:line_to(30, 130)
cr:close_path()
cr:set_source_rgb(0.8, 0.4, 1)
cr:fill()

cr:move_to(180, 30)
cr:line_to(100, 20)
cr:line_to(80, 120)
cr:set_source_rgb(0.5, 0.7, 0.3)
cr:fill_preserve()
cr:set_source_rgb(0, 0, 0)
cr:set_line_width(3)
cr:stroke()

-- use scrupp to display the surface
scrupp.init("Cairo: simple-example", 200, 200, 32, false)

-- convert the Cairo surface to a Scrupp image
local image = scrupp.addImageFromCairo(surface)

main = {
	render = function(dt)
		image:render(0,0)
	end,
	
	keypressed = function(key)
		if key == "ESCAPE" then
			scrupp.exit()
		end
	end
}

Hosted by
SourceForge.net Logo

Valid XHTML 1.0!

Last modified Wed Feb 22, 2012