--- deliantra/Deliantra-Client/texcache.c 2006/07/04 23:56:34 1.2 +++ deliantra/Deliantra-Client/texcache.c 2007/12/25 18:58:32 1.5 @@ -1,4 +1,5 @@ // all these must be powers of two +// width and height better be <=256 as we use bytes in the rendercache #define TC_WIDTH 256 #define TC_HEIGHT 256 #define TC_ROUND 4 @@ -24,6 +25,7 @@ struct tc_texture *next; GLuint name; int avail; + char *saved; /* stores saved texture data */ } tc_texture; typedef struct tc_slice { @@ -35,7 +37,7 @@ static tc_texture *first_texture; void -tc_clear () +tc_clear (void) { int i; @@ -45,7 +47,7 @@ while (first_texture) { tc_texture *next = first_texture->next; - glDeleteTextures (1, &first_texture->name); + del_texture (first_texture->name); g_slice_free (tc_texture, first_texture); first_texture = next; } @@ -54,6 +56,40 @@ } void +tc_backup (void) +{ + tc_texture *tex = first_texture; + while (tex) + { + tex->saved = g_slice_alloc (TC_WIDTH * TC_HEIGHT); + + glBindTexture (GL_TEXTURE_2D, tex->name); + glGetTexImage (GL_TEXTURE_2D, 0, GL_ALPHA, GL_UNSIGNED_BYTE, tex->saved); + + tex = tex->next; + } +} + +void +tc_restore (void) +{ + tc_texture *tex = first_texture; + + while (tex) + { + glBindTexture (GL_TEXTURE_2D, tex->name); + glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, TC_WIDTH, TC_HEIGHT, 0, GL_ALPHA, GL_UNSIGNED_BYTE, tex->saved); + + g_slice_free1 (TC_WIDTH * TC_HEIGHT, tex->saved); + tex->saved = 0; + + tex = tex->next; + } +} + +void tc_get (tc_area *area, int width, int height) { int slice_height = MIN (height + TC_ROUND - 1, TC_HEIGHT) & ~(TC_ROUND - 1); @@ -77,10 +113,11 @@ if (!match) { match = g_slice_new (tc_texture); - match->next = first_texture; + match->next = first_texture; first_texture = match; - glGenTextures (1, &match->name); - match->avail = TC_HEIGHT; + match->name = gen_texture (); + match->avail = TC_HEIGHT; + match->saved = 0; glBindTexture (GL_TEXTURE_2D, match->name); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);