diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-04-23 15:39:59 +0200 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-04-21 15:39:59 +0200 |
commit | d46f3a7180afdb5213afc80c97ae5fc8db43248a (patch) | |
tree | 2e00ab460474084042176d71df3f6f2cee8d3d63 /Display.cpp | |
parent | 2017-04-21 (diff) | |
download | AltCraft-d46f3a7180afdb5213afc80c97ae5fc8db43248a.tar AltCraft-d46f3a7180afdb5213afc80c97ae5fc8db43248a.tar.gz AltCraft-d46f3a7180afdb5213afc80c97ae5fc8db43248a.tar.bz2 AltCraft-d46f3a7180afdb5213afc80c97ae5fc8db43248a.tar.lz AltCraft-d46f3a7180afdb5213afc80c97ae5fc8db43248a.tar.xz AltCraft-d46f3a7180afdb5213afc80c97ae5fc8db43248a.tar.zst AltCraft-d46f3a7180afdb5213afc80c97ae5fc8db43248a.zip |
Diffstat (limited to '')
-rw-r--r-- | Display.cpp | 53 |
1 files changed, 28 insertions, 25 deletions
diff --git a/Display.cpp b/Display.cpp index ef7dd9a..7c3e59e 100644 --- a/Display.cpp +++ b/Display.cpp @@ -4,9 +4,11 @@ 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() { @@ -16,19 +18,19 @@ Display::~Display() { 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(); + 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) @@ -42,7 +44,10 @@ void Display::renderWorld() { if (sectionIt->first.GetY() != renderLayer / 16) continue; Section §ion = sectionIt->second; - sf::Texture &texture = GetSectionTexture(sectionIt->first); + 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); @@ -178,6 +183,9 @@ void Display::pollEvents() { 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: @@ -212,15 +220,17 @@ void Display::MainLoop() { 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(); + { + 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) { +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]; @@ -270,16 +280,9 @@ sf::Texture &Display::GetSectionTexture(PositionI pos) { image.setPixel(x, z, color); } } - /*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; + /*sf::Texture texture; texture.create(16, 16); - texture.update(image); - sectionTextures[pos][renderLayer - pos.GetY() * 16] = texture; + texture.update(image);*/ + sectionTextures[pos][renderLayer - pos.GetY() * 16] = image; return sectionTextures[pos][renderLayer - pos.GetY() * 16]; }
\ No newline at end of file |