pingus-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Pingus-CVS] [pingus] push by address@hidden - Cache CollisionMap sprite


From: pingus
Subject: [Pingus-CVS] [pingus] push by address@hidden - Cache CollisionMap sprite and only update it when the CollisionMap cha... on 2011-10-19 18:32 GMT
Date: Wed, 19 Oct 2011 18:34:51 +0000

Revision: f0e5a7527720
Author:   Ingo Ruhnke <address@hidden>
Date:     Wed Oct 19 11:32:35 2011
Log: Cache CollisionMap sprite and only update it when the CollisionMap changes

http://code.google.com/p/pingus/source/detail?r=f0e5a7527720

Modified:
 /src/pingus/collision_map.cpp
 /src/pingus/collision_map.hpp

=======================================
--- /src/pingus/collision_map.cpp       Wed Sep  7 05:53:15 2011
+++ /src/pingus/collision_map.cpp       Wed Oct 19 11:32:35 2011
@@ -20,11 +20,13 @@
 #include "engine/display/sprite.hpp"
 #include "pingus/collision_mask.hpp"

-CollisionMap::CollisionMap(int w, int h)
-  : serial(0),
-    width(w),
-    height(h),
-    colmap(new unsigned char[width * height])
+CollisionMap::CollisionMap(int w, int h) :
+  serial(0),
+  width(w),
+  height(h),
+  colmap(new unsigned char[width * height]),
+  m_colmap_sprite(),
+  m_colmap_sprite_serial()
 {
   // Clear the colmap
memset(colmap.get(), Groundtype::GP_NOTHING, sizeof(unsigned char) * width * height);
@@ -150,57 +152,63 @@
 void
 CollisionMap::draw(DrawingContext& gc)
 {
-  Surface canvas(width, height);
-  unsigned char* buffer;
-
-  canvas.lock();
-  buffer = static_cast<unsigned char*>(canvas.get_data());
-
-  const int red   = 0;
-  const int green = 1;
-  const int blue  = 2;
-  const int alpha = 3;
-
-  uint8_t trans = 220;
-
-  for(int i = 0; i < (width * height); ++i)
-  {
-    switch(colmap[i])
-    {
-      case Groundtype::GP_NOTHING:
-        buffer[i * 4 + red  ] =   0;
-        buffer[i * 4 + green] =   0;
-        buffer[i * 4 + blue ] =   0;
-        buffer[i * 4 + alpha] =   0;
-        break;
-
-      case Groundtype::GP_SOLID:
-        buffer[i * 4 + red  ] = 100;
-        buffer[i * 4 + green] = 100;
-        buffer[i * 4 + blue ] = 100;
-        buffer[i * 4 + alpha] = trans;
-        break;
-
-      case Groundtype::GP_BRIDGE:
-        buffer[i * 4 + red  ] = 200;
-        buffer[i * 4 + green] =   0;
-        buffer[i * 4 + blue ] =   0;
-        buffer[i * 4 + alpha] = trans;
-        break;
-
-      default:
-        buffer[i * 4 + red  ] = 200;
-        buffer[i * 4 + green] = 200;
-        buffer[i * 4 + blue ] = 200;
-        buffer[i * 4 + alpha] = trans;
-        break;
-    }
-  }
-
-  canvas.unlock();
-
-  Sprite sprite(canvas);
-  gc.draw(sprite, Vector2i(0, 0), 1000);
+  if (serial != m_colmap_sprite_serial || !m_colmap_sprite)
+  {
+    m_colmap_sprite_serial = serial;
+
+    Surface canvas(width, height);
+    unsigned char* buffer;
+
+    canvas.lock();
+    buffer = static_cast<unsigned char*>(canvas.get_data());
+
+    const int red   = 0;
+    const int green = 1;
+    const int blue  = 2;
+    const int alpha = 3;
+
+    uint8_t trans = 220;
+
+    for(int i = 0; i < (width * height); ++i)
+    {
+      switch(colmap[i])
+      {
+        case Groundtype::GP_NOTHING:
+          buffer[i * 4 + red  ] =   0;
+          buffer[i * 4 + green] =   0;
+          buffer[i * 4 + blue ] =   0;
+          buffer[i * 4 + alpha] =   0;
+          break;
+
+        case Groundtype::GP_SOLID:
+          buffer[i * 4 + red  ] = 100;
+          buffer[i * 4 + green] = 100;
+          buffer[i * 4 + blue ] = 100;
+          buffer[i * 4 + alpha] = trans;
+          break;
+
+        case Groundtype::GP_BRIDGE:
+          buffer[i * 4 + red  ] = 200;
+          buffer[i * 4 + green] =   0;
+          buffer[i * 4 + blue ] =   0;
+          buffer[i * 4 + alpha] = trans;
+          break;
+
+        default:
+          buffer[i * 4 + red  ] = 200;
+          buffer[i * 4 + green] = 200;
+          buffer[i * 4 + blue ] = 200;
+          buffer[i * 4 + alpha] = trans;
+          break;
+      }
+    }
+
+    canvas.unlock();
+
+    m_colmap_sprite = Sprite(canvas);
+  }
+
+  gc.draw(m_colmap_sprite, Vector2i(0, 0), 1000);
 }

 unsigned
=======================================
--- /src/pingus/collision_map.hpp       Wed Sep  7 05:53:15 2011
+++ /src/pingus/collision_map.hpp       Wed Oct 19 11:32:35 2011
@@ -19,6 +19,7 @@

 #include <memory>

+#include "engine/display/sprite.hpp"
 #include "pingus/groundtype.hpp"

 class CollisionMask;
@@ -46,6 +47,9 @@
   /** A array of uchar, each uchar represents a pixel on the map. */
   std::unique_ptr<uint8_t[]> colmap;

+  Sprite m_colmap_sprite;
+  unsigned int m_colmap_sprite_serial;
+
 public:
   /** Init the colmap from a given area of memory.
       The memory will be deleted in the destructor. */



reply via email to

[Prev in Thread] Current Thread [Next in Thread]