TORCS  1.3.9
The Open Racing Car Simulator
splash.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 
3  file : splash.cpp
4  created : Sat Mar 18 23:49:03 CET 2000
5  copyright : (C) 2000 by Eric Espie
6  email : torcs@free.fr
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 #ifdef WIN32
20 #include <windows.h>
21 #define HAVE_CONFIG_H
22 #endif
23 #include <GL/glut.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <portability.h>
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include <tgfclient.h>
33 
34 #include "splash.h"
35 #include "mainmenu.h"
36 
37 static int s_imgWidth, s_imgHeight;
38 static GLuint s_texture = 0;
39 static int SplashDisplaying;
40 
41 /*
42  * Function
43  * splashKey
44  *
45  * Description
46  *
47  *
48  * Parameters
49  *
50  *
51  * Return
52  *
53  *
54  * Remarks
55  *
56  */
57 static void splashKey( unsigned char /* key */, int /* x */, int /* y */)
58 {
59  SplashDisplaying = 0;
60  glDeleteTextures(1, &s_texture);
61  s_texture = 0;
63 }
64 
65 /*
66  * Function
67  *
68  *
69  * Description
70  *
71  *
72  * Parameters
73  *
74  *
75  * Return
76  *
77  *
78  * Remarks
79  *
80  */
81 static void splashTimer(int /* value */)
82 {
83  if (SplashDisplaying) {
84  SplashDisplaying = 0;
85  glDeleteTextures(1, &s_texture);
86  s_texture = 0;
88  }
89 }
90 
91 
92 /*
93  * Function
94  * splashDisplay
95  *
96  * Description
97  *
98  *
99  * Parameters
100  *
101  *
102  * Return
103  *
104  *
105  * Remarks
106  *
107  */
108 static void splashDisplay( void )
109 {
110  int ScrW, ScrH, ViewW, ViewH;
111 
112  SplashDisplaying = 1;
113 
114  glDisable(GL_DEPTH_TEST);
115  glDisable(GL_LIGHTING);
116  glDisable(GL_CULL_FACE);
117  glDisable(GL_ALPHA_TEST);
118 
119  GfScrGetSize(&ScrW, &ScrH, &ViewW, &ViewH);
120 
121  glViewport((ScrW-ViewW) / 2, (ScrH-ViewH) / 2, ViewW, ViewH);
122  glMatrixMode(GL_PROJECTION);
123  glLoadIdentity();
124  gluOrtho2D(0, ScrW, 0, ScrH);
125  glMatrixMode(GL_MODELVIEW);
126  glLoadIdentity();
127 
128  if (s_texture != 0) {
129  GLfloat tx1 = 0.0f, tx2 = 1.0f, ty1 = 0.0f, ty2 = 1.0f;
130 
131  // Compute texture coordinates to ensure proper unskewed/unstretched display of
132  // image content.
133  //tdble rfactor = (float)(s_imgWidth*ViewH)/(float)(s_imgHeight*ViewW);
134  tdble rfactor = (16.0f*ViewH)/(10.0f*ViewW);
135 
136  if (rfactor >= 1.0f) {
137  // Aspect ratio of view is smaller than 16:10, "cut off" sides
138  tdble tdx = (1.0f-1.0f/rfactor)/2.0f;
139  tx1 += tdx;
140  tx2 -= tdx;
141  } else {
142  // Aspect ratio of view is larger than 16:10, "cut off" top and bottom
143  tdble tdy = (1.0f-rfactor)/2.0f;
144  ty1 += tdy;
145  ty2 -= tdy;
146  }
147 
148  glEnable(GL_TEXTURE_2D);
149  glBindTexture(GL_TEXTURE_2D, s_texture);
150  glBegin(GL_QUADS);
151  glTexCoord2f(tx1, ty1); glVertex3f(0.0, 0.0, 0.0);
152  glTexCoord2f(tx1, ty2); glVertex3f(0.0, ScrH, 0.0);
153  glTexCoord2f(tx2, ty2); glVertex3f(ScrW, ScrH, 0.0);
154  glTexCoord2f(tx2, ty1); glVertex3f(ScrW, 0.0, 0.0);
155  glEnd();
156  glDisable(GL_TEXTURE_2D);
157  }
158 
159 #ifdef HAVE_CONFIG_H
160  glMatrixMode(GL_PROJECTION);
161  glLoadIdentity();
162  gluOrtho2D(0, 640, 0, 480);
163 
164  static float grWhite[4] = {1.0, 1.0, 1.0, 1.0};
166 #endif
167 
168  glutSwapBuffers();
169 }
170 
171 static void splashMouse(int /* b */, int s, int /* x */, int /* y */)
172 {
173  if (s == GLUT_UP) {
174  SplashDisplaying = 0;
175  glDeleteTextures(1, &s_texture);
176  s_texture = 0;
178  }
179 }
180 
181 
182 /*
183  * Function
184  *
185  *
186  * Description
187  *
188  *
189  * Parameters
190  *
191  *
192  * Return
193  *
194  *
195  * Remarks
196  *
197  */
198 int SplashScreen(void)
199 {
200  void *handle;
201  float screen_gamma;
202  const char *filename = "data/img/splash.png";
203  const int BUFSIZE = 1024;
204  char buf[BUFSIZE];
205 
206  if (s_texture != 0) {
207  glDeleteTextures(1, &s_texture);
208  }
209 
210  snprintf(buf, BUFSIZE, "%s%s", GetLocalDir(), GFSCR_CONF_FILE);
212  screen_gamma = (float)GfParmGetNum(handle, GFSCR_SECT_PROP, GFSCR_ATT_GAMMA, (char*)NULL, 2.0);
213  GLbyte *tex = (GLbyte*)GfImgReadPng(filename, &s_imgWidth, &s_imgHeight, screen_gamma);
214  GfParmReleaseHandle(handle);
215  if (!tex) {
216  GfTrace("Couldn't read %s\n", filename);
217  return -1;
218  }
219 
220  glGenTextures(1, &s_texture);
221  glBindTexture(GL_TEXTURE_2D, s_texture);
222  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
223  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
224  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, s_imgWidth, s_imgHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)(tex));
225  free(tex);
226 
227  glutDisplayFunc(splashDisplay);
228  glutKeyboardFunc(splashKey);
229  glutSpecialFunc((void (*)(int key, int x, int y))NULL);
230  glutTimerFunc(7000, splashTimer, 0);
231  glutMouseFunc(splashMouse);
232 
233  return 0;
234 }
235 
236 
static int s_imgHeight
Definition: splash.cpp:37
#define GFSCR_ATT_GAMMA
static void splashMouse(int, int s, int, int)
Definition: splash.cpp:171
static GLuint s_texture
Definition: splash.cpp:38
void * GfParmReadFile(const char *file, int mode)
Read parameter set from file and return handle to parameter set.
Definition: params.cpp:1157
#define GFPARM_RMODE_STD
if handle already openned return it
Definition: tgf.h:265
char * GetLocalDir(void)
Definition: tgf.cpp:231
void GfParmReleaseHandle(void *parmHandle)
Release given parameter set handle parmHandle.
Definition: params.cpp:1834
#define GFSCR_CONF_FILE
static void splashKey(unsigned char, int, int)
Definition: splash.cpp:57
static int ViewH
Definition: gui.cpp:43
static int ViewW
Definition: gui.cpp:43
#define GFUI_FONT_SMALL_C
Definition: tgfclient.h:174
#define GFUI_ALIGN_HR_VB
Definition: tgfclient.h:75
#define GFSCR_SECT_PROP
The Gaming Framework API (client part).
static float grWhite[4]
Definition: grboard.cpp:41
float tdble
Floating point type used in TORCS.
Definition: tgf.h:48
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:54
static int ScrW
Definition: gui.cpp:43
void GfuiPrintString(const char *text, float *fgColor, int font, int x, int y, int align)
Definition: guiobject.cpp:43
static int s_imgWidth
Definition: splash.cpp:37
static int ScrH
Definition: gui.cpp:43
void GfScrGetSize(int *scrw, int *scrh, int *vieww, int *viewh)
Get the screen and viewport sizes.
Definition: screen.cpp:471
static void splashTimer(int)
Definition: splash.cpp:81
int SplashScreen(void)
Definition: splash.cpp:198
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
static Vector y[4]
Definition: Convex.cpp:56
static int SplashDisplaying
Definition: splash.cpp:39
static void splashDisplay(void)
Definition: splash.cpp:108
#define GfTrace
Definition: tgf.h:336
#define GFPARM_RMODE_CREAT
Create the file if doesn&#39;t exist.
Definition: tgf.h:267