spritecache.c
changeset 2305 f0856e98f7a5
parent 2244 e6b5ef68406d
child 2309 34824a8b336f
equal deleted inserted replaced
2304:f3309898aeb0 2305:f0856e98f7a5
    17 #define SPRITE_CACHE_SIZE 1024*1024
    17 #define SPRITE_CACHE_SIZE 1024*1024
    18 
    18 
    19 
    19 
    20 //#define WANT_SPRITESIZES
    20 //#define WANT_SPRITESIZES
    21 #define WANT_NEW_LRU
    21 #define WANT_NEW_LRU
    22 //#define WANT_LOCKED
       
    23 
    22 
    24 
    23 
    25 /* These are used in newgrf.c: */
    24 /* These are used in newgrf.c: */
    26 
    25 
    27 int _skip_sprites = 0;
    26 int _skip_sprites = 0;
    36 
    35 
    37 
    36 
    38 static void* _sprite_ptr[MAX_SPRITES];
    37 static void* _sprite_ptr[MAX_SPRITES];
    39 static uint16 _sprite_size[MAX_SPRITES];
    38 static uint16 _sprite_size[MAX_SPRITES];
    40 static uint32 _sprite_file_pos[MAX_SPRITES];
    39 static uint32 _sprite_file_pos[MAX_SPRITES];
    41 
       
    42 // This one is probably not needed.
       
    43 #if defined(WANT_LOCKED)
       
    44 static bool _sprite_locked[MAX_SPRITES];
       
    45 #endif
       
    46 
    40 
    47 #if defined(WANT_NEW_LRU)
    41 #if defined(WANT_NEW_LRU)
    48 static int16 _sprite_lru_new[MAX_SPRITES];
    42 static int16 _sprite_lru_new[MAX_SPRITES];
    49 #else
    43 #else
    50 static uint16 _sprite_lru[MAX_SPRITES];
    44 static uint16 _sprite_lru[MAX_SPRITES];
   269 	_sprite_xoffs[load_index] = _cur_sprite.x_offs;
   263 	_sprite_xoffs[load_index] = _cur_sprite.x_offs;
   270 	_sprite_yoffs[load_index] = _cur_sprite.y_offs;
   264 	_sprite_yoffs[load_index] = _cur_sprite.y_offs;
   271 #endif
   265 #endif
   272 
   266 
   273 	_sprite_ptr[load_index] = NULL;
   267 	_sprite_ptr[load_index] = NULL;
   274 #if defined(WANT_LOCKED)
       
   275 	_sprite_locked[load_index] = false;
       
   276 #endif
       
   277 
   268 
   278 #if defined(WANT_NEW_LRU)
   269 #if defined(WANT_NEW_LRU)
   279 	_sprite_lru_new[load_index] = 0;
   270 	_sprite_lru_new[load_index] = 0;
   280 #else
   271 #else
   281 	_sprite_lru[load_index] = 0xFFFF;
   272 	_sprite_lru[load_index] = 0xFFFF;
   524 			// Locate the sprite belonging to the next pointer.
   515 			// Locate the sprite belonging to the next pointer.
   525 			for (i = _sprite_ptr; *i != next->data; ++i) {
   516 			for (i = _sprite_ptr; *i != next->data; ++i) {
   526 				assert(i != endof(_sprite_ptr));
   517 				assert(i != endof(_sprite_ptr));
   527 			}
   518 			}
   528 
   519 
   529 			#ifdef WANT_LOCKED
       
   530 			if (_sprite_locked[i]) {
       
   531 				s = next;
       
   532 				continue;
       
   533 			}
       
   534 			#endif
       
   535 
       
   536 			*i = s->data; // Adjust sprite array entry
   520 			*i = s->data; // Adjust sprite array entry
   537 			// Swap this and the next block
   521 			// Swap this and the next block
   538 			temp = *s;
   522 			temp = *s;
   539 			memmove(s, next, next->size);
   523 			memmove(s, next, next->size);
   540 			s = NextBlock(s);
   524 			s = NextBlock(s);
   560 	DEBUG(spritecache, 2) ("DeleteEntryFromSpriteCache, inuse=%d", GetSpriteCacheUsage());
   544 	DEBUG(spritecache, 2) ("DeleteEntryFromSpriteCache, inuse=%d", GetSpriteCacheUsage());
   561 
   545 
   562 #if defined(WANT_NEW_LRU)
   546 #if defined(WANT_NEW_LRU)
   563 	cur_lru = 0xffff;
   547 	cur_lru = 0xffff;
   564 	for (i = 0; i != MAX_SPRITES; i++) {
   548 	for (i = 0; i != MAX_SPRITES; i++) {
   565 		if (_sprite_ptr[i] != 0 &&
   549 		if (_sprite_ptr[i] != NULL && _sprite_lru_new[i] < cur_lru) {
   566 				_sprite_lru_new[i] < cur_lru
       
   567 #if defined(WANT_LOCKED)
       
   568 				 && !_sprite_locked[i]
       
   569 #endif
       
   570 				) {
       
   571 			cur_lru = _sprite_lru_new[i];
   550 			cur_lru = _sprite_lru_new[i];
   572 			best = i;
   551 			best = i;
   573 		}
   552 		}
   574 	}
   553 	}
   575 #else
   554 #else
   576 	{
   555 	{
   577 	uint16 cur_lru = 0, cur_lru_cur = 0xffff;
   556 	uint16 cur_lru = 0, cur_lru_cur = 0xffff;
   578 	for (i = 0; i != MAX_SPRITES; i++) {
   557 	for (i = 0; i != MAX_SPRITES; i++) {
   579 		if (_sprite_ptr[i] == 0 ||
   558 		if (_sprite_ptr[i] == NULL || _sprite_lru[i] < cur_lru) continue;
   580 #if defined(WANT_LOCKED)
       
   581 				_sprite_locked[i] ||
       
   582 #endif
       
   583 				_sprite_lru[i] < cur_lru)
       
   584 					continue;
       
   585 
   559 
   586 		// Found a sprite with a higher LRU value, then remember it.
   560 		// Found a sprite with a higher LRU value, then remember it.
   587 		if (_sprite_lru[i] != cur_lru) {
   561 		if (_sprite_lru[i] != cur_lru) {
   588 			cur_lru = _sprite_lru[i];
   562 			cur_lru = _sprite_lru[i];
   589 			best = i;
   563 			best = i;