add <image ...> atlases support
This commit is contained in:
@@ -39,6 +39,7 @@ void Batch2D::setPrimitive(DrawPrimitive primitive) {
|
||||
void Batch2D::begin(){
|
||||
currentTexture = nullptr;
|
||||
blank->bind();
|
||||
region = blank->getUVRegion();
|
||||
color = glm::vec4(1.0f);
|
||||
primitive = DrawPrimitive::triangle;
|
||||
}
|
||||
@@ -50,8 +51,8 @@ void Batch2D::vertex(
|
||||
) {
|
||||
buffer[index++] = x;
|
||||
buffer[index++] = y;
|
||||
buffer[index++] = u;
|
||||
buffer[index++] = v;
|
||||
buffer[index++] = u * region.getWidth() + region.u1;
|
||||
buffer[index++] = v * region.getHeight() + region.v1;
|
||||
buffer[index++] = r;
|
||||
buffer[index++] = g;
|
||||
buffer[index++] = b;
|
||||
@@ -64,8 +65,8 @@ void Batch2D::vertex(
|
||||
) {
|
||||
buffer[index++] = point.x;
|
||||
buffer[index++] = point.y;
|
||||
buffer[index++] = uvpoint.x;
|
||||
buffer[index++] = uvpoint.y;
|
||||
buffer[index++] = uvpoint.x * region.getWidth() + region.u1;
|
||||
buffer[index++] = uvpoint.y * region.getHeight() + region.v1;
|
||||
buffer[index++] = r;
|
||||
buffer[index++] = g;
|
||||
buffer[index++] = b;
|
||||
@@ -80,8 +81,10 @@ void Batch2D::texture(Texture* new_texture){
|
||||
currentTexture = new_texture;
|
||||
if (new_texture == nullptr) {
|
||||
blank->bind();
|
||||
region = blank->getUVRegion();
|
||||
} else {
|
||||
new_texture->bind();
|
||||
region = currentTexture->getUVRegion();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,6 +92,10 @@ void Batch2D::untexture() {
|
||||
texture(nullptr);
|
||||
}
|
||||
|
||||
void Batch2D::setRegion(UVRegion region) {
|
||||
this->region = region;
|
||||
}
|
||||
|
||||
void Batch2D::point(float x, float y, float r, float g, float b, float a){
|
||||
if (index + 6*B2D_VERTEX_SIZE >= capacity)
|
||||
flush();
|
||||
|
||||
@@ -6,10 +6,10 @@
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "commons.hpp"
|
||||
#include "../../maths/UVRegion.hpp"
|
||||
|
||||
class Mesh;
|
||||
class Texture;
|
||||
struct UVRegion;
|
||||
|
||||
class Batch2D {
|
||||
std::unique_ptr<float[]> buffer;
|
||||
@@ -20,6 +20,7 @@ class Batch2D {
|
||||
glm::vec4 color;
|
||||
Texture* currentTexture;
|
||||
DrawPrimitive primitive = DrawPrimitive::triangle;
|
||||
UVRegion region {0.0f, 0.0f, 1.0f, 1.0f};
|
||||
|
||||
void setPrimitive(DrawPrimitive primitive);
|
||||
|
||||
@@ -42,6 +43,7 @@ public:
|
||||
void begin();
|
||||
void texture(Texture* texture);
|
||||
void untexture();
|
||||
void setRegion(UVRegion region);
|
||||
void sprite(float x, float y, float w, float h, const UVRegion& region, glm::vec4 tint);
|
||||
void sprite(float x, float y, float w, float h, int atlasRes, int index, glm::vec4 tint);
|
||||
void point(float x, float y, float r, float g, float b, float a);
|
||||
|
||||
@@ -20,6 +20,10 @@ public:
|
||||
virtual std::unique_ptr<ImageData> readData() override;
|
||||
virtual uint getId() const override;
|
||||
|
||||
virtual UVRegion getUVRegion() const override {
|
||||
return UVRegion(0.0f, 0.0f, 1.0f, 1.0f);
|
||||
}
|
||||
|
||||
static std::unique_ptr<GLTexture> from(const ImageData* image);
|
||||
};
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define GRAPHICS_CORE_TEXTURE_HPP_
|
||||
|
||||
#include "../../typedefs.hpp"
|
||||
#include "../../maths/UVRegion.hpp"
|
||||
#include "ImageData.hpp"
|
||||
|
||||
#include <memory>
|
||||
@@ -28,6 +29,7 @@ public:
|
||||
virtual uint getHeight() const {
|
||||
return height;
|
||||
}
|
||||
virtual UVRegion getUVRegion() const = 0;
|
||||
|
||||
virtual uint getId() const = 0;
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "../../core/DrawContext.hpp"
|
||||
#include "../../core/Batch2D.hpp"
|
||||
#include "../../core/Texture.hpp"
|
||||
#include "../../core/Atlas.hpp"
|
||||
#include "../../../assets/Assets.hpp"
|
||||
#include "../../../maths/UVRegion.hpp"
|
||||
|
||||
@@ -18,11 +19,28 @@ void Image::draw(const DrawContext* pctx, Assets* assets) {
|
||||
glm::vec2 pos = calcPos();
|
||||
auto batch = pctx->getBatch2D();
|
||||
|
||||
auto texture = assets->get<Texture>(this->texture);
|
||||
if (texture && autoresize) {
|
||||
setSize(glm::vec2(texture->getWidth(), texture->getHeight()));
|
||||
Texture* texture = nullptr;
|
||||
auto separator = this->texture.find(':');
|
||||
if (separator == std::string::npos) {
|
||||
texture = assets->get<Texture>(this->texture);
|
||||
batch->texture(texture);
|
||||
if (texture && autoresize) {
|
||||
setSize(glm::vec2(texture->getWidth(), texture->getHeight()));
|
||||
}
|
||||
} else {
|
||||
auto atlasName = this->texture.substr(0, separator);
|
||||
if (auto atlas = assets->get<Atlas>(atlasName)) {
|
||||
texture = atlas->getTexture();
|
||||
batch->texture(atlas->getTexture());
|
||||
auto& region = atlas->get(this->texture.substr(separator+1));
|
||||
batch->setRegion(region);
|
||||
if (autoresize) {
|
||||
setSize(glm::vec2(
|
||||
texture->getWidth()*region.getWidth(),
|
||||
texture->getHeight()*region.getHeight()));
|
||||
}
|
||||
}
|
||||
}
|
||||
batch->texture(texture);
|
||||
batch->rect(
|
||||
pos.x, pos.y, size.x, size.y,
|
||||
0, 0, 0, UVRegion(), false, true, calcColor()
|
||||
|
||||
Reference in New Issue
Block a user