diff --git a/README.md b/README.md index 771c06a..11ad267 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ It is a fork of [glowy2d](https://github.com/Rasie1/glowy2d/) engine previously - cpptoml - OpenGL, GLEW, glfw3 - zlib, libpng +- tinyobjloader ## How to build diff --git a/include/graphics/Camera.h b/include/graphics/Camera.h index b0da666..27a0cef 100644 --- a/include/graphics/Camera.h +++ b/include/graphics/Camera.h @@ -10,10 +10,10 @@ class Camera Camera(); //Returns Projection and View product matrix - const mat2& getMatrix(); + const mat3& getMatrix(); - void setPosition(const glm::vec2& position); - void addPosition(const glm::vec2& offset); + void setPosition(const glm::vec3& position); + void addPosition(const glm::vec3& offset); void addZoom(const float offset); void setDefaultZoom(); @@ -23,10 +23,10 @@ class Camera void enableTransform(); private: - mat2 matrix; + mat3 matrix; bool noTransform; - mat2 noTransformMatrix; + mat3 noTransformMatrix; }; } diff --git a/include/graphics/Layer.h b/include/graphics/Layer.h deleted file mode 100644 index 7103dfe..0000000 --- a/include/graphics/Layer.h +++ /dev/null @@ -1,88 +0,0 @@ -#pragma once -#include "g2dMath.h" -#include "math/quad.h" -#include "platforms/Tex2D.h" - -namespace glowy3d -{ - -class VertexPosBuffer; -class TexCoordsBuffer; -class VertexInterleavedBuffer; -class UniformMat2; -class Sprite; -class Texture; -class TextureAtlas; -class TextureData; - -class Layer -{ -public: - //Call Layer::draw every frame. After adding sprites - //call Layer::updateBuffer - Layer(uint maxSprites, uint atlasSize = 1024); - ~Layer(); - - void draw(); - void updateBuffer(); - - Sprite* add(Texture* texture); - Sprite* add(const char *imagePath); - Texture* addTexture(const char *imagePath); - Texture* addTexture(TextureData* imageData); - - void setShader(unsigned shader_id); - void setTexture(const Tex2D& tex); - - vec2 getPosition() const; - void setPosition(const vec2& pos); - void addPosition(const vec2& offset); - - float getRotation() const; - void setRotation(const float rotation); - void addRotation(const float offset); - - vec2 getScale() const; - void setScale(const vec2& scale); - void addScale(const vec2& offset); - - float getScaleX() const; - void setScaleX(const float scale); - void addScaleX(const float offset); - - float getScaleY() const; - void setScaleY(const float scale); - void addScaleY(const float offset); - - vec2 getOrigin() const; - void setOrigin(const vec2& point); - - float getZOrder() const; - void setZOrder(const float zOrder); - void addZOrder(const float offset); - -private: - VertexInterleavedBuffer* vertexBuffer; - - quad *vertices; - - mat2 matrix; - vec2 position; - float rotation; - float zOrder; - - uint spritesNum; - uint maxSprites; - - TextureAtlas* textures; - - Tex2D tex2d; - - void bindIbo(); - void bindBuffer(); - void addRotation(const float offset, - const float cosr, - const float sinr); -}; - -} diff --git a/include/graphics/LineLayer.h b/include/graphics/LineLayer.h deleted file mode 100644 index 274347e..0000000 --- a/include/graphics/LineLayer.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "graphics/Layer.h" -#include - -namespace glowy3d -{ - -class LineLayer : public Layer -{ -public: - LineLayer(uint maxSprites = 1024, std::string image = "blue.png"); - ~LineLayer(); - void addLine(vec2 p1, vec2 p2, float frequency, float zOrder); -private: - Texture* lineTexture; -}; - -} diff --git a/include/graphics/Material.h b/include/graphics/Material.h deleted file mode 100644 index 41304e5..0000000 --- a/include/graphics/Material.h +++ /dev/null @@ -1,24 +0,0 @@ -/**************************************************************************** -This file is part of glowy3d. - -Copyright (c) 2014 Kvachev 'Rasie1' V. D. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -****************************************************************************/ -#pragma once diff --git a/include/graphics/Sprite.h b/include/graphics/Sprite.h deleted file mode 100644 index 46e2b3e..0000000 --- a/include/graphics/Sprite.h +++ /dev/null @@ -1,64 +0,0 @@ -#pragma once -#include "g2dMath.h" -#include "math/quad.h" - -namespace glowy3d -{ - -class Texture; -class Sprite -{ -public: - Sprite(quad *vboPointer); - ~Sprite(); - - bool isHidden() const; - void setHidden(bool isHidden); - - vec2 getPosition() const; - void setPosition(const vec2& pos); - void addPosition(const vec2& offset); - - vec2 getScale() const; - void setScale(const vec2& scale); - void addScale(const vec2& offset); - - vec2 getOrigin() const; - void setOrigin(const vec2& pos); - void addOrigin(const vec2& offset); - - float getScaleX() const; - void setScaleX(const float scale); - void addScaleX(const float offset); - - float getScaleY() const; - void setScaleY(const float scale); - void addScaleY(const float offset); - - float getRotation() const; - void setRotation(const float rotation); - void addRotation(const float offset); - - float getZOrder() const; - void setZOrder(const float zorder); - void addZOrder(const float offset); - - Texture* getTexture() const; - void setTexture(Texture* texture); - -private: - vec2 position, - scale, - origin; - float rotation; - bool is_hidden = false; - - quad* coords; - Texture* texture; - - bool has_changed = true; - - void pointToVbo(float *vbo); -}; - -} diff --git a/include/math/vertex.h b/include/math/vertex.h index 9c8576c..2b2adb9 100644 --- a/include/math/vertex.h +++ b/include/math/vertex.h @@ -7,8 +7,7 @@ namespace glowy3d struct vertex { //Data - vec2 position; - float zOrder; + vec3 position; vec2 texture; vertex() {} diff --git a/res/shaders/Texture.fsh b/res/shaders/Texture.fsh index 1d7e4b0..9bb9565 100644 --- a/res/shaders/Texture.fsh +++ b/res/shaders/Texture.fsh @@ -3,5 +3,5 @@ uniform sampler2D mytexture; void main(void) { - gl_FragColor = texture2D(mytexture, f_texcoord); -} \ No newline at end of file + gl_FragColor = texture2D(mytexture, f_texcoord); +} diff --git a/src/base/System.cpp b/src/base/System.cpp index 011e3bb..b3b8d85 100644 --- a/src/base/System.cpp +++ b/src/base/System.cpp @@ -3,8 +3,6 @@ #include "base/Config.h" #include "base/Scheduler.h" #include "graphics/Camera.h" -#include "graphics/Sprite.h" -#include "graphics/Layer.h" #include "graphics/FramerateCounter.h" #include "platforms/Renderer.h" #include "platforms/ShaderProgram.h" @@ -28,7 +26,6 @@ Renderer * System::renderer; Camera * System::camera; Scheduler * System::scheduler; FramerateCounter * System::framerateCounter; -IndexBuffer * System::indexBuffer; unsigned short System::layersNum; double System::currentTime; double System::lastTime; @@ -52,26 +49,6 @@ bool System::init() scheduler->add(framerateCounter, 1.0); deltaTime = 0; - layersNum = 0; - - const uint maxSpritesPerLayer = 800 * 800; - const uint iboSize = maxSpritesPerLayer * 6; - uint * iboList = new uint[iboSize]; - - for (int iVertex = 0, i = 0; i < iboSize; iVertex += 4, i += 6) - { - iboList[i] = iVertex; - iboList[i + 1] = iVertex + 1; - iboList[i + 2] = iVertex + 2; - iboList[i + 3] = iVertex + 3; - iboList[i + 4] = iVertex + 2; - iboList[i + 5] = iVertex + 1; - } - - indexBuffer = new IndexBuffer(iboList, iboSize * sizeof(uint)); - indexBuffer->bind(); - - delete[] iboList; Input::setUpDefaultControls(); @@ -96,7 +73,6 @@ double System::getTime() void System::frameStart() { Input::process(); - renderer->setMatrix(mat2(1.f, 0.f, 0.f, 1.f), camera->getMatrix()); renderer->frameStart(); //Timing stuff @@ -118,10 +94,8 @@ void System::frameEnded() void System::exit() { Input::deinit(); - delete indexBuffer; delete framerateCounter; delete scheduler; - delete camera; delete renderer; delete window; std::exit(EXIT_SUCCESS); diff --git a/src/graphics/Camera.cpp b/src/graphics/Camera.cpp index 04db853..b36acbb 100644 --- a/src/graphics/Camera.cpp +++ b/src/graphics/Camera.cpp @@ -14,40 +14,49 @@ Camera::Camera() : setDefaultZoom(); } -const mat2& Camera::getMatrix() +const mat3& Camera::getMatrix() { return matrix; } -void Camera::setPosition(const vec2& position) +void Camera::setPosition(const vec3& position) { matrix[1][0] = position.x; - matrix[1][1] = position.y; + matrix[1][1] = position.y; + matrix[1][2] = position.z; } -void Camera::addPosition(const vec2& offset) +void Camera::addPosition(const vec3& offset) { matrix[1][0] += offset.x; - matrix[1][1] += offset.y; + matrix[1][1] += offset.y; + matrix[1][2] += offset.z; } void Camera::addZoom(const float offset) { matrix[0][0] += offset * matrix[0][0]; - matrix[0][1] += offset * matrix[0][1]; - matrix[1][0] += offset * matrix[1][0]; - matrix[1][1] += offset * matrix[1][1]; + matrix[0][1] += offset * matrix[0][1]; + matrix[0][2] += offset * matrix[0][2]; + matrix[1][0] += offset * matrix[1][0]; + matrix[1][1] += offset * matrix[1][1]; + matrix[1][2] += offset * matrix[1][2]; + matrix[2][0] += offset * matrix[2][0]; + matrix[2][1] += offset * matrix[2][1]; + matrix[2][2] += offset * matrix[2][2]; } void Camera::setDefaultZoom() { float xxx = 2.f / float(System::config->getScreenResolution().x); - float yyy = 2.f / - float(System::config->getScreenResolution().y); + float yyy = 2.f / + float(System::config->getScreenResolution().y); + float zzz = 1; - matrix = mat2(xxx, yyy, - 0.f, 0.f); + matrix = mat3(xxx, yyy, zzz, + 0.f, 0.f, 0.f, + 0.f, 0.f, 0.f); } void Camera::setUpDefaultMovementControls() @@ -56,25 +65,29 @@ void Camera::setUpDefaultMovementControls() Input::setKeyCallback(Input::Keyboard::w, Input::PressType::hold, [&]() { - addPosition(vec2(0.f, - -moveSpeed * System::getDeltaTime())); + addPosition(vec3(0.f, + -moveSpeed * System::getDeltaTime(), + 0.f)); }); Input::setKeyCallback(Input::Keyboard::s, Input::PressType::hold, [&]() { - addPosition(vec2(0.f, - moveSpeed * System::getDeltaTime())); + addPosition(vec3(0.f, + moveSpeed * System::getDeltaTime(), + 0.f)); }); Input::setKeyCallback(Input::Keyboard::d, Input::PressType::hold, [&]() { - addPosition(vec2(-moveSpeed * System::getDeltaTime(), + addPosition(vec3(0.f, + -moveSpeed * System::getDeltaTime(), 0.f)); }); Input::setKeyCallback(Input::Keyboard::a, Input::PressType::hold, [&]() { - addPosition(vec2(moveSpeed * System::getDeltaTime(), + addPosition(vec3(0.f, + moveSpeed * System::getDeltaTime(), 0.f)); }); Input::setKeyCallback(Input::Keyboard::space, Input::PressType::hold, diff --git a/src/graphics/Layer.cpp b/src/graphics/Layer.cpp deleted file mode 100644 index 690ea8f..0000000 --- a/src/graphics/Layer.cpp +++ /dev/null @@ -1,234 +0,0 @@ -#include "graphics/Layer.h" -#include "base/System.h" -#include "graphics/Sprite.h" -#include "graphics/Camera.h" -#include "graphics/Texture.h" -#include "graphics/TextureAtlas.h" -#include "graphics/TextureData.h" -#include "platforms/IndexBuffer.h" -#include "platforms/VertexInterleavedBuffer.h" -#include "platforms/Renderer.h" - - -using namespace std; - -namespace glowy3d -{ - -Layer::Layer(uint maxSprites, uint atlasSize) : - maxSprites(maxSprites), - vertices(new quad[maxSprites]), - textures(new TextureAtlas(atlasSize)), - matrix(0), - spritesNum(0), - zOrder(0) -{ - vertexBuffer = new VertexInterleavedBuffer(vertices, maxSprites * sizeof(quad)); - vertexBuffer->setLayoutForTextureCoords(); - vertexBuffer->setLayoutForPosition(); -} - -Layer::~Layer() -{ - delete vertexBuffer; - delete textures; - delete[] vertices; -} - -void Layer::updateBuffer() -{ - vertexBuffer->bind(); - vertexBuffer->updateData(vertices, - spritesNum * sizeof(quad), - 0); -} - -void Layer::draw() -{ - System::renderer->setMatrix(matrix, System::camera->getMatrix()); - vertexBuffer->bind(); - vertexBuffer->setLayoutForTextureCoords(); - textures->bind(); - vertexBuffer->setLayoutForPosition(); - bindIbo(); - - System::renderer->draw(spritesNum * 6); -} - -Texture * Layer::addTexture(const char * imagePath) -{ - return addTexture(TextureData::load(imagePath)); -} - -Texture * Layer::addTexture(TextureData * imageData) -{ - Texture * tex = textures->add(imageData); - - return tex; -} - -Sprite * Layer::add(Texture * texture) -{ - //Index for the start of current coords in array - const uint vertexPos = spritesNum * 12; - const uint texPos = spritesNum * 8; - - vertices[spritesNum].setDefaultCoords(); - texture->copyTo(vertices + spritesNum); - - vertices[spritesNum].tl.position.y *= texture->size.y; // here was x - vertices[spritesNum].br.position.x *= texture->size.x; - vertices[spritesNum].br.position.y *= texture->size.y; - vertices[spritesNum].bl.position.x *= texture->size.x; - - Sprite * ret = new Sprite(vertices + spritesNum); - ++spritesNum; - - return ret; -} - -Sprite * Layer::add(const char * imagePath) -{ - Texture * tex = addTexture(imagePath); - - return add(tex); -} - -void Layer::setShader(unsigned shared_id) -{ - throw runtime_error("set shader is in a TODO stage"); -} - -void Layer::setTexture(const Tex2D& tex) -{ - throw runtime_error("set texture is in a TODO stage"); -} - -void Layer::bindIbo() -{ - System::indexBuffer->bind(); -} - -void Layer::bindBuffer() -{ - vertexBuffer->bind(); -} - -vec2 Layer::getPosition() const -{ - return position; -} - -void Layer::setPosition(const vec2& pos) -{ - //position = pos; - matrix[0][0] = pos.x; - matrix[1][1] = pos.y; -} - -void Layer::addPosition(const vec2& offset) -{ - matrix[0][0] += offset.x; - matrix[1][1] += offset.y; - //position += offset; -} - -float Layer::getRotation() const -{ - return 0; -} - -void Layer::setRotation(const float rotation) -{ - addRotation(rotation - this->rotation); -} - -void Layer::addRotation(const float offset) -{ - addRotation(offset, cos(offset / 100.f), sin(offset / 100.f)); -} - -void Layer::addRotation(const float offset, const float cosr, const float sinr) -{ - throw runtime_error("rotation is in a TODO stage"); - rotation += offset; -} - -vec2 Layer::getScale() const -{ - return vec2(0, 0); -} - -void Layer::setScale(const vec2& scale) -{ -} - -void Layer::addScale(const vec2& offset) -{ -} - -float Layer::getScaleX() const -{ - return 0; -} - -void Layer::setScaleX(const float scale) -{ -} - -void Layer::addScaleX(const float offset) -{ -} - -float Layer::getScaleY() const -{ - return 0; -} - -void Layer::setScaleY(const float scale) -{ -} - -void Layer::addScaleY(const float offset) -{ -} - -vec2 Layer::getOrigin() const -{ - return vec2(0, 0); -} - -void Layer::setOrigin(const vec2& point) -{ -} - -float Layer::getZOrder() const -{ - return zOrder; -} - -void Layer::setZOrder(const float zOrder) -{ - for (uint i = 0; i < spritesNum; ++i) - { - vertices[i].bl.zOrder = zOrder; - vertices[i].br.zOrder = zOrder; - vertices[i].tl.zOrder = zOrder; - vertices[i].tr.zOrder = zOrder; - } - this->zOrder = zOrder; -} - -void Layer::addZOrder(const float offset) -{ - for (uint i = 0; i < spritesNum; ++i) - { - vertices[i].bl.zOrder += zOrder; - vertices[i].br.zOrder += zOrder; - vertices[i].tl.zOrder += zOrder; - vertices[i].tr.zOrder += zOrder; - } - this->zOrder += zOrder; -} - -} diff --git a/src/graphics/LineLayer.cpp b/src/graphics/LineLayer.cpp deleted file mode 100644 index b5c056e..0000000 --- a/src/graphics/LineLayer.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "graphics/LineLayer.h" -#include "graphics/Texture.h" -#include "graphics/Sprite.h" - -using namespace std; - -namespace glowy3d -{ - -LineLayer::LineLayer(uint maxSprites, string image) : - Layer(maxSprites) -{ - lineTexture = addTexture(image.c_str()); - updateBuffer(); -} - -LineLayer::~LineLayer() -{ - delete lineTexture; -} - -void LineLayer::addLine(vec2 p1, vec2 p2, float frequency, float zOrder) -{ - dvec2 step = dvec2(p2.x - p1.x, p2.y - p1.y) / static_cast(frequency); - for (dvec2 i = p1; glm::distance(i, static_cast(p2)) > 10.0; i += step) - add(lineTexture)->addPosition(i); - setZOrder(zOrder); - updateBuffer(); -} - - -} diff --git a/src/graphics/Material.cpp b/src/graphics/Material.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/graphics/Sprite.cpp b/src/graphics/Sprite.cpp deleted file mode 100644 index 8ca3824..0000000 --- a/src/graphics/Sprite.cpp +++ /dev/null @@ -1,189 +0,0 @@ -#include "graphics/Sprite.h" -#include "graphics/Texture.h" - -using namespace std; - -namespace glowy3d -{ - -Sprite::Sprite(quad *vboPointer) -{ - coords = vboPointer; - is_hidden = false; - has_changed = false; -} - -Sprite::~Sprite() -{ - -} - -bool Sprite::isHidden() const -{ - return is_hidden; -} - -void Sprite::setHidden(bool isHidden) -{ - is_hidden = isHidden; -} - -vec2 Sprite::getPosition() const -{ - return position; -} - -void Sprite::setPosition(const vec2& pos) -{ - coords->tl.position -= position - pos; - coords->tr.position -= position - pos; - coords->br.position -= position - pos; - coords->bl.position -= position - pos; - position = pos; - - has_changed = true; -} - -void Sprite::addPosition(const vec2& offset) -{ - addOrigin(offset); - - coords->tl.position += offset; - coords->tr.position += offset; - coords->br.position += offset; - coords->bl.position += offset; - - has_changed = true; -} - -vec2 Sprite::getScale() const -{ - return scale; -} - -void Sprite::setScale(const vec2& scale) -{ - -} - -void Sprite::addScale(const vec2& offset) -{ - -} - -vec2 Sprite::getOrigin() const -{ - return scale; -} - -void Sprite::setOrigin(const vec2& pos) -{ - origin = pos; -} - -void Sprite::addOrigin(const vec2& offset) -{ - origin += offset; -} - -float Sprite::getScaleX() const -{ - return scale.x; -} - -void Sprite::setScaleX(const float scale) -{ - -} - -void Sprite::addScaleX(const float offset) -{ - -} - -float Sprite::getScaleY() const -{ - return scale.y; -} - -void Sprite::setScaleY(const float scale) -{ - -} - -void Sprite::addScaleY(const float offset) -{ - -} - -float Sprite::getRotation() const -{ - return rotation; -} - -void Sprite::setRotation(const float rotation) -{ - addRotation(rotation - this->rotation); -} - -void Sprite::addRotation(const float offset) -{ - this->rotation += offset; - - auto cosr = cos(offset / 100.f); - auto sinr = sin(offset / 100.f); - - /*float deltaX = vertexCoords[0] - getOrigin().x; - float deltaY = vertexCoords[1] - getOrigin().y; - vertexCoords[0] = getOrigin().x + cosr * deltaX - sinr * deltaY; - vertexCoords[1] = getOrigin().y + sinr * deltaX + cosr * deltaY; - - deltaX = vertexCoords[3] - getOrigin().x; - deltaY = vertexCoords[4] - getOrigin().y; - vertexCoords[3] = getOrigin().x + cosr * deltaX - sinr * deltaY; - vertexCoords[4] = getOrigin().y + sinr * deltaX + cosr * deltaY; - - deltaX = vertexCoords[6] - getOrigin().x; - deltaY = vertexCoords[7] - getOrigin().y; - vertexCoords[6] = getOrigin().x + cosr * deltaX - sinr * deltaY; - vertexCoords[7] = getOrigin().y + sinr * deltaX + cosr * deltaY; - - deltaX = vertexCoords[9] - getOrigin().x; - deltaY = vertexCoords[10] - getOrigin().y; - vertexCoords[9] = getOrigin().x + cosr * deltaX - sinr * deltaY; - vertexCoords[10] = getOrigin().y + sinr * deltaX + cosr * deltaY;*/ -} - -float Sprite::getZOrder() const -{ - return coords->tl.z(); -} - -void Sprite::setZOrder(const float zorder) -{ - coords->tl.zOrder = zorder; - coords->tr.zOrder = zorder; - coords->br.zOrder = zorder; - coords->bl.zOrder = zorder; -} - -void Sprite::addZOrder(const float offset) -{ - coords->tl.zOrder += offset; - coords->tr.zOrder += offset; - coords->br.zOrder += offset; - coords->bl.zOrder += offset; -} - -Texture* Sprite::getTexture() const -{ - return texture; -} - -void Sprite::setTexture(Texture* texture) -{ - this->texture = texture; - texture->copyTo(coords); -} - -} diff --git a/src/math/vertex.cpp b/src/math/vertex.cpp index d11d4b4..12f0f9a 100644 --- a/src/math/vertex.cpp +++ b/src/math/vertex.cpp @@ -11,8 +11,7 @@ vertex::vertex(const vec3& position, const vec2& texture) : } vertex::vertex(float x, float y, float z, float u, float v) : - position(x, y), - zOrder(z), + position(x, y, z), texture(u, v) { } @@ -29,7 +28,7 @@ float vertex::y() const float vertex::z() const { - return zOrder; + return position.z; } float vertex::u() const diff --git a/src/platforms/IndexBuffer.cpp b/src/platforms/IndexBuffer.cpp index f302d99..6e36156 100644 --- a/src/platforms/IndexBuffer.cpp +++ b/src/platforms/IndexBuffer.cpp @@ -14,6 +14,7 @@ namespace glowy3d IndexBuffer::IndexBuffer(const void *data, uint size) { + glGenBuffers(1, &bufferId); bind(); this->size = size; glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, GL_STATIC_DRAW); diff --git a/src/platforms/Renderer.cpp b/src/platforms/Renderer.cpp index 403b4f0..c487a7e 100644 --- a/src/platforms/Renderer.cpp +++ b/src/platforms/Renderer.cpp @@ -37,14 +37,6 @@ Renderer::Renderer() glfwSwapInterval(System::config->getMaxFramerate()); - shaderProgram = new ShaderProgram(System::config->getVertexShaderPath(), - System::config->getFragmentShaderPath()); - shaderProgram->use(); - - modelMatrixUniform = new UniformMat2(shaderProgram, "model", 0); - modelMatrixUniform->bind(); - viewMatrixUniform = new UniformMat2(shaderProgram, "camera", 1); - viewMatrixUniform->bind(); } Renderer::~Renderer() diff --git a/src/platforms/VertexInterleavedBuffer.cpp b/src/platforms/VertexInterleavedBuffer.cpp index 11698a8..fc4b80e 100644 --- a/src/platforms/VertexInterleavedBuffer.cpp +++ b/src/platforms/VertexInterleavedBuffer.cpp @@ -39,14 +39,17 @@ void VertexInterleavedBuffer::bind() void VertexInterleavedBuffer::setLayoutForPosition() { glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), 0); + glVertexAttribPointer(0, 3, + GL_FLOAT, + GL_FALSE, + sizeof(vertex), + 0); } void VertexInterleavedBuffer::setLayoutForTextureCoords() { glEnableVertexAttribArray(1); - glVertexAttribPointer(1, - 2, + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertex), diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 956f805..6cfdba9 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,2 +1 @@ -add_subdirectory(Triangle) -add_subdirectory(SamplePlatformer) +add_subdirectory(Cube) diff --git a/test/Triangle/CMakeLists.txt b/test/Cube/CMakeLists.txt similarity index 82% rename from test/Triangle/CMakeLists.txt rename to test/Cube/CMakeLists.txt index 764f873..c273640 100644 --- a/test/Triangle/CMakeLists.txt +++ b/test/Cube/CMakeLists.txt @@ -4,9 +4,9 @@ include_directories (${TEST_SOURCE_DIR}) ADD_DEFINITIONS(-DBOOST_TEST_DYN_LINK) -add_executable (triangle triangle.cpp ${TEST_SOURCE_DIR}) +add_executable (cube main.cpp ${TEST_SOURCE_DIR}) -target_link_libraries(triangle +target_link_libraries(cube ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} @@ -19,7 +19,8 @@ target_link_libraries(triangle dl GLEW png + tinyobjloader ) enable_testing() -add_test(glowy3d triangle) +add_test(glowy3d cube) diff --git a/test/Cube/main.cpp b/test/Cube/main.cpp new file mode 100644 index 0000000..9e84dee --- /dev/null +++ b/test/Cube/main.cpp @@ -0,0 +1,238 @@ +#include "Engine.h" +#include +#include +#include "base/System.h" +#include "base/Config.h" +#include "platforms/Window.h" +#include "platforms/ShaderProgram.h" +#include "platforms/VertexInterleavedBuffer.h" +#include "platforms/IndexBuffer.h" +#include "platforms/UniformMat2.h" +#include "tiny_obj_loader.h" +#include "platforms/Tex2D.h" +#include "platforms/Renderer.h" +#include "math/vertex.h" +#include +#include +#include "g2dMath.h" +#include + +using namespace std; +using namespace glowy3d; + +int main() +{ + + VertexInterleavedBuffer* buffer; + IndexBuffer* indexBuffer; + ShaderProgram* shaderProgram; + Tex2D* bricksTexture; + // UniformMat2*? + + std::string inputfile = "cube.obj"; + + auto init = [&](){ + + + shaderProgram = new ShaderProgram(System::config->getVertexShaderPath(), + System::config->getFragmentShaderPath()); + shaderProgram->use(); + + // auto modelMatrixUniform = new UniformMat2(shaderProgram, "model", 0); + // modelMatrixUniform->bind(); + // auto viewMatrixUniform = new UniformMat2(shaderProgram, "camera", 1); + // viewMatrixUniform->bind(); + + // tinyobj::attrib_t attrib; + // std::vector shapes; + // std::vector materials; + + // std::string err; + // bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, inputfile.c_str()); + + // if (!err.empty()) + // { + // std::cerr << err << std::endl; + // } + + // if (!ret) + // { + // exit(1); + // } + + // vector data; + // vector indexData; + + // for (size_t s = 0; s < shapes.size(); s++) + // { + // size_t index_offset = 0; + // for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++) + // { + // int fv = shapes[s].mesh.num_face_vertices[f]; + // for (size_t v = 0; v < fv; v++) + // { + // tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v]; + // float vx = attrib.vertices[3*idx.vertex_index+0]; + // float vy = attrib.vertices[3*idx.vertex_index+1]; + // float vz = attrib.vertices[3*idx.vertex_index+2]; + // float nx = attrib.normals[3*idx.normal_index+0]; + // float ny = attrib.normals[3*idx.normal_index+1]; + // float nz = attrib.normals[3*idx.normal_index+2]; + // float tx = attrib.texcoords[2*idx.texcoord_index+0]; + // float ty = attrib.texcoords[2*idx.texcoord_index+1]; + + // data.push_back(vertex(vec3(vx, vy, vz), + // vec2(tx, ty))); + // indexData.push_back(indexData.size()); + // indexData.push_back(indexData.size()); + // indexData.push_back(indexData.size()); + // } + // index_offset += fv; + // shapes[s].mesh.material_ids[f]; + // } + // } + + auto data = vector({ + // Vertex data for face 0 + {vec3(-1.0f, -1.0f, 1.0f), vec2(0.0f, 0.0f)}, // v0 + {vec3( 1.0f, -1.0f, 1.0f), vec2(0.33f, 0.0f)}, // v1 + {vec3(-1.0f, 1.0f, 1.0f), vec2(0.0f, 0.5f)}, // v2 + {vec3( 1.0f, 1.0f, 1.0f), vec2(0.33f, 0.5f)}, // v3 + + // Vertex data for face 1 + {vec3( 1.0f, -1.0f, 1.0f), vec2( 0.0f, 0.5f)}, // v4 + {vec3( 1.0f, -1.0f, -1.0f), vec2(0.33f, 0.5f)}, // v5 + {vec3( 1.0f, 1.0f, 1.0f), vec2(0.0f, 1.0f)}, // v6 + {vec3( 1.0f, 1.0f, -1.0f), vec2(0.33f, 1.0f)}, // v7 + + // Vertex data for face 2 + {vec3( 1.0f, -1.0f, -1.0f), vec2(0.66f, 0.5f)}, // v8 + {vec3(-1.0f, -1.0f, -1.0f), vec2(1.0f, 0.5f)}, // v9 + {vec3( 1.0f, 1.0f, -1.0f), vec2(0.66f, 1.0f)}, // v10 + {vec3(-1.0f, 1.0f, -1.0f), vec2(1.0f, 1.0f)}, // v11 + + // Vertex data for face 3 + {vec3(-1.0f, -1.0f, -1.0f), vec2(0.66f, 0.0f)}, // v12 + {vec3(-1.0f, -1.0f, 1.0f), vec2(1.0f, 0.0f)}, // v13 + {vec3(-1.0f, 1.0f, -1.0f), vec2(0.66f, 0.5f)}, // v14 + {vec3(-1.0f, 1.0f, 1.0f), vec2(1.0f, 0.5f)}, // v15 + + // Vertex data for face 4 + {vec3(-1.0f, -1.0f, -1.0f), vec2(0.33f, 0.0f)}, // v16 + {vec3( 1.0f, -1.0f, -1.0f), vec2(0.66f, 0.0f)}, // v17 + {vec3(-1.0f, -1.0f, 1.0f), vec2(0.33f, 0.5f)}, // v18 + {vec3( 1.0f, -1.0f, 1.0f), vec2(0.66f, 0.5f)}, // v19 + + // Vertex data for face 5 + {vec3(-1.0f, 1.0f, 1.0f), vec2(0.33f, 0.5f)}, // v20 + {vec3( 1.0f, 1.0f, 1.0f), vec2(0.66f, 0.5f)}, // v21 + {vec3(-1.0f, 1.0f, -1.0f), vec2(0.33f, 1.0f)}, // v22 + {vec3( 1.0f, 1.0f, -1.0f), vec2(0.66f, 1.0f)} // v23 + }); + + auto indexData = vector{ + 0, 1, 2, 3, 3, // Face 0 - triangle strip ( v0, v1, v2, v3) + 4, 4, 5, 6, 7, 7, // Face 1 - triangle strip ( v4, v5, v6, v7) + 8, 8, 9, 10, 11, 11, // Face 2 - triangle strip ( v8, v9, v10, v11) + 12, 12, 13, 14, 15, 15, // Face 3 - triangle strip (v12, v13, v14, v15) + 16, 16, 17, 18, 19, 19, // Face 4 - triangle strip (v16, v17, v18, v19) + 20, 20, 21, 22, 23 // Face 5 - triangle strip (v20, v21, v22, v23) + }; + + buffer = new VertexInterleavedBuffer(data.data(), data.size() * sizeof(vertex)); + buffer->setLayout(); + buffer->bind(); + // int positionLocation = program->attributeLocation("position"); + // int texcoordLocation = program->attributeLocation("texcoord"); + indexBuffer = new IndexBuffer(indexData.data(), indexData.size() * sizeof(uint)); + indexBuffer->bind(); + + bricksTexture = new Tex2D("TileStone.png"); + bricksTexture->bind(); + }; + + auto update = [&](){ + + glm::vec3 position = glm::vec3( 0, 0, 5 ); + float horizontalAngle = 3.14f; + float verticalAngle = 0.0f; + float initialFoV = 45.0f; + + float speed = 3.0f; + float mouseSpeed = 0.005f; + double xpos, ypos; + float deltaTime = System::getDeltaTime(); + + // double currentTime = glfwGetTime(); + // float deltaTime = float(currentTime - lastTime); + + auto window = System::window->getHandle(); + + glfwGetCursorPos(window, &xpos, &ypos); + glfwSetCursorPos(window, 1024/2, 768/2); + + horizontalAngle += mouseSpeed * deltaTime * float(1024/2 - xpos ); + verticalAngle += mouseSpeed * deltaTime * float( 768/2 - ypos ); + + + glm::vec3 direction( + cos(verticalAngle) * sin(horizontalAngle), + sin(verticalAngle), + cos(verticalAngle) * cos(horizontalAngle) + ); + + glm::vec3 right = glm::vec3( + sin(horizontalAngle - 3.14f/2.0f), + 0, + cos(horizontalAngle - 3.14f/2.0f) + ); + + glm::vec3 up = glm::cross( right, direction ); + + if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS){ + position += direction * deltaTime * speed; + } + // Move backward + if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS){ + position -= direction * deltaTime * speed; + } + // Strafe right + if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS){ + position += right * deltaTime * speed; + } + // Strafe left + if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS){ + position -= right * deltaTime * speed; + } + + float FoV = initialFoV;// - 5 * glfwGetMouseWheel(); + + mat4 ProjectionMatrix = glm::perspective(FoV, 4.0f / 3.0f, 0.1f, 100.0f); + mat4 ViewMatrix = glm::lookAt( + position, // Camera is here + position+direction, // and looks here : at the same position, plus "direction" + up // Head is up (set to 0,-1,0 to look upside-down) + ); + + + glm::mat4 ModelMatrix = glm::mat4(1.0); + glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix; + + auto mvpUniformLocation = glGetUniformLocation( + shaderProgram->id, + "mvp_matrix"); + + glUniformMatrix4fv(mvpUniformLocation, 1, GL_FALSE, (float*)&MVP); + + + indexBuffer->bind(); + buffer->bind(); + bricksTexture->bind(); + glDrawElements(GL_TRIANGLE_STRIP, 34, GL_UNSIGNED_INT, 0); + }; + + + glowy3d::start(init, update); + delete buffer; + delete shaderProgram; +} diff --git a/test/SamplePlatformer/Actor.h b/test/SamplePlatformer/Actor.h deleted file mode 100644 index bb2844e..0000000 --- a/test/SamplePlatformer/Actor.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -namespace game -{ - -class Actor -{ -public: - virtual void update(float dt) = 0; - virtual void draw() = 0; - -private: -}; - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Background.cpp b/test/SamplePlatformer/Background.cpp deleted file mode 100644 index 9bd1905..0000000 --- a/test/SamplePlatformer/Background.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "Background.h" -#include -#include "base/System.h" -#include "graphics/Camera.h" -#include "platforms/Window.h" - -using namespace glm; -using namespace std; -using namespace glowy3d; -namespace game -{ - -Background::Background(const string& image) : - backgroundLayer(new glowy3d::Layer(8, 2048)) -{ - backgroundLayer->add((image + ".png").c_str()); - backgroundLayer->setZOrder(0.9f); - backgroundLayer->updateBuffer(); - auto screen = -static_cast(System::window->getSize()); - backgroundLayer->setPosition(screen / 2.f); -} - -Background::~Background() -{ - delete backgroundLayer; -} - -int Background::update(float dt) -{ - return 0; -} - -void Background::draw() -{ - System::camera->disableTransform(); - backgroundLayer->draw(); - System::camera->enableTransform(); -} - -} diff --git a/test/SamplePlatformer/Background.h b/test/SamplePlatformer/Background.h deleted file mode 100644 index 5baa93f..0000000 --- a/test/SamplePlatformer/Background.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include -#include "graphics/Layer.h" -#include "math/AdditionGLM.h" - -namespace game -{ -class Background -{ -public: - Background(const std::string& image); - ~Background(); - - int update(float dt); - void draw(); -private: - glowy3d::Layer * backgroundLayer; -}; - -} diff --git a/test/SamplePlatformer/Body.cpp b/test/SamplePlatformer/Body.cpp deleted file mode 100644 index 7b083d1..0000000 --- a/test/SamplePlatformer/Body.cpp +++ /dev/null @@ -1,103 +0,0 @@ -#include "Body.h" -#include -#include -#include "Engine.h" -#include "GlobalData.h" -#include "CollisionData.h" - -using namespace glm; -using namespace std; -namespace game -{ - -Body::Body(glm::vec2 pos) : - pos(pos), - layer(new glowy3d::Layer(4)), - floating(false), - solid(true), - velocity(0.f, 0.f) -{ - layer->add("ukata.png"); - layer->setZOrder(-1.f); - layer->setPosition(pos); - prevPos = pos; - layer->updateBuffer(); -} - -Body::~Body() -{ - -} - -void Body::setPosition(vec2 pos) -{ - this->pos = pos; -} - -void Body::addPosition(vec2 pos) -{ - this->pos += pos; -} - -void Body::momentum(vec2 pos) -{ - velocity += pos; -} - -void Body::update(float dt) -{ - if (dt > 0.3f) - dt = 0.f; - - if (!isFloating()) - velocity += GlobalData::collisionData->getGravity() * dt; - - GlobalData::collisionData->collision(this); - nextPosOffset = velocity * dt; - - prevPos = getPosition(); - addPosition(nextPosOffset); - layer->setPosition(toRenderer(pos)); -} - -vec2 Body::getPosition() const -{ - return pos; -} - -vec2 Body::getVelocity() const -{ - return velocity; -} - -void Body::setVelocity(vec2 vel) -{ - velocity = vel; -} - -void Body::addVelocity(vec2 vel) -{ - velocity += vel; -} - -void Body::draw() -{ - layer->draw(); -} - -vec2 Body::getPrevPosition() const -{ - return prevPos; -} - -bool Body::isFloating() const -{ - return floating; -} - -void Body::setFloating(bool floating) -{ - this->floating = floating; -} - -} diff --git a/test/SamplePlatformer/Body.h b/test/SamplePlatformer/Body.h deleted file mode 100644 index f7242f8..0000000 --- a/test/SamplePlatformer/Body.h +++ /dev/null @@ -1,50 +0,0 @@ -#pragma once -#include "graphics/Layer.h" -#include "math/AdditionGLM.h" -#include "Actor.h" - -namespace game -{ - -class CollisionData; -class RigidLine; -class Character; -class Body : public Actor -{ -public: - Body(glm::vec2 pos); - virtual ~Body(); - - virtual void update(float dt); - void draw(); - - glm::vec2 getPosition() const; - glm::vec2 getPrevPosition() const; - void setPosition(glm::vec2 pos); - void addPosition(glm::vec2 pos); - void momentum(glm::vec2 pos); - - glm::vec2 getVelocity() const; - void setVelocity(glm::vec2 vel); - void addVelocity(glm::vec2 vel); - bool isFloating() const; - void setFloating(bool floating); - - virtual void collision(Body * other) {} - virtual void collision(RigidLine * other) {} - virtual void collision(Character * other) {} - -private: - glm::vec2 pos; - glm::vec2 prevPos; - glm::vec2 nextPosOffset; - - glm::vec2 velocity; - - glowy3d::Layer * layer; - - bool floating; - bool solid; -}; - -} diff --git a/test/SamplePlatformer/CMakeLists.txt b/test/SamplePlatformer/CMakeLists.txt deleted file mode 100644 index 4d372dd..0000000 --- a/test/SamplePlatformer/CMakeLists.txt +++ /dev/null @@ -1,28 +0,0 @@ -find_package(Boost COMPONENTS system filesystem unit_test_framework REQUIRED) - -include_directories (${TEST_SOURCE_DIR}) - -ADD_DEFINITIONS(-DBOOST_TEST_DYN_LINK) - -file(GLOB_RECURSE SAMPLE_PLATFORMER_SRC *.cpp) -file(GLOB_RECURSE SAMPLE_PLATFORMER_RES ../../res/SamplePlatformer/*) - -add_executable (sampleplatformer ${SAMPLE_PLATFORMER_SRC} ${TEST_SOURCE_DIR}) - -target_link_libraries(sampleplatformer - ${Boost_FILESYSTEM_LIBRARY} - ${Boost_SYSTEM_LIBRARY} - ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} - glowy3d - glfw3 - m rt - GL GLU - X11 Xxf86vm Xrandr Xi Xinerama Xcursor - pthread - dl - GLEW - png - ) - -enable_testing() -add_test(glowy3d sampleplatformer) diff --git a/test/SamplePlatformer/Character.cpp b/test/SamplePlatformer/Character.cpp deleted file mode 100644 index 179091d..0000000 --- a/test/SamplePlatformer/Character.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include "Character.h" -#include -#include "RigidLine.h" -#include "Walker.h" -#include "GlobalData.h" -#include "CollisionData.h" - -using namespace glm; -namespace game -{ - -Character::Character(vec2 pos) : - body(new Walker(pos)), - jumpMomentum(-20.f), - accelerationSpeed(8.f), - airAccelerationSpeed(0.05f) -{ - -} - -Character::~Character() -{ - delete body; -} - -void Character::update(float dt) -{ - body->update(dt); -} - -void Character::moveLeft() -{ - body->momentum(vec2(-getAcceleration(), 0)); -} - -void Character::moveRight() -{ - body->momentum(vec2(getAcceleration(), 0)); -} - -void Character::jump() -{ - if (!body->isGrounded()) - return; - body->detachFromGround(); - body->momentum(vec2(0, jumpMomentum)); -} - -void Character::flyUp() -{ - body->momentum(vec2(0, -getAcceleration() - GlobalData::collisionData->getGravity().y)); -} - -void Character::flyDown() -{ - getBody()->momentum(vec2(0, getAcceleration())); -} - -float Character::getAcceleration() const -{ - if (body->isGrounded()) - return accelerationSpeed; - else - return airAccelerationSpeed; -} - -Body * Character::getBody() -{ - return body; -} - -void Character::draw() -{ - body->draw(); -} - -vec2 Character::getPosition() const -{ - return body->getPosition(); -} - - -vec2 Character::getPrevPosition() const -{ - return body->getPrevPosition(); -} - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Character.h b/test/SamplePlatformer/Character.h deleted file mode 100644 index 6f6ad74..0000000 --- a/test/SamplePlatformer/Character.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once -#include "math/AdditionGLM.h" -#include "Actor.h" - -namespace game -{ - -class Walker; -class Body; -class Character : public Actor -{ -public: - Character(glm::vec2 pos); - ~Character(); - - virtual void update(float dt); - void draw(); - - void moveLeft(); - void moveRight(); - void jump(); - void flyDown(); - void flyUp(); - glm::vec2 getPosition() const; - glm::vec2 getPrevPosition() const; - Body * getBody(); - - float getAcceleration() const; - -private: - float accelerationSpeed; - float airAccelerationSpeed; - float jumpMomentum; - - bool isGrounded; - - Walker * body; -}; - -} diff --git a/test/SamplePlatformer/Circle.cpp b/test/SamplePlatformer/Circle.cpp deleted file mode 100644 index d9437d1..0000000 --- a/test/SamplePlatformer/Circle.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include "Circle.h" -#include "RigidLine.h" -#include "Body.h" - -using namespace glm; -namespace game -{ - -Circle::Circle(vec2 pos, float radius, vec2 bounce) : - Body(pos), - bounce(bounce), - radius(radius) -{ -} - -void Circle::update(float dt) -{ - Body::update(dt); -} - -float Circle::getRadius() const -{ - return radius; -} - -void Circle::setRadius(float radius) -{ - this->radius = radius; -} - -void Circle::collision(Body * other) -{ - other->collision(this); -} - -void Circle::collision(RigidLine * other) -{ - other->collision(this); -} - -void Circle::collision(Circle * other) -{ - -} - -void Circle::setBounce(vec2 bounce) -{ - this->bounce = bounce; -} - -vec2 Circle::getBounce() const -{ - return bounce; -} - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Circle.h b/test/SamplePlatformer/Circle.h deleted file mode 100644 index 6ff837d..0000000 --- a/test/SamplePlatformer/Circle.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once -#include "Body.h" - -namespace game -{ - -class Circle : public Body -{ -public: - Circle(glm::vec2 pos, float radius, glowy3d::vec2 bounce); - virtual ~Circle() {} - - virtual void update(float dt); - float getRadius() const; - void setRadius(float radius); - - virtual glm::vec2 getBounce() const; - void setBounce(glm::vec2 bounce); - - virtual void collision(Body * other); - virtual void collision(RigidLine * other); - virtual void collision(Circle * other); - -private: - float radius; - glm::vec2 bounce; -}; - -} diff --git a/test/SamplePlatformer/CollisionData.cpp b/test/SamplePlatformer/CollisionData.cpp deleted file mode 100644 index 8642b30..0000000 --- a/test/SamplePlatformer/CollisionData.cpp +++ /dev/null @@ -1,195 +0,0 @@ -#include "CollisionData.h" -#include -#include -#include "graphics/LineLayer.h" -#include "Engine.h" -#include "MapInfo.h" -#include "Tiles/Tile.h" -#include "Body.h" -#include "RigidLine.h" -#include "GlobalData.h" - -using namespace std; -using namespace glowy3d; -using namespace glm; -namespace game -{ - -CollisionData::CollisionData(const MapInfo& map) : - gravity(vec2(0, 44.f)) -{ - - staticCollision.resize(map.tiles.size() + 2); - staticCollision[0].resize(map.tiles[0].size() + 2, false); - staticCollision[map.tiles.size() + 1].resize(map.tiles[0].size() + 2, false); - - for (uint i = 0; i < map.tiles.size(); ++i) - { - staticCollision[i+1].push_back(false); - for (uint j = 0; j < map.tiles[i].size(); ++j) - staticCollision[i+1].push_back(map.tiles[i][j]->isSolid()); - staticCollision[i+1].push_back(false); - } -} - -CollisionData::~CollisionData() -{ -} - -void CollisionData::addBody(Body * body) -{ - dynamicCollision.push_back(body); -} - -void CollisionData::removeBody(Body * body) -{ - dynamicCollision.erase(remove(begin(dynamicCollision), - end(dynamicCollision), - body)); -} - -void CollisionData::check(Body * body, bool& returnCollided, vec2& returnCollisionPos) -{ - auto pos = body->getPosition(); - - returnCollided = staticCollision[pos.y][pos.x]; -} - -usvec2 CollisionData::findStart() -{ - for (uint8 i = 1; i < staticCollision.size(); ++i) - for (uint8 j = 0; j < staticCollision[i].size(); ++j) - if (staticCollision[i][j]) - return usvec2(j, i); -} - -usvec2 nextClockwise(usvec2 current, uint8 clockwiseCounter) -{ - switch (clockwiseCounter) - { - case 0: - return usvec2(current.x - 1, current.y); - case 1: - return usvec2(current.x - 1, current.y + 1); - case 2: - return usvec2(current.x, current.y + 1); - case 3: - return usvec2(current.x + 1, current.y + 1); - case 4: - return usvec2(current.x + 1, current.y); - case 5: - return usvec2(current.x + 1, current.y - 1); - case 6: - return usvec2(current.x, current.y - 1); - case 7: - return usvec2(current.x - 1, current.y - 1); - } -} - -void checkDiagonalAndAddIfNeeded(usvec2 prev, usvec2 current, vector& points) -{ - if (prev + usvec2(1, 1) == current) - points.push_back(prev + usvec2(1, 0)); - if (prev + usvec2(-1, 1) == current) - points.push_back(prev + usvec2(0, 1)); - if (prev + usvec2(1, -1) == current) - points.push_back(prev + usvec2(0, -1)); - if (prev + usvec2(-1, -1) == current) - points.push_back(prev + usvec2(-1, 0)); -} - -void CollisionData::traceContours(usvec2 start) -{ - for (auto start : tracingPoints) - { - vector boundaryPoints; - boundaryPoints.push_back(start); - - auto currentBoundaryPoint = start; - - uint8 clockwiseCounter = 0; - auto currentPoint = nextClockwise(currentBoundaryPoint, clockwiseCounter); - auto backtrackPoint = currentPoint; - - while (currentPoint != start) - { - if (staticCollision[currentPoint.y][currentPoint.x]) - { - checkDiagonalAndAddIfNeeded(currentBoundaryPoint, currentPoint, boundaryPoints); - boundaryPoints.push_back(currentPoint); - currentBoundaryPoint = currentPoint; - currentPoint = backtrackPoint; - while (currentPoint != nextClockwise(currentBoundaryPoint, clockwiseCounter)) - clockwiseCounter = (clockwiseCounter + 1) % 8; - } - else - { - clockwiseCounter = (clockwiseCounter + 1) % 8; - backtrackPoint = currentPoint; - currentPoint = nextClockwise(currentBoundaryPoint, clockwiseCounter); - } - } - - - //remove redundant points - auto prev1 = begin(boundaryPoints); - auto prev2 = next(begin(boundaryPoints)); - for (uint i = 2; i < boundaryPoints.size();) - { - if ((prev1->x == prev2->x && - prev2->x == boundaryPoints[i].x || - prev1->y == prev2->y && - prev2->y == boundaryPoints[i].y) && - boundaryPoints[i] != *prev1) - { - boundaryPoints.erase(prev2); - } - else - { - ++i; - prev1 = prev2; - } - prev2 = begin(boundaryPoints) + i - 1; - } - - for (auto first = begin(boundaryPoints), second = prev(end(boundaryPoints)); - first != end(boundaryPoints);) - { - auto gap = vec2(0.5, 0.5); - dynamicCollision.push_back(new RigidLine(vec2(second->x, second->y) - gap, - vec2(first->x, first->y) - gap)); - second = first; - ++first; - } - } -} - -bool CollisionData::check(usvec2 pos) -{ - return staticCollision[pos.y][pos.x]; -} - -void CollisionData::collision(Body * body) -{ - for (auto &x : dynamicCollision) - x->collision(body); -} - -vec2 CollisionData::getGravity() const -{ - return gravity; -} - -void CollisionData::setGravity(vec2 gravity) -{ - this->gravity = gravity; -} - -void CollisionData::addTracingPoint(usvec2 pos) -{ - tracingPoints.push_back(pos + usvec2(1,1)); -} - -std::vector CollisionData::tracingPoints; - -} diff --git a/test/SamplePlatformer/CollisionData.h b/test/SamplePlatformer/CollisionData.h deleted file mode 100644 index 751947a..0000000 --- a/test/SamplePlatformer/CollisionData.h +++ /dev/null @@ -1,48 +0,0 @@ -#pragma once -#include -#include "math/AdditionGLM.h" - -namespace glowy3d -{ -class LineLayer; -class Layer; -} - -namespace game -{ - -class MapInfo; -class Body; -class CollisionData -{ -public: - CollisionData(const MapInfo& map); - ~CollisionData(); - - void addBody(Body * body); - void removeBody(Body * body); - - void collision(Body * body); - void check(Body * body, bool& returnCollided, glm::vec2& returnCollisionPos); - bool check(glm::usvec2 pos); - - void traceContours(glm::usvec2 start); - - glm::vec2 getGravity() const; - void setGravity(glm::vec2 gravity); - - //Add a tracing point for a set of connected solid blocks on map - //to trace them and make them solid in-game - static void addTracingPoint(glm::usvec2 pos); - -private: - glm::vec2 gravity; - - std::vector> staticCollision; - std::vector dynamicCollision; - static std::vector tracingPoints; - - glm::usvec2 findStart(); -}; - -} diff --git a/test/SamplePlatformer/Creature.cpp b/test/SamplePlatformer/Creature.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/test/SamplePlatformer/Creature.h b/test/SamplePlatformer/Creature.h deleted file mode 100644 index e69de29..0000000 diff --git a/test/SamplePlatformer/Finish.cpp b/test/SamplePlatformer/Finish.cpp deleted file mode 100644 index a05c6fa..0000000 --- a/test/SamplePlatformer/Finish.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "Finish.h" - -using namespace glm; -using namespace std; -namespace game -{ - -Finish::Finish(usvec2 pos) : - Body(pos) -{ - -} - -Finish::~Finish() -{ - -} - -void Finish::update(float dt) -{ - -} - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Finish.h b/test/SamplePlatformer/Finish.h deleted file mode 100644 index ef4ab25..0000000 --- a/test/SamplePlatformer/Finish.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once -#include "math/AdditionGLM.h" -#include "Body.h" - -namespace game -{ - -class Finish : public Body -{ -public: - Finish(glm::usvec2 pos); - ~Finish(); - - void update(float dt); - -private: -}; - -} diff --git a/test/SamplePlatformer/GlobalData.cpp b/test/SamplePlatformer/GlobalData.cpp deleted file mode 100644 index 8d6327b..0000000 --- a/test/SamplePlatformer/GlobalData.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "GlobalData.h" - -using namespace std; - -namespace game -{ - -std::vector * GlobalData::actors = nullptr; -CollisionData * GlobalData::collisionData = nullptr; -Player * GlobalData::player = nullptr; -const float GlobalData::tilesize = 128; - -} diff --git a/test/SamplePlatformer/GlobalData.h b/test/SamplePlatformer/GlobalData.h deleted file mode 100644 index 4277675..0000000 --- a/test/SamplePlatformer/GlobalData.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once -#include -#include "math/AdditionGLM.h" - -namespace game -{ - -class Actor; -class CollisionData; -class Player; -class GlobalData -{ -public: - static std::vector * actors; - static CollisionData * collisionData; - static Player * player; - static const float tilesize; -}; - - -inline glm::vec2 toWorld(glm::vec2 point) -{ - return glm::vec2(point.x / GlobalData::tilesize, -point.y / GlobalData::tilesize); -} - -inline glm::vec2 toRenderer(glm::vec2 point) -{ - return glm::vec2(point.x * GlobalData::tilesize, -point.y * GlobalData::tilesize); -} - -} diff --git a/test/SamplePlatformer/Main.cpp b/test/SamplePlatformer/Main.cpp deleted file mode 100644 index e064621..0000000 --- a/test/SamplePlatformer/Main.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "Engine.h" -#include -#include "glm/glm.hpp" -#include "platforms/Input.h" -#include "graphics/LineLayer.h" -#include "base/System.h" -#include "math/AdditionGLM.h" -#include "math/rect.h" -#include "MapInfo.h" -#include "World.h" -#include "GL/glew.h" -#include "GLFW/glfw3.h" - -using namespace game; -using namespace glowy3d; - -World * world; - -void init() -{ - world = new World(MapInfo("level1")); -} - -void update() -{ - world->update(glowy3d::System::getDeltaTime()); -} - -int main() -{ - glowy3d::start(init, update); -} diff --git a/test/SamplePlatformer/MapInfo.cpp b/test/SamplePlatformer/MapInfo.cpp deleted file mode 100644 index 9e97e77..0000000 --- a/test/SamplePlatformer/MapInfo.cpp +++ /dev/null @@ -1,87 +0,0 @@ -#include "MapInfo.h" -#include -#include -#include -#include -#include -#include "Tiles/TileGrass.h" -#include "Tiles/TileStone.h" -#include "Tiles/TileEmpty.h" -#include "Tiles/TileStart.h" -#include "CollisionData.h" - -using namespace std; -using namespace glm; -namespace game -{ - -MapInfo::MapInfo(const string& name) : - tiles(loadMap(name)) -{ -} - -MapInfo::~MapInfo() -{ - for (auto &row : tiles) - for (auto &tile : row) - delete tile; -} - -std::vector> MapInfo::loadMap(const string& name) -{ - ifstream f; - f.open("Maps/" + name + ".txt"); - if (!f.good()) - throw ifstream::failure("Failed to open map \"" + name +"\"!"); - - usvec2 size; - f >> size.x >> size.y; - - vector> tiles(size.y); - for (unsigned i = 0; i < size.y; ++i) - for (unsigned j = 0; j < size.x; ++j) - { - char c; - f >> c; - switch (c) - { - case 'e': - { - tiles[i].push_back(new TileEmpty()); - break; - } - case 'g': - { - tiles[i].push_back(new TileGrass()); - break; - } - case 'r': - { - tiles[i].push_back(new TileStone()); - break; - } - case 'R': - { - tiles[i].push_back(new TileStone()); - CollisionData::addTracingPoint(usvec2(j, i)); - break; - } - case 's': - { - tiles[i].push_back(new TileStart()); - break; - } - default: - --j; - } - } - - return tiles; -} - -bool MapInfo::collision(glm::usvec2 pos) const -{ - return tiles[pos.y][pos.x]->isSolid(); -} - -} diff --git a/test/SamplePlatformer/MapInfo.h b/test/SamplePlatformer/MapInfo.h deleted file mode 100644 index f140195..0000000 --- a/test/SamplePlatformer/MapInfo.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once -#include -#include -#include "math/AdditionGLM.h" - -namespace game -{ - -class Tile; -class MapInfo -{ -public: - MapInfo(const std::string& name); - ~MapInfo(); - - bool collision(glm::usvec2 pos) const; - - std::vector> tiles; - std::string backgroundImage = "sky"; - -private: - - std::vector> loadMap(const std::string& name); -}; - -} diff --git a/test/SamplePlatformer/Player.cpp b/test/SamplePlatformer/Player.cpp deleted file mode 100644 index 872e818..0000000 --- a/test/SamplePlatformer/Player.cpp +++ /dev/null @@ -1,85 +0,0 @@ -#include "Player.h" -#include -#include -#include "Engine.h" -#include "platforms/Input.h" -#include "platforms/Window.h" -#include "graphics/Camera.h" -#include "Body.h" -#include "GlobalData.h" - -using namespace glm; -using namespace std; -using namespace glowy3d; -namespace game -{ - - -Player::Player(vec2 pos) : - Character(pos) -{ - initPlayerControls(); - GlobalData::player = this; - System::camera->addZoom(-0.75); -} - -Player::~Player() -{ - -} - -void Player::update(float dt) -{ - Character::update(dt); - System::camera->setPosition(vec2(getPosition().x / -GlobalData::tilesize, - getPosition().y / GlobalData::tilesize) * 10.f); -} - -void Player::initPlayerControls() -{ - - Input::setKeyCallback(Input::Keyboard::up, Input::PressType::press, - [&]() - { - jump(); - }); - Input::setKeyCallback(Input::Keyboard::rshift, Input::PressType::hold, - [&]() - { - flyUp(); - }); - Input::setKeyCallback(Input::Keyboard::down, Input::PressType::hold, - [&]() - { - flyDown(); - }); - Input::setKeyCallback(Input::Keyboard::right, Input::PressType::hold, - [&]() - { - moveRight(); - }); - Input::setKeyCallback(Input::Keyboard::left, Input::PressType::hold, - [&]() - { - moveLeft(); - }); -} - -void Player::setAim(float radian) -{ - this->aim = aim; - //there is missing stuff -} - -float Player::getAim() const -{ - return aim; -} - -Projectile * Player::fire() -{ - return nullptr; - //return new Rocket(getPosition(), aim); -} - -} diff --git a/test/SamplePlatformer/Player.h b/test/SamplePlatformer/Player.h deleted file mode 100644 index d3f207f..0000000 --- a/test/SamplePlatformer/Player.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once -#include "math/AdditionGLM.h" -#include "Character.h" - -namespace game -{ - -class Projectile; -class Player : public Character -{ -public: - Player(glm::vec2 pos); - ~Player(); - - virtual void update(float dt); - - void setAim(float radian); - float getAim() const; - Projectile * fire(); - -private: - void initPlayerControls(); - - float cooldown = 2.f; - float currentCooldown = 0.f; - float aim = 0.f; -}; - -} diff --git a/test/SamplePlatformer/Projectile.cpp b/test/SamplePlatformer/Projectile.cpp deleted file mode 100644 index 54f7037..0000000 --- a/test/SamplePlatformer/Projectile.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "Projectile.h" - -using namespace glm; -namespace game -{ - -Projectile::Projectile(vec2 position, vec2 speed) : - Circle(position, 0.5f, vec2(1.f, 1.0f)) -{ - -} - -void Projectile::update(float dt) -{ - Circle::update(dt); -} - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Projectile.h b/test/SamplePlatformer/Projectile.h deleted file mode 100644 index 18a2c67..0000000 --- a/test/SamplePlatformer/Projectile.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "Circle.h" -#include "math/AdditionGLM.h" - -namespace game -{ - -class Projectile : public Circle -{ -public: - Projectile(glm::vec2 position, glm::vec2 speed); - ~Projectile() {} - - virtual void update(float dt); - -}; - -} diff --git a/test/SamplePlatformer/RigidLine.cpp b/test/SamplePlatformer/RigidLine.cpp deleted file mode 100644 index 56d46bf..0000000 --- a/test/SamplePlatformer/RigidLine.cpp +++ /dev/null @@ -1,119 +0,0 @@ -#include "RigidLine.h" -#include "Circle.h" -#include -#include "Walker.h" - -using namespace glm; -namespace game -{ - -RigidLine::RigidLine(vec2 p1, vec2 p2) : - Body(p1), - p1(p1), - p2(p2), - horisontal(abs(p1.x - p2.x) > abs(p1.y - p2.y)), - collided(false) -{ - if (this->p1.x > this->p2.x) - std::swap(this->p1, this->p2); - - bounce = vec2(1.f, 1.f); -} - - -void RigidLine::update(float dt) -{ - Body::update(dt); -} - -void RigidLine::collision(Body * other) -{ - other->collision(this); -} - -void RigidLine::collision(Walker * other) -{ - collided = false; - collision(dynamic_cast(other)); - - - if (!other->isGrounded() && - horisontal && - collided && - p1.y > other->getPosition().y) - other->attachToGround(p1.x, p2.x); - - if (collided) - other->setPosition(other->getPrevPosition()); -} - -bool intersects(vec2 p1, vec2 p2, vec2 c, float r, vec2& pointOnLineClosestToCircle) -{ - auto lineUnitVector = normalize(p2 - p1); - auto lineEndToCircle = c - p1; - auto projection = dot(lineEndToCircle, lineUnitVector); - - if (projection <= 0) - pointOnLineClosestToCircle = p1; - else if (projection >= length(p2 - p1)) - pointOnLineClosestToCircle = p2; - else - pointOnLineClosestToCircle = - vec2(p1.x + lineUnitVector.x * projection, - p1.y + lineUnitVector.y * projection); - - auto circleToLineDistance = distance(pointOnLineClosestToCircle, c); - - return circleToLineDistance < r; -} - -void RigidLine::collision(Circle * other) -{ - vec2 pointOnLineClosestToCircle; - - //collision(other->getPosition(), other->getPosition() + other->getVelocity()); - if (intersects( - p1, - p2, - other->getPosition(), - other->getRadius(), - pointOnLineClosestToCircle)) - { - auto circleToClosestPointOnLine = other->getPosition() - pointOnLineClosestToCircle; - - auto bounceNormal = normalize(circleToClosestPointOnLine); - - auto k = dot(other->getVelocity(), bounceNormal); - other->addVelocity( - vec2(bounceNormal.x * (-2.f) * k * (other->getBounce().x) * (bounce.x), - bounceNormal.y * (-2.f) * k * (other->getBounce().y) * (bounce.y))); - collided = true; - } -} - -void RigidLine::collision(glm::vec2 lineP1, glm::vec2 lineP2) -{ - auto p0_x = lineP1.x; - auto p0_y = lineP1.y; - auto p1_x = lineP2.x; - auto p1_y = lineP2.y; - auto p2_x = p1.x; - auto p2_y = p1.y; - auto p3_x = p2.x; - auto p3_y = p2.y; - - auto s1_x = p1_x - p0_x; - auto s1_y = p1_y - p0_y; - auto s2_x = p3_x - p2_x; - auto s2_y = p3_y - p2_y; - - auto s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / - (-s2_x * s1_y + s1_x * s2_y); - auto t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / - (-s2_x * s1_y + s1_x * s2_y); - - if (s >= 0 && s <= 1 && t >= 0 && t <= 1) - collided = true; -} - -} \ No newline at end of file diff --git a/test/SamplePlatformer/RigidLine.h b/test/SamplePlatformer/RigidLine.h deleted file mode 100644 index 2b1f076..0000000 --- a/test/SamplePlatformer/RigidLine.h +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once -#include "Body.h" -#include "math/AdditionGLM.h" - -namespace game -{ - -class Circle; -class Walker; -class RigidLine : public Body -{ -public: - RigidLine(glowy3d::vec2 p1, glowy3d::vec2 p2); - ~RigidLine() {} - - virtual void update(float dt); - - virtual void collision(Body * other); - virtual void collision(Circle * other); - virtual void collision(Walker * other); - virtual void collision(glowy3d::vec2 lineP1, glowy3d::vec2 lineP2); - -private: - glowy3d::vec2 p1, p2; - glowy3d::vec2 bounce; - bool collided; - bool horisontal; -}; - -} diff --git a/test/SamplePlatformer/Rocket.cpp b/test/SamplePlatformer/Rocket.cpp deleted file mode 100644 index 0139ab2..0000000 --- a/test/SamplePlatformer/Rocket.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "Rocket.h" - -using namespace glm; -namespace game -{ - -Rocket::Rocket(vec2 position, float speed) : - Projectile(position, vec2(speed, speed)) -{ - -} - -void Rocket::update(float dt) -{ - Projectile::update(dt); -} - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Rocket.h b/test/SamplePlatformer/Rocket.h deleted file mode 100644 index 2f2cfd1..0000000 --- a/test/SamplePlatformer/Rocket.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include "Projectile.h" - -namespace game -{ - -class Rocket : public Projectile -{ -public: - Rocket(glm::vec2 position, float aim); - ~Rocket() {} - - virtual void update(float dt); - -private: - float trailFrequency = 0.3f; - float lastTrail = 0; -}; - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Tiles/Tile.cpp b/test/SamplePlatformer/Tiles/Tile.cpp deleted file mode 100644 index b8cdf53..0000000 --- a/test/SamplePlatformer/Tiles/Tile.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "Tile.h" - -namespace game -{ - -Actor * Tile::spawn(glm::vec2 pos) -{ - return nullptr; -} - -bool Tile::isTextureSet() const -{ - return false; -} - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Tiles/Tile.h b/test/SamplePlatformer/Tiles/Tile.h deleted file mode 100644 index a3c6048..0000000 --- a/test/SamplePlatformer/Tiles/Tile.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once -#include -#include "math/AdditionGLM.h" - -namespace game -{ - -class Actor; -class Tile -{ -public: - virtual bool isSolid() const = 0; - //return "" if it's untextured - virtual std::string textureName() const = 0; - virtual Actor * spawn(glm::vec2 pos); - virtual bool isTextureSet() const; - -private: -}; - -} diff --git a/test/SamplePlatformer/Tiles/TileAmmo.cpp b/test/SamplePlatformer/Tiles/TileAmmo.cpp deleted file mode 100644 index 9096770..0000000 --- a/test/SamplePlatformer/Tiles/TileAmmo.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "TileAmmo.h" - -namespace game -{ - -bool TileAmmo::isSolid() const -{ - return false; -} - -std::string TileAmmo::textureName() const -{ - return ""; -} - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Tiles/TileAmmo.h b/test/SamplePlatformer/Tiles/TileAmmo.h deleted file mode 100644 index 8002915..0000000 --- a/test/SamplePlatformer/Tiles/TileAmmo.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#include "Tile.h" - -namespace game -{ - -class TileAmmo : public Tile -{ -public: - virtual bool isSolid() const; - virtual std::string textureName() const; - -private: -}; - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Tiles/TileDirt.cpp b/test/SamplePlatformer/Tiles/TileDirt.cpp deleted file mode 100644 index 98173e0..0000000 --- a/test/SamplePlatformer/Tiles/TileDirt.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "TileDirt.h" - -namespace game -{ - -bool TileDirt::isSolid() const -{ - return true; -} - -std::string TileDirt::textureName() const -{ - return "TileDirt.png"; -} - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Tiles/TileDirt.h b/test/SamplePlatformer/Tiles/TileDirt.h deleted file mode 100644 index 962ea20..0000000 --- a/test/SamplePlatformer/Tiles/TileDirt.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#include "Tile.h" - -namespace game -{ - -class TileDirt : public Tile -{ -public: - virtual bool isSolid() const; - virtual std::string textureName() const; - -private: -}; - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Tiles/TileEmpty.cpp b/test/SamplePlatformer/Tiles/TileEmpty.cpp deleted file mode 100644 index c60a178..0000000 --- a/test/SamplePlatformer/Tiles/TileEmpty.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "TileEmpty.h" - -namespace game -{ - -bool TileEmpty::isSolid() const -{ - return false; -} - -std::string TileEmpty::textureName() const -{ - return ""; -} - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Tiles/TileEmpty.h b/test/SamplePlatformer/Tiles/TileEmpty.h deleted file mode 100644 index 7ab820f..0000000 --- a/test/SamplePlatformer/Tiles/TileEmpty.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#include "Tile.h" - -namespace game -{ - -class TileEmpty : public Tile -{ -public: - virtual bool isSolid() const; - virtual std::string textureName() const; - -private: -}; - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Tiles/TileEnemy.cpp b/test/SamplePlatformer/Tiles/TileEnemy.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/test/SamplePlatformer/Tiles/TileEnemy.h b/test/SamplePlatformer/Tiles/TileEnemy.h deleted file mode 100644 index cbfba70..0000000 --- a/test/SamplePlatformer/Tiles/TileEnemy.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -namespace game -{ - -class Tile -{ -public: - Tile() = 0; - ~Tile() = 0; - -private: -}; - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Tiles/TileFinish.cpp b/test/SamplePlatformer/Tiles/TileFinish.cpp deleted file mode 100644 index d2da158..0000000 --- a/test/SamplePlatformer/Tiles/TileFinish.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "TileFinish.h" -#include "math/AdditionGLM.h" -//#include "Finish.h" - -namespace game -{ - -bool TileFinish::isSolid() const -{ - return false; -} - -std::string TileFinish::textureName() const -{ - return "TileFinish.png"; -} - -Actor * TileFinish::spawn(glm::usvec2 pos) -{ - return nullptr; - //return new Finish(pos); -} - -} diff --git a/test/SamplePlatformer/Tiles/TileFinish.h b/test/SamplePlatformer/Tiles/TileFinish.h deleted file mode 100644 index 74c7507..0000000 --- a/test/SamplePlatformer/Tiles/TileFinish.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "Tile.h" - -namespace game -{ - -class TileFinish : public Tile -{ -public: - virtual bool isSolid() const; - virtual std::string textureName() const; - - Actor * spawn(glm::usvec2 pos); - -private: -}; - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Tiles/TileGrass.cpp b/test/SamplePlatformer/Tiles/TileGrass.cpp deleted file mode 100644 index 5eb9d74..0000000 --- a/test/SamplePlatformer/Tiles/TileGrass.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "TileGrass.h" - -namespace game -{ - -bool TileGrass::isSolid() const -{ - return true; -} - -std::string TileGrass::textureName() const -{ - return "TileGrass.png"; -} - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Tiles/TileGrass.h b/test/SamplePlatformer/Tiles/TileGrass.h deleted file mode 100644 index bc67c6c..0000000 --- a/test/SamplePlatformer/Tiles/TileGrass.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#include "Tile.h" - -namespace game -{ - -class TileGrass : public Tile -{ -public: - virtual bool isSolid() const; - virtual std::string textureName() const; - -private: -}; - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Tiles/TileStart.cpp b/test/SamplePlatformer/Tiles/TileStart.cpp deleted file mode 100644 index c419e20..0000000 --- a/test/SamplePlatformer/Tiles/TileStart.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "TileStart.h" -#include -#include "../Player.h" -#include "math/AdditionGLM.h" - -namespace game -{ - -bool TileStart::isSolid() const -{ - return false; -} - -std::string TileStart::textureName() const -{ - return ""; -} - -Actor * TileStart::spawn(glm::vec2 pos) -{ - return new Player(pos); -} - -} diff --git a/test/SamplePlatformer/Tiles/TileStart.h b/test/SamplePlatformer/Tiles/TileStart.h deleted file mode 100644 index abd5e5c..0000000 --- a/test/SamplePlatformer/Tiles/TileStart.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "Tile.h" - -namespace game -{ - -class TileStart : public Tile -{ -public: - virtual bool isSolid() const; - virtual std::string textureName() const; - - Actor * spawn(glm::vec2 pos); - -private: -}; - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Tiles/TileStone.cpp b/test/SamplePlatformer/Tiles/TileStone.cpp deleted file mode 100644 index 26e73a4..0000000 --- a/test/SamplePlatformer/Tiles/TileStone.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "TileStone.h" - -namespace game -{ - -bool TileStone::isSolid() const -{ - return true; -} - -bool TileStone::isTextureSet() const -{ - return true; -} - -std::string TileStone::textureName() const -{ - return "Tiles/stone.png"; -} - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Tiles/TileStone.h b/test/SamplePlatformer/Tiles/TileStone.h deleted file mode 100644 index bfb0a78..0000000 --- a/test/SamplePlatformer/Tiles/TileStone.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once -#include "Tile.h" - -namespace game -{ - -class TileStone : public Tile -{ -public: - virtual bool isSolid() const; - virtual std::string textureName() const; - virtual bool isTextureSet() const; - -private: -}; - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Tiles/TileWood.cpp b/test/SamplePlatformer/Tiles/TileWood.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/test/SamplePlatformer/Tiles/TileWood.h b/test/SamplePlatformer/Tiles/TileWood.h deleted file mode 100644 index cbfba70..0000000 --- a/test/SamplePlatformer/Tiles/TileWood.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -namespace game -{ - -class Tile -{ -public: - Tile() = 0; - ~Tile() = 0; - -private: -}; - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Walker.cpp b/test/SamplePlatformer/Walker.cpp deleted file mode 100644 index 61a75af..0000000 --- a/test/SamplePlatformer/Walker.cpp +++ /dev/null @@ -1,77 +0,0 @@ -#include "Walker.h" -#include "RigidLine.h" -#include - -using namespace glm; -namespace game -{ - -Walker::Walker(glm::vec2 pos) : - Circle(pos, 1.f, vec2(1.0f, 1.f)), - grounded(false) -{ - -} - -void Walker::update(float dt) -{ - Circle::update(dt); - if (grounded) - { - setVelocity(getVelocity() * 0.999f * dt); - checkGrounded(); - if (getPosition().y > groundedHeight) - setPosition(vec2(getPosition().x, - getPrevPosition().y)); - } -} - -void Walker::checkGrounded() -{ - if (getPosition().y < groundedHeight - 0.01f || - getPosition().x < groundedLeft || - getPosition().x > groundedRight) - { - detachFromGround(); - } -} - -void Walker::attachToGround(float platformLeft, float platformRight) -{ - setFloating(true); - groundedHeight = getPosition().y; - groundedLeft = platformLeft - getRadius(); - groundedRight = platformRight + getRadius(); - grounded = true; -} - -void Walker::detachFromGround() -{ - grounded = false; - setFloating(false); -} - -bool Walker::isGrounded() const -{ - return grounded; -} - -void Walker::collision(Body * other) -{ - other->collision(this); -} - -void Walker::collision(RigidLine * other) -{ - other->collision(this); -} - -vec2 Walker::getBounce() const -{ - if (grounded) - return vec2(0.51f, 0.51f); - else - return Circle::getBounce(); -} - -} \ No newline at end of file diff --git a/test/SamplePlatformer/Walker.h b/test/SamplePlatformer/Walker.h deleted file mode 100644 index fea1cbe..0000000 --- a/test/SamplePlatformer/Walker.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once -#include "Circle.h" - -namespace game -{ - -class Walker : public Circle -{ -public: - Walker(glm::vec2 pos); - - virtual void update(float dt); - - void checkGrounded(); - void attachToGround(float platformLeft, - float platformRight); - void detachFromGround(); - bool isGrounded() const; - - virtual void collision(Body * other); - virtual void collision(RigidLine * other); - - glm::vec2 getBounce() const override; - -private: - bool grounded; - float groundedHeight; - float groundedLeft; - float groundedRight; -}; - -} \ No newline at end of file diff --git a/test/SamplePlatformer/World.cpp b/test/SamplePlatformer/World.cpp deleted file mode 100644 index 3800f50..0000000 --- a/test/SamplePlatformer/World.cpp +++ /dev/null @@ -1,214 +0,0 @@ -#include "World.h" -#include -#include -#include "math/AdditionGLM.h" -#include "Tiles/Tile.h" -#include "Background.h" -#include "CollisionData.h" -#include "GlobalData.h" -#include "Actor.h" -#include "Engine.h" -#include "Player.h" -#include "graphics/Camera.h" -#include "graphics/Layer.h" -#include "graphics/Sprite.h" - -using namespace glm; -using namespace std; -using namespace glowy3d; -namespace game -{ - -void appendDirection(string& name, CollisionData * collisionData, usvec2 pos); -void randomizeIfPossible(string& name); - -World::World(const MapInfo& map) : - mapLayer(new glowy3d::Layer(2048, 2048)), - background(new Background(map.backgroundImage)), - collisionData(new CollisionData(map)) -{ - std::map textures; - for (uint i = 0; i < map.tiles.size(); ++i) - for (uint j = 0; j < map.tiles[i].size(); ++j) - { - auto name = map.tiles[i][j]->textureName(); - if (name != "") - { - if (map.tiles[i][j]->isTextureSet()) - appendDirection(name, collisionData, usvec2(j + 1, i + 1)); - randomizeIfPossible(name); - if (textures.find(name) == end(textures)) - textures[name] = mapLayer->addTexture(name.c_str()); - - mapLayer->add(textures[name]) - ->addPosition(toRenderer(vec2(j + 1, i))); - } - } - mapLayer->setZOrder(0.8); - mapLayer->updateBuffer(); - - GlobalData::actors = &actors; - GlobalData::collisionData = collisionData; - - for (uint i = 0; i < map.tiles.size(); ++i) - for (uint j = 0; j < map.tiles[i].size(); ++j) - { - auto tile = map.tiles[i][j]; - auto actor = tile->spawn(usvec2(j + 1, i + 1)); - if (actor) - actors.push_back(actor); - } - - collisionData->traceContours(usvec2(0,0)); - System::camera->addPosition(vec2(0.f, 0.f)); -} - -World::~World() -{ - for (auto &actor : actors) - delete actor; - delete mapLayer; - delete background; - delete collisionData; -} - -int World::update(float dt) -{ - for (auto &actor : actors) - actor->update(dt); - background->update(dt); - /*System::camera->addPosition((vec2(-GlobalData::player->getPosition().x, - GlobalData::player->getPosition().y) - - GlobalData::player->getPrevPosition()) - / 32.f);*/ - draw(); - return 0; -} - -void World::draw() -{ - background->draw(); - mapLayer->draw(); - for (auto &actor : actors) - actor->draw(); -} - -void appendDirection(string& name, CollisionData * collisionData, usvec2 pos) -{ - auto t = usvec2(0, 1); - auto tr = usvec2(1, 1); - auto tl = usvec2(-1, 1); - auto l = usvec2(-1, 0); - auto r = usvec2(1, 0); - auto b = usvec2(0,-1); - auto bl = usvec2(-1, -1); - auto br = usvec2(1, -1); - auto insertPos = name.size() - 4; - string postfix; - - if (collisionData->check(pos + t) && - !collisionData->check(pos + tl) && - collisionData->check(pos + tr) && - collisionData->check(pos + bl) && - collisionData->check(pos + br) && - collisionData->check(pos + b) && - collisionData->check(pos + l) && - collisionData->check(pos + r)) - postfix = "_inside_tr"; - else - if (collisionData->check(pos + t) && - collisionData->check(pos + tl) && - !collisionData->check(pos + tr) && - collisionData->check(pos + bl) && - collisionData->check(pos + br) && - collisionData->check(pos + b) && - collisionData->check(pos + l) && - collisionData->check(pos + r)) - postfix = "_inside_tl"; - else - if (!collisionData->check(pos + t) && - !collisionData->check(pos + tr) && - collisionData->check(pos + bl) && - collisionData->check(pos + b) && - collisionData->check(pos + l) && - !collisionData->check(pos + r)) - postfix = "_corner_br"; - else - if (!collisionData->check(pos + t) && - !collisionData->check(pos + tl) && - collisionData->check(pos + br) && - collisionData->check(pos + b) && - !collisionData->check(pos + l) && - collisionData->check(pos + r)) - postfix = "_corner_bl"; - else - if (collisionData->check(pos + t) && - collisionData->check(pos + tl) && - collisionData->check(pos + tr) && - collisionData->check(pos + bl) && - !collisionData->check(pos + br) && - collisionData->check(pos + b) && - collisionData->check(pos + l) && - collisionData->check(pos + r)) - postfix = "_inside_bl"; - else - if (collisionData->check(pos + t) && - collisionData->check(pos + tl) && - collisionData->check(pos + tr) && - !collisionData->check(pos + bl) && - collisionData->check(pos + br) && - collisionData->check(pos + b) && - collisionData->check(pos + l) && - collisionData->check(pos + r)) - postfix = "_inside_br"; - else - if (collisionData->check(pos + t) && - collisionData->check(pos + tr) && - !collisionData->check(pos + bl) && - !collisionData->check(pos + b) && - !collisionData->check(pos + l) && - collisionData->check(pos + r)) - postfix = "_corner_tl"; - else - if (collisionData->check(pos + t) && - collisionData->check(pos + tl) && - !collisionData->check(pos + br) && - !collisionData->check(pos + b) && - collisionData->check(pos + l) && - !collisionData->check(pos + r)) - postfix = "_corner_tr"; - else - if (!collisionData->check(pos + t) && - collisionData->check(pos + b) && - collisionData->check(pos + l) && - collisionData->check(pos + r)) - postfix = "_bottom"; - else - if (collisionData->check(pos + t) && - !collisionData->check(pos + b) && - collisionData->check(pos + l) && - collisionData->check(pos + r)) - postfix = "_top"; - else - if (collisionData->check(pos + t) && - collisionData->check(pos + b) && - collisionData->check(pos + l) && - !collisionData->check(pos + r)) - postfix = "_right"; - else - if (collisionData->check(pos + t) && - collisionData->check(pos + b) && - !collisionData->check(pos + l) && - collisionData->check(pos + r)) - postfix = "_left"; - - name.insert(insertPos, postfix); -} - -void randomizeIfPossible(string& name) -{ - -} - - -} diff --git a/test/SamplePlatformer/World.h b/test/SamplePlatformer/World.h deleted file mode 100644 index e35daee..0000000 --- a/test/SamplePlatformer/World.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once -#include -#include "graphics/Layer.h" -#include "MapInfo.h" - -namespace game -{ - -class CollisionData; -class Background; -class Actor; -class World -{ -public: - World(const MapInfo& map); - ~World(); - - int update(float dt); -private: - void draw(); - glowy3d::Layer * mapLayer; - Background * background; - CollisionData * collisionData; - std::vector actors; -}; - -} diff --git a/test/Triangle/triangle.cpp b/test/Triangle/triangle.cpp deleted file mode 100644 index e10159e..0000000 --- a/test/Triangle/triangle.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "Engine.h" -#include - -using namespace std; - -void init() -{ - cout << "test" << endl; -} - -void update() -{ - -} - -int main() -{ - glowy3d::start(init, update); -}