add blockSprite

This commit is contained in:
lllzebralll 2022-08-11 20:22:20 +03:00
parent 420ae4e993
commit 0711cb07f0
5 changed files with 82 additions and 5 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 257 KiB

After

Width:  |  Height:  |  Size: 257 KiB

View File

@ -45,6 +45,18 @@ void Batch2D::vertex(float x, float y,
buffer[index++] = b; buffer[index++] = b;
buffer[index++] = a; buffer[index++] = a;
} }
void Batch2D::vertex(vec2 point,
vec2 uvpoint,
float r, float g, float b, float a) {
buffer[index++] = point.x;
buffer[index++] = point.y;
buffer[index++] = uvpoint.x;
buffer[index++] = uvpoint.y;
buffer[index++] = r;
buffer[index++] = g;
buffer[index++] = b;
buffer[index++] = a;
}
void Batch2D::texture(Texture* new_texture){ void Batch2D::texture(Texture* new_texture){
if (_texture == new_texture) if (_texture == new_texture)
@ -81,6 +93,59 @@ void Batch2D::sprite(float x, float y, float w, float h, int atlasRes, int index
rect(x, y, w, h, u, v, scale, scale, tint.r, tint.g, tint.b, tint.a); rect(x, y, w, h, u, v, scale, scale, tint.r, tint.g, tint.b, tint.a);
} }
void Batch2D::blockSprite(float x, float y, float w, float h, int atlasRes, int index[6], vec4 tint){
float scale = 1.0f / (float)atlasRes;
float uu = (index[3] % atlasRes) * scale;
float vu = 1.0f - ((index[3] / atlasRes) * scale) - scale;
float uf = (index[0] % atlasRes) * scale;
float vf = 1.0f - ((index[0] / atlasRes) * scale) - scale;
if (index[0] + 6*VERTEX_SIZE >= capacity)
render();
vec2 points[7] = {vec2(x+(w*0.5f), y+(h*0.5f)),
vec2(x, y+(h*0.25f)),
vec2(x+(w*0.5f), y),
vec2(x+w, y+(h*0.25f)),
vec2(x+w, y+(h*0.75f)),
vec2(x+(w*0.5f), y+h),
vec2(x, y+(h*0.75f))};
vec2 uvpoints[8] = {vec2(uu, vu),
vec2(uu+scale, vu),
vec2(uu+scale, vu+scale),
vec2(uu, vu+scale),
vec2(uf, vf),
vec2(uf+scale, vf),
vec2(uf+scale, vf+scale),
vec2(uf, vf+scale)};
vertex(points[0], uvpoints[3], tint.r, tint.g, tint.b, tint.a);
vertex(points[1], uvpoints[0], tint.r, tint.g, tint.b, tint.a);
vertex(points[2], uvpoints[1], tint.r, tint.g, tint.b, tint.a);
vertex(points[0], uvpoints[3], tint.r, tint.g, tint.b, tint.a);
vertex(points[2], uvpoints[1], tint.r, tint.g, tint.b, tint.a);
vertex(points[3], uvpoints[2], tint.r, tint.g, tint.b, tint.a);
vertex(points[0], uvpoints[7], tint.r, tint.g, tint.b, tint.a);
vertex(points[3], uvpoints[6], tint.r, tint.g, tint.b, tint.a);
vertex(points[4], uvpoints[5], tint.r, tint.g, tint.b, tint.a);
vertex(points[0], uvpoints[7], tint.r, tint.g, tint.b, tint.a);
vertex(points[4], uvpoints[5], tint.r, tint.g, tint.b, tint.a);
vertex(points[5], uvpoints[4], tint.r, tint.g, tint.b, tint.a);
vertex(points[0], uvpoints[6], tint.r, tint.g, tint.b, tint.a);
vertex(points[5], uvpoints[5], tint.r, tint.g, tint.b, tint.a);
vertex(points[6], uvpoints[4], tint.r, tint.g, tint.b, tint.a);
vertex(points[0], uvpoints[6], tint.r, tint.g, tint.b, tint.a);
vertex(points[6], uvpoints[4], tint.r, tint.g, tint.b, tint.a);
vertex(points[1], uvpoints[7], tint.r, tint.g, tint.b, tint.a);
}
void Batch2D::rect(float x, float y, float w, float h, void Batch2D::rect(float x, float y, float w, float h,
float u, float v, float tx, float ty, float u, float v, float tx, float ty,
float r, float g, float b, float a){ float r, float g, float b, float a){

View File

@ -23,6 +23,10 @@ class Batch2D {
void vertex(float x, float y, void vertex(float x, float y,
float u, float v, float u, float v,
float r, float g, float b, float a); float r, float g, float b, float a);
void vertex(vec2 point,
vec2 uvpoint,
float r, float g, float b, float a);
public: public:
Batch2D(size_t capacity); Batch2D(size_t capacity);
~Batch2D(); ~Batch2D();
@ -30,6 +34,7 @@ public:
void begin(); void begin();
void texture(Texture* texture); void texture(Texture* texture);
void sprite(float x, float y, float w, float h, int atlasRes, int index, vec4 tint); void sprite(float x, float y, float w, float h, int atlasRes, int index, vec4 tint);
void blockSprite(float x, float y, float w, float h, int atlasRes, int index[6], vec4 tint);
void rect(float x, float y, float w, float h); void rect(float x, float y, float w, float h);
void rect(float x, float y, float w, float h, void rect(float x, float y, float w, float h,
float u, float v, float tx, float ty, float u, float v, float tx, float ty,

View File

@ -143,10 +143,10 @@ void WorldGenerator::generate(voxel* voxels, int cx, int cy, int cz, int seed){
} }
if (real_y <= 2) if (real_y <= 2)
id = 11; id = 11;
if ((real_y > 55) && ((int)height + 1 == real_y) && ((unsigned short)random() > 56000)){ if ((id == 0) && (real_y > 55) && ((int)height + 1 == real_y) && ((unsigned short)random() > 56000)){
id = 12; id = 12;
} }
if ((real_y > 55) && ((int)height + 1 == real_y) && ((unsigned short)random() > 64000)){ if ((id == 0) && (real_y > 55) && ((int)height + 1 == real_y) && ((unsigned short)random() > 64000)){
id = 13; id = 13;
} }
voxels[(y * CHUNK_D + z) * CHUNK_W + x].id = id; voxels[(y * CHUNK_D + z) * CHUNK_W + x].id = id;

View File

@ -145,8 +145,15 @@ void draw_hud(World* world, Level* level, Assets* assets, bool devdata, int fps)
batch->sprite(16, 640, 64, 64, 16, 0, vec4(1.0f)); batch->sprite(16, 640, 64, 64, 16, 0, vec4(1.0f));
batch->texture(blocks); batch->texture(blocks);
int texid = Block::blocks[player->choosenBlock]->textureFaces[3]; // face-3 is top face of block Block* cblock = Block::blocks[player->choosenBlock];
batch->sprite(24, 648, 48, 48, 16, texid, vec4(1.0f)); // int texid = Block::blocks[player->choosenBlock]->textureFaces[3]; // face-3 is top face of block
cblock->textureFaces;
if (cblock->type == 1){
batch->blockSprite(24, 648, 48, 48, 16, cblock->textureFaces, vec4(1.0f));
} else if (cblock->type == 2){
batch->sprite(24, 648, 48, 48, 16, cblock->textureFaces[3], vec4(1.0f));
}
batch->render(); batch->render();
} }