TORCS  1.3.9
The Open Racing Car Simulator
openglconfig.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 
3  file : openglconfig.cpp
4  created : Fri Jun 3 12:52:07 CET 2004
5  copyright : (C) 2005 Bernhard Wymann
6  email : berniw@bluewin.ch
7  version : $Id$
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  ***************************************************************************/
19 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <tgfclient.h>
28 #include <raceinit.h>
29 #include <graphic.h>
30 #include <glfeatures.h>
31 #include <portability.h>
32 #include "openglconfig.h"
33 
34 static float LabelColor[] = {1.0, 0.0, 1.0, 1.0};
35 
36 // Texture compression.
37 static const char *textureCompressOptionList[] = {
40 };
41 
43 static int curOptionTextComp = 0;
45 
46 // Texture sizing, order of list is important, do not change.
47 static int textureSizeOptionList[] = {8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384};
49 static int curOptionTextSize = 0;
51 static const int defaultTextSize = 64; // In case everything goes wrong.
52 
53 // gui screen handles.
54 static void *scrHandle = NULL;
55 static void *prevHandle = NULL;
56 
57 
58 // Read OpenGL configuration.
59 static void readOpenGLCfg(void)
60 {
61  int i;
62  const int BUFSIZE = 1024;
63  char buf[BUFSIZE];
64 
65  snprintf(buf, BUFSIZE, "%s%s", GetLocalDir(), GR_PARAM_FILE);
66  void *paramHandle = GfParmReadFile(buf, GFPARM_RMODE_REREAD | GFPARM_RMODE_CREAT);
67 
68  // Read texture compression parameters.
69  const char *optionName = GfParmGetStr(paramHandle, GR_SCT_GLFEATURES, GR_ATT_TEXTURECOMPRESSION, textureCompressOptionList[0]);
70  for (i = 0; i < nbOptionsTextComp; i++) {
71  if (strcmp(optionName, textureCompressOptionList[i]) == 0) {
73  break;
74  }
75  }
76  if (isCompressARBAvailable()) {
78  }
79 
80  // Read texture sizing parameters.
81  int maxsizenb = 0;
82  int sizelimit = getGLTextureMaxSize();
83  int tsize = (int) GfParmGetNum(paramHandle, GR_SCT_GLFEATURES, GR_ATT_TEXTURESIZE, (char*)NULL, (tdble) sizelimit);
84  bool found = false;
85 
86  for (i = 0; i < nbOptionsTextSize; i++) {
87  if (textureSizeOptionList[i] <= sizelimit) {
88  maxsizenb = i;
89  } else {
90  break;
91  }
92  }
93 
94  // Limit to available sizes.
95  nbOptionsTextSize = maxsizenb+1;
96 
97  for (i = 0; i < nbOptionsTextSize; i++) {
98  if (textureSizeOptionList[i] == tsize) {
100  found = true;
101  break;
102  }
103  }
104 
105  if (!found) {
106  // Should never come here if there is no bug in OpenGL.
107  tsize = defaultTextSize;
108  for (i = 0; i < nbOptionsTextSize; i++) {
109  if (textureSizeOptionList[i] == tsize) {
110  curOptionTextSize = i;
111  break;
112  }
113  }
114  }
115  snprintf(buf, BUFSIZE, "%d", textureSizeOptionList[curOptionTextSize]);
117 
118  GfParmReleaseHandle(paramHandle);
119 }
120 
121 
122 // Save the choosen values in the corresponding parameter file.
123 static void saveOpenGLOption(void *)
124 {
125  const int BUFSIZE = 1024;
126  char buf[BUFSIZE];
127  snprintf(buf, BUFSIZE, "%s%s", GetLocalDir(), GR_PARAM_FILE);
128  void *paramHandle = GfParmReadFile(buf, GFPARM_RMODE_REREAD | GFPARM_RMODE_CREAT);
129 
130  // Texture compression.
132  // Texture sizing.
134 
135  GfParmWriteFile(NULL, paramHandle, "graph");
136  GfParmReleaseHandle(paramHandle);
137 
138  // Return to previous screen.
142  return;
143 }
144 
145 
146 // Toggle texture compression state enabled/disabled.
147 static void changeTextureCompressState(void *vp)
148 {
149  if (vp == 0) {
151  if (curOptionTextComp < 0) {
153  }
154  } else {
157  curOptionTextComp = 0;
158  }
159  }
161 }
162 
163 
164 // Scroll through texture sizes smaller or equal the system limit.
165 static void changeTextureSizeState(void *vp)
166 {
167  const int BUFSIZE = 1024;
168  char buf[BUFSIZE];
169 
170  long delta = (long)vp;
171  curOptionTextSize += delta;
172  if (curOptionTextSize < 0) {
174  } else if (curOptionTextSize >= nbOptionsTextSize) {
176  }
177 
178  snprintf(buf, BUFSIZE, "%d", textureSizeOptionList[curOptionTextSize]);
180 }
181 
182 
183 static void onActivate(void * /* dummy */)
184 {
185  readOpenGLCfg();
186 }
187 
188 
189 // OpenGL menu
190 void * OpenGLMenuInit(void *prevMenu)
191 {
192  int y, dy;
193 
194  // Has screen already been created?
195  if (scrHandle) {
196  return scrHandle;
197  }
198 
199  prevHandle = prevMenu;
200 
201  scrHandle = GfuiScreenCreateEx((float*)NULL, NULL, onActivate, NULL, (tfuiCallback)NULL, 1);
202  GfuiTitleCreate(scrHandle, "OpenGL Options", 0);
203  GfuiScreenAddBgImg(scrHandle, "data/img/splash-simucfg.png");
204 
205  y = 400;
206  dy = 30;
207 
208  const int xleft = 160; // Center of left elements.
209  const int xright = 480; // Center of right elements.
210  const int width = 240; // Width of elements.
211 
212  y -= dy;
213 
214  // Texture compression.
215  GfuiLabelCreate(scrHandle, "Texture Compression", GFUI_FONT_LARGE, xleft, 400, GFUI_ALIGN_HC_VB, 0);
216 
217  if (isCompressARBAvailable()) {
218  GfuiGrButtonCreate(scrHandle, "data/img/arrow-left.png", "data/img/arrow-left.png",
219  "data/img/arrow-left.png", "data/img/arrow-left-pushed.png",
220  xleft-width/2, y, GFUI_ALIGN_HC_VB, 1,
221  (void*)-1, changeTextureCompressState,
222  NULL, (tfuiCallback)NULL, (tfuiCallback)NULL);
223 
224  GfuiGrButtonCreate(scrHandle, "data/img/arrow-right.png", "data/img/arrow-right.png",
225  "data/img/arrow-right.png", "data/img/arrow-right-pushed.png",
226  xleft+width/2, y, GFUI_ALIGN_HC_VB, 1,
227  (void*)1, changeTextureCompressState,
228  NULL, (tfuiCallback)NULL, (tfuiCallback)NULL);
229 
232  } else {
233  GfuiLabelCreate(scrHandle, "not available", GFUI_FONT_LARGE_C, xleft, y, GFUI_ALIGN_HC_VB, 0);
234  }
235 
236 
237  // Texture sizing.
238  GfuiLabelCreate(scrHandle, "Texture Size Limit", GFUI_FONT_LARGE, xright, 400, GFUI_ALIGN_HC_VB, 0);
239 
240  GfuiGrButtonCreate(scrHandle, "data/img/arrow-left.png", "data/img/arrow-left.png",
241  "data/img/arrow-left.png", "data/img/arrow-left-pushed.png",
242  xright-width/2, y, GFUI_ALIGN_HC_VB, 0,
243  (void*)-1, changeTextureSizeState,
244  NULL, (tfuiCallback)NULL, (tfuiCallback)NULL);
245 
246  GfuiGrButtonCreate(scrHandle, "data/img/arrow-right.png", "data/img/arrow-right.png",
247  "data/img/arrow-right.png", "data/img/arrow-right-pushed.png",
248  xright+width/2, y, GFUI_ALIGN_HC_VB, 0,
249  (void*)1, changeTextureSizeState,
250  NULL, (tfuiCallback)NULL, (tfuiCallback)NULL);
251 
254 
255 
257  NULL, saveOpenGLOption, NULL, (tfuiCallback)NULL, (tfuiCallback)NULL);
258 
260  prevMenu, GfuiScreenActivate, NULL, (tfuiCallback)NULL, (tfuiCallback)NULL);
261 
262  GfuiAddKey(scrHandle, 13, "Save", NULL, saveOpenGLOption, NULL);
263  GfuiAddKey(scrHandle, 27, "Cancel Selection", prevMenu, GfuiScreenActivate, NULL);
264  GfuiAddSKey(scrHandle, GLUT_KEY_F12, "Screen-Shot", NULL, GfuiScreenShot, NULL);
265  GfuiAddSKey(scrHandle, GLUT_KEY_LEFT, "Previous Option in list", (void*)0, changeTextureCompressState, NULL);
266  GfuiAddSKey(scrHandle, GLUT_KEY_RIGHT, "Next Option in list", (void*)1, changeTextureCompressState, NULL);
267 
268  readOpenGLCfg();
269 
270  return scrHandle;
271 }
static int curOptionTextComp
int GfParmSetStr(void *handle, const char *path, const char *key, const char *val)
Set a string parameter in the parameter set handle.
Definition: params.cpp:2477
#define GFUI_MOUSE_UP
Definition: tgfclient.h:80
static void * prevHandle
void * GfParmReadFile(const char *file, int mode)
Read parameter set from file and return handle to parameter set.
Definition: params.cpp:1157
int GfuiButtonCreate(void *scr, const char *text, int font, int x, int y, int width, int align, int mouse, void *userDataOnPush, tfuiCallback onPush, void *userDataOnFocus, tfuiCallback onFocus, tfuiCallback onFocusLost)
Add a button to a screen.
Definition: guibutton.cpp:248
static const int nbOptionsTextComp
static const int defaultTextSize
char * GetLocalDir(void)
Definition: tgf.cpp:231
static int TextureCompressOptionId
void GfParmReleaseHandle(void *parmHandle)
Release given parameter set handle parmHandle.
Definition: params.cpp:1834
void GfuiAddKey(void *scr, unsigned char key, const char *descr, void *userData, tfuiCallback onKeyPressed, tfuiCallback onKeyReleased)
Add a Keyboard callback to a screen.
Definition: gui.cpp:742
static const char * textureCompressOptionList[]
int GfuiLabelCreate(void *scr, const char *text, int font, int x, int y, int align, int maxlen)
Add a label to a screen.
Definition: guilabel.cpp:142
static void onActivate(void *)
void GfuiScreenAddBgImg(void *scr, const char *filename)
Add an image background to a screen.
Definition: gui.cpp:961
void getGLTextureMaxSize(int &result)
Definition: glfeatures.cpp:111
int GfParmWriteFile(const char *file, void *parmHandle, const char *name)
Write parameter set into file.
Definition: params.cpp:1610
#define GR_ATT_TEXTURECOMPRESSION
Definition: graphic.h:53
const char * GfParmGetStr(void *parmHandle, const char *path, const char *key, const char *deflt)
Get a string parameter from the parameter set handle.
Definition: params.cpp:2311
#define GFUI_FONT_LARGE_C
Definition: tgfclient.h:172
The Gaming Framework API (client part).
bool isCompressARBAvailable(void)
Definition: glfeatures.cpp:93
#define GFUI_FONT_LARGE
Definition: tgfclient.h:168
void * GfuiScreenCreateEx(float *bgColor, void *userDataOnActivate, tfuiCallback onActivate, void *userDataOnDeactivate, tfuiCallback onDeactivate, int mouseAllowed)
Create a screen.
Definition: gui.cpp:578
#define GR_ATT_TEXTURECOMPRESSION_DISABLED
Definition: graphic.h:55
static float LabelColor[]
void GfuiLabelSetText(void *scr, int id, const char *text)
Change the text of a label.
Definition: guilabel.cpp:212
float tdble
Floating point type used in TORCS.
Definition: tgf.h:48
#define GFPARM_RMODE_REREAD
reread the parameters from file and release the previous ones
Definition: tgf.h:266
void GfuiLabelSetColor(void *scr, int id, float *color)
Change the color of a label.
Definition: guilabel.cpp:239
Graphic Module Interface Definition.
void updateUserTextureMaxSize(void)
Definition: glfeatures.cpp:134
static int TextureSizeOptionId
void(* tfuiCallback)(void *)
Definition: tgfclient.h:105
void GfuiScreenActivate(void *screen)
Activate a screen and make it current.
Definition: gui.cpp:467
void GfuiAddSKey(void *scr, int key, const char *descr, void *userData, tfuiCallback onKeyPressed, tfuiCallback onKeyReleased)
Add a Special Keyboard shortcut to the screen.
Definition: gui.cpp:816
void updateCompressARBEnabled(void)
Definition: glfeatures.cpp:86
static int curOptionTextSize
#define GR_PARAM_FILE
Definition: graphic.h:42
int GfParmSetNum(void *handle, const char *path, const char *key, const char *unit, tdble val)
Set a numerical parameter in the parameter set handle.
Definition: params.cpp:2586
#define GFUI_ALIGN_HC_VB
Definition: tgfclient.h:72
#define GR_ATT_TEXTURESIZE
Definition: graphic.h:61
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:2392
#define GR_SCT_GLFEATURES
Definition: graphic.h:52
void GfuiScreenShot(void *)
Save a screen shot in png format.
Definition: gui.cpp:913
static Vector y[4]
Definition: Convex.cpp:56
void * OpenGLMenuInit(void *prevMenu)
int GfuiTitleCreate(void *scr, const char *text, int maxlen)
Add a Title to the screen.
Definition: guilabel.cpp:170
int GfuiGrButtonCreate(void *scr, const char *disabled, const char *enabled, const char *focused, const char *pushed, int x, int y, int align, int mouse, void *userDataOnPush, tfuiCallback onPush, void *userDataOnFocus, tfuiCallback onFocus, tfuiCallback onFocusLost)
Add a graphical button to a screen.
Definition: guibutton.cpp:62
static void changeTextureCompressState(void *vp)
static void * scrHandle
static int textureSizeOptionList[]
static void readOpenGLCfg(void)
static int nbOptionsTextSize
static void saveOpenGLOption(void *)
#define GFPARM_RMODE_CREAT
Create the file if doesn&#39;t exist.
Definition: tgf.h:267
#define GR_ATT_TEXTURECOMPRESSION_ENABLED
Definition: graphic.h:54
static void changeTextureSizeState(void *vp)