File: TextureUtil.cpp

package info (click to toggle)
pinball 0.3.20201218-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 8,452 kB
  • sloc: cpp: 15,230; makefile: 840; sh: 381; xml: 24
file content (441 lines) | stat: -rw-r--r-- 13,021 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
// #ident "$Id: TextureUtil.cpp $"
/***************************************************************************
                          TextureUtil.cpp  -  description
                             -------------------
    begin                : Mon Nov 27 2000
    copyright            : (C) 2000 by Henrik Enqvist
    email                : henqvist@excite.com
***************************************************************************/


// TODO free textures and image memory on exit/clear function
#include "Private.h"
#include "TextureUtil.h"
#include "Config.h"
#include <iostream>

#if EM_USE_SDL
#include <SDL_opengl.h> //should fix GL portability ; instead of <OpenGL/gl.h>
// TODO remove glu
#if EM_DEBUG
#include <GL/glu.h>
#endif
#include <SDL.h>
#include <SDL_image.h>

extern "C" {
  struct_image* loadP(const char * filename);

  struct_image* loadP(const char * filename) {
    SDL_Surface* surface = IMG_Load(filename);
    if (surface == NULL) {
      cerr << "::loadP could not load: " << filename << endl;
      return NULL;
    }
    struct_image* image = (struct_image*) malloc(sizeof(struct_image));
    image->width = surface->w;
    image->height = surface->h;
    // asume 24 bits are 3*8 and 32 are 4*8
    if (surface->format->BitsPerPixel == 24) {
      image->channels = 3;
    } else if (surface->format->BitsPerPixel == 32) {
      image->channels = 4;
    } else {
      cerr << "::loadP Only 32 bit RGBA and 24 bit RGB images supported"<<endl;
      // TODO free surface struct
      free(image);
      return NULL;
    }
    image->pixels = (unsigned char*) surface->pixels;
    return image;
  }
}
#endif // EM_USE_SDLIMAGE

#if EM_USE_ALLEGRO
#include <allegro.h>
BITMAP * backbuffer = NULL;
ZBUFFER * zbuffer = NULL;
#endif // EM_USE_ALLEGRO


TextureUtil* TextureUtil::p_TextureUtil = NULL;

TextureUtil::TextureUtil() {
  m_colClear.r = 0.0f;
  m_colClear.g = 0.0f;
  m_colClear.b = 0.0f;
  m_colClear.a = 0.0f;
}

TextureUtil::~TextureUtil() {
  //freeTextures(); // TODO: check mem leaks here //!rzr
  //EM_COUT("TextureUtil::~TextureUtil",0);
}

TextureUtil* TextureUtil::getInstance() {
  if (p_TextureUtil == NULL) {
    p_TextureUtil = new TextureUtil();
  }
  return p_TextureUtil;
}

//!+rzr : this workaround a WIN32 bug // TODO: check (bugs possible)
void TextureUtil::freeTextures()  {
#if EM_USE_SDL
  //EM_COUT("+ TextureUtil::freeTextures",1);
  map<string,EmTexture*>::iterator i;
  for ( i = m_hEmTexture.begin();
        i != m_hEmTexture.end();
        i++) {
    glDeleteTextures (1, (GLuint*) ((*i).second) ); //is that correct ?
    free((*i).second);  // malloc -> free
    (*i).second = 0;
  }
  m_hEmTexture.clear();
  /// same pointers
  m_hImageName.clear();
#endif    // TODO ALLEGRO
}


// may solve the w32 bug on resize //TODO check it @w32
void TextureUtil::reloadTextures()  {
  //cout<<"+ TextureUtil::reloadTextures"<<endl;
#if EM_USE_SDL
  m_hImageName.clear();
  map<string,EmTexture*>::iterator i;
  for ( i = m_hEmTexture.begin();
        i != m_hEmTexture.end();
        i++) {
    glDeleteTextures (1, (GLuint*) ((*i).second) ); //is that correct ?
    genTexture( (*i).first.c_str() ,  (*i).second  );
    m_hImageName.insert(pair<EmTexture*,string>(  (*i).second, (*i).first ));
  }
#endif    // TODO ALLEGRO
  //EM_CERR("- TextureUtil::reloadTextures");
  //cout<<"- TextureUtil::reloadTextures"<<endl;
}

void TextureUtil::getFilename(list<string> & files) {
  map<EmTexture*,string>::iterator i;
  for ( i = m_hImageName.begin();
        i != m_hImageName.end();
        i++) {
    files.push_back( (*i).second);
  }
}
//!-rzr


void TextureUtil::initGrx() {
  Config * config = Config::getInstance();

#if EM_USE_SDL
  cerr << "Initing SDL" << endl << endl;
  if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
    cerr << "Couldn't initialize SDL video" << SDL_GetError() << endl;
    exit(1);
  }

  if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
    cerr << "Couldn't initialize SDL joystick: " <<  SDL_GetError() << endl << endl;
  } else {
    int njoystick = SDL_NumJoysticks();
    cerr << njoystick << " joysticks were found." << endl;
    if (njoystick != 0) {
      cerr << "The names of the joysticks are:" << endl;
      for(int a=0; a<njoystick; a++ ) {
        cerr << "  " << SDL_JoystickName(a) << endl;
      }
      cerr << "Using " << SDL_JoystickName(0) << endl << endl;
      SDL_JoystickOpen(0);
      SDL_JoystickEventState(SDL_ENABLE);
    }
  }

  // See if we should detect the display depth
  if ( SDL_GetVideoInfo()->vfmt->BitsPerPixel <= 8 ) {
    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 2 );
    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 3 );
    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 3 );
  } else        if ( SDL_GetVideoInfo()->vfmt->BitsPerPixel <= 16 ) {
    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
  }     else {
    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
  }

  SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
  SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

  /* Initialize the display */
  SDL_Surface* screen =
    SDL_SetVideoMode(config->getWidth(), config->getHeight(), config->getBpp(),
                     SDL_OPENGL
                     | (config->useFullScreen() ? SDL_FULLSCREEN : 0));

  //    if (config->useFullScreen()) {
  SDL_ShowCursor(SDL_DISABLE);
  //    }
  SDL_WM_SetCaption("Emilia Pinball", NULL);

  if (screen == NULL) {
    cerr << "Couldn't set video mode: " << SDL_GetError() << endl;
    exit(1);
  }

  cerr << "Vendor     : " << glGetString( GL_VENDOR ) << endl;
  cerr << "Renderer   : " << glGetString( GL_RENDERER ) << endl;
  cerr << "Version    : " << glGetString( GL_VERSION ) << endl;
  cerr << "Extensions : " << glGetString( GL_EXTENSIONS ) << endl << endl;
  //TODO: that would be usefull to report CPU/RAM specs also //!rzr

  int value;
  SDL_GL_GetAttribute( SDL_GL_RED_SIZE, &value );
  cerr << "SDL_GL_RED_SIZE: " << value << endl;
  SDL_GL_GetAttribute( SDL_GL_GREEN_SIZE, &value );
  cerr << "SDL_GL_GREEN_SIZE: " << value << endl;
  SDL_GL_GetAttribute( SDL_GL_BLUE_SIZE, &value );
  cerr << "SDL_GL_BLUE_SIZE: " << value << endl;
  SDL_GL_GetAttribute( SDL_GL_DEPTH_SIZE, &value );
  cerr << "SDL_GL_DEPTH_SIZE: " << value << endl;
  SDL_GL_GetAttribute( SDL_GL_DOUBLEBUFFER, &value );
  cerr << "SDL_GL_DOUBLEBUFFER: " << value << endl << endl;

  this->resizeView(config->getWidth(), config->getHeight());
#endif // EM_USE_SDL

#if EM_USE_ALLEGRO
  //config->setSize(320, 240);

  allegro_init();
  install_keyboard();
  install_timer();
  install_mouse();

  COLOR_MAP colorMap;
  RGB_MAP rgbMap;
  COLOR_MAP transMap;

  RGB* paPalette = (RGB*) calloc(256, sizeof(RGB));
  generate_332_palette(paPalette);
  // create rgb table
  create_rgb_table(&rgbMap, paPalette, NULL);
  rgb_map = &rgbMap;
  // create light table and setup the truecolor blending functions.
  create_light_table(&colorMap, paPalette, 0, 0, 0, NULL);
  color_map = &colorMap;
  // texture and flat polygons are 50% transparent
  create_trans_table(&transMap, paPalette, 128, 128, 128, NULL);
  set_trans_blender(0, 0, 0, 128);
  // set the graphics mode
  int tc = GFX_AUTODETECT_WINDOWED, tw = config->getWidth();
  int th = config->getHeight(), tbpp = 16;
  /*
    set_gfx_mode(GFX_SAFE, 320, 200, 0, 0);
    set_palette(desktop_palette);

    if (!gfx_mode_select_ex(&tc, &tw, &th, &tbpp)) {
    allegro_exit();
    cerr << "Error setting safe graphics mode" << endl;
    }
  */
  set_color_depth(tbpp);
  if (set_gfx_mode(tc, tw, th, 0, 0) != 0) {
    allegro_exit();
    cerr << "Error setting graphics mode " << endl << allegro_error << endl;
  }
  set_palette(paPalette);
  config->setSize(tw, th);
  set_projection_viewport(0, 0, tw, th);
  // Create back buffer.
  backbuffer = create_bitmap(tw, th);
  clear(backbuffer);
  zbuffer = create_zbuffer(backbuffer);
  set_zbuffer(zbuffer);
  clear_zbuffer(zbuffer, 0);
#endif // EM_USE_ALLEGRO
}

void TextureUtil::stopGrx() {
  cerr << "Stopping graphics...";
#if EM_USE_SDL
  SDL_Quit();
#endif
#if EM_USE_ALLEGRO
  allegro_exit();
#endif
  cerr << "ok." << endl;
}

void TextureUtil::setClearColor(float r, float g, float b, float a) {
  m_colClear.r = r;
  m_colClear.g = g;
  m_colClear.b = b;
  m_colClear.a = a;
#if EM_USE_SDL
  glClearColor(m_colClear.r, m_colClear.g, m_colClear.b, m_colClear.a);
#endif
}

void TextureUtil::resizeView(unsigned int w, unsigned int h) {
#if EM_USE_SDL
  glViewport(0, 0, (GLsizei) w, (GLsizei) h);

  glClearColor(m_colClear.r, m_colClear.g, m_colClear.b, m_colClear.a);
  glClearDepth(1.0);

  glShadeModel(GL_SMOOTH);

  glEnable(GL_CULL_FACE);
  glCullFace(GL_BACK);
  glFrontFace(GL_CW);

  //    glDisable(GL_DITHER);
  glDisable(GL_LIGHTING);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  float ratio = Config::getInstance()->getRatio();
  if (ratio>=1 || ratio<=0) ratio=1; //TODO

  glFrustum(-EM_RIGHT*EM_NEAR*ratio, EM_RIGHT*EM_NEAR*ratio,
	    -EM_UP*EM_NEAR, EM_UP*EM_NEAR,
            EM_NEAR, EM_FAR);
  glMatrixMode(GL_MODELVIEW);

#if OPENGL_LIGHTS
  glEnable(GL_LIGHTING);
#endif

#endif // EM_USE_SDL
}


EmImage* TextureUtil::loadImage(const char* filename) {
#if EM_USE_SDL
  struct_image * image = loadP(filename);
  if (image == NULL) {
    cerr << "TextureUtil::loadImage unable to load " << filename << endl;
  }
  return image;
#endif // EM_USE_SDL
#if EM_USE_ALLEGRO
  RGB pal[256];
  BITMAP * bm = load_bitmap(filename, pal);
  if (bm == NULL) {
    cerr << "TextareUtil::loadImage unable to load " << filename << " : " << allegro_error << endl;
  }
  return bm;
#endif // EM_USE_ALLEGRO
}
int TextureUtil::genTexture( char const * const filename,
                             EmTexture * const texture)
{
  //cout<<"+ Texture::genTexture : "<<filename<<endl;
  *texture = 0;

#if EM_USE_SDL
  // Load Texture
  struct_image* image = 0;

  // load the texture
  image = loadP(filename);
  if (image == NULL) {
    cerr << "TextureUtil::loadTexture error loading file " << filename << endl;
    return -1;
  }
  //TODO : Pad texture != 2^n x 2^n //!rzr

  //cout<<" Create Texture"<<endl;
  glGenTextures(1, texture);
  glBindTexture(GL_TEXTURE_2D, *texture);


  // 2d texture, level of detail 0 (normal), 3 components (red, green, blue),
  // x size from image, y size from image,
  // border 0 (normal), rgb color data, unsigned byte data,
  // and finally the data itself.x
  GLenum comp;
  switch (image->channels) {
  case 3: comp = GL_RGB; break;
  case 4: comp = GL_RGBA; break;
  default: comp = GL_RGB;
    cerr << "TextureUtil::loadTexture unknown image format" << endl;
    return -1;
  }


  glTexImage2D(GL_TEXTURE_2D, 0, comp, image->width, image->height, 0, comp,
               GL_UNSIGNED_BYTE, image->pixels);

  EM_COUT("loaded texture: " << filename, 1);
  EM_COUT("size " << image->width <<" "<< image->height, 1);
  //    EM_COUT("bytes per pixel " << (int)image->format->BytesPerPixel, 1);
  //    EM_COUT("bits per pixel " << (int)image->format->BitsPerPixel, 1);

#endif
  EM_COUT("- Texture::genTexture : "<<filename<<hex<<texture,0);
  return 1;
}


EmTexture* TextureUtil::loadTexture(const char* filename) {
  EmTexture* texture = 0;
#if EM_USE_SDL
  // look if the texture is already loaded
  if (m_hEmTexture.find(string(filename)) != m_hEmTexture.end()) {
    EM_COUT("TextureUtil::loadTexture found texture "
            << filename << " in cache", 0);
    map<string, EmTexture*>::iterator element
      = m_hEmTexture.find(string(filename));
    return (*element).second;
  }

  texture =  new EmTexture;
  int t = genTexture( filename, texture); //!rzr {
  if ( t < 0 ) { delete texture; return 0; } // could have been written better

  //EM_GLERROR(" In TextureUtil::loadTexture ");
  // insert the texture into the cache
  m_hEmTexture.insert(pair<string, EmTexture*>(string(filename), texture));
  m_hImageName.insert(pair<EmTexture*, string>(texture, string(filename)));
  //cout<<"- TextureUtil::loadTexture"<<endl;
  return texture;
#endif // EM_USE_SDL
#if EM_USE_ALLEGRO
  RGB pal[256];
  BITMAP * bm = load_bitmap(filename, pal);
  if (bm == NULL) {
    cerr << "TextureUtil::loadTexture Unable to load "
         << filename << " : " << allegro_error << endl;
  }
  return bm;
#endif // EM_USE_ALLEGRO
  return texture;
}

const char * TextureUtil::getTextureName(EmTexture * tex) {
  map<EmTexture*, string>::iterator element = m_hImageName.find(tex);
  if (element != m_hImageName.end()) {
    return (*element).second.c_str();
  }
  cerr << "TextureUtil::getTextureName could not find image name" << endl;
  return NULL;
}
/*
  void TextureUtil::load(list<string> & files)
  {
  list<string>::iterator is = files.begin();
  for ( ; is != files.end() ; is++ )
  loadTexture( (*is).c_str() );
  }
*/

// #eof "$Id: TextureUtil.cpp $