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())
}
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() {
: 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) {
}
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 &);
std::vector<float> masses;
std::vector<Color> colors;
+ bool colorDirty;
std::list<Entity> entities;