Works under windows with ECL-8.12.0
[Procustean.git] / src / App.cpp
blobf88370e3ba16a613e016693bd9550addf5bc5eaa
1 /**
2 @file App.cpp
3 */
4 #include "App.h"
5 #include "ecl/ecl.h"
6 #include "Entity.h"
8 extern cl_object g_readErrorSymbol;
9 extern cl_object g_evalErrorSymbol;
10 extern cl_object g_eval_handler;
12 extern bool isError(cl_object obj, const char ** errorMessage);
13 extern cl_object safeRead(const std::string& strCmd);
15 App::App(const GApp::Settings& settings) : GApp(settings) {
16 // Uncomment the next line if you are running under a debugger:
17 //catchCommonExceptions = false;
19 // Uncomment the next line to hide the developer tools:
20 developerWindow->setVisible(true);
23 void App::onInit() {
24 // Called before the application loop beings. Load data here and
25 // not in the constructor so that common exceptions will be
26 // automatically caught.
27 sky = Sky::fromFile(dataDir + "sky/");
29 skyParameters = SkyParameters(G3D::toSeconds(11, 00, 00, AM));
30 lighting = Lighting::fromSky(sky, skyParameters, Color3::white());
32 // This simple demo has no shadowing, so make all lights unshadowed
33 lighting->lightArray.append(lighting->shadowedLightArray);
34 lighting->shadowedLightArray.clear();
36 turtle = new Turtle();
38 // Example debug GUI:
39 //debugPane->addCheckBox("Use explicit checking", &explicitCheck);
40 //debugPane->addButton("AMC/ASF");
41 //debugWindow->setVisible(true);
42 toneMap->setEnabled(false);
44 cl_load(1, make_simple_base_string("init.lisp"));
47 void App::onLogic() {
48 // Add non-simulation game logic and AI code here
51 void App::onNetwork() {
52 // Poll net messages here
55 void App::onSimulation(RealTime rdt, SimTime sdt, SimTime idt) {
56 // Add physical simulation here. You can make your time advancement
57 // based on any of the three arguments.
60 bool App::onEvent(const GEvent& e) {
61 // If you need to track individual UI events, manage them here.
62 // Return true if you want to prevent other parts of the system
63 // from observing this specific event.
64 return false;
67 void App::onUserInput(UserInput* ui) {
68 // Add key handling here based on the keys currently held or
69 // ones that changed in the last frame.
72 void App::onPose(Array<PosedModelRef>& posed3D, Array<PosedModel2DRef>& posed2D) {
73 // Append any models to the array that you want rendered by onGraphics
76 void App::onGraphics(RenderDevice* rd, Array<PosedModelRef>& posed3D, Array<PosedModel2DRef>& posed2D) {
77 Array<PosedModel::Ref> opaque, transparent;
78 LightingRef localLighting = toneMap->prepareLighting(lighting);
79 SkyParameters localSky = toneMap->prepareSkyParameters(skyParameters);
81 toneMap->beginFrame(rd);
82 rd->setProjectionAndCameraMatrix(defaultCamera);
84 rd->setColorClearValue(Color3(0.1f, 0.5f, 1.0f));
85 rd->clear(false, true, true);
86 sky->render(rd, localSky);
88 // Setup lighting
89 rd->enableLighting();
90 rd->setLight(0, localLighting->lightArray[0]);
91 rd->setAmbientLightColor(localLighting->ambientAverage());
93 // Sample immediate-mode rendering code
94 Draw::axes(CoordinateFrame(Vector3(0, 4, 0)), rd);
95 turtle->onGraphics(rd);
97 //Draw::sphere(Sphere(Vector3::zero(), 0.5f), rd, Color3::white());
98 //Draw::box(AABox(Vector3(-3,-0.5,-0.5), Vector3(-2,0.5,0.5)), rd, Color3::green());
100 Entity::renderEntities( rd );
102 // Always render the posed models passed in or the Developer Window and
103 // other Widget features will not appear.
104 if (posed3D.size() > 0) {
105 Vector3 lookVector = renderDevice->getCameraToWorldMatrix().lookVector();
106 PosedModel::sort(posed3D, lookVector, opaque, transparent);
108 for (int i = 0; i < opaque.size(); ++i) {
109 opaque[i]->render(renderDevice);
112 for (int i = 0; i < transparent.size(); ++i) {
113 transparent[i]->render(renderDevice);
116 rd->disableLighting();
118 sky->renderLensFlare(rd, localSky);
119 toneMap->endFrame(rd);
121 PosedModel2D::sortAndRender(rd, posed2D);
124 void App::onConsoleCommand(const std::string& str) {
125 // Add console processing here
127 extern cl_object eval_handler;
129 TextInput t(TextInput::FROM_STRING, str);
130 if (t.hasMore() && (t.peek().type() == Token::SYMBOL)) {
131 std::string cmd = toLower(t.readSymbol());
132 if (cmd == "exit") {
133 setExitCode(0);
134 return;
135 } else if (cmd == "up")
137 turtle->up(t.readNumber());
138 return;
139 } else if (cmd == "down")
141 turtle->up(-t.readNumber());
142 return;
143 } else if (cmd == "left")
145 turtle->left(t.readNumber());
146 return;
147 } else if (cmd == "right")
149 turtle->left(-t.readNumber());
150 return;
151 } else if (cmd == "forward")
153 turtle->forward(t.readNumber());
154 return;
155 } else if (cmd == "back")
157 turtle->forward(-t.readNumber());
158 return;
159 } else if (cmd == "help")
161 printConsoleHelp();
162 return;
163 } else {
165 cl_object cl_str = make_simple_base_string((char *) str.c_str());
166 cl_object result = funcall(4, g_eval_handler, cl_str, Cnil, g_evalErrorSymbol);
167 cl_terpri(0);
168 cl_princ(1,result);
169 cl_terpri(0);
170 return;
172 // Add commands here
175 console->printf("Unknown command\n");
176 printConsoleHelp();
179 void App::printConsoleHelp() {
180 console->printf("exit - Quit the program\n");
181 console->printf("help - Display this text\n\n");
182 console->printf("~/ESC - Open/Close console\n");
183 console->printf("F2 - Enable first-person camera control\n");
186 void App::onCleanup() {
187 // Called after the application loop ends. Place a majority of cleanup code
188 // here instead of in the constructor so that exceptions can be caught