drawing one image multiple times on the screen

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
michielewiel
Prole
Posts: 3
Joined: Tue Aug 20, 2013 7:50 am

drawing one image multiple times on the screen

Post by michielewiel »

Hi, my name is micheal thanks for reading my problem and i appoligize bothering your time.
Well i have a bit of a problem i'm basicly making a program like paint . so evrything is setup basic colors etc.
But now i ran in to some kind of trouble i want to draw to the screen multiple copies of the same color (you can't have a picture with only one red dot. i need more red dots) so i tried making an array of red dots an draw them all one per one that didn't work out i'm puzzeled i'm kinda new to this engine and it's amazing only i can't see how to overcome this problem. i uploaded the .love file on sendspace here's the link http://www.sendspace.com/file/xj9yit also here is the code ( sorry it's a bit messy

Code: Select all

local i
local volume
local cursor
local font
local colors= {"blue", "red", "yellow", "brown", "black", "white"}
local paul
local k
local red={}
local xcol=
local ycol
function love.load()
k = 1
volume = 0.0
paul = black
bgm = love.audio.newSource("/music/jazz.ogg", "stream")
love.audio.play(bgm)
cursor = love.graphics.newImage("/images/cursor.png")
love.mouse.setVisible(false)
font = love.graphics.newFont("/fonts/font.ttf", 20)
i=1
while k < 101 do
red[k] =love.graphics.newImage("/colors/red.png")
k = k+1
end
end

function riet()
function love.draw()
love.graphics.setFont(font)
local foo, boo = love.mouse.getPosition( )
love.graphics.printf(foo.." "..boo,0,70,500, "left")
love.graphics.draw(cursor,foo,boo)
love.graphics.draw(red[i],xcol[i],ycol[i])
i=i+1
end
end

function love.update(dt)
mich()
love.audio.setVolume(volume)
end

function love.draw()
love.graphics.setFont(font)
local foo, boo = love.mouse.getPosition( )
love.graphics.printf(foo.." "..boo,0,70,500, "left")
love.graphics.draw(cursor,foo,boo)
end

function mich()
function love.keypressed( key, unicode )
if key == "1" then
paul= blue
end
if key == "2" then
paul = red
end
if key == "3" then
paul= yellow
end
if key == "4" then
paul = brown
end
if key == "5" then
paul = black
end
if key == "6" then
paul = white
end
if key == "f" then
love.graphics.toggleFullscreen()
end
if key == "m" then
love.audio.stop()
end
if key == "kp+" then
volume = volume + 0.01
end
if key == "kp-" then
volume = volume -0.01
end
if volume > 1.0 then
volume = 1.0
end
if volume < 0 then
volume = 0
end
end
if love.mouse.isDown("r") then
text2= "you pressed the right mouse button"
end
if love.mouse.isDown("l") then
end
function love.mousereleased(x, y, button)
   if button == "l" then
   local foo, boo = love.mouse.getPosition( )
xcol=foo
ycol=boo
riet()
end
end
end
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: drawing one image multiple times on the screen

Post by raidho36 »

Well instead of storing every single pixel to an array, I really suggest you drawing your activity onto the offscreen surface (canvas), and then display the canvas to the screen.

I also really suggest you reading Programming in Lua book through (it's available online for free), since this question is about something as basic as loops. To do same thing multiple times you would simly use a loop. Oddly enough, there is already loops in your code, so I don't know how comes you're asking this question. Your code is really messy overall so I can hardly understand what's going on, so I just again suggest you reading the PiL book.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: drawing one image multiple times on the screen

Post by Plu »

You really shouldn't load an image more than once. You can simply load the image using love.graphics.newImage and then re-use that variable every time you draw the item:

Code: Select all

image = love.graphics.newImage( 'some_image.png' )
for i = 1, 10 do
  love.graphics.draw( image, some_x, some_y )
end
Otherwise you will use up huge amounts of memory for nothing.

Also, you will need to store actual X/Y coordinates in xcol and ycol, otherwise all the pixels will be drawn in the top left of the screen (0,0) and you won't see them.
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: drawing one image multiple times on the screen

Post by substitute541 »

raidho36 wrote:Well instead of storing every single pixel to an array, I really suggest you drawing your activity onto the offscreen surface (canvas), and then display the canvas to the screen.
Or transform screen coordinates to image pixel coordinates and use that to draw on an ImageData?

Also, you might want to read this michiel: http://lua-users.org/wiki/LuaStyleGuide
Currently designing themes for WordPress.

Sometimes lurks around the forum.
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests