Graphics devices

Started by
4 comments, last by v0dKA 17 years, 6 months ago
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.
.:<<-v0d[KA]->>:.
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.

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.
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?
.:<<-v0d[KA]->>:.
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).
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.
.:<<-v0d[KA]->>:.

This topic is closed to new replies.

Advertisement