]> git.localhorst.tv Git - space.git/blob - src/app/Application.cpp
grid view
[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(10, 10, 10, 10, 10)
13 , focus(Vector<float>(500, 500), 500)
14 , cam(800, 800, focus.Pos())
15 , last(SDL_GetTicks())
16 , running(false) {
17
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                 default:
79                         break;
80         }
81 }
82
83 void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
84         switch (e.keysym.sym) {
85                 case SDLK_UP:
86                         focus.StopUp();
87                         break;
88                 case SDLK_DOWN:
89                         focus.StopDown();
90                         break;
91                 case SDLK_LEFT:
92                         focus.StopLeft();
93                         break;
94                 case SDLK_RIGHT:
95                         focus.StopRight();
96                         break;
97                 default:
98                         break;
99         }
100 }
101
102
103 void Application::Update(int dt) {
104         focus.Update(dt / 1e3);
105 }
106
107
108 void Application::Render() {
109         constexpr Color background(0x00, 0x00, 0x00);
110         constexpr Color univGrid(0xFF, 0xFF, 0xFF);
111         constexpr Color sectGrid(0xAA, 0xAA, 0xAA);
112
113         constexpr Vector<int> areaSize(10, 10);
114         constexpr Vector<int> sectSize(areaSize * 10);
115         constexpr Vector<int> univSize(sectSize * 10);
116
117         SDL_Surface *dst = screen.Screen();
118         Vector<int> offset = cam.Offset();
119
120         Fill(dst, background);
121         Grid(dst, offset, offset + univSize + Vector<int>(1, 1), areaSize, sectGrid);
122         Grid(dst, offset, offset + univSize + Vector<int>(1, 1), sectSize, univGrid);
123         Cross(dst, offset + Vector<int>(focus.Pos()), 15, Color(0xFF, 0xFF, 0x00));
124 }
125
126 }