Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //<-------------OpenGLInit_CPP------------->//
- #include "StdAfx.h"
- #include "OpenGLInit.h"
- //<---Funtkionen--->//
- //Constructor
- OpenGLInit::OpenGLInit(){
- fRotateY = 0.0f;
- fTransX = 0.0f;
- fTransY = 0.0f;
- fTransZ = 0.0f;
- fPi180 = 0.0174532925f;
- for(int i = 0; i<256; i++){
- bKeys[i] = false;
- }
- }
- //Variablen Funktionen
- inline HDC OpenGLInit::get_hDC(){
- return this->hDC;
- }
- inline RECT OpenGLInit::get_rect(){
- return this-> rect;
- }
- inline int OpenGLInit::get_iHoehe(){
- //return this->iBreite;
- return 480;
- }
- inline int OpenGLInit::get_iBreite(){
- //return this->iBreite;
- return 640;
- }
- void OpenGLInit::set_rect(int iWidth, int iHeight, bool bFullscreen){
- if(bFullscreen){
- HWND hWndDesktop = GetDesktopWindow();
- GetWindowRect(hWndDesktop, &rect);
- dwStyle = WS_POPUP;
- }
- else{
- rect.top = 0;
- rect.bottom = 0;
- rect.right = iWidth;
- rect.left = iHeight;
- dwStyle = WS_OVERLAPPEDWINDOW;
- }
- }
- //Generelle Funktionen
- LRESULT CALLBACK OpenGLInit::WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
- switch(msg){
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- case WM_QUIT:
- MessageBox(NULL, "Programm wird beendet", "Programm Ende", MB_ICONINFORMATION | MB_OK);
- break;
- case WM_SIZE:
- opengl_resize_scene(LOWORD(lParam), HIWORD(lParam));
- break;
- default:
- return DefWindowProc(hWnd, msg, wParam, lParam);
- break;
- }
- return 0;
- }
- LRESULT CALLBACK OpenGLInit::StaticWndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
- OpenGLInit* pOGlInit;
- if(msg == WM_CREATE){
- pOGlInit = (OpenGLInit*) (((LPCREATESTRUCT) lParam)->lpCreateParams);
- SetWindowLongPtr(hWnd, GWL_USERDATA, (LONG_PTR) pOGlInit);
- }
- else{
- pOGlInit = (OpenGLInit*)GetWindowLongPtr(hWnd, GWL_USERDATA);
- }
- return pOGlInit->WndProc(hWnd, msg, wParam, lParam);
- }
- bool OpenGLInit::screen_setup(HINSTANCE hInst, int iCmdShow, int iHoehe, int iBreite){
- strcpy_s(szClassName, "MyWndClass");
- strcpy_s(szWindowTitle, "OpenGl");
- this->iHoehe = iHoehe;
- this->iBreite = iBreite;
- //Registrieren
- this->WndClassEx.cbSize = sizeof(WndClassEx);
- this->WndClassEx.cbClsExtra = NULL;
- this->WndClassEx.cbWndExtra = NULL;
- this->WndClassEx.hInstance = hInst;
- this->WndClassEx.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
- this->WndClassEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- this->WndClassEx.hCursor = LoadCursor(NULL, IDC_ARROW);
- this->WndClassEx.hIconSm = NULL;
- this->WndClassEx.lpfnWndProc = StaticWndProc;
- this->WndClassEx.style = CS_OWNDC;
- this->WndClassEx.lpszMenuName = NULL;
- this->WndClassEx.lpszClassName = szClassName;
- if(!RegisterClassEx(&WndClassEx)){
- MessageBox(NULL, "Fehler beim Registrieren des Fensters", "Error!", MB_ICONERROR | MB_OK);
- return false;
- }
- hWnd = CreateWindow(szClassName, szWindowTitle, dwStyle | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, this->iBreite, this->iHoehe, NULL, NULL, hInst, NULL);
- if(!hWnd){
- MessageBox(NULL, "Fehler beim Erstellen des Fensters!", "Error!", MB_ICONERROR | MB_OK);
- return false;
- }
- ShowWindow(hWnd, iCmdShow);
- UpdateWindow(hWnd);
- return true;
- }
- void OpenGLInit::opengl_setup(){
- //Rest
- memset(&pfd, 0, sizeof(pfd));
- 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 = 32;
- pfd.cDepthBits = 32;
- pfd.cAlphaBits = 8;
- //Fullscreen
- memset(&dmSettings, 0, sizeof(dmSettings));
- dmSettings.dmSize = sizeof(dmSettings);
- dmSettings.dmPelsHeight = rect.bottom;
- dmSettings.dmPelsWidth = rect.right;
- dmSettings.dmBitsPerPel = 32;
- dmSettings.dmFields = DM_PELSHEIGHT | DM_PELSWIDTH | DM_BITSPERPEL;
- ChangeDisplaySettings(&dmSettings, CDS_FULLSCREEN);
- //Rest
- hDC = GetDC(hWnd);
- iFormat = ChoosePixelFormat(hDC, &pfd);
- SetPixelFormat(hDC, iFormat, &pfd);
- hGLRC = wglCreateContext(hDC);
- wglMakeCurrent(hDC, hGLRC);
- glLoadIdentity();
- }
- unsigned char OpenGLInit::key_pressed(){
- if(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)){
- if(msg.message == WM_QUIT){
- return 1;
- }
- else if(msg.message == WM_KEYDOWN){
- bKeys[msg.wParam] = true;
- if(msg.wParam == VK_ESCAPE){
- MessageBox(NULL, "Error", "Menu noch nicht implementiert", MB_OK | MB_ICONERROR);
- }
- }
- else if(msg.message == WM_KEYUP){
- bKeys[msg.wParam] = false;
- }
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
- void OpenGLInit::HandleKeyboard(){
- if(bKeys[VK_UP]){
- fTransZ -= cos(fRotateY * fPi180);
- fTransX -= sin(fRotateY * fPi180);
- }
- else if(bKeys[VK_DOWN]){
- fTransZ += cos(fRotateY * fPi180);
- fTransX += sin(fRotateY * fPi180);
- }
- }
- void OpenGLInit::HandleMouse(){
- }
- //OpenGl
- void OpenGLInit::opengl_reset_scene(){
- if(this->iBreite == 0 || this->iHoehe == 0){
- this->iBreite = 1;
- this->iHoehe = 1;
- }
- glViewport(0, 0, this->iBreite, this->iHoehe);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective( 45.0f, this->iBreite/this->iHoehe, 1.0f, 100.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glEnable(GL_DEPTH_TEST);
- glClearColor(0.0f, 0.3f, 0.8, 1.0f);
- }
- void OpenGLInit::opengl_resize_scene( int iBreite, int iHoehe){
- if(iBreite == 0 || iHoehe == 0){
- iBreite = 1;
- iHoehe = 1;
- }
- glViewport(0, 0, iBreite, iHoehe);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective( 45.0f, iBreite/iHoehe, 1.0f, 100.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- void OpenGLInit::opengl_draw_house(float fX, float fZ, float fY, float fR, float fG, float fB){
- glColor3f(0.5f, 0.5f, 0.0f);
- glBegin(GL_QUADS);
- //<---Haus-Grundgerüst--->//
- //Front
- glColor3f(1.0f, 0.0f, 0.0f);
- glVertex3f(fX-1.0f, fZ+1.0f, fZ+1.0f);
- glVertex3f(fX-1.0f, fZ-1.0f, fZ+1.0f);
- glVertex3f(fX+1.0f, fZ-1.0f, fZ+1.0f);
- glVertex3f(fX+1.0f, fZ+1.0f, fZ+1.0f);
- //Right
- glColor3f(0.0f, 1.0f, 0.0f);
- glVertex3f(fX+1.0f, fZ+1.0f, fZ+1.0f);
- glVertex3f(fX+1.0f, fZ-1.0f, fZ+1.0f);
- glVertex3f(fX+1.0f, fZ-1.0f, fZ-1.0f);
- glVertex3f(fX+1.0f, fZ+1.0f, fZ-1.0f);
- //Back
- glColor3f(1.0f, 0.0f, 0.0f);
- glVertex3f(fX+1.0f, fZ+1.0f, fZ-1.0f);
- glVertex3f(fX+1.0f, fZ-1.0f, fZ-1.0f);
- glVertex3f(fX-1.0f, fZ-1.0f, fZ-1.0f);
- glVertex3f(fX-1.0f, fZ+1.0f, fZ-1.0f);
- //Left
- glColor3f(0.0f, 1.0f, 0.0f);
- glVertex3f(fX-1.0f, fZ+1.0f, fZ+1.0f);
- glVertex3f(fX-1.0f, fZ+1.0f, fZ-1.0f);
- glVertex3f(fX-1.0f, fZ-1.0f, fZ-1.0f);
- glVertex3f(fX-1.0f, fZ-1.0f, fZ+1.0f);
- //Top
- glColor3f(0.0f, 0.0f, 1.0f);
- glVertex3f(fX-1.0f, fZ+1.0f, fZ+1.0f);
- glVertex3f(fX+1.0f, fZ+1.0f, fZ+1.0f);
- glVertex3f(fX+1.0f, fZ+1.0f, fZ-1.0f);
- glVertex3f(fX-1.0f, fZ+1.0f, fZ-1.0f);
- //Bottom
- glColor3f(0.0f, 0.0f, 1.0f);
- glVertex3f(fX-1.0f, fZ-1.0f, fZ+1.0f);
- glVertex3f(fX+1.0f, fZ-1.0f, fZ+1.0f);
- glVertex3f(fX+1.0f, fZ-1.0f, fZ-1.0f);
- glVertex3f(fX-1.0f, fZ-1.0f, fZ-1.0f);
- //<---Dach--->//
- //Teil 1
- glColor3f(0.7f, 0.0f, 0.3f);
- glVertex3f(fX+0.0f, fZ+4.0f, fZ+0.0f);
- glVertex3f(fX-1.0f, fZ+1.0f, fZ+0.0f);
- glVertex3f(fX-1.0f, fZ+1.0f, fZ+1.0f);
- glVertex3f(fX+0.0f, fZ+1.0f, fZ+0.0f);
- //Teil 2
- glColor3f(0.7f, 0.0f, 0.3f);
- glVertex3f(fX+0.0f, fZ+4.0f, fZ+0.0f);
- glVertex3f(fX+1.0f, fZ+1.0f, fZ+0.0f);
- glVertex3f(fX+1.0f, fZ+1.0f, fZ-1.0f);
- glVertex3f(fX+0.0f, fZ+1.0f, fZ+0.0f);
- //Teil 3
- glColor3f(0.7f, 0.0f, 0.3f);
- glVertex3f(fX+0.0f, fZ+4.0f, fZ+0.0f);
- glVertex3f(fX-1.0f, fZ+1.0f, fZ+0.0f);
- glVertex3f(fX-1.0f, fZ+1.0f, fZ+1.0f);
- glVertex3f(fX+0.0f, fZ-1.0f, fZ+0.0f);
- //Teil 4
- glColor3f(0.7f, 0.0f, 0.3f);
- glVertex3f(fX+0.0f, fZ+4.0f, fZ+0.0f);
- glVertex3f(fX+1.0f, fZ+1.0f, fZ+0.0f);
- glVertex3f(fX+1.0f, fZ+1.0f, fZ+-1.0f);
- glVertex3f(fX+0.0f, fZ-1.0f, fZ+0.0f);
- glEnd();
- }
- void OpenGLInit::opengl_draw_tower(float fX, float fZ, float fY, float fR, float fG, float fB){
- glBegin(GL_QUADS);
- //<---Tower-Grundgerüst--->//
- //Front
- glColor3f(1.0f, 0.0f, 0.0f);
- glVertex3f(fX-1.0f, fZ+20.0f, fZ+1.0f);
- glVertex3f(fX-1.0f, fZ+19.0f, fZ+1.0f);
- glVertex3f(fX+1.0f, fZ+19.0f, fZ+1.0f);
- glVertex3f(fX+1.0f, fZ+20.0f, fZ+1.0f);
- //Right
- glColor3f(0.0f, 1.0f, 0.0f);
- glVertex3f(fX+1.0f, fZ+20.0f, fZ+1.0f);
- glVertex3f(fX+1.0f, fZ+19.0f, fZ+1.0f);
- glVertex3f(fX+1.0f, fZ+19.0f, fZ-1.0f);
- glVertex3f(fX+1.0f, fZ+20.0f, fZ-1.0f);
- //Back
- glColor3f(1.0f, 0.0f, 0.0f);
- glVertex3f(fX+1.0f, fZ+20.0f, fZ-1.0f);
- glVertex3f(fX+1.0f, fZ-20.0f, fZ-1.0f);
- glVertex3f(fX-1.0f, fZ-20.0f, fZ-1.0f);
- glVertex3f(fX-1.0f, fZ+20.0f, fZ-1.0f);
- //Left
- glColor3f(0.0f, 1.0f, 0.0f);
- glVertex3f(fX-1.0f, fZ+20.0f, fZ+1.0f);
- glVertex3f(fX-1.0f, fZ+20.0f, fZ-1.0f);
- glVertex3f(fX-1.0f, fZ-20.0f, fZ-1.0f);
- glVertex3f(fX-1.0f, fZ-20.0f, fZ+1.0f);
- //Top
- glColor3f(0.0f, 0.0f, 1.0f);
- glVertex3f(fX-1.0f, fZ+20.0f, fZ+1.0f);
- glVertex3f(fX+1.0f, fZ+20.0f, fZ+1.0f);
- glVertex3f(fX+1.0f, fZ+20.0f, fZ-1.0f);
- glVertex3f(fX-1.0f, fZ+20.0f, fZ-1.0f);
- //Bottom
- glColor3f(0.0f, 0.0f, 1.0f);
- glVertex3f(fX-1.0f, fZ+19.0f, fZ+1.0f);
- glVertex3f(fX+1.0f, fZ+19.0f, fZ+1.0f);
- glVertex3f(fX+1.0f, fZ+19.0f, fZ-1.0f);
- glVertex3f(fX-1.0f, fZ+19.0f, fZ-1.0f);
- //<---Dach--->//
- //Teil 1
- glColor3f(0.7f, 0.0f, 0.3f);
- glVertex3f(fX-1.0f, fZ+19.0f, fZ+1.0f);
- glVertex3f(fX+1.0f, fZ+19.0f, fZ+1.0f);
- glVertex3f(fX+1.0f, fZ+19.0f, fZ-1.0f);
- glVertex3f(fX-1.0f, fZ+19.0f, fZ-1.0f);
- glEnd();
- }
- void OpenGLInit::opengl_draw_ground(float fR, float fG, float fB){
- glColor3f(fR, fG, fB);
- glBegin(GL_QUADS);
- glVertex3f(-125.0f, 0.0f, -125.0f);
- glVertex3f(-125.0f, 0.0f, 125.0f);
- glVertex3f(125.0f, 0.0f, 125.0f);
- glVertex3f(125.0f, 0.0f, -125.0f);
- glEnd();
- }
- bool OpenGLInit::opengl_draw_scene(){
- //Allgemein
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glColor3f(1.0f, 1.0f, 1.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glRotatef(360 - fRotateY, 0.0f, 1.0f, 0.0f);
- glTranslatef( -fTransX, fTransY, -fTransZ);
- opengl_draw_ground(0.4f, 0.3f, 0.3f);
- opengl_draw_house(0.0f, 1.0f, -20.0f, 0.0f, 0.0f, 0.0f);
- opengl_draw_house(4.0f, 1.0f, -40.0f, 0.0f, 0.0f, 0.0f);
- opengl_draw_house(-7.0f, 1.0f, -35.0f, 0.0f, 0.0f, 0.0f);
- opengl_draw_tower(-15.0f, 1.0f, -15.0f, 0.0f, 0.0f, 0.0f);
- opengl_draw_tower(-12.0f, 1.0f, -19.0f, 0.0f, 0.0f, 0.0f);
- return true;
- }
- //<-------------OpenGLInit_H------------->//
- //<---Class--->//
- class OpenGLInit{
- private:
- //Generell
- HWND hWnd;
- WNDCLASSEX WndClassEx;
- char szClassName[30];
- char szWindowTitle[30];
- int iHoehe;
- int iBreite;
- MSG msg;
- bool bKeys[256];
- //OpenGl
- HDC hDC;
- HGLRC hGLRC;
- PIXELFORMATDESCRIPTOR pfd;
- int iFormat;
- DEVMODE dmSettings;
- RECT rect;
- DWORD dwStyle;
- float fRotateY;
- float fTransX;
- float fTransY;
- float fTransZ;
- float fPi180;
- public:
- //Constructor
- OpenGLInit();
- //Variablen Funktionen
- inline HDC get_hDC();
- inline RECT get_rect();
- inline int get_iHoehe();
- inline int get_iBreite();
- void set_rect(int iWidth, int iHeight, bool bFullscreen);
- //Generelle Funktionen
- LRESULT CALLBACK WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
- static LRESULT CALLBACK StaticWndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
- bool screen_setup(HINSTANCE hInst, int iCmdShow, int iHoehe, int iBreite);
- void opengl_setup();
- unsigned char key_pressed();
- void HandleKeyboard();
- void HandleMouse();
- //OpenGl
- void opengl_reset_scene();
- void opengl_resize_scene( int iBreite, int iHoehe);
- void opengl_draw_house(float fX, float fZ, float fY, float fR, float fG, float fB);
- void opengl_draw_tower(float fX, float fZ, float fY, float fR, float fG, float fB);
- void opengl_draw_ground(float fR, float fG, float fB);
- bool opengl_draw_scene();
- };
- //<-------------STDAFX_H------------->//
- #ifndef STDAFX_H
- #define STDAFX_H
- //<---Includes--->//
- #include <Windows.h>
- #include <gl\gl.h>
- #include <gl\glu.h>
- #include <math.h>
- //<---Pragmas--->//
- #pragma comment(lib, "opengl.lib")
- #pragma comment(lib, "glu.lib")
- //#pragma comment(lib, "glut32.lib")
- //<---Variablen--->//
- /*char "Error!"[] = "Error!";
- char "Fehler beim Setup des Fensters!"[] = "Fehler beim Setup des Fensters!";
- char "Fehler beim Setup von OpenGl!"[] = "Fehler beim Setup von OpenGl!";
- char "Fehler beim Registrieren des Fensters"[] = "Fehler beim erstellen des Fensters";
- char "Fehler beim Erstellen des Fensters!"[] = "Fehler beim erstellen des Fensters!";*/
- #endif
- //<-------------MAIN_CPP------------->//
- #include "StdAfx.h"
- #include "OpenGLInit.h"
- int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int iCmdShow){
- OpenGLInit OpenGL_Start;
- //Fullscreen?
- if(MessageBox(NULL, "Programm in Fullscreen?", "Fullscreen?", MB_YESNO | MB_ICONINFORMATION) == IDNO){
- OpenGL_Start.set_rect(OpenGL_Start.get_iHoehe(), OpenGL_Start.get_iBreite(), false);
- }
- else{
- OpenGL_Start.set_rect(0, 0, true);
- }
- //<---Fenster+OpenGl--->//
- //Fenster+OpenGl Setup
- if(!OpenGL_Start.screen_setup(hInst, iCmdShow, OpenGL_Start.get_rect().right, OpenGL_Start.get_rect().bottom)){
- MessageBox(NULL, "Error!", "Fehler beim Setup des Fensters!", MB_ICONERROR | MB_OK);
- return false;
- }
- OpenGL_Start.opengl_setup();
- OpenGL_Start.opengl_reset_scene();
- //Fenster ausgeben
- while(true){
- if(OpenGL_Start.key_pressed()){
- break;
- }
- else{
- OpenGL_Start.HandleKeyboard();
- if(!OpenGL_Start.opengl_draw_scene()){
- break;
- }
- else{
- SwapBuffers(OpenGL_Start.get_hDC());
- }
- }
- }
- //<---Ende--->//
- MessageBox(NULL, "Programm wird beendet", "Programm Ende", MB_ICONINFORMATION | MB_OK);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement