#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
///////////////////////////
// defines
#define MY_APPLICATION_NAME "GLTest1"
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define SCREEN_BPP 32
///////////////////////////
// globals:
HDC g_hDC;
bool fullscreen = false;
///////////////////////////
// prototypes:
void RenderScene();
void ChangeResolution()
{
DEVMODE devModeScreen;
memset(&devModeScreen,0,sizeof(devModeScreen));
devModeScreen.dmSize = sizeof(devModeScreen);
devModeScreen.dmPelsHeight = SCREEN_HEIGHT;
devModeScreen.dmPelsWidth = SCREEN_WIDTH;
devModeScreen.dmBitsPerPel = SCREEN_BPP;
devModeScreen.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
if(ChangeDisplaySettings(&devModeScreen, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
fullscreen = false;
}
void SetupPixelFormat()
{
int nPixelFormat;
static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
SCREEN_BPP,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0 };
nPixelFormat = ChoosePixelFormat(g_hDC,&pfd);
SetPixelFormat(g_hDC,nPixelFormat,&pfd);
}
///////////////////////////
// WindowProcedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HGLRC hRC;
int width, height;
switch(message)
{
case WM_CREATE:
g_hDC = GetDC(hwnd);
SetupPixelFormat();
hRC = wglCreateContext(g_hDC);
wglMakeCurrent(g_hDC,hRC);
return 0;
break;
case WM_CLOSE:
wglMakeCurrent(g_hDC,NULL);
wglDeleteContext(hRC);
ReleaseDC(hwnd,g_hDC);
PostQuitMessage(0);
return 0;
break;
case WM_SIZE:
height = HIWORD(lParam);
width = LOWORD(wParam);
if(height==0)
{
height=1;
}
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
return 0;
break;
default:
break;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
void InitGLStates()
{
// enable the depth buffer and backface culling
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
// use smooth shading
glShadeModel(GL_SMOOTH);
// Clear background to black
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
///////////////////////////
// WinMain
// Entrypoint
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
WNDCLASSEX windowClass;
HWND hWnd;
MSG msg;
bool done;
DWORD exWindowStyle, windowStyle;
RECT windowRect;
if(fullscreen)
ChangeResolution();
windowClass.cbClsExtra = 0;
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.cbWndExtra = 0;
windowClass.hbrBackground = NULL;
windowClass.hCursor = LoadCursor(NULL,IDC_ARROW);
windowClass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
windowClass.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
windowClass.hInstance = hInstance;
windowClass.lpfnWndProc = (WNDPROC)WndProc;
windowClass.lpszClassName = MY_APPLICATION_NAME;
windowClass.lpszMenuName = NULL;
windowClass.style = CS_HREDRAW | CS_VREDRAW;
if(!RegisterClassEx(&windowClass))
{
MessageBox(NULL,"Error: WindowCreation failure","Error",MB_OK);
return 0;
}
if(fullscreen)
{
exWindowStyle = WS_EX_APPWINDOW;
windowStyle = WS_POPUP;
}
else
{
exWindowStyle = NULL;
windowStyle = WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU |
WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
windowRect.top = 0;
windowRect.left = 0;
windowRect.bottom = SCREEN_HEIGHT;
windowRect.right = SCREEN_WIDTH;
}
AdjustWindowRectEx(&windowRect,windowStyle,NULL,exWindowStyle);
hWnd = CreateWindowEx(exWindowStyle,
MY_APPLICATION_NAME,MY_APPLICATION_NAME,
windowStyle,
0,0,
windowRect.right,windowRect.bottom,
NULL,
NULL,
hInstance,
NULL);
if(!hWnd)
{
MessageBox(NULL,"Error: WindowCreation failure","Error",MB_OK);
return 0;
}
ShowWindow(hWnd,SW_SHOWNORMAL);
UpdateWindow(hWnd);
done=false;
// finally initiate the GLStates:
InitGLStates();
while(!done)
{
PeekMessage(&msg,hWnd,NULL,NULL,PM_REMOVE);
if(msg.message == WM_QUIT)
{
done = true;
}
else
{
RenderScene();
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
// get back to normal desktop size:
if(fullscreen)
ChangeDisplaySettings(NULL,0);
return msg.wParam;
}
///////////////////////////
// Render function
void RenderScene()
{
// clear screen and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// move back 15 units
glTranslatef(0.0f, 0.0f, -50.0f);
// draw our smooth shaded triangle
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f); // red vertex
glVertex3f(-10.0f, -10.0f, -5.0f);
glColor3f(0.0f, 1.0f, 0.0f); // green vertex
glVertex3f(20.0f, -10.0f, -5.0f);
glColor3f(0.0f, 0.0f, 1.0f); // blue vertex
glVertex3f(-10.0f, 20.0f, -5.0f);
glEnd();
glFlush();
SwapBuffers(g_hDC); // bring backbuffer to foreground
}
thnx in advance for any suggestions !
Have a nice day !Grimmm Games - Game Developer"