]> git.localhorst.tv Git - space.git/blob - src/app/Application.cpp
b2ef9161287b5cf77e30ca188386174f116d1390
[space.git] / src / app / Application.cpp
1 #include "Application.h"
2
3 #include "../graphics/Color.h"
4 #include "../graphics/primitive.h"
5 #include "../sdl/InitScreen.h"
6
7
8 namespace space {
9
10 Application::Application(InitScreen &s)
11 : screen(s)
12 , univ(Vector<int>(10, 10), Vector<int>(10, 10), Vector<int>(10, 10), 10)
13 , focus(Vector<float>(500, 500), 500)
14 , cam(800, 800, focus.Pos())
15 , last(SDL_GetTicks())
16 , running(false) {
17         controlled = univ.AddEntity(Entity());
18 }
19
20
21 void Application::Run() {
22         running = true;
23         while (running) {
24                 Uint32 now = SDL_GetTicks();
25                 int delta = now - last;
26                 Loop(delta);
27                 last = now;
28         }
29 }
30
31
32 void Application::Loop(int delta) {
33         HandleEvents();
34         Update(delta);
35         Render();
36         screen.Flip();
37 }
38
39
40 void Application::HandleEvents() {
41         SDL_Event event;
42         while (SDL_PollEvent(&event)) {
43                 switch (event.type) {
44                         case SDL_QUIT:
45                                 running = false;
46                                 break;
47                         case SDL_VIDEORESIZE:
48                                 screen.Resize(event.resize.w, event.resize.h);
49                                 cam.Resize(event.resize.w, event.resize.h);
50                                 break;
51                         case SDL_KEYDOWN:
52                                 OnKeyDown(event.key);
53                                 break;
54                         case SDL_KEYUP:
55                                 OnKeyUp(event.key);
56                                 break;
57                         default:
58                                 // skip event
59                                 break;
60                 }
61         }
62 }
63
64 void Application::OnKeyDown(const SDL_KeyboardEvent &e) {
65         switch (e.keysym.sym) {
66                 case SDLK_UP:
67                         focus.MoveUp();
68                         break;
69                 case SDLK_DOWN:
70                         focus.MoveDown();
71                         break;
72                 case SDLK_LEFT:
73                         focus.MoveLeft();
74                         break;
75                 case SDLK_RIGHT:
76                         focus.MoveRight();
77                         break;
78                 case SDLK_w:
79                         control.y -= 1;
80                         break;
81                 case SDLK_s:
82                         control.y += 1;
83                         break;
84                 case SDLK_a:
85                         control.x -= 1;
86                         break;
87                 case SDLK_d:
88                         control.x += 1;
89                         break;
90                 default:
91                         break;
92         }
93 }
94
95 void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
96         switch (e.keysym.sym) {
97                 case SDLK_UP:
98                         focus.StopUp();
99                         break;
100                 case SDLK_DOWN:
101                         focus.StopDown();
102                         break;
103                 case SDLK_LEFT:
104                         focus.StopLeft();
105                         break;
106                 case SDLK_RIGHT:
107                         focus.StopRight();
108                         break;
109                 case SDLK_w:
110                         control.y += 1;
111                         break;
112                 case SDLK_s:
113                         control.y -= 1;
114                         break;
115                 case SDLK_a:
116                         control.x += 1;
117                         break;
118                 case SDLK_d:
119                         control.x -= 1;
120                         break;
121                 default:
122                         break;
123         }
124 }
125
126
127 void Application::Update(int dt) {
128         const float delta = dt / 1e3;
129         controlled->acc = Vector<float>(control * 10);
130         univ.Update(delta);
131         focus.Update(delta);
132 }
133
134
135 void Application::Render() {
136         constexpr Color background(0x00, 0x00, 0x00);
137         constexpr Color univGrid(0xEE, 0xEE, 0xEE);
138         constexpr Color secGrid(0x77, 0x77, 0x77);
139         constexpr Color entityColor(0x00, 0xAA, 0xAA);
140         constexpr Color focusColor(0xFA, 0xFA, 0x00);
141
142         SDL_Surface *dst = screen.Screen();
143         const Vector<int> begin = cam.Offset();
144         const Vector<int> end =
145                 begin + (univ.size * univ.secSize * univ.areaSize) + Vector<int>(1, 1);
146
147         Fill(dst, background);
148         Grid(dst, begin, end, univ.areaSize, secGrid);
149         Grid(dst, begin, end, univ.secSize * univ.areaSize, univGrid);
150         Cross(dst, begin + Vector<int>(focus.Pos()), 15, focusColor);
151
152         for (const Entity &e : univ.Entities()) {
153                 Cross(dst, begin + (e.area * univ.areaSize) + Vector<int>(e.pos), 10, entityColor);
154         }
155 }
156
157 }