Advertisement

Slow, sloppy GDI

Started by February 26, 2000 05:17 AM
4 comments, last by BeanDog 24 years, 6 months ago
There must be a faster and better way to draw primitives onto a DDraw surface than through Windows GDI functions. I have to call the GetDC, create a pen with the needed color, and draw the primitive. This slows to a crawl with only a few dozen primitives per frame, and sometimes the shape decides to be a different color than I told the pen to be. What seems to be the trouble with the following code: void DrawEllipse(LPDIRECTDRAWSURFACE7 &Surface, int x1, int y1, int x2, int y2, COLORREF col, bool fill) { HDC hdc; HPEN pen; if (Surface->GetDC(&hdc) == DD_OK) { pen = CreatePen(PS_SOLID, 0, col); SelectObject(hdc, pen); if (fill) Ellipse(hdc, x1, y1, x2, y2); if (!fill) Arc(hdc, x1, y1, x2, y2, 1, 1, 1, 1); Surface->ReleaseDC(hdc); } } You pass in the COLORREF col, and a pen is created using that COLORREF. It will work for a few seconds, but then my colors will start to all become black. WHY? Thanks again, Bean Dog Simple Versioning System: Source code control for the common man. [Edited by - BeanDog on January 15, 2006 12:50:12 PM]
Well i don''t know why does colors go black (I have the same problem), but I think that the faster way is to lock surface and than draw on it with your own functions...
Advertisement
Is there a good group of pre-made primitive functions for DDSurfaces out there so I don''t have to go into writing my own?
The ClanLib wrapper has a small set of primitives. I think CDX does too. But I don''t think anyone''s release a full-set of primitives, per se.
I''m not sure if this will give you faster fps, but it might fix the color. I remember having a similar problem.

You don''t delete the pen you create. You must do that otherwise you get memoryleaks. Use DeleteObject(pen) to do this. Make sure that you''ve restored the original pen in the DC before deleting the pen though, otherwise GDI gets real angry with you, and might even crash your program.
If you want to do your own primitive functions there''s plenty of info online about line & polygon drawing algorithms (do a search for Bresenham). For stuff like circles, ellipses, and arcs, you''ll have to look harder.

The CDX library uses code based on some of Michael Abrash''s algortithms for some of it''s primitives (I know it handles circles, not sure about ellipses). So check out a bunch of free libs to see what you can find.

If that fails, pick up a computer graphics book. I grabbed a copy of ''Computer Graphics'' by Hearn & Baker a few months back and it has some good, speedy algortihms for various primitives. If you run into a brick wall, email me and I''ll see what I can dig up.

This topic is closed to new replies.

Advertisement