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.8 by root, Sun Apr 9 01:19:15 2006 UTC vs.
Revision 1.17 by root, Tue Apr 11 17:02:35 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines