quicker way to draw images

Posted by Zimmy on Mon 11 Nov 2019 05:27 AM — 9 posts, 30,249 views.

#0
I have a miniwindow where I use DrawImage() to draw a bunch of little bmp images (directional arrows). I have around 50 of these to draw, although there are only 9 different bmps (everything is just a copy of one of these 9 bmps). The problem is it takes around .4 seconds to draw all the images, which is too slow. Is there a more efficient way to do this?
Australia Forum Administrator #1
Can you show your code please?

I can't suggest a more efficient way until I see your current way.
#2
-- this is how I load them
file_path = (GetPluginInfo (GetPluginID (), 6)):match("^(.*\).*$")

local directions = {"n", "ne", "e", "se", "s", "sw", "w", "nw"}
for _, v in ipairs(directions) do
WindowLoadImage (win, v, file_path.."arrows\"..v..".bmp")
end
------------------------------------------------------
--below are the functions I use that draw them

local function give_direction(exit)
if exit == "n" then return 0, 1 end
if exit == "ne" then return 1, 1 end
if exit == "e" then return 1, 0 end
if exit == "se" then return 1, -1 end
if exit == "s" then return 0, -1 end
if exit == "sw" then return -1, -1 end
if exit == "w" then return -1, 0 end
if exit == "nw" then return -1, 1 end
end

local function draw_exit(x_pos, y_pos, exit_dir, arrow_dir, exit_colour)
x_dir, y_dir = give_direction(exit_dir)
local exit_center_x = mw_buffer_x + (x_pos*block_x) - (block_x/2) + ((room_width + exit_width)/2)*x_dir
local exit_center_y = mw_buffer_y + (y_pos*block_y) + ((room_height + exit_height)/2)*-y_dir

local x1 = exit_center_x - exit_width/2
local y1 = exit_center_y - exit_height/2
local x2 = exit_center_x + exit_width/2
local y2 = exit_center_y + exit_height/2

if arrow_dir then WindowDrawImage (win, arrow_dir, x1+2, y1+2, x2-2, y2-2, 2) end

WindowCircleOp (win, 2,
x1, y1, x2, y2,
ColourNameToRGB(exit_colour), 0, 1,
ColourNameToRGB("black"), miniwin.brush_null)
end

-- I commented out the WindowDragImage line and the code ran literally 100x faster. I emulate mush on wine, perhaps that's why I'm having performance issues?
USA Global Moderator #3
Is it faster if you change the last 2 in your WindowDrawImage call to 1? You shouldn't be stretching the images.
Amended on Mon 11 Nov 2019 07:06 PM by Fiendish
#4
Fiendish said:

Is it faster if you change the last 2 in your WindowDrawImage call to 1? You shouldn't be stretching the images.


This is 10x faster. Is there a way to shrink the images when I load them so they'll be the right size? Even so, its still 10x slower than drawing no images. To be fair the images themselves are way bigger than they need to be.
USA Global Moderator #5
Quote:
To be fair the images themselves are way bigger than they need to be.

Yeah. So make them smaller.

Quote:
Is there a way to shrink the images when I load them so they'll be the right size?

Yes. First make a new hidden miniwindow with the final size you want, then draw the image to that secret miniwindow stretched to fill it, and then make a new image in your real miniwindow from the contents of the hidden miniwindow using WindowImageFromWindow.
Amended on Mon 11 Nov 2019 10:44 PM by Fiendish
#6
Oh, I see. So I do a separate secret mini window for each image I plan on reusing. And I only need to do this once?
Australia Forum Administrator #7
Surely the simple way is to use ImageMagick to resize your images in advance? Why load the client up with this work? Resizing images is slow (comparatively) and should only be done once if you can help it.
USA Global Moderator #8
Yurt said:

Oh, I see. So I do a separate secret mini window for each image I plan on reusing. And I only need to do this once?

You only need to do this once. You can use the same secret miniwindow for all of your images. The secret miniwindow just gives you a place to stretch into and then copy out of.

But Nick is right. If you're not dynamically changing the size of your images, you should probably just update the actual images themselves.