Wie manche sicher wissen, habe ich ein Schiffeversenken-Spiel erstellt.
Nun habe ich aber vor, ein neues Game zu machen - Mit Sprites.
Ja, nun fehlen mir DENKE ich die Befehle und so weiter.
In meinem Buch steht da was von:
Sprite.hpp
|
C/C++ Quellcode
|
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
|
#ifndef SPRITE_HPP //mal ein Kommentar von mir, was macht #ifndef eigentlich?
#define SPRITE_HPP
#include "Framework.hpp"
class CSprite
{
public:
CSprite ();
~CSprite ();
void Load (const string sFilename);
void Load (const string sFilename, int NumFrames,
int FrameWidth, int FrameHeight);
void SetColorKey (int R, int G, int B);
void SetPos (float fXPos, float fYPos);
void Render ();
void Render (float fFrameNumber);
SDL_Rect GetRect () {return m_Rect;}
private:
SDL_Surface *m_pScreen; // Zeiger auf den Screen des Frameworks
SDL_Surface *m_pImage; // Das eigentliche Bild des Sprites
SDL_Rect m_Rect; // Rect des Sprites
SDL_Rect m_FrameRect; // Ausschnitt für Animationsphase
int m_NumFrames; // Anzahl der Animationsphasen
int m_FrameWidth; // Breite einer Animationsphase
int m_FrameHeight; // Höhe einer Animationsphase
int m_NumFramesX; // Wie viele Anim-Phasen in X-Richtung?
};
#endif
|
Sprite.cpp
|
C/C++ Quellcode
|
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
|
#include "Sprite.hpp"
// Konstruktor
//
// Aufgabe: Zeiger auf Screen holen
//
CSprite::CSprite ()
{
// Zeiger auf Screen holen
m_pScreen = g_pFramework->GetScreen ();
} // Konstruktor
// Destruktor
//
// Aufgabe: Surface des Sprites freigeben
//
CSprite::~CSprite ()
{
// Surface freigeben
SDL_FreeSurface (m_pImage);
} // Destruktor
// Load
//
// Aufgabe: Einfaches, nicht animiertes Sprite laden
//
void CSprite::Load (const string sFilename)
{
// Bitmap laden
m_pImage = SDL_LoadBMP (sFilename.c_str () );
// Prüfen, ob alles glatt ging
if (m_pImage == NULL)
{
cout << "Fehler beim Laden von: " << sFilename.c_str ();
cout << endl;
cout << "Fehlermeldung: " << SDL_GetError () << endl;
// Framework herunterfahren
g_pFramework->Quit ();
// Gesamtes Spiel beenden
exit (1);
}
// Rect initialisieren
m_Rect.x = 0;
m_Rect.y = 0;
m_Rect.w = m_pImage->w;
m_Rect.h = m_pImage->h;
} // Load
// Load
//
// Aufgabe: Animiertes Sprite laden
//
void CSprite::Load (const string sFilename, int NumFrames,
int FrameWidth, int FrameHeight)
{
// Bitmap laden
Load (sFilename);
// Rect für Animationsphase initialisieren
m_NumFrames = NumFrames;
m_FrameWidth = FrameWidth;
m_FrameHeight = FrameHeight;
m_FrameRect.w = FrameWidth;
m_FrameRect.h = FrameHeight;
m_NumFramesX = m_pImage->w / m_FrameWidth;
} // Load
// SetColorKey
//
// Aufgabe: Transparente Farbe festlegen
//
void CSprite::SetColorKey (int R, int G, int B)
{
// Colorkey einstellen
SDL_SetColorKey (m_pImage, SDL_SRCCOLORKEY,
SDL_MapRGB (m_pImage->format, R, G, B) );
} // SetColorKey
// SetPos
//
// Aufgabe: Position des Sprites festlegen
//
void CSprite::SetPos (float fXPos, float fYPos)
{
// Rect updaten
m_Rect.x = static_cast<int>(fXPos);
m_Rect.y = static_cast<int>(fYPos);
} // SetPos
// Render
//
// Aufgabe: Sprite rendern (ohne Animation)
//
void CSprite::Render ()
{
// Sprite rendern
SDL_BlitSurface (m_pImage, NULL, m_pScreen, &m_Rect);
} // Render
// Render
//
// Aufgabe: Ausschnitt des Sprites rendern (Animationsphase)
//
void CSprite::Render (float fFrameNumber)
{
// Ausschnitt der aktuellen Animationsphase berechnen
//
// Spalte berechnen
int Column = static_cast<int>(fFrameNumber)%m_NumFramesX;
// Zeile berechnen
int Row = static_cast<int>(fFrameNumber)/m_NumFramesX;
// Rect berechnen
m_FrameRect.x = Column * m_FrameWidth;
m_FrameRect.y = Row * m_FrameHeight;
// Ausschnitt rendern
SDL_BlitSurface (m_pImage, &m_FrameRect, m_pScreen, &m_Rect);
} // Render
|
Sagt mir mal bitte, wo da der Sprite geladen wird? Sicher, ich sehe auch den Abschnitt wo es geladen wird, aber wo ist da bitte der Name des *.bmp Files?
ich sehe auch das dieser in sFilename zu sein scheint.
Könnte mir vielleicht wer ein "besseres" Beispiel geben um einen Sprite einer Klasse zuzuordnen, zu laden und an zu zeigen?
PS: Ja, es gibt noch Framework.hpp und Framework.cpp aber dort wird der Name auch nicht bestimmt.