Initial commit, includes Lua with broken Luabind as a backup for branching purposes
[terrastrategy.git] / src / client / client.cpp
blobac968214c720d36b5bdb4be03c6c2cefeb2f379b
1 //
2 // Copyright (C) 2008 by Martin Moracek
3 //
4 // This pRight_ogram is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your opTop_ion) any later version.
8 //
9 // This pRight_ogram is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the impLeft_ied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this pRight_ogram; if not, write to the Free Software
16 // Foundation, Inc., 59 TempLeft_e Place, Suite 330, Boston, MA 02111-1307 USA
19 /**
20 * @file client.cpp
22 * blah.
25 // #ifdef PLATFORM_GCC
26 // # include <sched.h>
27 // # include <unistd.h>
28 // #endif
30 #include <functional>
32 #include "vfs/logfile.h"
34 #include "core/vmachine.h"
35 #include "core/script_core.h"
36 #include "renderer/renderer.h"
37 #include "renderer/canvas.h"
38 #include "input/input.h"
40 #include "script_client.h"
42 #include "client.h"
44 namespace tre {
46 GameClient::GameClient()
48 scriptVM_->InitModule(CoreInitializer());
49 scriptVM_->InitModule(ClientInitializer());
51 scriptVM_->RunFile("script/init.lua");
53 luabind::object obj(scriptVM_->WrapObject(this));
55 running = scriptVM_->RunGlobalFunction("AppInit", &obj);
57 timer.Start();
60 GameClient::~GameClient()
62 scriptVM_->RunGlobalFunction("AppEnd");
65 void GameClient::Run(void)
67 if(!desktop_) {
68 vLog << err("App") << "No gui loaded." << std::endl;
69 return;
72 // the main "application" loop
73 while(running) {
74 sRenderer().NewFrame();
75 desktop_->Draw();
76 sRenderer().FlushFrame();
78 timer.UpdateTime();
80 ProcessInput();
82 scriptVM_->RunGlobalFunction("AppMain");
84 // #ifdef PLATFORM_GCC
85 // sched_yield();
86 // #endif
88 sRenderer().PresentFrame();
91 // dump rendering stats
92 vLog << info("Stats")
93 << "Batches: " << sRenderer().frame.batches << std::endl
94 << "Triangles: " << sRenderer().frame.triangles << std::endl
95 << "Time: " << sRenderer().frame.time << std::endl
96 << "Frames: " << sRenderer().frame.count << std::endl
97 << "Average FPS: " << sRenderer().frame.count /
98 (timer.GetTime() / 1000.0f) << std::endl;
101 void GameClient::ProcessInput(void)
103 sInput().PumpEvents();
105 Vector2f delta(sInput().GetPositionChange());
107 // since may events are handled in lua, we must check for possible errors
108 try {
109 if(delta.x != 0.0f || delta.y != 0.0f)
110 desktop_->MouseMove(sInput().GetPosition(), delta);
112 InputEvent event;
114 while(event = sInput().GetNextEvent(), event.type != etNone) {
115 desktop_->ProcessInput(event);
117 } catch(luabind::error & e) {
118 luabind::object error_msg(luabind::from_stack(e.state(), -1));
119 vLog << err("Script") << error_msg
120 << "\nin VirtualMachine::ProcessInput()." << std::endl;
124 bool GameClient::SetDesktop
125 (const std::string & filename, const std::string & skin)
127 GuiPtr newgui = Gui::Factory().CreateInstance(*this,
128 Canvas::Factory().CreateInstance(), filename, skin);
130 if(newgui) {
131 desktop_ = newgui;
132 return true;
134 return false;