add static Engine::graphicsEnabled function and disable player graphics loading when false
authorterom
Sat, 06 Dec 2008 14:20:01 +0000
changeset 218 86d22fe82b30
parent 217 de56d9d16705
child 219 ec472f8ac482
add static Engine::graphicsEnabled function and disable player graphics loading when false
src/Engine.cc
src/Engine.hh
src/Player.cc
--- a/src/Engine.cc	Sat Dec 06 13:58:11 2008 +0000
+++ b/src/Engine.cc	Sat Dec 06 14:20:01 2008 +0000
@@ -4,6 +4,8 @@
 
 #include <iostream>
 
+bool Engine::_graphicsEnabled = false;
+
 Engine::Engine (void) : is_running(true) {
 
 }
@@ -11,6 +13,9 @@
 void Engine::setupGraphics (void) {
     // create the graphics
     graphics = new Graphics(*this, game_state);
+
+    // set enabled
+    Engine::_graphicsEnabled = true;
 }
 
 void Engine::setupNetworkServer (const std::string &listen_port) {
@@ -50,6 +55,10 @@
     }
 }
 
+bool Engine::graphicsEnabled (void) {
+    return Engine::_graphicsEnabled;
+}
+
 Logger Engine::log (enum LogLevel level, const char *type) {
     return Logger(level <= WARN ? std::cerr : std::cout, level, type);
 }
--- a/src/Engine.hh	Sat Dec 06 13:58:11 2008 +0000
+++ b/src/Engine.hh	Sat Dec 06 14:20:01 2008 +0000
@@ -1,7 +1,7 @@
 #ifndef ENGINE_HH
 #define ENGINE_HH
 
-// XXX: forward-declare Engine for other components
+// forward-declare
 class Engine;
 
 #include "GameState.hh"
@@ -25,6 +25,9 @@
 
         // to exit the mainloop
         bool is_running;
+
+        // global...
+        static bool _graphicsEnabled;
     
     public:    
         // default constructor
@@ -46,6 +49,9 @@
         void stop (void);
 
     public:
+        // avoid loading resources for graphics if we're not going to use them
+        static bool graphicsEnabled (void);
+
         // logging utility
         static Logger log (enum LogLevel level, const char *type);
 
--- a/src/Player.cc	Sat Dec 06 13:58:11 2008 +0000
+++ b/src/Player.cc	Sat Dec 06 14:20:01 2008 +0000
@@ -1,9 +1,11 @@
+
 #include "Engine.hh"
 #include "Player.hh" 
 
 #include <cstdlib>
 #include <ClanLib/display.h>
 #include <string>
+#include <cassert>
 
 //Player::image = NULL;
 
@@ -18,11 +20,12 @@
     PhysicsObject(state.world, PLAYER_MASS, position, Vector(0, 0)), state(state), visible(visible), arsenal(), selectedWeapon(0), changing(false) {
     // TODO: arsenal's size should be affected by some value
     // and weapons should be loaded from somewhere, not generated here
-    for(int i = 0; i < 5; i++) {
+    for (int i = 0; i < 5; i++) {
         arsenal.push_back(Weapon(10000, (5-i)*40+30, i*6+5, i*100+50, "asdf"));
     }
-
-    if(!player_image_created) {
+    
+    // XXX: sanitize
+    if (Engine::graphicsEnabled() && !player_image_created) {
         player_image_created = true;
         player_image = CL_Surface(player_image_filename);
     }
@@ -142,23 +145,29 @@
 }
 
 void Player::draw(CL_GraphicContext *gc) {
+    assert(Engine::graphicsEnabled());
 
     int img_num_aim = 9;
     int aim_img_idx = (int)((1 - (getAim()+KG_PI/2)/KG_PI)*img_num_aim);
     int img_height = 9;
     int img_width = 10;
 
-    CL_Rectf destination(position.x-4, position.y-4, position.x+5, position.y+4);
-    if(!getFacing()) {
-        destination = CL_Rect(position.x+5, position.y-4, position.x-4, position.y+4);
+    CL_Rectf destination(position.x - 4, position.y - 4, position.x + 5, position.y + 4);
+
+    if (!getFacing()) {
+        destination = CL_Rect(position.x + 5, position.y - 4, position.x - 4, position.y + 4);
     }
 
-    player_image.draw_subpixel(CL_Rectf(1, aim_img_idx*img_height+1, 1+img_width, (aim_img_idx+1)*img_height+1), 
-            destination, gc);
+    player_image.draw_subpixel(
+            CL_Rectf(1, aim_img_idx * img_height + 1, 1 + img_width, (aim_img_idx + 1) * img_height + 1), 
+            destination, gc
+    );
     
     const uint16_t chlen = 10;
     uint16_t x = position.x;
     uint16_t y = position.y;
+    
+    // draw "crosshair"
     if (facingRight) {
         gc->draw_line(x + std::cos(aim)*chlen/2, 
                       y - std::sin(aim)*chlen/2,