OpenGL slow speed ??!!
i am a beginner in OpenGL programming.
as i am working on my project(still not finished) ...i have encountered a problem...
here is the source code..
*************************CODE******************************************
#include<windows.h>
#include<stdlib.h>
#include<gl/gl.h>
#include<gl/glu.h>
#include<gl/glaux.h>
HDC ghdc;
float q=.1;
float x=0;
float w=-.1;
int bull=0;
int ubull=0;
int fullscreen=1;
int WIDTH=1024;
int HEIGHT=768;
float j=-.8;
bool countflag=true;
struct sqr
{
bool alive;
float k,l;
float centre;
}a;
void updatecentre()
{
a.l-=.05;
}
void getcount()
{
if(a.alive==false)
{
a.k=(rand()%90)/100;
a.l=.99;
a.alive=true;
a.centre=a.k;
}
}
void drawsqr()
{
glBegin(GL_POLYGON);
glVertex3f(a.k,a.l-.1,0);
glVertex3f(a.k+.2,a.l-.1,0);
glVertex3f(a.k+.2,a.l,0);
glVertex3f(a.k,a.l,0);
glEnd();
}
void DrawBull()
{
glColor3f(0,0,1);
glEnable(GL_POINT_SMOOTH);
glPointSize(7);
glBegin(GL_POINTS);
glVertex3f(x,-.8,0);
glEnd();
bull=0;
ubull=1;
}
void UpdateBull()
{
j+=.05;
if(j<=1)
{
glColor3f(0,0,1);
glPointSize(7);
glBegin(GL_POINTS);
glVertex3f(x,j,0);
glEnd();
}
else
{
ubull=0;
}
}
void DrawMyScene(float q,float x,float w)
{
int i;;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor3f(1,0,0);
glEnable(GL_POLYGON_SMOOTH);
glBegin(GL_TRIANGLES);
glVertex3f(q,-.99,0);
glVertex3f(x,-.8,0);
glVertex3f(w,-.99,0);
glEnd();
if(countflag==true)
{
a.alive=false;
countflag=false;
}
for(i=0;i<2;i++)
{
getcount();
}
drawsqr();
updatecentre();
if(bull==1)
{
DrawBull();
}
if(ubull==1)
{
UpdateBull();
}
SwapBuffers(ghdc);
}
void mypfd(HDC hdc)
{
int why;
static PIXELFORMATDESCRIPTOR pfd={sizeof(PIXELFORMATDESCRIPTOR),1,PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,PFD_TYPE_RGBA,32,0,0,0,0,0,0,0,0,0,0,0,0,0,16,0,0,PFD_MAIN_PLANE,0,0,0,0};
why=ChoosePixelFormat(hdc,&pfd);
SetPixelFormat(hdc,why,&pfd);
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
static HDC hdc;
static HGLRC hrc;
int wi,he;
switch(msg)
{
case WM_CREATE:
hdc=GetDC(hwnd);
ghdc=hdc;
mypfd(hdc);
hrc=wglCreateContext(hdc);
wglMakeCurrent(hdc,hrc);
return 0;
break;
case WM_QUIT:
case WM_CLOSE:
wglMakeCurrent(hdc,NULL);
wglDeleteContext(hrc);
PostQuitMessage(0);
return 0;
break;
case WM_SIZE:
he=HIWORD(lParam);
wi=LOWORD(lParam);
glViewport(0,0,wi,he);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
return 0;
break;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR CmdLine,int nShowCmd)
{
WNDCLASSEX wc;
wc.cbClsExtra=0;
wc.cbSize=sizeof(WNDCLASSEX);
wc.cbWndExtra=0;
wc.hbrBackground=0;
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hIcon=0;
wc.hIconSm=0;
wc.hInstance=hInstance;
wc.lpfnWndProc=(WNDPROC)WndProc;
wc.lpszClassName="game";
wc.lpszMenuName=0;
wc.style=CS_VREDRAW|CS_HREDRAW;
if(!RegisterClassEx(&wc))
{
MessageBox(NULL,"ERROR:Register","ERROR REPORT",NULL);
return 0;
}
if(fullscreen)
{
DEVMODE sc;
memset(&sc,0,sizeof(sc));
sc.dmPelsWidth=WIDTH;
sc.dmPelsHeight=HEIGHT;
sc.dmSize=sizeof(sc);
sc.dmBitsPerPel=32;
sc.dmFields=DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
if (ChangeDisplaySettings(&sc,CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
{
if (MessageBox(NULL, "Cannot run fullscreen..play normally?","ERROR",MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
{
fullscreen =0;
}
else
{
return 0;
}
}
}
DWORD dwExstyle;
DWORD dwstyle;
if (fullscreen)
{
dwExstyle = WS_EX_APPWINDOW;
dwstyle = WS_POPUP;
ShowCursor(FALSE);
}
else
{
dwExstyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwstyle = WS_OVERLAPPEDWINDOW;
}
RECT rect;
rect.left = 0;
rect.right = (LONG) WIDTH;
rect.top = 0;
rect.bottom = (LONG) HEIGHT;
AdjustWindowRectEx(&rect, dwstyle, FALSE, dwExstyle);
HWND hwnd;
hwnd=CreateWindowEx(dwExstyle,"game","GAME",dwstyle|WS_CLIPCHILDREN|WS_CLIPSIBLINGS,0,0,rect.right - rect.left,rect.bottom - rect.top,0,0,hInstance,0);
if(!hwnd)
{
MessageBox(NULL,"ERROR:Creating.....","ERROR REPORT",NULL);
return 0;
}
ShowWindow(hwnd,SW_SHOW);
UpdateWindow(hwnd);
MSG msg;
bool flag=false;
while(!flag)
{
if(PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE))
{
if(msg.message==WM_QUIT)
flag=true;
else
{
if(GetAsyncKeyState(VK_LEFT) & 0x8000)
{
q-=.05;
x-=.05;
w-=.05;
}
if(GetAsyncKeyState(VK_RIGHT) & 0x8000)
{
q+=.05;
x+=.05;
w+=.05;
}
if(GetAsyncKeyState(VK_SPACE) & 0x8000)
bull=1;
DrawMyScene(q,x,w);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
return msg.wParam;
}
*****************************CODE**************************************
when i run it..the SQUARE(falling body) and the POINT(working as the bullet) which are to be updated..they r very slow..
but if i move the mouse..it becomes normal(i mean fast)..although there is no mouse interfacing used...
i also cant run the program in full screen..
i dont know why is happening..
plz run the program ..and if u see the same problem,plz help me..
it will b appreciated :)
Your DrawMyScene() function is called inside the PeekMessage() if, so your scene is only drawn when there are messages to process. Try moving the function just after the closing curly bracket of the PeekMessage() if like this:
Hope that helps!
Edit: You can put your source code between [ source ] and [ /source ] tags (without the spaces between the bracket and the "source") this will put the code in a box like above!
if(PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE)){ // DrawMyScene(q,x,w); <--- remove the draw my scene from here}DrawMyScene(q,x,w); // <--- and put it here
Hope that helps!
Edit: You can put your source code between [ source ] and [ /source ] tags (without the spaces between the bracket and the "source") this will put the code in a box like above!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement