]> git.localhorst.tv Git - gworm.git/blobdiff - src/app/Application.cpp
speed up rendering by caching world texture
[gworm.git] / src / app / Application.cpp
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() {