Page 1 of 1

Simple Image Lib (lImage)

Posted: Fri Apr 03, 2009 10:36 am
by Gerrit
Hi there.

Just wanted to share a lib of mine which I use in my project to use the x & y coordinates as the upper left point of the image (as it should be). Shouldn't be needed in Löve 0.6 but until then have fun using it.

Usage:

Code: Select all

function load()
love.graphics.setMode( 640, 480, false, true, 0 )

--Include the lib in your project
require("lImage.lua");

--Create a new lImage
myImage = lImage:new ( "circle.png", 200, 200 )

end


function draw()

--Draw the image
myImage:draw()

end

Functions:

Code: Select all

new( image_url)                     Create a new lImage
new( image_url, x, y )              Create a new lImage and set the x & y coordinates
new( image_url, x, y, angle)        Create a new lImage with coordinates x & y and angle in degrees
getX()                              Returns the images x coordinate
getY()                              Returns the images y coordinate
getPos()                            Returns both x & y
getCenter()                         Returns the x & y coordinates of the images center
getHeight()                         Returns the images height
getWidth()                          Returns the images width
getAngle()                          Returns the angle in degrees (default = 0)
getScale()                          Returns the scale (default = nil)
getScaleX()                         Returns the scale x-axis coordinate (default = nil)
getScaleY()                         Returns the scale y-axis coordinate (defaule = nil)
setX( x )                           Set the images x coordinate
setY( y )                           Set the images y coordinate
setPos( x, y )                      Set both coordinates at the same time
setAngle( angle )                   Set the angle
setCenter( x, y)                    Set the x & y coordinates of the images center
setScale( scale )                   Set the scale
setScaleX( sx )                     Set the scale x-axis coordinate
setScaleY( sy )                     Set the scale y-axis coordinate
draw()                              Draw the image to the screen with the settings applied
                                    - The x & y coordinates represent the upper left point of the image
                                    - If the angle is not NIL it will be drawn with the angle
                                    - If the scale is not NIL it will be drawn scaled
                                    - If the scale_x & scale_y are not NIL it will be drawn scaled
draw( x ,y )                        Same as draw() with manual set x & y coordinates which will be saved
draw( x, y, angle )                 Same as draw( x, y) with manual set angle which will also be saved
drawc()                             Same as draw() except it uses the x & y coordinates as center (default in Löve 0.5)
drawc( x, y )                       Same as drawc() with manual set x & y coordinates which will be saved
drawc( x, y, angle )                Same as drawc( x, y ) with manual set angle which will also be saved
Changelog

Code: Select all

lImage v2.1
 * Added "setAngle( angle )"
 * Added download in compiled bytecode to save you some time
 * Fixed "getAngle()", it wasn't there. Just forgot it somehow..

lImage v2
 * Added "draw( x ,y )"
 * Added "draw( x, y, angle )"
 * Added "drawc( x, y )"
 * Added "drawc( x, y, angle)"

lImage v1
* Initial release

License

Code: Select all

THE POETIC LICENSE — Alexander Genaud

© 2009 Gerrit Guenther

This work ‘as-is’ we provide.
No warranty express or implied.
We’ve done our best,
to debug and test.
Liability for damages denied.

Permission is granted hereby,
to copy, share, and modify.
Use as is fit,
free or for profit.
These rights, on this notice, rely.

http://genaud.net/2005/10/poetic-license/
Please write if anything doesn't work as expected. Should be O.k. though :)

PS: Updated to v2.1!

Re: Simple Image Lib (lImage)

Posted: Fri Apr 03, 2009 5:38 pm
by mikembley
I like it!

Makes things simpler for me, Although a thought on setting the X and Y of an image, why not extend the draw function to be able to set it via that

For example:

Code: Select all

image:draw(100,200)
As it would reduce the need to do:

Code: Select all

image:setX(100)
image:setY(200)
image:draw()

Re: Simple Image Lib (lImage)

Posted: Fri Apr 03, 2009 9:34 pm
by Gerrit
mikembley wrote:I like it!

Makes things simpler for me, Although a thought on setting the X and Y of an image, why not extend the draw function to be able to set it via that

For example:

Code: Select all

image:draw(100,200)
As it would reduce the need to do:

Code: Select all

image:setX(100)
image:setY(200)
image:draw()
How about

Code: Select all

image:setPos(100, 200)
image:draw()
Makes just 1 more line :) You can also do

Code: Select all

love.graphics.draw( image.image, image.center_x, image.center_y )
;)

But it heard you and updated the lib to v2. Now it includes draw(x, y); draw(x, y, angle), drawc(x, y) and drawc(x, y, angle) which should make us all happy :) If someone else want more options in the draw() function like the scale: You're free to expand it yourself. Shouldn't be that hard as it's pretty simple code. New code and function list in first post.

PS: Nicest thing in v2 is that I added the stuff you needed and the source grew only by 2 lines. But that's because I rewrote some stuff to make it shorter.

Re: Simple Image Lib (lImage)

Posted: Mon Apr 06, 2009 12:53 pm
by Gerrit
Updated to v2.1. It seems I forgot to write getAngle() and setAngle(). The first one was mentioned in the function list but it wasn't there :) So, now it should be complete. If you need more functions watch out. I'll release a new, advanced lib, loosely based on this one, in the next couple of days.

Re: Simple Image Lib (lImage)

Posted: Tue Apr 07, 2009 10:47 pm
by AirPump
I think you should add a function to change drawing order of sprites. Quite frankly, this would be much easier than putting them into a table and foreaching them.

Re: Simple Image Lib (lImage)

Posted: Tue Apr 07, 2009 11:36 pm
by Gerrit
AirPump wrote:I think you should add a function to change drawing order of sprites. Quite frankly, this would be much easier than putting them into a table and foreaching them.
Well these objects aren't stored together in a table. This is just a shorter way of using love.graphics. You only draw the object your currently using. Like:

NewImage = lImage:new( "test.jpg", 200, 300 )

Creates an image object "NewImage" with the image "test.jpg" and x = 200, y = 300 as coordinates. In your applications draw() function you can now simply write NewImage:draw() to draw your image to the screen.

If you'd like to store multiple instances into one big table you could expand the lImage object with a field "drawingorder" and put all of your objects into a new table and draw it entirely sorted by your drawingorder. I'm currently writing an expanded version of this lib which should cover the drawingorder and a table with all images :) But this will take some more days..