]> git.localhorst.tv Git - space.git/blob - src/app/Application.cpp
9ebc2613d7d24a93be4fca69ddc66c200d16a13d
[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                 case SDLK_z:
91                         cam.DoubleZoom();
92                         break;
93                 case SDLK_x:
94                         cam.HalfZoom();
95                         break;
96                 default:
97                         break;
98         }
99 }
100
101 void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
102         switch (e.keysym.sym) {
103                 case SDLK_UP:
104                         focus.StopUp();
105                         break;
106                 case SDLK_DOWN:
107                         focus.StopDown();
108                         break;
109                 case SDLK_LEFT:
110                         focus.StopLeft();
111                         break;
112                 case SDLK_RIGHT:
113                         focus.StopRight();
114                         break;
115                 case SDLK_w:
116                         control.y += 1;
117                         break;
118                 case SDLK_s:
119                         control.y -= 1;
120                         break;
121                 case SDLK_a:
122                         control.x += 1;
123                         break;
124                 case SDLK_d:
125                         control.x -= 1;
126                         break;
127                 default:
128                         break;
129         }
130 }
131
132
133 void Application::Update(int dt) {
134         const float delta = dt / 1e3;
135         controlled->acc = Vector<float>(control * 10);
136         cam.Update(delta);
137         univ.Update(delta);
138         focus.Update(delta);
139 }
140
141
142 void Application::Render() {
143         constexpr Color background(0x00, 0x00, 0x00);
144         constexpr Color univGrid(0xEE, 0xEE, 0xEE);
145         constexpr Color secGrid(0x77, 0x77, 0x77);
146         constexpr Color entityColor(0x00, 0xAA, 0xAA);
147         constexpr Color focusColor(0xFA, 0xFA, 0x00);
148
149         SDL_Surface *dst = screen.Screen();
150         const Vector<int> begin = cam.ToScreen(Vector<float>(0, 0));
151         const Vector<int> end =
152                 cam.ToScreen((univ.size * univ.secSize * univ.areaSize)) + Vector<int>(1, 1);;
153
154         Fill(dst, background);
155         Grid(dst, begin, end, cam.ToScale(univ.areaSize), secGrid);
156         Grid(dst, begin, end, cam.ToScale(univ.secSize * univ.areaSize), univGrid);
157         Cross(dst, cam.ToScreen(focus.Pos()), 15, focusColor);
158
159         for (const Entity &e : univ.Entities()) {
160                 Cross(dst, cam.ToScreen((e.area * univ.areaSize) + Vector<int>(e.pos)), 10, entityColor);
161         }
162 }
163
164 }