-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
120 lines (101 loc) · 3.76 KB
/
Copy pathmain.cpp
File metadata and controls
120 lines (101 loc) · 3.76 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
#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_getglobal(runtime, "onStart");
js_pushnull(runtime);
if (js_pcall(runtime, 0)) {
fprintf(stderr, "an exception occurred in the javascript callback\n");
js_pop(runtime, 1);
} else {
js_pop(runtime, 1);
}
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_getglobal(runtime, "onFrame");
js_pushnull(runtime);
if (js_pcall(runtime, 0)) {
fprintf(stderr, "an exception occurred in the javascript callback\n");
js_pop(runtime, 1);
} else {
js_pop(runtime, 1);
}
}
return 0;
}