summaryrefslogtreecommitdiffstats
path: root/Display.cpp
blob: 7c3e59eca22cb95280f3228ebe3e22f6566c7c03 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <iostream>
#include "Display.hpp"

Display::Display(int w, int h, std::string title, World *world, std::condition_variable &gameStartWaiter)
        : gameStartWaiter(gameStartWaiter) {
    window = new sf::RenderWindow(sf::VideoMode(w, h), title);
    window->setActive(true);
    window->clear(sf::Color::Black);
    window->display();
    this->world = world;
    window->setFramerateLimit(60);
}

Display::~Display() {
    delete window;
}

void Display::Update() {
    pollEvents();

    /*{
        std::chrono::steady_clock clock;
        static auto timeOfPreviousUpdate(clock.now());
        std::chrono::duration<double> delta = clock.now() - timeOfPreviousUpdate;
        if (delta.count() > 0.5) {
            timeOfPreviousUpdate = clock.now();*/
    window->setTitle(
            std::string("Render layer: " + std::to_string(renderLayer) + "\t" +
                        //" BlockID: " + std::to_string(currentId) +
                        " Mouse pos" + std::to_string(mousePos.x) + "  " + std::to_string(mousePos.y) +
                        " FPS: " + std::to_string(1.0 / frameTime)));/*
        }
    }*/

    window->clear(sf::Color::Black);
    if (isGameStarted)
        renderWorld();
    window->display();
}

void Display::renderWorld() {
    //currentId = 0;
    for (auto sectionIt = world->m_sections.begin(); sectionIt != world->m_sections.end(); ++sectionIt) {
        if (sectionIt->first.GetY() != renderLayer / 16)
            continue;
        Section &section = sectionIt->second;
        sf::Image &image = GetSectionTexture(sectionIt->first);
        sf::Texture texture;
        texture.create(16, 16);
        texture.update(image);
        sf::Sprite sprite(texture);
        sprite.setPosition(sectionIt->first.GetX() * 16, sectionIt->first.GetZ() * 16);
        window->draw(sprite);
        //                                                                                                                                              sf::Texture &texture = GetSectionTexture(sectionIt->first);
        /*for (int x = 0; x < 16; x++) {
            for (int z = 0; z < 16; z++) {
                int y = renderLayer - sectionIt->first.GetY() * 16;
                int absoluteX = sectionIt->first.GetX() * 16 + x;
                int absoluteZ = sectionIt->first.GetZ() * 16 + z;


                Block &block = section.GetBlock(PositionI(x, z, y));
                sf::RectangleShape shape(sf::Vector2f(1, 1));

                shape.setPosition(absoluteX, absoluteZ);
                shape.setFillColor(sf::Color::Magenta);
                if (mousePos.x > shape.getPosition().x && mousePos.y > shape.getPosition().y) {
                    if (mousePos.x < shape.getPosition().x + 1 && mousePos.y < shape.getPosition().y + 1) {
                        currentId = block.id;
                        if (isClicked) {
                            std::cout << "Clicked it " << absoluteX << " " << absoluteZ << std::endl;
                            isClicked = false;
                        }
                    }
                }
                switch (block.id) {
                    case 0:
                        shape.setFillColor(sf::Color::Transparent);
                        break;
                    case 7:
                        shape.setFillColor(sf::Color::Yellow);
                        break;
                    case 1:
                        shape.setFillColor(sf::Color::White);
                        break;
                    case 11:
                        shape.setFillColor(sf::Color::Red);
                        break;
                    case 10:
                        shape.setFillColor(sf::Color::Red);
                        break;
                    case 3:
                        shape.setFillColor(sf::Color(139, 69, 69));
                        break;
                    case 13:
                        shape.setFillColor(sf::Color(220, 220, 220));
                        break;
                    case 9:
                        shape.setFillColor(sf::Color::Blue);
                        break;
                    case 8:
                        shape.setFillColor(sf::Color::Blue);
                        break;
                    case 2:
                        shape.setFillColor(sf::Color::Green);
                        break;
                    default:
                        //std::cout << "Unknown id is " << sectionIt.second.GetId() << std::endl;
                        break;
                }
                sf::Color darkness(0, 0, 0, ((15 - block.light) / 15.0f) * 255);
                shape.setFillColor(shape.getFillColor() + darkness);
                window->draw(shape);
            }
        }
        sf::Vector2f p1 = sf::Vector2f(sectionIt->first.GetX() * 16, sectionIt->first.GetZ() * 16);
        sf::Vector2f p2 = sf::Vector2f(sectionIt->first.GetX() * 16 + 16, sectionIt->first.GetZ() * 16);
        sf::Vector2f p3 = sf::Vector2f(sectionIt->first.GetX() * 16 + 16, sectionIt->first.GetZ() * 16 + 16);
        sf::Vector2f p4 = sf::Vector2f(sectionIt->first.GetX() * 16, sectionIt->first.GetZ() * 16 + 16);
        sf::Vertex line1[] = {
                sf::Vertex(p1),
                sf::Vertex(p2),
        };
        sf::Vertex line2[] = {
                sf::Vertex(p2),
                sf::Vertex(p3),
        };
        sf::Vertex line3[] = {
                sf::Vertex(p3),
                sf::Vertex(p4),
        };
        sf::Vertex line4[] = {
                sf::Vertex(p4),
                sf::Vertex(p1),
        };
        window->draw(line1, 2, sf::Lines);
        window->draw(line2, 2, sf::Lines);
        window->draw(line3, 2, sf::Lines);
        window->draw(line4, 2, sf::Lines);*/
    }
}

void Display::pollEvents() {
    sf::Event e;
    while (window->pollEvent(e)) {
        switch (e.type) {
            case sf::Event::Closed:
                window->close();
                break;
            case sf::Event::MouseMoved:
                mousePos = window->mapPixelToCoords(sf::Vector2i(e.mouseMove.x, e.mouseMove.y));
                break;
            case sf::Event::KeyPressed:
                if (e.key.code == sf::Keyboard::Z) {
                    if (renderLayer > 0)
                        renderLayer--;
                } else if (e.key.code == sf::Keyboard::X) {
                    if (renderLayer < 256)
                        renderLayer++;
                } else if (e.key.code == sf::Keyboard::Up) {
                    sf::View view = window->getView();
                    view.move(0, -coeff);
                    window->setView(view);
                } else if (e.key.code == sf::Keyboard::Down) {
                    sf::View view = window->getView();
                    view.move(0, coeff);
                    window->setView(view);
                } else if (e.key.code == sf::Keyboard::Right) {
                    sf::View view = window->getView();
                    view.move(coeff, 0);
                    window->setView(view);
                } else if (e.key.code == sf::Keyboard::Left) {
                    sf::View view = window->getView();
                    view.move(-coeff, 0);
                    window->setView(view);
                } else if (e.key.code == sf::Keyboard::A) {
                    sf::View view = window->getView();
                    //view.setSize(view.getSize().x + coeff2, view.getSize().y + coeff2);
                    view.zoom(1.1);
                    window->setView(view);
                } else if (e.key.code == sf::Keyboard::S) {
                    sf::View view = window->getView();
                    view.zoom(0.9);
                    //view.setSize(view.getSize().x - coeff2, view.getSize().y - coeff2);
                    window->setView(view);
                } else if (e.key.code == sf::Keyboard::K) {
                    std::cout << "Allocated memory is freed" << std::endl;
                    sectionTextures.clear();
                }
                break;
            case sf::Event::MouseButtonPressed:
                isClicked = true;
                break;
        }
    }
}


bool Display::IsClosed() {
    return !window->isOpen();
}

void Display::SetPlayerPos(float x, float z) {
    x = -55;
    z = 196;
    isGameStarted = true;
    float div = 5;
    float X = window->getSize().x / div, Z = window->getSize().y / div;
    sf::View view(sf::Vector2f(x, z), sf::Vector2f(X, Z));
    window->setView(view);
}

void Display::MainLoop() {
    /*std::unique_lock<std::mutex> gameStartLocker(gameStartMutex);
    gameStartWaiter.wait(gameStartLocker);
    while (!isGameStarted) {
        std::cout << "Catch spirious wakeup" << std::endl;
        gameStartWaiter.wait(gameStartLocker);
    }
    std::cout << "Graphics subsystem initialized" << std::endl;*/
    while (!IsClosed()) {
        Update();
        {
            std::chrono::steady_clock clock;
            static auto timeOfPreviousUpdate(clock.now());
            std::chrono::duration<double> delta = clock.now() - timeOfPreviousUpdate;
            timeOfPreviousUpdate = clock.now();
            frameTime = delta.count();
        }
    }
}

sf::Image &Display::GetSectionTexture(PositionI pos) {
    if (sectionTextures.find(pos) != sectionTextures.end() &&
        sectionTextures[pos][renderLayer - pos.GetY() * 16].getSize() != sf::Vector2u(0, 0))
        return sectionTextures[pos][renderLayer - pos.GetY() * 16];

    auto sectionIt = world->m_sections.find(pos);
    Section &section = sectionIt->second;
    sf::Image image;
    image.create(16, 16);
    for (int x = 0; x < 16; x++) {
        for (int z = 0; z < 16; z++) {
            int y = renderLayer - sectionIt->first.GetY() * 16;
            sf::Color color = sf::Color::Magenta;
            switch (section.GetBlock(PositionI(x, z, y)).id) {
                case 0:
                    color = sf::Color::Transparent;
                    break;
                case 7:
                    color = sf::Color::Yellow;
                    break;
                case 1:
                    color = sf::Color::White;
                    break;
                case 11:
                    color = sf::Color::Red;
                    break;
                case 10:
                    color = sf::Color::Red;
                    break;
                case 3:
                    color = sf::Color(139, 69, 69);
                    break;
                case 13:
                    color = sf::Color(220, 220, 220);
                    break;
                case 9:
                    color = sf::Color::Blue;
                    break;
                case 8:
                    color = sf::Color::Blue;
                    break;
                case 2:
                    color = sf::Color::Green;
                    break;
                default:
                    break;
            }
            image.setPixel(x, z, color);
        }
    }
    /*sf::Texture texture;
    texture.create(16, 16);
    texture.update(image);*/
    sectionTextures[pos][renderLayer - pos.GetY() * 16] = image;
    return sectionTextures[pos][renderLayer - pos.GetY() * 16];
}