Ich bin gerade dabei ein kleines 2D-Spiel zu proggen, hab noch nicht viel, ein Hauptmenü und einen kleinen Abspann, bei dem der Text von unten nach oben laufen soll, arbeite mit SDL_ttf, SDL_Mixer, aber wenn ich den Abspann laufen lasse, stürzt das Spiel mittendrin ab, erst verschwindet der Text, dann stockt die Hintergrundmusik(MIDI-Datei), und zum Schluss beendet sich das Spiel, aber seltsamerweiser erst nachdem die 30s lange Musikdatei zu Ende gespielt wurde, mit der Fehlermeldung:
Unbehandelte Ausnahme bei 0x61739170 in Spiel.exe: 0xC00000005:
Zugriffsverletzung beim Lesen an Position 0x000000004.
Hier poste ich mal die relevanten Klassen:
CMusic: für abspielen von Musik mit SDL:
Music.hpp:
|
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
|
#ifndef Music_HPP
#define Music_HPP
#include <SDL_mixer.h>
#include <string>
#include <iostream>
class CMusic
{
public:
CMusic();
~CMusic();
bool LoadWav(const std::string Filename);
bool LoadMusic(const std::string Filename);
bool Play(int count);
static bool Init(int audio_rate, Uint16 audio_format, int audio_channels, int audio_buffers);
static void Quit();
static void StopSound() {Mix_HaltChannel(-1);}
static void StopMusic() {Mix_HaltMusic();}
private:
Mix_Chunk *m_psound;
Mix_Music *m_pmusic;
};
#endif
|
Music.cpp
|
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
|
#include "Music.hpp"
CMusic::CMusic()
{
m_psound = NULL;
m_pmusic = NULL;
}
CMusic::~CMusic()
{
if (m_psound != NULL)
{
Mix_FreeChunk(m_psound);
m_psound = NULL;
}
if (m_pmusic != NULL)
{
Mix_FreeMusic(m_pmusic);
m_pmusic = NULL;
}
}
bool CMusic::LoadWav(const std::string Filename)
{
m_psound = Mix_LoadWAV(Filename.c_str());
if(m_psound == NULL)
{
std::cout << "Konnte " << Filename << " nicht laden: " << Mix_GetError() << std::endl;
return false;
}
m_pmusic = NULL;
return true;
}
bool CMusic::LoadMusic(const std::string Filename)
{
m_pmusic = Mix_LoadMUS(Filename.c_str());
if (m_pmusic == NULL)
{
std::cout << "Konnte " << Filename << " nicht laden: " << Mix_GetError() << std::endl;
return false;
}
m_psound = NULL;
return true;
}
bool CMusic::Play(int count)
{
if (m_pmusic != NULL)
{
if(Mix_PlayMusic(m_pmusic, count) == -1)
{
std::cout << "Konnte Musik nicht abspielen: " << Mix_GetError() << std::endl;
return false;
}
return true;
}
else if (m_psound != NULL)
{
if(Mix_PlayChannel(-1, m_psound, count)== -1)
{
std::cout << "Konnte Sound nicht abspielen: " << Mix_GetError() << std::endl;
return false;
}
return true;
}
return false;
}
bool CMusic::Init(int audio_rate, Uint16 audio_format, int audio_channels, int audio_buffers)
{
if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0)
{
std::cout << "Fehler beim Initialisieren des Sounds: " << Mix_GetError() << std::endl;
return false;
}
return true;
}
void CMusic::Quit()
{
Mix_HaltChannel(-1);
Mix_HaltMusic();
Mix_CloseAudio();
}
|
CFont: Für das darstellen von Schrift mittels SDL_ttf:
Font.hpp
|
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
|
#ifndef Font_HPP
#define Font_HPP
#include <SDL_ttf.h>
#include <string>
#include <iostream>
enum RenderMode
{
Solid = 0,
Shaded,
Blended,
};
class CFont
{
private:
TTF_Font *m_pFontFile;
SDL_Color m_ColText;
SDL_Color m_ColBack;
SDL_Rect m_RecPos;
std::string m_sText;
RenderMode m_RM_Mode;
SDL_Surface *m_pSurface;
SDL_Surface *m_pScreen;
int m_iFontsize;
std::string m_sFileName;
public:
CFont();
~CFont();
bool LoadFont(const std::string Filename, int FontSize, RenderMode RM = Solid);
void Render();
void SetPos(unsigned short x, unsigned short y);
void SetText(const std::string Text) {m_sText = Text;}
SDL_Color GetTextColor() {return m_ColText;}
void SetTextColor(const SDL_Color &TextColor) {m_ColText = TextColor;}
SDL_Color GetBackColorForShaded() {return m_ColBack;}
void SetBackColorForShaded(const SDL_Color &BackColor) {m_ColBack = BackColor;}
SDL_Rect GetRect() {return m_RecPos;}
RenderMode GetRenderMode() {return m_RM_Mode;}
void SetRenderMode(RenderMode RM) {m_RM_Mode = RM;}
static bool Init();
static void Quit();
};
#endif
|
Font.cpp
|
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
|
#include "Font.hpp"
#include "Framework.hpp"
CFont::CFont()
{
m_pFontFile = NULL;
m_pSurface = NULL;
m_pScreen = g_pFramework->GetScreen();
m_ColText.r = m_ColText.g = m_ColText.b = 0x0;
m_RecPos.x = m_RecPos.y = m_RecPos.h = m_RecPos.w = 0;
}
CFont::~CFont()
{
if (m_pSurface != NULL)
{
SDL_FreeSurface(m_pSurface);
m_pSurface = NULL;
}
if (m_pFontFile != NULL)
{
TTF_CloseFont(m_pFontFile);
m_pFontFile = NULL;
}
}
bool CFont::LoadFont(const std::string Filename, int Fontsize, RenderMode RM)
{
m_pFontFile = TTF_OpenFont(Filename.c_str(), Fontsize);
if(m_pFontFile == NULL)
{
cout << "Schriftart " << Filename << " mit Schriftgroesse " << Fontsize << " konnte nicht geladen werden: " << TTF_GetError() << endl;
return false;
}
m_RM_Mode = RM;
return true;
}
void CFont::SetPos(unsigned short x, unsigned short y)
{
m_RecPos.x = x;
m_RecPos.y = y;
}
void CFont::Render()
{
switch(m_RM_Mode)
{
case(Solid):
{
m_pSurface = TTF_RenderText_Solid(m_pFontFile, m_sText.c_str(), m_ColText);
} break;
case(Shaded):
{
m_pSurface = TTF_RenderText_Shaded(m_pFontFile, m_sText.c_str(), m_ColText, m_ColBack);
} break;
case(Blended):
{
m_pSurface = TTF_RenderText_Blended(m_pFontFile, m_sText.c_str(), m_ColText);
} break;
}
if (m_pSurface != NULL)
{
SDL_BlitSurface(m_pSurface, NULL, m_pScreen, &m_RecPos);
}
}
bool CFont::Init()
{
if (TTF_Init() == -1)
{
cout << "SDL_TTF konnte nicht gestartet werden: " << TTF_GetError() << endl;
return false;
}
return true;
}
void CFont::Quit()
{
TTF_Quit();
}
|
Nun die Klasse für den Abspann: CEnd:
End.hpp
|
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
|
#ifndef End_HPP
#define End_HPP
#include <iostream>
#include "Singleton.hpp"
#include "Timer.hpp"
#include "Music.hpp"
#include "Gamestate.hpp"
#include "Font.hpp"
#include "Sprite.hpp"
#include "FRect.hpp"
#define g_pEnd CEnd::Get()
class CEnd : public TSingleton<CEnd>
{
public:
CEnd ();
bool Init (bool bBackToMainMenu);
Gamestate Quit ();
void Update ();
void Render ();
bool GetActive () {return m_bActive;}
bool GetInitialized() {return m_bInitialized;}
private:
bool m_bActive;
Gamestate m_NextGS;
bool m_bInitialized;
bool m_bBackToMainMenu;
CSprite *m_pSpriteBackground;
CMusic *m_pMusicBackground;
CFont *m_pFFreeSans;
string m_sTextL0;
string m_sTextL1;
string m_sTextL2;
string m_sTextL3;
string m_sTextL4;
string m_sTextL5;
string m_sTextL6;
string m_sTextL7;
string m_sTextL8;
string m_sTextL9;
string m_sTextL10;
string m_sTextL11;
string m_sTextL12;
string m_sTextL13;
string m_sTextL14;
CFRect *m_pFRText;
SDL_Color m_ColText;
};
#endif
|
End.cpp
|
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
#include "End.hpp"
CEnd::CEnd()
{
m_bActive = m_bInitialized = false;
m_NextGS = None;
m_pMusicBackground = NULL;
m_pSpriteBackground = NULL;
m_pFRText = NULL;
m_ColText.r = m_ColText.g = m_ColText.b = 0;
}
bool CEnd::Init(bool bBackToMainMenu)
{
CMusic::StopMusic();
m_pMusicBackground = new CMusic;
m_pMusicBackground->LoadMusic("C:/My Little Farm/Data.bin/MainMenuMusic.MID");
m_pSpriteBackground = new CSprite;
m_pSpriteBackground->Load("C:/My Little Farm/Data.bin/MainMenuBackground.bmp");
m_pFRText = new CFRect;
m_pFRText->x = 50.0f;
m_pFRText->y = 800.0f;
m_pFRText->h = m_pFRText->w = 0.0f;
m_pFFreeSans = new CFont;
if (!(m_pFFreeSans->LoadFont("FreeSans.ttf", 45)))
return false;
m_ColText.r = 0x0;
m_ColText.g = 0xAF;
m_ColText.b = 0xFF;
m_pFFreeSans->SetTextColor(m_ColText);
m_sTextL0 = "Blabla";
m_sTextL1 = "Zeile";
m_sTextL3 = "Text";
m_sTextL2 = "...!";
m_sTextL4 = ".....";
m_sTextL5 = "------";
m_sTextL6 = "..........";
m_sTextL7 = "-----------";
m_sTextL8 = "..-.---.-.-.-.-.--.";
m_sTextL9 = "^^";
m_sTextL10 = ":)";
m_sTextL11 = ":(";
m_sTextL12 = ":)";
m_sTextL13 = ":(";
m_sTextL14 = "Tschüss";
m_bBackToMainMenu = bBackToMainMenu;
m_bInitialized = true;
m_bActive = true;
return m_pMusicBackground->Play(-1);
}
Gamestate CEnd::Quit()
{
CMusic::StopMusic();
if (m_pMusicBackground != NULL)
{
delete(m_pMusicBackground);
m_pMusicBackground = NULL;
}
if (m_pSpriteBackground != NULL)
{
delete(m_pSpriteBackground);
m_pSpriteBackground = NULL;
}
if (m_pFRText != NULL)
{
delete(m_pFRText);
m_pFRText = NULL;
}
if (m_pFFreeSans != NULL)
{
delete(m_pFFreeSans);
m_pFFreeSans = NULL;
}
m_bInitialized = false;
return m_NextGS;
}
void CEnd::Render()
{
m_pSpriteBackground->SetPos(0.0f, 0.0f);
m_pSpriteBackground->Render();
float tx = m_pFRText->x;
float ty = m_pFRText->y;
m_pFFreeSans->SetText(m_sTextL0);
m_pFFreeSans->SetPos(static_cast<Uint16>(tx), static_cast<Uint16>(ty));
m_pFFreeSans->Render();
ty += 50 * 2;
m_pFFreeSans->SetText(m_sTextL1);
m_pFFreeSans->SetPos(static_cast<Uint16>(tx), static_cast<Uint16>(ty));
m_pFFreeSans->Render();
ty += 50;
m_pFFreeSans->SetText(m_sTextL3);
m_pFFreeSans->SetPos(static_cast<Uint16>(tx), static_cast<Uint16>(ty));
m_pFFreeSans->Render();
ty += 50;
m_pFFreeSans->SetText(m_sTextL2);
m_pFFreeSans->SetPos(static_cast<Uint16>(tx), static_cast<Uint16>(ty));
m_pFFreeSans->Render();
ty += 50 * 2;
m_pFFreeSans->SetText(m_sTextL4);
m_pFFreeSans->SetPos(static_cast<Uint16>(tx), static_cast<Uint16>(ty));
m_pFFreeSans->Render();
ty += 50 * 2;
m_pFFreeSans->SetText(m_sTextL5);
m_pFFreeSans->SetPos(static_cast<Uint16>(tx), static_cast<Uint16>(ty));
m_pFFreeSans->Render();
ty += 50 * 3;
m_pFFreeSans->SetText(m_sTextL6);
m_pFFreeSans->SetPos(static_cast<Uint16>(tx), static_cast<Uint16>(ty));
m_pFFreeSans->Render();
ty += 50;
m_pFFreeSans->SetText(m_sTextL7);
m_pFFreeSans->SetPos(static_cast<Uint16>(tx), static_cast<Uint16>(ty));
m_pFFreeSans->Render();
ty += 50;
m_pFFreeSans->SetText(m_sTextL8);
m_pFFreeSans->SetPos(static_cast<Uint16>(tx), static_cast<Uint16>(ty));
m_pFFreeSans->Render();
ty += 50;
m_pFFreeSans->SetText(m_sTextL9);
m_pFFreeSans->SetPos(static_cast<Uint16>(tx), static_cast<Uint16>(ty));
m_pFFreeSans->Render();
ty += 50;
m_pFFreeSans->SetText(m_sTextL10);
m_pFFreeSans->SetPos(static_cast<Uint16>(tx), static_cast<Uint16>(ty));
m_pFFreeSans->Render();
ty += 50;
m_pFFreeSans->SetText(m_sTextL11);
m_pFFreeSans->SetPos(static_cast<Uint16>(tx), static_cast<Uint16>(ty));
m_pFFreeSans->Render();
ty += 50;
m_pFFreeSans->SetText(m_sTextL12);
m_pFFreeSans->SetPos(static_cast<Uint16>(tx), static_cast<Uint16>(ty));
m_pFFreeSans->Render();
ty += 50;
m_pFFreeSans->SetText(m_sTextL13);
m_pFFreeSans->SetPos(static_cast<Uint16>(tx), static_cast<Uint16>(ty));
m_pFFreeSans->Render();
ty += 50 * 7;
m_pFFreeSans->SetText(m_sTextL14);
m_pFFreeSans->SetPos(static_cast<Uint16>(tx), static_cast<Uint16>(ty));
m_pFFreeSans->Render();
}
void CEnd::Update()
{
if(m_pFRText->y > -1000.0f)
{
m_pFRText->y -= g_pTimer->GetElapsed() * 10.0f;
}
else
{
m_bActive = false;
if (m_bBackToMainMenu)
m_NextGS = MainMenu;
else
m_NextGS = None;
}
if (g_pFramework->KeyDown(SDLK_ESCAPE) || g_pFramework->KeyDown(SDLK_RETURN) ||
g_pFramework->KeyDown(SDLK_SPACE))
{
m_bActive = false;
if (m_bBackToMainMenu)
m_NextGS = MainMenu;
else
m_NextGS = None;
}
}
|
Die Hauptschleife:
|
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
|
while (m_bGameRun == true)
{
// Events bearbeiten
ProcessEvents ();
if (g_pFramework->GetFullscreen())
SDL_ShowCursor(false);
// Framework updaten und Buffer löschen
g_pFramework->Update ();
g_pFramework->Clear ();
switch(m_GS)
{
case (MainMenu):
{
if (!(g_pMainMenu->GetInitialized()))
{
if (!g_pMainMenu->Init())
m_bGameRun = false;
}
if (g_pMainMenu->GetActive())
{
g_pMainMenu->Update();
g_pMainMenu->Render();
}
else
{
m_GS = g_pMainMenu->Quit();
CMainMenu::Del();
}
} break;
case (Game):
{
cout << "Game:" << endl;
m_GS = None;
} break;
case (End):
{
if (!(g_pEnd->GetInitialized()))
{
if (!g_pEnd->Init(true))
m_bGameRun = false;
}
if (g_pEnd->GetActive())
{
g_pEnd->Update();
g_pEnd->Render();
}
else
{
m_GS = g_pEnd->Quit();
CEnd::Del();
}
} break;
case (None):
{
m_bGameRun = false;
}
}
SDL_ShowCursor(true);
// Buffer flippen
g_pFramework->Flip ();
}
|
Die Klasse CFRect ist nur eine von mir erstellte float-Variante zu SDL_Rect.
In der Datei Gamestate.hpp ist nur eine enum mit drei Werten für die Spielzustände: MainMenu: Das Hauptmenü
Game: Das eigentliche Spiel, noch niht implementiert
End: Der Abspann
btw: Ich benutze VC 2008 Express, SDL-1.2.8, SDL_mixer-1.2.8 und SDL_ttf-2.0.9.
Ich hoffe, dass mir jemand helfen kann, ich suche schon seit 5 Stunden nach dem Fehler
.
PS: Mir fällt gerade noch ein, dass der Fehler auch ohne abspielen einer Hintergrundmusik auftritt, und dass in der Dissassembly der Fehler bei folgender Anweisung auftritt, den Quellcode an der Stelle findet der Debugger nicht, sagt immer üwas von Symbole nicht geladen oder kein Quellcode verfügbar:
|
Quellcode
|
1
|
61739170 mov eax,dword ptr [eax+4]
|
eax ist 0.
Ich hoffe das jemand den Fehler kennt oder mir helfen kann, ich bin echt verzweifelt.
mfg C--
EDIT: seltsam: vor Spielstart 40% arbeitsspeicher belegt
während Spiel läuft: 50% arbeitsspeicher belegt
nach absturz: 30% arbeitsspeicher belegt
mfg C--