summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-04-23 15:39:59 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-04-21 15:39:59 +0200
commitd46f3a7180afdb5213afc80c97ae5fc8db43248a (patch)
tree2e00ab460474084042176d71df3f6f2cee8d3d63
parent2017-04-21 (diff)
downloadAltCraft-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
-rw-r--r--CMakeLists.txt3
-rw-r--r--Display.cpp53
-rw-r--r--Display.hpp5
-rw-r--r--Game.cpp14
-rw-r--r--Section.cpp14
-rw-r--r--Section.hpp2
-rw-r--r--World.cpp15
-rw-r--r--World.hpp10
8 files changed, 65 insertions, 51 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 65d0205..811443d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,7 +2,8 @@ cmake_minimum_required(VERSION 3.5)
project(AltCraft)
set(CMAKE_CXX_STANDARD 14)
-set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic-errors -w -Werror -g")
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic-errors -w -Werror")
+set(CMAKE_CXX_FLASG "${CMAKE_CXX_FLAGS} -g -O0")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -pg")
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 &section = 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
diff --git a/Display.hpp b/Display.hpp
index d7a9089..3d54482 100644
--- a/Display.hpp
+++ b/Display.hpp
@@ -8,7 +8,8 @@
class Display {
sf::RenderWindow *window;
- std::map<PositionI, std::array<sf::Texture,16>> sectionTextures;
+ std::map<PositionI, std::array<sf::Image,16>> sectionTextures;
+ //std::map<PositionI, std::array<sf::Texture,16>> sectionTextures;
World *world;
bool isGameStarted = false;
std::condition_variable &gameStartWaiter;
@@ -21,7 +22,7 @@ class Display {
void Update();
- sf::Texture &GetSectionTexture(PositionI pos);
+ sf::Image &GetSectionTexture(PositionI pos);
//gameState vars
sf::Vector2f mousePos;
diff --git a/Game.cpp b/Game.cpp
index 8674942..c3adaae 100644
--- a/Game.cpp
+++ b/Game.cpp
@@ -7,8 +7,8 @@ Game::Game() {
m_display = new Display(1280, 720, "AltCraft", &m_world, gameStartWaiter);
m_nc = new NetworkClient("127.0.0.1", 25565, "HelloOne");
Packet &response = *m_nc->GetPacket();
- if (response.GetId()!=0x02){
- std::cout<< response.GetId()<<std::endl;
+ if (response.GetId() != 0x02) {
+ std::cout << response.GetId() << std::endl;
throw 127;
}
PacketParser::Parse(response, Login);
@@ -20,11 +20,11 @@ Game::Game() {
}
Game::~Game() {
- std::cout<<"Stopping game thread..."<<std::endl;
+ std::cout << "Stopping game thread..." << std::endl;
m_gameThread.join();
- std::cout<<"Stopping graphics..."<<std::endl;
+ std::cout << "Stopping graphics..." << std::endl;
delete m_display;
- std::cout<<"Stopping network..."<<std::endl;
+ std::cout << "Stopping network..." << std::endl;
delete m_nc;
}
@@ -38,7 +38,7 @@ void Game::MainLoop() {
}
void Game::ParsePackets() {
- Packet *packetPtr =m_nc->GetPacket();
+ Packet *packetPtr = m_nc->GetPacket();
if (!packetPtr) {
using namespace std::chrono_literals;
std::this_thread::sleep_for(16ms);
@@ -154,6 +154,6 @@ void Game::ParsePackets() {
}
void Game::Exec() {
- m_gameThread = std::thread(&Game::MainLoop,this);
+ m_gameThread = std::thread(&Game::MainLoop, this);
m_display->MainLoop();
}
diff --git a/Section.cpp b/Section.cpp
index a04ed41..8df5953 100644
--- a/Section.cpp
+++ b/Section.cpp
@@ -34,7 +34,12 @@ Section::~Section() {
Block &Section::GetBlock(PositionI pos) {
if (m_dataBlocks != nullptr) {
- Parse();
+ std::mutex parseMutex;
+ std::unique_lock<std::mutex> parseLocker(parseMutex);
+ parseWaiter.wait(parseLocker);
+ while (m_dataBlocks != nullptr) {
+ parseWaiter.wait(parseLocker);
+ }
}
return m_blocks[pos.GetY() * 256 + pos.GetZ() * 16 + pos.GetX()];
}
@@ -89,6 +94,7 @@ void Section::Parse() {
m_dataLight = nullptr;
delete[] m_dataSkyLight;
m_dataSkyLight = nullptr;
+ parseWaiter.notify_all();
}
Section &Section::operator=(Section other) {
@@ -109,14 +115,14 @@ void Section::swap(Section &other) {
Section::Section(const Section &other) {
m_dataBlocksLen = other.m_dataBlocksLen;
m_dataBlocks = new byte[m_dataBlocksLen];
- std::copy(other.m_dataBlocks,other.m_dataBlocks+m_dataBlocksLen,m_dataBlocks);
+ std::copy(other.m_dataBlocks, other.m_dataBlocks + m_dataBlocksLen, m_dataBlocks);
m_dataLight = new byte[2048];
- std::copy(other.m_dataLight,other.m_dataLight+2048,m_dataLight);
+ std::copy(other.m_dataLight, other.m_dataLight + 2048, m_dataLight);
if (other.m_dataSkyLight) {
m_dataSkyLight = new byte[2048];
- std::copy(other.m_dataSkyLight,other.m_dataSkyLight+2048,m_dataSkyLight);
+ std::copy(other.m_dataSkyLight, other.m_dataSkyLight + 2048, m_dataSkyLight);
}
m_palette = other.m_palette;
diff --git a/Section.hpp b/Section.hpp
index 14ea2f8..8e1a0d1 100644
--- a/Section.hpp
+++ b/Section.hpp
@@ -2,6 +2,7 @@
#include <vector>
#include <map>
+#include <condition_variable>
#include "Block.hpp"
#include "Field.hpp"
@@ -17,6 +18,7 @@ class Section {
byte *m_dataSkyLight = nullptr;
byte m_bitsPerBlock = 0;
std::vector<Block> m_blocks;
+ std::condition_variable parseWaiter;
public:
void Parse();
diff --git a/World.cpp b/World.cpp
index 4a750ab..c19f88c 100644
--- a/World.cpp
+++ b/World.cpp
@@ -5,7 +5,6 @@
void World::ParseChunkData(Packet packet) {
int chunkX = packet.GetField(0).GetInt();
int chunkZ = packet.GetField(1).GetInt();
- std::cout << "Parsing chunk " << chunkX << "x" << chunkZ << std::endl;
bool isGroundContinuous = packet.GetField(2).GetBool();
std::bitset<16> bitmask(packet.GetField(3).GetVarInt());
int entities = packet.GetField(5).GetVarInt();
@@ -23,7 +22,7 @@ void World::ParseChunkData(Packet packet) {
if (bitmask[i]) {
size_t len = 0;
m_sections[PositionI(chunkX, chunkZ, i)] = ParseSection(content, len);
- m_sectionToParse.push(m_sections.find(PositionI(chunkX,chunkZ,i)));
+ m_sectionToParse.push(m_sections.find(PositionI(chunkX, chunkZ, i)));
m_parseSectionWaiter.notify_one();
content += len;
}
@@ -80,27 +79,29 @@ Section World::ParseSection(byte *data, size_t &dataLen) {
}
World::~World() {
- isContinue=false;
+ isContinue = false;
m_parseSectionWaiter.notify_all();
m_sectionParseThread.join();
}
void World::SectionParsingThread() {
- while (isContinue){
+ while (isContinue) {
std::unique_lock<std::mutex> sectionParseLocker(m_parseSectionMutex);
m_parseSectionWaiter.wait(sectionParseLocker);
- while (!m_sectionToParse.size()==0 && isContinue) {
+ while (m_sectionToParse.size() == 0 && isContinue) {
m_parseSectionWaiter.wait(sectionParseLocker);
}
- while (m_sectionToParse.size()>0){
+ while (m_sectionToParse.size() > 0) {
auto it = m_sectionToParse.front();
m_sectionToParse.pop();
it->second.Parse();
+ /*std::cout << "Parsed chunk" << it->first.GetX() << "x" << it->first.GetY() << "x" << it->first.GetZ()
+ << std::endl;*/
}
}
}
World::World() {
- m_sectionParseThread = std::thread(&World::SectionParsingThread,this);
+ m_sectionParseThread = std::thread(&World::SectionParsingThread, this);
}
diff --git a/World.hpp b/World.hpp
index ac3433b..89873b6 100644
--- a/World.hpp
+++ b/World.hpp
@@ -19,15 +19,15 @@ private:
//utility vars
World(const World& other);
World&operator=(const World &other);
- //utility methods
+ bool isContinue=true;
+ std::mutex m_parseSectionMutex;
+ std::condition_variable m_parseSectionWaiter;
std::thread m_sectionParseThread;
std::queue<std::map<PositionI,Section>::iterator> m_sectionToParse;
+ //utility methods
+ void SectionParsingThread();
//game vars
int m_dimension = 0;
//game methods
- std::mutex m_parseSectionMutex;
- std::condition_variable m_parseSectionWaiter;
Section ParseSection(byte *data, size_t &dataLen);
- void SectionParsingThread();
- bool isContinue=true;
}; \ No newline at end of file