@@ -0,0 +1,27 @@
#include "GUI.h"

GUI::GUI()
{
for (int i = 0; i < m_nrButtons; i++)
{
//m_buttons[i].initalize();
}
}

GUI::~GUI()
{
}

void GUI::buttonAction(unsigned int action)
{
switch (GUIButtonActions(action))
{
case Powers:

break;

case Populace:

break;
}
}
@@ -0,0 +1,23 @@
#pragma once
#include "Button.h"

class GUI : public IButtonListener
{
public:
GUI();
~GUI();

virtual void buttonAction(unsigned int action) override;

private:
enum GUIButtonActions
{
Powers,
Populace

};

static const int m_nrButtons = 5;
Button m_buttons[m_nrButtons];

};
@@ -78,15 +78,20 @@
</ClCompile>
<Link>
<AdditionalLibraryDirectories>..\SFML-2.4.1\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>sfml-graphics-d.lib;sfml-window-d.lib;sfml-system-d.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>sfml-graphics-d.lib;sfml-window-d.lib;sfml-system-d.lib;openal32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>..\SFML-2.4.1\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>..\SFML-2.4.1\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>sfml-system-d.lib;sfml-window-d.lib;sfml-graphics-d.lib;openal32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
@@ -118,7 +123,10 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="Animation.h" />
<ClInclude Include="Button.h" />
<ClInclude Include="CameraController.h" />
<ClInclude Include="Clickable.h" />
<ClInclude Include="Constants.h" />
<ClInclude Include="Controller.h" />
<ClInclude Include="Entity.h" />
@@ -127,25 +135,32 @@
<ClInclude Include="GameManager.h" />
<ClInclude Include="GameState.h" />
<ClInclude Include="GameStatePlaying.h" />
<ClInclude Include="GUI.h" />
<ClInclude Include="IsometricProjection.h" />
<ClInclude Include="Map.h" />
<ClInclude Include="MapChunk.h" />
<ClInclude Include="MapGenerator.h" />
<ClInclude Include="MapLoader.h" />
<ClInclude Include="ResourceManager.h" />
<ClInclude Include="SimplexNoise.h" />
<ClInclude Include="Tile.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Animation.cpp" />
<ClCompile Include="Button.cpp" />
<ClCompile Include="CameraController.cpp" />
<ClCompile Include="Clickable.cpp" />
<ClCompile Include="Controller.cpp" />
<ClCompile Include="Entity.cpp" />
<ClCompile Include="EventManager.cpp" />
<ClCompile Include="GameManager.cpp" />
<ClCompile Include="GameStatePlaying.cpp" />
<ClCompile Include="GUI.cpp" />
<ClCompile Include="Main.cpp" />
<ClCompile Include="Map.cpp" />
<ClCompile Include="MapChunk.cpp" />
<ClCompile Include="MapGenerator.cpp" />
<ClCompile Include="MapLoader.cpp" />
<ClCompile Include="ResourceManager.cpp" />
<ClCompile Include="SimplexNoise.cpp" />
<ClCompile Include="Tile.cpp" />
@@ -63,6 +63,21 @@
<ClInclude Include="Map.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MapLoader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Clickable.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Button.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="GUI.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Animation.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Entity.cpp">
@@ -104,5 +119,20 @@
<ClCompile Include="Map.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MapLoader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Clickable.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Button.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="GUI.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Animation.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
@@ -1,13 +1,39 @@
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Event.hpp>
#include <fstream>

#include "MapChunk.h"
#include "GameManager.h"

int main()
{
GameManager gm;
gm.run();


/*char* memblock;
std::streampos size = 4;
char toWrite[4] = { 0b00000001, 0b00010000, 0b01111111, 0b01000000 };
memblock = toWrite;
std::string file = "Resources/Maps/";
file += toWrite;
std::ofstream oss(file, std::ios::binary);
if (oss.is_open())
{
oss.write(memblock, size);
oss.close();
}
std::ofstream os;
os.open("Resources/Maps/Savename.txt", std::ios::binary);
os.write(memblock, size);
os.close();
std::ifstream is;
std::streampos begin, end;
is.open("Resources/Maps/Savename.txt", std::ios::binary);
begin = is.tellg();
is.seekg(0, std::ios::end);
end = is.tellg();
is.close();
std::printf("size of file: %i bytes", end - begin);*/

return 0;
}
@@ -1,23 +1,18 @@
#include "Map.h"
#include "CameraController.h"
#include <fstream>
#include "Constants.h"
#include <SFML/Graphics/RenderWindow.hpp>

Map::Map(CameraController* cameraController) :
Map::Map(CameraController* cameraController, unsigned int seed) :
m_cameraController(cameraController)
{
sf::Vector2f pos;
int seed = std::rand();
for (int i = 0; i < CHUNKS_X; i++)
{
for (int j = 0; j < CHUNKS_Y; j++)
{
pos.x = i * ChunkNrTilesX * TileWidth;
pos.y = j * ChunkNrTilesY * TileHeight;
int index = (i + j * CHUNKS_X);
m_mapChunk[index].setPosition(pos);
m_mapChunk[index].setup(0.001f, seed);
}
}
m_seed = seed;
if (seed == 0)
m_seed = std::rand();

readFile(sf::Vector2f(0.0f, 0.0f));
}

Map::~Map()
@@ -36,6 +31,52 @@ void Map::drawWorld(sf::RenderWindow* window)
window->draw(m_mapChunk[i]);
}

int charArrayToInt(char* arr)
{
int val = 0;
for (int i = 0; i < 4; i++)
{
val += arr[i] << (24 - i * 8);
}
return val;
}

void Map::readFile(sf::Vector2f &pos)
{
std::string filepath = Constants::Filepaths::MapsFolder;
std::string filename;

// Data should be arranged in an orderly fashion so all we have to do is retrieve it
for (int i = 0; i < CHUNKS_X; i++)
{
for (int j = 0; j < CHUNKS_Y; j++)
{
pos.x = float(i * ChunkNrTilesX * TileWidth);
pos.y = float(j * ChunkNrTilesY * TileHeight);
int posX = (int)pos.x, posY = (int)pos.y;
filename = filepath + std::to_string(posX) + "-" + std::to_string(posY) + ".txt";
std::ifstream is(filename, std::ios::binary);
bool fileExists = is.is_open();
is.close();
int index = (i + j * CHUNKS_X);
m_mapChunk[index].setPosition(pos);
if (fileExists)
{
m_mapChunk[index].setupFromData(filename.c_str());
}
else
{
m_mapChunk[index].setupNew(0.001f, m_seed);
}
}
}
}

unsigned int Map::getSeed() const
{
return m_seed;
}

void Map::onButtonPress(Controller::Buttons button)
{

@@ -11,22 +11,25 @@ class CameraController;
class Map : public IControllerListener
{
public:
Map(CameraController* cameraController);
Map(CameraController* cameraController, unsigned int seed = 0);
~Map();

void setupController(Controller* controller);
void drawWorld(sf::RenderWindow* window);

void readFile(sf::Vector2f &pos);
unsigned int getSeed() const;


private:
static const int CHUNKS_X = 4;
static const int CHUNKS_Y = 4;
static const int NR_CHUNKS = 16;
static const int CHUNKS_X = 12;
static const int CHUNKS_Y = 12;
static const int NR_CHUNKS = CHUNKS_X * CHUNKS_Y;
unsigned int m_seed;

CameraController* m_cameraController;
MapChunk m_mapChunk[NR_CHUNKS];

// Inherited via IControllerListener
virtual void onButtonPress(Controller::Buttons button) override;
virtual void onButtonHold(Controller::Buttons button, float time) override;
};
@@ -1,6 +1,9 @@
#include "MapChunk.h"
#include "ResourceManager.h"
#include "MapGenerator.h"
#include <fstream>
#include <SFML/System/Lock.hpp>
#include <SFML/System/Mutex.hpp>
#include <SFML/Graphics/RenderTarget.hpp>


@@ -12,22 +15,21 @@ MapChunk::MapChunk()
m_backgroundTilesetTexture = &ResourceManager::getInstance().getTexture(BACKGROUND_TILESET_TEXTURE);
m_objectTilesetTexture = &ResourceManager::getInstance().getTexture(OBJECT_TILESET_TEXTURE);


sf::Vector2f pos = getPosition();

}

MapChunk::~MapChunk()
{

}

void MapChunk::setup(float frequency, int seed)
void MapChunk::setupNew(float frequency, int seed)
{
sf::Vector2f pos = getPosition();

int tileType[ChunkNrTilesX * ChunkNrTilesY];
int heightMap[ChunkNrTilesX * ChunkNrTilesY];
//int objectType[ChunkNrTilesX * ChunkNrTilesY];

MapGenerator::generateMap(seed, tileType, heightMap, ChunkNrTilesX, ChunkNrTilesY, frequency, pos);

@@ -42,8 +44,6 @@ void MapChunk::setup(float frequency, int seed)
int tilenr = tileType[index];

sf::Vector2i uvcoords = findTextureCoords(tilenr);
//std::printf("index: %u tile nr: %i, u: %u, v: %u\n", index, tilenr, uvcoords.x, uvcoords.y);


sf::Vertex* quad = &m_tileVertexArray[index * 4];

@@ -59,13 +59,111 @@ void MapChunk::setup(float frequency, int seed)
quad[3].texCoords = sf::Vector2f((float)(uvcoords.x * TileWidth), (float)((uvcoords.y + 1) * TileHeight));
}
}
/*size_t limit = m_tileVertexArray.getVertexCount();
for (size_t i = 0; i < limit; i++)
writeToFile();
}

// Takes a char array with a size of 2 and converts it to a short
unsigned short charArrayToShort(char* arr)
{
unsigned short val = (unsigned char)arr[0];
unsigned short val1 = (unsigned char)arr[1];
val1 <<= 8;
val += val1;

return val;
}

void retrieveData(unsigned short in, unsigned int &outHeight, unsigned int &outTile, unsigned int &outObj)
{
// No need to mask here
outHeight = in >> 11;
// Mask to retrieve the relevant bits
outTile = (in & 0b0000011110000000);
outTile = outTile >> 7;
outObj = (in & 0b0000000001111111);
}

void MapChunk::setupFromData(const char* file)
{
std::streampos size = 2, begin, end;
char* memblock = new char[size];
std::ifstream is(file, std::ios::binary);
if (!is.is_open()) // No such file was found. It has to be generated
{
return;
}
// TODO: Do a check to see if the size of the file match the estimate size

sf::Vector2f pos = getPosition();

m_tileVertexArray.setPrimitiveType(sf::Quads);
m_tileVertexArray.resize(VertArrSize);

for (unsigned int i = 0; i < ChunkNrTilesX; i++)
{
for (unsigned int j = 0; j < ChunkNrTilesY; j++)
{
unsigned int index = i + j * ChunkNrTilesX;
unsigned int height, tileType, objectType;
is.read(memblock, size);
unsigned short _16bit = charArrayToShort(memblock);
retrieveData(_16bit, height, tileType, objectType);

sf::Vector2i uvcoords = findTextureCoords(tileType);
sf::Vertex* quad = &m_tileVertexArray[index * 4];

m_tiles[index].initalize(index, this);
m_tiles[index].setTextureID(tileType);
m_tiles[index].setVertexArrayFirstIndex(index * 4);
m_tiles[index].setPosition((float)(i * TileWidth), (float)(j * TileHeight));
m_tiles[index].setHeight(height);

quad[0].texCoords = sf::Vector2f((float)(uvcoords.x * TileWidth), (float)(uvcoords.y * TileHeight));
quad[1].texCoords = sf::Vector2f((float)((uvcoords.x + 1) * TileWidth), (float)(uvcoords.y * TileHeight));
quad[2].texCoords = sf::Vector2f((float)((uvcoords.x + 1) * TileWidth), (float)((uvcoords.y + 1) * TileHeight));
quad[3].texCoords = sf::Vector2f((float)(uvcoords.x * TileWidth), (float)((uvcoords.y + 1) * TileHeight));
}
}
delete[size] memblock;

}

// arr has to be an array size 2
void intToCharArray(char* arr, unsigned int height, unsigned int tileType, unsigned int objectType)
{
tileType <<= 7;
height <<= 11;
unsigned int val = objectType | height | tileType;
arr[0] = val;
val >>= 8;
arr[1] = val;
}

void MapChunk::writeToFile()
{
std::string filename = Constants::Filepaths::MapsFolder;
sf::Vector2f pos = getPosition();
int posX = (int)pos.x, posY = (int)pos.y;
filename += std::to_string(posX) + "-" + std::to_string(posY) + ".txt";
std::streampos size = 2;
char* memblock = new char[size];
std::ofstream os(filename, std::ios::binary);
if (!os.is_open()) return; // Error creating and opening file.

for (unsigned int i = 0; i < ChunkNrTilesX; i++)
{
int xPos = int(m_tileVertexArray[i].position.x);
int yPos = int(m_tileVertexArray[i].position.y);
std::printf("[x: %i, y: %i] ", xPos, yPos);
}*/
for (unsigned int j = 0; j < ChunkNrTilesY; j++)
{
unsigned int index = i + j * ChunkNrTilesX;
unsigned int height = m_tiles[index].getHeight();
unsigned int tileType = m_tiles[index].getTextureID();
unsigned int objectType = 0;

intToCharArray(memblock, height, tileType, objectType);
os.write(memblock, size);
}
}
delete[size] memblock;
}

// Helper function for finding the uv coordinates in the texture
@@ -127,3 +225,7 @@ void MapChunk::updateTileHeight(int firstVertexIndex, int chunkIndex, int height
quad[i].color = col;
}
}

void MapChunk::updateTileID(int ID)
{
}
@@ -23,7 +23,9 @@ class MapChunk : public Entity
MapChunk();
virtual ~MapChunk();

void setup(float frequency, int seed);
void setupNew(float frequency, int seed);
void setupFromData(const char* file);
void writeToFile();

private:
sf::Vector2i findTextureCoords(int tileNr);
@@ -34,6 +36,7 @@ class MapChunk : public Entity
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;

void updateTileHeight(int firstVertexIndex, int chunkIndex, int height);
void updateTileID(int ID);

private:
Tile m_tiles[ChunkNrTilesX * ChunkNrTilesY];
@@ -0,0 +1,9 @@
#include "MapLoader.h"

MapLoader::MapLoader()
{
}

MapLoader::~MapLoader()
{
}
@@ -0,0 +1,12 @@
#pragma once

class MapLoader
{
public:
MapLoader();
~MapLoader();


private:

};
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,25 @@
[height] [tileindex] [object]
5 bit 4 bit 7

Chunk 0 Chunk 1 etc... Chunk 15
0000000000000000 0000000000000000 0000000000000000
|___||__||_____| |___||__||_____| |___||__||_____|
hei tile obj hei tile obj hei tile obj

filename will be:
positionX-positionY.txt
0-0.txt




before:
h = 2, t = 3 00010 - h 0011 - t

after:
00010000 00000000 - h = 4096
00000001 10000000 - t = 384
| =

00010001 10000000 = 4480
17 128
@@ -2,7 +2,7 @@
#include "MapChunk.h"

Tile::Tile()
:m_height(0)
:m_height(0), m_hasChanges(false)
{

}
@@ -30,12 +30,12 @@ void Tile::setVertexArrayFirstIndex(int firstVertexIndex)
m_vertexArrayFirstIndex = firstVertexIndex;
}

int Tile::getVertexArrayIndices() const
unsigned int Tile::getVertexArrayIndices() const
{
return m_vertexArrayFirstIndex;
}

void Tile::setHeight(int newHeight)
void Tile::setHeight(unsigned int newHeight)
{
int oldHeight = m_height;
m_height = newHeight;
@@ -47,17 +47,18 @@ void Tile::setHeight(int newHeight)
m_mapChunk->updateTileHeight(m_vertexArrayFirstIndex, m_chunkIndex, newHeight);
}

int Tile::getHeight() const
unsigned int Tile::getHeight() const
{
return m_height;
}

void Tile::setTextureID(int ID)
void Tile::setTextureID(unsigned int ID)
{
m_textureID = ID;
//TODO: reflect changes in map object by updating the vertex array
}

int Tile::getTextureID() const
unsigned int Tile::getTextureID() const
{
return m_textureID;
}
@@ -66,3 +67,8 @@ int Tile::getChunkIndex() const
{
return m_chunkIndex;
}

void Tile::setHasChanges()
{
m_hasChanges = true;
}
@@ -11,20 +11,22 @@ class Tile : public sf::Transformable
void initalize(int chunkIndex, class MapChunk* mapChunk);

void setVertexArrayFirstIndex(int firstVertexIndex);
int getVertexArrayIndices() const;
unsigned int getVertexArrayIndices() const;

void setHeight(int newHeight);
int getHeight() const;
void setHeight(unsigned int newHeight);
unsigned int getHeight() const;

void setTextureID(int ID);
int getTextureID() const;
void setTextureID(unsigned int ID);
unsigned int getTextureID() const;

int getChunkIndex() const;
void setHasChanges();

private:
int m_vertexArrayFirstIndex;
int m_height;
int m_textureID;
int m_chunkIndex;
unsigned int m_vertexArrayFirstIndex;
unsigned int m_height;
unsigned int m_textureID;
unsigned int m_chunkIndex;
bool m_hasChanges;
class MapChunk* m_mapChunk;
};