Jump to content
  • Advertisement
Sign in to follow this  
v0dKA

Graphics devices

This topic is 4241 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

How are graphics devices meant to be used: do you create a single graphics object and reuse it every time you need to redraw something, or do you keep creating, drawing with, and then destroying the device every time? The scenario is simple: I'm creating a drawing application where the user can draw lines using the mouse. I create a "rubberband line" while the user has the mouse button down: that is, I show a ghost line while the user drags the mouse but has not decided where to place it yet. This rubberband line needs to be redrawn every time the user moves the mouse, and the old line (from the last frame) has to be removed (just how, I am not yet sure). I'm using C#.NET, though I think this may apply to other languages as well. Thanks in advance for any help.

Share this post


Link to post
Share on other sites
Advertisement
With something like GDI, you need to draw the line something like:
line1(position n) by XOR with the frame
line2(position n) by XOR with the frame
line3(position n+1) by XOR.....
....

The XOR allows you to preserve what is under the line while still making it look different.

With something like OpenGL, you just draw the new line. When you swap buffers, the old line is gone.

Share this post


Link to post
Share on other sites
Not sure if this fully answers your question/is helpful at all, but I was working on a drawing program sometime back and what I can say is that it depends on what you're drawing and how. If you're drawing the 'rubberband line' with a pen that just inverts pixels, you can simply redraw with the inverting pen (and mouse x,y coordinates you have saved before, as opposed to the new mouse x,y) to erase the old line before drawing the new. My solution otherwise was to save a bitmap of the drawing area in a DC before the line was first drawn, then restoring it everytime the old line needed to be drawn over and drawing the new line overtop.

Share this post


Link to post
Share on other sites
Thanks, I think I understand what you mean. In fact, while searching, I found just what I need to accomplish this in C#:
ControlPaint.DrawReversibleLine().

But I was also referring to to whether or not I should re-create a GDI device for every frame. In pseudo-code, which of the following is a better solution?


#1
======

class MyApplication
{
GDIDevice device;

MyApplication()
{
device = new GDIDevice( ... );
}

onPaint()
{
device.paint( something );
}

~MyApplication()
{
device.destroy();
}
};




#2
=======

class MyApplication
{
onPaint()
{
GDIDevice device = new GDIDevice(...);
device.paint( something );
device.destroy();
}
};



Should I create a new device, along with a new set of pens/brushes/etc every time I need to repaint, or just reuse one?

Share this post


Link to post
Share on other sites
In general, device contexts, which I guess might be old terminology for graphics devices, are created in response to a paint message (which would be the reason for calling onPaint()) to do their job, then immediately destroyed. I think this goes back to the earlier days of Windows when device contexts and other GDI objects were limited resources, and hence you couldn't have too many existing at a time.
If your application is memory limited then it would be a good idea to have the graphics device around only when you need it. If it is time limited it would be better to not be continually recreating them. Other than that I'm not sure it makes a lot of difference (I've used both approaches with no ill effects).

Share this post


Link to post
Share on other sites
Quote:
Original post by rsood
In general, device contexts, which I guess might be old terminology for graphics devices, are created in response to a paint message (which would be the reason for calling onPaint()) to do their job, then immediately destroyed. I think this goes back to the earlier days of Windows when device contexts and other GDI objects were limited resources, and hence you couldn't have too many existing at a time.
If your application is memory limited then it would be a good idea to have the graphics device around only when you need it. If it is time limited it would be better to not be continually recreating them. Other than that I'm not sure it makes a lot of difference (I've used both approaches with no ill effects).


OK, thank you for your advice.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!