Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Smooth Graphics (Pixels) in C++ (Win32 Window)

I have a little Win32 C++ program that draws little particles orbiting a center object in 3D(or it looks like it at least).

However, I cannot seem to get it to run at 70 fps SMOOTHLY.

What should I do, let me know if I should post the code here.

The G

This is the main section of the code, I don't think it will run on it's own. I just made everything double because I don't THINK I need to worry about speed or space. (If I don't slow it down it runs so fast all you see is a bunch of flickering dots.)

[code]
//Initialize
// double CenterX = GetRand(1, 1000), CenterY = GetRand(1, 750), OrbitX = 20, OrbitY = 20;
double CenterX = (GetSystemMetrics(SM_CXSCREEN)/2), CenterY = (GetSystemMetrics(SM_CYSCREEN)/2);
double OrbitX = 0, OrbitY = 0;
double Gravity = 10, Frame = 0, Start = 0, Num = 1000, FrameLen = 0, Fps = 70;
double PX[1000], PY[1000], VX[1000], VY[1000];
char strFps[5+3+1];


for(int N = 1; N <= Num; N++)
{
PX[N] = GetRand(1, 32) + 32;
PY[N] = GetRand(1, 20) + 20;
VX[N] = GetRand(1, 10) - 5;
VY[N] = GetRand(1, 10) - 5;
}


//Run
HDC hMainDC = GetDC(hMainWnd);
double XDiff, YDiff;
double L;
int r, g, b;

GetClientRect(hMainWnd, &aRect);
SelectObject(hMainDC, Pen);
SetBkColor(hMainDC, RGB(0, 0, 0));
SetBkMode(hMainDC, OPAQUE);
SetTextColor(hMainDC, RGB(255, 255, 255));

for(;;)
{
for(N = 1; N <= Num; N++)
{
SetPixel(hMainDC, (int)(PX[N] + CenterX), (int)(PY[N] + enterY), RGB(0, 0, 0));

XDiff = OrbitX - PX[N];
YDiff = OrbitY - PY[N];

L = sqrt(((XDiff * XDiff) + (YDiff * YDiff)));
L = (L * Gravity);

if(L > 0)
{
VX[N] = VX[N] + XDiff / L;
VY[N] = VY[N] + YDiff / L;
PX[N] = PX[N] + VX[N];
PY[N] = PY[N] + VY[N];
}

switch(N & 15)
{
case 0: r = 0; g = 0; b = 0; break;
case 1: r = 255; g = 0; b = 0; break;
case 2: r = 0; g = 255; b = 0; break;
case 3: r = 0; g = 0; b = 255; break;
case 4: r = 255; g = 255; b = 0; break;
case 5: r = 255; g = 0; b = 255; break;
case 6: r = 0; g = 255; b = 255; break;
case 7: r = 255; g = 255; b = 255; break;
case 8: r = 128; g = 0; b = 0; break;
case 9: r = 0; g = 128; b = 0; break;
case 10: r = 0; g = 0; b = 128; break;
case 11: r = 128; g = 128; b = 0; break;
case 12: r = 128; g = 0; b = 128; break;
case 13: r = 0; g = 128; b = 128; break;
case 14: r = 128; g = 128; b = 128; break;
case 15: r = 255; g = 255; b = 128; break;
default: break;
}

SetPixel(hMainDC, (int)(PX[N] + CenterX), (int)(PY[N] + enterY), RGB(r, g, b));
}


FrameLen = CLOCKS_PER_SEC / Fps;
if(clock() >= Start + CLOCKS_PER_SEC)
{
//Print The Number Of Frames This Second.
strcpy(strFps, "FPS: ");
itoa((int)Frame, (strFps+5), 10);

DrawText(hMainDC, strFps, -1,
&aRect, DT_SINGLELINE);


Start = clock();
Frame = 0;
}
Frame += 1;

//Wait so that this runs at Fps frames per second.
if(clock() - Start < Start + (Frame * FrameLen))
{
//Pause until the second is over.
while(clock() < Start + (Frame * FrameLen)) { }
}

}

}
[/code]

[hr]
The name's Genesis, PhyloGenesis.

Comments

  • CroWCroW Member Posts: 348
    well,ivent read the whole code,but here some simple ways to improve performance:

    the most important thing is to draw to a memory-buffer without using any win32-api.'SelectObject' and friends take a lot of time.its better to make an array,alter it with own functions and copy the whole array to a bitmap which is put onto your window.as less win32-gdi-functions as better.dont create and destroy the bitmap each time,just update its data.im not sure,but i think win32-gdi is always limited by vsync,so you will have maximal 75 fps when using a display mode with 75hz.

    i think its a good idea to switch to directdraw or opengl which both support bitmap-graphics with backbuffering and are not limited to vsync (at cost of visual quality /tearing effect).

    directdraw (the last version is 7.0,afterwards MS stopped developing) is a good way for 2D-bitmap-graphics.its quite fast and easy to use.and it uses some build-in features of graphics-hardware like hardware-accelerated sprite drawing and backbuffering.
Sign In or Register to comment.