Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #define STB_TRUETYPE_IMPLEMENTATION
- #include <stb_truetype.h>
- unsigned char ttf_buffer[1 << 20];
- unsigned char temp_bitmap[512 * 512];
- stbtt_bakedchar cdata[96]; // ASCII 32..126 is 95 glyphs
- GLuint ftex;
- void my_stbtt_initfont() {
- fread(ttf_buffer, 1, 1 << 20, fopen("/System/Library/Fonts/Supplemental/Arial Unicode.ttf", "rb"));
- stbtt_BakeFontBitmap(ttf_buffer,0, 32.0, temp_bitmap,512,512, 32,96, cdata); // no guarantee this fits!
- // can free ttf_buffer at this point
- glGenTextures(1, &ftex);
- glBindTexture(GL_TEXTURE_2D, ftex);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 512,512, 0, GL_ALPHA, GL_UNSIGNED_BYTE, temp_bitmap);
- // can free temp_bitmap at this point
- 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);
- }
- void my_stbtt_print(float x, float y, char *text) {
- // assume orthographic projection with units = screen pixels, origin at top left
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, ftex);
- glBegin(GL_TRIANGLE_FAN);
- while (*text) {
- if (*text >= 32 && *text < 127) {
- stbtt_aligned_quad q;
- stbtt_GetBakedQuad(cdata, 512,512, *text - 32, &x, &y, &q,1);//1=opengl & d3d10+,0=d3d9
- glTexCoord2f(q.s0,q.t1); glVertex2f(q.x0,q.y0);
- glTexCoord2f(q.s1,q.t1); glVertex2f(q.x1,q.y0);
- glTexCoord2f(q.s1,q.t0); glVertex2f(q.x1,q.y1);
- glTexCoord2f(q.s0,q.t0); glVertex2f(q.x0,q.y1);
- glTexCoord2f(q.s0,q.t1); glVertex2f(q.x0,q.y0);
- }
- ++text;
- }
- glEnd();
- }
- void drawQuad(float const& x, float const& y, float const& w, float const& h) {
- glBegin(GL_TRIANGLE_FAN); {
- glVertex2f(x, y);
- glVertex2f(x + w, y);
- glVertex2f(x + w, y + h);
- glVertex2f(x, y + h);
- glVertex2f(x, y);
- } glEnd();
- }
- int main() {
- if (!glfwInit()) {
- std::cerr << "unable to init glfw" << std::endl;
- return 1;
- }
- glfwSetErrorCallback([](int e, char const* msg) {
- std::cerr << "[ERROR] (" << e << "): " << msg << std::endl;
- });
- glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
- auto window = glfwCreateWindow(400, 400, "Foo", nullptr, nullptr);
- glfwMakeContextCurrent(window);
- if (glewInit() != GLEW_OK) {
- std::cerr << "unable to init glew" << std::endl;
- glfwTerminate();
- return 1;
- }
- glfwSwapInterval(1);
- glViewport(0, 0, 400, 400);
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- my_stbtt_initfont();
- while (!glfwWindowShouldClose(window)) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0.0f, 400, 0, 400, 0.0f, 1.0f);
- glColor3f(1, 0, 0);
- drawQuad(300, 300, 100, 100);
- my_stbtt_print(100, 100, (char*) "Foo Bar Foo Bar");
- glfwPollEvents();
- glfwSwapBuffers(window);
- }
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement