Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
LRESULT CALLBACK CGame::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_CLOSE: { PostQuitMessage( 0 ); return 0; } case WM_DESTROY: return 0; case WM_LBUTTONDOWN: { cam.setMouseState(true); cam.reset(hWnd); //ShowCursor(false); return 0; } case WM_KEYDOWN: switch ( wParam ) { case VK_ESCAPE: { PostQuitMessage(0); return 0; } case 0x50://[P] { cam.setMouseState(false); ShowCursor(true); return 0; } } return 0; default: return DefWindowProc( hWnd, message, wParam, lParam ); } } LRESULT CALLBACK CGame::StaticWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { CGame* pGame; if(message == WM_CREATE) { pGame = (CGame*)(((LPCREATESTRUCT)lParam)->lpCreateParams); SetWindowLongPtr(hWnd, GWL_USERDATA, (LONG_PTR)pGame); }else { pGame = (CGame*)GetWindowLongPtr(hWnd, GWL_USERDATA); } return pGame->WndProc(hWnd, message, wParam, lParam); } |
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
//CAMERA_H #include "stdafx.h" #ifndef CAMERA_H #define CAMERA_H class camera { private: float camX; //current position of the camera float camY; //camera rotation in Y axis float camZ; //camera rotation in X axis float camYaw; //rotation yAxis float camPitch; //rotation xAxis float mouseVel, moveVel; //Velocity-mouse, WASD PBYTE keyState; //keystate of all keys bool mouseIn; //Mouse in window? void lock(); //check if a rotation is over rage void move(float, float); //move on the x-z axis void moveUp(float, float); //move on the y axis public: camera(); ~camera(); void setMouseState(bool mIn){mouseIn = mIn;} void reset(HWND); //set mouse to the middlepoint of the window void control(HWND, float); //control the game with the mouse void update(); //translate the matrix with the camera matrix void setSpeed(float mouseVel, float moveVel); }; #endif //CAMERA_CPP #include "camera.h" camera::camera() { this->mouseVel = 0.2f; this->moveVel = 0.2f; camX = 0.0; camY = 0.0; camZ = 0.0; camYaw = 0.0; camPitch = 0.0; keyState = new BYTE[256]; mouseIn = false; } camera::~camera() { //free memory delete []keyState; } void camera::lock() { //set campitch between -90 and 90 and set camyaw between 0 and 360 degrees if(camPitch>90) camPitch=90; if(camPitch<-90) camPitch=-90; if(camYaw<0.0) camYaw+=360.0; if(camYaw>360.0) camYaw-=360; } void camera::move(float dir, float moveVel) { float rad=(float)((camYaw+dir)*M_PI/180.0); //convert the degrees into radians(cameraYaw plus direction) - yAxis camX-=sin(rad)*moveVel; //new x-Position(sin*distance) - 90°____270° camZ-=cos(rad)*moveVel; //new z-Position(cos*distance) - 0°____180° } void camera::moveUp(float dir, float moveVel) { float rad=(float)((camPitch+dir)*M_PI/180.0); //convert the degrees into radians(cameraPitch plus direction) - xAxis camY+=sin(rad)*moveVel; //new y-Position(sin*distance) - 90°___-90° } void camera::control(HWND hwnd, float frameTime)//mouse move(sensitivity + mouse in window?) { if(mouseIn) { POINT mousePos; //current mouse position GetCursorPos(&mousePos); //get the mouse position ScreenToClient(hwnd, &mousePos); //mouse position -> relative to window camYaw+=mouseVel*(SCREEN_WIDTH/2-mousePos.x)*frameTime; //Rotation on yAxis(camYaw += half of width subtracted by current mouseposition) camPitch+=mouseVel*(SCREEN_HEIGHT/2-mousePos.y)*frameTime; //Rotation on xAxis(camPitch += half of height subtracted by current mouseposition) lock(); //Check if a rotation is over rage reset(hwnd); if(!GetKeyboardState(keyState))// & 0x80 -> Highest bit(BYTE) MessageBox(NULL, "Couldn't get the keyboard state!", "Error!", MB_OK | MB_ICONERROR); if(keyState[0x57]& 0x80)//[W] { if(camPitch!=90 && camPitch!=-90) move(0.0, moveVel*frameTime); moveUp(0.0, moveVel*frameTime); }else if(keyState[0x53]& 0x80)//[S] { if(camPitch!=90 && camPitch!=-90) move(180.0, moveVel*frameTime); moveUp(180.0, moveVel*frameTime); } if(keyState[0x41]& 0x80)//[A] move(90.0, moveVel*frameTime); else if(keyState[0x44]& 0x80)//[D] move(270, moveVel*frameTime); } glRotatef(-camPitch,1.0,0.0,0.0); //rotate the camera (more precisly move everything in the opposit direction) glRotatef(-camYaw,0.0,1.0,0.0); } void camera::update() { glTranslatef(-camX,-camY,-camZ); //move the camera } void camera::reset(HWND hwnd) { POINT middle; //middle of the window middle.x = SCREEN_WIDTH/2; middle.y = SCREEN_HEIGHT/2; ClientToScreen(hwnd, &middle); //relative to window -> screen coordinates SetCursorPos(middle.x,middle.y); //move the cursor back to the middle of the window } void camera::setSpeed(float mouseVel, float moveVel) { this->mouseVel = mouseVel; this->moveVel = moveVel; } |
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Also dem Code und den Symptomen nach würde ich jetzt mal vermuten, dass das Problem ist, dass die lpCreateParams einfach nicht den richtigen Wert bekommen und daher der this Pointer in CGame::WndProc Müll ist. Zeig doch mal, wie dein Aufruf von CreateWindow aussieht...
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// Create The Window if (!(hWnd=CreateWindowEx( dwExStyle, szClassname, szWindowTitle, dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, windowRect.right-windowRect.left, windowRect.bottom-windowRect.top, NULL, NULL, hInst, NULL))){ MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION); running = false; } |
Werbeanzeige