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

View File

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

View File

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