Audio module, simple text rendering
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
#include "Batch2D.h"
|
||||
#include "Mesh.h"
|
||||
#include "Texture.h"
|
||||
|
||||
#include <GL/glew.h>
|
||||
|
||||
Batch2D::Batch2D(size_t capacity) : capacity(capacity),
|
||||
offset(0),
|
||||
@@ -8,11 +11,80 @@ Batch2D::Batch2D(size_t capacity) : capacity(capacity),
|
||||
2, 2, 4, 0 //null terminator
|
||||
};
|
||||
|
||||
buffer = new float[capacity];
|
||||
mesh = new Mesh(nullptr, 0, attrs);
|
||||
buffer = new float[capacity * 8];
|
||||
mesh = new Mesh(buffer, 0, attrs);
|
||||
index = 0;
|
||||
|
||||
unsigned char pixels[] = {
|
||||
255, 255, 255, 255,
|
||||
};
|
||||
blank = new Texture(pixels, 1, 1);
|
||||
_texture = nullptr;
|
||||
}
|
||||
|
||||
Batch2D::~Batch2D(){
|
||||
delete blank;
|
||||
delete buffer;
|
||||
delete mesh;
|
||||
}
|
||||
|
||||
void Batch2D::begin(){
|
||||
_texture = nullptr;
|
||||
blank->bind();
|
||||
}
|
||||
|
||||
void Batch2D::vertex(float x, float y,
|
||||
float u, float v,
|
||||
float r, float g, float b, float a) {
|
||||
buffer[index++] = x;
|
||||
buffer[index++] = y;
|
||||
buffer[index++] = u;
|
||||
buffer[index++] = v;
|
||||
buffer[index++] = r;
|
||||
buffer[index++] = g;
|
||||
buffer[index++] = b;
|
||||
buffer[index++] = a;
|
||||
}
|
||||
|
||||
void Batch2D::texture(Texture* new_texture){
|
||||
if (_texture == new_texture)
|
||||
return;
|
||||
_texture = new_texture;
|
||||
if (new_texture == nullptr)
|
||||
blank->bind();
|
||||
else
|
||||
new_texture->bind();
|
||||
}
|
||||
|
||||
void Batch2D::rect(float x, float y, float w, float h){
|
||||
const float r = color.r;
|
||||
const float g = color.g;
|
||||
const float b = color.b;
|
||||
const float a = color.a;
|
||||
|
||||
vertex(x, y, 0, 0, r,g,b,a);
|
||||
vertex(x+w, y+h, 1, 1, r,g,b,a);
|
||||
vertex(x, y+h, 0, 1, r,g,b,a);
|
||||
|
||||
vertex(x, y, 0, 0, r,g,b,a);
|
||||
vertex(x+w, y, 1, 0, r,g,b,a);
|
||||
vertex(x+w, y+h, 1, 1, r,g,b,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){
|
||||
vertex(x, y, u, v, r,g,b,a);
|
||||
vertex(x+w, y+h, u+tx, v+ty, r,g,b,a);
|
||||
vertex(x, y+h, u, v+ty, r,g,b,a);
|
||||
|
||||
vertex(x, y, u, v, r,g,b,a);
|
||||
vertex(x+w, y, u+tx, v, r,g,b,a);
|
||||
vertex(x+w, y+h, u+tx, v+ty, r,g,b,a);
|
||||
}
|
||||
|
||||
void Batch2D::render() {
|
||||
mesh->reload(buffer, index / 8);
|
||||
mesh->draw(GL_TRIANGLES);
|
||||
index = 0;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Mesh;
|
||||
class Texture;
|
||||
|
||||
class Batch2D {
|
||||
float* buffer;
|
||||
@@ -12,6 +13,10 @@ class Batch2D {
|
||||
size_t offset;
|
||||
glm::vec4 color;
|
||||
Mesh* mesh;
|
||||
size_t index;
|
||||
|
||||
Texture* blank;
|
||||
Texture* _texture;
|
||||
|
||||
void vertex(float x, float y,
|
||||
float u, float v,
|
||||
@@ -20,7 +25,12 @@ public:
|
||||
Batch2D(size_t capacity);
|
||||
~Batch2D();
|
||||
|
||||
void begin();
|
||||
void texture(Texture* texture);
|
||||
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 r, float g, float b, float a);
|
||||
void render();
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "Font.h"
|
||||
#include "Texture.h"
|
||||
#include "Batch2D.h"
|
||||
|
||||
Font::Font(Texture* texture) : texture(texture) {
|
||||
}
|
||||
|
||||
Font::~Font(){
|
||||
delete texture;
|
||||
}
|
||||
|
||||
|
||||
void Font::draw(Batch2D* batch, std::string text, int x, int y) {
|
||||
for (char c : text){
|
||||
float u = (c % 16) / 16.0f;
|
||||
float v = 1.0f - ((c / 16) / 16.0f) - 1.0f/16.0f;
|
||||
batch->rect(x, y, 8, 8, u, v, 1.0f/16.0f, 1.0f/16.0f, 1,1,1,1);
|
||||
|
||||
int gw = 7;
|
||||
switch (c){
|
||||
case 'l':
|
||||
case 'i':
|
||||
case 'j':
|
||||
case '|':
|
||||
case '.':
|
||||
case ',':
|
||||
case ':':
|
||||
case ';':
|
||||
gw = 3;
|
||||
break;
|
||||
case 't':
|
||||
gw = 5;
|
||||
break;
|
||||
case ' ':
|
||||
gw = 3;
|
||||
break;
|
||||
}
|
||||
x += gw;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef GRAPHICS_FONT_H_
|
||||
#define GRAPHICS_FONT_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
class Texture;
|
||||
class Batch2D;
|
||||
|
||||
class Font {
|
||||
public:
|
||||
Texture* texture;
|
||||
Font(Texture* texture);
|
||||
~Font();
|
||||
|
||||
void draw(Batch2D* batch, std::string text, int x, int y);
|
||||
};
|
||||
|
||||
#endif /* GRAPHICS_FONT_H_ */
|
||||
Reference in New Issue
Block a user