TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
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-2026 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
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <tgfclient.h>
25#include <raceinit.h>
26#include <graphic.h>
27#include <glfeatures.h>
28#include <portability.h>
29#include "openglconfig.h"
30
31static float LabelColor[] = {1.0, 0.0, 1.0, 1.0};
32
33// Texture compression.
38
40static int curOptionTextComp = 0;
42
43// Texture sizing, order of list is important, do not change.
44static int textureSizeOptionList[] = {8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384};
46static int curOptionTextSize = 0;
48static const int defaultTextSize = 64; // In case everything goes wrong.
49
50// Texture anisotropic filtering, order of list is important, do not change.
51static const char *textureAnisotropyOptionLabelList[] = {
52 "disabled",
53 "2x",
54 "4x",
55 "8x",
56 "16x"
57};
58static int textureAnisotropyOptionList[] = {0, 2, 4, 8, 16};
63
64// gui screen handles.
65static void *scrHandle = NULL;
66static void *prevHandle = NULL;
67
68
69// Read OpenGL configuration.
70static void readOpenGLCfg(void)
71{
72 int i;
73 const int BUFSIZE = 1024;
74 char buf[BUFSIZE];
75
76 snprintf(buf, BUFSIZE, "%s%s", GetLocalDir(), GR_PARAM_FILE);
77 void *paramHandle = GfParmReadFile(buf, GFPARM_RMODE_REREAD | GFPARM_RMODE_CREAT);
78
79 // Read texture compression parameters.
81 for (i = 0; i < nbOptionsTextComp; i++) {
82 if (strcmp(optionName, textureCompressOptionList[i]) == 0) {
84 break;
85 }
86 }
89 }
90
91 // Read texture sizing parameters.
92 int maxsizenb = 0;
93 int sizelimit = getGLTextureMaxSize();
94 int tsize = (int) GfParmGetNum(paramHandle, GR_SCT_GLFEATURES, GR_ATT_TEXTURESIZE, (char*)NULL, (tdble) sizelimit);
95 bool found = false;
96
97 for (i = 0; i < nbOptionsTextSize; i++) {
98 if (textureSizeOptionList[i] <= sizelimit) {
99 maxsizenb = i;
100 } else {
101 break;
102 }
103 }
104
105 // Limit to available sizes.
106 nbOptionsTextSize = maxsizenb+1;
107
108 for (i = 0; i < nbOptionsTextSize; i++) {
109 if (textureSizeOptionList[i] == tsize) {
111 found = true;
112 break;
113 }
114 }
115
116 if (!found) {
117 // Should never come here if there is no bug in OpenGL.
118 tsize = defaultTextSize;
119 for (i = 0; i < nbOptionsTextSize; i++) {
120 if (textureSizeOptionList[i] == tsize) {
122 break;
123 }
124 }
125 }
126 snprintf(buf, BUFSIZE, "%d", textureSizeOptionList[curOptionTextSize]);
128
129 // Read texture anisotropic filtering parameters.
131 int maxanisotropy = getGLTextureMaxAnisotropy();
133 int anisotropy = getUserTextureAnisotropy();
136
137 for (i = 1; i < nbTextureAnisotropyOptions; i++) {
138 if (textureAnisotropyOptionList[i] <= maxanisotropy) {
140 } else {
141 break;
142 }
143 }
144
145 for (i = 0; i < nbOptionsTextAnisotropy; i++) {
146 if (textureAnisotropyOptionList[i] <= anisotropy) {
148 }
149 }
150
152 }
153
154 GfParmReleaseHandle(paramHandle);
155}
156
157
158// Save the choosen values in the corresponding parameter file.
159static void saveOpenGLOption(void *)
160{
161 const int BUFSIZE = 1024;
162 char buf[BUFSIZE];
163 snprintf(buf, BUFSIZE, "%s%s", GetLocalDir(), GR_PARAM_FILE);
164 void *paramHandle = GfParmReadFile(buf, GFPARM_RMODE_REREAD | GFPARM_RMODE_CREAT);
165
166 // Texture compression.
168 // Texture sizing.
171 // Texture anisotropic filtering.
173 }
174
175 GfParmWriteFile(NULL, paramHandle, "graph");
176 GfParmReleaseHandle(paramHandle);
177
178 // Return to previous screen.
183 return;
184}
185
186
187// Toggle texture compression state enabled/disabled.
198
199
200// Scroll through texture sizes smaller or equal the system limit.
201static void changeTextureSizeState(int delta)
202{
203 const int BUFSIZE = 1024;
204 char buf[BUFSIZE];
205
206 curOptionTextSize += delta;
207 if (curOptionTextSize < 0) {
209 } else if (curOptionTextSize >= nbOptionsTextSize) {
211 }
212
213 snprintf(buf, BUFSIZE, "%d", textureSizeOptionList[curOptionTextSize]);
215}
216
217
218// Scroll through texture anisotropic filtering modes supported by the system.
230
231
232static void onActivate(void * /* dummy */)
233{
235}
236
237
238// OpenGL menu
239void * OpenGLMenuInit(void *prevMenu)
240{
241 int y, dy;
242
243 // Has screen already been created?
244 if (scrHandle) {
245 return scrHandle;
246 }
247
248 prevHandle = prevMenu;
249
250 scrHandle = GfuiScreenCreateExAction((float*)NULL, [] { onActivate(NULL); }, tGfuiAction(), 1);
251 GfuiTitleCreate(scrHandle, "OpenGL Options", 0);
252 GfuiScreenAddBgImg(scrHandle, "data/img/splash-simucfg.png");
253
254 y = 400;
255 dy = 30;
256
257 const int xleft = 160; // Center of left elements.
258 const int xright = 480; // Center of right elements.
259 const int width = 240; // Width of elements.
260
261 y -= dy;
262
263 // Texture compression.
264 GfuiLabelCreate(scrHandle, "Texture Compression", GFUI_FONT_LARGE, xleft, 400, GFUI_ALIGN_HC_VB, 0);
265
267 GfuiGrButtonCreateAction(scrHandle, "data/img/arrow-left.png", "data/img/arrow-left.png",
268 "data/img/arrow-left.png", "data/img/arrow-left-pushed.png",
269 xleft-width/2, y, GFUI_ALIGN_HC_VB, 1,
270 [] { changeTextureCompressState(-1); },
272
273 GfuiGrButtonCreateAction(scrHandle, "data/img/arrow-right.png", "data/img/arrow-right.png",
274 "data/img/arrow-right.png", "data/img/arrow-right-pushed.png",
275 xleft+width/2, y, GFUI_ALIGN_HC_VB, 1,
278
281 } else {
282 GfuiLabelCreate(scrHandle, "not available", GFUI_FONT_LARGE_C, xleft, y, GFUI_ALIGN_HC_VB, 0);
283 }
284
285
286 // Texture sizing.
287 GfuiLabelCreate(scrHandle, "Texture Size Limit", GFUI_FONT_LARGE, xright, 400, GFUI_ALIGN_HC_VB, 0);
288
289 GfuiGrButtonCreateAction(scrHandle, "data/img/arrow-left.png", "data/img/arrow-left.png",
290 "data/img/arrow-left.png", "data/img/arrow-left-pushed.png",
291 xright-width/2, y, GFUI_ALIGN_HC_VB, 0,
292 [] { changeTextureSizeState(-1); },
294
295 GfuiGrButtonCreateAction(scrHandle, "data/img/arrow-right.png", "data/img/arrow-right.png",
296 "data/img/arrow-right.png", "data/img/arrow-right-pushed.png",
297 xright+width/2, y, GFUI_ALIGN_HC_VB, 0,
298 [] { changeTextureSizeState(1); },
300
303
304 y -= 80;
305
306 // Texture anisotropic filtering.
307 GfuiLabelCreate(scrHandle, "Anisotropic Filtering", GFUI_FONT_LARGE, xleft, y + dy, GFUI_ALIGN_HC_VB, 0);
308
310 GfuiGrButtonCreateAction(scrHandle, "data/img/arrow-left.png", "data/img/arrow-left.png",
311 "data/img/arrow-left.png", "data/img/arrow-left-pushed.png",
312 xleft-width/2, y, GFUI_ALIGN_HC_VB, 1,
315
316 GfuiGrButtonCreateAction(scrHandle, "data/img/arrow-right.png", "data/img/arrow-right.png",
317 "data/img/arrow-right.png", "data/img/arrow-right-pushed.png",
318 xleft+width/2, y, GFUI_ALIGN_HC_VB, 1,
321
324 } else {
325 GfuiLabelCreate(scrHandle, "not available", GFUI_FONT_LARGE_C, xleft, y, GFUI_ALIGN_HC_VB, 0);
326 }
327
328
330 [] { saveOpenGLOption(NULL); }, tGfuiAction(), tGfuiAction());
331
333 [prevMenu] { GfuiScreenActivate(prevMenu); }, tGfuiAction(), tGfuiAction());
334
335 GfuiAddKeyAction(scrHandle, 13, "Save", [] { saveOpenGLOption(NULL); }, tGfuiAction());
336 GfuiAddKeyAction(scrHandle, 27, "Cancel Selection", [prevMenu] { GfuiScreenActivate(prevMenu); }, tGfuiAction());
337 GfuiAddSKeyAction(scrHandle, GLUT_KEY_F12, "Screen-Shot", [] { GfuiScreenShot(NULL); }, tGfuiAction());
338 GfuiAddSKeyAction(scrHandle, GLUT_KEY_LEFT, "Previous Option in list", [] { changeTextureCompressState(-1); }, tGfuiAction());
339 GfuiAddSKeyAction(scrHandle, GLUT_KEY_RIGHT, "Next Option in list", [] { changeTextureCompressState(1); }, tGfuiAction());
340
342
343 return scrHandle;
344}
static Vector y[4]
Definition Convex.cpp:56
static void * prevHandle
static void * scrHandle
static void onActivate(void)
static float LabelColor[]
int getGLTextureMaxSize(void)
int getUserTextureAnisotropy(void)
void updateUserTextureMaxSize(void)
void updateUserTextureAnisotropy(void)
bool isCompressARBAvailable(void)
int getGLTextureMaxAnisotropy(void)
void updateCompressARBEnabled(void)
bool isTextureAnisotropyAvailable(void)
#define GR_ATT_TEXTUREANISOTROPY
Definition graphic.h:55
#define GR_ATT_TEXTURECOMPRESSION_DISABLED
Definition graphic.h:53
#define GR_ATT_TEXTURECOMPRESSION
Definition graphic.h:51
#define GR_ATT_TEXTURESIZE
Definition graphic.h:61
#define GR_ATT_TEXTURECOMPRESSION_ENABLED
Definition graphic.h:52
#define GR_SCT_GLFEATURES
Definition graphic.h:50
#define GR_PARAM_FILE
Definition graphic.h:40
float tdble
Floating point type used in TORCS.
Definition tgf.h:47
void * GfuiScreenCreateExAction(float *bgColor, const tGfuiAction &onActivate, const tGfuiAction &onDeactivate, int mouseAllowed)
Create a screen.
Definition gui.cpp:619
void GfuiLabelSetText(void *scr, int id, const char *text)
Change the text of a label.
Definition guilabel.cpp:193
int GfuiTitleCreate(void *scr, const char *text, int maxlen)
Add a Title to the screen.
Definition guilabel.cpp:150
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 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
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:121
void GfuiLabelSetColor(void *scr, int id, float *color)
Change the color of a label.
Definition guilabel.cpp:221
int GfuiGrButtonCreateAction(void *scr, const char *disabled, const char *enabled, const char *focused, const char *pushed, int x, int y, int align, int mouse, const tGfuiAction &onPush, const tGfuiAction &onFocus, const tGfuiAction &onFocusLost)
Add a graphical button to a screen.
Definition guibutton.cpp:58
void GfuiScreenActivate(void *screen)
Activate a screen and make it current.
Definition gui.cpp:508
int GfuiButtonCreateAction(void *scr, const char *text, int font, int x, int y, int width, int align, int mouse, const tGfuiAction &onPush, const tGfuiAction &onFocus, const tGfuiAction &onFocusLost)
Add a button to a screen.
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:2453
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:2623
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
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:2737
void GfParmReleaseHandle(void *parmHandle)
Release given parameter set handle parmHandle.
Definition params.cpp:1963
int GfParmWriteFile(const char *file, void *parmHandle, const char *name)
Write parameter set into file.
Definition params.cpp:1732
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
static int textureAnisotropyOptionList[]
static void changeTextureCompressState(int delta)
static int TextureSizeOptionId
static void changeTextureSizeState(int delta)
static const int nbOptionsTextComp
void * OpenGLMenuInit(void *prevMenu)
static const int nbTextureAnisotropyOptions
static int curOptionTextSize
static int textureSizeOptionList[]
static int TextureCompressOptionId
static void changeTextureAnisotropyState(int delta)
static const int defaultTextSize
static int nbOptionsTextAnisotropy
static int nbOptionsTextSize
static int curOptionTextAnisotropy
static const char * textureAnisotropyOptionLabelList[]
static int TextureAnisotropyOptionId
static void readOpenGLCfg(void)
static const char * textureCompressOptionList[]
static void saveOpenGLOption(void *)
static int curOptionTextComp
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_REREAD
Clear shared cached data and reread the file.
Definition tgf.h:265
#define GFUI_FONT_LARGE
Definition tgfclient.h:248
#define GFUI_FONT_LARGE_C
Definition tgfclient.h:252
#define GFUI_MOUSE_UP
Definition tgfclient.h:82
std::function< void()> tGfuiAction
Definition tgfclient.h:107
#define GFUI_ALIGN_HC_VB
Definition tgfclient.h:74