-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
356 lines (271 loc) · 9.61 KB
/
main.cpp
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <stdio.h>
#include <string.h>
#include <psxgpu.h>
#include <psxgte.h>
#include <inline_c.h>
#include <Graphics.h>
#include <Joystick.h>
#include <TextureManager.h>
#include <animated/cube.h>
#include <camera.h>
#include <Util.h>
#include <scenario/meshs/skysphere.h>
#include <scenario/meshs/skyUVInfo.h>
#include <scenario/meshs/hills.h>
#include<scenario/meshs/sun.h>
#include <scenario/Map.h>
#include <AmbientLightController.h>
#include <NoiseGenerator.h>
#include <PerlinNoise.h>
#include <gamestate/GameState.h>
#include <ui/MainMenu.h>
#include <ui/HUD.h>
/* Number of faces of cube */
#define CUBE_FACES 6
template<typename T>
T abs(const T& value)
{
return value > 0 ? value : -value;
}
/* Light color matrix */
/* Each column represents the color matrix of each light source and is */
/* used as material color when using gte_ncs() or multiplied by a */
/* source color when using gte_nccs(). 4096 is 1.0 in this matrix */
/* A column of zeroes disables the light source. */
MATRIX color_mtx = {
4096, 0, 0, /* Red */
4096, 0, 0, /* Green */
4096, 0, 0 /* Blue */
};
/* Light matrix */
/* Each row represents a vector direction of each light source. */
/* An entire row of zeroes disables the light source. */
MATRIX light_mtx = {
/* X, Y, Z */
-2048 , -512 , -2048,
//0,0,0,
0 , 0 , 0,
0 , 0 , 0
};/*141,157,52
151,167,244*/
extern unsigned int sunset0_tim[];
extern unsigned int sunset1_tim[];
extern unsigned int sunset2_tim[];
extern unsigned int sunset3_tim[];
extern unsigned int sunset4_tim[];
extern unsigned int skysphere_tim[];
extern unsigned int hills_tim[];
extern unsigned int sun_tim[];
/*TIM_IMAGE sunset0_texture;
TIM_IMAGE sunset1_texture;
TIM_IMAGE sunset2_texture;
TIM_IMAGE sunset3_texture;
TIM_IMAGE sunset4_texture;*/
TIM_IMAGE skysphere_texture;
TIM_IMAGE hills_texture;
TIM_IMAGE sun_texture;
GraphClass *graphics;
MATRIX DrawSkyBox(const MATRIX& mtx);
MATRIX DrawSun(const MATRIX& mtx);
int main() {
graphics = new GraphClass(2048,65536);
SVECTOR rot = { 0 }; /* Rotation vector for Rotmatrix */
VECTOR pos = { 0, 0, 0 }; /* Translation vector for TransMatrix */
/* Init graphics and GTE */
graphics->SetResolution(320,240);
//gte_SetColorMatrix( &color_mtx );
FntLoad(960, 0);
FntOpen(0, 8, 320, 216, 0, 100);
DrawSync(0);
TextureManager::LoadTexture(skysphere_tim, skysphere_texture);
TextureManager::LoadTexture(hills_tim, hills_texture);
TextureManager::LoadTexture(sun_tim, sun_texture);
Joystick joystick = Joystick();
Camera camera;
// Object and light matrix for object
camera.LookAt({0,0,0});
//camera.SetPosition(12457346, 3932160 , 27444391);
// Multiply light matrix to object matrix
/* Main loop */
//camera.SetPosition(686592, -2445312, 11012608);
//camera.SetPosition(0, -(352<<12), 0);
camera.SetPosition((11<<12)*512,-768<<12,(11<<12)*512);
int mapPosX=(0<<12), mapPosZ=(0<<12);
auto *map = new Map(graphics, mapPosX, mapPosZ);
MATRIX objectMTX{0};
MainMenu mainMenu;
GameState gameState;
TextureManager::LoadTexture(parchment_tim, parchment_texture);
while( 1 ) {
//FntPrint(-1, "x,y = %d,%d", parchment_texture.prect->x, parchment_texture.prect->y);
if(joystick.Status() == 0)
{
if( joystick.IsAnalog() || joystick.IsDualshock() )
{
const char LSX = joystick.LeftStickX() - 128;
const char LSY = joystick.LeftStickY() - 128;
const char RSX = joystick.RightStickX() - 128;
const char RSY = joystick.RightStickY() - 128;
if(RSY < -24)
{
camera.Roll(RSY*4);
}
else if(RSY > 24)
{
camera.Roll(RSY*4);
}
if(RSX < -24)
{
camera.Pich(-RSX*4);
}
else if(RSX > 24)
{
camera.Pich(-RSX*4);
}
if(joystick.IsPressed(PAD_LEFT))
{
//camera.Translate(-ONE,0,0);
//rot.vy += 8;
}
if(joystick.IsPressed(PAD_RIGHT))
{
//camera.Translate(ONE,0,0);
//rot.vy -= 8;
}
if(joystick.IsPressed(PAD_DOWN))
{
//camera.Translate(0,ONE*4,0);
}
if(joystick.IsPressed(PAD_UP))
{
//camera.Translate(0,-ONE*4,0);
}
if(joystick.JustPressed(PAD_START))
{
gameState.SetGameState((gameState.GetCurrentGameState() == GameStates::MainMenu ? GameStates::Level01 : GameStates::MainMenu));
}
const int FPS = graphics->FPS();
if(gameState.GetCurrentGameState() == GameStates::Level01 && FPS > 0)
{
static HUD hud;
hud.RenderHUD(graphics, camera.Rotation().vy);
auto diff = 240;
if(FPS >= 30)
diff = diff/FPS;
const int sin = isin(camera.Rotation().vy);
const int cos = icos(camera.Rotation().vy);
const auto lsysin = (LSY*sin*(diff>>1));
const auto lsxcos = (LSX*cos*(diff>>1));
const auto lsycos = (LSY*cos*(diff>>1));
const auto lsxsin = (LSX*sin*(diff>>1));
//if(LSX > 32 || LSX < -32 || LSY > 32 || LSY < -32)
{
mapPosX += (lsysin + lsxcos)>>12;
mapPosZ += (-lsycos + lsxsin)>>12;
}
}
}
}
FntPrint(-1, "FPS=%d\n", graphics->FPS());
camera.Update();//Set rotation and transformation matrix;
const auto& cameraMTX = camera.Matrix();
RotMatrix( &rot, &objectMTX );
TransMatrix( &objectMTX, &pos );
// Composite coordinate matrix transform, so object will be rotated and
// positioned relative to camera matrix (mtx), so it'll appear as
// world-space relative.
CompMatrixLV( &cameraMTX, &objectMTX, &objectMTX );
//MulMatrix0( &light_mtx, &objectMTX, &lightMTX );
//gte_SetLightMatrix( &lightMTX );
gte_SetRotMatrix( &objectMTX );
gte_SetTransMatrix( &objectMTX );
if(gameState.GetCurrentGameState() == GameStates::MainMenu)
{
mainMenu.RenderMainUI(graphics);
}
else if(gameState.GetCurrentGameState() == GameStates::Level01)
{
const auto sunMTX = DrawSkyBox(cameraMTX);
const auto targetY = map->Render(mapPosX, mapPosZ, objectMTX, sunMTX);
const auto camy = camera.Position().vy;
constexpr auto gravity = 24;
if((camy>>12) - targetY > gravity)
camera.Translate(0,-gravity<<12,0);
if((camy>>12) - targetY < -gravity)
camera.Translate(0,gravity<<12,0);
}
FntFlush(-1);
joystick.Update();
graphics->Display<false>();
}
return 0;
}
skysphere sky{.texture = &skysphere_texture};
hills hill{.texture = &hills_texture};
skyUVOffset skyDayOffset;
int skyTickTimer = 3163;
MATRIX DrawSkyBox(const MATRIX& mtx)
{
MATRIX skybox_color_mtx = {
0, 0, 0, /* Red */
0, 0, 0, /* Green */
0, 0, 0 /* Blue */
};
MATRIX skybox_light_mtx = {
/* X, Y, Z */
0 , 0 , 0,
0 , 0, 0,
0 , 0 , 0
};
MATRIX omtx{0};
VECTOR pos{0};
TransMatrix( &omtx, &pos );
gte_SetColorMatrix(&skybox_color_mtx);
gte_SetLightMatrix( &skybox_light_mtx );
/*skyTickTimer++;
if(skyTickTimer > 10)
{
skyTickTimer = 0;
//gte_SetBackColor(r++,g++,b++);
if(r > 255) r = 0;
if(g > 255) g = 0;
if(b > 255) b = 0;
}*/
const auto toAdd = skyDayOffset.getValueToAdd();
for(int i =0; i < 26; ++i)
{
sky.uvs[i].vx += toAdd.vx;
sky.uvs[i].vy += toAdd.vy;
}
skyTickTimer++;
if(skyTickTimer>(4096))
skyTickTimer = 0;
//<<8 = ~30 min complete day cicle
const auto color = AmbientLightController::executeWithTime(skyTickTimer);
gte_SetBackColor(color.r, color.g, color.b);
//FntPrint(-1, "time=%d\n", skyTickTimer );
//FntPrint(-1, "rgb=%d,%d,%d\n", color.r, color.g, color.b);
//FntPrint(-1, "uv=%d,%d\n", sky.uvs[0].vx, sky.uvs[0].vy);
//FntPrint(-1, "offset=%d\n", skyDayOffset.);
PushMatrix();
gte_SetTransMatrix( &omtx );
graphics->DrawObject3D<skysphere>(sky);
graphics->DrawObject3D<hills>(hill);
const auto sunMTX = DrawSun(mtx);
PopMatrix();
return sunMTX;
}
SVECTOR sunRotation{0 , -600, 0};
MATRIX DrawSun(const MATRIX& mtx)
{
constexpr sun mysun{.texture = &sun_texture};
sunRotation.vz--;
if(sunRotation.vz < -4096)
sunRotation.vz = 0;
MATRIX rmtx{0};
RotMatrix(&sunRotation, &rmtx);
MulMatrix0(&mtx, &rmtx, &rmtx);
gte_SetRotMatrix( &rmtx );
graphics->DrawObject3D<sun>(mysun);
return rmtx;
}