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

Comparing deliantra/Deliantra-Client/Client.xs (file contents):
Revision 1.13 by root, Tue Apr 11 12:27:51 2006 UTC vs.
Revision 1.20 by root, Wed Apr 12 02:00:06 2006 UTC

17#include <netinet/tcp.h> 17#include <netinet/tcp.h>
18 18
19static PangoContext *context; 19static PangoContext *context;
20static PangoFontMap *fontmap; 20static PangoFontMap *fontmap;
21 21
22typedef struct cf_layout {
23 PangoLayout *pl;
24 int base_height;
25} *CFClient__Layout;
26
27static void
28substitute_func (FcPattern *pattern, gpointer data)
29{
30 FcPatternAddBool (pattern, FC_HINTING , 1);
31 FcPatternAddBool (pattern, FC_AUTOHINT, 1);
32}
33
34static void
35layout_update (CFClient__Layout self)
36{
37 /* use a random scale factor to account for unknown descenders, 0.8 works
38 * reasonably well with bitstream vera
39 */
40 PangoFontDescription *font = pango_context_get_font_description (context);
41 pango_font_description_set_absolute_size (font, self->base_height * (PANGO_SCALE * 8 / 10));
42}
43
44static void
45layout_get_pixel_size (CFClient__Layout self, int *w, int *h)
46{
47 layout_update (self);
48
49 pango_layout_get_pixel_size (self->pl, w, h);
50
51 *w = (*w + 3) & ~3;
52 if (!*w) *w = 1;
53 if (!*h) *h = 1;
54}
55
22MODULE = Crossfire::Client PACKAGE = Crossfire::Client 56MODULE = CFClient PACKAGE = CFClient
23 57
24PROTOTYPES: ENABLE 58PROTOTYPES: ENABLE
25 59
26BOOT: 60BOOT:
27{ 61{
28 fontmap = pango_ft2_font_map_new (); 62 fontmap = pango_ft2_font_map_new ();
63 pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)fontmap, substitute_func, 0, 0);
29 context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap); 64 context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap);
30} 65}
31 66
32void 67void
33lowdelay (int fd, int val = 1) 68lowdelay (int fd, int val = 1)
62 PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0); 97 PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0);
63 FcPatternDestroy (pattern); 98 FcPatternDestroy (pattern);
64 pango_context_set_font_description (context, font); 99 pango_context_set_font_description (context, font);
65} 100}
66 101
67MODULE = Crossfire::Client PACKAGE = Crossfire::Client 102MODULE = CFClient PACKAGE = CFClient::Layout
68 103
69void 104CFClient::Layout
70font_render (SV *text_, int height = 10) 105new (SV *class, int base_height = 10)
71 PPCODE: 106 CODE:
107 New (0, RETVAL, 1, struct cf_layout);
108 RETVAL->base_height = base_height;
109 RETVAL->pl = pango_layout_new (context);
110 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR);
111 OUTPUT:
112 RETVAL
113
114void
115DESTROY (CFClient::Layout self)
116 CODE:
117 g_object_unref (self->pl);
118 Safefree (self);
119
120void
121set_markup (CFClient::Layout self, SV *text_)
122 CODE:
72{ 123{
73 STRLEN textlen; 124 STRLEN textlen;
74 char *text = SvPVutf8 (text_, textlen); 125 char *text = SvPVutf8 (text_, textlen);
126
127 pango_layout_set_markup (self->pl, text, textlen);
128}
129
130void
131set_height (CFClient::Layout self, int base_height)
132 CODE:
133 self->base_height = base_height;
134
135void
136set_width (CFClient::Layout self, int max_width = -1)
137 CODE:
138 pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE);
139
140void
141size (CFClient::Layout self)
142 PPCODE:
143{
144 int w, h;
145
146 layout_update (self);
147 layout_get_pixel_size (self, &w, &h);
148
149 EXTEND (SP, 2);
150 PUSHs (sv_2mortal (newSViv (w)));
151 PUSHs (sv_2mortal (newSViv (h)));
152}
153
154int
155xy_to_index (CFClient::Layout self, int x, int y)
156 CODE:
157{
158 int index, trailing;
159
160 layout_update (self);
161 pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing);
162
163 RETVAL = index;
164}
165 OUTPUT:
166 RETVAL
167
168void
169cursor_pos (CFClient::Layout self, int index)
170 PPCODE:
171{
172 PangoRectangle strong_pos;
173 layout_update (self);
174 pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0);
175 EXTEND (SP, 3);
176 PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE)));
177 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE)));
178 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE)));
179}
180
181void
182render (CFClient::Layout self)
183 PPCODE:
184{
75 SV *retval; 185 SV *retval;
76 int w, h; 186 int w, h;
77 FT_Bitmap bitmap; 187 FT_Bitmap bitmap;
78 PangoLayout *layout;
79 PangoFontDescription *font = pango_context_get_font_description (context);
80 pango_font_description_set_absolute_size (font, height * PANGO_SCALE);
81 188
82 layout = pango_layout_new (context); 189 layout_update (self);
83 pango_layout_set_markup (layout, text, textlen);
84 pango_layout_get_pixel_size (layout, &w, &h); 190 layout_get_pixel_size (self, &w, &h);
85
86 w = (w + 3) & ~3;
87 if (!w) w = 1;
88 if (!h) h = 1;
89 191
90 retval = newSV (w * h); 192 retval = newSV (w * h);
91 SvPOK_only (retval); 193 SvPOK_only (retval);
92 SvCUR_set (retval, w * h); 194 SvCUR_set (retval, w * h);
93 195
98 bitmap.num_grays = 256; 200 bitmap.num_grays = 256;
99 bitmap.pixel_mode = FT_PIXEL_MODE_GRAY; 201 bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
100 202
101 memset (bitmap.buffer, 0, w * h); 203 memset (bitmap.buffer, 0, w * h);
102 204
103 pango_ft2_render_layout (&bitmap, layout, 0 * PANGO_SCALE, 0 * PANGO_SCALE); 205 pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE);
104 g_object_unref (layout);
105 206
106 EXTEND (SP, 3); 207 EXTEND (SP, 3);
107 PUSHs (sv_2mortal (newSViv (w))); 208 PUSHs (sv_2mortal (newSViv (w)));
108 PUSHs (sv_2mortal (newSViv (h))); 209 PUSHs (sv_2mortal (newSViv (h)));
109 PUSHs (sv_2mortal (retval)); 210 PUSHs (sv_2mortal (retval));
110} 211}
111 212
112MODULE = Crossfire::Client PACKAGE = Crossfire::Client::Texture 213MODULE = CFClient PACKAGE = CFClient::Texture
113 214
114void 215void
115draw_quad (SV *self, double x, double y, double w = 0, double h = 0) 216draw_quad (SV *self, double x, double y, double w = 0, double h = 0)
116 PROTOTYPE: $$$;$$ 217 PROTOTYPE: $$$;$$
117 CODE: 218 CODE:
121 double t = SvNV (*hv_fetch (hv, "t", 1, 1)); 222 double t = SvNV (*hv_fetch (hv, "t", 1, 1));
122 int name = SvIV (*hv_fetch (hv, "name", 4, 1)); 223 int name = SvIV (*hv_fetch (hv, "name", 4, 1));
123 224
124 if (items < 5) 225 if (items < 5)
125 { 226 {
126 w = SvNV (*hv_fetch (hv, "width", 5, 1)); 227 w = SvNV (*hv_fetch (hv, "w", 1, 1));
127 h = SvNV (*hv_fetch (hv, "height", 6, 1)); 228 h = SvNV (*hv_fetch (hv, "h", 1, 1));
128 } 229 }
129 230
130 glBindTexture (GL_TEXTURE_2D, name); 231 glBindTexture (GL_TEXTURE_2D, name);
131 glBegin (GL_QUADS); 232 glBegin (GL_QUADS);
132 glTexCoord2d (0, 0); glVertex2d (x , y ); 233 glTexCoord2d (0, 0); glVertex2d (x , y );

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines