render a file's contents

This commit is contained in:
Josh Holtrop 2016-07-10 17:24:00 -04:00
parent 5da9cde758
commit d8fcbcd428
3 changed files with 32 additions and 11 deletions

View File

@ -65,7 +65,7 @@ static bool Initialize_OpenGL()
* @retval true The Window was created successfully.
* @retval false There was an error while creating the Window.
*/
bool Window::create()
bool Window::create(std::shared_ptr<Buffer> buffer)
{
if (!Initialize_SDL())
{
@ -110,6 +110,8 @@ bool Window::create()
m_programs.text->set_texture(0);
m_programs.text->set_color(1.0, 1.0, 1.0, 1.0);
m_buffer = buffer;
resize();
return true;
@ -179,16 +181,26 @@ void Window::redraw()
glClear(GL_COLOR_BUFFER_BIT);
m_programs.text->use();
static const char msg[] = "Welcome to " APPNAME " pre-pre-pre-alpha!!";
int advance = m_font.get_advance();
int x = 0;
int y = m_height - FONT_SIZE;
for (int i = 0, len = strlen(msg); i < len; i++)
int line_height = m_font.get_line_height();
int y = m_height - line_height;
for (auto line = m_buffer->cbegin(); line != m_buffer->cend(); line++)
{
m_programs.text->set_position(x, y);
auto g = m_font.get_glyph(msg[i]);
g->render();
x += advance;
int x = 0;
for (int i = 0, len = (*line)->size(); i < len; i++)
{
uint8_t c = (**line)[i];
auto g = m_font.get_glyph(c);
m_programs.text->set_position(x, y);
g->render();
x += advance;
}
y -= line_height;
if (y + line_height < 0)
{
break;
}
}
SDL_GL_SwapWindow(m_window);

View File

@ -4,11 +4,12 @@
#include <SDL.h>
#include "TextProgram.h"
#include "Font.h"
#include "Buffer.h"
class Window
{
public:
bool create();
bool create(std::shared_ptr<Buffer> buffer);
void run_event_loop();
protected:
@ -26,6 +27,8 @@ protected:
} m_programs;
Font m_font;
std::shared_ptr<Buffer> m_buffer;
};
#endif

View File

@ -1,13 +1,19 @@
#include <iostream>
#include "Window.h"
#include "Runtime.h"
#include <memory>
int main(int argc, char * argv[])
{
Runtime::init(argv[0], APPNAME);
std::shared_ptr<Buffer> b = std::make_shared<Buffer>();
if (argc > 1)
{
b->load_from_file(argv[1]);
}
Window w;
if (!w.create())
if (!w.create(b))
{
std::cerr << "Error creating window." << std::endl;
return 1;