Du bist nicht angemeldet.

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

1

11.11.2012, 11:48

C++ - Win32 - OpenGL - Zugriffsverletzung - Unklarheiten

Hallo Community!
In letzter Zeit beschäftige ich mich mit OpenGL. Nach vielen Versuchen OpenGL mit der Win32 zu verbinden, habe ich es endlich geschafft.
Nun ist leider bei der Verschiebung der Funktionen in eine Klasse ein Fehler aufgelaufen. Zuerst musste ich nach einer Lösung suchen, die WndProc in die Klasse zu verschieben, ich habe mir einfach diese Lösung herausgesucht:

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);
}


Nun das Problem taucht beim Mausklick auf, um genau zu sein, hier -> cam.setMouseState(...);
Es ist angeblich eine Zugriffsverletzung, hier die Frage an euch, was mache ich falsch?

Danke für alle kommenden Antworten

lg. denniro
Only God can judge me.

BlueCobold

Community-Fossil

Beiträge: 10 738

Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer

  • Private Nachricht senden

2

11.11.2012, 12:41

Debug?
Wir können nämlich sehr schlecht beurteilen, was "cam" überhaupt ist und was the Methode intern macht. Also wirf den Debugger an und geh Step für Step durch.
Teamleiter von Rickety Racquet (ehemals das "Foren-Projekt") und von Marble Theory

Willkommen auf SPPRO, auch dir wird man zu Unity oder zur Unreal-Engine raten, ganz bestimmt.[/Sarkasmus]

3

11.11.2012, 12:55

Ich habe den Debugger über das Programm laufen lassen und genau an der oben erwähnten Stelle liegt der Fehler.
cam ist eine Instanz meiner Camera Klasse, hier der Quellcode für diese:

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;
}


lg. denniro
Only God can judge me.

BlueCobold

Community-Fossil

Beiträge: 10 738

Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer

  • Private Nachricht senden

4

11.11.2012, 13:19

Das kann irgendwie nicht sein, außer Du hast vielleicht den Speicher vermüllt, wo "cam" eigentlich liegen sollte. Oder Dein Code passt nicht zu dem, was der Debugger ausführt.
Teamleiter von Rickety Racquet (ehemals das "Foren-Projekt") und von Marble Theory

Willkommen auf SPPRO, auch dir wird man zu Unity oder zur Unreal-Engine raten, ganz bestimmt.[/Sarkasmus]

5

11.11.2012, 13:31

Dann muss ich wohl die WinApi loslassen und mir was anderes suchen. Vermutlich müllt die WndProc den Speicher zu..
Only God can judge me.

BlueCobold

Community-Fossil

Beiträge: 10 738

Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer

  • Private Nachricht senden

6

11.11.2012, 14:04

Nein, die WndProc müllt nichts zu, maximal Du.
Teamleiter von Rickety Racquet (ehemals das "Foren-Projekt") und von Marble Theory

Willkommen auf SPPRO, auch dir wird man zu Unity oder zur Unreal-Engine raten, ganz bestimmt.[/Sarkasmus]

7

11.11.2012, 14:09

Vermutlich ist einfach dein GameObjekt Zeiger Müll, also der pGame Zeiger. Sicher, dass in lpCreateParams etwas sinnvolles steht?
Lieber dumm fragen, als dumm bleiben!

8

11.11.2012, 14:16

Ich benutze jetzt fürs WindowInterface die SDL, ich kann jetzt leider nicht mehr nachschauen, ob dort was sinnvolles stand, weil ich die Nase voll davon hatte, trotzdem danke für eure Hilfe!

lg. denniro

PS: Kann geschlossen werden!
Only God can judge me.

dot

Supermoderator

Beiträge: 9 757

Wohnort: Graz

  • Private Nachricht senden

9

24.11.2012, 14:17

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...

10

24.11.2012, 17:23

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...

So ich habe nun wieder auf die WinApi zurückgegriffen, leider erscheint dasselbe Problem. Hier der gewünschte Code, der das Kreieren des Fensters beinhaltet:

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;
    }


Danke im Voraus

lg. denniro
Only God can judge me.

Werbeanzeige