Added 3 unicode pages for font, fixed UI, added Block.breakable

Added 1, 2, 3 and 4 unicode pages from Minecraft.
Font now uses std::wstring instead of std::string
Added Block.breakable field (the only unbreakable block is bedrock)
Bedrock is now selectable
This commit is contained in:
MihailRis
2022-07-14 19:23:31 +03:00
committed by GitHub
parent b2872650d6
commit bdd3fbf7d4
17 changed files with 94 additions and 47 deletions
+9 -2
View File
@@ -4,6 +4,8 @@
#include <GL/glew.h>
#define VERTEX_SIZE 8
Batch2D::Batch2D(size_t capacity) : capacity(capacity),
offset(0),
color(1.0f, 1.0f, 1.0f, 1.0f){
@@ -11,7 +13,7 @@ Batch2D::Batch2D(size_t capacity) : capacity(capacity),
2, 2, 4, 0 //null terminator
};
buffer = new float[capacity * 8];
buffer = new float[capacity * VERTEX_SIZE];
mesh = new Mesh(buffer, 0, attrs);
index = 0;
@@ -49,6 +51,7 @@ void Batch2D::vertex(float x, float y,
void Batch2D::texture(Texture* new_texture){
if (_texture == new_texture)
return;
render();
_texture = new_texture;
if (new_texture == nullptr)
blank->bind();
@@ -61,6 +64,8 @@ void Batch2D::rect(float x, float y, float w, float h){
const float g = color.g;
const float b = color.b;
const float a = color.a;
if (index + 6*VERTEX_SIZE >= capacity)
render();
vertex(x, y, 0, 0, r,g,b,a);
vertex(x+w, y+h, 1, 1, r,g,b,a);
@@ -81,6 +86,8 @@ void Batch2D::sprite(float x, float y, float w, float h, int atlasRes, int index
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){
if (index + 6*VERTEX_SIZE >= capacity)
render();
vertex(x, y, u, v+ty, r,g,b,a);
vertex(x+w, y+h, u+tx, v, r,g,b,a);
vertex(x, y+h, u, v, r,g,b,a);
@@ -91,7 +98,7 @@ void Batch2D::rect(float x, float y, float w, float h,
}
void Batch2D::render() {
mesh->reload(buffer, index / 8);
mesh->reload(buffer, index / VERTEX_SIZE);
mesh->draw(GL_TRIANGLES);
index = 0;
}
+29 -15
View File
@@ -2,11 +2,12 @@
#include "Texture.h"
#include "Batch2D.h"
Font::Font(Texture* texture) : texture(texture) {
Font::Font(std::vector<Texture*> pages) : pages(pages) {
}
Font::~Font(){
delete texture;
for (Texture* texture : pages)
delete texture;
}
int Font::getGlyphWidth(char c) {
@@ -26,7 +27,7 @@ int Font::getGlyphWidth(char c) {
}
bool Font::isPrintableChar(char c) {
bool Font::isPrintableChar(int c) {
switch (c){
case ' ':
case '\t':
@@ -39,20 +40,33 @@ bool Font::isPrintableChar(char c) {
}
}
#define RES 16
void Font::draw(Batch2D* batch, std::string text, int x, int y) {
for (char c : text){
if (isPrintableChar(c))
batch->sprite(x, y, 8, 8, 16, c, vec4(1.0f));
x += getGlyphWidth(c);
}
}
void Font::drawWithShadow(Batch2D* batch, std::string text, int x, int y) {
for (char c : text){
void Font::draw(Batch2D* batch, std::wstring text, int x, int y) {
for (unsigned c : text){
if (isPrintableChar(c)){
batch->sprite(x+1, y+1, 8, 8, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f));
batch->sprite(x, y, 8, 8, 16, c, vec4(1.0f));
batch->texture(pages[c >> 8]);
batch->sprite(x, y, RES, RES, 16, c, vec4(1.0f));
}
x += getGlyphWidth(c);
}
}
void Font::drawWithShadow(Batch2D* batch, std::wstring text, int x, int y) {
for (unsigned c : text){
if (isPrintableChar(c)){
batch->texture(pages[c >> 8]);
batch->sprite(x+1, y+1, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f));
batch->sprite(x+1, y-1, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f));
batch->sprite(x-1, y, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f));
batch->sprite(x+1, y, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f));
batch->sprite(x-1, y-1, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f));
batch->sprite(x+1, y-1, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f));
batch->sprite(x+1, y+1, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f));
batch->sprite(x-1, y+1, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f));
batch->sprite(x, y, RES, RES, 16, c, vec4(1.0f));
}
x += getGlyphWidth(c);
}
+6 -5
View File
@@ -2,20 +2,21 @@
#define GRAPHICS_FONT_H_
#include <string>
#include <vector>
class Texture;
class Batch2D;
class Font {
public:
Texture* texture;
Font(Texture* texture);
std::vector<Texture*> pages;
Font(std::vector<Texture*> pages);
~Font();
int getGlyphWidth(char c);
bool isPrintableChar(char c);
void draw(Batch2D* batch, std::string text, int x, int y);
void drawWithShadow(Batch2D* batch, std::string text, int x, int y);
bool isPrintableChar(int c);
void draw(Batch2D* batch, std::wstring text, int x, int y);
void drawWithShadow(Batch2D* batch, std::wstring text, int x, int y);
};
#endif /* GRAPHICS_FONT_H_ */