Drawing while in love.update function
Posted: Sun Dec 01, 2013 9:38 pm
Hi,
I'm pretty new to Löve/programming in general and I have the following problem: I want to program a visualized quick-sort. Now, I already visualized insert-sort and bubble-sort, by doing this:
The G_StartSort is a variable which is true when a button is pressed (I use löveframes for that). The Time:hasDelay(G_Speed) is just an optional condition, if you want to slow the animation down. G_Stop is for pause.
So every time the G_SortFuncs:Step() is called, he does the following:
It's a step from the original insert-sort. I do this so I can draw the new array after every step. Here's the normal code of insert-sort for comparison:
So, after he is done doing the step, he draws the Array with this draw function:
The canvas is then drawn in love.draw():
Now, my problem is this "step-by-step" functions are pretty hard to do for sorting algorithms like quick-sort. What I would like to do is to just use the original sorting algorithm, and tell the engine to draw every time after a certain point in the algorithm. I tried to do this, but since the sort operation is called in the love.update function, the engine won't draw anything (As it should be -> separation of concerns). So does anyone have an idea what I could do to solve this problem, without doing this "step"-solution I came up with? Any help is appreciated!
Thanks!
I'm pretty new to Löve/programming in general and I have the following problem: I want to program a visualized quick-sort. Now, I already visualized insert-sort and bubble-sort, by doing this:
Code: Select all
function love.update(dt)
if G_StartSort then
if Time:hasDelay(G_Speed) and not G_Stop then
G_SortFuncs:Step() --G_SortFuncs is a function pointer to the sort function.
end
--print("drawing")
G_SortFuncs:Draw()
end
--Timer update
Time:update(dt)
--LoveFrames
loveframes.update(dt)
end
So every time the G_SortFuncs:Step() is called, he does the following:
Code: Select all
function InsertSort:Step()
Time:resetDelay()
if self.j <= #self.Array then
if self.i > 0 and self.Array[self.i] >= self.key then
self.Array[self.i+1] = self.Array[self.i]
self.Array[self.i] = 0 --This is not needed, but makes the animation look better
self.i = self.i - 1
else
self.Array[self.i+1] = self.key
self.j = self.j + 1
self.key = self.Array[self.j]
self.i = self.j - 1
end
else
InsertSort.isSorted = true
end
end
Code: Select all
function InsertSort(Array)
for j = 2, #Array do
key = Array[j]
i = j - 1
while i > 0 and Array[i] >= key do
Array[i+1] = Array[i]
i = i - 1
end
Array[i+1] = key
end
end
Code: Select all
function InsertSort:Draw()
canvas:clear()
local tempposx = self.posx
local tempposy = self.posy
for i = 1, #self.Array do
local height = self.stretch*self.Array[i]--*100/self.maxheight
canvas:renderTo(function()
love.graphics.rectangle("fill", tempposx, tempposy-height, self.width-1, height)
end)
tempposx = tempposx + self.width
end
end
Code: Select all
function love.draw()
love.graphics.draw(canvas)
--LoveFrames
loveframes.draw()
end
Thanks!