TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
gui.cpp
Go to the documentation of this file.
1/***************************************************************************
2 gui.cpp -- gui
3 -------------------
4 created : Fri Aug 13 22:01:33 CEST 1999
5 copyright : (C) 1999-2026 by Eric EspiƩ, Bernhard Wymann
6 email : berniw@bluewin.ch
7
8 ***************************************************************************/
9
10/***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18
22
23#include <stdlib.h>
24#include <stdio.h>
25#include <time.h>
26
27#include <tgfclient.h>
28#include "gui.h"
29
30#include <portability.h>
31
32tGfuiScreen *GfuiScreen; /* current screen */
33static int GfuiMouseVisible = 1;
35
37
39
40static int ScrW, ScrH, ViewW, ViewH;
41
43static double LastTimeClick;
44#define REPEAT1 1.0
45#define REPEAT2 0.2
46
48gfuiMakeAction(void *userData, tfuiCallback callback)
49{
50 if (callback == NULL) {
51 return tGfuiAction();
52 }
53
54 return [userData, callback] {
55 callback(userData);
56 };
57}
58
61{
62 if (!action) {
63 return NULL;
64 }
65
66 return new tGfuiAction(action);
67}
68
69void
71{
72 if (action != NULL) {
73 tGfuiAction runAction = *action;
74 runAction();
75 }
76}
77
78void
80{
81 delete action;
82}
83
84static void
86{
87 const int BUFSIZE = 1024;
88 char buf[BUFSIZE];
89
90 void *hdle;
91 int i, j;
93 const char *clr[GFUI_COLORNB] = {
100 };
101
102 snprintf(buf, BUFSIZE, "%s%s", GetLocalDir(), GFSCR_CONF_FILE);
104
105 for (i = 0; i < GFUI_COLORNB; i++) {
106 for (j = 0; j < 4; j++) {
107 snprintf(buf, BUFSIZE, "%s/%s/%s", GFSCR_SECT_MENUCOL, GFSCR_LIST_COLORS, clr[i]);
108 GfuiColor[i][j] = GfParmGetNum(hdle, buf, rgba[j], (char*)NULL, 1.0);
109 }
110 }
111
113
114 /* Remove the X11/Windows cursor */
115 if (!GfuiMouseHW) {
116 glutSetCursor(GLUT_CURSOR_NONE);
117 }
118
120}
121
122
123void
125{
127 gfuiHelpInit();
132
133/* glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF); */
134}
135
136
141void
143{
144}
145
151void
153{
154 double curtime = GfTimeClock();
155
156 if ((curtime - LastTimeClick) > DelayRepeat) {
158 LastTimeClick = curtime;
159 if (GfuiScreen->mouse == 1) {
160 /* button down */
163 glutPostRedisplay();
164 }
165 }
166}
167
174void
176{
177 tGfuiObject *curObj;
178
179 glDisable(GL_DEPTH_TEST);
180 glDisable(GL_LIGHTING);
181 glDisable(GL_TEXTURE_2D);
182 glDisable(GL_CULL_FACE);
183 glDisable(GL_ALPHA_TEST);
184 glEnable(GL_BLEND);
185 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
186
188
189 glViewport((ScrW-ViewW) / 2, (ScrH-ViewH) / 2, ViewW, ViewH);
190 glMatrixMode(GL_PROJECTION);
191 glLoadIdentity();
192 gluOrtho2D(0, GfuiScreen->width, 0, GfuiScreen->height);
193 glMatrixMode(GL_MODELVIEW);
194 glLoadIdentity();
195
196 if (GfuiScreen->bgColor[3] != 0.0) {
197 glClearColor(GfuiScreen->bgColor[0],
198 GfuiScreen->bgColor[1],
199 GfuiScreen->bgColor[2],
200 GfuiScreen->bgColor[3]);
201 glClear(GL_COLOR_BUFFER_BIT);
202 }
203
204 if (glIsTexture(GfuiScreen->bgImage) == GL_TRUE) {
205 GLfloat tx1 = 0.0f, tx2 = 1.0f, ty1 = 0.0f, ty2 = 1.0f;
206
207 // All background images are 16:10 images which are stored as quadratic images.
208 // Compute texture coordinates to ensure proper unskewed/unstretched display of
209 // image content.
210 tdble rfactor = (16.0f*ViewH)/(10.0f*ViewW);
211 if (rfactor >= 1.0f) {
212 // Aspect ratio of view is smaller than 16:10, "cut off" sides
213 tdble tdx = (1.0f-1.0f/rfactor)/2.0f;
214 tx1 += tdx;
215 tx2 -= tdx;
216 } else {
217 // Aspect ratio of view is larger than 16:10, "cut off" top and bottom
218 tdble tdy = (1.0f-rfactor)/2.0f;
219 ty1 += tdy;
220 ty2 -= tdy;
221 }
222
223 glDisable(GL_BLEND);
224 glEnable(GL_TEXTURE_2D);
225 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
226 glColor3f(0.0, 0.0, 1.0);
227 glBindTexture(GL_TEXTURE_2D, GfuiScreen->bgImage);
228 glBegin(GL_QUADS);
229 glTexCoord2f(tx1, ty1); glVertex3f(0.0, 0.0, 0.0);
230 glTexCoord2f(tx1, ty2); glVertex3f(0.0, GfuiScreen->height, 0.0);
231 glTexCoord2f(tx2, ty2); glVertex3f(GfuiScreen->width, GfuiScreen->height, 0.0);
232 glTexCoord2f(tx2, ty1); glVertex3f(GfuiScreen->width, 0.0, 0.0);
233 glEnd();
234 glDisable(GL_TEXTURE_2D);
235 glEnable(GL_BLEND);
236 }
237
238 curObj = GfuiScreen->objects;
239 if (curObj) {
240 do {
241 curObj = curObj->next;
242 GfuiDraw(curObj);
243 } while (curObj != GfuiScreen->objects);
244 }
245
246 if (!GfuiMouseHW && GfuiMouseVisible && GfuiScreen->mouseAllowed) {
248 }
249
250 glDisable(GL_BLEND);
251 glutSwapBuffers();
252}
253
258void
260{
261 GfuiScreen->mouseAllowed = 0;
262}
263
268void
270{
271 GfuiScreen->mouseAllowed = 1;
272}
273
277void
279{
280 GfuiMouseHW = 1;
281}
282
283static void
284gfuiKeyboard(unsigned char key, int /* x */, int /* y */)
285{
286 tGfuiKey *curKey;
287 int modifier;
288 tGfuiObject *obj;
289
290 modifier = glutGetModifiers();
291
292 /* user preempt key */
293 if (GfuiScreen->onKeyAction && GfuiScreen->onKeyAction(key, modifier, GFUI_KEY_DOWN)) {
294 return;
295 }
296
297 /* now see the user's defined keys */
298 if (GfuiScreen->userKeys != NULL) {
299 curKey = GfuiScreen->userKeys;
300 do {
301 curKey = curKey->next;
302 if ((curKey->key == key) && ((curKey->modifier == 0) || (curKey->modifier & modifier) != 0)) {
304 break;
305 }
306 } while (curKey != GfuiScreen->userKeys);
307 }
308
309 obj = GfuiScreen->hasFocus;
310 if (obj != NULL) {
311 switch (obj->widget) {
312 case GFUI_EDITBOX:
313 gfuiEditboxKey(obj, (int)key, modifier);
314 break;
315 }
316 }
317 glutPostRedisplay();
318}
319
320static void
321gfuiSpecial(int key, int /* x */, int /* y */)
322{
323 tGfuiKey *curKey;
324 int modifier;
325 tGfuiObject *obj;
326
327 modifier = glutGetModifiers();
328
329 /* user preempt key */
330 if (GfuiScreen->onSKeyAction && GfuiScreen->onSKeyAction(key, modifier, GFUI_KEY_DOWN)) {
331 return;
332 }
333
334 /* now see the user's defined keys */
335 if (GfuiScreen->userSpecKeys != NULL) {
336 curKey = GfuiScreen->userSpecKeys;
337 do {
338 curKey = curKey->next;
339 if ((curKey->specialkey == key) && ((curKey->modifier == 0) || (curKey->modifier & modifier) != 0)) {
341 break;
342 }
343 } while (curKey != GfuiScreen->userSpecKeys);
344 }
345
346 obj = GfuiScreen->hasFocus;
347 if (obj != NULL) {
348 switch (obj->widget) {
349 case GFUI_EDITBOX:
350 gfuiEditboxKey(obj, key + 256, modifier);
351 break;
352 }
353 }
354 glutPostRedisplay();
355}
356
357static void
358gfuiKeyboardUp(unsigned char key, int /* x */, int /* y */)
359{
360 tGfuiKey *curKey;
361 int modifier;
362
363 modifier = glutGetModifiers();
364
365 /* user preempt key */
366 if (GfuiScreen->onKeyAction && GfuiScreen->onKeyAction(key, modifier, GFUI_KEY_UP)) {
367 return;
368 }
369
370 /* now see the user's defined keys */
371 if (GfuiScreen->userKeys != NULL) {
372 curKey = GfuiScreen->userKeys;
373 do {
374 curKey = curKey->next;
375 if ((curKey->key == key) && ((curKey->modifier == 0) || (curKey->modifier & modifier) != 0)) {
377 break;
378 }
379 } while (curKey != GfuiScreen->userKeys);
380 }
381
382 glutPostRedisplay();
383}
384
385static void
386gfuiSpecialUp(int key, int /* x */, int /* y */)
387{
388 tGfuiKey *curKey;
389 int modifier;
390
391 modifier = glutGetModifiers();
392
393 /* user preempt key */
394 if (GfuiScreen->onSKeyAction && GfuiScreen->onSKeyAction(key, modifier, GFUI_KEY_UP)) {
395 return;
396 }
397
398 /* now see the user's defined keys */
399 if (GfuiScreen->userSpecKeys != NULL) {
400 curKey = GfuiScreen->userSpecKeys;
401 do {
402 curKey = curKey->next;
403 if ((curKey->specialkey == key) && ((curKey->modifier == 0) || (curKey->modifier & modifier) != 0)) {
405 break;
406 }
407 } while (curKey != GfuiScreen->userSpecKeys);
408 }
409
410 glutPostRedisplay();
411}
412
420{
421 return &GfuiMouse;
422}
423
430void GfuiMouseSetPos(int x, int y)
431{
432 glutWarpPointer(x, y);
433}
434
435
436static void
437gfuiMouse(int button, int state, int x, int y)
438{
439 // Check array range of button array!
440 if (button > -1 && button < 3) {
441 GfuiMouse.X = (x - (ScrW - ViewW)/2) * (int)GfuiScreen->width / ViewW;
442 GfuiMouse.Y = (ViewH - y + (ScrH - ViewH)/2) * (int)GfuiScreen->height / ViewH;
443
444 GfuiMouse.button[button] = 1 - state;
445
448 if (state == GLUT_DOWN) {
449 if (button == GLUT_RIGHT_BUTTON) {
450 GfuiScreen->mouse = 0;
451 /* GfuiMouseVisible = 1 - GfuiMouseVisible; */
453 } else {
454 GfuiScreen->mouse = 1;
457 }
458 } else {
459 GfuiScreen->mouse = 0;
461 if (button != GLUT_RIGHT_BUTTON) {
463 }
464 }
465 glutPostRedisplay();
466 }
467}
468
469static void
470gfuiMotion(int x, int y)
471{
472 GfuiMouse.X = (x - (ScrW - ViewW)/2) * (int)GfuiScreen->width / ViewW;
473 GfuiMouse.Y = (ViewH - y + (ScrH - ViewH)/2) * (int)GfuiScreen->height / ViewH;
475 gfuiMouseAction(1 - GfuiScreen->mouse);
476 glutPostRedisplay();
478}
479
480static void
482{
483 GfuiMouse.X = (x - (ScrW - ViewW)/2) * (int)GfuiScreen->width / ViewW;
484 GfuiMouse.Y = (ViewH - y + (ScrH - ViewH)/2) * (int)GfuiScreen->height / ViewH;
486 glutPostRedisplay();
487}
488
494int
496{
497 return (GfuiScreen == screen);
498}
499
507void
509{
510 if (GfuiScreen) {
511 gfuiRunAction(GfuiScreen->onDeactivateAction);
512 }
513
514 GfuiScreen = (tGfuiScreen*)screen;
515
516 glutKeyboardFunc(gfuiKeyboard);
517 glutSpecialFunc(gfuiSpecial);
518 glutKeyboardUpFunc(gfuiKeyboardUp);
519 glutSpecialUpFunc(gfuiSpecialUp);
520 glutMouseFunc(gfuiMouse);
521 glutMotionFunc(gfuiMotion);
522 glutPassiveMotionFunc(gfuiPassiveMotion);
523 glutIdleFunc((void(*)(void))NULL);
524
525 if (GfuiScreen->onlyCallback == 0) {
526 if (GfuiScreen->hasFocus == NULL) {
527 gfuiSelectNext(NULL);
528 }
529 glutDisplayFunc(GfuiDisplay);
530 } else {
531 glutDisplayFunc(GfuiDisplayNothing);
532 }
533
534 gfuiRunAction(GfuiScreen->onActivateAction);
535
536 if (GfuiScreen->onlyCallback == 0) {
537 GfuiDisplay();
538 glutPostRedisplay();
539 }
540}
541
542
548void
549GfuiScreenReplace(void *screen)
550{
551 tGfuiScreen *oldScreen = GfuiScreen;
552
553 if (oldScreen) {
554 GfuiScreenRelease(oldScreen);
555 }
556 GfuiScreenActivate(screen);
557}
558
564void
566{
567 gfuiRunAction(GfuiScreen->onDeactivateAction);
568
569 GfuiScreen = (tGfuiScreen*)NULL;
570
571 glutKeyboardFunc((void(*)(unsigned char,int,int))NULL);
572 glutSpecialFunc((void(*)(int,int,int))NULL);
573 glutKeyboardUpFunc((void(*)(unsigned char,int,int))NULL);
574 glutSpecialUpFunc((void(*)(int,int,int))NULL);
575 glutMouseFunc((void(*)(int,int,int,int))NULL);
576 glutMotionFunc((void(*)(int,int))NULL);
577 glutPassiveMotionFunc((void(*)(int,int))NULL);
578 glutIdleFunc((void(*)(void))NULL);
579 glutDisplayFunc(GfuiDisplayNothing);
580}
581
587void *
589{
590 tGfuiScreen *screen;
591
592 screen = (tGfuiScreen*)calloc(1, sizeof(tGfuiScreen));
593
594 screen->width = 640.0;
595 screen->height = 480.0;
596
597 screen->bgColor = (float*)calloc(4, sizeof(float));
598 int i;
599 for(i = 0; i < 4; i++) {
600 screen->bgColor[i] = GfuiColor[GFUI_BGCOLOR][i];
601 }
602
603 screen->mouseColor[0] = &(GfuiColor[GFUI_MOUSECOLOR1][0]);
604 screen->mouseColor[1] = &(GfuiColor[GFUI_MOUSECOLOR2][0]);
605 screen->mouseAllowed = 1;
606
607 return (void*)screen;
608}
609
618void *
620 const tGfuiAction &onActivate,
621 const tGfuiAction &onDeactivate,
622 int mouseAllowed)
623{
624 int i;
625 tGfuiScreen *screen;
626
627 screen = (tGfuiScreen*)calloc(1, sizeof(tGfuiScreen));
628
629 screen->width = 640.0;
630 screen->height = 480.0;
631
632 screen->bgColor = (float*)calloc(4, sizeof(float));
633 for(i = 0; i < 4; i++) {
634 if (bgColor != NULL) {
635 screen->bgColor[i] = bgColor[i];
636 } else {
637 screen->bgColor[i] = GfuiColor[GFUI_BGCOLOR][i];
638 }
639 }
640
641 screen->mouseColor[0] = &(GfuiColor[GFUI_MOUSECOLOR1][0]);
642 screen->mouseColor[1] = &(GfuiColor[GFUI_MOUSECOLOR2][0]);
644 screen->onDeactivateAction = gfuiCreateAction(onDeactivate);
645
646 screen->mouseAllowed = mouseAllowed;
647
648 return (void*)screen;
649}
650
651void *
652GfuiScreenCreateEx(float *bgColor,
653 void *userDataOnActivate, tfuiCallback onActivate,
654 void *userDataOnDeactivate, tfuiCallback onDeactivate,
655 int mouseAllowed)
656{
657 return GfuiScreenCreateExAction(bgColor,
658 gfuiMakeAction(userDataOnActivate, onActivate),
659 gfuiMakeAction(userDataOnDeactivate, onDeactivate),
660 mouseAllowed);
661}
662
669void
671{
672 tGfuiObject *curObject;
673 tGfuiObject *nextObject;
674 tGfuiKey *curKey;
675 tGfuiKey *nextKey;
676 tGfuiScreen *screen = (tGfuiScreen*)scr;
677
678 if (GfuiScreen == screen) {
680 }
681
682 if (glIsTexture(screen->bgImage) == GL_TRUE) {
683 glDeleteTextures(1, &screen->bgImage);
684 }
685
686 if (screen->bgColor != NULL) {
687 free(screen->bgColor);
688 screen->bgColor = NULL;
689 }
692
693 curObject = screen->objects;
694 if (curObject != NULL) {
695 do {
696 nextObject = curObject->next;
697 gfuiReleaseObject(curObject);
698 curObject = nextObject;
699 } while (curObject != screen->objects);
700 }
701
702 curKey = screen->userKeys;
703 if (curKey != NULL) {
704 do {
705 nextKey = curKey->next;
706 free(curKey->name);
707 free(curKey->descr);
710 free(curKey);
711 curKey = nextKey;
712 } while (curKey != screen->userKeys);
713 }
714 curKey = screen->userSpecKeys;
715 if (curKey != NULL) {
716 do {
717 nextKey = curKey->next;
718 free(curKey->name);
719 free(curKey->descr);
722 free(curKey);
723 curKey = nextKey;
724 } while (curKey != screen->userSpecKeys);
725 }
726 free(screen);
727}
728
735void *
737{
738 tGfuiScreen *screen;
739
740 screen = (tGfuiScreen*)calloc(1, sizeof(tGfuiScreen));
742 screen->onlyCallback = 1;
743
744 return (void*)screen;
745}
746
747void *
748GfuiHookCreate(void *userDataOnActivate, tfuiCallback onActivate)
749{
750 return GfuiHookCreateAction(gfuiMakeAction(userDataOnActivate, onActivate));
751}
752
759void
761{
762 tGfuiScreen *screen = (tGfuiScreen*)hook;
765 free(hook);
766}
767
768void
770{
771 tGfuiScreen *screen = (tGfuiScreen*)scr;
772
773 screen->onKeyAction = onKeyAction;
774}
775
776
777void
779{
780 tGfuiScreen *screen = (tGfuiScreen*)scr;
781
782 screen->onSKeyAction = onSKeyAction;
783}
784
785void
790
791
792void
797
798
808void
809GfuiAddKeyAction(void *scr, unsigned char key, const char *descr, const tGfuiAction &onKeyPressed, const tGfuiAction &onKeyReleased)
810{
811 tGfuiKey *curKey;
812 tGfuiScreen *screen = (tGfuiScreen*)scr;
813 const int BUFSIZE = 16;
814 char buf[BUFSIZE];
815
816 curKey = (tGfuiKey*)calloc(1, sizeof(tGfuiKey));
817 curKey->key = key;
818 curKey->onPressAction = gfuiCreateAction(onKeyPressed);
819 curKey->onReleaseAction = gfuiCreateAction(onKeyReleased);
820
821 if (descr == NULL) {
822 curKey->descr = strdup("");
823 } else {
824 curKey->descr = strdup(descr);
825 }
826
827 switch(key){
828 case 8:
829 curKey->name = strdup("backspace");
830 break;
831 case 9:
832 curKey->name = strdup("tab");
833 break;
834 case 13:
835 curKey->name = strdup("enter");
836 break;
837 case 27:
838 curKey->name = strdup("esc");
839 break;
840 case ' ':
841 curKey->name = strdup("space");
842 break;
843 default:
844 snprintf(buf, BUFSIZE, "%c", key);
845 curKey->name = strdup(buf);
846 break;
847 }
848
849 if (screen->userKeys == NULL) {
850 screen->userKeys = curKey;
851 curKey->next = curKey;
852 } else {
853 curKey->next = screen->userKeys->next;
854 screen->userKeys->next = curKey;
855 }
856}
857
858void
859GfuiAddKey(void *scr, unsigned char key, const char *descr, void *userData, tfuiCallback onKeyPressed, tfuiCallback onKeyReleased)
860{
861 GfuiAddKeyAction(scr, key, descr, gfuiMakeAction(userData, onKeyPressed), gfuiMakeAction(userData, onKeyReleased));
862}
863
872void
873GfuiRegisterKeyAction(unsigned char key, char *descr, const tGfuiAction &onKeyPressed, const tGfuiAction &onKeyReleased)
874{
875 GfuiAddKeyAction(GfuiScreen, key, descr, onKeyPressed, onKeyReleased);
876}
877
878void
879GfuiRegisterKey(unsigned char key, char *descr, void *userData, tfuiCallback onKeyPressed, tfuiCallback onKeyReleased)
880{
881 GfuiRegisterKeyAction(key, descr, gfuiMakeAction(userData, onKeyPressed), gfuiMakeAction(userData, onKeyReleased));
882}
883
894void
895GfuiAddSKeyAction(void *scr, int key, const char *descr, const tGfuiAction &onKeyPressed, const tGfuiAction &onKeyReleased)
896{
897 tGfuiKey *curKey;
898 tGfuiScreen *screen = (tGfuiScreen*)scr;
899
900 curKey = (tGfuiKey*)calloc(1, sizeof(tGfuiKey));
901 curKey->specialkey = key;
902 curKey->onPressAction = gfuiCreateAction(onKeyPressed);
903 curKey->onReleaseAction = gfuiCreateAction(onKeyReleased);
904
905 if (descr == NULL) {
906 curKey->descr = strdup("");
907 } else {
908 curKey->descr = strdup(descr);
909 }
910
911 switch(key) {
912 case GLUT_KEY_F1:
913 curKey->name = strdup("F1");
914 break;
915 case GLUT_KEY_F2:
916 curKey->name = strdup("F2");
917 break;
918 case GLUT_KEY_F3:
919 curKey->name = strdup("F3");
920 break;
921 case GLUT_KEY_F4:
922 curKey->name = strdup("F4");
923 break;
924 case GLUT_KEY_F5:
925 curKey->name = strdup("F5");
926 break;
927 case GLUT_KEY_F6:
928 curKey->name = strdup("F6");
929 break;
930 case GLUT_KEY_F7:
931 curKey->name = strdup("F7");
932 break;
933 case GLUT_KEY_F8:
934 curKey->name = strdup("F8");
935 break;
936 case GLUT_KEY_F9:
937 curKey->name = strdup("F9");
938 break;
939 case GLUT_KEY_F10:
940 curKey->name = strdup("F10");
941 break;
942 case GLUT_KEY_F11:
943 curKey->name = strdup("F11");
944 break;
945 case GLUT_KEY_F12:
946 curKey->name = strdup("F12");
947 break;
948 case GLUT_KEY_LEFT:
949 curKey->name = strdup("Left Arrow");
950 break;
951 case GLUT_KEY_UP:
952 curKey->name = strdup("Up Arrow");
953 break;
954 case GLUT_KEY_RIGHT:
955 curKey->name = strdup("Right Arrow");
956 break;
957 case GLUT_KEY_DOWN:
958 curKey->name = strdup("Down Arrow");
959 break;
960 case GLUT_KEY_PAGE_UP:
961 curKey->name = strdup("Page Up");
962 break;
963 case GLUT_KEY_PAGE_DOWN:
964 curKey->name = strdup("Page Down");
965 break;
966 case GLUT_KEY_HOME:
967 curKey->name = strdup("Home");
968 break;
969 case GLUT_KEY_END:
970 curKey->name = strdup("End");
971 break;
972 case GLUT_KEY_INSERT:
973 curKey->name = strdup("Insert");
974 break;
975 }
976
977
978 if (screen->userSpecKeys == NULL) {
979 screen->userSpecKeys = curKey;
980 curKey->next = curKey;
981 } else {
982 curKey->next = screen->userSpecKeys->next;
983 screen->userSpecKeys->next = curKey;
984 screen->userSpecKeys = curKey;
985 }
986}
987
988void
989GfuiAddSKey(void *scr, int key, const char *descr, void *userData, tfuiCallback onKeyPressed, tfuiCallback onKeyReleased)
990{
991 GfuiAddSKeyAction(scr, key, descr, gfuiMakeAction(userData, onKeyPressed), gfuiMakeAction(userData, onKeyReleased));
992}
993
997void
998GfuiScreenShot(void * /* notused */)
999{
1000 unsigned char *img;
1001 const int BUFSIZE = 1024;
1002 char buf[BUFSIZE];
1003 struct tm *stm;
1004 time_t t;
1005 int sw, sh, vw, vh;
1006 int len;
1007 char path[BUFSIZE];
1008
1009 len = snprintf(path, BUFSIZE, "%sscreenshots", GetLocalDir());
1010 if ((len < 0) || (len >= BUFSIZE)) {
1011 GfError("GfuiScreenShot: screenshot directory path too long\n");
1012 return;
1013 }
1014 // Ensure that screenshot directory exists.
1015 if (GfCreateDir(path) == GF_DIR_CREATED) {
1016
1017 GfScrGetSize(&sw, &sh, &vw, &vh);
1018 img = (unsigned char*)malloc(vw * vh * 3);
1019 if (img == NULL) {
1020 return;
1021 }
1022
1023 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
1024 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1025 glReadBuffer(GL_FRONT);
1026 glReadPixels((sw-vw)/2, (sh-vh)/2, vw, vh, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*)img);
1027
1028 t = time(NULL);
1029 stm = localtime(&t);
1030 len = snprintf(buf, BUFSIZE, "%s/torcs-%4d%02d%02d%02d%02d%02d.png",
1031 path,
1032 stm->tm_year+1900,
1033 stm->tm_mon+1,
1034 stm->tm_mday,
1035 stm->tm_hour,
1036 stm->tm_min,
1037 stm->tm_sec);
1038 if ((len < 0) || (len >= BUFSIZE)) {
1039 GfError("GfuiScreenShot: screenshot file path too long\n");
1040 free(img);
1041 return;
1042 }
1043 GfImgWritePngNoGamma(img, buf, vw, vh);
1044
1045 free(img);
1046 }
1047}
1048
1057void
1058GfuiScreenAddBgImg(void *scr, const char *filename)
1059{
1060 tGfuiScreen *screen = (tGfuiScreen*)scr;
1061 void *handle;
1062 float screen_gamma;
1063 GLbyte *tex;
1064 int w,h;
1065 const int BUFSIZE = 1024;
1066 char buf[BUFSIZE];
1067
1068 if (glIsTexture(screen->bgImage) == GL_TRUE) {
1069 glDeleteTextures(1, &screen->bgImage);
1070 }
1071
1072 snprintf(buf, BUFSIZE, "%s%s", GetLocalDir(), GFSCR_CONF_FILE);
1074 screen_gamma = (float)GfParmGetNum(handle, GFSCR_SECT_PROP, GFSCR_ATT_GAMMA, (char*)NULL, 2.0);
1075 tex = (GLbyte*)GfImgReadPng(filename, &w, &h, screen_gamma);
1076 if (!tex) {
1077 GfParmReleaseHandle(handle);
1078 return;
1079 }
1080
1081 glGenTextures(1, &screen->bgImage);
1082 glBindTexture(GL_TEXTURE_2D, screen->bgImage);
1083 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1084 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1085 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)(tex));
1086 free(tex);
1087 GfParmReleaseHandle(handle);
1088}
static Vector y[4]
Definition Convex.cpp:56
static int onKeyAction(unsigned char key, int, int state)
static int onSKeyAction(int key, int, int state)
static void onActivate(void)
void GfuiMouseSetHWPresent(void)
Force the hardware mouse pointer.
Definition gui.cpp:278
float tdble
Floating point type used in TORCS.
Definition tgf.h:47
void GfuiKeyEventRegisterCurrent(tfuiKeyCallback onKeyAction)
Set the ordinary-key interceptor on the currently active screen.
Definition gui.cpp:786
void * GfuiScreenCreateExAction(float *bgColor, const tGfuiAction &onActivate, const tGfuiAction &onDeactivate, int mouseAllowed)
Create a screen.
Definition gui.cpp:619
void GfuiMouseHide(void)
Disallow the software mouse cursor on the active screen.
Definition gui.cpp:259
void GfuiHookRelease(void *hook)
Release the given hook.
Definition gui.cpp:760
void * GfuiHookCreate(void *userDataOnActivate, tfuiCallback onActivate)
Create a callback-only screen hook.
Definition gui.cpp:748
void GfuiScreenRelease(void *scr)
Release the given screen.
Definition gui.cpp:670
void GfuiAddKeyAction(void *scr, unsigned char key, const char *descr, const tGfuiAction &onKeyPressed, const tGfuiAction &onKeyReleased)
Add a Keyboard callback to a screen.
Definition gui.cpp:809
void GfuiScreenAddBgImg(void *scr, const char *filename)
Add an image background to a screen.
Definition gui.cpp:1058
void GfuiSKeyEventRegisterCurrent(tfuiSKeyCallback onSKeyAction)
Set the special-key interceptor on the currently active screen.
Definition gui.cpp:793
void GfuiIdle(void)
Process held-mouse-button repeat actions on the active screen.
Definition gui.cpp:152
void GfuiDisplay(void)
Draw the active GUI screen and swap the GLUT buffers.
Definition gui.cpp:175
void GfuiAddSKeyAction(void *scr, int key, const char *descr, const tGfuiAction &onKeyPressed, const tGfuiAction &onKeyReleased)
Add a Special Keyboard shortcut to the screen.
Definition gui.cpp:895
void * GfuiScreenCreateEx(float *bgColor, void *userDataOnActivate, tfuiCallback onActivate, void *userDataOnDeactivate, tfuiCallback onDeactivate, int mouseAllowed)
Create a screen using callbacks with caller-supplied data.
Definition gui.cpp:652
void GfuiRegisterKey(unsigned char key, char *descr, void *userData, tfuiCallback onKeyPressed, tfuiCallback onKeyReleased)
Bind an ordinary key on the currently active screen.
Definition gui.cpp:879
void * GfuiHookCreateAction(const tGfuiAction &onActivate)
Create a callback hook.
Definition gui.cpp:736
void GfuiScreenReplace(void *screen)
Activate a screen and make it current plus release the current screen.
Definition gui.cpp:549
void GfuiKeyEventRegister(void *scr, tfuiKeyCallback onKeyAction)
Set the ordinary-key event interceptor for a screen.
Definition gui.cpp:769
void GfuiAddKey(void *scr, unsigned char key, const char *descr, void *userData, tfuiCallback onKeyPressed, tfuiCallback onKeyReleased)
Bind an ordinary key on a screen using callbacks with caller-supplied data.
Definition gui.cpp:859
tMouseInfo * GfuiMouseInfo(void)
Get the GUI's current mouse state.
Definition gui.cpp:419
void GfuiMouseShow(void)
Allow the software mouse cursor on the active screen.
Definition gui.cpp:269
void GfuiRegisterKeyAction(unsigned char key, char *descr, const tGfuiAction &onKeyPressed, const tGfuiAction &onKeyReleased)
Add a Keyboard callback to the current screen.
Definition gui.cpp:873
void GfuiAddSKey(void *scr, int key, const char *descr, void *userData, tfuiCallback onKeyPressed, tfuiCallback onKeyReleased)
Bind a GLUT special key, such as an arrow or function key, on a screen.
Definition gui.cpp:989
void * GfuiScreenCreate(void)
Create a new screen.
Definition gui.cpp:588
int GfuiScreenIsActive(void *screen)
Tell if the screen is active or not.
Definition gui.cpp:495
void GfuiScreenActivate(void *screen)
Activate a screen and make it current.
Definition gui.cpp:508
void GfuiSKeyEventRegister(void *scr, tfuiSKeyCallback onSKeyAction)
Set the special-key event interceptor for a screen.
Definition gui.cpp:778
void GfuiDisplayNothing(void)
GLUT display callback that performs no drawing or buffer swap.
Definition gui.cpp:142
void GfuiScreenDeactivate(void)
Deactivate the current screen.
Definition gui.cpp:565
void GfuiMouseSetPos(int x, int y)
Set the mouse position.
Definition gui.cpp:430
int GfImgWritePngNoGamma(unsigned char *img, const char *filename, int width, int height)
Write a framebuffer capture to a png image on disk without gamma metadata.
Definition img.cpp:292
unsigned char * GfImgReadPng(const char *filename, int *widthp, int *heightp, float screen_gamma)
Load an image from disk to a buffer in RGBA mode.
Definition img.cpp:53
tdble GfParmGetNum(void *handle, const char *path, const char *key, const char *unit, tdble deflt)
Get a numerical parameter from the parameter set handle.
Definition params.cpp:2534
void GfParmReleaseHandle(void *parmHandle)
Release given parameter set handle parmHandle.
Definition params.cpp:1963
void * GfParmReadFile(const char *file, int mode)
Read parameter set from file and return handle to parameter set.
Definition params.cpp:1173
void GfuiScreenShot(void *)
Save a screen shot in png format.
Definition gui.cpp:998
void GfScrGetSize(int *scrw, int *scrh, int *vieww, int *viewh)
Get the screen and viewport sizes.
Definition screen.cpp:529
tGfuiScreen * GfuiScreen
Definition gui.cpp:32
static int ScrH
Definition gui.cpp:40
static void gfuiSpecial(int key, int, int)
Definition gui.cpp:321
void gfuiReleaseAction(tGfuiAction *action)
Definition gui.cpp:79
static void gfuiKeyboard(unsigned char key, int, int)
Definition gui.cpp:284
static void gfuiKeyboardUp(unsigned char key, int, int)
Definition gui.cpp:358
tGfuiAction gfuiMakeAction(void *userData, tfuiCallback callback)
Definition gui.cpp:48
void gfuiInit(void)
Definition gui.cpp:124
static void gfuiPassiveMotion(int x, int y)
Definition gui.cpp:481
static tdble DelayRepeat
Definition gui.cpp:42
static int ViewW
Definition gui.cpp:40
float GfuiColor[GFUI_COLORNB][4]
Definition gui.cpp:38
tGfuiAction * gfuiCreateAction(const tGfuiAction &action)
Definition gui.cpp:60
static double LastTimeClick
Definition gui.cpp:43
#define REPEAT1
Definition gui.cpp:44
void gfuiRunAction(const tGfuiAction *action)
Definition gui.cpp:70
static void gfuiMotion(int x, int y)
Definition gui.cpp:470
tMouseInfo GfuiMouse
Definition gui.cpp:34
static void gfuiColorInit(void)
Definition gui.cpp:85
static int ScrW
Definition gui.cpp:40
static void gfuiSpecialUp(int key, int, int)
Definition gui.cpp:386
static int ViewH
Definition gui.cpp:40
static void gfuiMouse(int button, int state, int x, int y)
Definition gui.cpp:437
#define REPEAT2
Definition gui.cpp:45
static int GfuiMouseVisible
Definition gui.cpp:33
int GfuiMouseHW
Definition gui.cpp:36
void gfuiEditboxKey(tGfuiObject *obj, int key, int modifier)
Definition guiedit.cpp:274
void GfuiDrawCursor()
Definition guiobject.cpp:76
struct GfuiKey tGfuiKey
#define GFUI_COLORNB
Definition gui.h:24
void GfuiDraw(tGfuiObject *obj)
void gfuiLoadFonts(void)
Definition guifont.cpp:65
#define GFUI_BGCOLOR
Definition gui.h:27
void gfuiSelectNext(void *)
void gfuiReleaseObject(tGfuiObject *curObject)
void gfuiButtonInit(void)
Definition guibutton.cpp:34
#define GFUI_MOUSECOLOR1
Definition gui.h:39
struct GfuiObject tGfuiObject
void gfuiLabelInit(void)
Definition guilabel.cpp:32
void gfuiObjectInit(void)
Definition guiobject.cpp:27
void gfuiUpdateFocus()
#define GFUI_MOUSECOLOR2
Definition gui.h:40
void gfuiHelpInit(void)
Definition guihelp.cpp:36
void gfuiMouseAction(int action)
double GfTimeClock(void)
Get the time in seconds.
Definition os.cpp:50
#define GFSCR_ATTR_BLUE
#define GFSCR_CONF_FILE
#define GFSCR_ELT_EDITCURSORCLR
#define GFSCR_ELT_BGSELSCROLLIST
#define GFSCR_ELT_BGBTNFOCUS
#define GFSCR_ELT_BTNCLICK
#define GFSCR_ELT_BTNENABLED
#define GFSCR_ELT_BGSCROLLIST
#define GFSCR_ELT_TITLECOLOR
#define GFSCR_SECT_PROP
#define GFSCR_ATT_GAMMA
#define GFSCR_ELT_LABELCOLOR
#define GFSCR_ATTR_GREEN
#define GFSCR_ELT_BGBTNCLICK
#define GFSCR_ELT_BGBTNENABLED
#define GFSCR_ELT_HELPCOLOR2
#define GFSCR_ELT_TIPCOLOR
#define GFSCR_ELT_BTNFOCUS
#define GFSCR_ELT_BGCOLOR
#define GFSCR_ATTR_RED
#define GFSCR_ELT_SCROLLIST
#define GFSCR_ELT_MOUSECOLOR2
#define GFSCR_SECT_MENUCOL
#define GFSCR_ATTR_ALPHA
#define GFSCR_LIST_COLORS
#define GFSCR_ELT_MOUSECOLOR1
#define GFSCR_ELT_HELPCOLOR1
#define GFSCR_ELT_BGBTNDISABLED
#define GFSCR_ELT_SELSCROLLIST
#define GFSCR_ELT_BTNDISABLED
int specialkey
Definition gui.h:221
struct GfuiKey * next
Definition gui.h:225
tGfuiAction * onReleaseAction
Definition gui.h:224
int modifier
Definition gui.h:222
unsigned char key
Definition gui.h:218
char * name
Definition gui.h:219
char * descr
Definition gui.h:220
tGfuiAction * onPressAction
Definition gui.h:223
int widget
Definition gui.h:193
struct GfuiObject * next
Definition gui.h:211
tGfuiObject * objects
Definition gui.h:236
tGfuiKey * userKeys
Definition gui.h:241
float height
Definition gui.h:231
float * bgColor
Definition gui.h:232
tfuiKeyCallback onKeyAction
Definition gui.h:247
int onlyCallback
Definition gui.h:259
tGfuiAction * onActivateAction
Definition gui.h:243
GLuint bgImage
Definition gui.h:233
float width
Definition gui.h:231
tGfuiAction * onDeactivateAction
Definition gui.h:244
int mouseAllowed
Definition gui.h:252
float * mouseColor[2]
Definition gui.h:253
tGfuiKey * userSpecKeys
Definition gui.h:242
tfuiSKeyCallback onSKeyAction
Definition gui.h:248
int GfCreateDir(char *path)
Create directory for given path recursively, so all missing parent directories are created as well.
Definition tgf.cpp:308
char * GetLocalDir(void)
Definition tgf.cpp:230
#define GFPARM_RMODE_CREAT
Allow an empty in-memory set on file-open failure; does not write a file.
Definition tgf.h:266
#define GFPARM_RMODE_STD
Return a new handle sharing cached data for this filename, if available.
Definition tgf.h:264
#define GF_DIR_CREATED
Definition tgf.h:622
#define GfError
Definition tgf.h:350
The Gaming Framework API (client part).
void(* tfuiCallback)(void *)
Definition tgfclient.h:108
struct MouseInfo tMouseInfo
int(* tfuiKeyCallback)(unsigned char key, int modifier, int state)
return 1 to prevent normal key computing
Definition tgfclient.h:110
#define GFUI_KEY_UP
Definition tgfclient.h:86
int(* tfuiSKeyCallback)(int key, int modifier, int state)
return 1 to prevent normal key computing
Definition tgfclient.h:111
#define GFUI_KEY_DOWN
Definition tgfclient.h:87
std::function< void()> tGfuiAction
Definition tgfclient.h:107
#define GFUI_EDITBOX
Definition tgfclient.h:68