#include #include #include /* Declare Windows procedure */ LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); /* Make the class name into a global variable */ char szClassName[ ] = "WindowsApp"; HWND hMainWnd; /* This is the handle for our window */ HWND hControl; // fenètre de controle HWND hOGL; // fenètre OpenGL HDC MainDC; HGLRC MainHRC; RECT MainRect,PctRect; HINSTANCE hMainInstance; GLfloat light_pos[4] = { 0.0, -100.0, 4.0, 0.0 }; const int COLOR_DEPTH = 16; int angle = 0; void draw(); void createOpenGLWindow(HINSTANCE hInstance); void createControlWindow(HINSTANCE hInstance); bool InitOpenGL(); bool MySetupPixelFormat(HDC); void SizeOpenGL(int,int); void UnInit(); int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow) { MSG messages; /* Here messages to the application are saved */ WNDCLASSEX wincl; /* Data structure for the windowclass */ /* The Window structure */ wincl.hInstance = hThisInstance; wincl.lpszClassName = szClassName; wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */ wincl.style = CS_DBLCLKS;// | CS_HREDRAW | CS_VREDRAW | CS_OWNDC; /* Catch double-clicks */ wincl.cbSize = sizeof (WNDCLASSEX); /* Use default icon and mouse-pointer */ wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); wincl.hCursor = LoadCursor (NULL, IDC_ARROW); wincl.lpszMenuName = NULL; /* No menu */ wincl.cbClsExtra = 0; /* No extra bytes after the window class */ wincl.cbWndExtra = 0; /* structure or the window instance */ /* Use Windows's default color as the background of the window */ wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; /* Register the window class, and if it fails quit the program */ if (!RegisterClassEx (&wincl)) return 0; /* The class is registered, let's create the program*/ hMainWnd = CreateWindowEx ( 0, /* Extended possibilites for variation */ szClassName, /* Classname */ "Windows App", /* Title Text */ WS_OVERLAPPEDWINDOW, /* default window */ CW_USEDEFAULT, /* Windows decides the position */ CW_USEDEFAULT, /* where the window ends up on the screen */ 544, /* The programs width */ 375, /* and height in pixels */ HWND_DESKTOP, /* The window is a child-window to desktop */ NULL, /* No menu */ hThisInstance, /* Program Instance handler */ NULL /* No Window Creation data */ ); hMainInstance = hThisInstance; createOpenGLWindow(hMainInstance); createControlWindow(hThisInstance); /* Make the window visible on the screen */ ShowWindow (hMainWnd, nCmdShow); /* Run the message loop. It will run until GetMessage() returns 0 */ //while /*(PeekMessage(&messages,NULL,0,0,PM_REMOVE))//*/(GetMessage (&messages, NULL, 0, 0)) //{ /* Translate virtual-key messages into character messages */ //TranslateMessage(&messages); /* Send message to WindowProcedure */ //DispatchMessage(&messages); //} while( true ) { if( PeekMessage(&messages, NULL, 0, 0, PM_REMOVE) ) { if(messages.message == WM_QUIT) break; TranslateMessage(&messages); DispatchMessage(&messages); } else{ // affichage des images, calcul des positions … } } /* The program return-value is 0 - The value that PostQuitMessage() gave */ return messages.wParam; } /* This function is called by the Windows function DispatchMessage() */ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; switch (message) /* handle the messages */ { case WM_DESTROY: PostQuitMessage (0); /* send a WM_QUIT to the message queue */ break; case WM_PAINT: UpdateWindow(hControl); UpdateWindow(hOGL); //wglMakeCurrent(GetDC(hOGL),wglCreateContext(GetDC(hOGL))); draw(); break; default: /* for messages that we don't deal with */ /*GetClientRect(hOGL,&PctRect); SizeOpenGL(PctRect.right,PctRect.bottom);*/ //draw(); return DefWindowProc (hwnd, message, wParam, lParam); } return 0; } void createControlWindow(HINSTANCE hInstance) { hControl = CreateWindow( "STATIC","", WS_VISIBLE|WS_CHILD|WS_BORDER, 10,10,20,20, hMainWnd,NULL,hInstance,NULL ); } void createOpenGLWindow(HINSTANCE hInstance) { bool result = TRUE; hOGL = CreateWindow( "STATIC","", WS_VISIBLE|WS_CHILD|WS_BORDER, 100,0,200,200, hMainWnd,NULL,hInstance,NULL ); MainDC = GetDC(hOGL); GetClientRect(hOGL,&PctRect); if (!MySetupPixelFormat(MainDC)) { result=FALSE; PostQuitMessage(0); } MainHRC = wglCreateContext(MainDC); wglMakeCurrent(MainDC,MainHRC); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); glLightfv(GL_LIGHT0, GL_POSITION, light_pos); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glShadeModel(GL_SMOOTH); glDisable(GL_CULL_FACE); glEnable(GL_NORMALIZE); glEnable(GL_DEPTH_TEST); SizeOpenGL(PctRect.right,PctRect.bottom); } bool MySetupPixelFormat(HDC hDC) { PIXELFORMATDESCRIPTOR pfd; int pixelformat; pfd.nSize=sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion=1; pfd.dwFlags= PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pfd.dwLayerMask= PFD_MAIN_PLANE; pfd.iPixelType=PFD_TYPE_RGBA; pfd.cColorBits=COLOR_DEPTH; pfd.cDepthBits=COLOR_DEPTH; pfd.cAccumBits=0; pfd.cStencilBits=0; if ((pixelformat=ChoosePixelFormat(hDC,&pfd))==FALSE) { MessageBox(NULL,"ChoosePixelFormat : L'opération a échoué.","Q2K2",MB_ICONERROR); return FALSE; } if (SetPixelFormat(hDC,pixelformat,&pfd)==FALSE) { MessageBox(NULL,"SetPixelFormat : L'opération a échoué.","Q2K2",MB_ICONERROR); return FALSE; } return TRUE; } void SizeOpenGL(int width,int height) { if (height==0) height=1; glViewport(0,0,width,height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); //MyPerspective(width,height); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glLineWidth(2.0f); } void UnInit() { if (MainHRC) { wglMakeCurrent(NULL,NULL); wglDeleteContext(MainHRC); } if (MainDC) ReleaseDC(hMainWnd,MainDC); UnregisterClass("Q2K2_WndClassEx",hMainInstance); PostQuitMessage(0); } void draw() { glClearColor(0.9f, 0.9f, 0.9f, 0.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glLightfv(GL_LIGHT0, GL_POSITION, light_pos); glTranslatef(0.0f, 0.0f, -50.0f); glDisable(GL_LIGHTING); glColor3f(1.0f, 0.0f, 0.0f); glRotatef(angle++, 0, 0, 1); glBegin(GL_TRIANGLE_FAN); /*glTexCoord2i(0, 0); */glVertex3i(-20, -20, 0); /*glTexCoord2i(1, 0); */glVertex3i( 20, -20, 0); /*glTexCoord2i(1, 1); */glVertex3i( 20, 20, 0); /*glTexCoord2i(0, 1); */glVertex3i(-20, 20, 0); glEnd(); SwapBuffers(MainDC); }