#include <iostream>
#include <fstream>
#include <sstream>
#include "initialization.hpp"
class ExampleText:public Initialization
{
public:
ExampleText(DemoRunner *runner, int argc, char **argv);
~ExampleText();
virtual
void
draw_frame(void) override;
virtual
void
handle_event(const SDL_Event &ev) override;
private:
float m_scale;
};
ExampleText::
ExampleText(DemoRunner *runner, int argc, char **argv):
Initialization(runner, argc, argv),
m_glyph_sequence(32.0f,
m_painter_engine_gl->glyph_cache()),
m_translate(0.0f, 0.0f),
m_scale(1.0f)
{
{
std::cerr << "Unable to extract scalable font from \""
<< argv[1] << "\"\n";
end_demo(-1);
return;
}
std::istream *text_src(nullptr);
std::istringstream default_text("Hello World");
std::ifstream text_file;
if (argc >= 3)
{
text_file.open(argv[2]);
if (text_file)
{
text_src = &text_file;
}
}
if (!text_src)
{
text_src = &default_text;
}
float ratio(m_glyph_sequence.format_size() / m_font->metrics().units_per_EM());
char ch;
pen.y() += ratio * m_font->metrics().height();
while (text_src->get(ch))
{
if (ch == '\n')
{
pen.y() += ratio * m_font->metrics().height();
pen.x() = 0.0f;
}
else
{
uint32_t glyph_code;
glyph_metrics = m_glyph_sequence.glyph_cache().fetch_glyph_metrics(m_font.get(), glyph_code);
if (glyph_metrics.
valid())
{
pen.x() += ratio * glyph_metrics.
advance().
x();
}
}
}
std::cout << "Arrow keys to scroll, PageUp/PageDown to change zoom level\n";
}
void
ExampleText::
draw_frame(void)
{
m_surface_gl->viewport(vwp);
m_painter->translate(m_translate);
m_painter->scale(m_scale);
brush.
color(1.0f, 1.0f, 1.0f, 1.0f);
m_painter->draw_glyphs(brush, m_glyph_sequence);
m_painter->end();
fastuidraw_glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
fastuidraw_glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
m_surface_gl->blit_surface(GL_NEAREST);
}
void
ExampleText::
handle_event(const SDL_Event &ev)
{
switch (ev.type)
{
case SDL_KEYDOWN:
switch (ev.key.keysym.sym)
{
case SDLK_UP:
m_translate.y() += 16.0f;
break;
case SDLK_DOWN:
m_translate.y() -= 16.0f;
break;
case SDLK_LEFT:
m_translate.x() += 16.0f;
break;
case SDLK_RIGHT:
m_translate.x() -= 16.0f;
break;
case SDLK_PAGEUP:
m_scale += 0.2f;
break;
case SDLK_PAGEDOWN:
m_scale -= 0.2f;
break;
case SDLK_SPACE:
m_scale = 1.0f;
m_translate.x() = 0.0f;
m_translate.y() = 0.0f;
break;
}
break;
}
Initialization::handle_event(ev);
}
ExampleText::
~ExampleText()
{
}
int
main(int argc, char **argv)
{
if (argc < 2)
{
std::cout << "Usage: " << argv[0] << " font_file [text_file]\n";
return -1;
}
DemoRunner demo;
return demo.main<ExampleText>(argc, argv);
}