text.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. Copyright (C) 2005, 2010 - Cryptic Sea
  3. This file is part of Gish.
  4. Gish is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. See the GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. void drawtext(unsigned char *textstring,int x,int y,int textsize,float red,float green,float blue,float alpha,...)
  17. {
  18. int count,count2;
  19. int variabletemp;
  20. int texturenum;
  21. va_list ap;
  22. float vec[3];
  23. float texcoord[2];
  24. count=0;
  25. count2=0;
  26. va_start(ap,alpha);
  27. while (textstring[count]!=0 && count2<1000)
  28. {
  29. if (textstring[count]=='/')
  30. {
  31. switch (textstring[count+1])
  32. {
  33. case '/':
  34. count++;
  35. textstring2[count2++]='/';
  36. break;
  37. case 'i':
  38. count++;
  39. sprintf(textstring3,"%d",(int)va_arg(ap,int));
  40. memcpy(textstring2+count2,textstring3,strlen(textstring3));
  41. count2+=strlen(textstring3);
  42. break;
  43. case 'I':
  44. count++;
  45. variabletemp=(int)va_arg(ap,int);
  46. if (variabletemp>=1000000)
  47. {
  48. sprintf(textstring3,"%d",(variabletemp/1000000));
  49. memcpy(textstring2+count2,textstring3,strlen(textstring3));
  50. count2+=strlen(textstring3);
  51. textstring2[count2++]=',';
  52. }
  53. if (variabletemp>=1000)
  54. {
  55. if (variabletemp>=1000000)
  56. sprintf(textstring3,"%.3d",((variabletemp/1000)%1000));
  57. else
  58. sprintf(textstring3,"%d",((variabletemp/1000)%1000));
  59. memcpy(textstring2+count2,textstring3,strlen(textstring3));
  60. count2+=strlen(textstring3);
  61. textstring2[count2++]=',';
  62. }
  63. if (variabletemp>=1000)
  64. sprintf(textstring3,"%.3d",(variabletemp%1000));
  65. else
  66. sprintf(textstring3,"%d",variabletemp);
  67. memcpy(textstring2+count2,textstring3,strlen(textstring3));
  68. count2+=strlen(textstring3);
  69. break;
  70. case 'u':
  71. count++;
  72. sprintf(textstring3,"%u",(unsigned int)va_arg(ap,unsigned int));
  73. memcpy(textstring2+count2,textstring3,strlen(textstring3));
  74. count2+=strlen(textstring3);
  75. break;
  76. case 'f':
  77. count++;
  78. sprintf(textstring3,"%f",(double)va_arg(ap,double));
  79. memcpy(textstring2+count2,textstring3,strlen(textstring3));
  80. count2+=strlen(textstring3);
  81. break;
  82. case 'c':
  83. count++;
  84. variabletemp=va_arg(ap,int);
  85. if (variabletemp!=0)
  86. textstring2[count2++]=variabletemp;
  87. break;
  88. case 's':
  89. count++;
  90. sprintf(textstring3,"%s",va_arg(ap,char *));
  91. memcpy(textstring2+count2,textstring3,strlen(textstring3));
  92. count2+=strlen(textstring3);
  93. break;
  94. case 'x':
  95. count++;
  96. sprintf(textstring3,"%X",(unsigned int)va_arg(ap,unsigned int));
  97. memcpy(textstring2+count2,textstring3,strlen(textstring3));
  98. count2+=strlen(textstring3);
  99. break;
  100. }
  101. }
  102. else
  103. textstring2[count2++]=textstring[count];
  104. count++;
  105. }
  106. va_end(ap);
  107. textstring2[count2]=0;
  108. if ((x&TEXT_CENTER)==TEXT_CENTER)
  109. x=x-(count2*textsize)/2;
  110. if ((y&TEXT_CENTER)==TEXT_CENTER)
  111. y=y-(textsize)/2;
  112. if ((x&TEXT_END)==TEXT_END)
  113. x=x-(count2*textsize);
  114. if ((y&TEXT_END)==TEXT_END)
  115. y=y-textsize;
  116. x&=0xFFFF;
  117. y&=0xFFFF;
  118. glBindTexture(GL_TEXTURE_2D,texture[1000+font.texturenum].glname);
  119. count=0;
  120. while (textstring2[count]!=0)
  121. {
  122. if (textstring2[count]!=127)
  123. {
  124. glBegin(GL_QUADS);
  125. glColor4f(red,green,blue,alpha);
  126. vec[0]=(float)x+(float)count*(float)textsize;
  127. vec[1]=(float)y;
  128. convertscreenvertex(vec,font.sizex,font.sizey);
  129. texcoord[0]=(float)(textstring2[count]&15)*16.0f+0.5f;
  130. texcoord[1]=(float)(textstring2[count]>>4)*16.0f+0.5f;
  131. texcoord[0]/=256.0f;
  132. texcoord[1]/=256.0f;
  133. glTexCoord2fv(texcoord);
  134. glVertex3fv(vec);
  135. vec[0]=(float)x+(float)textsize+(float)(count)*(float)textsize;
  136. vec[1]=(float)y;
  137. convertscreenvertex(vec,font.sizex,font.sizey);
  138. texcoord[0]=(float)(textstring2[count]&15)*16.0f+16.0f-0.5f;
  139. texcoord[1]=(float)(textstring2[count]>>4)*16.0f+0.5f;
  140. texcoord[0]/=256.0f;
  141. texcoord[1]/=256.0f;
  142. glTexCoord2fv(texcoord);
  143. glVertex3fv(vec);
  144. vec[0]=(float)x+(float)textsize+(float)(count)*(float)textsize;
  145. vec[1]=(float)y+(float)textsize;
  146. convertscreenvertex(vec,font.sizex,font.sizey);
  147. texcoord[0]=(float)(textstring2[count]&15)*16.0f+16.0f-0.5f;
  148. texcoord[1]=(float)(textstring2[count]>>4)*16.0f+16.0f-0.5f;
  149. texcoord[0]/=256.0f;
  150. texcoord[1]/=256.0f;
  151. glTexCoord2fv(texcoord);
  152. glVertex3fv(vec);
  153. vec[0]=(float)x+(float)(count)*(float)textsize;
  154. vec[1]=(float)y+(float)textsize;
  155. convertscreenvertex(vec,font.sizex,font.sizey);
  156. texcoord[0]=(float)(textstring2[count]&15)*16.0f+0.5f;
  157. texcoord[1]=(float)(textstring2[count]>>4)*16.0f+16.0f-0.5f;
  158. texcoord[0]/=256.0f;
  159. texcoord[1]/=256.0f;
  160. glTexCoord2fv(texcoord);
  161. glVertex3fv(vec);
  162. glEnd();
  163. }
  164. else
  165. {
  166. glDisable(GL_TEXTURE_2D);
  167. glBegin(GL_QUADS);
  168. glColor4f(red,green,blue,alpha);
  169. vec[0]=(float)x+(float)count*(float)textsize;
  170. vec[1]=(float)y;
  171. convertscreenvertex(vec,font.sizex,font.sizey);
  172. glVertex3fv(vec);
  173. vec[0]=(float)x+(float)textsize+(float)(count)*(float)textsize;
  174. vec[1]=(float)y;
  175. convertscreenvertex(vec,font.sizex,font.sizey);
  176. glVertex3fv(vec);
  177. vec[0]=(float)x+(float)textsize+(float)(count)*(float)textsize;
  178. vec[1]=(float)y+(float)textsize;
  179. convertscreenvertex(vec,font.sizex,font.sizey);
  180. glVertex3fv(vec);
  181. vec[0]=(float)x+(float)(count)*(float)textsize;
  182. vec[1]=(float)y+(float)textsize;
  183. convertscreenvertex(vec,font.sizex,font.sizey);
  184. glVertex3fv(vec);
  185. glEnd();
  186. glEnable(GL_TEXTURE_2D);
  187. }
  188. count++;
  189. }
  190. }
  191. void setuptextdisplay(void)
  192. {
  193. glEnable(GL_TEXTURE_2D);
  194. glEnable(GL_BLEND);
  195. glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  196. glViewport(0,0,windowinfo.resolutionx,windowinfo.resolutiony);
  197. glMatrixMode(GL_PROJECTION);
  198. glLoadIdentity();
  199. glFrustum(-1.0f,1.0f,-0.75f,0.75f,1.0f,1000.0f);
  200. glMatrixMode(GL_MODELVIEW);
  201. glLoadIdentity();
  202. glMatrixMode(GL_TEXTURE);
  203. glLoadIdentity();
  204. }
  205. void convertscreenvertex(float vec[3],int resolutionx,int resolutiony)
  206. {
  207. vec[0]=(vec[0]-(float)(resolutionx>>1))/(float)(resolutionx>>1);
  208. vec[1]=((float)(resolutiony>>1)-vec[1])/(float)(resolutionx>>1);
  209. vec[2]=-1.0f;
  210. }
  211. void drawbackground(int texturenum,int x,int y,int sizex,int sizey,int resolutionx,int resolutiony)
  212. {
  213. float vec[3];
  214. if ((x&TEXT_CENTER)==TEXT_CENTER)
  215. x=x-(sizex)/2;
  216. if ((y&TEXT_CENTER)==TEXT_CENTER)
  217. y=y-(sizey)/2;
  218. if ((x&TEXT_END)==TEXT_END)
  219. x=x-(sizex);
  220. if ((y&TEXT_END)==TEXT_END)
  221. y=y-sizey;
  222. x&=0xFFFF;
  223. y&=0xFFFF;
  224. glBindTexture(GL_TEXTURE_2D,texture[texturenum].glname);
  225. glBegin(GL_QUADS);
  226. vec[0]=(float)x;
  227. vec[1]=(float)y;
  228. convertscreenvertex(vec,resolutionx,resolutiony);
  229. glTexCoord2f(0.0f,0.0f);
  230. glVertex3fv(vec);
  231. vec[0]=(float)(x+sizex);
  232. vec[1]=(float)y;
  233. convertscreenvertex(vec,resolutionx,resolutiony);
  234. glTexCoord2f(1.0f,0.0f);
  235. glVertex3fv(vec);
  236. vec[0]=(float)(x+sizex);
  237. vec[1]=(float)(y+sizey);
  238. convertscreenvertex(vec,resolutionx,resolutiony);
  239. glTexCoord2f(1.0f,1.0f);
  240. glVertex3fv(vec);
  241. vec[0]=(float)x;
  242. vec[1]=(float)(y+sizey);
  243. convertscreenvertex(vec,resolutionx,resolutiony);
  244. glTexCoord2f(0.0f,1.0f);
  245. glVertex3fv(vec);
  246. glEnd();
  247. }
  248. void drawmousecursor(int texturenum,int x,int y,int textsize,float red,float green,float blue,float alpha)
  249. {
  250. float vec[3];
  251. glBindTexture(GL_TEXTURE_2D,texture[texturenum].glname);
  252. glBegin(GL_QUADS);
  253. vec[0]=(float)x-(float)textsize;
  254. vec[1]=(float)y-(float)textsize;
  255. convertscreenvertex(vec,font.sizex,font.sizey);
  256. glColor4f(red,green,blue,alpha);
  257. glTexCoord2f(0.0f,0.0f);
  258. glVertex3fv(vec);
  259. vec[0]=(float)x+(float)textsize;
  260. vec[1]=(float)y-(float)textsize;
  261. convertscreenvertex(vec,font.sizex,font.sizey);
  262. glTexCoord2f(1.0f,0.0f);
  263. glVertex3fv(vec);
  264. vec[0]=(float)x+(float)textsize;
  265. vec[1]=(float)y+(float)textsize;
  266. convertscreenvertex(vec,font.sizex,font.sizey);
  267. glTexCoord2f(1.0f,1.0f);
  268. glVertex3fv(vec);
  269. vec[0]=(float)x-(float)textsize;
  270. vec[1]=(float)y+(float)textsize;
  271. convertscreenvertex(vec,font.sizex,font.sizey);
  272. glTexCoord2f(0.0f,1.0f);
  273. glVertex3fv(vec);
  274. glEnd();
  275. }