Not logged inGosu Forums
Forum back to Help Search Register Login
Up Topic Gosu / Gosu Exchange / draw_line y coordinate off by one?
- - By psylem Date 2011-10-05 14:42
Hi folks, the following lines of code produce unexpected results. Is it just me?

draw_line(0,0, Gosu::Color::YELLOW, 0,10, Gosu::Color::YELLOW, 999)
draw_line(0,0, Gosu::Color::YELLOW, 10,0, Gosu::Color::YELLOW, 999)

This should produce two lines from the top left of my window, one vertical and one horizontal. The horizontal line is missing until I do this...

draw_line(0,1, Gosu::Color::YELLOW, 0,11, Gosu::Color::YELLOW, 999)
draw_line(0,1, Gosu::Color::YELLOW, 10,1, Gosu::Color::YELLOW, 999)

I'm using whatever comes with OSX when you do all that gem jazz.

ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
Parent - By lol_o2 Date 2011-10-05 18:08
Checked your code and I have the same thing, but with vertical line.
Parent - By erisdiscord Date 2011-10-06 01:23
It's an unfortunate OpenGL bug, or implementation detail, or something. I think Julian has mentioned before that OpenGL doesn't really have a good, consistent way to draw a line onscreen. On some platforms, the lines will be drawn in exactly the right place; on others, they will be off by one in either direction. If you can use pre-made images of lines instead, you'll be better off. :C
Parent - - By Spooner Date 2011-10-06 02:11
What I do for this is create a 1x1 pixel white image and #draw that coloured and scaled to render orthogonal lines, rather than using #draw_line. This ensures that the lines are always pixel-perfect and will be drawn consistently in situations like this (especially true when you are trying to draw rectangular frames).
Parent - By erisdiscord Date 2011-10-06 04:01
+1 This is what I was trying to suggest, but expressed more clearly. C:
Parent - - By psylem Date 2011-10-09 14:38
Thanks for the workaround, I can live with this since I'm only using this function currently for debugging collision detection. I'd suggest noting the issue and workaround in the documentation...

On a related point, can anyone think of a way to get the colour value of a pixel? Perhaps then we could test for glitchy behavior on start up and use an offset based on results. Then draw_line wouldn't be so useless, and I'm guessing it's more efficient than the 1x1 image workaround. How can I find out more about the function?
Parent - - By jlnr (dev) Date 2011-10-09 15:26
I made a note to add it to the docs ASAP.

You can take a look at examples/OpenGLIntegration.rb to see how gl can be used. But I will actually replace draw_line by a replacement that uses a 1x1px image, so that is not a good plan for the long term. And in the short term, I am not convinced that there is a speed difference at all :)
Parent - By Spooner Date 2011-10-09 17:11
Yeah, using images is unlikely to be slower and trying to hack the current #draw_line to work would be orders of magnitude slower (the error offset is not consistent across the screen!).
Parent - - By psylem Date 2011-10-10 00:20
Cool, I was just guessing. I've not peered under the hood to see how Gosu works yet.

BTW Spooner, I seem to get consistent results across the screen. When I make a wrapper method that just ups the y coordinate by one it works perfectly. I assumed it was consistently off by one (platform specifically or something). But if your suggesting that's not the case, I'll forget about hacking it.
Parent - By Spooner Date 2011-10-11 00:10
I mainly noticed this effect when drawing rectangular boxes. Some boxes would be drawn correctly, but other ones, at other positions on the screen, would have one or both the sides off by one. It seemed to be dependent on position on the screen and also seemed to be consistent across runs of the game. I'm on win7x64 using an NVidia card.
Up Topic Gosu / Gosu Exchange / draw_line y coordinate off by one?

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill