123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- /*
- Copyright (C) 2005, 2010 - Cryptic Sea
- This file is part of Gish.
- Gish is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version 2
- of the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- See the GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- void drawtext(unsigned char *textstring,int x,int y,int textsize,float red,float green,float blue,float alpha,...)
- {
- int count,count2;
- int variabletemp;
- int texturenum;
- va_list ap;
- float vec[3];
- float texcoord[2];
- count=0;
- count2=0;
- va_start(ap,alpha);
- while (textstring[count]!=0 && count2<1000)
- {
- if (textstring[count]=='/')
- {
- switch (textstring[count+1])
- {
- case '/':
- count++;
- textstring2[count2++]='/';
- break;
- case 'i':
- count++;
- sprintf(textstring3,"%d",(int)va_arg(ap,int));
- memcpy(textstring2+count2,textstring3,strlen(textstring3));
- count2+=strlen(textstring3);
- break;
- case 'I':
- count++;
- variabletemp=(int)va_arg(ap,int);
- if (variabletemp>=1000000)
- {
- sprintf(textstring3,"%d",(variabletemp/1000000));
- memcpy(textstring2+count2,textstring3,strlen(textstring3));
- count2+=strlen(textstring3);
- textstring2[count2++]=',';
- }
- if (variabletemp>=1000)
- {
- if (variabletemp>=1000000)
- sprintf(textstring3,"%.3d",((variabletemp/1000)%1000));
- else
- sprintf(textstring3,"%d",((variabletemp/1000)%1000));
- memcpy(textstring2+count2,textstring3,strlen(textstring3));
- count2+=strlen(textstring3);
- textstring2[count2++]=',';
- }
- if (variabletemp>=1000)
- sprintf(textstring3,"%.3d",(variabletemp%1000));
- else
- sprintf(textstring3,"%d",variabletemp);
- memcpy(textstring2+count2,textstring3,strlen(textstring3));
- count2+=strlen(textstring3);
- break;
- case 'u':
- count++;
- sprintf(textstring3,"%u",(unsigned int)va_arg(ap,unsigned int));
- memcpy(textstring2+count2,textstring3,strlen(textstring3));
- count2+=strlen(textstring3);
- break;
- case 'f':
- count++;
- sprintf(textstring3,"%f",(double)va_arg(ap,double));
- memcpy(textstring2+count2,textstring3,strlen(textstring3));
- count2+=strlen(textstring3);
- break;
- case 'c':
- count++;
- variabletemp=va_arg(ap,int);
- if (variabletemp!=0)
- textstring2[count2++]=variabletemp;
- break;
- case 's':
- count++;
- sprintf(textstring3,"%s",va_arg(ap,char *));
- memcpy(textstring2+count2,textstring3,strlen(textstring3));
- count2+=strlen(textstring3);
- break;
- case 'x':
- count++;
- sprintf(textstring3,"%X",(unsigned int)va_arg(ap,unsigned int));
- memcpy(textstring2+count2,textstring3,strlen(textstring3));
- count2+=strlen(textstring3);
- break;
- }
- }
- else
- textstring2[count2++]=textstring[count];
- count++;
- }
- va_end(ap);
- textstring2[count2]=0;
- if ((x&TEXT_CENTER)==TEXT_CENTER)
- x=x-(count2*textsize)/2;
- if ((y&TEXT_CENTER)==TEXT_CENTER)
- y=y-(textsize)/2;
- if ((x&TEXT_END)==TEXT_END)
- x=x-(count2*textsize);
- if ((y&TEXT_END)==TEXT_END)
- y=y-textsize;
- x&=0xFFFF;
- y&=0xFFFF;
- glBindTexture(GL_TEXTURE_2D,texture[1000+font.texturenum].glname);
- count=0;
- while (textstring2[count]!=0)
- {
- if (textstring2[count]!=127)
- {
- glBegin(GL_QUADS);
- glColor4f(red,green,blue,alpha);
- vec[0]=(float)x+(float)count*(float)textsize;
- vec[1]=(float)y;
- convertscreenvertex(vec,font.sizex,font.sizey);
- texcoord[0]=(float)(textstring2[count]&15)*16.0f+0.5f;
- texcoord[1]=(float)(textstring2[count]>>4)*16.0f+0.5f;
- texcoord[0]/=256.0f;
- texcoord[1]/=256.0f;
-
- glTexCoord2fv(texcoord);
- glVertex3fv(vec);
-
- vec[0]=(float)x+(float)textsize+(float)(count)*(float)textsize;
- vec[1]=(float)y;
- convertscreenvertex(vec,font.sizex,font.sizey);
- texcoord[0]=(float)(textstring2[count]&15)*16.0f+16.0f-0.5f;
- texcoord[1]=(float)(textstring2[count]>>4)*16.0f+0.5f;
- texcoord[0]/=256.0f;
- texcoord[1]/=256.0f;
-
- glTexCoord2fv(texcoord);
- glVertex3fv(vec);
-
- vec[0]=(float)x+(float)textsize+(float)(count)*(float)textsize;
- vec[1]=(float)y+(float)textsize;
- convertscreenvertex(vec,font.sizex,font.sizey);
- texcoord[0]=(float)(textstring2[count]&15)*16.0f+16.0f-0.5f;
- texcoord[1]=(float)(textstring2[count]>>4)*16.0f+16.0f-0.5f;
- texcoord[0]/=256.0f;
- texcoord[1]/=256.0f;
-
- glTexCoord2fv(texcoord);
- glVertex3fv(vec);
-
- vec[0]=(float)x+(float)(count)*(float)textsize;
- vec[1]=(float)y+(float)textsize;
- convertscreenvertex(vec,font.sizex,font.sizey);
- texcoord[0]=(float)(textstring2[count]&15)*16.0f+0.5f;
- texcoord[1]=(float)(textstring2[count]>>4)*16.0f+16.0f-0.5f;
- texcoord[0]/=256.0f;
- texcoord[1]/=256.0f;
-
- glTexCoord2fv(texcoord);
- glVertex3fv(vec);
-
- glEnd();
- }
- else
- {
- glDisable(GL_TEXTURE_2D);
- glBegin(GL_QUADS);
- glColor4f(red,green,blue,alpha);
- vec[0]=(float)x+(float)count*(float)textsize;
- vec[1]=(float)y;
- convertscreenvertex(vec,font.sizex,font.sizey);
- glVertex3fv(vec);
-
- vec[0]=(float)x+(float)textsize+(float)(count)*(float)textsize;
- vec[1]=(float)y;
- convertscreenvertex(vec,font.sizex,font.sizey);
- glVertex3fv(vec);
-
- vec[0]=(float)x+(float)textsize+(float)(count)*(float)textsize;
- vec[1]=(float)y+(float)textsize;
- convertscreenvertex(vec,font.sizex,font.sizey);
- glVertex3fv(vec);
-
- vec[0]=(float)x+(float)(count)*(float)textsize;
- vec[1]=(float)y+(float)textsize;
- convertscreenvertex(vec,font.sizex,font.sizey);
- glVertex3fv(vec);
-
- glEnd();
- glEnable(GL_TEXTURE_2D);
- }
- count++;
- }
- }
- void setuptextdisplay(void)
- {
- glEnable(GL_TEXTURE_2D);
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
- glViewport(0,0,windowinfo.resolutionx,windowinfo.resolutiony);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glFrustum(-1.0f,1.0f,-0.75f,0.75f,1.0f,1000.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glMatrixMode(GL_TEXTURE);
- glLoadIdentity();
- }
- void convertscreenvertex(float vec[3],int resolutionx,int resolutiony)
- {
- vec[0]=(vec[0]-(float)(resolutionx>>1))/(float)(resolutionx>>1);
- vec[1]=((float)(resolutiony>>1)-vec[1])/(float)(resolutionx>>1);
- vec[2]=-1.0f;
- }
- void drawbackground(int texturenum,int x,int y,int sizex,int sizey,int resolutionx,int resolutiony)
- {
- float vec[3];
- if ((x&TEXT_CENTER)==TEXT_CENTER)
- x=x-(sizex)/2;
- if ((y&TEXT_CENTER)==TEXT_CENTER)
- y=y-(sizey)/2;
- if ((x&TEXT_END)==TEXT_END)
- x=x-(sizex);
- if ((y&TEXT_END)==TEXT_END)
- y=y-sizey;
- x&=0xFFFF;
- y&=0xFFFF;
- glBindTexture(GL_TEXTURE_2D,texture[texturenum].glname);
- glBegin(GL_QUADS);
- vec[0]=(float)x;
- vec[1]=(float)y;
- convertscreenvertex(vec,resolutionx,resolutiony);
- glTexCoord2f(0.0f,0.0f);
- glVertex3fv(vec);
- vec[0]=(float)(x+sizex);
- vec[1]=(float)y;
- convertscreenvertex(vec,resolutionx,resolutiony);
- glTexCoord2f(1.0f,0.0f);
- glVertex3fv(vec);
- vec[0]=(float)(x+sizex);
- vec[1]=(float)(y+sizey);
- convertscreenvertex(vec,resolutionx,resolutiony);
- glTexCoord2f(1.0f,1.0f);
- glVertex3fv(vec);
- vec[0]=(float)x;
- vec[1]=(float)(y+sizey);
- convertscreenvertex(vec,resolutionx,resolutiony);
- glTexCoord2f(0.0f,1.0f);
- glVertex3fv(vec);
- glEnd();
- }
- void drawmousecursor(int texturenum,int x,int y,int textsize,float red,float green,float blue,float alpha)
- {
- float vec[3];
- glBindTexture(GL_TEXTURE_2D,texture[texturenum].glname);
- glBegin(GL_QUADS);
- vec[0]=(float)x-(float)textsize;
- vec[1]=(float)y-(float)textsize;
- convertscreenvertex(vec,font.sizex,font.sizey);
- glColor4f(red,green,blue,alpha);
- glTexCoord2f(0.0f,0.0f);
- glVertex3fv(vec);
- vec[0]=(float)x+(float)textsize;
- vec[1]=(float)y-(float)textsize;
- convertscreenvertex(vec,font.sizex,font.sizey);
- glTexCoord2f(1.0f,0.0f);
- glVertex3fv(vec);
- vec[0]=(float)x+(float)textsize;
- vec[1]=(float)y+(float)textsize;
- convertscreenvertex(vec,font.sizex,font.sizey);
- glTexCoord2f(1.0f,1.0f);
- glVertex3fv(vec);
- vec[0]=(float)x-(float)textsize;
- vec[1]=(float)y+(float)textsize;
- convertscreenvertex(vec,font.sizex,font.sizey);
- glTexCoord2f(0.0f,1.0f);
- glVertex3fv(vec);
- glEnd();
- }
|