31#include <srSurfaceIOManager.hpp>
34static TextureFileClass* head;
35static bool mipmaps=
true;
36static int texture_count;
37static int locked_surface_count;
43unsigned int TextureFileClass::_CurrentTimeStamp = 0U;
44float TextureFileClass::_SwitchThreshold = 0.75f;
48static int Calculate_Size(srColorSurfaceIFace* surface,
int red_factor=0)
51 int p=surface->getPitch();
52 int h=surface->getHeight();
66void TextureFileClass::Switch_Mipmaping_Debug()
69 TextureFileClass* n=head;
71 if (mipmaps) n->params.setMipmap(MIPMAP_DEFAULT);
72 else n->params.setMipmap(MIPMAP_NONE);
79int TextureFileClass::Get_Total_Locked_Surface_Size()
81 int total_locked_surface_size=0;
82 TextureFileClass* n=head;
84 total_locked_surface_size+=n->Get_Locked_Surface_Size();
87 return total_locked_surface_size;
92int TextureFileClass::Get_Total_Texture_Size()
94 int total_texture_size=0;
95 TextureFileClass* n=head;
97 total_texture_size+=n->Get_Texture_Size();
100 return total_texture_size;
105int TextureFileClass::Get_Total_Non_Reduced_Texture_Size()
107 int total_texture_size=0;
108 TextureFileClass* n=head;
110 total_texture_size+=n->Get_Non_Reduced_Texture_Size();
113 return total_texture_size;
118int TextureFileClass::Get_Total_Texture_Count()
120 return texture_count;
125int TextureFileClass::Get_Total_Locked_Surface_Count()
127 return locked_surface_count;
134 TextureFileClass* n=head;
136 "Missing textures\n\n"
138 "------------------------\n";
141 if (n->Get_File_Error()) {
143 tmp.
Format(
"%4.4d %s\n",n->ID(),n->Get_File_Name());
148 if (!missing_count) s=
"No missing textures\n\n";
151 tmp.
Format(
"\nTotal missing textures: %d",missing_count);
159TextureFileClass::TextureFileClass(
const char * filename) :
161 LockedSurfaceReductionFactor(0),
162 LockedSurfaceSize(0),
164 NonReducedTextureSize(0),
165 CurrentReductionFactor(0),
166 DesiredReductionFactor(0.0f),
167 TimeStampOfLastRequestReductionCall(_CurrentTimeStamp - 1),
168 TimeStampOfLastProcessReductionCall(_CurrentTimeStamp - 1),
170 TextureFrameHandle(getNewFrameHandle()),
174 flash_store_surface(
NULL),
177 if(filename && *filename) {
185 if (!TextureLoader::Texture_File_Exists(FileName)) {
192 Load_Locked_Surface();
198 flags.clear(GENERATESURFACE_FAILURE);
199 flags.set(DIRTY_DEFAULTS);
201 if (WW3D::Get_Texture_Thumbnail_Mode()!=WW3D::TEXTURE_THUMBNAIL_MODE_RESIZING) {
202 ReductionEnabled=
false;
207TextureFileClass::~TextureFileClass()
210 if (flash_store_surface) flash_store_surface->release();
211 if (LockedSurface) locked_surface_count--;
212 TextureFileClass* n=head;
228 if (FileName)
delete [] FileName;
229 if (LockedSurface) LockedSurface->release();
234TextureFileClass::TextureFileClass(
const TextureFileClass & src)
239TextureFileClass & TextureFileClass::operator = (
const TextureFileClass &that)
243 srTexture::operator = (that);
251srTextureIFace::FrameHandle TextureFileClass::getTextureFrameHandle(
void)
253 if (flags.test(GENERATESURFACE_FAILURE))
256 return TextureFrameHandle;
259void TextureFileClass::getMipmapData(MultiRequest& m)
262 WWASSERT(m.largeLOD <= m.smallLOD && m.smallLOD < MAX_LOD);
263 WWASSERT_PRINT(m.levels[m.largeLOD],
"TextureFileClass::getMipmapData() -- NULL surface passed in!\n");
264 if (!m.levels[m.largeLOD])
return;
281 if (TempSurfacePtr) {
282 Fill_Multi_Request_From_Surface(m, TempSurfacePtr);
283 Release_Temp_Surface();
286 NonReducedTextureSize=0;
288 Fill_Multi_Request_From_Surface(m, LockedSurface);
289 if (CurrentReductionFactor < LockedSurfaceReductionFactor) {
291 CurrentReductionFactor = LockedSurfaceReductionFactor;
292 float des_red_factor = floor(DesiredReductionFactor + 0.5f);
294 unsigned int int_desired_reduction_factor = (int)des_red_factor;
298 if (int_desired_reduction_factor < LockedSurfaceReductionFactor) {
299 texture_loader_info.reduction_factor = int_desired_reduction_factor;
300 TextureLoader::Add_Load_Task(
this);
311 if (TempSurfacePtr) {
312 TextureSize=Calculate_Size(TempSurfacePtr);
313 NonReducedTextureSize=TextureSize;
315 Fill_Multi_Request_From_Surface(m, TempSurfacePtr);
316 Release_Temp_Surface();
322void TextureFileClass::invalidate(
void)
324 Release_Temp_Surface();
325 invalidateFrameHandle(TextureFrameHandle);
326 flags.clear(GENERATESURFACE_FAILURE);
329void TextureFileClass::setupDefaultValues(
void)
331 if (flags.test(DIRTY_DEFAULTS))
333 flags.clear (DIRTY_DEFAULTS);
349 if (TempSurfacePtr) {
350 setupDefaultValuesFromSurface(TempSurfacePtr);
351 TextureSize=Calculate_Size(TempSurfacePtr);
352 NonReducedTextureSize=Calculate_Size(TempSurfacePtr,CurrentReductionFactor);
355 setupDefaultValuesFromSurface(LockedSurface);
356 if (CurrentReductionFactor >= LockedSurfaceReductionFactor) {
358 unsigned int diff = CurrentReductionFactor - LockedSurfaceReductionFactor;
359 defaultDimensions.width = defaultDimensions.width >> diff;
360 if (defaultDimensions.width == 0) defaultDimensions.width = 1;
361 defaultDimensions.height = defaultDimensions.height >> diff;
362 if (defaultDimensions.height == 0) defaultDimensions.height = 1;
364 CurrentReductionFactor = LockedSurfaceReductionFactor;
367 NonReducedTextureSize=0;
370 if (TempSurfacePtr) {
371 setupDefaultValuesFromSurface(TempSurfacePtr);
372 TextureSize=Calculate_Size(TempSurfacePtr);
373 NonReducedTextureSize=TextureSize;
380void TextureFileClass::Apply_New_Surface()
383 TempSurfacePtr = texture_loader_info.new_surface;
384 texture_loader_info.new_surface = 0;
385 flags.set(DIRTY_DEFAULTS);
386 CurrentReductionFactor = texture_loader_info.reduction_factor;
391void TextureFileClass::Request_Reduction(
float reduction_factor)
393 if (!ReductionEnabled)
return;
395 if (reduction_factor < 0.0f) reduction_factor = 0.0f;
399 if (TimeStampOfLastRequestReductionCall != _CurrentTimeStamp) {
401 DesiredReductionFactor = reduction_factor;
404 TimeStampOfLastRequestReductionCall = _CurrentTimeStamp;
407 DesiredReductionFactor =
MIN(reduction_factor, DesiredReductionFactor);
417void TextureFileClass::Process_Reduction(
void)
420 if (TimeStampOfLastProcessReductionCall != _CurrentTimeStamp) {
424 if (LockedSurface && fabs(
float(CurrentReductionFactor) - DesiredReductionFactor) > _SwitchThreshold) {
427 float des_red_factor = floor(DesiredReductionFactor + 0.5f);
429 unsigned int int_desired_reduction_factor = (int)des_red_factor;
430 if (int_desired_reduction_factor >= LockedSurfaceReductionFactor) {
432 CurrentReductionFactor = int_desired_reduction_factor;
436 texture_loader_info.reduction_factor = int_desired_reduction_factor;
437 TextureLoader::Add_Load_Task(
this);
442 TimeStampOfLastProcessReductionCall = _CurrentTimeStamp;
447void TextureFileClass::_Set_Switch_Threshold(
float switch_threshold)
449 _SwitchThreshold =
MIN(switch_threshold, 0.999);
450 _SwitchThreshold =
MAX(_SwitchThreshold, 0.5);
453float TextureFileClass::_Get_Switch_Threshold(
void)
455 return _SwitchThreshold;
458void TextureFileClass::Load_Temp_Surface(
void)
460 if (TempSurfacePtr) invalidate();
462 if (!FileName)
return;
472 cache->Validate_Texture(FileName);
473 TempSurfacePtr = cache->Get_Surface(
477 TempSurfacePtr = ::Load_Surface(FileName);
481void TextureFileClass::Release_Temp_Surface(
void)
485 TempSurfacePtr->release();
490void TextureFileClass::Fill_Multi_Request_From_Surface(MultiRequest& m, srColorSurfaceIFace* surface)
492 m.levels[m.largeLOD]->copy (*surface);
493 for (LOD i = m.largeLOD+1; i <= m.smallLOD; i++)
495 WWASSERT(m.levels[i] && m.levels[i-1]);
496 if (m.levels[i] && m.levels[i-1])
497 m.levels[i]->copy (*(m.levels[i-1]));
504void TextureFileClass::Set_Texture_Flash(
bool b)
509 flash_store_surface=TempSurfacePtr;
510 flash_store_surface->addReference();
514 invalidateFrameHandle(TextureFrameHandle);
515 Release_Temp_Surface();
516 TempSurfacePtr=flash_store_surface;
517 flash_store_surface=
NULL;
522bool TextureFileClass::Get_Texture_Flash()
const
527TextureFileClass* TextureFileClass::Get_Texture(
int id)
529 TextureFileClass* n=head;
531 if (n->ID()==
id)
return n;
537static unsigned latest_time;
538static bool flash_state;
539static unsigned flash_counter;
540const int flash_time=500;
542void TextureFileClass::Update_Texture_Flash()
545 int delta=cur_time-latest_time;
546 latest_time=cur_time;
547 flash_counter+=delta;
548 if (flash_counter>flash_time) {
549 flash_counter-=flash_time;
550 if (flash_counter>flash_time) flash_counter=0;
551 flash_state=!flash_state;
555 srColorSurface* s=
NULL;
557 int w=srCore.getSurface()->getWidth();
558 srColorSurfaceIFace::PixelFormat pf;
559 srCore.getSurface()->getPixelFormat(pf);
560 s=
W3DNEW srColorSurface(pf,w,w);
561 s->copy(*srCore.getSurface());
564 TextureFileClass* n=head;
567 n->invalidateFrameHandle(n->TextureFrameHandle);
568 n->Release_Temp_Surface();
574 n->TempSurfacePtr=n->flash_store_surface;
575 n->TempSurfacePtr->addReference();
585void TextureFileClass::Enable_Reduction(
bool b)
587 if (ReductionEnabled==b)
return;
590 Load_Locked_Surface();
593 if (LockedSurface) LockedSurface->release();
595 LockedSurfaceReductionFactor=0;
596 CurrentReductionFactor=0;
597 DesiredReductionFactor=0;
600 if (TempSurfacePtr) {
601 setupDefaultValuesFromSurface(TempSurfacePtr);
602 TextureSize=Calculate_Size(TempSurfacePtr);
603 NonReducedTextureSize=TextureSize;
608void TextureFileClass::Load_Locked_Surface()
610 if (LockedSurface) LockedSurface->release();
612 LockedSurfaceReductionFactor=0;
614 TextureLoader::Load_Locked_Surface_Immediate(FileName, LockedSurface, LockedSurfaceReductionFactor);
621 CurrentReductionFactor = LockedSurfaceReductionFactor;
623 LockedSurfaceSize=Calculate_Size(LockedSurface);
624 locked_surface_count++;
int _cdecl Format(const TCHAR *format,...)
static WW3DAssetManager * Get_Instance(void)
static unsigned int Get_Sync_Time(void)
char * nstrdup(const char *str)
#define WWASSERT_PRINT(expr, string)