summaryrefslogtreecommitdiffstats
path: root/Display.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Display.cpp')
-rw-r--r--Display.cpp268
1 files changed, 214 insertions, 54 deletions
diff --git a/Display.cpp b/Display.cpp
index fe9abf3..ef7dd9a 100644
--- a/Display.cpp
+++ b/Display.cpp
@@ -1,23 +1,142 @@
#include <iostream>
#include "Display.hpp"
-Display::Display(int w, int h, std::string title) {
+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->clear(sf::Color::Black);
+ window->display();
+ this->world = world;
}
Display::~Display() {
delete window;
}
-void Display::SetWorld(World *wrd) {
- world = wrd;
+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) {
+ 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 / frameTime)));
+ timeOfPreviousUpdate = clock.now();
+ }
+ }
+
+ window->clear(sf::Color::Black);
+ if (isGameStarted)
+ renderWorld();
+ window->display();
}
-void Display::Update() {
+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::Texture &texture = GetSectionTexture(sectionIt->first);
+ 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;
- static sf::Vector2f mousePos;
- static int renderLayer = 0, currentId = 0;
- int coeff = 10;
while (window->pollEvent(e)) {
switch (e.type) {
case sf::Event::Closed:
@@ -28,9 +147,11 @@ void Display::Update() {
break;
case sf::Event::KeyPressed:
if (e.key.code == sf::Keyboard::Z) {
- renderLayer--;
+ if (renderLayer > 0)
+ renderLayer--;
} else if (e.key.code == sf::Keyboard::X) {
- renderLayer++;
+ if (renderLayer < 256)
+ renderLayer++;
} else if (e.key.code == sf::Keyboard::Up) {
sf::View view = window->getView();
view.move(0, -coeff);
@@ -58,68 +179,107 @@ void Display::Update() {
//view.setSize(view.getSize().x - coeff2, view.getSize().y - coeff2);
window->setView(view);
}
+ break;
+ case sf::Event::MouseButtonPressed:
+ isClicked = true;
+ break;
}
}
- window->setTitle(
- std::string("Render layer: " +
- std::to_string(renderLayer) + "\t" + " BlockID: " + std::to_string(currentId) +
- std::string("\tMouse pos: ") + std::to_string(mousePos.x) + " " + std::to_string(mousePos.y)));
- currentId = 0;
- window->clear(sf::Color::Green);
-
- if (!world->m_blocks.empty())
- for (auto it:world->m_blocks) {
- if (it.first.GetY() != renderLayer)
- continue;
- //std::cout<<it.first.GetY()<<std::endl;
- sf::RectangleShape shape(sf::Vector2f(1, 1));
- shape.setPosition(it.first.GetX(), it.first.GetZ());
- shape.setFillColor(sf::Color::Blue);
- 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 = it.second.GetId();
- }
- }
- switch (it.second.GetId()) {
+}
+
+
+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::Texture &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:
- shape.setFillColor(sf::Color::Black);
+ color = sf::Color::Transparent;
break;
case 7:
- shape.setFillColor(sf::Color::Yellow);
+ color = sf::Color::Yellow;
break;
case 1:
- shape.setFillColor(sf::Color::White);
+ color = sf::Color::White;
break;
case 11:
- shape.setFillColor(sf::Color::Red);
+ color = sf::Color::Red;
break;
case 10:
- shape.setFillColor(sf::Color::Red);
+ color = sf::Color::Red;
break;
case 3:
- shape.setFillColor(sf::Color(165, 42, 42));
+ color = sf::Color(139, 69, 69);
break;
case 13:
- shape.setFillColor(sf::Color(220, 220, 220));
+ 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:
- //std::cout << "Unknown id is " << it.second.GetId() << std::endl;
break;
}
- window->draw(shape);
+ image.setPixel(x, z, color);
}
- window->display();
-
-}
-
-
-bool Display::IsClosed() {
- return !window->isOpen();
-}
-
-void Display::SetPlayerPos(float x, float z) {
- float div = 1;
- 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);
-}
+ }
+ /*for (int i = 0; i < 16; i++) {
+ for (int j = 0; j < 16; j++) {
+ std::cout << std::hex << (int)pixels[i * 256 + j * 16] << (int)pixels[i * 256 + j * 16 + 1]
+ << (int)pixels[i * 256 + j * 16 + 2] <<(int) pixels[i * 256 + j * 16 + 3] << " ";
+ }
+ std::cout<<std::endl;
+ }*/
+ sf::Texture texture;
+ texture.create(16, 16);
+ texture.update(image);
+ sectionTextures[pos][renderLayer - pos.GetY() * 16] = texture;
+ return sectionTextures[pos][renderLayer - pos.GetY() * 16];
+} \ No newline at end of file