.[ ČeskéHry.cz ].

Laboratoř ČeskýchHer.cz - PasteBin

Vložit nový kód

ČeskéHry.cz - KOMUNITA HERNÍCH VÝVOJÁŘŮ

  1. T3en y1oung collection Cute te3ns
    2 hod
  2. CP lovers forum
    2 hod
  3. axcae5hljh
    4 hod
  4. Watch THe Movies online free streaming subtitle english
    7 hod
  5. cerasiyud
    8 hod
  6. ADSSRGFG
    10 hod
  7. ADFAEDFG
    11 hod
  8. 2025102304h59min59sec
    12 hod
  9. 2025102304h47min42sec
    12 hod
  10. adsdsfg
    12 hod
Link: http://nopaste.ceske-hry.cz/subdom/nopaste862
Zaslal: Weny Sky
Jazyk: C++
Vloženo: 2.2.2008, 11:04
Stáhnout jako soubor
  1. //-----------------------------------------------------------------------------
  2. // Name: BAMWE::loadTexture(int TID, const char* name)
  3. // Desc: nacte texturu
  4. //-----------------------------------------------------------------------------
  5. int BAMWE::loadTexture(int TID, const char* name)
  6. {
  7. SDL_Surface *surface;
  8. GLenum texture_format;
  9. GLint num_of_colors;
  10. size_t name_length;
  11. SDL_RWops *rwop;
  12. std::map<int,bool>::iterator active_texture;
  13. if(name == NULL)
  14. {
  15. log(__FILE__, __LINE__, "NULL pointer");
  16. return -1;
  17. }
  18.  
  19. // ziskani noveho TID
  20. if(TID == BAMWE_TEXTURE_DEFAULT)
  21. {
  22. do
  23. {
  24. TID = free_TID.top();
  25. free_TID.pop();
  26. } while (used_textures.find(TID) != used_textures.end());
  27. }
  28. else if((active_texture = used_textures.find(TID)) != used_textures.end() && active_texture->second)
  29. {
  30. const unsigned int deleted_textures[1] = {textures[TID]};
  31. log(NULL, 0, "Texture \"%s\" with ID:%d was replaced by texture \"%s\"", textures_files[TID].c_str(), TID, name);
  32. glDeleteTextures(1, deleted_textures); log_error();
  33. }
  34.  
  35. name_length = strlen(name);
  36.  
  37. if(name_length <= 4)
  38. {
  39. log(NULL, 0, "Bad texture name length (%s)", name);
  40. return -1;
  41. }
  42.  
  43. if(name[name_length - 4] != '.')
  44. {
  45. log(NULL, 0, "Bad file name format (%s)", name);
  46. return -1;
  47. }
  48.  
  49. // nacitaji se normalni nekomprimovane textury
  50. if(strcmp(&name[name_length-3], "dds") != 0)
  51. {
  52. rwop = SDL_RWFromFile(name, "rb");
  53.  
  54. if(rwop == NULL)
  55. {
  56. log(NULL, 0, "Texture (%s): %s", name, SDL_GetError( ));
  57. return -1;
  58. }
  59.  
  60. if(strcmp(&name[name_length-3], "png") == 0)
  61. surface = IMG_LoadPNG_RW(rwop);
  62. else if(strcmp(&name[name_length-3], "tga") == 0)
  63. surface = IMG_LoadTGA_RW(rwop);
  64. else if(strcmp(&name[name_length-3], "jpg") == 0)
  65. surface = IMG_LoadJPG_RW(rwop);
  66. else if(strcmp(&name[name_length-3], "bmp") == 0)
  67. surface = IMG_LoadBMP_RW(rwop);
  68. else
  69. {
  70. log(NULL, 0, "Unrecognized texture format(%s). Try to lower case the file extension.", name);
  71. SDL_FreeRW(rwop);
  72. return -1;
  73. }
  74.  
  75. if(!surface)
  76. {
  77. log(NULL, 0, "Texture (%s) load error : %s", name, IMG_GetError());
  78. SDL_FreeRW(rwop);
  79. return -1;
  80. }
  81.  
  82. SDL_FreeRW(rwop);
  83.  
  84. // kontrola, jestli je sirka nasobkem dvou
  85. if ( (surface->w & (surface->w - 1)) != 0 )
  86. log(__FILE__, __LINE__, "WARNING: %s's width is not a power of 2", name);
  87. // kontrola, jestli je vyska nasobkem dvou
  88. if ( (surface->h & (surface->h - 1)) != 0 )
  89. log(__FILE__, __LINE__, "WARNING: %s's height is not a power of 2", name);
  90. // urce barevnych kanalu
  91. num_of_colors = surface->format->BytesPerPixel;
  92. if (num_of_colors == 4) // s alfakanalem
  93. {
  94. if (surface->format->Rmask == 0x000000ff)
  95. texture_format = GL_RGBA;
  96. else
  97. texture_format = GL_BGRA;
  98. }
  99. else if (num_of_colors == 3) // bez alfakanalu
  100. {
  101. if (surface->format->Rmask == 0x000000ff)
  102. texture_format = GL_RGB;
  103. else
  104. texture_format = GL_BGR;
  105. }
  106. else
  107. {
  108. log(NULL, 0, "Texture (%s) bad color depth", name);
  109. SDL_FreeSurface(surface);
  110. return -1;
  111. }
  112. // vygenerovani ukazatele pro aktualni texturu
  113. glGenTextures( 1, &textures[TID] ); log_error();
  114. // bindnuti textury
  115. glBindTexture( GL_TEXTURE_2D, textures[TID] ); log_error();
  116. // nastaveni parametru textury
  117. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); log_error();
  118. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); log_error();
  119. // nahrani dat do textury
  120. glTexImage2D( GL_TEXTURE_2D, 0, num_of_colors, surface->w, surface->h, 0,
  121. texture_format, GL_UNSIGNED_BYTE, surface->pixels ); log_error();
  122.  
  123. #ifdef _DEBUG
  124. log(0,0,"Create texture ID(%d) from %s : %dx%d-%db - %d", TID, name, surface->w, surface->h, num_of_colors*8, texture_format);
  125. #endif
  126.  
  127. textures_width[TID] = surface->w;
  128. textures_height[TID] = surface->h;
  129.  
  130. SDL_FreeSurface( surface );
  131.  
  132. used_textures[TID] = true;
  133. textures_files[TID] = name;
  134. return TID;
  135. }
  136. else // nacteni komprimovane textury
  137. {
  138. DDS_IMAGE_DATA *pDDSImageData = loadDDSTextureFile( name );
  139.  
  140. if( pDDSImageData != NULL )
  141. {
  142. int nHeight = pDDSImageData->height;
  143. int nWidth = pDDSImageData->width;
  144. int nNumMipMaps = pDDSImageData->numMipMaps;
  145.  
  146. int nBlockSize;
  147.  
  148. if( pDDSImageData->format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT )
  149. nBlockSize = 8;
  150. else
  151. nBlockSize = 16;
  152.  
  153. #ifdef _DEBUG
  154. log(0,0,"Create texture ID(%d) from %s : %dx%d - %d", TID, name, pDDSImageData->width,pDDSImageData->height, pDDSImageData->format);
  155. #endif
  156.  
  157. textures_width[TID] = pDDSImageData->width;
  158. textures_height[TID] = pDDSImageData->height;
  159.  
  160. glGenTextures( 1, &textures[TID] ); log_error();
  161. glBindTexture( GL_TEXTURE_2D, textures[TID]); log_error();
  162.  
  163. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); log_error();
  164. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); log_error();
  165.  
  166. int nSize;
  167. int nOffset = 0;
  168.  
  169. // Nacteni mip-map
  170.  
  171. for( int i = 0; i < nNumMipMaps; ++i )
  172. {
  173. if( nWidth == 0 ) nWidth = 1;
  174. if( nHeight == 0 ) nHeight = 1;
  175.  
  176. nSize = ((nWidth+3)/4) * ((nHeight+3)/4) * nBlockSize;
  177.  
  178. glCompressedTexImage2DARB( GL_TEXTURE_2D,
  179. i,
  180. pDDSImageData->format,
  181. nWidth,
  182. nHeight,
  183. 0,
  184. nSize,
  185. pDDSImageData->pixels + nOffset ); log_error();
  186.  
  187. nOffset += nSize;
  188.  
  189. // polovicni velikost textury pro dalsi mip-map
  190. nWidth = (nWidth / 2);
  191. nHeight = (nHeight / 2);
  192. }
  193. }
  194.  
  195. if( pDDSImageData != NULL )
  196. {
  197. if( pDDSImageData->pixels != NULL )
  198. delete [] pDDSImageData->pixels;
  199. delete pDDSImageData;
  200. }
  201.  
  202. used_textures[TID] = true;
  203. textures_files[TID] = name;
  204. return TID;
  205. }
  206. }
  207.