ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/texcache.c
(Generate patch)

Comparing deliantra/Deliantra-Client/texcache.c (file contents):
Revision 1.1 by root, Tue Jul 4 23:23:32 2006 UTC vs.
Revision 1.5 by root, Tue Dec 25 18:58:32 2007 UTC

1// all these must be powers of two 1// all these must be powers of two
2// width and height better be <=256 as we use bytes in the rendercache
2#define TC_WIDTH 256 3#define TC_WIDTH 256
3#define TC_HEIGHT 256 4#define TC_HEIGHT 256
4#define TC_ROUND 4 5#define TC_ROUND 4
5 6
6typedef struct { 7typedef struct {
7 GLuint name; 8 GLuint name;
8 int x, y, w, h; 9 int x, y, w, h;
9} tc_area; 10} tc_area;
10 11
12extern int tc_generation;
13
11void tc_get (tc_area *area, int width, int height); 14void tc_get (tc_area *area, int width, int height);
12void tc_put (tc_area *area); 15void tc_put (tc_area *area);
13void tc_clear (); 16void tc_clear ();
14 17
15///////////////////////////////////////////////////////////////////////////// 18/////////////////////////////////////////////////////////////////////////////
16 19
17#include <glib.h> 20#include <glib.h>
18 21
22int tc_generation;
23
19typedef struct tc_texture { 24typedef struct tc_texture {
20 struct tc_texture *next; 25 struct tc_texture *next;
21 GLuint name; 26 GLuint name;
22 int avail; 27 int avail;
28 char *saved; /* stores saved texture data */
23} tc_texture; 29} tc_texture;
24 30
25typedef struct tc_slice { 31typedef struct tc_slice {
26 GLuint name; 32 GLuint name;
27 int avail, y; 33 int avail, y;
29 35
30static tc_slice slices[TC_HEIGHT / TC_ROUND]; 36static tc_slice slices[TC_HEIGHT / TC_ROUND];
31static tc_texture *first_texture; 37static tc_texture *first_texture;
32 38
33void 39void
34tc_clear () 40tc_clear (void)
35{ 41{
36 int i; 42 int i;
37 43
38 for (i = TC_HEIGHT / TC_ROUND; i--; ) 44 for (i = TC_HEIGHT / TC_ROUND; i--; )
39 slices [i].name = 0; 45 slices [i].name = 0;
40 46
41 while (first_texture) 47 while (first_texture)
42 { 48 {
43 tc_texture *next = first_texture->next; 49 tc_texture *next = first_texture->next;
44 glDeleteTextures (1, &first_texture->name); 50 del_texture (first_texture->name);
45 g_slice_free (tc_texture, first_texture); 51 g_slice_free (tc_texture, first_texture);
46 first_texture = next; 52 first_texture = next;
53 }
54
55 ++tc_generation;
56}
57
58void
59tc_backup (void)
60{
61 tc_texture *tex = first_texture;
62 while (tex)
63 {
64 tex->saved = g_slice_alloc (TC_WIDTH * TC_HEIGHT);
65
66 glBindTexture (GL_TEXTURE_2D, tex->name);
67 glGetTexImage (GL_TEXTURE_2D, 0, GL_ALPHA, GL_UNSIGNED_BYTE, tex->saved);
68
69 tex = tex->next;
70 }
71}
72
73void
74tc_restore (void)
75{
76 tc_texture *tex = first_texture;
77
78 while (tex)
79 {
80 glBindTexture (GL_TEXTURE_2D, tex->name);
81 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
82 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
83 glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, TC_WIDTH, TC_HEIGHT, 0, GL_ALPHA, GL_UNSIGNED_BYTE, tex->saved);
84
85 g_slice_free1 (TC_WIDTH * TC_HEIGHT, tex->saved);
86 tex->saved = 0;
87
88 tex = tex->next;
47 } 89 }
48} 90}
49 91
50void 92void
51tc_get (tc_area *area, int width, int height) 93tc_get (tc_area *area, int width, int height)
69 111
70 // create a new texture if necessary 112 // create a new texture if necessary
71 if (!match) 113 if (!match)
72 { 114 {
73 match = g_slice_new (tc_texture); 115 match = g_slice_new (tc_texture);
74 match->next = first_texture; 116 match->next = first_texture;
75 first_texture = match; 117 first_texture = match;
76 glGenTextures (1, &match->name); 118 match->name = gen_texture ();
77 match->avail = TC_HEIGHT; 119 match->avail = TC_HEIGHT;
120 match->saved = 0;
78 121
79 glBindTexture (GL_TEXTURE_2D, match->name); 122 glBindTexture (GL_TEXTURE_2D, match->name);
80 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 123 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
81 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 124 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
82 glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, TC_WIDTH, TC_HEIGHT, 0, GL_ALPHA, GL_UNSIGNED_BYTE, 0); 125 glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, TC_WIDTH, TC_HEIGHT, 0, GL_ALPHA, GL_UNSIGNED_BYTE, 0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines