Several refactorizations:

- Removed several unused and unusable functions
- Rewrote BMP writing
- Reduced variable scope
- Use const char* instead of char[40] in gl_values

git-svn-id: https://svn.code.sf.net/p/extremetuxracer/code/trunk@439 0420edf4-82e4-42fc-9478-35b55e6d67a3
master
pkeus 2013-09-03 14:09:21 +00:00
parent 5bc10e9505
commit a5348fabf8
7 changed files with 113 additions and 220 deletions

View File

@ -23,7 +23,6 @@ GNU General Public License for more details.
#include "spx.h"
#include <iostream>
#include <cerrno>
#include <cstdio>
#include <ctime>
// --------------------------------------------------------------------
@ -94,16 +93,6 @@ void PrintFloat8 (const float val) {
cout << val << '\n';
}
void PrintBool (const bool val) {
if (val == true) cout <<"bool: true\n";
else cout << "bool: false\n";
}
void PrintPointer (void *p) {
if (p == NULL) cout << "Pointer: NULL\n";
else cout << "Pointer: " << p << '\n';
}
void PrintVector4 (const TVector4& v) {
cout.precision(3);
cout << v.x << " " << v.y << " " << v.z << " " << v.w << '\n';
@ -161,8 +150,6 @@ void PrintQuaternion (const TQuaternion& q) {
static CSPList msg_list (100);
void InitMessages () {}
void SaveMessages () {
msg_list.Save (param.config_dir, "messages");
}
@ -238,7 +225,7 @@ void GetTimeComponents (double time, int *min, int *sec, int *hundr) {
*hundr = ((int) (time * 100 + 0.5) ) % 100;
}
string GetTimeString1 () {
string GetTimeString () {
time_t rawtime;
struct tm * timeinfo;
@ -252,19 +239,3 @@ string GetTimeString1 () {
line += Int_StrN (timeinfo->tm_sec);
return line;
}
// --------------------------------------------------------------------
// FILE, read and write
// --------------------------------------------------------------------
size_t write_word (FILE *fp, uint16_t w) {
return fwrite(&w, 2, 1, fp);
}
size_t write_dword(FILE *fp, uint32_t dw) {
return fwrite(&dw, 4, 1, fp);
}
size_t write_long (FILE *fp, int32_t l) {
return fwrite(&l, 4, 1, fp);
}

View File

@ -84,8 +84,6 @@ void PrintFloat (const float val);
void PrintDouble (const double val);
void PrintFloat8 (const float val);
void PrintFloat (const char *s, const float val);
void PrintBool (const bool val);
void PrintPointer (void *p);
void PrintVector (const TVector3& v);
void PrintVector4 (const TVector4& v);
void PrintColor (const TColor& c);
@ -121,10 +119,7 @@ void SaveMessages ();
// --------------------------------------------------------------------
void GetTimeComponents (double time, int *min, int *sec, int *hundr);
string GetTimeString1 ();
string GetTimeString ();
size_t write_word (FILE *fp, uint16_t w);
size_t write_dword (FILE *fp, uint32_t dw);
size_t write_long (FILE *fp, int32_t l);
#endif

View File

@ -880,11 +880,3 @@ void FTGLPixmapFont::Render( const wchar_t* string) {
glPopClientAttrib();
glPopAttrib();
}

View File

@ -22,16 +22,13 @@ GNU General Public License for more details.
#include "ogl.h"
#include "spx.h"
#include "winsys.h"
#include <cstdarg>
#include <stack>
struct gl_value_t {
char name[40];
static const struct {
const char* name;
GLenum value;
GLenum type;
};
static const gl_value_t gl_values[] = {
} gl_values[] = {
{ "maximum lights", GL_MAX_LIGHTS, GL_INT },
{ "modelview stack depth", GL_MAX_MODELVIEW_STACK_DEPTH, GL_INT },
{ "projection stack depth", GL_MAX_PROJECTION_STACK_DEPTH, GL_INT },
@ -53,13 +50,6 @@ void check_gl_error() {
}
}
void init_glfloat_array (int num, GLfloat arr[], ...) {
va_list args;
va_start (args, arr);
for (int i=0; i<num; i++) arr[i] = va_arg(args, double);
va_end (args);
}
PFNGLLOCKARRAYSEXTPROC glLockArraysEXT_p = NULL;
PFNGLUNLOCKARRAYSEXTPROC glUnlockArraysEXT_p = NULL;
@ -67,13 +57,7 @@ typedef void (*(*get_gl_proc_fptr_t)(const GLubyte *))();
void InitOpenglExtensions () {
get_gl_proc_fptr_t get_gl_proc;
#if defined (HAVE_SDL)
get_gl_proc = (get_gl_proc_fptr_t) SDL_GL_GetProcAddress;
#elif defined (OS_WIN32_MSC)
get_gl_proc = (get_gl_proc_fptr_t) wglGetProcAddress;
#else
get_gl_proc = NULL;
#endif
if (get_gl_proc) {
glLockArraysEXT_p = (PFNGLLOCKARRAYSEXTPROC)
@ -95,11 +79,6 @@ void InitOpenglExtensions () {
}
void PrintGLInfo () {
GLint int_val;
GLfloat float_val;
GLboolean boolean_val;
string ss;
Message ("Gl vendor: ", (char*)glGetString (GL_VENDOR));
Message ("Gl renderer: ", (char*)glGetString (GL_RENDERER));
Message ("Gl version: ", (char*)glGetString (GL_VERSION));
@ -120,44 +99,48 @@ void PrintGLInfo () {
Message ("", "");
for (int i=0; i<(int)(sizeof(gl_values)/sizeof(gl_values[0])); i++) {
switch (gl_values[i].type) {
case GL_INT:
case GL_INT: {
GLint int_val;
glGetIntegerv (gl_values[i].value, &int_val);
ss = Int_StrN (int_val);
string ss = Int_StrN (int_val);
Message (gl_values[i].name, ss);
break;
case GL_FLOAT:
}
case GL_FLOAT: {
GLfloat float_val;
glGetFloatv (gl_values[i].value, &float_val);
ss = Float_StrN (float_val, 2);
string ss = Float_StrN (float_val, 2);
Message (gl_values[i].name, ss);
break;
case GL_UNSIGNED_BYTE:
}
case GL_UNSIGNED_BYTE: {
GLboolean boolean_val;
glGetBooleanv (gl_values[i].value, &boolean_val);
ss = Int_StrN (boolean_val);
string ss = Int_StrN (boolean_val);
Message (gl_values[i].name, ss);
break;
}
default:
Message ("","");
Message ("", "");
}
}
}
void set_material (const TColor& diffuse_colour, const TColor& specular_colour, double specular_exp) {
GLfloat mat_amb_diff[4];
GLfloat mat_specular[4];
mat_amb_diff[0] = diffuse_colour.r;
mat_amb_diff[1] = diffuse_colour.g;
mat_amb_diff[2] = diffuse_colour.b;
mat_amb_diff[3] = diffuse_colour.a;
GLfloat mat_amb_diff[4] = {
diffuse_colour.r,
diffuse_colour.g,
diffuse_colour.b,
diffuse_colour.a
};
glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff);
mat_specular[0] = specular_colour.r;
mat_specular[1] = specular_colour.g;
mat_specular[2] = specular_colour.b;
mat_specular[3] = specular_colour.a;
GLfloat mat_specular[4] = {
specular_colour.r,
specular_colour.g,
specular_colour.b,
specular_colour.a
};
glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, specular_exp);
@ -192,11 +175,10 @@ void SetupGuiDisplay () {
}
void Reshape (int w, int h) {
double far_clip_dist;
glViewport (0, 0, (GLint) w, (GLint) h );
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
far_clip_dist = param.forward_clip_distance + FAR_CLIP_FUDGE_AMOUNT;
double far_clip_dist = param.forward_clip_distance + FAR_CLIP_FUDGE_AMOUNT;
gluPerspective (param.fov, (double)w/h, NEAR_CLIP_DIST, far_clip_dist );
glMatrixMode (GL_MODELVIEW);
}
@ -204,7 +186,7 @@ void Reshape (int w, int h) {
// GL options
// ====================================================================
TRenderMode currentMode = (TRenderMode)-1;
TRenderMode currentMode = RM_UNINITIALIZED;
void set_gl_options (TRenderMode mode) {
currentMode = mode;
switch (mode) {
@ -488,7 +470,7 @@ void set_gl_options (TRenderMode mode) {
break;
*/
stack<TRenderMode> modestack;
static stack<TRenderMode> modestack;
void PushRenderMode(TRenderMode mode) {
if (currentMode != mode)
set_gl_options(mode);

View File

@ -35,6 +35,7 @@ enum TRenderMode {
SKY,
FOG_PLANE,
TRACK_MARKS,
RM_UNINITIALIZED = -1
};
@ -44,7 +45,6 @@ extern PFNGLLOCKARRAYSEXTPROC glLockArraysEXT_p;
extern PFNGLUNLOCKARRAYSEXTPROC glUnlockArraysEXT_p;
void check_gl_error();
void init_glfloat_array( int num, GLfloat arr[], ... );
void InitOpenglExtensions();
void PrintGLInfo();

View File

@ -25,7 +25,6 @@ GNU General Public License for more details.
#include "winsys.h"
#include <fstream>
#include <cctype>
#include <cstdio>
// --------------------------------------------------------------------
// class CImage
@ -158,29 +157,28 @@ void CImage::WritePPM (const char *filepath) {
file.close ();
}
void CImage::WritePPM (const char *dir, const char *filename) {
string path = dir;
path += SEP;
path += filename;
WritePPM (path.c_str());
}
struct TTgaHeader {
char tfType;
char tfColorMapType;
char tfImageType;
char tfColorMapSpec[5];
short tfOrigX;
short tfOrigY;
short tfWidth;
short tfHeight;
char tfBpp;
char tfImageDes;
};
void CImage::WriteTGA (const char *filepath) {
if (data == NULL) return;
std::ofstream out(filepath, std::ios_base::out|std::ios_base::binary);
short TGAhead[] = {0, 2, 0, 0, 0, 0, nx, ny, 24};
static short TGAhead[] = {0, 2, 0, 0, 0, 0, nx, ny, 24};
out.write(reinterpret_cast<char*>(&TGAhead), sizeof(TGAhead));
out.write(reinterpret_cast<char*>(data), 3 * nx * ny);
}
void CImage::WriteTGA (const char *dir, const char *filename) {
string path = dir;
path += SEP;
path += filename;
WriteTGA (path.c_str());
}
void CImage::WriteTGA_H (const char *filepath) {
if (data == NULL) return;
TTgaHeader header;
@ -201,86 +199,86 @@ void CImage::WriteTGA_H (const char *filepath) {
out.write(reinterpret_cast<char*>(data), 3 * nx * ny);
}
void CImage::WriteTGA_H (const char *dir, const char *filename) {
string path = dir;
path += SEP;
path += filename;
WriteTGA_H (path.c_str());
}
#define BF_TYPE 0x4D42 // "MB"
#ifdef _MSC_VER
#pragma pack(push, 1)
#endif
struct TBmpHeader {
uint16_t bfType; // identifier of bmp format
uint32_t bfSize; // size of file, including the headers
uint16_t bfReserved1; // reserved, always 0
uint16_t bfReserved2; // reserved, always 0
uint32_t bfOffBits; // offset to bitmap data
#ifdef _MSC_VER
};
#else
} __attribute__((packed));
#endif
struct TBmpInfo {
uint32_t biSize; // size of info header, normally 40
int32_t biWidth; // width
int32_t biHeight; // height
uint16_t biPlanes; // number of color planes, normally 1
uint16_t biBitCount; // Number of bits per pixel (8 * depth)
uint32_t biCompression; // type of compression, normally 0 = no compr.
uint32_t biSizeImage; // size of data
int32_t biXPelsPerMeter; // normally 0
int32_t biYPelsPerMeter; // normally 0
uint32_t biClrUsed; // normally 0
uint32_t biClrImportant; // normally 0
#ifdef _MSC_VER
};
#pragma pack(pop)
#else
} __attribute__((packed));
#endif
void CImage::WriteBMP (const char *filepath) {
if (data == NULL) return;
TBmpInfo info;
FILE *fp;
int infosize;
unsigned int bitsize;
if (data == NULL)
return;
info.biSize = 40;
info.biWidth = nx;
info.biHeight = ny;
int infosize = 40;
int width = nx;
int height = ny;
int imgsize = nx * ny * depth;
int bitcnt = 8 * depth; // 24 or 32
unsigned int bitsize;
// (width * bitcnt + 7) / 8 = width * depth
if (imgsize == 0) bitsize = (width * bitcnt + 7) / 8 * height;
else bitsize = imgsize;
TBmpHeader header;
header.bfType = BF_TYPE;
header.bfSize = 14 + infosize + bitsize;
header.bfReserved1 = 0;
header.bfReserved2 = 0;
header.bfOffBits = sizeof(TBmpHeader) + sizeof(TBmpInfo);
TBmpInfo info;
info.biSize = infosize;
info.biWidth = width;
info.biHeight = height;
info.biPlanes = 1;
info.biBitCount = 8 * depth;
info.biBitCount = bitcnt;
info.biCompression = 0;
info.biSizeImage = nx * ny * depth;
info.biSizeImage = imgsize;
info.biXPelsPerMeter = 0;
info.biYPelsPerMeter= 0;
info.biClrUsed = 0;
info.biClrImportant = 0;
if ((fp = fopen (filepath, "wb")) == NULL) {
std::ofstream out(filepath, std::ios_base::out|std::ios_base::binary);
if (!out) {
Message ("could not open bmp file", filepath);
return;
}
int imgsize = info.biSizeImage;
int width = info.biWidth;
int height = info.biHeight;
int bitcnt = info.biBitCount; // 24 or 32
out.write(reinterpret_cast<char*>(&header), sizeof(TBmpHeader));
out.write(reinterpret_cast<char*>(&info), sizeof(TBmpInfo));
// (width * bitcnt + 7) / 8 = width * depth
if (imgsize == 0) bitsize = (width * bitcnt + 7) / 8 * height;
else bitsize = imgsize;
infosize = info.biSize; // 40
if (infosize != 40 || info.biCompression != 0) {
Message ("wrong bmp header");
fclose(fp);
return;
}
write_word (fp, 0x4D42);
write_dword (fp, 14 + infosize + bitsize);
write_word (fp, 0);
write_word (fp, 0);
write_dword (fp, 54);
write_dword (fp, info.biSize);
write_long (fp, info.biWidth);
write_long (fp, info.biHeight);
write_word (fp, info.biPlanes);
write_word (fp, info.biBitCount);
write_dword (fp, info.biCompression);
write_dword (fp, info.biSizeImage);
write_long (fp, info.biXPelsPerMeter);
write_long (fp, info.biYPelsPerMeter);
write_dword (fp, info.biClrUsed);
write_dword (fp, info.biClrImportant);
if (fwrite (data, 1, bitsize, fp) != bitsize) {
Message ("error on writing bmp data");
fclose (fp);
return;
}
fclose(fp);
return;
}
void CImage::WriteBMP (const char *dir, const char *filename) {
string path = dir;
path += SEP;
path += filename;
WriteBMP (path.c_str());
out.write(reinterpret_cast<char*>(data), bitsize);
}
// --------------------------------------------------------------------
@ -675,7 +673,7 @@ void ScreenshotN () {
path += SEP;
path += Course.CourseList[g_game.course_id].dir;
path += "_";
path += GetTimeString1 ();
path += GetTimeString();
int type = SCREENSHOT_PROC;
switch (type) {
@ -683,25 +681,21 @@ void ScreenshotN () {
path += ".ppm";
image.ReadFrameBuffer_PPM ();
image.WritePPM (path.c_str());
image.DisposeData ();
break;
case 1:
path += ".tga";
image.ReadFrameBuffer_TGA ();
image.WriteTGA (path.c_str());
image.DisposeData ();
break;
case 2:
path += ".tga";
image.ReadFrameBuffer_TGA ();
image.WriteTGA_H (path.c_str());
image.DisposeData ();
break;
case 3:
path += ".bmp";
image.ReadFrameBuffer_BMP ();
image.WriteBMP (path.c_str());
image.DisposeData ();
break;
}
}

View File

@ -65,43 +65,6 @@ GNU General Public License for more details.
#define T_SNOW2 42
#define T_SNOW3 43
#define BF_TYPE 0x4D42 // "MB"
struct TTgaHeader {
char tfType;
char tfColorMapType;
char tfImageType;
char tfColorMapSpec[5];
short tfOrigX;
short tfOrigY;
short tfWidth;
short tfHeight;
char tfBpp;
char tfImageDes;
};
struct TBmpHeader {
unsigned short bfType; // identifier of bmp format
unsigned long bfSize; // size of file, including the headers
unsigned short bfReserved1; // reserved, always 0
unsigned short bfReserved2; // reserved, always 0
unsigned long bfOffBits; // offset to bitmap data
};
struct TBmpInfo {
unsigned long biSize; // size of info header, normally 40
long biWidth; // width
long biHeight; // height
unsigned short biPlanes; // number of color planes, normally 1
unsigned short biBitCount; // Number of bits per pixel (8 * depth)
unsigned long biCompression; // type of compression, normally 0 = no compr.
unsigned long biSizeImage; // size of data
long biXPelsPerMeter; // normally 0
long biYPelsPerMeter; // normally 0
unsigned long biClrUsed; // normally 0
unsigned long biClrImportant; // normally 0
};
// --------------------------------------------------------------------
// class CImage
@ -130,15 +93,11 @@ public:
void ReadFrameBuffer_TGA ();
void ReadFrameBuffer_BMP ();
void WritePPM (const char *filepath);
void WritePPM (const char *dir, const char *filename);
void WriteTGA (const char *filepath);
void WriteTGA (const char *dir, const char *filename);
// versions with explicite header
void WriteTGA_H (const char *filepath);
void WriteTGA_H (const char *dir, const char *filename);
void WriteBMP (const char *filepath);
void WriteBMP (const char *dir, const char *filename);
};
// --------------------------------------------------------------------