Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Drawing many rectangles
- - By Askr Date 2013-07-11 14:24
Hey there,

I'm fairly new to Gosu and Ruby, but I already gathered some experience in the C-Family, Python and Java.
Right now I'm playing around a bit, trying to hack a small Roguelike together using Gosu and Ruby 1.9.

The current problem I'm experiencing is, that my program is slowing down very fast when I try to draw a map using many rectangles. The code is as follows:

    for i in 0..@map.width-1
      for j in 0..@map.height-1
        draw_quad(x, y, 0xffffffff, x+width, y, 0xffffffff, x, y+height, 0xffffffff, x+width, y+height, 0xffffffff, 0)
      end
    end

Unncessery bits removed.

So is there some kind of way of drawing the rectangle only once, then refering to it and just "blit" it to the screen or something?

Many thanks in advance. :)
Parent - - By lol_o2 Date 2013-07-11 18:15
On the beginning

rectangle = window.record do
   your drawing code...
end


where 'window' is your Gosu::Window instance
and then

rectangle.draw(x,y,z)
Parent - By Askr Date 2013-07-12 06:50
Thank you good sir, this works like a charm. :)
Parent - - By jlnr (dev) Date 2013-07-11 22:44
What lol_o2 said :) FWIW, draw_quad is not the problem, but all the Ruby code around it (loops, calculations).

Ruby mini hint: (one dot more) is "up to but not including", so you could rewrite the for loops as for i in 0…@map.width. Or you could use @map.width.times do |x|….
Parent - By Askr Date 2013-07-12 06:50
Thanks, I didn't know that. :) Ruby is full of little surprises like this.
Up Topic Gosu / Gosu Exchange / Drawing many rectangles

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill