Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 7, 2005, 4:16:51 PM (18 years ago)
Author:
patrick
Message:

network: merged the trunk into the network with the command svn merge -r5824:HEAD ../trunk network, changes changed… bla bla..

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/network/src/lib/graphics/importer/texture.cc

    r5790 r5968  
    1919
    2020#include "debug.h"
    21 #include "graphics_engine.h"
    22 
     21
     22// INCLUDING SDL_Image
    2323#ifdef HAVE_SDL_IMAGE_H
    2424#include <SDL_image.h>
     
    4545}
    4646
     47
    4748/**
    4849 *  Destructor of a Texture
     
    6566bool Texture::loadImage(const char* imageName)
    6667{
    67   if (GraphicsEngine::texturesEnabled)
     68  if (Texture::texturesEnabled)
    6869    {
    6970      if (this->image != NULL)
     
    8081        {
    8182          SDL_Surface* tmpSurf;
    82           if (this->texture)
     83          if (this->texture != 0 && glIsTexture(this->texture))
    8384            glDeleteTextures(1, &this->texture);
    8485          // load the new Image to memory
     
    8788          {
    8889            PRINTF(4)("loading Image %s\n", imageName);
    89             if (this->prepareSurface(tmpSurf))
    90               loadTexToGL();
     90            bool hasAlpha;
     91            SDL_Surface* newSurf = this->prepareSurface(tmpSurf, hasAlpha);
     92            if (newSurf != NULL)
     93            {
     94              this->setSurface(newSurf);
     95              this->setAlpha(hasAlpha);
     96              this->setTexture(Texture::loadTexToGL(newSurf));
     97            }
     98
    9199            SDL_FreeSurface(tmpSurf);
    92100            return true;
     
    108116}
    109117
     118
     119/**
     120 * rebuilds the texture.
     121 * reloads the Texture from Memory to OpenGL.
     122 */
    110123bool Texture::rebuild()
    111124{
    112125  if (this->texture != 0)
    113126    {
    114       glDeleteTextures(1,&this->texture);
    115       this->texture = 0;
     127      if (glIsTexture(this->texture))
     128        glDeleteTextures(1,&this->texture);
     129      this->setTexture(0);
    116130    }
    117131
     
    119133    {
    120134      PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName());
    121       this->loadTexToGL();
     135      this->setTexture(loadTexToGL(this->image));
    122136    }
    123 
    124 }
    125 
     137}
     138
     139
     140/**
     141 * set the surface this Texture handles
     142 * @param newSurface the new Surface to set as the image for this Texture.
     143 *
     144 * This deletes the old version of the stored Texture,
     145 * and sets the newly given Surface as current.
     146 */
     147bool Texture::setSurface(SDL_Surface* newSurface)
     148{
     149  if (this->image != NULL)
     150    SDL_FreeSurface(this->image);
     151
     152  this->image = newSurface;
     153
     154  return (this->image != NULL);
     155}
     156
     157
     158bool Texture::texturesEnabled = true;
     159
     160/**
     161 * enables, disables textures
     162 * @param texturesEnabled true if the textures should be enabled
     163 */
     164void Texture::setTextureEnableState(bool texturesEnabled)
     165{
     166  Texture::texturesEnabled = texturesEnabled;
     167}
     168
     169
     170//////////////////////////////////////
     171// UTILITY FUNCTIONALITY OF TEXTURE //
     172//////////////////////////////////////
    126173/**
    127174 * converts surface to a new SDL_Surface, that is loadable by openGL
    128175 * @param surface the Surface to convert
     176 * @param hasAlpha if the newly created Surface has an alpha channel, true is returned otherwise false.
    129177 * @returns a !!new!! Surface, that is loadable by openGL.
    130178 */
    131 bool Texture::prepareSurface(SDL_Surface* surface)
     179SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha)
    132180{
    133181  PRINTF(4)("Loading texture to OpenGL-Environment.\n");
    134182
    135   SDL_Surface* putSurface;
     183  SDL_Surface* retSurface;
    136184  SDL_Rect area;
    137185  Uint32 saved_flags;
    138186  Uint8  saved_alpha;
    139187
    140   putSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
    141                                surface->w, surface->h,
    142                                32,
     188  hasAlpha = false;
     189  retSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
     190                                    surface->w, surface->h,
     191                                    32,
    143192#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
    144193                               0x000000FF,
     
    147196                               0xFF000000
    148197#else
    149                                0xFF000000,
     198                                   0xFF000000,
    150199                               0x00FF0000,
    151200                               0x0000FF00,
    152201                               0x000000FF
    153202#endif
    154                                );
    155   if ( putSurface == NULL )
     203                                   );
     204  if ( retSurface == NULL )
    156205  {
    157     this->setSurface(NULL);
    158     return false;
     206    return NULL;
    159207  }
    160208
     
    171219  area.w = surface->w;
    172220  area.h = surface->h;
    173   SDL_BlitSurface(surface, &area, putSurface, &area);
     221  SDL_BlitSurface(surface, &area, retSurface, &area);
    174222
    175223  /* Restore the alpha blending attributes */
     
    177225  {
    178226    SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
    179     this->bAlpha = true;
    180   }
    181 
    182   return (this->setSurface(putSurface));
    183 }
    184 
    185 bool Texture::setSurface(SDL_Surface* newSurface)
    186 {
    187   if (this->image != NULL)
    188     SDL_FreeSurface(this->image);
    189 
    190   this->image = newSurface;
    191 
    192   return (this->image != NULL);
     227    hasAlpha = true;
     228  }
     229
     230  return (retSurface);
    193231}
    194232
     
    199237 * @returns The ID of the texture.
    200238 */
    201 GLuint Texture::loadTexToGL ()
    202 {
    203   if (this->texture != 0 && glIsTexture(this->texture))
    204     glDeleteTextures(1, &this->texture);
    205   this->texture = 0;
    206 
    207   if (this->image == NULL)
     239GLuint Texture::loadTexToGL (const SDL_Surface* surface)
     240{
     241//   if (this->texture != 0 && glIsTexture(this->texture))
     242//     glDeleteTextures(1, &this->texture);
     243//   this->texture = 0;
     244
     245  GLuint texture;
     246
     247  if (surface == NULL)
    208248    return 0;
    209249
    210250  /* Create an OpenGL texture for the image */
    211   glGenTextures(1, &this->texture);
    212   glBindTexture(GL_TEXTURE_2D, this->texture);
     251  glGenTextures(1, &texture);
     252  glBindTexture(GL_TEXTURE_2D, texture);
    213253  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    214254  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     
    217257               0,
    218258               GL_RGBA,
    219                this->image->w, this->image->h,
     259               surface->w, surface->h,
    220260               0,
    221261               GL_RGBA,
    222262               GL_UNSIGNED_BYTE,
    223                this->image->pixels);
     263               surface->pixels);
    224264  // build the MipMaps
    225265  gluBuild2DMipmaps(GL_TEXTURE_2D,
    226266                    GL_RGBA,
    227                     this->image->w,
    228                     this->image->h,
     267                    surface->w,
     268                    surface->h,
    229269                    GL_RGBA,
    230270                    GL_UNSIGNED_BYTE,
    231                     this->image->pixels);
     271                    surface->pixels);
    232272  glBindTexture(GL_TEXTURE_2D, 0);
    233   return this->texture;
    234 }
     273  return texture;
     274}
Note: See TracChangeset for help on using the changeset viewer.