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 | |
6 | typedef struct { |
7 | typedef struct { |
… | |
… | |
22 | |
23 | |
23 | typedef struct tc_texture { |
24 | typedef struct tc_texture { |
24 | struct tc_texture *next; |
25 | struct tc_texture *next; |
25 | GLuint name; |
26 | GLuint name; |
26 | int avail; |
27 | int avail; |
|
|
28 | char *saved; /* stores saved texture data */ |
27 | } tc_texture; |
29 | } tc_texture; |
28 | |
30 | |
29 | typedef struct tc_slice { |
31 | typedef struct tc_slice { |
30 | GLuint name; |
32 | GLuint name; |
31 | int avail, y; |
33 | int avail, y; |
… | |
… | |
33 | |
35 | |
34 | static tc_slice slices[TC_HEIGHT / TC_ROUND]; |
36 | static tc_slice slices[TC_HEIGHT / TC_ROUND]; |
35 | static tc_texture *first_texture; |
37 | static tc_texture *first_texture; |
36 | |
38 | |
37 | void |
39 | void |
38 | tc_clear () |
40 | tc_clear (void) |
39 | { |
41 | { |
40 | int i; |
42 | int i; |
41 | |
43 | |
42 | for (i = TC_HEIGHT / TC_ROUND; i--; ) |
44 | for (i = TC_HEIGHT / TC_ROUND; i--; ) |
43 | slices [i].name = 0; |
45 | slices [i].name = 0; |
44 | |
46 | |
45 | while (first_texture) |
47 | while (first_texture) |
46 | { |
48 | { |
47 | tc_texture *next = first_texture->next; |
49 | tc_texture *next = first_texture->next; |
48 | glDeleteTextures (1, &first_texture->name); |
50 | del_texture (first_texture->name); |
49 | g_slice_free (tc_texture, first_texture); |
51 | g_slice_free (tc_texture, first_texture); |
50 | first_texture = next; |
52 | first_texture = next; |
51 | } |
53 | } |
52 | |
54 | |
53 | ++tc_generation; |
55 | ++tc_generation; |
|
|
56 | } |
|
|
57 | |
|
|
58 | void |
|
|
59 | tc_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 | |
|
|
73 | void |
|
|
74 | tc_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; |
|
|
89 | } |
54 | } |
90 | } |
55 | |
91 | |
56 | void |
92 | void |
57 | tc_get (tc_area *area, int width, int height) |
93 | tc_get (tc_area *area, int width, int height) |
58 | { |
94 | { |
… | |
… | |
75 | |
111 | |
76 | // create a new texture if necessary |
112 | // create a new texture if necessary |
77 | if (!match) |
113 | if (!match) |
78 | { |
114 | { |
79 | match = g_slice_new (tc_texture); |
115 | match = g_slice_new (tc_texture); |
80 | match->next = first_texture; |
116 | match->next = first_texture; |
81 | first_texture = match; |
117 | first_texture = match; |
82 | glGenTextures (1, &match->name); |
118 | match->name = gen_texture (); |
83 | match->avail = TC_HEIGHT; |
119 | match->avail = TC_HEIGHT; |
|
|
120 | match->saved = 0; |
84 | |
121 | |
85 | glBindTexture (GL_TEXTURE_2D, match->name); |
122 | glBindTexture (GL_TEXTURE_2D, match->name); |
86 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
123 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
87 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
124 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
88 | 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); |