Jump to content
  • Advertisement
Sign in to follow this  
boontje

Using GDI to draw to surface

This topic is 4165 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am working on GUI elements for my little engine. Now, I thought it would be a good idea to use GDI functions(Rectangle, RoundRect, etc etc) to draw things like buttons, panels, borders etc etc. I have this working, but I was wondering if it is the right way to go. What I do is this: Create a texture Get surface level Surface->GetDC() Do GDI stuff(Rectangle, RoundRect etc) Surface->ReleaseDC() and for rendering the texture I use a sprite. As said, this works. But my guts is telling me I am making a mistake, but I dont know why/what/how. So, can I continue using this method? Or should I use another method to do some 2D drawing for GUI purposes? Cheers!

Share this post


Link to post
Share on other sites
Advertisement
It depends how often you're doing this. I'd avoid doing it every frame as much as possible. Using GDI stuff is going to be pretty slow, and you might be better off implementing your own functions which operate on a locked texture.

Another problem with the GDI functions is that they only work when the texture is in certain formats (See the docs for GetDC() for a list), so if you support different texture formats, this could be a problem.

Share this post


Link to post
Share on other sites
Well I'm 100% sure it's faster to do pure DirectX drawing and leave the GDI be. For example I use a total of 9 sprites for drawing windows, 4 for corners 4 for sides and 1 for the inner space, check out the link:

http://picasaweb.google.com/m.flayer/TheD20project

GDI has VERY slow line-drawing functions (and by slow i mean you can achieve 10-100 the speed by coding it yourself). if you really MUST draw lines/rectangles, you'd better use the lock-draw pixels-unlock method.

In the above link you'll see some screenshots from my current project. I must confess at this point i use GDI for font drawing purposes (until i get to writing my own font engine), but that's where it stops. Rest of the drawing is done using sprites.

One of the screenshots has around 30-40 visible windows. Using the sprite method (an advanced version would consider the overlapping of sprites and wouldnt draw the hidden ones) you are way faster than when using GDI drawing methods. If you want we can do a comparison =P

EDIT: Damn, I was too slow =)

Share this post


Link to post
Share on other sites
Well, ok... GDI is slow! No need for doing a comparison, I'm an easy to convince person ;)

However, GUI textures dont change much. In the initialize stage of the app textures can be created and drawn too. So, instead of rendering 9 sprites I will only have 1 sprite. And by generating the textures in the initialize stage I can easy change the look of the GUI.

So filling the textures each frame with GDI is not necassery, therefore performance should'nt be a problem. However, I will look into creating my own drawing methods, and maybey we can do a comparison then :P

Thanks for the quick and usefull replies!

Share this post


Link to post
Share on other sites
In case you really decide to start drawing lines, i suggest you do some research on line drawing algorithms. There are some which are really fast, from the standard slope-intercept to the famous Bresenham algorithm. Well if you want to go hardcore, try the two step Xaolin Wu (not sure if i spell it right) algorithm, it just rocks.

Good Luck.

Concerning the nine sprites... better speed often means more memory usage (e.g. lookup tables). That's why I am willing to sacrifice 36kb (9 sprites, each 32x32, 32bpp) for the sake of high fps rate (which i yet have to measure o_O )

Share this post


Link to post
Share on other sites
"Do it right, or dont do it at all"

So, lets do some hardcore stuff :P

You almost spelled it right: Xiaolin Wu. Found it( http://en.wikipedia.org/wiki/Xiaolin_Wu's_line_algorithm ), and going to implement it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!