PDA

View Full Version : opengl & dialogbox in win32api



mr somboon
07-07-2005, 01:52 PM
How can I initilize opengl in a dialog box,
using pure win32api ?

and where should I call
my rendering code?

thank

Silkut
07-08-2005, 04:59 AM
Hi, yes you can initialize OpenGL app with pure Win32 API you should call your rendering in the end, this is not an obligation but most tutorials explain like that..


#include <windows.h>
#include <stdio.h>

//-----------------------------------------------------------------------------
// Func: Globals declarations
// Desc: déclaration globales de ce qui sera utilisé pour creer la fenetre
//-----------------------------------------------------------------------------
HDC g_hDC = NULL; // Device context
HGLRC g_hRC = NULL; /* Render context /!\ */
HWND g_hWnd = NULL; // window handle
HINSTANCE g_hInstance = NULL; // instance de l'appli

//-----------------------------------------------------------------------------
// Func: Prototypes
// Desc: prototypes de fonction
//-----------------------------------------------------------------------------
int WINAPI WinMain( HINSTANCE g_hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow );

LRESULT CALLBACK WindowProc( HWND g_hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
void init(void);
void render(void);
void shutDown(void);

//-----------------------------------------------------------------------------
// Func: WinMain()
// Desc: Début de l'application
//-----------------------------------------------------------------------------
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )

{
WNDCLASSEX winClass;
MSG uMsg;

memset(&amp;uMsg, 0, sizeof(uMsg));

winClass.lpszClassName = "MA_CLASSE"; // nom de notre classe (il doit etre le meme pendant toute l'appli
winClass.cbSize = sizeof(WNDCLASSEX); // spécifie la taille en bytes de la structure
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // redessine la scene si la taille est modifiée
winClass.lpfnWndProc = WindowProc; // pointeur de la procédure
winClass.hInstance = hInstance; // handle qui contient la procedure
winClass.hIcon = NULL; // ne charger aucune icone
winClass.hIconSm = NULL; // handle de l'icone associée a la classe
winClass.hCursor = LoadCursor(NULL, IDC_ARROW); // curseur = fleche
winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); // couleur de background
winClass.lpszMenuName = NULL; // pas de menu
winClass.cbClsExtra = 0; // pas d'extra-bytes a allouer pour la procedure
winClass.cbWndExtra = 0; // pas d'extra-bytes pour l'instance

if(!RegisterClassEx(&amp;winClass))
{
MessageBox(NULL, "L'enregistrement de winClass a échoué", "misérable failure", MB_OK|MB_ICONEXCLAMATION);
return E_FAIL;
}

g_hWnd = CreateWindowEx( NULL, "MA_CLASSE", "Une super fenetre en win32", WS_OVERLAPPEDWINDOW, 0, 0, 640, 480, NULL, NULL, g_hInstance, NULL );

if(g_hWnd == NULL)
{
MessageBox(NULL, "La création de la fenetre a échouée", "super failure !", MB_OK|MB_ICONEXCLAMATION);
return E_FAIL;
}

ShowWindow(g_hWnd, nCmdShow);
UpdateWindow(g_hWnd);

init();
// fonctions()...

while( uMsg.message != WM_QUIT )
{
if(PeekMessage(&amp;uMsg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&amp;uMsg);
DispatchMessage(&amp;uMsg);
}
else
render();
}

shutDown();

UnregisterClass("MA_CLASSE", g_hInstance);

return uMsg.wParam;
}

//-----------------------------------------------------------------------------
// Func: WindowProc()
// Desc: Callback et boucle de messages..
//-----------------------------------------------------------------------------
LRESULT CALLBACK WindowProc(HWND g_hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_KEYDOWN:
{
switch(wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
}break;

case WM_CLOSE:
{
PostQuitMessage(0);
}
case WM_DESTROY:
{
PostQuitMessage(0);
}
break;

default:
{
return DefWindowProc(g_hWnd, msg, wParam, lParam);
}
break;
}
return 0;
}

//-----------------------------------------------------------------------------
// Func: init()
// Desc: initialisation des params de la fenetre
//-----------------------------------------------------------------------------
void init(void)
{
int PixelFormat;
PIXELFORMATDESCRIPTOR pfd;
memset(&amp;pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));

pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 16;
pfd.cDepthBits = 16;

g_hDC = GetDC(g_hWnd);
PixelFormat = ChoosePixelFormat(g_hDC, &amp;pfd);
SetPixelFormat(g_hDC, PixelFormat, &amp;pfd);
g_hRC = wglCreateContext(g_hDC); /* /!\ */
wglMakeCurrent(g_hDC, g_hRC); /* /!\ */

//-------------
// HERE GOES EVERYTHING ABOUT OPENGL MATRICES, MODELVIEW, ETC..
//-------------
}

//-----------------------------------------------------------------------------
// Func: shutDown()
// Desc: on ferme l'application, en oubliant pas de liberer quelques trucs..
//-----------------------------------------------------------------------------
void shutDown(void)
{
if(g_hDC != NULL)
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(g_hRC);
g_hRC = NULL;
}

if(g_hRC != NULL)
{
ReleaseDC(g_hWnd, g_hDC);
g_hDC = NULL;
}
}

//-----------------------------------------------------------------------------
// Func: render()
// Desc: operation graphique et affichage a l'ecran
//-----------------------------------------------------------------------------
void render(void)
{
// RENDER WHAT YOU WANT
SwapBuffers(g_hDC);
} Try this

Sorry for the french comments, forget them, i placed english comments where you should render and where you should specify opengl matrices etc..