diff --git a/src/dawn/asset/assets/TilesetAsset.cpp b/src/dawn/asset/assets/TilesetAsset.cpp index 77ebbeef..d9b6c145 100644 --- a/src/dawn/asset/assets/TilesetAsset.cpp +++ b/src/dawn/asset/assets/TilesetAsset.cpp @@ -43,6 +43,20 @@ void TilesetAsset::updateAsync() { this->state = 0x04; assertTrue(this->tileset.rows > 0); + strCurrent = strNext+1; + strNext = strchr(strCurrent, '|'); + *strNext = '\0'; + this->tileset.divX = atoi(strCurrent); + this->state = 0x05; + assertTrue(this->tileset.rows > 0); + + strCurrent = strNext+1; + strNext = strchr(strCurrent, '|'); + *strNext = '\0'; + this->tileset.divY = atoi(strCurrent); + this->state = 0x06; + assertTrue(this->tileset.rows > 0); + // Begin reading tiles. int32_t done = 0; int32_t count = this->tileset.columns * this->tileset.rows; @@ -79,7 +93,7 @@ void TilesetAsset::updateAsync() { done++; } - this->state = 0x05; + this->state = 0x07; this->loaded = true; this->eventLoaded.invoke(); diff --git a/src/dawn/display/RenderPipeline.cpp b/src/dawn/display/RenderPipeline.cpp index 5ad038af..1226f148 100644 --- a/src/dawn/display/RenderPipeline.cpp +++ b/src/dawn/display/RenderPipeline.cpp @@ -126,10 +126,6 @@ void RenderPipeline::renderUI( // Clear / Bind / Update the render target. renderTarget->bind(); - renderTarget->clear( - RENDER_TARGET_CLEAR_FLAG_DEPTH | - RENDER_TARGET_CLEAR_FLAG_COLOR - ); this->renderManager->setRenderFlags( RENDER_MANAGER_RENDER_FLAG_BLEND | RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST diff --git a/src/dawn/display/Tileset.hpp b/src/dawn/display/Tileset.hpp index cd824a84..d2396155 100644 --- a/src/dawn/display/Tileset.hpp +++ b/src/dawn/display/Tileset.hpp @@ -16,12 +16,20 @@ namespace Dawn { struct Tileset { public: std::vector tiles; + + struct Tile getTile(int32_t tile) { + assertTrue(tile >= 0); + assertTrue(tile < this->tiles.size()); + return this->tiles[tile]; + } }; struct TilesetGrid : public Tileset{ public: int32_t rows; int32_t columns; + int32_t divX; + int32_t divY; TilesetGrid() { @@ -30,8 +38,8 @@ namespace Dawn { TilesetGrid( int32_t columns, int32_t rows, - int32_t width, - int32_t height, + int32_t w, + int32_t h, int32_t gapX, int32_t gapY, int32_t borderX, @@ -39,49 +47,43 @@ namespace Dawn { ) { assertTrue(columns >= 1); assertTrue(rows >= 1); - assertTrue(width >= 1); - assertTrue(height >= 1); + assertTrue(w >= 1); + assertTrue(h >= 1); assertTrue(gapX >= 0); assertTrue(gapY >= 0); assertTrue(borderX >= 0); assertTrue(borderY >= 0); - assertTrue(width >= (columns + (gapX * columns) + borderX + borderX)); - assertTrue(height >= (rows + (gapY * rows) + borderY + borderY)); + assertTrue(w >= (columns + (gapX * columns) + borderX + borderX)); + assertTrue(h >= (rows + (gapY * rows) + borderY + borderY)); this->rows = rows; this->columns = columns; // Calculate division sizes (pixels) - float_t divX = ( - (float_t)width - ((float_t)borderX * 2.0f) - - ((float_t)gapX * ((float_t)columns - 1)) - ) / columns; - float_t divY = ( - (float_t)height - ((float_t)borderY * 2.0f) - - ((float_t)gapY * ((float_t)rows - 1)) - ) / rows; + this->divX = (w - (borderX * 2.0f) - (gapX * (columns - 1))) / columns; + this->divY = (h - (borderY * 2.0f) - (gapY * (rows - 1))) / rows; // Calculate the division sizes (units) - float_t tdivX = divX / (float_t)width; - float_t tdivY = divY / (float_t)height; + float_t tdivX = (float_t)this->divX / (float_t)w; + float_t tdivY = (float_t)this->divY / (float_t)h; for(int32_t y = 0; y < rows; y++) { for(int32_t x = 0; x < columns; x++) { struct Tile tile; - tile.uv0.x = (borderX + (divX * x) + (gapX * x)) / width; + tile.uv0.x = (borderX + ((float_t)this->divX * x) + (gapX * x)) / w; tile.uv1.x = tile.uv0.x + tdivX; - tile.uv0.y = (borderY + (divY * y) + (gapY * y)) / height; + tile.uv0.y = (borderY + ((float_t)this->divY * y) + (gapY * y)) / h; tile.uv1.y = tile.uv0.y + tdivY; this->tiles.push_back(tile); } } } - struct Tile getTile(int32_t row, int32_t column) { + struct Tile getTileFromGrid(int32_t row, int32_t column) { assertTrue(row > 0 && row < this->rows); assertTrue(column > 0 && column < this->columns); - return this->tiles[row + (column * this->rows)]; + return this->getTile(row + (column * this->rows)); } }; } \ No newline at end of file diff --git a/src/dawn/display/mesh/QuadMesh.cpp b/src/dawn/display/mesh/QuadMesh.cpp index c178c60b..f28a5f14 100644 --- a/src/dawn/display/mesh/QuadMesh.cpp +++ b/src/dawn/display/mesh/QuadMesh.cpp @@ -50,6 +50,33 @@ void QuadMesh::bufferQuadMesh( ); } +void QuadMesh::bufferCoordinates( + Mesh *mesh, + glm::vec2 uv0, glm::vec2 uv1, + int32_t verticeStart +) { + assertNotNull(mesh); + mesh->bufferCoordinates(verticeStart, std::array{{ + uv0, glm::vec2(uv1.x, uv0.y), + glm::vec2(uv0.x, uv1.y), uv1 + }}); +} + +void QuadMesh::bufferPositions( + Mesh *mesh, + glm::vec2 xy0, glm::vec2 xy1, + int32_t verticeStart +) { + mesh->bufferPositions( + verticeStart, std::array{{ + glm::vec3(xy0, 0), + glm::vec3(xy1.x, xy0.y, 0), + glm::vec3(xy0.x, xy1.y, 0), + glm::vec3(xy1, 0) + }} + ); +} + void QuadMesh::initQuadMesh( Mesh *mesh, glm::vec2 xy0, glm::vec2 uv0, diff --git a/src/dawn/display/mesh/QuadMesh.hpp b/src/dawn/display/mesh/QuadMesh.hpp index 2604317c..df057481 100644 --- a/src/dawn/display/mesh/QuadMesh.hpp +++ b/src/dawn/display/mesh/QuadMesh.hpp @@ -16,7 +16,7 @@ namespace Dawn { /** * Buffers the vertices of a quad onto a primitive. * - * @param primitive The primitive to buffer to. + * @param mesh The primitive to buffer to. * @param xy0 The lower X and Y coordinate. * @param uv0 The lower Xand Y texture coordinate. * @param xy1 The higher X and Y coordinate. @@ -35,7 +35,7 @@ namespace Dawn { /** * Buffers the vertices of a quad onto a primitive. * - * @param primitive The primitive to buffer to. + * @param mesh The primitive to buffer to. * @param xy0 The lower X and Y coordinate. * @param uv0 The lower Xand Y texture coordinate. * @param xy1 The higher X and Y coordinate. @@ -50,6 +50,44 @@ namespace Dawn { int32_t verticeStart, int32_t indiceStart ); + /** + * Buffers texture coordinates on to an already initialized quad mesh. + * + * @param mesh Mesh to buffer the texture coordinates on to. + * @param uv0 Lower X and Y coordinates. + * @param uv1 Upper X and Y coordinates. + * @param verticeStart Start vertice to buffer in to. + */ + static void bufferCoordinates( + Mesh *mesh, + glm::vec2 uv0, glm::vec2 uv1, + int32_t verticeStart + ); + + /** + * Buffers the positions of a quad onto a primitive. + * + * @param mesh The primitive to buffer to. + * @param xy0 The lower X and Y coordinate. + * @param xy1 The higher X and Y coordinate. + * @param verticeStart Start vertice to buffer to. + */ + static void bufferPositions( + Mesh *mesh, + glm::vec2 xy0, glm::vec2 xy1, + int32_t verticeStart + ); + + /** + * Initializes a mesh to be a single quad. + * + * @param mesh The primitive to buffer to. + * @param xy0 The lower X and Y coordinate. + * @param uv0 The lower Xand Y texture coordinate. + * @param xy1 The higher X and Y coordinate. + * @param uv1 The higher X and Y texture coordinate. + * @param z The Z position of the coordinates. + */ static void initQuadMesh( Mesh *mesh, glm::vec2 xy0, glm::vec2 uv0, diff --git a/src/dawn/scene/SceneItem.cpp b/src/dawn/scene/SceneItem.cpp index 9223e425..503bb379 100644 --- a/src/dawn/scene/SceneItem.cpp +++ b/src/dawn/scene/SceneItem.cpp @@ -36,8 +36,8 @@ void SceneItem::init() { auto it2 = deps.begin(); while(it2 != deps.end()) { if(*it2 == nullptr) { - continue; ++it2; + continue; } // Has the dep not yet inited? diff --git a/src/dawn/scene/components/CMakeLists.txt b/src/dawn/scene/components/CMakeLists.txt index d22abb4e..48fbe08f 100644 --- a/src/dawn/scene/components/CMakeLists.txt +++ b/src/dawn/scene/components/CMakeLists.txt @@ -5,4 +5,5 @@ # Subdirs add_subdirectory(display) +add_subdirectory(example) add_subdirectory(ui) \ No newline at end of file diff --git a/src/dawn/scene/components/Components.hpp b/src/dawn/scene/components/Components.hpp index 5f9b8767..dd35e821 100644 --- a/src/dawn/scene/components/Components.hpp +++ b/src/dawn/scene/components/Components.hpp @@ -10,4 +10,6 @@ #include "scene/components/display/Material.hpp" #include "scene/components/display/TiledSprite.hpp" +#include "scene/components/example/ExampleSpin.hpp" + #include "scene/components/ui/UICanvas.hpp" \ No newline at end of file diff --git a/src/dawn/scene/components/display/Material.hpp b/src/dawn/scene/components/display/Material.hpp index cfb59727..017f0d47 100644 --- a/src/dawn/scene/components/display/Material.hpp +++ b/src/dawn/scene/components/display/Material.hpp @@ -12,7 +12,7 @@ namespace Dawn { class Material : public SceneItemComponent { private: - Shader *shader; + Shader *shader = nullptr; /** * Internal method that will be invoked to go through and update all of diff --git a/src/dawn/scene/components/display/MeshRenderer.cpp b/src/dawn/scene/components/display/MeshRenderer.cpp index a34b7eea..dca8d22c 100644 --- a/src/dawn/scene/components/display/MeshRenderer.cpp +++ b/src/dawn/scene/components/display/MeshRenderer.cpp @@ -14,15 +14,14 @@ MeshRenderer::MeshRenderer(SceneItem *item) : SceneItemComponent(item) { std::vector MeshRenderer::getDependencies() { return std::vector{ - this->item->getComponent() + this->meshHost = this->item->getComponent() }; } void MeshRenderer::onStart() { SceneItemComponent::onStart(); - if(mesh == nullptr) { - auto host = this->item->getComponent(); - if(host != nullptr) this->mesh = &host->mesh; + if(this->mesh == nullptr && this->meshHost != nullptr) { + this->mesh = &this->meshHost->mesh; } } \ No newline at end of file diff --git a/src/dawn/scene/components/display/MeshRenderer.hpp b/src/dawn/scene/components/display/MeshRenderer.hpp index 57a4af4b..da673a1f 100644 --- a/src/dawn/scene/components/display/MeshRenderer.hpp +++ b/src/dawn/scene/components/display/MeshRenderer.hpp @@ -8,7 +8,12 @@ #include "display/mesh/Mesh.hpp" namespace Dawn { + class MeshHost; + class MeshRenderer : public SceneItemComponent { + protected: + MeshHost *meshHost = nullptr; + public: Mesh * mesh = nullptr; diff --git a/src/dawn/scene/components/display/TiledSprite.cpp b/src/dawn/scene/components/display/TiledSprite.cpp index 3aaf07e4..34f3ad7e 100644 --- a/src/dawn/scene/components/display/TiledSprite.cpp +++ b/src/dawn/scene/components/display/TiledSprite.cpp @@ -12,6 +12,52 @@ TiledSprite::TiledSprite(SceneItem *item) : SceneItemComponent(item) { } +glm::vec2 TiledSprite::getUV0() { + auto tile = this->tileset->getTile(tileIndex); + return glm::vec2( + (this->flipState & TILED_SPRITE_FLIP_X) == 0 ? tile.uv0.x : tile.uv1.x, + (this->flipState & TILED_SPRITE_FLIP_Y) == 0 ? tile.uv0.y : tile.uv1.y + ); +} + +glm::vec2 TiledSprite::getUV1() { + auto tile = this->tileset->getTile(tileIndex); + return glm::vec2( + (this->flipState & TILED_SPRITE_FLIP_X) == 0 ? tile.uv1.x : tile.uv0.x, + (this->flipState & TILED_SPRITE_FLIP_Y) == 0 ? tile.uv1.y : tile.uv0.y + ); +} + +void TiledSprite::setTileset(Tileset *tileset) { + assertNotNull(tileset); + this->tileset = tileset; + this->setTile(0); +} + +void TiledSprite::setTile(int32_t tileIndex) { + assertNotNull(this->tileset); + this->tileIndex = tileIndex; + if(this->host != nullptr) { + QuadMesh::bufferCoordinates( + &this->host->mesh, this->getUV0(), this->getUV1(), 0 + ); + } +} + +void TiledSprite::setSize(glm::vec2 size, glm::vec2 center) { + this->xy0 = -center; + this->xy1 = size - center; + if(this->host != nullptr) { + QuadMesh::bufferPositions( + &this->host->mesh, this->xy0, this->xy1, 0 + ); + } +} + +void TiledSprite::setSize(glm::vec2 size) { + this->setSize(size, size / 2.0f); +} + std::vector TiledSprite::getDependencies() { this->renderer = this->item->getComponent(); this->host = this->item->getComponent(); @@ -24,12 +70,12 @@ std::vector TiledSprite::getDependencies() { void TiledSprite::onStart() { SceneItemComponent::onStart(); - assertNotNull(this->host); + assertNotNull(this->tileset); QuadMesh::initQuadMesh(&this->host->mesh, - glm::vec2(0, 0), glm::vec2(0, 0), - glm::vec2(1, 1), glm::vec2(0.125f, 0.125f), + this->xy0, this->getUV0(), + this->xy1, this->getUV1(), 0 ); } \ No newline at end of file diff --git a/src/dawn/scene/components/display/TiledSprite.hpp b/src/dawn/scene/components/display/TiledSprite.hpp index 8d381162..0660bce1 100644 --- a/src/dawn/scene/components/display/TiledSprite.hpp +++ b/src/dawn/scene/components/display/TiledSprite.hpp @@ -10,15 +10,32 @@ #include "display/mesh/QuadMesh.hpp" #include "display/Tileset.hpp" +#define TILED_SPRITE_FLIP_Y FLAG_DEFINE(0) +#define TILED_SPRITE_FLIP_X FLAG_DEFINE(1) + namespace Dawn { class TiledSprite : public SceneItemComponent { protected: - MeshRenderer *renderer; - MeshHost *host; + MeshRenderer *renderer = nullptr; + MeshHost *host = nullptr; + Tileset *tileset = nullptr; + flag_t flipState = TILED_SPRITE_FLIP_Y; + int32_t tileIndex; + glm::vec2 xy0 = glm::vec2(0, 0); + glm::vec2 xy1 = glm::vec2(1, 1); + + glm::vec2 getUV0(); + glm::vec2 getUV1(); public: TiledSprite(SceneItem *item); + void setTileset(Tileset *tileset); + void setTile(int32_t tile); + void setFlippedState(flag_t flippedState); + void setSize(glm::vec2 size, glm::vec2 center); + void setSize(glm::vec2 size); + std::vector getDependencies() override; void onStart() override; }; diff --git a/src/dawn/scene/components/example/CMakeLists.txt b/src/dawn/scene/components/example/CMakeLists.txt new file mode 100644 index 00000000..13a039b4 --- /dev/null +++ b/src/dawn/scene/components/example/CMakeLists.txt @@ -0,0 +1,10 @@ +# Copyright (c) 2022 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Sources +target_sources(${DAWN_TARGET_NAME} + PRIVATE + ExampleSpin.cpp +) \ No newline at end of file diff --git a/src/dawn/scene/components/example/ExampleSpin.cpp b/src/dawn/scene/components/example/ExampleSpin.cpp new file mode 100644 index 00000000..66b436e7 --- /dev/null +++ b/src/dawn/scene/components/example/ExampleSpin.cpp @@ -0,0 +1,44 @@ +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "ExampleSpin.hpp" +#include "scene/Scene.hpp" +#include "game/DawnGame.hpp" +#include "scene/components/display/MeshRenderer.hpp" +#include "display/mesh/CubeMesh.hpp" + +using namespace Dawn; + +SceneItem * ExampleSpin::create(Scene *scene) { + auto item = scene->createSceneItem(); + auto mr = item->addComponent(); + mr->mesh = new Mesh(); + mr->mesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT); + CubeMesh::buffer(mr->mesh, glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(1, 1, 1), 0, 0); + auto mat = item->addComponent(); + item->addComponent(); + + return item; +} + +ExampleSpin::ExampleSpin(SceneItem *item) : + SceneItemComponent(item) +{ + getScene()->eventSceneUnpausedUpdate.addListener(this, &ExampleSpin::onUnpausedUpdate); +} + + +void ExampleSpin::onUnpausedUpdate() { + auto quat = this->transform->getLocalRotation(); + quat = glm::rotate(quat, getGame()->timeManager.delta, glm::vec3(0, 1, 0)); + quat = glm::rotate(quat, getGame()->timeManager.delta / 2.0f, glm::vec3(1, 0, 0)); + quat = glm::rotate(quat, getGame()->timeManager.delta / 4.0f, glm::vec3(0, 0, 1)); + + this->transform->setLocalRotation(quat); +} + +ExampleSpin::~ExampleSpin() { + getScene()->eventSceneUnpausedUpdate.removeListener(this, &ExampleSpin::onUnpausedUpdate); +} \ No newline at end of file diff --git a/src/dawn/scene/components/example/ExampleSpin.hpp b/src/dawn/scene/components/example/ExampleSpin.hpp new file mode 100644 index 00000000..3fa7323e --- /dev/null +++ b/src/dawn/scene/components/example/ExampleSpin.hpp @@ -0,0 +1,19 @@ +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "scene/SceneItemComponent.hpp" +#include "display/shader/Shader.hpp" + +namespace Dawn { + class ExampleSpin : public SceneItemComponent { + public: + static SceneItem * create(Scene *scene); + + ExampleSpin(SceneItem *item); + void onUnpausedUpdate(); + ~ExampleSpin(); + }; +} \ No newline at end of file diff --git a/src/dawnopengl/display/shader/SimpleTexturedShader.hpp b/src/dawnopengl/display/shader/SimpleTexturedShader.hpp index 182166bb..c1713a59 100644 --- a/src/dawnopengl/display/shader/SimpleTexturedShader.hpp +++ b/src/dawnopengl/display/shader/SimpleTexturedShader.hpp @@ -30,6 +30,7 @@ namespace Dawn { void setDefaultParameters(Material *material) override { material->colorValues[this->paramColor] = COLOR_WHITE; + material->textureValues[this->paramTexture] = nullptr; } void setGlobalParameters(glm::mat4 proj, glm::mat4 view) override { @@ -45,12 +46,12 @@ namespace Dawn { shaderparameter_t param, Texture *texture ) override { - if(texture == nullptr) { + if(texture == nullptr || !texture->isReady()) { this->setBoolean(this->paramHasTexture, false); } else { this->setBoolean(this->paramHasTexture, true); - this->setTextureSlot(param, 0x00); - texture->bind(0x00); + this->setTextureSlot(param, 0x01); + texture->bind(0x01); } } @@ -73,11 +74,11 @@ namespace Dawn { // Fragment Shader "#version 330 core\n" - "out vec4 o_Color;\n" "in vec2 o_TextCoord;\n" + "out vec4 o_Color;\n" "uniform vec4 u_Color;\n" - "uniform sampler2D u_Text;\n" "uniform bool u_HasTexture;\n" + "uniform sampler2D u_Text;\n" "void main() {\n" "if(u_HasTexture) {\n" diff --git a/src/dawnopengl/display/shader/UIShader.hpp b/src/dawnopengl/display/shader/UIShader.hpp index 7fc5a7cd..9d043473 100644 --- a/src/dawnopengl/display/shader/UIShader.hpp +++ b/src/dawnopengl/display/shader/UIShader.hpp @@ -30,6 +30,7 @@ namespace Dawn { void setDefaultParameters(Material *material) override { material->colorValues[this->paramColor] = COLOR_WHITE; + material->textureValues[this->paramTexture] = nullptr; } void setGlobalParameters(glm::mat4 proj, glm::mat4 view) override { @@ -73,11 +74,11 @@ namespace Dawn { // Fragment Shader "#version 330 core\n" - "out vec4 o_Color;\n" "in vec2 o_TextCoord;\n" + "out vec4 o_Color;\n" "uniform vec4 u_Color;\n" - "uniform sampler2D u_Text;\n" "uniform bool u_HasTexture;\n" + "uniform sampler2D u_Text;\n" "void main() {\n" "if(u_HasTexture) {\n" @@ -98,7 +99,6 @@ namespace Dawn { this->setBoolean(this->paramHasTexture, false); } - void setUICamera(glm::mat4 view, glm::mat4 projection) { this->setMatrix(this->paramView, view); this->setMatrix(this->paramProjection, projection); diff --git a/src/dawnpokergame/prefabs/VNPenny.hpp b/src/dawnpokergame/prefabs/VNPenny.hpp index 532f3df5..28da0706 100644 --- a/src/dawnpokergame/prefabs/VNPenny.hpp +++ b/src/dawnpokergame/prefabs/VNPenny.hpp @@ -22,9 +22,17 @@ namespace Dawn { auto item = scene->createSceneItem(); auto meshRenderer = item->addComponent(); + auto material = item->addComponent(); + auto asset = scene->game->assetManager.get("texture_penny"); + auto param = material->getShader()->getParameterByName("u_Text"); + material->textureValues[param] = &asset->texture; + auto meshHost = item->addComponent(); auto tiledSprite = item->addComponent(); + tiledSprite->setTileset(&scene->game->assetManager.get("tileset_penny")->tileset); + tiledSprite->setSize(glm::vec2(2, 2)); + auto pokerPlayer = item->addComponent(); return item; diff --git a/src/dawnpokergame/scenes/TestScene.hpp b/src/dawnpokergame/scenes/TestScene.hpp index 57737ad8..25805dda 100644 --- a/src/dawnpokergame/scenes/TestScene.hpp +++ b/src/dawnpokergame/scenes/TestScene.hpp @@ -39,8 +39,8 @@ namespace Dawn { void stage() override { // Camera auto camera = Camera::create(this); - camera->transform->lookAt(glm::vec3(50, 50, 50), glm::vec3(0, 0, 0)); - + camera->transform->lookAt(glm::vec3(3, 3, 3), glm::vec3(0, 0, 0)); + // UI auto canvas = UICanvas::createCanvas(this); auto textbox = PokerGameTextbox::create(canvas); diff --git a/src/dawntools/display/tilesetgen/main.c b/src/dawntools/display/tilesetgen/main.c index f3a79152..e2653be1 100644 --- a/src/dawntools/display/tilesetgen/main.c +++ b/src/dawntools/display/tilesetgen/main.c @@ -105,31 +105,25 @@ int main(int argc, char *argv[]) { } // Calculate division sizes (pixels) - float divX = ( - (float)w - ((float)borderX * 2.0f) - - ((float)gapX * ((float)cols - 1)) - ) / cols; - float divY = ( - (float)h - ((float)borderY * 2.0f) - - ((float)gapY * ((float)rows - 1)) - ) / rows; + int divX = (w - (borderX * 2) - (gapX * (cols - 1))) / cols; + int divY = (h - (borderY * 2) - (gapY * (rows - 1))) / rows; // Calculate the division sizes (units) - float tdivX = divX / (float)w; - float tdivY = divY / (float)h; + float tdivX = (float)divX / (float)w; + float tdivY = (float)divY / (float)h; // Output buffer prep char *buffer = malloc(sizeof(char) * (cols * rows * 48 + 48 + 48)); buffer[0] = '\0'; - sprintf(buffer, "%i|%i|", cols, rows); + sprintf(buffer, "%i|%i|%i|%i|", cols, rows, divX, divY); // Now prep tileset. for(int y = 0; y < rows; y++) { for(int x = 0; x < cols; x++) { - float ux0 = (borderX + (divX * x) + (gapX * x)) / w; + float ux0 = (borderX + ((float)divX * x) + ((float)gapX * x)) / w; float ux1 = ux0 + tdivX; - float uy0 = (borderY + (divY * y) + (gapY * y)) / h; + float uy0 = (borderY + ((float)divY * y) + ((float)gapY * y)) / h; float uy1 = uy0 + tdivY; sprintf(buffer, "%s%f,%f,%f,%f|", buffer, ux0, ux1, uy0, uy1); }