Testing textures

This commit is contained in:
2023-05-11 23:57:08 -07:00
parent 642f1408ff
commit f44fe25011
7 changed files with 76 additions and 173 deletions

View File

@ -15,5 +15,5 @@ TiledSpriteAnimation::TiledSpriteAnimation(TiledSprite *sprite) :
void TiledSpriteAnimation::tick(float_t delta) {
SimpleAnimation::tick(delta);
this->sprite->setTile(frame);
this->sprite->tile = frame;
}

View File

@ -8,84 +8,36 @@
using namespace Dawn;
TiledSprite::TiledSprite(SceneItem *item) : SceneItemComponent(item) {
TiledSprite::TiledSprite(SceneItem *item) :
SceneItemComponent(item),
tile(-1),
tileset(nullptr),
meshHost(nullptr)
{
}
glm::vec2 TiledSprite::getUV0() {
assertNotNull(this->tileset);
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() {
assertNotNull(this->tileset);
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::setTilesetAndSize(TilesetGrid *tileset, glm::vec2 center) {
this->setTileset(tileset);
this->setSize(glm::vec2(tileset->divX, tileset->divY), center);
}
void TiledSprite::setTilesetAndSize(TilesetGrid *tileset) {
this->setTileset(tileset);
this->setSize(glm::vec2(tileset->divX, tileset->divY));
}
void TiledSprite::setTile(int32_t tileIndex) {
if(tileIndex == this->tileIndex) return;
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<SceneItemComponent*> TiledSprite::getDependencies() {
this->host = this->item->getComponent<MeshHost>();
if(this->meshHost == nullptr) {
this->meshHost = this->item->getComponent<QuadMeshHost>();
}
return std::vector<SceneItemComponent*>{
this->host
return {
this->meshHost
};
}
void TiledSprite::onStart() {
SceneItemComponent::onStart();
assertNotNull(this->host);
assertNotNull(this->tileset);
QuadMesh::initQuadMesh(&this->host->mesh,
this->xy0, this->getUV0(),
this->xy1, this->getUV1(),
0
);
useEffect([&]{
if(this->meshHost == nullptr || this->tileset == nullptr) return;
auto tile = this->tileset->getTile(this->tile);
this->meshHost->uv0 = tile.uv0;
this->meshHost->uv1 = tile.uv1;
}, {
&this->tile,
&this->meshHost,
&this->tileset
})();
}

View File

@ -4,85 +4,21 @@
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "scene/components/display/mesh/MeshRenderer.hpp"
#include "scene/components/display/mesh/MeshHost.hpp"
#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)
#include "scene/components/display/mesh/QuadMeshHost.hpp"
namespace Dawn {
class TiledSprite : public SceneItemComponent {
protected:
MeshHost *host = nullptr;
Tileset *tileset = nullptr;
flag_t flipState = TILED_SPRITE_FLIP_Y;
int32_t tileIndex = -1;
glm::vec2 xy0 = glm::vec2(0, 0);
glm::vec2 xy1 = glm::vec2(1, 1);
glm::vec2 getUV0();
glm::vec2 getUV1();
public:
// @optional
StateProperty<Tileset*> tileset;
// @optional
StateProperty<QuadMeshHost*> meshHost;
// @optional
StateProperty<int32_t> tile;
TiledSprite(SceneItem *item);
std::vector<SceneItemComponent*> getDependencies() override;
std::vector<SceneItemComponent*> getDependencies();
void onStart() override;
/**
* Sets which tileset to use for this sprite.
*
* @param tileset Tileset to use.
*/
void setTileset(Tileset *tileset);
/**
* Sets the tileset for the sprite, and autosizes the sprite based on
* this tileset.
*
* @param gridTileset Tileset to use.
* @param center The center offset of the sprite.
*/
void setTilesetAndSize(TilesetGrid *gridTileset, glm::vec2 center);
/**
* Sets the tileset for the sprite, and autosizes the sprite based on
* this tileset. This will put the sprite centered on its origin.
*
* @param gridTileset Tileset to use.
*/
void setTilesetAndSize(TilesetGrid *gridTileset);
/**
* Updates the selected tile.
*
* @param tile Tile to use.
*/
void setTile(int32_t tile);
/**
* Adjust how the sprite is flippxed.
*
* @param flippedState Flipped axis flags.
*/
void setFlippedState(flag_t flippedState);
/**
* Sets the dimensions of this tiled sprite.
*
* @param size Size of the sprite.
* @param center Negative center offset.
*/
void setSize(glm::vec2 size, glm::vec2 center);
/**
* Sets the size of this sprite. This will center the sprite on its origin
*
* @param size Size of the sprite.
*/
void setSize(glm::vec2 size);
};
}