Guest
Public paste!

Untitled

By: a guest | Mar 16th, 2010 | Syntax: C++ | Size: 6.58 KB | Hits: 25 | Expires: Never
Copy text to clipboard
  1. // rendertext.cpp: based on Don's gl_text.cpp
  2.  
  3. #include "cube.h"
  4.  
  5. short char_coords[96][4] =
  6. {
  7.     {0,0,25,64},        //!
  8.     {25,0,54,64},       //"
  9.     {54,0,107,64},      //#
  10.     {107,0,148,64},     //$
  11.     {148,0,217,64},     //%
  12.     {217,0,263,64},     //&
  13.     {263,0,280,64},     //'
  14.     {280,0,309,64},     //(
  15.     {309,0,338,64},     //)
  16.     {338,0,379,64},     //*
  17.     {379,0,432,64},     //+
  18.     {432,0,455,64},     //,
  19.     {455,0,484,64},     //-
  20.     {0,64,21,128},      //.
  21.     {23,64,52,128},     ///
  22.     {52,64,93,128},     //0
  23.     {93,64,133,128},    //1
  24.     {133,64,174,128},   //2
  25.     {174,64,215,128},   //3
  26.     {215,64,256,128},   //4
  27.     {256,64,296,128},   //5
  28.     {296,64,337,128},   //6
  29.     {337,64,378,128},   //7
  30.     {378,64,419,128},   //8
  31.     {419,64,459,128},   //9
  32.     {459,64,488,128},   //:
  33.     {0,128,29,192},     //;
  34.     {29,128,81,192},    //<
  35.     {81,128,134,192},   //=
  36.     {134,128,186,192},  //>
  37.     {186,128,221,192},  //?
  38.     {221,128,285,192},  //@
  39.     {285,128,329,192},  //A
  40.     {329,128,373,192},  //B
  41.     {373,128,418,192},  //C
  42.     {418,128,467,192},  //D
  43.     {0,192,40,256},     //E
  44.     {40,192,77,256},    //F
  45.     {77,192,127,256},   //G
  46.     {127,192,175,256},  //H
  47.     {175,192,202,256},  //I
  48.     {202,192,231,256},  //J
  49.     {231,192,275,256},  //K
  50.     {275,192,311,256},  //L
  51.     {311,192,365,256},  //M
  52.     {365,192,413,256},  //N
  53.     {413,192,463,256},  //O
  54.     {1,256,38,320},     //P
  55.     {38,256,89,320},    //Q
  56.     {89,256,133,320},   //R
  57.     {133,256,176,320},  //S
  58.     {177,256,216,320},  //T
  59.     {217,256,263,320},  //U
  60.     {263,256,307,320},  //V
  61.     {307,256,370,320},  //W
  62.     {370,256,414,320},  //X
  63.     {414,256,453,320},  //Y
  64.     {453,256,497,320},  //Z
  65.     {0,320,29,384},     //[
  66.     {29,320,58,384},    //"\"
  67.     {59,320,87,384},    //]
  68.     {87,320,139,384},   //^
  69.     {139,320,180,384},  //_
  70.     {180,320,221,384},  //`
  71.     {221,320,259,384},  //a
  72.     {259,320,299,384},  //b
  73.     {299,320,332,384},  //c
  74.     {332,320,372,384},  //d
  75.     {372,320,411,384},  //e
  76.     {411,320,433,384},  //f
  77.     {435,320,473,384},  //g
  78.     {0,384,40,448},     //h
  79.     {40,384,56,448},    //i
  80.     {58,384,80,448},    //j
  81.     {80,384,118,448},   //k
  82.     {118,384,135,448},  //l
  83.     {135,384,197,448},  //m
  84.     {197,384,238,448},  //n
  85.     {238,384,277,448},  //o
  86.     {277,384,317,448},  //p
  87.     {317,384,356,448},  //q
  88.     {357,384,384,448},  //r
  89.     {385,384,417,448},  //s
  90.     {417,384,442,448},  //t
  91.     {443,384,483,448},  //u
  92.     {0,448,38,512},     //v
  93.     {38,448,90,512},    //w
  94.     {90,448,128,512},   //x
  95.     {128,448,166,512},  //y
  96.     {166,448,200,512},  //z
  97.     {200,448,241,512},  //{
  98.     {241,448,270,512},  //|
  99.     {270,448,310,512},  //}
  100.     {310,448,363,512},  //~
  101. };
  102.  
  103. int text_width(char *str)
  104. {
  105.     int x = 0;
  106.     for (int i = 0; str[i] != 0; i++)
  107.     {
  108.         int c = str[i];
  109.         if(c=='\t') { x = (x+PIXELTAB)/PIXELTAB*PIXELTAB; continue; };
  110.         if(c=='\f') continue;
  111.         if(c==' ') { x += FONTH/2; continue; };
  112.         c -= 33;
  113.         if(c<0 || c>=95) continue;
  114.         int in_width = char_coords[c][2] - char_coords[c][0];
  115.         x += in_width + 1;
  116.     }
  117.     return x;
  118. }
  119.  
  120.  
  121. void draw_textf(char *fstr, int left, int top, int gl_num, int arg)
  122. {
  123.     sprintf_sd(str)(fstr,arg);
  124.     draw_text(str, left, top, gl_num);
  125. };
  126.  
  127. void draw_text(char *str, int left, int top, int gl_num)
  128. {
  129.     glBlendFunc(GL_ONE, GL_ONE);
  130.     glBindTexture(GL_TEXTURE_2D, gl_num);
  131.     glColor3ub(255,255,255);
  132.  
  133.     int x = left;
  134.     int y = top;
  135.  
  136.     int i;
  137.     float in_left, in_top, in_right, in_bottom;
  138.     int in_width, in_height;
  139.  
  140.     for (i = 0; str[i] != 0; i++)
  141.     {
  142.         int c = str[i];
  143.         if(c=='\t') { x = (x-left+PIXELTAB)/PIXELTAB*PIXELTAB+left; continue; };
  144.         if(c=='\f') { glColor3ub(64,255,128); continue; };
  145.         if(c==' ') { x += FONTH/2; continue; };
  146.         c -= 33;
  147.         if(c<0 || c>=95) continue;
  148.  
  149.         in_left    = ((float) char_coords[c][0])   / 512.0f;
  150.         in_top     = ((float) char_coords[c][1]+2) / 512.0f;
  151.         in_right   = ((float) char_coords[c][2])   / 512.0f;
  152.         in_bottom  = ((float) char_coords[c][3]-2) / 512.0f;
  153.  
  154.         in_width   = char_coords[c][2] - char_coords[c][0];
  155.         in_height  = char_coords[c][3] - char_coords[c][1];
  156.  
  157.         glBegin(GL_QUADS);
  158.         glTexCoord2f(in_left,  in_top   ); glVertex2i(x,            y);
  159.         glTexCoord2f(in_right, in_top   ); glVertex2i(x + in_width, y);
  160.         glTexCoord2f(in_right, in_bottom); glVertex2i(x + in_width, y + in_height);
  161.         glTexCoord2f(in_left,  in_bottom); glVertex2i(x,            y + in_height);
  162.         glEnd();
  163.        
  164.         xtraverts += 4;
  165.         x += in_width  + 1;
  166.     }
  167. }
  168.  
  169. // also Don's code, so goes in here too :)
  170.  
  171. void draw_envbox_aux(float s0, float t0, int x0, int y0, int z0,
  172.                      float s1, float t1, int x1, int y1, int z1,
  173.                      float s2, float t2, int x2, int y2, int z2,
  174.                      float s3, float t3, int x3, int y3, int z3,
  175.                      int texture)
  176. {
  177.     glBindTexture(GL_TEXTURE_2D, texture);
  178.     glBegin(GL_QUADS);
  179.     glTexCoord2f(s3, t3); glVertex3d(x3, y3, z3);
  180.     glTexCoord2f(s2, t2); glVertex3d(x2, y2, z2);
  181.     glTexCoord2f(s1, t1); glVertex3d(x1, y1, z1);
  182.     glTexCoord2f(s0, t0); glVertex3d(x0, y0, z0);
  183.     glEnd();
  184.     xtraverts += 4;
  185. }
  186.  
  187. void draw_envbox(int t, int w)
  188. {
  189.     glDepthMask(GL_FALSE);
  190.  
  191.     draw_envbox_aux(1.0f, 1.0f, -w, -w,  w,
  192.                     0.0f, 1.0f,  w, -w,  w,
  193.                     0.0f, 0.0f,  w, -w, -w,
  194.                     1.0f, 0.0f, -w, -w, -w, t);
  195.  
  196.     draw_envbox_aux(1.0f, 1.0f, +w,  w,  w,
  197.                     0.0f, 1.0f, -w,  w,  w,
  198.                     0.0f, 0.0f, -w,  w, -w,
  199.                     1.0f, 0.0f, +w,  w, -w, t+1);
  200.  
  201.     draw_envbox_aux(0.0f, 0.0f, -w, -w, -w,
  202.                     1.0f, 0.0f, -w,  w, -w,
  203.                     1.0f, 1.0f, -w,  w,  w,
  204.                     0.0f, 1.0f, -w, -w,  w, t+2);
  205.  
  206.     draw_envbox_aux(1.0f, 1.0f, +w, -w,  w,
  207.                     0.0f, 1.0f, +w,  w,  w,
  208.                     0.0f, 0.0f, +w,  w, -w,
  209.                     1.0f, 0.0f, +w, -w, -w, t+3);
  210.  
  211.     draw_envbox_aux(0.0f, 1.0f, -w,  w,  w,
  212.                     0.0f, 0.0f, +w,  w,  w,
  213.                     1.0f, 0.0f, +w, -w,  w,
  214.                     1.0f, 1.0f, -w, -w,  w, t+4);
  215.  
  216.     draw_envbox_aux(0.0f, 1.0f, +w,  w, -w,
  217.                     0.0f, 0.0f, -w,  w, -w,
  218.                     1.0f, 0.0f, -w, -w, -w,
  219.                     1.0f, 1.0f, +w, -w, -w, t+5);
  220.  
  221.     glDepthMask(GL_TRUE);
  222. }