Skip to content

Commit

Permalink
GRAPHICS: Implement render, renderBind and renderUnbind for ABCFont
Browse files Browse the repository at this point in the history
  • Loading branch information
mirv-sillyfish authored and DrMcCoy committed Nov 17, 2018
1 parent c6a693d commit 00f66c4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/graphics/aurora/abcfont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
#include "src/graphics/aurora/texture.h"
#include "src/graphics/aurora/abcfont.h"

#include "src/graphics/mesh/meshman.h"
#include "src/graphics/shader/surfaceman.h"

namespace Graphics {

namespace Aurora {
Expand All @@ -41,9 +44,18 @@ ABCFont::ABCFont(const Common::UString &name) : _base(0) {
_texture = TextureMan.get(name);

load(name);

_mesh = static_cast<Mesh::MeshFont *>(MeshMan.getMesh("defaultMeshFont"));
_material = new Shader::ShaderMaterial(ShaderMan.getShaderObject("default/text.frag", Shader::SHADER_FRAGMENT), "text");
Shader::ShaderSampler *sampler;
sampler = (Shader::ShaderSampler *)(_material->getVariableData("sampler_0_id"));
sampler->handle = _texture;
_renderable = new Shader::ShaderRenderable(SurfaceMan.getSurface("textSurface"), _material, _mesh);
}

ABCFont::~ABCFont() {
delete _renderable;
delete _material;
}

float ABCFont::getHeight() const {
Expand Down Expand Up @@ -73,6 +85,47 @@ void ABCFont::draw(uint32 c) const {
glTranslatef(cC.width + cC.spaceR, 0.0f, 0.0f);
}

void ABCFont::renderBind(const glm::mat4 &transform) const {
glUseProgram(_renderable->getProgram()->glid);
_material->bindProgram(_renderable->getProgram(), 1.0f);
_material->bindGLState();
_renderable->getSurface()->bindProgram(_renderable->getProgram(), &transform);
_renderable->getSurface()->bindGLState();
_mesh->renderBind();
}

void ABCFont::render(uint32 c, float &x, float &y, float *rgba) const {
const Char &cC = findChar(c);

x += cC.spaceL;

float v_pos[12];
float v_uv[8];
float v_rgba[4*4];

for (int i = 0; i < 4; ++i) {
v_uv[i*2] = cC.tX[i];
v_uv[i*2 +1] = cC.tY[i];
v_pos[i*3] = x + cC.vX[i];
v_pos[i*3 +1] = y + cC.vY[i];
v_pos[i*3 +2] = 0.0f;
v_rgba[i*4] = rgba[0];
v_rgba[i*4 +1] = rgba[1];
v_rgba[i*4 +2] = rgba[2];
v_rgba[i*4 +3] = rgba[3];
}
_mesh->render(v_pos, v_uv, v_rgba);
x += cC.width + cC.spaceR;
}

void ABCFont::renderUnbind() const {
_mesh->renderUnbind();

_renderable->getSurface()->unbindGLState();
_material->unbindGLState();
glUseProgram(0);
}

void ABCFont::load(const Common::UString &name) {
Common::ScopedPtr<Common::SeekableReadStream> abc(ResMan.getResource(name, ::Aurora::kFileTypeABC));
if (!abc)
Expand Down
16 changes: 16 additions & 0 deletions src/graphics/aurora/abcfont.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@

#include "src/graphics/aurora/texturehandle.h"

#include "src/graphics/mesh/meshfont.h"

#include "src/graphics/shader/shaderrenderable.h"

namespace Common {
class UString;
class SeekableReadStream;
Expand All @@ -53,6 +57,15 @@ class ABCFont : public Graphics::Font {

void draw(uint32 c) const;

/**
* @brief Bind the font for rendering. Must be performed before render is called.
* @param transform Base modelview transform. Under most circumstances this is expected to be the identity matrix.
*/
virtual void renderBind(const glm::mat4 &transform) const;

virtual void render(uint32 c, float &x, float &y, float *rgba) const;
virtual void renderUnbind() const;

private:
/** A font character. */
struct Char {
Expand All @@ -67,6 +80,9 @@ class ABCFont : public Graphics::Font {
};

TextureHandle _texture;
Mesh::MeshFont *_mesh;
Shader::ShaderMaterial *_material;
Shader::ShaderRenderable *_renderable;

uint8 _base;

Expand Down

0 comments on commit 00f66c4

Please sign in to comment.