draw some test text in Window for now

This commit is contained in:
Josh Holtrop 2016-07-10 16:26:37 -04:00
parent d2f39cbddc
commit 6cf59fa7d7
2 changed files with 46 additions and 4 deletions

View File

@ -1,8 +1,10 @@
#include "gl3w.h" #include "gl3w.h"
#include "Window.h" #include "Window.h"
#include "Runtime.h"
#define INITIAL_WIDTH 500 #define INITIAL_WIDTH 500
#define INITIAL_HEIGHT 500 #define INITIAL_HEIGHT 500
#define FONT_SIZE 16
/** /**
* Initialize SDL. * Initialize SDL.
@ -92,6 +94,23 @@ bool Window::create()
m_exit_requested = false; m_exit_requested = false;
/* TODO: user configurable font, size */
std::string font_path = Runtime::find(Runtime::FONT, "DejaVuSansMono");
if (font_path == "")
{
return false;
}
if (!m_font.load(font_path.c_str(), FONT_SIZE))
{
return false;
}
m_programs.text = std::make_shared<TextProgram>();
m_programs.text->use();
m_programs.text->set_texture(0);
m_programs.text->set_color(1.0, 0.9, 0.8, 1.0);
resize(); resize();
return true; return true;
@ -149,10 +168,10 @@ void Window::handle_event(SDL_Event & event)
void Window::resize() void Window::resize()
{ {
int width; SDL_GetWindowSize(m_window, &m_width, &m_height);
int height; glViewport(0, 0, m_width, m_height);
SDL_GetWindowSize(m_window, &width, &height); m_programs.text->use();
glViewport(0, 0, width, height); m_programs.text->set_viewport_size(m_width, m_height);
} }
void Window::redraw() void Window::redraw()
@ -160,5 +179,18 @@ void Window::redraw()
glClearColor (0.0, 0.0, 0.0, 0.0); glClearColor (0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT); 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++)
{
m_programs.text->set_position(x, y);
auto g = m_font.get_glyph(msg[i]);
g->render();
x += advance;
}
SDL_GL_SwapWindow(m_window); SDL_GL_SwapWindow(m_window);
} }

View File

@ -2,6 +2,8 @@
#define WINDOW_H #define WINDOW_H
#include <SDL.h> #include <SDL.h>
#include "TextProgram.h"
#include "Font.h"
class Window class Window
{ {
@ -16,6 +18,14 @@ protected:
SDL_Window * m_window; SDL_Window * m_window;
bool m_exit_requested; bool m_exit_requested;
int m_width;
int m_height;
struct
{
std::shared_ptr<TextProgram> text;
} m_programs;
Font m_font;
}; };
#endif #endif