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 { |
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 | |
|
|
12 | extern int tc_generation; |
|
|
13 | |
11 | void tc_get (tc_area *area, int width, int height); |
14 | void tc_get (tc_area *area, int width, int height); |
12 | void tc_put (tc_area *area); |
15 | void tc_put (tc_area *area); |
13 | void tc_clear (); |
16 | void tc_clear (); |
14 | |
17 | |
15 | ///////////////////////////////////////////////////////////////////////////// |
18 | ///////////////////////////////////////////////////////////////////////////// |
16 | |
19 | |
17 | #include <glib.h> |
20 | #include <glib.h> |
18 | |
21 | |
|
|
22 | int tc_generation; |
|
|
23 | |
19 | typedef struct tc_texture { |
24 | typedef 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 | |
25 | typedef struct tc_slice { |
31 | typedef struct tc_slice { |
26 | GLuint name; |
32 | GLuint name; |
27 | int avail, y; |
33 | int avail, y; |
… | |
… | |
29 | |
35 | |
30 | static tc_slice slices[TC_HEIGHT / TC_ROUND]; |
36 | static tc_slice slices[TC_HEIGHT / TC_ROUND]; |
31 | static tc_texture *first_texture; |
37 | static tc_texture *first_texture; |
32 | |
38 | |
33 | void |
39 | void |
34 | tc_clear () |
40 | tc_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 | |
|
|
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; |
47 | } |
89 | } |
48 | } |
90 | } |
49 | |
91 | |
50 | void |
92 | void |
51 | tc_get (tc_area *area, int width, int height) |
93 | tc_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); |