Application::Application(InitScreen &s)
: screen(s)
-, univ(10, 10, 10, 10, 10)
+, univ(Vector<int>(10, 10), Vector<int>(10, 10), Vector<int>(10, 10), 10)
, focus(Vector<float>(500, 500), 500)
, cam(800, 800, focus.Pos())
, last(SDL_GetTicks())
, running(false) {
-
+ controlled = univ.AddEntity(Entity());
}
case SDLK_RIGHT:
focus.MoveRight();
break;
+ case SDLK_w:
+ control.y -= 1;
+ break;
+ case SDLK_s:
+ control.y += 1;
+ break;
+ case SDLK_a:
+ control.x -= 1;
+ break;
+ case SDLK_d:
+ control.x += 1;
+ break;
default:
break;
}
case SDLK_RIGHT:
focus.StopRight();
break;
+ case SDLK_w:
+ control.y += 1;
+ break;
+ case SDLK_s:
+ control.y -= 1;
+ break;
+ case SDLK_a:
+ control.x += 1;
+ break;
+ case SDLK_d:
+ control.x -= 1;
+ break;
default:
break;
}
void Application::Update(int dt) {
- focus.Update(dt / 1e3);
+ const float delta = dt / 1e3;
+ controlled->acc = Vector<float>(control * 10);
+ univ.Update(delta);
+ focus.Update(delta);
}
void Application::Render() {
constexpr Color background(0x00, 0x00, 0x00);
- constexpr Color univGrid(0xFF, 0xFF, 0xFF);
- constexpr Color sectGrid(0xAA, 0xAA, 0xAA);
-
- constexpr Vector<int> areaSize(10, 10);
- constexpr Vector<int> sectSize(areaSize * 10);
- constexpr Vector<int> univSize(sectSize * 10);
+ constexpr Color univGrid(0xEE, 0xEE, 0xEE);
+ constexpr Color secGrid(0x77, 0x77, 0x77);
+ constexpr Color entityColor(0x00, 0xAA, 0xAA);
+ constexpr Color focusColor(0xFA, 0xFA, 0x00);
SDL_Surface *dst = screen.Screen();
- Vector<int> offset = cam.Offset();
+ const Vector<int> begin = cam.Offset();
+ const Vector<int> end =
+ begin + (univ.size * univ.secSize * univ.areaSize) + Vector<int>(1, 1);
Fill(dst, background);
- Grid(dst, offset, offset + univSize + Vector<int>(1, 1), areaSize, sectGrid);
- Grid(dst, offset, offset + univSize + Vector<int>(1, 1), sectSize, univGrid);
- Cross(dst, offset + Vector<int>(focus.Pos()), 15, Color(0xFF, 0xFF, 0x00));
+ Grid(dst, begin, end, univ.areaSize, secGrid);
+ Grid(dst, begin, end, univ.secSize * univ.areaSize, univGrid);
+ Cross(dst, begin + Vector<int>(focus.Pos()), 15, focusColor);
+
+ for (const Entity &e : univ.Entities()) {
+ Cross(dst, begin + (e.area * univ.areaSize) + Vector<int>(e.pos), 10, entityColor);
+ }
}
}
namespace space {
class InitScreen;
+class Entity;
class Application {
void Update(int delta_ms);
- Vector<float> FocusVel() const;
-
void Render();
private:
Moveable<float> focus;
Camera cam;
+ Entity *controlled;
+ Vector<int> control;
+
Uint32 last;
bool running;
--- /dev/null
+#ifndef SPACE_ENTITY_H_
+#define SPACE_ENTITY_H_
+
+#include "../math/Vector.h"
+
+
+namespace space {
+
+class Entity {
+
+public:
+ constexpr Entity() { }
+
+public:
+ Vector<int> area;
+ Vector<float> pos;
+ Vector<float> vel;
+ Vector<float> acc;
+
+public:
+ void Update(float delta) {
+ pos += vel * delta;
+ vel += acc * delta;
+ }
+
+};
+
+}
+
+#endif
constexpr Vector<Scalar> operator *(Scalar lhs, Vector<Scalar> rhs) {
return rhs * lhs;
}
+template<class Scalar>
+constexpr Vector<Scalar> operator *(Vector<Scalar> lhs, Vector<Scalar> rhs) {
+ return Vector<Scalar>(lhs.x * rhs.x, lhs.y * rhs.y);
+}
}
namespace space {
-Sector::Sector(int w, int h, int numres)
-: w(w)
-, h(h)
+Sector::Sector(Vector<int> size, int numres)
+: size(size)
, numres(numres)
-, total(w * h * numres)
+, total(size.x * size.y * numres)
, res_begin(new int[total])
, res_end(res_begin + total) {
std::memset(res_begin, 0, total * sizeof(int));
#ifndef SPACE_SECTOR_H_
#define SPACE_SECTOR_H_
+#include "../math/Vector.h"
+
+
namespace space {
class Sector {
public:
- Sector(int w, int h, int numres);
+ Sector(Vector<int> size, int numres);
~Sector();
Sector(const Sector &) = delete;
Sector &operator =(const Sector &) = delete;
private:
- int w;
- int h;
+ Vector<int> size;
int numres;
int total;
int *res_begin;
namespace space {
-Universe::Universe(int w, int h, int sec_w, int sec_h, int numres)
-: w(w)
-, h(h)
+Universe::Universe(Vector<int> size, Vector<int> secSize, Vector<int> areaSize, int numres)
+: size(size)
+, secSize(secSize)
+, areaSize(areaSize)
, numres(numres)
-, total(w * h)
+, total(size.x * size.y)
, sec_begin(reinterpret_cast<Sector *>(new char[total * sizeof(Sector)]))
, sec_end(sec_begin + total) {
for (Sector *i = sec_begin; i < sec_end; ++i) {
- new (i) Sector(sec_w, sec_h, numres);
+ new (i) Sector(secSize, numres);
}
}
delete[] reinterpret_cast<char *>(sec_begin);
}
+
+Entity *Universe::AddEntity(const Entity &e) {
+ entities.emplace_back(e);
+ return &entities.back();
+}
+
+
+void Universe::Update(float delta) {
+ for (Entity &e : entities) {
+ e.Update(delta);
+ while (e.pos.x > areaSize.x) {
+ e.pos.x -= areaSize.x;
+ ++e.area.x;
+ }
+ while (e.pos.x < 0) {
+ e.pos.x += areaSize.x;
+ --e.area.x;
+ }
+ while (e.pos.y > areaSize.y) {
+ e.pos.y -= areaSize.y;
+ ++e.area.y;
+ }
+ while (e.pos.y < 0) {
+ e.pos.y += areaSize.y;
+ --e.area.y;
+ }
+ }
+}
+
}
#ifndef SPACE_UNIVERSE_H_
#define SPACE_UNIVERSE_H_
+#include "../entity/Entity.h"
+#include "../math/Vector.h"
+
+#include <list>
+
+
namespace space {
class Sector;
class Universe {
public:
- Universe(int w, int h, int sec_w, int sec_h, int numres);
+ Universe(Vector<int> size, Vector<int> secSize, Vector<int> areaSize, int numres);
~Universe();
-private:
- Universe(const Universe &);
- Universe &operator =(const Universe &);
+
+ Universe(const Universe &) = delete;
+ Universe &operator =(const Universe &) = delete;
+
+public:
+ const Vector<int> size;
+ const Vector<int> secSize;
+ const Vector<int> areaSize;
+
+public:
+ Entity *AddEntity(const Entity &);
+ const std::list<Entity> &Entities() const { return entities; }
+
+public:
+ void Update(float deltaT);
private:
- int w;
- int h;
+
int numres;
int total;
+
Sector *sec_begin;
Sector *sec_end;
+ std::list<Entity> entities;
+
};
}