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.11 by root, Mon Apr 10 19:34:03 2006 UTC vs.
Revision 1.21 by root, Wed Apr 12 18:09:21 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)
47 RETVAL = (char *)glGetString (GL_EXTENSIONS); 82 RETVAL = (char *)glGetString (GL_EXTENSIONS);
48 OUTPUT: 83 OUTPUT:
49 RETVAL 84 RETVAL
50 85
51void 86void
87add_font (char *file)
88 CODE:
89 FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */
90
91void
52set_font (char *file) 92set_font (char *file)
53 CODE: 93 CODE:
54{ 94{
55 int count; 95 int count;
56 FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)file, 0, 0, &count); 96 FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)file, 0, 0, &count);
57 FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */
58 PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0); 97 PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0);
59 FcPatternDestroy (pattern); 98 FcPatternDestroy (pattern);
60 pango_context_set_font_description (context, font); 99 pango_context_set_font_description (context, font);
61} 100}
62 101
63void 102void
64font_render (SV *text_, int height = 10) 103wait_events (SV *rfd_, SV *wfd_)
104 PPCODE:
105{
106}
107
108MODULE = CFClient PACKAGE = CFClient::Layout
109
110CFClient::Layout
111new (SV *class, int base_height = 10)
65 PPCODE: 112 CODE:
113 New (0, RETVAL, 1, struct cf_layout);
114 RETVAL->base_height = base_height;
115 RETVAL->pl = pango_layout_new (context);
116 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR);
117 OUTPUT:
118 RETVAL
119
120void
121DESTROY (CFClient::Layout self)
122 CODE:
123 g_object_unref (self->pl);
124 Safefree (self);
125
126void
127set_markup (CFClient::Layout self, SV *text_)
128 CODE:
66{ 129{
67 STRLEN textlen; 130 STRLEN textlen;
68 char *text = SvPVutf8 (text_, textlen); 131 char *text = SvPVutf8 (text_, textlen);
132
133 pango_layout_set_markup (self->pl, text, textlen);
134}
135
136void
137set_height (CFClient::Layout self, int base_height)
138 CODE:
139 self->base_height = base_height;
140
141void
142set_width (CFClient::Layout self, int max_width = -1)
143 CODE:
144 pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE);
145
146void
147size (CFClient::Layout self)
148 PPCODE:
149{
150 int w, h;
151
152 layout_update (self);
153 layout_get_pixel_size (self, &w, &h);
154
155 EXTEND (SP, 2);
156 PUSHs (sv_2mortal (newSViv (w)));
157 PUSHs (sv_2mortal (newSViv (h)));
158}
159
160int
161xy_to_index (CFClient::Layout self, int x, int y)
162 CODE:
163{
164 int index, trailing;
165
166 layout_update (self);
167 pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing);
168
169 RETVAL = index;
170}
171 OUTPUT:
172 RETVAL
173
174void
175cursor_pos (CFClient::Layout self, int index)
176 PPCODE:
177{
178 PangoRectangle strong_pos;
179 layout_update (self);
180 pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0);
181 EXTEND (SP, 3);
182 PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE)));
183 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE)));
184 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE)));
185}
186
187void
188render (CFClient::Layout self)
189 PPCODE:
190{
69 SV *retval; 191 SV *retval;
70 int w, h; 192 int w, h;
71 FT_Bitmap bitmap; 193 FT_Bitmap bitmap;
72 PangoLayout *layout;
73 PangoFontDescription *font = pango_context_get_font_description (context);
74 pango_font_description_set_absolute_size (font, height * PANGO_SCALE);
75 194
76 layout = pango_layout_new (context); 195 layout_update (self);
77 pango_layout_set_markup (layout, text, textlen);
78 pango_layout_get_pixel_size (layout, &w, &h); 196 layout_get_pixel_size (self, &w, &h);
79
80 w = (w + 3) & ~3;
81 if (!w) w = 1;
82 if (!h) h = 1;
83 197
84 retval = newSV (w * h); 198 retval = newSV (w * h);
85 SvPOK_only (retval); 199 SvPOK_only (retval);
86 SvCUR_set (retval, w * h); 200 SvCUR_set (retval, w * h);
87 201
92 bitmap.num_grays = 256; 206 bitmap.num_grays = 256;
93 bitmap.pixel_mode = FT_PIXEL_MODE_GRAY; 207 bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
94 208
95 memset (bitmap.buffer, 0, w * h); 209 memset (bitmap.buffer, 0, w * h);
96 210
97 pango_ft2_render_layout (&bitmap, layout, 0 * PANGO_SCALE, 0 * PANGO_SCALE); 211 pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE);
98 g_object_unref (layout);
99 212
100 EXTEND (SP, 3); 213 EXTEND (SP, 3);
101 PUSHs (sv_2mortal (newSViv (w))); 214 PUSHs (sv_2mortal (newSViv (w)));
102 PUSHs (sv_2mortal (newSViv (h))); 215 PUSHs (sv_2mortal (newSViv (h)));
103 PUSHs (sv_2mortal (retval)); 216 PUSHs (sv_2mortal (retval));
104} 217}
105 218
106MODULE = Crossfire::Client PACKAGE = Crossfire::Client::Texture 219MODULE = CFClient PACKAGE = CFClient::Texture
107 220
108void 221void
109texture_quad (SV *self, double x0, double y0, double w, double h) 222draw_quad (SV *self, double x, double y, double w = 0, double h = 0)
223 PROTOTYPE: $$$;$$
110 CODE: 224 CODE:
111{ 225{
112 //glBegin 226 HV *hv = (HV *)SvRV (self);
227 double s = SvNV (*hv_fetch (hv, "s", 1, 1));
228 double t = SvNV (*hv_fetch (hv, "t", 1, 1));
229 int name = SvIV (*hv_fetch (hv, "name", 4, 1));
230
231 if (items < 5)
232 {
233 w = SvNV (*hv_fetch (hv, "w", 1, 1));
234 h = SvNV (*hv_fetch (hv, "h", 1, 1));
235 }
236
237 glBindTexture (GL_TEXTURE_2D, name);
238 glBegin (GL_QUADS);
239 glTexCoord2d (0, 0); glVertex2d (x , y );
240 glTexCoord2d (0, t); glVertex2d (x , y + h);
241 glTexCoord2d (s, t); glVertex2d (x + w, y + h);
242 glTexCoord2d (s, 0); glVertex2d (x + w, y );
243 glEnd ();
113} 244}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines