2 #include "graphics/Camera.h"
3 #include "graphics/Canvas.h"
4 #include "graphics/Color.h"
5 #include "graphics/Vector.h"
6 #include "graphics/Window.h"
7 #include "world/AABB.h"
8 #include "world/Collision.h"
29 vector<AABB> stationary;
31 vector<Collision> coll;
36 , cam(Vector<int>(), focus)
42 void key_down(const SDL_KeyboardEvent &e) {
43 switch (e.keysym.sym) {
61 void key_up(const SDL_KeyboardEvent &e) {
62 switch (e.keysym.sym) {
82 while (SDL_PollEvent(&event)) {
88 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
89 cam.Resize(event.window.data1, event.window.data2);
93 if (!event.key.repeat) {
98 if (!event.key.repeat) {
106 void update(int dt) {
107 const Vector<float> speed { 5, 5 };
109 const float delta = dt / 1e3;
111 controlled.Move(Vector<float>(move) * speed * delta);
112 focus = controlled.Center();
115 for (const AABB &e : stationary) {
117 if (controlled.Intersects(e, c)) {
123 void render(Canvas &canvas) const {
124 constexpr Color background(0x00, 0x00, 0x00);
125 constexpr Color outlineColor(0x00, 0x00, 0xFA);
126 constexpr Color controlledColor(0xFA, 0xFA, 0x00);
127 constexpr Color entityColor(0x00, 0xFA, 0x00);
128 constexpr Color collisionColor(0xFA, 0x00, 0x00);
129 constexpr Color normalColor(0xFA, 0x00, 0x00);
131 canvas.SetColor(background);
134 canvas.SetColor(outlineColor);
136 cam.ToScreen(Vector<int>(0, 0)),
137 cam.ToScale(Vector<float>(10, 10)),
138 cam.ToScale(Vector<float>(1, 1)));
140 canvas.SetColor(entityColor);
141 for (const AABB &e : stationary) {
143 cam.ToScreen(Vector<float>(e.Left(), e.Top())),
144 cam.ToScale(e.Size()));
147 canvas.SetColor(controlledColor);
149 cam.ToScreen(Vector<float>(controlled.Left(), controlled.Top())),
150 cam.ToScale(controlled.Size()));
152 if (coll.empty()) return;
154 for (const Collision &c : coll) {
155 canvas.SetColor(collisionColor);
158 cam.ToScreen(c.pos + c.depth));
159 canvas.SetColor(normalColor);
162 cam.ToScreen(c.pos) + Vector<int>(c.norm * 25.0f));
166 void run(Canvas &canvas) {
167 cam.SetScale(Vector<float>(32, 32));
168 controlled.Resize(Vector<float>(2, 3));
169 controlled.Move(Vector<float>(1, 1.5));
172 e.Resize(Vector<float>(2, 2));
173 e.Move(Vector<float>(5, 5));
174 stationary.push_back(e);
175 e.Move(Vector<float>(0, 2));
176 stationary.push_back(e);
177 e.Move(Vector<float>(-2, 0));
178 stationary.push_back(e);
180 Uint32 last = SDL_GetTicks();
183 Uint32 now = SDL_GetTicks();
185 int delta = now - last;
189 } else if (delta > 30) {
206 int main(int argc, const char *argv[]) {
207 SDL sdl(SDL_INIT_VIDEO);
210 "orbi collision test",
212 Vector<int>(800, 600),
215 Canvas canv(win.CreateCanvas(