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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
#include "carrace.h"
// __________________________________________________________________
tbModel* g_pCityModel = NULL; // Modell der Stadt
tbModel* g_pBus = NULL;
//Camera
tbVector3 g_vCameraPos = tbVector3(0.0f, -25.0f, -20.0f); //
Kameraposition
float g_fCameraRot; // Rotation der Kamera
float g_fCameraUpDown; // Schaut die Kamera hoch oder runter?
//BUS
tbVector3 buspos = tbVector3(0.0f, -25.0f, -20.0f);
float gang = 0.0f;
// Initialisiert den Spielzustand
tbResult CGame::Init()
{
// Laden...
if(Load()) TB_ERROR("Fehler beim Laden des Spielzustands!", TB_ERROR);
return TB_OK;
}
// __________________________________________________________________
// Fährt den Spielzustand herunter
tbResult CGame::Exit()
{
// Entladen...
Unload();
return TB_OK;
}
// __________________________________________________________________
// Lädt den Spielzustand
tbResult CGame::Load()
{
// Das Stadtmodell laden
g_pBus = new tbModel;
if(g_pBus->Init("Data\\bus.tbm", "Data\\"))
{
TB_ERROR("konnte model net laden", TB_ERROR);
}
// Das Stadtmodell laden
g_pCityModel = new tbModel;
if(g_pCityModel->Init("Data\\leveleins.tbm", "Data\\"))
{
TB_ERROR("konnte modebusl net laden", TB_ERROR);
}
return TB_OK;
}
// __________________________________________________________________
// Entlädt den Spielzustand
tbResult CGame::Unload()
{
TB_SAFE_DELETE(g_pBus);
TB_SAFE_DELETE(g_pCityModel);
return TB_OK;
}
// __________________________________________________________________
// Bewegt den Spielzustand
tbResult CGame::Move(float fTime)
{
SetPosition(buspos);
// Wenn die Escape-Taste gedrückt wird, geht's zurück ins Hauptmenü.
if(g_pbButtons[TB_KEY_ESCAPE]) PostQuitMessage(0);
tbVector3 vCameraDir;
// P oder Pause hält das Spiel an oder setzt es fort.
if(g_pbButtons[TB_KEY_P] || g_pbButtons[TB_KEY_PAUSE])
{
m_bPaused = !m_bPaused;
tbDelay(100);
}
// Wenn das Spiel pausiert, gibt es nichts mehr zu tun.
if(m_bPaused) return TB_OK;
// ------------------------------------------------------------------
// Tastatursteuerung...
vCameraDir = tbVector3(sinf(g_fCameraRot) * cosf(g_fCameraUpDown),
sinf(g_fCameraUpDown),
cosf(g_fCameraRot) * cosf(g_fCameraUpDown));
if(g_pbButtons[TB_KEY_LEFT]) g_fCameraRot -= 1.0f * fTime;
if(g_pbButtons[TB_KEY_RIGHT]) g_fCameraRot += 1.0f * fTime;
if(g_pbButtons[TB_KEY_UP]) g_vCameraPos += vCameraDir * gang * fTime;
if(g_pbButtons[TB_KEY_DOWN]) g_vCameraPos -= vCameraDir * gang * fTime;
if(g_pbButtons[TB_KEY_X]){ gang+=15.0f; tbDelay(50); }
if(g_pbButtons[TB_KEY_Y]){ gang-=15.0f; tbDelay(50); }
if(g_pbButtons[TB_KEY_W]){ TranslateRel(tbVector3(0.0f, 0.0f, 10.0f)); }
// ------------------------------------------------------------------
Update();
Move(fTime);
// ------------------------------------------------------------------
return TB_OK;
}
// __________________________________________________________________
// Rendert den Spielzustand
tbResult CGame::Render(float fTime)
{
float fAspect;
tbMatrix mProjection;
tbMatrix mCamera;
tbMatrix mWorld;
tbMatrix mTranslation;
tbVector3 vCameraDir;
// Puffer leeren und Szene beginnen
tbDirect3D::Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
tbColor(0.0f, 0.0f, 1.0f), 1.0f, 0);
tbDirect3D::BeginScene();
// ------------------------------------------------------------------
tbDirect3D::GetDevice()->SetRenderState(D3DRS_LIGHTING , FALSE);
// ------------------------------------------------------------------
// Projektionsmatrix erstellen und einsetzen
fAspect = (float)(800) / (float)(600);
mProjection = tbMatrixProjection(TB_DEG_TO_RAD(60.0f), fAspect, 1.0f, 400.0f);
tbDirect3D::GetDevice()->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)(&mProjection));
// Kameramatrix erstellen und einsetzen
vCameraDir = tbVector3(sinf(g_fCameraRot) * cosf(g_fCameraUpDown),
sinf(g_fCameraUpDown),
cosf(g_fCameraRot) * cosf(g_fCameraUpDown));
mCamera = tbMatrixCamera(g_vCameraPos, g_vCameraPos + vCameraDir);
tbDirect3D::GetDevice()->SetTransform(D3DTS_VIEW, (D3DMATRIX*)(&mCamera));
// ------------------------------------------------------------------
// Weltmatrix zurücksetzen
tbDirect3D::GetDevice()->SetTransform(D3DTS_WORLD, (D3DMATRIX*)(&tbMatrixIdentity()));
// Linearer schwarzer Nebel!
tbDirect3D::SetRS(D3DRS_FOGENABLE, TRUE);
tbDirect3D::SetRS(D3DRS_FOGVERTEXMODE, D3DFOG_EXP);
tbDirect3D::SetRS(D3DRS_RANGEFOGENABLE, TRUE);
tbDirect3D::SetRS(D3DRS_FOGCOLOR, tbColor(0.2f, 0.2f, 0.5f));
tbDirect3D::SetRSF(D3DRS_FOGSTART, 100.0f);
tbDirect3D::SetRSF(D3DRS_FOGEND, 300.0f);
tbDirect3D::SetRSF(D3DRS_FOGDENSITY, 0.01f);
// Das Modell rendern (zuerst alle opaken Effekte, dann alle transparenten)
g_pCityModel->Render(-1, -1, TRUE, FALSE);
g_pCityModel->Render(-1, -1, FALSE, TRUE);
mTranslation = tbMatrixTranslation(buspos);
mWorld = mTranslation;
tbDirect3D::GetDevice()->SetTransform(D3DTS_WORLD, (D3DMATRIX*)(&mWorld));
// Rendern
g_pBus->Render();
char acText[50];
sprintf(acText, "FPS: %.2f", 1.0f / fTime);
g_pCarrace->m_pFont1->Begin();
g_pCarrace->m_pFont1->DrawText(tbVector2(0.5f, 0.5f), acText);
g_pCarrace->m_pFont1->End();
g_pCarrace->m_pFont1->Begin();
if(gang == 0.0f)
{
g_pCarrace->m_pFont1->DrawText(tbVector2(300.5f, 0.0f), "Leerlauf", TB_FF_RELATIVESCALING);
}
else if(gang == 15.0f)
{
g_pCarrace->m_pFont1->DrawText(tbVector2(300.5f, 0.0f), "Gang 1", TB_FF_RELATIVESCALING);
}
else if(gang == 30.0f)
{
g_pCarrace->m_pFont1->DrawText(tbVector2(300.5f, 0.0f), "Gang2", TB_FF_RELATIVESCALING);
}
else if(gang == 45.0f)
{
g_pCarrace->m_pFont1->DrawText(tbVector2(300.5f, 0.0f), "Gang 3", TB_FF_RELATIVESCALING);
}
else if (gang == 60.0f)
{
g_pCarrace->m_pFont1->DrawText(tbVector2(300.5f, 0.0f), "Gang 4", TB_FF_RELATIVESCALING);
}
else if (gang == 75.0f)
{
g_pCarrace->m_pFont1->DrawText(tbVector2(300.5f, 0.0f), "Gang 5", TB_FF_RELATIVESCALING);
}
else
{
g_pCarrace->m_pFont1->DrawText(tbVector2(300.5f, 0.0f), "Gang 6", TB_FF_RELATIVESCALING);
}
g_pCarrace->m_pFont1->End();
// Wenn das Spiel pausiert, wird "Pause" in der Bildmitte angezeigt.
if(m_bPaused)
{
g_pCarrace->m_pFont1->Begin();
g_pCarrace->m_pFont1->DrawText(tbVector2(0.5f, 0.5f), "Pause",
TB_FF_ALIGN_HCENTER | TB_FF_ALIGN_VCENTER | TB_FF_RELATIVE |
TB_FF_RELATIVESCALING);
g_pCarrace->m_pFont1->End();
}
// ------------------------------------------------------------------
// Szene beenden
tbDirect3D::EndScene();
// tbDirect3D::GetDevice()->Present(NULL, NULL, NULL, NULL);
return TB_OK;
}
// __________________________________________________________________
|