- #include <windows.h>
- #include <iostream.h>
- #include <GL\gl.h>
- #include <GL\glu.h>
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- LRESULT CALLBACK WndProc_child(HWND, UINT, WPARAM, LPARAM);
- HINSTANCE hinstance;
- HWND hwnd_maparea;
- HWND hwnd; //Handle zum Fenster
- HGLRC hRC = NULL;
- HDC hDC = NULL;
- bool InitGL(void)
- {
- glEnable(GL_TEXTURE_2D);
- glShadeModel(GL_SMOOTH);
- glClearColor((float) ((162.0f * 100.0f / 255.0f) / 100.0f), (55.0f * 100.0f / 255.0f) / 100.0f, (55.0f * 100.0f / 255.0f) / 100.0f, 0.0f);
- glClearDepth(1.0f);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LEQUAL);
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
- glColor4f(1.0f,1.0f,1.0f,0.5f); // Full Brightness, 50% Alpha ( NEW )
- return TRUE;
- }
- bool CreateGlWindow(void)
- {
- DWORD dwExStyle;
- DWORD dwStyle;
- RECT WindowRect;
- WindowRect.left = (long) 0;
- WindowRect.right = (long) 640;
- WindowRect.top = (long) 0;
- WindowRect.bottom = (long) 480;
- unsigned int PixelFormat;
- dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
- dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style
- AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size
- hwnd_maparea = CreateWindow("maparea",
- "maparea",
- WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwStyle,
- 100, 30, 640, 480,
- NULL,
- NULL,
- hinstance,
- NULL);
- static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
- {
- sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
- 1, // Version Number
- PFD_DRAW_TO_WINDOW | // Format Must Support Window
- PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
- PFD_DOUBLEBUFFER, // Must Support Double Buffering
- PFD_TYPE_RGBA, // Request An RGBA Format
- 32, // Select Our Color Depth
- 0, 0, 0, 0, 0, 0, // Color Bits Ignored
- 0, // No Alpha Buffer
- 0, // Shift Bit Ignored
- 0, // No Accumulation Buffer
- 0, 0, 0, 0, // Accumulation Bits Ignored
- 16, // 16Bit Z-Buffer (Depth Buffer)
- 0, // No Stencil Buffer
- 0, // No Auxiliary Buffer
- PFD_MAIN_PLANE, // Main Drawing Layer
- 0, // Reserved
- 0, 0, 0 // Layer Masks Ignored
- };
- if (!(hDC=GetDC(hwnd_maparea))) // Did We Get A Device Context?
- {
- MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
- return FALSE; // Return FALSE
- }
- if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?
- {
- MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
- return FALSE; // Return FALSE
- }
- if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?
- {
- MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
- return FALSE; // Return FALSE
- }
- if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?
- {
- MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
- return FALSE; // Return FALSE
- }
- if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context
- {
- MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
- return FALSE; // Return FALSE
- }
- ShowWindow(hwnd_maparea,SW_SHOW); // Show The Window
- SetForegroundWindow(hwnd_maparea); // Slightly Higher Priority
- SetFocus(hwnd_maparea); // Sets Keyboard Focus To The Window
- InitGL();
- }
- int DrawGL(void)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
- glLoadIdentity();
- glColor4f(1.0, 0.5, 0.0, 0.5);
- glBegin(GL_TRIANGLES);
- glVertex3f(0, 0, 0);
- glVertex3f(0, 1, 0);
- glVertex3f(1, 0, 0);
- glEnd();
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
- PSTR szCmdLine, int showpref)
- {
- static char AppName[] = "WinTextedit 1.0";
- MSG msg;
- WNDCLASS wndclass;
- wndclass.style = CS_HREDRAW | CS_VREDRAW;
- wndclass.lpfnWndProc = WndProc;
- wndclass.cbClsExtra = 0;
- wndclass.cbWndExtra = 0;
- wndclass.hInstance = hinstance;
- wndclass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
- wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- wndclass.hbrBackground = (HBRUSH) COLOR_MENU + 3;
- wndclass.lpszMenuName = NULL;
- wndclass.lpszClassName = AppName;
- if (RegisterClass(&wndclass) == false)
- {
- MessageBox(NULL, "Es ist ein Fehler aufgetreten. ", "Error! ", MB_ICONERROR);
- return 0;
- }
- wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
- wndclass.hbrBackground = NULL;
- wndclass.lpfnWndProc = WndProc_child;
- wndclass.lpszClassName = "maparea";
- if (RegisterClass(&wndclass) == false)
- {
- MessageBox(NULL, "Es ist ein Fehler aufgetreten. ", "Error! ", MB_ICONERROR);
- return 0;
- }
- hwnd = CreateWindow (AppName,
- TEXT("WinProgramm 1.0"),
- WS_OVERLAPPEDWINDOW,
- 100,
- 100,
- 800,
- 800,
- NULL,
- NULL,
- hInstance,
- NULL);
- ShowWindow(hwnd, SW_SHOWNORMAL);
- UpdateWindow(hwnd);
- bool done = FALSE;
- while(!done) // Loop That Runs Until done=TRUE
- {
- if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting?
- {
- if (msg.message==WM_QUIT) // Have We Received A Quit Message?
- {
- done=TRUE; // If So done=TRUE
- }
- else // If Not, Deal With Window Messages
- {
- TranslateMessage(&msg); // Translate The Message
- DispatchMessage(&msg); // Dispatch The Message
- }
- }
- else // If There Are No Messages
- {
- DrawGL();
- SwapBuffers(hDC);
- }
- }
- return (msg.wParam); // Exit The Program
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch(message)
- {
- case WM_CREATE:
- CreateGlWindow();
- return 0;
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProc(hwnd, message, wParam, lParam);
- }
- LRESULT CALLBACK WndProc_child(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch(message)
- {
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProc(hwnd, message, wParam, lParam);
- }