Hallo
Ich hab jetzt das SDL Game des 12.Kapitels schon um Explosionen erweitert was auch Problemlos funktioniert.
Jetzt wollte ich mit einem Punktezähler weitermachen. Dazu hab ich mir für die Textausgabe die sdl_ttf runtergeladen und mir
dieses Tutorial angeschaut. Und für die Uwandlung eines Integer Wert in einen char
diese Lösung übernommen.
Für die Textausgabe hab ich mir dann eine eigene Klasse gemacht damit ich sie in anderen Spielen wieder verwnenden kann.
Hier jetzt erstmal der Quellcode:
Text.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
|
#ifndef TEXT_HPP
#define TEXT_HPP
#include "Singleton.hpp"
#include "Framework.hpp"
#include <list>
#include <sstream>
#include "SDL_ttf.h"
using namespace std;
#define g_pText CText::Get()
class CText : public TSingleton<CText>
{
public:
bool Init ();
TTF_Font* Open (const int Zahl, const size);
void Write(TTF_Font* Font,
const char *text,
int x_pos,
int y_pos,
int R,
int G,
int B);//Funktion zum ausgeben von Text
void Write(TTF_Font* Font,
const float Number,
int x_pos,
int y_pos,
int R,
int G,
int B);//Funktion zum Ausgeben von Zahlen
void Quit ();
private:
TTF_Font* m_Font; //aktuelle Font
list<TTF_Font*> m_Fontlist;//Liste mit allen Fonts
SDL_Surface *m_pscreen;
};
#endif
|
Text.cpp
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include "Text.hpp"
bool CText::Init ()
{
if(TTF_Init() == -1)
{
cout << "Konnte SDL_ttf nicht starten: " << endl << TTF_GetError() << endl;
return (false);
}
m_pscreen=g_pFramework->GetScreen ();
return true;
}
TTF_Font* CText::Open (const int Zahl, const size)
{
m_Font = TTF_OpenFont("FreeSansBold.ttf", size);
m_Fontlist.push_back (m_Font);
return m_Font;
}
|
Da die Übergabe mit nem char der den filenname enthält noch nicht Funktioniert wird hier nur iergendein Parameter übergeben und der Funktion TTF_OpenFont gleich die richtige Schriftart übergeben.
|
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
|
//Write Funktion für Text
void CText::Write (TTF_Font* Font, const char *text, int x_pos, int y_pos, int R, int G, int B)
{
SDL_Color textColor;
textColor.r = R;
textColor.g = G;
textColor.b = B;
SDL_Surface* text1 = TTF_RenderText_Blended(Font, text, textColor);
SDL_Rect text_pos;
text_pos.x = x_pos;
text_pos.y = y_pos;
SDL_BlitSurface(text1, NULL, m_pscreen, &text_pos);
SDL_FreeSurface(text1);
}
//Write Funktion für Zahlen
void CText::Write (TTF_Font* Font, const float Number, int x_pos, int y_pos, int R, int G, int B)
{
stringstream Text;
Text.clear();
Text.str( "" );
Text << Number;
SDL_Color textColor;
textColor.r = R;
textColor.g = G;
textColor.b = B;
SDL_Surface* text1 = TTF_RenderText_Blended(Font, Text.str().c_str(), textColor);
SDL_Rect text_pos;
text_pos.x = x_pos;
text_pos.y = y_pos;
SDL_BlitSurface(text1, NULL, m_pscreen, &text_pos);
SDL_FreeSurface(text1);
}
void CText::Quit ()
{
list<TTF_Font*>::iterator It;
for (It=m_Fontlist.begin (); It!=m_Fontlist.end (); It++)
{
TTF_CloseFont(*It);
}
m_Fontlist.clear ();
TTF_Quit();
}
|
Hier noch der Funktionsaufruf der Funktion Write.
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
|
g_pText->Write(m_Font,
m_Points,
0,
0,
255,
255,
255);
|
Wenn ich jetzt das Programm starte Funktioniert das Programm zwar einwandfrei allerdings werden keine Punkte angezeigt.
Die Init Funktion und die Open Funktion der Klasse Text werden beide an einer anderen Stelle im Queltext ausgeführt
Wäre nett wenn mir jemand helfen kann.
Gruß manu7777