-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
116 lines (88 loc) · 3.01 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
#include <wrenbind17/wrenbind17.hpp>
#include <ctime>
#include <iostream>
#include <sstream>
#include <fstream>
#include <filesystem>
#include <tinyfiledialogs.h>
#include <raylib.h>
namespace Wren = wrenbind17;
std::ofstream LogFile;
void CustomLog(int LOG_LEVEL, const char * Text, va_list Args);
void BindRaylib(Wren::VM & VM);
void BindRaymath(Wren::VM & VM);
void BindEngine(Wren::VM & VM);
void LoadPlugins(Wren::VM & VM);
#define FileNotFound(File) Wren::Exception(std::string("[") + File + std::string("] was not found."))
int main() {
LogFile.open("./Log.txt");
if(!LogFile.is_open()) {
TraceLog(LOG_WARNING, "Unable to open log file!");
TraceLog(LOG_WARNING, "Printing only to stderr!");
}
SetTraceLogCallback(CustomLog);
TraceLog(LOG_INFO, "Initializing Cube2D Engine");
if(!FileExists("main.wren")) {
TraceLog(LOG_ERROR, "[%s] was not found.", "main.wren");
tinyfd_messageBox("Cube2D - An Error Occured" , "[main.wren] was not found." , "ok" , "error", 1);
return EXIT_FAILURE;
}
TraceLog(LOG_INFO, "Initializing Wren::VM");
Wren::VM VM;
VM.setPrintFunc([](const char * Text){
std::clog << ((strlen(Text) != 1) ? "[WREN] " : "") << Text;
LogFile << ((strlen(Text) != 1) ? "[WREN] " : "") << Text;
});
VM.setLoadFileFunc([](
const std::vector<std::string>&,
const std::string& Name) -> std::string {
std::string File;
Name != "main.wren" ? File = "Scripts/" + (IsFileExtension(Name.c_str(), ".wren") ? Name : (Name + ".wren"))
: File = Name;
if(!FileExists(File.c_str())) {
std::string Error = "[" + File + "] was not found.";
TraceLog(LOG_ERROR, Error.c_str());
tinyfd_messageBox("Cube2D - An Error Occured", Error.c_str(), "ok", "error", 1);
return "";
}
char * Text = LoadFileText(File.c_str());
Text = TextReplace(Text, "import \"Engine\"", "import \"Engine\" for Rect, Scene, SceneManager, Tools, Shared");
std::string ToReturn = Text;
UnloadFileText(Text);
return ToReturn;
});
// Bind everything
BindRaylib(VM);
BindRaymath(VM);
BindEngine(VM);
// LoadPlugins(VM);
InitWindow(640, 480, "Cube2D");
InitAudioDevice();
SetExitKey(KEY_NULL);
std::string Icon = "Resources/Icon.png";
if(FileExists(Icon.c_str())) SetWindowIcon(LoadImage(Icon.c_str()));
try {
VM.runFromModule("main.wren");
} catch(wrenbind17::Exception& Error) {
TraceLog(LOG_ERROR, Error.what());
tinyfd_messageBox("Cube2D - An Error Occured", Error.what(), "ok", "error", 1);
}
CloseAudioDevice();
CloseWindow();
if(LogFile.is_open()) LogFile.close();
return EXIT_SUCCESS;
}
void CustomLog(int LOG_LEVEL, const char * Text, va_list Args) {
std::string Tag;
switch(LOG_LEVEL) {
case LOG_INFO: Tag = "[INFO] "; break;
case LOG_ERROR: Tag = "[ERROR] "; break;
case LOG_WARNING: Tag = "[WARN] "; break;
case LOG_DEBUG: Tag = "[DEBUG] "; break;
default: break;
}
char Buffer[1024];
vsnprintf(Buffer, sizeof(Buffer), Text, Args);
std::clog << Tag << Buffer << std::endl;
if(LogFile.is_open()) LogFile << Tag << Buffer << std::endl;
}