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.3 by root, Fri Apr 27 21:27:51 2007 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 {
8 int x, y, w, h; 9 int x, y, w, h;
9} tc_area; 10} tc_area;
10 11
11extern int tc_generation; 12extern int tc_generation;
12 13
13void tc_get (tc_area *area, int width, int height); 14static void tc_get (tc_area *area, int width, int height);
14void tc_put (tc_area *area); 15static void tc_put (tc_area *area);
15void tc_clear (); 16static void tc_clear (void);
17static void tc_backup (void);
18static void tc_restore (void);
16 19
17///////////////////////////////////////////////////////////////////////////// 20/////////////////////////////////////////////////////////////////////////////
18 21
19#include <glib.h> 22#include <glib.h>
20 23
22 25
23typedef struct tc_texture { 26typedef struct tc_texture {
24 struct tc_texture *next; 27 struct tc_texture *next;
25 GLuint name; 28 GLuint name;
26 int avail; 29 int avail;
30 char *saved; /* stores saved texture data */
27} tc_texture; 31} tc_texture;
28 32
29typedef struct tc_slice { 33typedef struct tc_slice {
30 GLuint name; 34 GLuint name;
31 int avail, y; 35 int avail, y;
33 37
34static tc_slice slices[TC_HEIGHT / TC_ROUND]; 38static tc_slice slices[TC_HEIGHT / TC_ROUND];
35static tc_texture *first_texture; 39static tc_texture *first_texture;
36 40
37void 41void
38tc_clear () 42tc_clear (void)
39{ 43{
40 int i; 44 int i;
41 45
42 for (i = TC_HEIGHT / TC_ROUND; i--; ) 46 for (i = TC_HEIGHT / TC_ROUND; i--; )
43 slices [i].name = 0; 47 slices [i].name = 0;
49 g_slice_free (tc_texture, first_texture); 53 g_slice_free (tc_texture, first_texture);
50 first_texture = next; 54 first_texture = next;
51 } 55 }
52 56
53 ++tc_generation; 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);
54} 97}
55 98
56void 99void
57tc_get (tc_area *area, int width, int height) 100tc_get (tc_area *area, int width, int height)
58{ 101{
75 118
76 // create a new texture if necessary 119 // create a new texture if necessary
77 if (!match) 120 if (!match)
78 { 121 {
79 match = g_slice_new (tc_texture); 122 match = g_slice_new (tc_texture);
80 match->next = first_texture; 123 match->next = first_texture;
81 first_texture = match; 124 first_texture = match;
82 match->name = gen_texture (); 125 match->name = gen_texture ();
83 match->avail = TC_HEIGHT; 126 match->avail = TC_HEIGHT;
127 match->saved = 0;
84 128
85 glBindTexture (GL_TEXTURE_2D, match->name); 129 glBindTexture (GL_TEXTURE_2D, match->name);
86 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 130 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
87 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 131 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); 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