From 0711cb07f037417505a4e7338f315694b21cd492 Mon Sep 17 00:00:00 2001 From: lllzebralll Date: Thu, 11 Aug 2022 20:22:20 +0300 Subject: [PATCH] add blockSprite --- res/sprite.png | Bin 262913 -> 262913 bytes src/graphics/Batch2D.cpp | 65 ++++++++++++++++++++++++++++++++++ src/graphics/Batch2D.h | 7 +++- src/voxels/WorldGenerator.cpp | 4 +-- src/world_render.h | 11 ++++-- 5 files changed, 82 insertions(+), 5 deletions(-) diff --git a/res/sprite.png b/res/sprite.png index f9e20e0dfd52089d846fa19d75b1a4185845f57f..65c5d3adbfdf6dd479164e96dad5df01f2f06793 100644 GIT binary patch delta 147 zcmZo{6KHG`*bulB#}kN3)+pyPpFi5HoG}b6}o-NTjz}e4Bgp^ff@s89ZJ6 KT-G@yGywoSPAoG3 diff --git a/src/graphics/Batch2D.cpp b/src/graphics/Batch2D.cpp index a96aa4fd..4cde0087 100644 --- a/src/graphics/Batch2D.cpp +++ b/src/graphics/Batch2D.cpp @@ -45,6 +45,18 @@ void Batch2D::vertex(float x, float y, buffer[index++] = b; 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){ 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); } +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, float u, float v, float tx, float ty, float r, float g, float b, float a){ diff --git a/src/graphics/Batch2D.h b/src/graphics/Batch2D.h index 21fe3b6d..814305f9 100644 --- a/src/graphics/Batch2D.h +++ b/src/graphics/Batch2D.h @@ -18,11 +18,15 @@ class Batch2D { size_t index; Texture* blank; - Texture* _texture; + Texture* _texture; void vertex(float x, float y, float u, float v, float r, float g, float b, float a); + void vertex(vec2 point, + vec2 uvpoint, + float r, float g, float b, float a); + public: Batch2D(size_t capacity); ~Batch2D(); @@ -30,6 +34,7 @@ public: void begin(); void texture(Texture* texture); 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, float u, float v, float tx, float ty, diff --git a/src/voxels/WorldGenerator.cpp b/src/voxels/WorldGenerator.cpp index 46d6e3dc..54e80f83 100644 --- a/src/voxels/WorldGenerator.cpp +++ b/src/voxels/WorldGenerator.cpp @@ -143,10 +143,10 @@ void WorldGenerator::generate(voxel* voxels, int cx, int cy, int cz, int seed){ } if (real_y <= 2) 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; } - 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; } voxels[(y * CHUNK_D + z) * CHUNK_W + x].id = id; diff --git a/src/world_render.h b/src/world_render.h index a022a531..b5c7e380 100644 --- a/src/world_render.h +++ b/src/world_render.h @@ -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->texture(blocks); - int texid = Block::blocks[player->choosenBlock]->textureFaces[3]; // face-3 is top face of block - batch->sprite(24, 648, 48, 48, 16, texid, vec4(1.0f)); + Block* cblock = Block::blocks[player->choosenBlock]; + // 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(); }