-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
512 lines (399 loc) · 16.5 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
---------------------------------------------------------------------------------
| Includes |
---------------------------------------------------------------------------------
*/
#pragma once
#include "Math.h"
#include "techstorm/common.h"
#include "techstorm/core/utils/Button.h" // TODO : Can this be moved to common.h?
// Game Engine Includes
#include "techstorm/Game.h"
#include "techstorm/globalobj/Block.h"
#include "techstorm/core/obj/Gameobject.h"
#include "techstorm/core/gamescreen/MenuCamera.h"
#include "techstorm/player/Player.h"
#include "techstorm/core/rendering/Light.h"
#include "techstorm/core/threading/ThreadGroups.h"
#include "techstorm/core/ui/UIElement.h"
#include "techstorm/core/ui/UIMan.h"
#include "techstorm/ui/UIFadingMsg.h"
#include "techstorm/core/audio/FxMan.h"
#include "techstorm/globalobj/Objects.h"
#include "techstorm/console/Console.h"
#include "techstorm/core/ui/UIMenu.h"
#include "techstorm/ui/MainMenu.h"
#include "techstorm/ui/PlayerHUD.h"
#include "techstorm/economy/SystemEconomy.h"
#include "techstorm/discord/DiscordAPI.h"
#include <time.h>
#include <vector> // needed for game object list
#include <raylib.h>
#include <lua.hpp>
#include <sol/sol.hpp>
//config
#include <toml++/toml.hpp>
#include <tinyxml2.h>
#define NUM_MODELS 9 // Parametric 3d shapes to generate
namespace TechStormRivalry {
static void mainThread() {
// Lets the program know if the game should use HDR as the skybox
bool useHDR = false;
// Initialie the condition for the breathing sound.
bool isBreathing = false;
// Viewsway
float swayAmount = 0.0003f; // how much should the view sway be affected.
float swaySpeed = 0.02f; // how fast should camera sway
float swayTimer = 0.0f; // Dont mess with this. it will fuck up the swaying.
/*
* Near death affect.
* Important :
* THESE ARE REQUIRED FOR THE ARCH ALGORITHM TO WORK!
* See Math.h for the Arch algorithm explanation and uses.
*
* Note:
* Use a graphing calculator to visualize the algorithm
*/
float nearDeathTimer = 0.0f; // The X axis
float amplitude = 255; // How high the Parabola goes
float frequency = 5; // How often the arches are
float steepness = 2.4f; // How steep the curve is, NOTE : If it is an odd number, it will not show as it only works for even times.
float offset = 1; // Offset of the arches from x = 0
const float scaleFactor = 25; // Scaling factor, how much the parabolas is scaled
float nearDeathIntensity = 0.0f; // The Y Axis of the algorithm
// Health bar fuckery
float healthBarPositionX = 0.0f;
float healthBarPositionY = 0.0f;
int healthBarOffsetX = 0;
int healthBarOffsetY = 500;
// represents the raycasting from the player to the world.
Ray ray;
// Create game & load it into memory.
Game* game = new Game();
// Start the game and do any needed setup
game->init();
DiscordAPI* disc = new DiscordAPI();
disc->initAPI();
TechStorm::Logman::Log("Main menu ready");
Console::ConsoleUI* console = new Console::ConsoleUI();
MainMenu::MainMenu* menu = new MainMenu::MainMenu(game);
menu->awakeMenu();
menu->drawMenu(console);
console = new Console::ConsoleUI();
game->pushRogueElement(console);
// Assign already setup PBR shader to model.materials[0], used by models.meshes[0]
// Create a default block model with a length, width, and height of the value of BLOCK_SIZE.
// This creates a cube mesh with a length, width, and height of BLOCK_SIZE and then converts it
// to a model, loading it into memory.
Model DefaultBlockModel = LoadModelFromMesh(
GenMeshCube(BLOCK_SIZE, BLOCK_SIZE,
BLOCK_SIZE)); // this is the default model used in blocksd.
// todo : change all use instances of DefaultBlockModel to use the global default model.
SetDefaultModel(DefaultBlockModel);
// Construct the block object
Block* block = new Block(Vector3Zero(), WHITE, game->pbrShader, LoadModelFromMesh(GenMeshCylinder(BLOCK_SIZE, BLOCK_SIZE, 100)));
// Finally push it off to the object manager
game->pushObject(block);
// Create the player model
// NOTE: This model is TEMPORARY! It will be deleted after a proper model is made. Refers to the
// size of the player model. Same process as the default block model creation.
const int Playersize = 3;
Model PlayerModel = LoadModelFromMesh(GenMeshCube(Playersize, Playersize, Playersize));
// Construct the player object
Player::Player* player = new Player::Player(Vector3{ 0.0f, 2.0f, 4.0f }, PlayerModel, CAMERA_FIRST_PERSON);
// Finally push it off to the object manager
game->pushObject(player);
// Skybox geometry generation step
Mesh skyboxMesh = GenMeshCube(1.0f, 1.0f, 1.0f);
// Skybox model loading step
Model skybox = LoadModelFromMesh(skyboxMesh);
// Load skybox shader and set required locations step
// NOTE: Some locations are automatically set at shader loading
skybox.materials[0].shader = LoadShader("resources/skybox.vs", "resources/skybox.fs");
// since the compiler complains about the references of such, these three vars are for the
// skybox shaders. They will be deleted after.
static int skyboxEnvironmentMap = MATERIAL_MAP_CUBEMAP;
// Both variables are equal to 1 if useHDR is true, otherwise 0
static int skyboxUsesGamma = { useHDR ? 1 : 0 };
static int skyboxIsVFlipped = { useHDR ? 1 : 0 };
// Load the environment map
SetShaderValue(
skybox.materials[0].shader,
GetShaderLocation(skybox.materials[0].shader, "environmentMap"), &skyboxEnvironmentMap,
SHADER_UNIFORM_INT);
// Should the shader use gamma correction
SetShaderValue(skybox.materials[0].shader,
GetShaderLocation(skybox.materials[0].shader, "doGamma"), &skyboxUsesGamma,
SHADER_UNIFORM_INT);
// Is the skybox flipped vertically
SetShaderValue(skybox.materials[0].shader,
GetShaderLocation(skybox.materials[0].shader, "vflipped"), &skyboxIsVFlipped,
SHADER_UNIFORM_INT);
// Load cubemap shader and setup required shader locations
Shader shdrCubemap =
LoadShader("resources/cubemap.vs", "resources/cubemap.fs");
// set shdrCubemap's equirectangular map.
const static int equimap = 0;
SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), &equimap, SHADER_UNIFORM_INT);
// Note: The filename is hardcoded here and also has a limit of 256 characters! I will be
// dissapointed if you somehow dont know what that variable does...
char skyboxFileName[256] = { 0 };
// This is the panoramic texture, It is used IF useHDR is true
Texture2D panorama = { 0 };
TextureCubemap cubemap = { 0 };
Image img;
if (useHDR)
{
TextCopy(skyboxFileName, "resources/milkyWay.hdr");
// Load HDR panorama (sphere) texture
//panorama = LoadTexture(skyboxFileName);
Image pan = LoadImage(skyboxFileName);
printf(TextFormat("Format %i", pan.format));
//ImageFormat(&pan, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
//panorama = LoadTextureFromImage(pan);
//skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = GenTextureCubemap(
// shdrCubemap, panorama, 1024, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
}
else
{
// it isnt going to be used. so it can fuck off.
UnloadTexture(panorama);
// Load non HDR panorama (cube) texture
img = LoadImage("resources/textures/space.png");
// Set it's cubemap texture.
skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = LoadTextureCubemap(
img, CUBEMAP_LAYOUT_AUTO_DETECT); // CUBEMAP_LAYOUT_PANORAMA
// unload img as we dont need it anymore.
}
TechStorm::Light sun = TechStorm::CreateLight(TechStorm::LIGHT_DIRECTIONAL, TechStorm::uVec3f{ 10.0f, 10.0f, 10.0f }, Vector3One(), WHITE, 4.0f, game->pbrShader);
// The texture for the near death affect initialization
Texture2D nearDeathTex = { 0 };
// The image for the near death affect initialization
Image nearDeathAffect = { 0 };
// The color for the near death affect initialization
TechStorm::uColor nearDeathColor = TechStorm::uColor();
healthBarPositionX = game->winWidth + healthBarOffsetX;
healthBarPositionY = game->winHeight + healthBarOffsetY;
DisableCursor();
PlayerHUD* hud = new PlayerHUD(game);
Economy::MarketProduct tester = Economy::MarketProduct();
tester.baseValue = 5;
tester.supply = 100;
Economy::Merchant* merchant = new Economy::Merchant();
merchant->m_market[0] = tester;
merchant->m_priceUptick = 0.25f;
Faction::CharacterFaction fa = Faction::CharacterFaction();
Faction::CharacterFaction fb = Faction::CharacterFaction();
merchant->m_owner = &fb;
fa.meetFaction(fb);
fb.meetFaction(fa);
SetTargetFPS(60);
while (!WindowShouldClose())
{
int scroll = GetMouseWheelMove();
disc->update();
// Camera PRO usage example (EXPERIMENTAL)
// This new camera function allows custom movement/rotation values to be directly provided
// as input parameters, with this approach, rcamera module is internally independent of raylib inputs
UpdateCameraPro(player->cameraComponent->getSelfCameraPointer(),
Vector3{
(IsKeyDown(player->controller->forward) || IsKeyDown(KEY_UP)) * player->controller->speed - // Move forward-backward
(IsKeyDown(player->controller->backward) || IsKeyDown(KEY_DOWN)) * player->controller->speed,
(IsKeyDown(player->controller->right) || IsKeyDown(KEY_RIGHT)) * player->controller->speed - // Move right-left
(IsKeyDown(player->controller->left) || IsKeyDown(KEY_LEFT)) * player->controller->speed,
(IsKeyDown(player->controller->jump)) * player->controller->speed - // Move up-down
(IsKeyDown(player->controller->crouch)) * player->controller->speed
},
Vector3{
GetMouseDelta().x * player->controller->mouseSensitivity, // Rotation: yaw
GetMouseDelta().y * player->controller->mouseSensitivity, // Rotation: pitch
player->controller->CalculateCameraTilt() // Rotation: roll
},
GetMouseWheelMove() * 2.0f); // Move to target (zoom)
// NOTE : This is a very basic implementation of placing an object into the world. This is
// temporary and should be fleshed out.
if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
{
Vector3 placepos = player->cameraComponent->getTarget();
}
if (IsKeyPressed(KEY_ESCAPE))
{
// Todo, move the menuCamera to be created on game startup and then hidden. it gets
// shown on if statement validation
bool exitMenu = false;
bool manualExit = false;
MenuCamera* men_pause = new MenuCamera();
TechStorm::ButtonR* save = new TechStorm::ButtonR("Save", 100, 100);
TechStorm::ButtonR* Options = new TechStorm::ButtonR("Options", 100, 200);
TechStorm::ButtonR* exitButton = new TechStorm::ButtonR("Exit to Windows", 100, 300);
EnableCursor();
while (!exitMenu)
{
game->updateUI();
BeginDrawing();
game->drawUI(DRAW_FINAL);
save->draw();
save->updateObjects();
Options->draw();
Options->updateObjects();
exitButton->draw();
exitButton->updateObjects();
if (exitButton->IsClicked())
{
manualExit = true;
}
if (Options->IsClicked())
{
bool closeOptions = false;
while (!closeOptions)
{
// TODO : Add options
if (IsKeyDown(KEY_ESCAPE))
{
closeOptions = true;
}
}
}
EndDrawing();
if (IsKeyPressed(KEY_ESCAPE) && exitMenu == false)
{
exitMenu = true;
}
else if (IsKeyPressed(KEY_ESCAPE) && exitMenu == true)
{
break;
}
if (manualExit)
{
exitMenu = true;
}
}
// cleanup UnloadTexture(tex);
delete men_pause;
delete save;
delete exitButton;
if (manualExit)
{
TechStorm::Logman::customLog(LOG_INFO, "Exiting Game", NULL);
exit(0);
}
}
if (IsKeyPressed(KEY_E)) {
}
if (IsKeyDown(KEY_Y)) {
player->damage(15);
}
if (IsKeyPressed(KEY_B)) {
merchant->buy(0, 5, fa);
TechStorm::Logman::Log(TextFormat("Supply %i, Demand %i, buy price %f, sell %f, base %i", merchant->m_market[0].supply, merchant->m_market[0].demand, merchant->m_market[0].buyPrice, merchant->m_market[0].sellPrice, merchant->m_market[0].baseValue));
}
if (IsKeyPressed(KEY_F9))
{
// take a screenshot
for (int i = 0; i < INT_MAX; i++)
{
const char* fileName = TextFormat("screen%i.png", i);
if (FileExists(fileName) == 0)
{
ExportImage(LoadImageFromTexture(game->getFBO().texture), fileName);
break;
}
}
}
if (IsKeyPressed(KEY_K)) {
Economy::MarketPurchase purchase = merchant->sell(0, 500, fa);
//player->m_balance += purchase.value;
TechStorm::Logman::Log(TextFormat("Supply %i, Demand %i, buy price %f, sell %f, base %i", merchant->m_market[0].supply, merchant->m_market[0].demand, merchant->m_market[0].buyPrice, merchant->m_market[0].sellPrice, merchant->m_market[0].baseValue));
}
// The player's looking direction is the target of the camera. This is the direction the
// player is looking TODO : Check relevancy.
ray.position = player->cameraComponent->getPosition();
ray.direction = player->cameraComponent->getTarget();
// Sway timer is equal to swayTimer + frame delta time
swayTimer += GetFrameTime();
// sway the camera according to the sway algorithm.
player->cameraComponent->setTarget(Vector3{ player->cameraComponent->getTarget().x + sin(swayTimer * swaySpeed) * swayAmount, player->cameraComponent->getTarget().y, player->cameraComponent->getTarget().z + cos(swayTimer * swaySpeed) * swayAmount });
/*
* if (player->health <= 15) {
// equal to nearDeathTimer + frame delta time
//nearDeathTimer += GetFrameTime();
if (nearDeathIntensity >= 255) {
nearDeathIntensity -= nearDeathIntensity / 2;
}
else if ((nearDeathIntensity > 0) && (nearDeathIntensity < 255)) {
nearDeathIntensity += nearDeathIntensity / 2;
}
Image img = LoadImageFromTexture(game->renderer->getRenderTexture().texture);
nearDeathColor = Color{ 255, 0, 0, (unsigned char)nearDeathIntensity };
ImageColorTint(&img, nearDeathColor);
Color* pixels = LoadImageColors(img);
UpdateTexture(game->renderer->getRenderTexture().texture, pixels); // Update texture with new image data
UnloadImageColors(pixels); // Unload pixels data from RAM
UpdateTexture(game->renderer->getRenderTexture().texture, pixels);
}
*/
// Stashes input events for later
PollInputEvents(); // helps for some reason?
sun.position = player->cameraComponent->getTarget();
TechStorm::UpdateLight(game->pbrShader, sun);
game->updateObjects();
game->startTexturingStep(player->cameraComponent->getSelfCameraPointer());
game->updateUI();
game->drawUI(DRAW_CLIPPABLE);
//DrawTextureRec(nearDeathTex, Rectangle{ 0, 0, (float)(game->renderer->fboDimensions.x), (float)(-game->renderer->fboDimensions.y) }, TechStorm::uVec2i{ 0, 0 }, WHITE);
game->startRenderingStep(player->cameraComponent->getSelfCamera());
SetShaderValue(game->pbrShader, game->textureTilingLoc, &block->blockTextureTiling, SHADER_UNIFORM_VEC2);
Vector4 carEmissiveColor = ColorNormalize(block->model.materials[0].maps[MATERIAL_MAP_EMISSION].color);
SetShaderValue(game->pbrShader, game->emissiveColorLoc, &carEmissiveColor, SHADER_UNIFORM_VEC4);
// How bright should the object emit it's emission color.
float emissiveIntensity = 0.1f;
SetShaderValue(game->pbrShader, game->emissiveIntensityLoc, &emissiveIntensity, SHADER_UNIFORM_FLOAT);
// We are inside the cube, we need to disable backface culling!
rlDisableBackfaceCulling();
rlDisableDepthMask();
DrawModel(skybox, Vector3{ 0, 0, 0 }, 1.0f, WHITE);
rlEnableBackfaceCulling();
rlEnableDepthMask();
game->renderObjects();
//game->renderObjects();
game->endRenderingStep();
game->endTexturingStep();
game->startDrawingstep();
game->drawUI(DRAW_FINAL);
game->endDrawingStep();
}
UnloadModel(DefaultBlockModel);
// Unload textures
UnloadTexture(nearDeathTex);
// Unload images
UnloadImage(nearDeathAffect);
// Delete temporary files.
using namespace std;
{
remove("PerlinTest.png");
remove("HMap.png");
}
game->assets->unloadAssets();
game->endGame();
//rCloseWindow();
delete game;
delete block;
//delete man;
delete player;
}
}
int main()
{
try
{
// Run the game
std::thread t(std::move(TechStormRivalry::mainThread));
//scriptManager->startScriptMan(true);
t.join();
}
catch (const std::exception& e)
{
TechStorm::Logman::Log(e.what());
}
return 0;
}