-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
136 lines (98 loc) · 2.95 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
#include <stdlib.h>
#include <dlfcn.h>
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "ofAppGlutWindow.h"
#include "ofMain.h"
ofAppGlutWindow window;
void *livecodeLib = NULL;
string livefile = "default.cpp";
ofBaseApp *app = new ofBaseApp();
void reload() {
// don't forget to clean up
if(livecodeLib!=NULL) {
app->exit();
delete app;
dlclose(livecodeLib);
}
livecodeLib = dlopen("livecode.dylib", RTLD_LAZY);
if (livecodeLib == NULL) {
// report error ...
printf("Error: No dice loading livecode.dylib\n");
} else {
// use the result in a call to dlsym
printf("Success loading\n");
void *ptrFunc = dlsym(livecodeLib, "getAppPtr");
if(ptrFunc!=NULL) {
app = ((ofBaseApp *(*)(ofAppBaseWindow&))ptrFunc)(window);
app->setup();
} else {
printf("Couldn't find the getAppPtr() function\n");
}
}
}
void recompileAndReload() {
long t = ofGetSystemTime();
// call our makefile
string cmd = "make live LIVEFILE=";
cmd += livefile;
system(cmd.c_str());
reload();
printf("Reload took %ldms\n", ofGetSystemTime() - t);
}
long prevUpdateTime = 0;
void checkAndUpdate() {
struct stat fileStat;
if(stat(livefile.c_str(), &fileStat) < 0) {
printf("Couldn't stat file\n");
return;
}
long currUpdateTime = fileStat.st_mtime;
if(currUpdateTime!=prevUpdateTime) {
printf("Compile reload\n");
recompileAndReload();
}
prevUpdateTime = currUpdateTime;
}
float lastTimeChecked = 0;
void idle(void) {
//long t = ofGetSystemTime();
float f = ofGetElapsedTimef();
// check update time on the file
if(f>lastTimeChecked+0.1) { // check every 300ms
checkAndUpdate();
lastTimeChecked = f;
}
}
class forwarderApp: public ofBaseApp {
virtual ~forwarderApp(){}
void setup(){ app->setup(); }
void update(){ idle(); app->update(); }
void draw(){ app->draw(); }
void exit(){ app->exit(); }
void windowResized(int w, int h){app->windowResized(w, h); }
void keyPressed( int key ){ app->keyPressed(key); }
void keyReleased( int key ){app->keyReleased(key); }
void mouseMoved( int x, int y ){ app->mouseMoved(x, y); }
void mouseDragged( int x, int y, int button ){ app->mouseDragged(x, y, button); }
void mousePressed( int x, int y, int button ){ app->mousePressed(x, y, button); }
void mouseReleased(){ app->mouseReleased(); }
void mouseReleased(int x, int y, int button ){ app->mouseReleased(x, y, button); }
void dragEvent(ofDragInfo dragInfo) { app->dragEvent(dragInfo); }
void gotMessage(ofMessage msg){ app->gotMessage(msg); }
};
int main(int argc, char** argv) {
// if there's an argument,
if(argc>1) {
livefile = argv[1];
printf("Using live file '%s'\n", argv[1]);
} // otherwise using default
ofSetupOpenGL(&window, 1024,768, OF_WINDOW); // <-------- setup the GL context
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp(new forwarderApp());
return EXIT_SUCCESS;
}