Advertisement
SrinjoySS01

Foo

Nov 13th, 2021
1,029
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <GL/glew.h>
  3. #include <GLFW/glfw3.h>
  4. #define STB_TRUETYPE_IMPLEMENTATION
  5. #include <stb_truetype.h>
  6.  
  7. unsigned char ttf_buffer[1 << 20];
  8. unsigned char temp_bitmap[512 * 512];
  9.  
  10. stbtt_bakedchar cdata[96]; // ASCII 32..126 is 95 glyphs
  11. GLuint ftex;
  12.  
  13. void my_stbtt_initfont() {
  14.     fread(ttf_buffer, 1, 1 << 20, fopen("/System/Library/Fonts/Supplemental/Arial Unicode.ttf", "rb"));
  15.     stbtt_BakeFontBitmap(ttf_buffer,0, 32.0, temp_bitmap,512,512, 32,96, cdata); // no guarantee this fits!
  16.     // can free ttf_buffer at this point
  17.     glGenTextures(1, &ftex);
  18.     glBindTexture(GL_TEXTURE_2D, ftex);
  19.     glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 512,512, 0, GL_ALPHA, GL_UNSIGNED_BYTE, temp_bitmap);
  20.     // can free temp_bitmap at this point
  21.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  22.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  23.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  24.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  25. }
  26.  
  27. void my_stbtt_print(float x, float y, char *text) {
  28.     // assume orthographic projection with units = screen pixels, origin at top left
  29.     glEnable(GL_TEXTURE_2D);
  30.     glBindTexture(GL_TEXTURE_2D, ftex);
  31.     glBegin(GL_TRIANGLE_FAN);
  32.     while (*text) {
  33.         if (*text >= 32 && *text < 127) {
  34.             stbtt_aligned_quad q;
  35.             stbtt_GetBakedQuad(cdata, 512,512, *text - 32, &x, &y, &q,1);//1=opengl & d3d10+,0=d3d9
  36.             glTexCoord2f(q.s0,q.t1); glVertex2f(q.x0,q.y0);
  37.             glTexCoord2f(q.s1,q.t1); glVertex2f(q.x1,q.y0);
  38.             glTexCoord2f(q.s1,q.t0); glVertex2f(q.x1,q.y1);
  39.             glTexCoord2f(q.s0,q.t0); glVertex2f(q.x0,q.y1);
  40.             glTexCoord2f(q.s0,q.t1); glVertex2f(q.x0,q.y0);
  41.         }
  42.         ++text;
  43.     }
  44.     glEnd();
  45. }
  46.  
  47. void drawQuad(float const& x, float const& y, float const& w, float const& h) {
  48.     glBegin(GL_TRIANGLE_FAN); {
  49.         glVertex2f(x, y);
  50.         glVertex2f(x + w, y);
  51.         glVertex2f(x + w, y + h);
  52.         glVertex2f(x, y + h);
  53.         glVertex2f(x, y);
  54.     } glEnd();
  55. }
  56.  
  57. int main() {
  58.     if (!glfwInit()) {
  59.         std::cerr << "unable to init glfw" << std::endl;
  60.         return 1;
  61.     }
  62.     glfwSetErrorCallback([](int e, char const* msg) {
  63.         std::cerr << "[ERROR] (" << e << "): " << msg << std::endl;
  64.     });
  65.     glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
  66.     auto window = glfwCreateWindow(400, 400, "Foo", nullptr, nullptr);
  67.     glfwMakeContextCurrent(window);
  68.     if (glewInit() != GLEW_OK) {
  69.         std::cerr << "unable to init glew" << std::endl;
  70.         glfwTerminate();
  71.         return 1;
  72.     }
  73.     glfwSwapInterval(1);
  74.     glViewport(0, 0, 400, 400);
  75.     glEnable(GL_BLEND);
  76.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  77.     my_stbtt_initfont();
  78.     while (!glfwWindowShouldClose(window)) {
  79.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  80.         glMatrixMode(GL_PROJECTION);
  81.         glLoadIdentity();
  82.         glOrtho(0.0f, 400, 0, 400, 0.0f, 1.0f);
  83.         glColor3f(1, 0, 0);
  84.         drawQuad(300, 300, 100, 100);
  85.         my_stbtt_print(100, 100, (char*) "Foo Bar Foo Bar");
  86.         glfwPollEvents();
  87.         glfwSwapBuffers(window);
  88.     }
  89.     glfwTerminate();
  90.     return 0;
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement