diff --git a/Meinkraft/Block.cpp b/Meinkraft/Block.cpp index 823bcdf..1d86086 100644 --- a/Meinkraft/Block.cpp +++ b/Meinkraft/Block.cpp @@ -1,12 +1,119 @@ #include "Block.h" +#include - - -Block::Block() +Block::Block(Vec3f position, int textureTop, int textureSides) { -} + this->position = position; + int rowNum = textureSides / 16; + int columnNum = textureSides % 16; + + float part = (float)1 / 16; + + float row = rowNum * part; + float column = columnNum * part; + float rowEnd = row + part; + float columnEnd = column + part; + + texColsSides = Vec2f(column, columnEnd); + texRowsSides = Vec2f(row, rowEnd); + + if (textureTop == textureSides) + { + texColsTop = texColsSides; + texRowsTop = texRowsSides; + } + else + { + if (textureTop != -1) + { + hasTopTexture = true; + + int rowNum = textureTop / 16; + int columnNum = textureTop% 16; + + float part = (float)1 / 16; + + row = rowNum * part; + column = columnNum * part; + rowEnd = row + part; + columnEnd = column + part; + + texColsTop = Vec2f(column, columnEnd); + texRowsTop = Vec2f(row, rowEnd); + } + } +} Block::~Block() { } + +void Block::draw() +{ + glPushMatrix(); + + glTranslatef(position.x, position.y, position.z); + + glBegin(GL_QUADS); + + glColor4f(1.0, 1.0, 1.0, 1.0); + + float column = texColsSides.x; + float columnEnd = texColsSides.y; + + float row = texRowsSides.x; + float rowEnd = texRowsSides.y; + + //Side size + glTexCoord2f(column, rowEnd); glVertex3f(0, 0, 0); //Linksonder + glTexCoord2f(columnEnd, rowEnd); glVertex3f(size, 0, 0); //Rechtsonder + glTexCoord2f(columnEnd, row); glVertex3f(size, size, 0); //Rechtsboven + glTexCoord2f(column, row); glVertex3f(0, size, 0); //Linksboven + + //Side 2 + glTexCoord2f(column, rowEnd); glVertex3f(0, 0, size); + glTexCoord2f(columnEnd, rowEnd); glVertex3f(size, 0, size); + glTexCoord2f(columnEnd, row); glVertex3f(size, size, size); + glTexCoord2f(column, row); glVertex3f(0, size, size); + + //Side 3 + glTexCoord2f(column, rowEnd); glVertex3f(0, 0, 0); + glTexCoord2f(column, row); glVertex3f(0, size, 0); + glTexCoord2f(columnEnd, row); glVertex3f(0, size, size); + glTexCoord2f(columnEnd, rowEnd); glVertex3f(0, 0, size); + + //Side 4 + glTexCoord2f(column, rowEnd); glVertex3f(size, 0, 0); + glTexCoord2f(column, row); glVertex3f(size, size, 0); + glTexCoord2f(columnEnd, row); glVertex3f(size, size, size); + glTexCoord2f(columnEnd, rowEnd); glVertex3f(size, 0, size); + + if (!hasTopTexture) + { + glEnd(); + glPopMatrix(); + return; + } + + //Different textures + column = texColsTop.x; + columnEnd = texColsTop.y; + + row = texRowsTop.x; + rowEnd = texRowsTop.y; + + //Bottom + glTexCoord2f(column, row); glVertex3f(0, 0, 0); + glTexCoord2f(column, rowEnd); glVertex3f(size, 0, 0); + glTexCoord2f(columnEnd, rowEnd); glVertex3f(size, 0, size); + glTexCoord2f(columnEnd, row); glVertex3f(0, 0, size); + + //Top + glTexCoord2f(column, row); glVertex3f(0, size, 0); + glTexCoord2f(column, rowEnd); glVertex3f(size, size, 0); + glTexCoord2f(columnEnd, rowEnd); glVertex3f(size, size, size); + glTexCoord2f(columnEnd, row); glVertex3f(0, size, size); + glEnd(); + glPopMatrix(); +} diff --git a/Meinkraft/Block.h b/Meinkraft/Block.h index 1283876..603a963 100644 --- a/Meinkraft/Block.h +++ b/Meinkraft/Block.h @@ -1,8 +1,23 @@ #pragma once +#include "Vector.h" + class Block { +private: + float size = 1; + bool hasTopTexture = false; + + Vec2f texColsTop; + Vec2f texRowsTop; + Vec2f texColsSides; + Vec2f texRowsSides; + public: - Block(); + Block(Vec3f position, int top, int sides); ~Block(); + + void draw(void); + + Vec3f position; }; diff --git a/Meinkraft/Chunk.cpp b/Meinkraft/Chunk.cpp deleted file mode 100644 index 5cc415d..0000000 --- a/Meinkraft/Chunk.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "Chunk.h" - - - -Chunk::Chunk() -{ -} - - -Chunk::~Chunk() -{ -} diff --git a/Meinkraft/Chunk.h b/Meinkraft/Chunk.h deleted file mode 100644 index 3da758d..0000000 --- a/Meinkraft/Chunk.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once -class Chunk -{ -public: - Chunk(); - ~Chunk(); -}; - diff --git a/Meinkraft/Meinkraft.cpp b/Meinkraft/Meinkraft.cpp index 12cc721..078d225 100644 --- a/Meinkraft/Meinkraft.cpp +++ b/Meinkraft/Meinkraft.cpp @@ -1,13 +1,44 @@ #include "Meinkraft.h" -#include #include #include #include "Player.h" #include "StateHandler.h" +#define _USE_MATH_DEFINES +#include + +#include "stb_image.h" +#include "stb_perlin.h" + int Meinkraft::width = 0; int Meinkraft::height = 0; +GLuint Meinkraft::texture = NULL; + +void Meinkraft::loadTexture(void) +{ + //Load Textures :: blocks + int img_width, img_height, bpp; + unsigned char* imgData = stbi_load("/resources/terrain.png", &img_width, &img_height, &bpp, 4); + + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + + glTexImage2D(GL_TEXTURE_2D, + 0, //level + GL_RGBA, //internal format + img_width, //width + img_height, //height + 0, //border + GL_RGBA, //data format + GL_UNSIGNED_BYTE, //data type + imgData); //data + + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + stbi_image_free(imgData); +} void Meinkraft::init() { @@ -15,6 +46,8 @@ void Meinkraft::init() statehandler = StateHandler::getInstance(); //cursor = Cursor::getInstance(); + loadTexture(); + lastFrameTime = 0; glClearColor(0.7f, 0.7f, 1.0f, 1.0f); @@ -37,6 +70,9 @@ void Meinkraft::draw() glMatrixMode(GL_MODELVIEW); glLoadIdentity(); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, texture); + statehandler->draw(); //cursor->draw(); diff --git a/Meinkraft/Meinkraft.h b/Meinkraft/Meinkraft.h index a4b78d9..215da95 100644 --- a/Meinkraft/Meinkraft.h +++ b/Meinkraft/Meinkraft.h @@ -3,6 +3,7 @@ class Player; class StateHandler; #include "Vector.h" +#include class KeyboardState { @@ -16,6 +17,8 @@ public: class Meinkraft { +private: + void loadTexture(void); public: void init(); void draw(); @@ -32,4 +35,6 @@ public: Vec2f mousePosition; float lastFrameTime; + + static GLuint texture; }; \ No newline at end of file diff --git a/Meinkraft/Meinkraft.vcxproj b/Meinkraft/Meinkraft.vcxproj index 39b9762..f42101e 100644 --- a/Meinkraft/Meinkraft.vcxproj +++ b/Meinkraft/Meinkraft.vcxproj @@ -151,7 +151,6 @@ - @@ -168,7 +167,6 @@ - diff --git a/Meinkraft/Meinkraft.vcxproj.filters b/Meinkraft/Meinkraft.vcxproj.filters index 2755ef4..73f2a87 100644 --- a/Meinkraft/Meinkraft.vcxproj.filters +++ b/Meinkraft/Meinkraft.vcxproj.filters @@ -39,9 +39,6 @@ Source Files - - Source Files\World - Source Files\World @@ -95,9 +92,6 @@ Header Files - - Header Files - Header Files diff --git a/Meinkraft/StateHandler.cpp b/Meinkraft/StateHandler.cpp index 44e74bb..e259a36 100644 --- a/Meinkraft/StateHandler.cpp +++ b/Meinkraft/StateHandler.cpp @@ -7,8 +7,8 @@ StateHandler* StateHandler::instance = nullptr; StateHandler::StateHandler() { available = false; - CState = MENU; - CurrentState = new MenuState(); + CState = WORLD; + CurrentState = new WorldState(); CurrentState->init(); available = true; } diff --git a/Meinkraft/World.cpp b/Meinkraft/World.cpp index 81f73cf..6a2d25d 100644 --- a/Meinkraft/World.cpp +++ b/Meinkraft/World.cpp @@ -1,5 +1,5 @@ #include "World.h" - +#include "Block.h" World::World() @@ -10,3 +10,13 @@ World::World() World::~World() { } + +void World::draw(void) +{ + Block b = Block(Vec3f(0, 0, 0), 1, 3); + b.draw(); +} + +void World::update(float deltaTime) +{ +} diff --git a/Meinkraft/World.h b/Meinkraft/World.h index 0b7264d..d3fd8b9 100644 --- a/Meinkraft/World.h +++ b/Meinkraft/World.h @@ -4,5 +4,8 @@ class World public: World(); ~World(); + + void draw(void); + void update(float deltaTime); }; diff --git a/Meinkraft/WorldState.cpp b/Meinkraft/WorldState.cpp index ab260c8..10f96da 100644 --- a/Meinkraft/WorldState.cpp +++ b/Meinkraft/WorldState.cpp @@ -13,16 +13,20 @@ WorldState::~WorldState() void WorldState::init(void) { + world = new World(); } void WorldState::exit(void) { + delete world; } void WorldState::draw(void) { + world->draw(); } void WorldState::update(float deltaTime) { + world->update(deltaTime); } diff --git a/Meinkraft/WorldState.h b/Meinkraft/WorldState.h index 26d2787..7b3f48e 100644 --- a/Meinkraft/WorldState.h +++ b/Meinkraft/WorldState.h @@ -1,8 +1,11 @@ #pragma once #include "State.h" +#include "World.h" class WorldState : public State { +private: + World* world; public: WorldState(); ~WorldState(); diff --git a/Meinkraft/resources/terrain.png b/Meinkraft/resources/terrain.png new file mode 100644 index 0000000..041e1c0 Binary files /dev/null and b/Meinkraft/resources/terrain.png differ diff --git a/Meinkraft/resources/theme.wav b/Meinkraft/resources/theme.wav new file mode 100644 index 0000000..5af30a7 Binary files /dev/null and b/Meinkraft/resources/theme.wav differ