Ja, det mener jeg helt bestemt, men du kan jo selv prøve at se:
#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN
#include <windows>
#include <gl/gl>
#include <gl/glu>
using namespace std;
bool exiting = false;
long windowWidth = 800;
long windowHeight = 600;
long windowBits = 32;
bool fullscreen = true;
HDC hDC;
int m_windowWidth, m_windowHeight;
bool keys[256] = {0};
float x_pos;
void SetupPixelFormat(HDC hDC)
{
int pixelFormat;
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_SUPPORT_OPENGL |
PFD_DRAW_TO_WINDOW |
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
};
pixelFormat = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, pixelFormat, &pfd);
}
void SetupProjection(int width, int height)
{
if(height == 0) height = 1;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(52.0f, (GLfloat)width/(GLfloat)height, 1.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
m_windowWidth = width;
m_windowHeight = height;
}
bool Init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
return true;
}
void Render()
{
if (keys[VK_LEFT])
x_pos -= 0.1;
else
if (keys[VK_RIGHT])
x_pos += 0.1;
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0, 0, 5, 0, 0, -5, 0, 5, 0);
glPushMatrix();
glTranslatef(x_pos, 0, 0);
glBegin(GL_QUADS);
glVertex2f(0, 0);
glVertex2f(1, 0);
glVertex2f(1, 1);
glVertex2f(0, 1);
glEnd();
glPopMatrix();
}
LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static HDC hDC;
static HGLRC hRC;
int height, width;
switch (uMsg)
{
case WM_CREATE:
hDC = GetDC(hWnd);
SetupPixelFormat(hDC);
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
break;
case WM_DESTROY:
case WM_QUIT:
wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRC);
PostQuitMessage(0);
break;
case WM_SIZE:
height = HIWORD(lParam);
width = LOWORD(lParam);
SetupProjection(width, height);
break;
case WM_KEYDOWN:
keys[wParam] = true;
break;
case WM_KEYUP:
keys[wParam] = false;
break;
default:
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
//100
WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX windowClass;
HWND hwnd;
MSG msg;
DWORD dwExStyle;
DWORD dwStyle;
RECT windowRect;
windowRect.left = (long) 0;
windowRect.right = (long) windowWidth;
windowRect.top = (long) 0;
windowRect.bottom = (long) windowHeight;
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = MainWindowProc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = hInstance;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground = NULL;
windowClass.lpszMenuName = NULL;
windowClass.lpszClassName = "GLClass";
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
if(!RegisterClassEx(&windowClass)) return 0;
if(fullscreen) {
DEVMODE dmScreenSettings;
memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
dmScreenSettings.dmSize = sizeof(dmScreenSettings);
dmScreenSettings.dmPelsWidth = windowWidth;
dmScreenSettings.dmPelsHeight = windowHeight;
dmScreenSettings.dmBitsPerPel = windowBits;
dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) {
MessageBox(NULL, "DisplayMode failed", NULL, MB_OK);
fullscreen = false;
}
}
if(fullscreen) {
dwExStyle = WS_EX_APPWINDOW;
dwStyle = WS_POPUP;
ShowCursor(false);
}
else {
dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwStyle = WS_OVERLAPPEDWINDOW;
}
AdjustWindowRectEx(&windowRect, dwStyle, false, dwExStyle);
hwnd = CreateWindowEx (NULL,
"GLCLASS",
"openGL Application",
dwStyle | WS_CLIPCHILDREN |
WS_CLIPSIBLINGS,
0, 0,
windowRect.right - windowRect.left,
windowRect.bottom - windowRect.top,
NULL,
NULL,
hInstance,
NULL);
hDC = GetDC(hwnd);
if(!hwnd) return 0;
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
Init();
while(!exiting) {
Render();
SwapBuffers(hDC);
while(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
if(!GetMessage(&msg, NULL, 0, 0)) {
exiting = true;
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
if(fullscreen) {
ChangeDisplaySettings(NULL, 0);
ShowCursor(true);
}
return (int)msg.wParam;
}
[Redigeret d. 23/02-06 13:38:30 af Kristian]