citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit 5e284f4b138ff09ec9ceba6c2571341130f9bdf5
parent 2a76b6943e874aea6c0ba03d89471f58cb44ba74
Author: kyle <kyle@getaddrinfo.net>
Date:   Tue, 27 Oct 2015 20:16:38 -0600

viewer: initial commit

Diffstat:
Aviewer/.viewer.cpp.swp | 0
Aviewer/DejaVuSansMono.ttf | 0
Aviewer/Makefile | 4++++
Aviewer/vert.glsl | 9+++++++++
Aviewer/viewer.cpp | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 112 insertions(+), 0 deletions(-)

diff --git a/viewer/.viewer.cpp.swp b/viewer/.viewer.cpp.swp Binary files differ. diff --git a/viewer/DejaVuSansMono.ttf b/viewer/DejaVuSansMono.ttf Binary files differ. diff --git a/viewer/Makefile b/viewer/Makefile @@ -0,0 +1,4 @@ +CXXFLAGS += -I/usr/X11R6/include -I/usr/X11R6/include/freetype2 +CXXFLAGS += -I/usr/X11R6/include/libdrm + +viewer: viewer.o diff --git a/viewer/vert.glsl b/viewer/vert.glsl @@ -0,0 +1,9 @@ +#version 120 + +attribute vec4 coord; +varying vec2 texcoord; + +void main(void) { + gl_Position = vec4(coord.xy, 0, 1); + texcoord = coord.zw; +} diff --git a/viewer/viewer.cpp b/viewer/viewer.cpp @@ -0,0 +1,99 @@ +#include <ft2build.h> +#include FT_FREETYPE_H +#include <GL/gl.h> +#include <GL/glext.h> +#include <GLES3/gl31.h> + +#include <err.h> + +class Text { +public: + Text(); + int draw_source_file(const char **lines); + +private: + FT_Library ft; + FT_Face face; + FT_GlyphSlot g; + + void render_text(const char *, float x, float y, float sx, float sy); +}; + +int +main(void) +{ + GLuint tex; + glActiveTexture(GL_TEXTURE0); + glGenTextures(1, &tex); + glBindTexture(GL_TEXTURE_2D, tex); + glUniform1i(uniform_tex, 0); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + + GLuint vbo; + glGenBuffers(1, &vbo); + glEnableVertexAttribArray(attribute_coord); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glVertexAttribPointer(attribute_coord, 4, GL_FLOAT, GL_FALSE, 0, 0); + + Text text; + text.draw_source_file(NULL); +} + +Text::Text() +{ + if (FT_Init_FreeType(&ft)) + err(1, "Could not init freetype library\n"); + + if (FT_New_Face(ft, "DejaVuSans.ttf", 0, &face)) + err(1, "Could not open font\n"); + + FT_Set_Pixel_Sizes(face, 0, 48); + g = face->glyph; +} + +void +Text::render_text(const char *text, float x, float y, float sx, float sy) +{ + const char *p; + + for (p = text; *p; p++) { + if (FT_Load_Char(face, *p, FT_LOAD_RENDER)) + continue; + + glTexImage2D(GL_TEXTURE_2D, + 0, + GL_RED, + g->bitmap.width, + g->bitmap.rows, + 0, + GL_RED, + GL_UNSIGNED_BYTE, + g->bitmap.buffer + ); + + float x2 = x + g->bitmap_left * sx; + float y2 = -y - g->bitmap_top * sy; + float w = g->bitmap.width * sx; + float h = g->bitmap.rows * sy; + + GLfloat box[4][4] = { + {x2, -y2, 0, 0}, + {x2 + w, -y2, 1, 0}, + {x2, -y2 - h, 0, 1}, + {x2 + w, -y2 - h, 1, 1}, + }; + + glBufferData(GL_ARRAY_BUFFER, sizeof box, box, GL_DYNAMIC_DRAW); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + x += (g->advance.x >> 6) * sx; + y += (g->advance.y >> 6) * sy; + } +}