1 #include <GL/glut.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 
6 #include "LA.h"
7 #include "bmp.h"
8 #include "createTexture.h"
9 #include "Textfeld.h"
10 
11 GLint links[] = {
12  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
13  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
14  0,  9, 11,  9,  9,  8,  8,  7,  8,  7,  7,  7,  6,  8,  9,  7,
15  9,  7,  8,  7,  7,  7,  8,  7,  7,  7,  9,  6,  8,  7,  7,  7,
16  9,  7,  9,  8,  9,  9,  9,  8,  8,  8,  7,  8,  8,  7,  8,  7,
17  9,  8,  9,  7,  6,  8,  6,  7,  7,  6,  6, 10,  7,  7,  9,  5,
18  5,  8,  9,  8,  8,  8,  6,  7,  8,  8,  8,  8,  8,  7,  8,  7,
19  9,  8,  9,  8,  7,  8,  7,  7,  7,  7,  7,  7,  9,  6,  8,  0,
20 
21  0,  0,  0,  8,  7, 10,  8,  8,  3,  7,  0,  9,  7,  0,  0,  0,
22  0,  7,  8,  8,  7, 10,  7,  8,  3,  9,  0, 10,  7,  0,  0,  0,
23  0,  9, 11,  8, 10,  8, 10,  9,  4,  8,  7,  9,  7,  0,  7,  0,
24  0,  8,  8,  8,  7,  8,  8,  9,  7,  7,  7,  9,  8,  8,  8,  7,
25  8,  7,  8,  7,  7,  7,  7,  8,  8,  8,  8,  8,  4,  7,  3,  4,
26  6,  9,  8,  8,  8,  8,  8,  0,  7,  8,  8,  8,  8,  6,  8,  8,
27  8,  8,  8,  8,  8,  8,  8,  7,  7,  7,  7,  7,  4,  6,  3,  3,
28  9,  9,  8,  8,  8,  8,  8,  0,  7,  8,  8,  8,  8,  6,  8,  6
29 };
30 
31 GLint rechts[]={
32  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
33  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
34 10, 15, 22, 28, 29, 33, 33, 15, 15, 14, 20, 29, 14, 21, 14, 23,
35 31, 17, 26, 26, 30, 26, 27, 25, 27, 27, 15, 14, 28, 28, 28, 25,
36 37, 32, 29, 30, 32, 24, 24, 34, 32, 14, 21, 30, 23, 39, 32, 34,
37 29, 36, 29, 27, 25, 32, 30, 43, 30, 28, 27, 19, 23, 16, 27, 26,
38 15, 28, 29, 24, 28, 27, 20, 27, 27, 13, 13, 26, 13, 38, 26, 27,
39 28, 28, 20, 23, 19, 27, 25, 37, 26, 26, 24, 19, 12, 19, 28,  0,
40 
41  0,  0,  0, 28, 25, 43, 28, 28, 18, 47,  0, 17, 43,  0,  0,  0,
42  0, 16, 16, 25, 25, 23, 26, 22, 18, 44,  0, 17, 42,  0,  0,  0,
43  0, 15, 28, 31, 29, 30, 13, 28, 17, 37, 20, 27, 29,  0, 36,  0,
44  0, 30, 20, 20, 17, 27, 29, 15, 15, 13, 20, 27, 42, 41, 42, 24,
45 32, 32, 32, 31, 31, 31, 40, 30, 24, 24, 24, 24, 15, 17, 18, 17,
46 32, 33, 35, 35, 34, 35, 35,  0, 35, 32, 32, 32, 32, 27, 28, 27,
47 28, 28, 28, 28, 28, 28, 43, 24, 27, 27, 27, 27, 14, 17, 18, 17,
48 28, 27, 28, 28, 28, 28, 28,  0, 28, 26, 26, 26, 26, 25, 28, 25
49 };
50 
Textfeld()51 Textfeld::Textfeld() {
52   //
53   x=y=Hoehe=Aspekt=Alpha = 0;
54   alt_x=alt_y=alt_Hoehe=alt_Alpha = 0;
55   soll_x=soll_y=soll_Hoehe=soll_Alpha = 0;
56   Ausrichtung = 0;
57   InAnimation = 0;
58   Signal = 0;
59   Zeit = 0;
60   DisplayListAnfang = 0;
61   for (GLint i=0;i<512;i++)
62 	  TexturenAnfang[i] = 0;
63   TextfeldIndex = 0;
64   Horchen = 0;
65   Zeilen=0;
66 
67   MaxBreite=0;
68 
69 }
70 
dummyInitialisiere(GLint TexGr)71 GLint Textfeld::dummyInitialisiere(GLint TexGr){
72   FMatrix tex;
73   if (!DisplayListAnfang)
74     DisplayListAnfang=glGenLists(512);
75 
76   if (!TexturenAnfang[0])
77     glGenTextures(512,&TexturenAnfang[0]);
78 
79   char DateiName[80];
80   sprintf(DateiName,"Texturen/%i/buchstaben.bmp",TexGr);
81   loadBMP(tex,tex,tex,DateiName);
82   GLint Rastergroesse=(tex.nch+1)/16;
83 
84   for (GLint Buchstabe=0;Buchstabe<256;Buchstabe++) {
85     GLint x = Buchstabe % 16;
86     GLint y = Buchstabe / 16;
87 
88     if (rechts[Buchstabe] && Buchstabe!=32) {
89       glBindTexture(GL_TEXTURE_2D,TexturenAnfang[Buchstabe]);
90       createTextureText(tex,x,y,Rastergroesse);
91 	}
92 
93     glNewList(DisplayListAnfang+Buchstabe,GL_COMPILE_AND_EXECUTE);
94     if (rechts[Buchstabe] && Buchstabe!=32) {
95       glBindTexture(GL_TEXTURE_2D,TexturenAnfang[Buchstabe]);
96       //glEnable(GL_TEXTURE_2D);
97       //glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
98       glBegin(GL_QUADS);
99       //    printf ("%i %i %i\n",Buchstabe,x,y);
100       glTexCoord2f(0.0,0.0);glVertex2f(0.0,0.0);
101       glTexCoord2f(1.0,0.0);glVertex2f(1.0,0.0);
102       glTexCoord2f(1.0,1.0);glVertex2f(1.0,1.0);
103       glTexCoord2f(0.0,1.0);glVertex2f(0.0,1.0);
104       glEnd();
105       //glDisable(GL_TEXTURE_2D);
106     }
107     glEndList();
108   }
109 
110   sprintf(DateiName,"Texturen/%i/buchstabenalpha.bmp",TexGr);
111   loadBMP(tex,tex,tex,DateiName);
112   Rastergroesse=(tex.nch+1)/16;
113 
114   for (GLint Buchstabe2=0;Buchstabe2<256;Buchstabe2++) {
115     GLint x = Buchstabe2 % 16;
116     GLint y = Buchstabe2 / 16;
117 
118     if (rechts[Buchstabe2] && Buchstabe2!=32) {
119       glBindTexture(GL_TEXTURE_2D,TexturenAnfang[256+Buchstabe2]);
120       createTextureText2(tex,x,y,Rastergroesse);
121 	}
122 
123     glNewList(DisplayListAnfang+256+Buchstabe2,GL_COMPILE_AND_EXECUTE);
124     if (rechts[Buchstabe2] && Buchstabe2!=32) {
125       glBindTexture(GL_TEXTURE_2D,TexturenAnfang[256+Buchstabe2]);
126       //glEnable(GL_TEXTURE_2D);
127       //glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
128       glBegin(GL_QUADS);
129       //    printf ("%i %i %i\n",Buchstabe,x,y);
130       glTexCoord2f(0.0,0.0);glVertex2f(0.0,0.0);
131       glTexCoord2f(1.0,0.0);glVertex2f(1.0,0.0);
132       glTexCoord2f(1.0,1.0);glVertex2f(1.0,1.0);
133       glTexCoord2f(0.0,1.0);glVertex2f(0.0,1.0);
134       glEnd();
135       //glDisable(GL_TEXTURE_2D);
136     }
137     glEndList();
138   }
139 
140   tex.free_FMatrix();
141 
142   x=y=Hoehe=Aspekt=Alpha=0.0;
143   alt_x=alt_y=alt_Hoehe=alt_Alpha=0.0;
144   soll_x=soll_y=soll_Hoehe=soll_Alpha=0.0;
145   Ausrichtung=A_LINKS;
146   InAnimation=0;
147   Signal=0;
148   Zeit=0;
149   Zeichenkette[0]='\0';
150   Horchen=0;
151   return DisplayListAnfang;
152 }
153 
154 
155 
156 
157 
Initialisiere(GLint DLA)158 void Textfeld::Initialisiere(GLint DLA){
159   DisplayListAnfang=DLA;
160   x=y=Hoehe=Aspekt=Alpha=0.0;
161   alt_x=alt_y=alt_Hoehe=alt_Alpha=0.0;
162   soll_x=soll_y=soll_Hoehe=soll_Alpha=0.0;
163   Ausrichtung=A_LINKS;
164   InAnimation=0;
165   Signal=0;
166   Zeit=0;
167   Zeichenkette[0]='\0';
168   Horchen=0;
169 }
170 
Initialisiere(GLint DLA,char TextZ[])171 void Textfeld::Initialisiere(GLint DLA, char TextZ[]){
172   Initialisiere(DLA);
173   SetzeText(TextZ);
174 }
175 
InitialisiereKDL(GLint DLA,char TextZ[])176 void Textfeld::InitialisiereKDL(GLint DLA, char TextZ[]){
177   Initialisiere(DLA);
178   SetzeTextKDL(TextZ);
179 }
180 
male()181 void Textfeld::male(){
182 	if (Alpha<.03) {return;}
183     glPushMatrix();
184     glTranslatef(x,y,0);
185     glScalef(Hoehe,Hoehe,1);
186     glColor4f(1.0,1.0,1.0,Alpha);
187     glCallList(TextfeldIndex);
188     glPopMatrix();
189 
190 }
191 
PositioniereFix(GLfloat X,GLfloat Y,GLfloat H,GLint A)192 void Textfeld::PositioniereFix(GLfloat X,GLfloat Y,GLfloat H,GLint A){
193 
194   if (A) Ausrichtung=A;
195 
196   switch (Ausrichtung) {
197   case A_LINKS: { soll_x = X; } break;
198   case A_MITTE: { soll_x = X-.5*Aspekt*H; } break;
199   case A_RECHTS:{ soll_x = X-Aspekt*H; } break;
200   }
201 
202   soll_y     = Y;
203   soll_Hoehe = H;
204   soll_Alpha = EINGEBLENDET;
205 
206   x=soll_x;
207   y=soll_y;
208   Hoehe=soll_Hoehe;
209 
210   Signal=0;
211 
212 }
213 
Positioniere(GLfloat X,GLfloat Y,GLfloat H,GLint A)214 void Textfeld::Positioniere(GLfloat X,GLfloat Y,GLfloat H,GLint A){
215 
216   if (A) Ausrichtung=A;
217 
218   switch (Ausrichtung) {
219   case A_LINKS: { soll_x = X; } break;
220   case A_MITTE: { soll_x = X-.5*Aspekt*H; } break;
221   case A_RECHTS:{ soll_x = X-Aspekt*H; } break;
222   }
223 
224   soll_y     = Y;
225   soll_Hoehe = H;
226   soll_Alpha = EINGEBLENDET;
227 
228   if (Alpha==AUSGEBLENDET) {
229 
230     x=((soll_x-8)/1.5)+8;
231     y=((soll_y-6)/1.5)+6;
232     Hoehe=soll_Hoehe/1.5;
233 
234     /*
235       ax=((24.0*rand())/RAND_MAX)-4;
236       ay=((14.0*rand())/RAND_MAX)-1;
237       bx=ax+2*(bx_-ax_);
238       by=ay+2*(by_-ay_);
239     */
240   }
241 
242   Signal=0;
243 
244   StarteAnimation();
245 }
246 
247 
SetzeText(char TextZ[])248 void Textfeld::SetzeText(char TextZ[]){
249   int i=0;
250   while (TextZ[i] && i<1999) {
251     Zeichenkette[i]=TextZ[i];
252     i++;
253   }
254   Zeichenkette[i]=0;
255   GeneriereDisplayList();
256 }
257 
SetzeTextKDL(char TextZ[])258 void Textfeld::SetzeTextKDL(char TextZ[]){
259   int i=0;
260   while (TextZ[i] && i<1999) {
261     Zeichenkette[i]=TextZ[i];
262     i++;
263   }
264   Zeichenkette[i]=0;
265 }
266 
Zeichen(char Taste)267 GLint Textfeld::Zeichen(char Taste){
268   if (Horchen) {
269     if (Taste>=32 &&
270 	Taste!=8 && Taste!=127 &&
271 	Taste!=12 && Taste!=10 ) {//normale Zeichen
272       GLint i;
273       for (i=0;i<9 && Zeichenkette[i];i++) {}
274       if (i<9) sprintf(Zeichenkette,"%s%c",Zeichenkette,Taste);
275       GeneriereDisplayList();
276       return 1;
277     } else if (Taste==8 || Taste==127) { // loeschen
278       for (GLint i=0;i<9;i++)
279 	if (!Zeichenkette[i+1]) Zeichenkette[i]='\0';
280       GeneriereDisplayList();
281       return 1;
282     } else if (Taste==13 || Taste==10) { // enter
283       soll_Alpha=EINGEBLENDET;
284       Horchen=0;
285       if (soll_Alpha!=Alpha) StarteAnimation();
286       //GeneriereDisplayList();
287       return 1;
288     }
289   }
290   return 0;
291 }
292 
Text()293 char* Textfeld::Text(){return Zeichenkette;}
294 
Desaktiviere()295 void Textfeld::Desaktiviere(){
296   soll_Alpha=AUSGEBLENDET;
297 
298   soll_x=((x-8)*1.5)+8;
299   soll_y=((y-6)*1.5)+6;
300   soll_Hoehe=Hoehe*1.5;
301 
302   Horchen = 0;
303 
304   if (soll_Alpha!=Alpha) StarteAnimation();
305 }
306 
Weghoeren()307 void Textfeld::Weghoeren(){
308   if (Alpha && soll_Alpha) Eingeblendet();
309   Horchen = 0;
310 }
311 
Herhoeren()312 void Textfeld::Herhoeren(){
313   VollSichtbar();
314   Horchen = 1;
315 }
316 
Angewaehlt()317 void Textfeld::Angewaehlt(){
318   Alpha=ANGEWAEHLT;
319   soll_Alpha=EINGEBLENDET;
320   if (soll_Alpha!=Alpha) StarteAnimation();
321 }
322 
Eingeblendet()323 void Textfeld::Eingeblendet(){
324   soll_Alpha=EINGEBLENDET;
325   if (soll_Alpha!=Alpha) StarteAnimation();
326 }
327 
VollSichtbar()328 void Textfeld::VollSichtbar(){
329   soll_Alpha=VOLLSICHTBAR;
330   if (soll_Alpha!=Alpha) StarteAnimation();
331 }
332 
Animiere(GLint Faktor)333 GLint Textfeld::Animiere(GLint Faktor){
334   if (!InAnimation) {return 0;}
335   Zeit+=Faktor;
336   if (Zeit>=ANIMATIONSDAUER) {
337     x=soll_x;
338     y=soll_y;
339     Hoehe=soll_Hoehe;
340     Alpha=soll_Alpha;
341     InAnimation=0;
342     return 1;
343   } else {
344     GLfloat Faktor=(.5-.5*cos(M_PI*Zeit/ANIMATIONSDAUER));
345     x=(soll_x-alt_x)*Faktor+alt_x;
346     y=(soll_y-alt_y)*Faktor+alt_y;
347     Hoehe=(soll_Hoehe-alt_Hoehe)*Faktor+alt_Hoehe;
348     Alpha=(soll_Alpha-alt_Alpha)*Faktor+alt_Alpha;
349     //    printf("%i: %f\n",Zeit,Alpha);
350     return 0;
351   }
352 }
353 
SetzeSignal(GLint NeuesSignal)354 void Textfeld::SetzeSignal(GLint NeuesSignal){
355   Signal=NeuesSignal;
356 }
357 
StarteAnimation()358 void Textfeld::StarteAnimation(){
359   InAnimation=1;
360   Zeit=0;
361   alt_x=x;
362   alt_y=y;
363   alt_Hoehe=Hoehe;
364   alt_Alpha=Alpha;
365 }
366 
GeneriereDisplayList()367 void Textfeld::GeneriereDisplayList(){
368   if (!TextfeldIndex) {
369     TextfeldIndex=glGenLists(1);
370   }
371 
372 
373   if (!MaxBreite) {
374     glNewList(TextfeldIndex,GL_COMPILE_AND_EXECUTE);
375 	glEnable(GL_TEXTURE_2D);
376     glPushMatrix();
377     Aspekt=0.0;
378     Zeilen=1;
379     GLint p=0;
380     GLint c=0;
381     while ((c=Zeichenkette[p++])) {
382       if (c<0) c+=256;
383       Aspekt+=(rechts[c]-links[c]+4)/64.0;
384       glTranslatef(-links[c]/64.0,0,0);
385       glCallList(DisplayListAnfang+c+256);
386       glTranslatef((rechts[c]+4)/64.0,0,0);
387     }
388     glPopMatrix();
389     glPushMatrix();
390     Zeilen=1;
391     p=0;
392     while ((c=Zeichenkette[p++])) {
393       if (c<0) c+=256;
394       glTranslatef(-links[c]/64.0,0,0);
395       glCallList(DisplayListAnfang+c);
396       glTranslatef((rechts[c]+4)/64.0,0,0);
397     }
398     glPopMatrix();
399 	glDisable(GL_TEXTURE_2D);
400     glEndList();
401   }
402 
403 
404   if (MaxBreite) {
405     glNewList(TextfeldIndex,GL_COMPILE_AND_EXECUTE);
406 	glEnable(GL_TEXTURE_2D);
407     Aspekt=0.0;
408     Zeilen=1;
409 
410     GLint Anfang=0;
411     GLint Zeichen=0;
412     GLint Pos=0;
413     GLint LeerzeichenAnzahl=0;
414     GLint LetztesLeerzeichen=0;
415     Zeilen=0;
416     GLfloat ZeilenBreite=0.0;
417     GLfloat ZeilenBreiteBisLetztesLeerzeichen=0.0;
418     while (Zeichenkette[Pos]) {
419 
420       Anfang=Pos;
421       LeerzeichenAnzahl=0;
422       LetztesLeerzeichen=0;
423       ZeilenBreite=0.0;
424 
425       while ((Zeichen=Zeichenkette[Pos]) && ZeilenBreite<MaxBreite*1.05) {
426 	if (Zeichen==32) {
427 	  ZeilenBreiteBisLetztesLeerzeichen=ZeilenBreite;
428 	  LetztesLeerzeichen=Pos;
429 	  LeerzeichenAnzahl++;
430 	}
431 	//printf("%c",Zeichen);fflush(stdout);
432 	if (Zeichen<0) Zeichen+=256;
433 	ZeilenBreite+=(rechts[Zeichen]-links[Zeichen]+4)/64.0;
434 	Pos++;
435       }
436 
437       if (Zeichenkette[Pos]) { // noch nicht am Ende: Blocksatz
438 	GLfloat delta=(MaxBreite-ZeilenBreiteBisLetztesLeerzeichen)/(LeerzeichenAnzahl-1.0);
439 	//printf("%f\n",delta);
440 	glPushMatrix();
441 	glTranslatef(0,-Zeilen*.7,0);
442 	GLint c;
443 	Pos=Anfang;
444 	while ((c=Zeichenkette[Pos++])&& Pos<=LetztesLeerzeichen) {
445 	  if (c<0) c+=256;
446 	  glTranslatef(-links[c]/64.0,0,0);
447 	  glCallList(DisplayListAnfang+c+256);
448 	  glTranslatef((rechts[c]+4)/64.0,0,0);
449 	  if (c==32) glTranslatef(delta,0,0);
450 	}
451 	glPopMatrix();
452 	glPushMatrix();
453 	glTranslatef(0,-Zeilen*.7,0);
454 	Pos=Anfang;
455 	while ((c=Zeichenkette[Pos++])&& Pos<=LetztesLeerzeichen) {
456 	  if (c<0) c+=256;
457 	  glTranslatef(-links[c]/64.0,0,0);
458 	  glCallList(DisplayListAnfang+c);
459 	  glTranslatef((rechts[c]+4)/64.0,0,0);
460 	  if (c==32) glTranslatef(delta,0,0);
461 	}
462 	glPopMatrix();
463 	Zeilen++;
464       } else { // am Ende: Linksbuendig
465 	GLfloat delta=0;
466 	if (ZeilenBreite>MaxBreite)
467 	  delta=(MaxBreite-ZeilenBreite)/(LeerzeichenAnzahl+0.0);
468 	glPushMatrix();
469 	glTranslatef(0,-Zeilen*.7,0);
470 	GLint c;
471 	Pos=Anfang;
472 	while ((c=Zeichenkette[Pos++])) {
473 	  if (c<0) c+=256;
474 	  glTranslatef(-links[c]/64.0,0,0);
475 	  glCallList(DisplayListAnfang+c+256);
476 	  glTranslatef((rechts[c]+4)/64.0,0,0);
477 	  if (c==32) glTranslatef(delta,0,0);
478 	}
479 	glPopMatrix();
480 	glPushMatrix();
481 	glTranslatef(0,-Zeilen*.7,0);
482 	Pos=Anfang;
483 	while ((c=Zeichenkette[Pos++])) {
484 	  if (c<0) c+=256;
485 	  glTranslatef(-links[c]/64.0,0,0);
486 	  glCallList(DisplayListAnfang+c);
487 	  glTranslatef((rechts[c]+4)/64.0,0,0);
488 	  if (c==32) glTranslatef(delta,0,0);
489 	}
490 	glPopMatrix();
491 	Zeilen++;
492 	break;
493       }
494     }
495 
496     glDisable(GL_TEXTURE_2D);
497 
498     glEndList();
499 
500   }
501 
502 }
503 
Maustaste(int Taste,int Richtung,int x_,int y_)504 GLint Textfeld::Maustaste(int Taste,int Richtung,int x_,int y_) {
505   GLfloat xf=16.0*x_/glutGet(GLUT_WINDOW_WIDTH);
506   GLfloat yf=12.0-12.0*y_/glutGet(GLUT_WINDOW_HEIGHT);
507   if (soll_Alpha>0.0 &&
508       Signal!=0 &&
509       x<=xf && xf<=x+Hoehe*Aspekt && y<=yf && yf<=y+Hoehe) {
510     if (Taste==GLUT_LEFT_BUTTON) {
511       if (Richtung==GLUT_DOWN) {
512 	Angewaehlt();
513 	return -1;
514       } else {
515 	return Signal;
516       }
517     } else {
518       return -1;
519     }
520   } else {
521     return 0;
522   }
523 }
524 
TextfeldHoehe()525 GLfloat Textfeld::TextfeldHoehe() {
526   return .7*Zeilen;
527 }
528 
529 
SetzeMaxBreite(GLfloat mb)530 void Textfeld::SetzeMaxBreite(GLfloat mb) {
531   if (mb<0) mb=0;
532   MaxBreite=mb;
533 }
534