increase the map size and control the camera to always have the screen full of terrain
authorterom
Sun, 07 Dec 2008 21:58:34 +0000
changeset 266 ad72d0a0cc02
parent 265 d97bf6790c22
child 267 2cb6f1421e45
increase the map size and control the camera to always have the screen full of terrain
src/Config.hh
src/GameState.cc
src/GameState.hh
src/Graphics.cc
src/Graphics.hh
--- a/src/Config.hh	Sun Dec 07 21:34:12 2008 +0000
+++ b/src/Config.hh	Sun Dec 07 21:58:34 2008 +0000
@@ -14,9 +14,10 @@
 
 // Physics simulation
 // Physics resolution
-const uint16_t MAP_WIDTH = 800;
-const uint16_t MAP_HEIGHT = 600;
+const uint16_t MAP_WIDTH = 1000;
+const uint16_t MAP_HEIGHT = 800;
 const float MAP_SCALE = 1; // One "pixel" in "real" units
+
 // Simulation
 const uint16_t PHYSICS_TICK_MS = 10;
 
@@ -46,6 +47,7 @@
 // how far away from the player the projectile spawns
 const float PROJECTILE_START_DISTANCE = 10.0;
 
+// rope
 const float ROPE_GROWTH_RATE = 5;
 const float ROPE_FORCE = 3500;
 const float ROPE_MASS = 10.0;   // same as player mass...?
@@ -57,7 +59,14 @@
 const CL_Color COLOR_DIRT(144, 82, 23);
 const CL_Color COLOR_ROCK(132, 136, 135);
 
-// Data paths
+// graphics params
+const std::string GRAPHICS_WINDOW_TITLE = "Kisna Glista";
+const uint32_t GRAPHICS_RESOLUTION_WIDTH = 800;
+const uint32_t GRAPHICS_RESOLUTION_HEIGHT = 600;
+const uint16_t GRAPHICS_UPDATE_INTERVAL_MS = 20;
+
+
+// Filesystem paths
 const std::string PLAYER_SKIN_PATH = (PROJECT_DATA_DIR "/skin.png");
 const std::string RESOURCE_XML_PATH = (PROJECT_DATA_DIR "/resources.xml");
 
--- a/src/GameState.cc	Sun Dec 07 21:34:12 2008 +0000
+++ b/src/GameState.cc	Sun Dec 07 21:58:34 2008 +0000
@@ -34,12 +34,7 @@
     player_list.remove(player);
 }
     
-void GameState::draw(Graphics *g, bool displayWeapon) {
-    PixelCoordinate camera(0, 0);
-
-    if (local_player)
-        camera = local_player->getCoordinate() - world.getDimensions() / 2;
-
+void GameState::draw(Graphics *g, PixelCoordinate camera, bool displayWeapon) {
     // Draw world/terrain
     world.draw(g, camera);
 
--- a/src/GameState.hh	Sun Dec 07 21:34:12 2008 +0000
+++ b/src/GameState.hh	Sun Dec 07 21:58:34 2008 +0000
@@ -38,7 +38,7 @@
 
     void removePlayer (Player *player);
 
-    virtual void draw (Graphics *g, bool displayWeapon);
+    virtual void draw (Graphics *g, PixelCoordinate camera, bool displayWeapon);
 };
 
 #endif
--- a/src/Graphics.cc	Sun Dec 07 21:34:12 2008 +0000
+++ b/src/Graphics.cc	Sun Dec 07 21:58:34 2008 +0000
@@ -5,6 +5,7 @@
 
 Graphics::Graphics (Engine &engine, GameState &state) :
     CL_DisplayWindow(GRAPHICS_WINDOW_TITLE, GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT),
+    resolution(GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT),
     engine(engine), 
     state(state), 
     update_timer(GRAPHICS_UPDATE_INTERVAL_MS),
@@ -49,14 +50,41 @@
         player->handleInput(input_mask);
 }
 
+static PixelDimension value_between (PixelDimension low, PixelDimension value, PixelDimension high) {
+    if (value < low)
+        return low;
+
+    else if (value > high)
+        return high;
+
+    else
+        return value;
+}
+
 void Graphics::do_redraw (void) {
     CL_GraphicContext *gc = get_gc();
+    LocalPlayer *player;
+
+    // calculate camera
+    PixelCoordinate camera(0, 0);
+    
+    // ...to track our local player
+    if ((player = state.getLocalPlayer()) != NULL) {
+        PixelCoordinate target = player->getCoordinate() - resolution / 2;
+        PixelCoordinate max = state.world.getDimensions() - resolution;
+        
+        // keep the terrain in view
+        camera = PixelCoordinate(
+            value_between(0, target.x, max.x),
+            value_between(0, target.y, max.y)
+        );
+    }
     
     // White background
     gc->clear(CL_Color::black);
 
     // Draw the game
-    state.draw(this, flags & GUI_INPUT_DISPLAY_WEAPON);
+    state.draw(this, camera, flags & GUI_INPUT_DISPLAY_WEAPON);
 
     // Flip window buffer, sync
     flip(1);
--- a/src/Graphics.hh	Sun Dec 07 21:34:12 2008 +0000
+++ b/src/Graphics.hh	Sun Dec 07 21:58:34 2008 +0000
@@ -7,20 +7,18 @@
 #include "Input.hh"
 #include "Timer.hh"
 #include "Engine.hh"
+#include "Config.hh"
 
 #include <ClanLib/core.h>
 #include <ClanLib/gl.h>
 #include <ClanLib/display.h>
 
-const std::string GRAPHICS_WINDOW_TITLE = "Kisna Glista";
-const uint32_t GRAPHICS_RESOLUTION_WIDTH = 800;
-const uint32_t GRAPHICS_RESOLUTION_HEIGHT = 600;
-const uint16_t GRAPHICS_UPDATE_INTERVAL_MS = 20;
-
 class Graphics : public CL_DisplayWindow {
 private:
     Engine &engine;
     GameState &state;
+
+    PixelCoordinate resolution;
     
     CL_SlotContainer slots;