From: Daniel Karbach Date: Sun, 19 Jan 2014 13:47:43 +0000 (+0100) Subject: speed up rendering by caching world texture X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=b358fcf95d5f0d05c47c48dc741b8ce06cb45090;p=gworm.git speed up rendering by caching world texture --- diff --git a/src/app/Application.cpp b/src/app/Application.cpp index b7983aa..490e167 100644 --- a/src/app/Application.cpp +++ b/src/app/Application.cpp @@ -16,6 +16,7 @@ namespace gworm { Application::Application(Canvas &c, World &w) : canvas(c) , world(w) +, worldTex(canvas.CreateStaticTexture(world.Size())) , focus(Vector(250, 250), 25) , cam(c.Size(), focus.Pos()) , last(SDL_GetTicks()) @@ -158,39 +159,13 @@ void Application::RenderBackground() { } void Application::RenderWorld() { - Vector begin(0, 0); - Vector end(world.Size()); - Vector topLeft = cam.ToScreen(Vector(begin)); - Vector bottomRight = cam.ToScreen(Vector(end)); - Vector 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 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 pos = cam.ToScreen(Vector(0, 0)); + canvas.Copy(worldTex, pos); } void Application::RenderEntities() { diff --git a/src/app/Application.h b/src/app/Application.h index 23815d3..5b46f9c 100644 --- a/src/app/Application.h +++ b/src/app/Application.h @@ -3,6 +3,7 @@ #include "../graphics/Camera.h" #include "../graphics/Moveable.h" +#include "../graphics/Texture.h" #include "../graphics/Vector.h" #include @@ -40,6 +41,8 @@ private: Canvas &canvas; World &world; + Texture worldTex; + Moveable focus; Camera cam; diff --git a/src/world/World.cpp b/src/world/World.cpp index dcac95e..4cb8ffd 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -9,7 +9,8 @@ World::World(Vector 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) { } diff --git a/src/world/World.h b/src/world/World.h index 8a00f60..9953e46 100644 --- a/src/world/World.h +++ b/src/world/World.h @@ -24,12 +24,19 @@ public: bool InBounds(Vector pos) const { return pos.x > 0 && pos.y > 0 && pos.x < size.x && pos.y < size.y; } - int Index(Vector pos) const { return pos.x * size.y + pos.y; } + int Index(Vector pos) const { return pos.y * size.x + pos.x; } float MassAt(Vector pos) const { return masses[Index(pos)]; } void SetMass(Vector pos, float m) { masses[Index(pos)] = m; } + Color ColorAt(Vector pos) const { return colors[Index(pos)]; } - void SetColor(Vector pos, Color c) { colors[Index(pos)] = c; } + const Color *GetColors() const { return colors.data(); } + void SetColor(Vector pos, Color c) { + colors[Index(pos)] = c; + colorDirty = true; + } + bool ColorDirty() const { return colorDirty; } + void CleanColor() { colorDirty = false; } const std::list &Entities() const { return entities; } Entity &AddEntity(const Entity &); @@ -45,6 +52,7 @@ private: std::vector masses; std::vector colors; + bool colorDirty; std::list entities;