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.7 by root, Thu Nov 26 07:19:11 2009 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); 14static void tc_get (tc_area *area, int width, int height);
12void tc_put (tc_area *area); 15static void tc_put (tc_area *area);
13void tc_clear (); 16static void tc_clear (void);
17static void tc_backup (void);
18static void tc_restore (void);
14 19
15///////////////////////////////////////////////////////////////////////////// 20/////////////////////////////////////////////////////////////////////////////
16 21
17#include <glib.h> 22#include <glib.h>
18 23
24int tc_generation;
25
19typedef struct tc_texture { 26typedef struct tc_texture {
20 struct tc_texture *next; 27 struct tc_texture *next;
21 GLuint name; 28 GLuint name;
22 int avail; 29 int avail;
30 char *saved; /* stores saved texture data */
23} tc_texture; 31} tc_texture;
24 32
25typedef struct tc_slice { 33typedef struct tc_slice {
26 GLuint name; 34 GLuint name;
27 int avail, y; 35 int avail, y;
29 37
30static tc_slice slices[TC_HEIGHT / TC_ROUND]; 38static tc_slice slices[TC_HEIGHT / TC_ROUND];
31static tc_texture *first_texture; 39static tc_texture *first_texture;
32 40
33void 41void
34tc_clear () 42tc_clear (void)
35{ 43{
36 int i; 44 int i;
37 45
38 for (i = TC_HEIGHT / TC_ROUND; i--; ) 46 for (i = TC_HEIGHT / TC_ROUND; i--; )
39 slices [i].name = 0; 47 slices [i].name = 0;
40 48
41 while (first_texture) 49 while (first_texture)
42 { 50 {
43 tc_texture *next = first_texture->next; 51 tc_texture *next = first_texture->next;
44 glDeleteTextures (1, &first_texture->name); 52 del_texture (first_texture->name);
45 g_slice_free (tc_texture, first_texture); 53 g_slice_free (tc_texture, first_texture);
46 first_texture = next; 54 first_texture = next;
47 } 55 }
56
57 ++tc_generation;
58}
59
60void
61tex_backup (tc_texture *tex)
62{
63 tex->saved = g_slice_alloc (TC_WIDTH * TC_HEIGHT);
64
65 glBindTexture (GL_TEXTURE_2D, tex->name);
66 glGetTexImage (GL_TEXTURE_2D, 0, GL_ALPHA, GL_UNSIGNED_BYTE, tex->saved);
67}
68
69void
70tex_restore (tc_texture *tex)
71{
72 glBindTexture (GL_TEXTURE_2D, tex->name);
73 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
74 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
75 glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, TC_WIDTH, TC_HEIGHT, 0, GL_ALPHA, GL_UNSIGNED_BYTE, tex->saved);
76
77 g_slice_free1 (TC_WIDTH * TC_HEIGHT, tex->saved);
78 tex->saved = 0;
79}
80
81void
82tc_backup (void)
83{
84 tc_texture *tex;
85
86 for (tex = first_texture; tex; tex = tex->next)
87 tex_backup (tex);
88}
89
90void
91tc_restore (void)
92{
93 tc_texture *tex;
94
95 for (tex = first_texture; tex; tex = tex->next)
96 tex_restore (tex);
48} 97}
49 98
50void 99void
51tc_get (tc_area *area, int width, int height) 100tc_get (tc_area *area, int width, int height)
52{ 101{
69 118
70 // create a new texture if necessary 119 // create a new texture if necessary
71 if (!match) 120 if (!match)
72 { 121 {
73 match = g_slice_new (tc_texture); 122 match = g_slice_new (tc_texture);
74 match->next = first_texture; 123 match->next = first_texture;
75 first_texture = match; 124 first_texture = match;
76 glGenTextures (1, &match->name); 125 match->name = gen_texture ();
77 match->avail = TC_HEIGHT; 126 match->avail = TC_HEIGHT;
127 match->saved = 0;
78 128
79 glBindTexture (GL_TEXTURE_2D, match->name); 129 glBindTexture (GL_TEXTURE_2D, match->name);
80 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 130 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
81 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 131 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); 132 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