-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
146 lines (129 loc) · 4.75 KB
/
Copy pathmain.cpp
File metadata and controls
146 lines (129 loc) · 4.75 KB
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
#include <iostream>
#include <mujs.h>
#include <filesystem>
#include <fstream>
#include <raylib.h>
#include <unordered_map>
#include <jsFuncs.hpp>
#include <chrono>
#include <multiPlayer.hpp>
#include <string>
#include <templates.hpp>
#include "vendor/r3d/include/r3d/r3d.h"
#ifdef noserial
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
#endif
using namespace std::chrono_literals;
int main(const int argc, char **argv) {
js_State *runtime = js_newstate(nullptr, nullptr, 0);
setupRaylibFuncs(runtime);
std::string scriptPath = "script.js";
if (argc > 1) scriptPath = argv[1];
//R3D_LoadModel("someModel.glb");
//Create a script template
if (!std::filesystem::exists(scriptPath)) create_script_template(scriptPath.c_str());
//Create the function declerations
if (!std::filesystem::exists(".d.ts")) create_dts_template();
if (js_dofile(runtime, scriptPath.c_str())) {
std::cerr << "Error while running script\n";
return -1;
}
//Setup args
argCount = argc;
args = argv;
// Function on init
// App start, Raylib not initilaized
js_dostring(runtime, "onStart();");
std::cout << "Called onStart" << std::endl;
#ifdef multiplayer
if (!g_headLessMode)
#endif
InitWindow(1080, 720, "Hello world");
R3D_Init(1080, 720);
R3D_SetAntiAliasingMode(R3D_ANTI_ALIASING_MODE_FXAA);
// ------------------------------------------------------- SKY
// Define procedural skybox parameters
auto skyParams = R3D_PROCEDURAL_SKY_BASE;
skyParams.groundEnergy = 2.0f;
skyParams.skyEnergy = 0.5f;
skyParams.sunEnergy = 0.5f;
/*
* Skybox from: https://polyhaven.com/a/citrus_orchard_puresky
*/
const R3D_Cubemap cubemap = R3D_LoadCubemap("models/citrus_orchard_puresky_2k.hdr", R3D_CUBEMAP_LAYOUT_AUTO_DETECT);
//const R3D_Cubemap skyProcedural = R3D_GenProceduralSky(512, skyParams);
R3D_GetEnvironment()->tonemap.white = 4.0f;
const R3D_Light light = R3D_CreateLight(R3D_LIGHT_DIR);
R3D_SetLightDirection(light, (Vector3) {1, -.5, 1});
R3D_SetLightActive(light, true);
R3D_SetLightEnergy(light, 1);
//R3D_ENVIRONMENT_SET(background.sky, cubemap);
R3D_GetEnvironment()->background.sky = cubemap;
// Setup environment ambient
const R3D_AmbientMap ambientMap = R3D_GenAmbientMap(cubemap, R3D_AMBIENT_ILLUMINATION | R3D_AMBIENT_REFLECTION);
R3D_ENVIRONMENT_SET(ambient.map, ambientMap);
R3D_ENVIRONMENT_SET(ambient.color, DARKGRAY);
// ------------------------------------------------------- SKY
defaultMaterial = R3D_GetDefaultMaterial();
defaultMaterial.albedo.color = {200, 200, 200, 255};
defaultMaterial.unlit = false;
const auto cube = R3D_GenMeshCube(.25, .25, .25);
g_vR3DMeshes.push_back(cube);
// Onready function (after window creation)
js_dostring(runtime, "onReady();");
size_t fileModTime = GetFileModTime(scriptPath.c_str());
while (!WindowShouldClose() || g_headLessMode) {
if (IsWindowResized()) {
R3D_SetResolution(GetScreenWidth(), GetScreenHeight());
}
//Hot reload
if (GetFileModTime(scriptPath.c_str()) != fileModTime) {
std::cout << (js_dofile(runtime, scriptPath.c_str()) ? "Failed to reload" : "Successfully reloaded") << std::endl;
fileModTime = GetFileModTime(scriptPath.c_str());
}
js_dostring(runtime, "onFrame();");
#ifdef multiplayer
// Networking update
std::string onMsgCall = "onMessage(\"";
bool gotMsg = false;
switch (g_eNetMode) {
case NETWORK_MODE_HOST:
g_nServer.Listen();
if (g_nServer.messageAvailable()) {
onMsgCall+=g_nServer.getMessage() + "\");";
gotMsg = true;
//std::clog << onMsgCall << std::endl;
}
break;
case NETWORK_MODE_CLIENT:
g_nClient.Poll();
if (g_nClient.messageAvailable()) {
onMsgCall+=g_nClient.getMessage() + "\");";
gotMsg = true;
//std::clog << onMsgCall << std::endl;
}
break;
default:
break;
}
if (g_eNetMode != NETWORK_MODE_SINGLEPLAYER && gotMsg) {
//std::cout << "Running onMessage(): " << onMsgCall << std::endl;
js_dostring(runtime, onMsgCall.c_str());
}
#endif
}
#ifdef multiplayer
switch (g_eNetMode) {
case NETWORK_MODE_HOST:
g_nServer.Cleanup();
break;
case NETWORK_MODE_CLIENT:
g_nClient.Cleanup();
break;
default:
break;
}
if (!g_headLessMode) CloseWindow();
#endif
return 0;
}