-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraylib-duktape.cpp
71 lines (57 loc) · 1.85 KB
/
raylib-duktape.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
#include <iostream>
#include <string>
#include "raylib.h"
#include <duktape.h>
//#include <console/duk_console.h>
#include "../lib/raylib-duktape.h"
#include <dukglue/dukglue.h>
int main(int argc, char *argv[]) {
std::string executableName;
std::string fileToLoad;
SetTraceLogLevel(LOG_FATAL);
switch (argc) {
case 0:
executableName = "raylib-duktape";
fileToLoad = "main.js";
break;
case 1:
executableName = argv[0];
fileToLoad = "main.js";
break;
default:
executableName = argv[0];
fileToLoad = argv[1];
break;
}
if (!FileExists(fileToLoad.c_str())) {
std::cout << "Usage:" << std::endl << " " << GetFileName(executableName.c_str()) << " myfile.js" << std::endl << std::endl;
std::cout << "Attempted file " << fileToLoad << " was not found." << std::endl;
return 1;
}
if (!IsFileExtension(fileToLoad.c_str(), ".js")) {
std::cout << "Expected file to be a .js file." << std::endl;
return 1;
}
char* fileText = LoadFileText(fileToLoad.c_str());
std::string contents(fileText);
UnloadFileText(fileText);
if (contents.empty()) {
std::cout << "File was empty." << std::endl;
return 1;
}
// Make sure it runs in the correct folder.
if (!ChangeDirectory(GetDirectoryPath(fileToLoad.c_str()))) {
TraceLog(LOG_WARNING, "Failed to change directory");
}
// Construct the Duktape environment.
duk_context* ctx = duk_create_heap_default();
// Initialize the console module.
//duk_console_init(ctx, 0);
// Initialize the raylib module.
duk_raylib_init(ctx, 0);
// Eval the loaded code.
dukglue_peval<void>(ctx, contents.c_str());
// Destroy the environment.
duk_destroy_heap(ctx);
return 0;
}