Hi,
ich bin mit dem Buch(C++ für Spieleprogrammierer) fertig und arbeite grad an dem SDL Spiel.Ich habe das Spiel schon um eine animierte Explosion erweitert, die beim Zerstören von Asteroiden aufgerufen wird.
Nun wollte ich als nächtes hingehen und mir eine Score Anzeige basteln.Dafür habe ich mir nach langem Suchen die SDL TTF gefunden.
Nun bin jetzt einfach hingegangen und habe meine Framework Klasse um ein paar Variablen und eine Funktion namens ShowText erweitert, die dazu dient den text anzuzeigen.
So hier erstmal die erweiterte CFramework.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
|
#ifndef FRAMEWORK_HPP
#define FRAMEWORK_HPP
#include <iostream>
#include "Singleton.hpp"
#include "Timer.hpp"
#include <C:/SDL_TTF/SDL_ttf.h>
using namespace std;
#define g_pFramework CFramework::Get()
class CFramework : public TSingleton<CFramework>
{
public:
bool Init (int ScreenWidth, int ScreenHeight,
int ColorDepth, bool bFullscreen);
void Quit ();
void Update ();
void Clear ();
void Flip ();
bool KeyDown (int Key_ID);
SDL_Surface *GetScreen () {return m_pScreen;}
void ShowText();
private:
SDL_Surface *m_pScreen; // Surface für den Screen
Uint8 *m_pKeystate; // Array für aktuellen Tastaturstatus
SDL_Color m_Color; //Farbe der Schrift
TTF_Font *m_pFont; //Font für die Schrift
SDL_Surface *m_pSurface; //Surface für den Font
};
#endif
|
So und hier nochmal die CFramework.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
|
#include "Framework.hpp"
// Init
//
// Aufgabe: Framework initialisieren
//
bool CFramework::Init (int ScreenWidth, int ScreenHeight,
int ColorDepth, bool bFullscreen)
{
// Alle Systeme der SDL initialisieren
if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1 )
{
cout << "SDL konnte nicht initialisiert werden!" << endl;
cout << "Fehlermeldung: " << SDL_GetError () << endl;
Quit ();
return (false);
}
// Vollbild oder Fenstermodus einstellen
if (bFullscreen == true)
{
m_pScreen = SDL_SetVideoMode (ScreenWidth, ScreenHeight, ColorDepth,
SDL_HWSURFACE | SDL_DOUBLEBUF |
SDL_FULLSCREEN);
}
else
{
m_pScreen = SDL_SetVideoMode (ScreenWidth, ScreenHeight, ColorDepth,
SDL_HWSURFACE | SDL_DOUBLEBUF);
}
// Prüfen, ob alles funktioniert hat
if (m_pScreen == NULL)
{
cout << "Videomodus konnte nicht gesetzt werden!" << endl;
cout << "Fehlermeldung: " << SDL_GetError () << endl;
Quit ();
return (false);
}
// Zeiger auf internes Array für Tastaturstatus ermitteln
m_pKeystate = SDL_GetKeyState (NULL);
// Alles ging glatt, also true zurückliefern
return (true);
} // Init
// Quit
//
// Aufgabe: Framework (SDL) beenden
//
void CFramework::Quit ()
{
// SDL beenden
SDL_Quit ();
} // Quit
// Update
//
// Aufgabe: Timer und Keyboardstatus updaten
//
void CFramework::Update ()
{
// Timer updaten
g_pTimer->Update ();
// Tastaturstatus ermitteln
SDL_PumpEvents ();
} // Update
// Keydown
//
// Aufgabe: Tastendruck abfragen
//
bool CFramework::KeyDown (int Key_ID)
{
// Prüfen, ob Taste gedrückt ist
return (m_pKeystate[Key_ID] ? true : false);
} // KeyDown
// Clear
//
// Aufgabe: Buffer löschen
//
void CFramework::Clear ()
{
// Buffer (Surface) mit Hintergrundfarbe füllen
SDL_FillRect (m_pScreen, NULL,
SDL_MapRGB (m_pScreen->format, 0, 0, 0));
} // Clear
// Flip
//
// Aufgabe: Surface umschalten (flippen)
//
void CFramework::Flip ()
{
// Surface umschalten
SDL_Flip (m_pScreen);
} // Flip
void CFramework::ShowText()
{
//SDL TTF initialisieren und falls etwas schief läuft 1 zurückgeben
if(TTF_Init() < 0)
{
cout<<"Fehler beim Initialisieren von SDL TTF "<<SDL_GetError()<<endl;
exit(1);
}
//Ein Font laden mit der Schriftgröße von 60
m_pFont = TTF_OpenFont("Data/LaPointesRoad.ttf", 60);
//Prüfen ob Font geladen werden konnte
if (m_pFont == NULL)
{
cout<<"Fehler beim Laden vom Font "<<SDL_GetError()<<endl;
exit(1);
}
//Farbe festlegen
m_Color.r = 255;
m_Color.g = 0;
m_Color.b = 255;
m_pSurface = TTF_RenderText_Solid("Hallo", m_Color);
//Text Position bestimmen
SDL_Rect TextPosition = {0, 250, 64, 0};
//Bildschirm löschen
Clear();
//Text anzeigen
SDL_BlitSurface(&m_pSurface, NULL, m_pScreen, &TextPosition);
//zwischen Back und Frontbuffer schalten
Flip();
//Aufräumen
//Surface lösachen
SDL_FreeSurface(m_pSurface);
//TTF Font löschen
TTF_CloseFont(m_pFont);
TTF_Quit();
}
|
Das wars erstmal, alles müsste normal Fehlerfrei laufen.Zumindest, wenn ich alles richitg verstanden habe.
Aber es folgen ein paar Fehler, die ich nciht verstehe:
|
Quellcode
|
1
2
3
4
|
C:\SDL Projekte\Spiel\SDL Spiel\Framework.cpp||In member function `void CFramework::ShowText()':|
C:\SDL Projekte\Spiel\SDL Spiel\Framework.cpp|141|error: cannot convert `const char*' to `TTF_Font*' for argument `1' to `SDL_Surface* TTF_RenderText_Solid(TTF_Font*, const char*, SDL_Color)'|
C:\SDL Projekte\Spiel\SDL Spiel\Framework.cpp|151|error: cannot convert `SDL_Surface**' to `SDL_Surface*' for argument `1' to `int SDL_UpperBlit(SDL_Surface*, SDL_Rect*, SDL_Surface*, SDL_Rect*)'|
||=== Build finished: 2 errors, 0 warnings ===|
|
Ich weiß nicht woran es liegt würde mich über eine Erklärung, falls ich was nicht richitg verstanden habe freuen.
Danke.
Edit:
Ich habe dieses Tut hier zu SDL TTF gefunden und auch durchgegangen und fast genau so angewendet.
http://sppro.fkrauthan.de/2008/09/12/sdl-eine-einfuehrung/5/
Wenn hier etwas falsch ist, dann bitte ein anderes gutes Tut empfehlen.