]> git.localhorst.tv Git - gworm.git/commitdiff
speed up rendering by caching world texture master
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 19 Jan 2014 13:47:43 +0000 (14:47 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 19 Jan 2014 13:47:43 +0000 (14:47 +0100)
src/app/Application.cpp
src/app/Application.h
src/world/World.cpp
src/world/World.h

index b7983aa0fcc56323e9bc07629cdcf53409c249c5..490e1675d8b83b25276bde6ddc219eaf59103b84 100644 (file)
@@ -16,6 +16,7 @@ namespace gworm {
 Application::Application(Canvas &c, World &w)
 : canvas(c)
 , world(w)
+, worldTex(canvas.CreateStaticTexture(world.Size()))
 , focus(Vector<float>(250, 250), 25)
 , cam(c.Size(), focus.Pos())
 , last(SDL_GetTicks())
@@ -158,39 +159,13 @@ void Application::RenderBackground() {
 }
 
 void Application::RenderWorld() {
-       Vector<int> begin(0, 0);
-       Vector<int> end(world.Size());
-       Vector<int> topLeft = cam.ToScreen(Vector<float>(begin));
-       Vector<int> bottomRight = cam.ToScreen(Vector<float>(end));
-       Vector<int> clip(canvas.Size());
-
-       if (begin.x > clip.x || begin.y > clip.y || end.x < 0 || end.y < 0) {
-               return;
+       if (world.ColorDirty()) {
+               worldTex.SetColors(world.GetColors());
+               world.CleanColor();
        }
 
-       if (topLeft.x < 0) {
-               begin.x -= topLeft.x;
-               topLeft.x = 0;
-       }
-       if (topLeft.y < 0) {
-               begin.y -= topLeft.y;
-               topLeft.y = 0;
-       }
-       if (bottomRight.x > clip.x) {
-               end.x -= bottomRight.x - clip.x;
-               bottomRight.x = clip.x;
-       }
-       if (bottomRight.y > clip.y) {
-               end.y -= bottomRight.y - clip.y;
-               bottomRight.y = clip.y;
-       }
-
-       for (Vector<int> pos(begin), cur(topLeft); pos.y < end.y; ++pos.y, ++cur.y) {
-               for (pos.x = begin.x, cur.x = topLeft.x; pos.x < end.x; ++pos.x, ++cur.x) {
-                       canvas.SetColor(world.ColorAt(pos));
-                       canvas.Dot(cur);
-               }
-       }
+       const Vector<int> pos = cam.ToScreen(Vector<float>(0, 0));
+       canvas.Copy(worldTex, pos);
 }
 
 void Application::RenderEntities() {
index 23815d34460fe64b0815db3cf9111ea1a04cb2b8..5b46f9c9a1aa4bf094ba8af7ecbdac7e827b3d34 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "../graphics/Camera.h"
 #include "../graphics/Moveable.h"
+#include "../graphics/Texture.h"
 #include "../graphics/Vector.h"
 
 #include <SDL.h>
@@ -40,6 +41,8 @@ private:
        Canvas &canvas;
        World &world;
 
+       Texture worldTex;
+
        Moveable<float> focus;
        Camera cam;
 
index dcac95eb72708791b37b15f4a29a76035aa0e503..4cb8ffdf66e5ba36644b732675e1a292923ad08c 100644 (file)
@@ -9,7 +9,8 @@ World::World(Vector<int> size)
 : size(size)
 , count(size.x * size.y)
 , masses(count, 1000000000.0f)
-, colors(count, Color(0x7F, 0x7F, 0x7F)) {
+, colors(count, Color(0x7F, 0x7F, 0x7F))
+, colorDirty(true) {
 
 }
 
index 8a00f60541302a489d8e34565c916eecea3f7b5d..9953e465901f3d2bca329fbdbb6d55a40f1395aa 100644 (file)
@@ -24,12 +24,19 @@ public:
 
        bool InBounds(Vector<int> pos) const
                { return pos.x > 0 && pos.y > 0 && pos.x < size.x && pos.y < size.y; }
-       int Index(Vector<int> pos) const { return pos.x * size.y + pos.y; }
+       int Index(Vector<int> pos) const { return pos.y * size.x + pos.x; }
 
        float MassAt(Vector<int> pos) const { return masses[Index(pos)]; }
        void SetMass(Vector<int> pos, float m) { masses[Index(pos)] = m; }
+
        Color ColorAt(Vector<int> pos) const { return colors[Index(pos)]; }
-       void SetColor(Vector<int> pos, Color c) { colors[Index(pos)] = c; }
+       const Color *GetColors() const { return colors.data(); }
+       void SetColor(Vector<int> pos, Color c) {
+               colors[Index(pos)] = c;
+               colorDirty = true;
+       }
+       bool ColorDirty() const { return colorDirty; }
+       void CleanColor() { colorDirty = false; }
 
        const std::list<Entity> &Entities() const { return entities; }
        Entity &AddEntity(const Entity &);
@@ -45,6 +52,7 @@ private:
 
        std::vector<float> masses;
        std::vector<Color> colors;
+       bool colorDirty;
 
        std::list<Entity> entities;