Advertisement

i think my code is haunted

Started by March 16, 2002 09:32 AM
5 comments, last by azaxaca 22 years, 11 months ago
i tried to write my own basecode but it doesnt work with all the error checking, i narrowed the problem down to the CreateWindow function not working, but there is nothing that could cause that.. if anyone could find the error, i would realy appreciate it (i removed all error checking because it makes the code look messy) (i also removed some code thats got nothing to do wit the problem) (oh and notice that i only use one hwnd as a global, so i dont need to put"hwnd=" infront of the WindowCreate function, because its already done inside the function.)
      
#//include stuff was here


#define WIDTH 640
#define HEIGHT 480
bool keys[256];


HWND hwnd=NULL;
HDC hdc=NULL;
HGLRC hrc=NULL;
RECT rect;


LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
bool fullscreen()
{}//there is nothing wrong wit this code, so its out

bool pixelformat()
{}//pixelformat code was here

void resize(GLsizei width,GLsizei height)
{}//resize code skipped

void GLinit()
{}//clear color, load identity stuff was here

void DeInit()
{}//the error happes way before this function is called(its only called once when program exits) so its got nothing to do wit the problem    




void WindowCreate(HINSTANCE hInst)
{
WNDCLASS wc;
memset(&wc,0,sizeof(wc));
wc.style=CS_VREDRAW|CS_HREDRAW;
wc.lpfnWndProc=WndProc;
wc.hInstance=hInst;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName="app";

if(!RegisterClass(&wc))msg("register class");

//fullscreen();

hwnd=CreateWindow("app","app",WS_POPUP|WS_CLIPSIBLINGS|WS_CLIPCHILDREN,0,0,WIDTH,HEIGHT,NULL,NULL,hInst,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
SetFocus(hwnd);
fullscreen();

}


   
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst, PSTR cmdline, int cmdshow)
{

WindowCreate();

hdc=GetDC(hwnd);

pixelformat();
hrc=wglCreateContext(hdc);
wglMakeCurrent(hdc,hrc);

resize(640,480);
GLinit();

int hori=0;
int verti=0;

 MSG msg;
    
 while(1)
 {
    if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
      if(keys[VK_ESCAPE])break;
    }
    else
    {
      if(keys[VK_LEFT ])hori --;
      if(keys[VK_RIGHT])hori ++;
      if(keys[VK_UP   ])verti++;
      if(keys[VK_DOWN ])verti--;
  
      gluLookAt(1,0,5,  hori,verti,0,  0,1,0);
      glColor3f(0.0,1.0,0.0);
      glBegin(GL_TRIANGLES);
       glVertex3f(0.0,1.0,0.0);
       glVertex3f(-1.0,0.0,0.0);
       glVertex3f(1.0,0.0,0.0);
      glEnd();
    }
 }
return(msg.wParam);
}




LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
int dfwp;
 switch(uMsg)
 {
    case WM_KEYDOWN:{
    keys[wParam]=TRUE;
    break;}

    case WM_KEYUP:{
    keys[wParam]=FALSE;
    break;}

    case WM_SIZE:{
    GetClientRect(hwnd,▭);
    resize(rect.top,rect.right);
    break;}

    case WM_DESTROY:{
    DeInit();
    break;}
    
    default:{dfwp=DefWindowProc(hWnd,uMsg,lParam,wParam);
    break;}
    
  }
    return dfwp;
}
      
Edited by - azaxaca on March 16, 2002 10:35:14 AM
..I still dont get the difference between c and c++...
It looks like you reversed the last two parameters to DefWindowProc.
Advertisement
no its in the right order, you can check the nehe tutorials if you want.
..I still dont get the difference between c and c++...
Well, I think that Nehe could make a mistake. If you look in the documentation (MSDN) you''ll see a prototype like this :
LRESULT DefWindowProc(
HWND hWnd, // handle to window
UINT Msg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);



thats whats on my code and on nehe''s tut wparam first then lparam
but thanks for the concern anyways
..I still dont get the difference between c and c++...
I assume by "CreateWindow function not working" you mean it returns NULL? What does GetLastError return if you call it right after CreateWindow?

Apparently, source box is not an edit box? When I try to copy the code from it it just gives me a single line, which is extremely annoying. Any ideas on how to get the code with line breaks?

And you should reverse wParam and lParam to DefWindowProc. This might be the cause of creation failure.

---visit #directxdev on afternet <- not just for directx, despite the name
Advertisement
get last error returns 1400 which is invalid windows handle" i looked it up, o and i never noticed the reversed lparam and wparam in DefWindowProc, i only looked at the WndProc and its prototype.
..Infact, i just reversed it and now it creates a window, thanks for noticing the error

now the only problem i got is that it doesnt show a triangle, instead it shows a messed up screenshot of my desktop.....
i''ll ask for some more help again if i cant fix it, its probably something with GLinit() ...

Thank you, i appreciate your help, i thought i''d never fix this.


..I still dont get the difference between c and c++...

This topic is closed to new replies.

Advertisement