ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/Client.xs
Revision: 1.21
Committed: Wed Apr 12 18:09:21 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.20: +6 -0 lines
Log Message:
less SDL more SDL

File Contents

# User Rev Content
1 root 1.1 #include "EXTERN.h"
2     #include "perl.h"
3     #include "XSUB.h"
4    
5 root 1.5 #include <string.h>
6    
7 root 1.2 #include <SDL.h>
8 root 1.3 #include <SDL_opengl.h>
9 root 1.5
10     #include <pango/pango.h>
11 root 1.10 #include <pango/pangofc-fontmap.h>
12 root 1.5 #include <pango/pangoft2.h>
13    
14 root 1.10 #include <sys/types.h>
15     #include <sys/socket.h>
16     #include <netinet/in.h>
17     #include <netinet/tcp.h>
18    
19 root 1.5 static PangoContext *context;
20     static PangoFontMap *fontmap;
21 root 1.2
22 root 1.14 typedef struct cf_layout {
23     PangoLayout *pl;
24     int base_height;
25 root 1.15 } *CFClient__Layout;
26 root 1.14
27     static void
28 root 1.19 substitute_func (FcPattern *pattern, gpointer data)
29     {
30 root 1.20 FcPatternAddBool (pattern, FC_HINTING , 1);
31     FcPatternAddBool (pattern, FC_AUTOHINT, 1);
32 root 1.19 }
33    
34     static void
35 root 1.17 layout_update (CFClient__Layout self)
36     {
37 root 1.19 /* use a random scale factor to account for unknown descenders, 0.8 works
38     * reasonably well with bitstream vera
39     */
40 root 1.17 PangoFontDescription *font = pango_context_get_font_description (context);
41 root 1.19 pango_font_description_set_absolute_size (font, self->base_height * (PANGO_SCALE * 8 / 10));
42 root 1.17 }
43    
44     static void
45 root 1.15 layout_get_pixel_size (CFClient__Layout self, int *w, int *h)
46 root 1.14 {
47 root 1.19 layout_update (self);
48 root 1.16
49 root 1.14 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    
56 root 1.15 MODULE = CFClient PACKAGE = CFClient
57 root 1.1
58 root 1.11 PROTOTYPES: ENABLE
59    
60 root 1.5 BOOT:
61     {
62     fontmap = pango_ft2_font_map_new ();
63 root 1.19 pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)fontmap, substitute_func, 0, 0);
64 root 1.8 context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap);
65 root 1.5 }
66    
67 root 1.10 void
68     lowdelay (int fd, int val = 1)
69     CODE:
70     setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val));
71    
72 root 1.3 char *
73 root 1.4 gl_version ()
74     CODE:
75 root 1.5 RETVAL = (char *)glGetString (GL_VERSION);
76 root 1.4 OUTPUT:
77     RETVAL
78    
79     char *
80 root 1.3 gl_extensions ()
81     CODE:
82 root 1.5 RETVAL = (char *)glGetString (GL_EXTENSIONS);
83 root 1.3 OUTPUT:
84     RETVAL
85    
86 root 1.5 void
87 root 1.13 add_font (char *file)
88     CODE:
89     FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */
90    
91     void
92 root 1.9 set_font (char *file)
93 root 1.8 CODE:
94     {
95 root 1.9 int count;
96     FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)file, 0, 0, &count);
97 root 1.10 PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0);
98 root 1.9 FcPatternDestroy (pattern);
99 root 1.8 pango_context_set_font_description (context, font);
100     }
101    
102 root 1.21 void
103     wait_events (SV *rfd_, SV *wfd_)
104     PPCODE:
105     {
106     }
107    
108 root 1.15 MODULE = CFClient PACKAGE = CFClient::Layout
109 root 1.14
110 root 1.15 CFClient::Layout
111 root 1.14 new (SV *class, int base_height = 10)
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    
120     void
121 root 1.15 DESTROY (CFClient::Layout self)
122 root 1.14 CODE:
123     g_object_unref (self->pl);
124     Safefree (self);
125 root 1.13
126 root 1.8 void
127 root 1.15 set_markup (CFClient::Layout self, SV *text_)
128 root 1.14 CODE:
129 root 1.5 {
130     STRLEN textlen;
131     char *text = SvPVutf8 (text_, textlen);
132 root 1.14
133     pango_layout_set_markup (self->pl, text, textlen);
134     }
135    
136     void
137 root 1.16 set_height (CFClient::Layout self, int base_height)
138     CODE:
139     self->base_height = base_height;
140    
141     void
142 root 1.15 set_width (CFClient::Layout self, int max_width = -1)
143 root 1.14 CODE:
144     pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE);
145    
146     void
147 root 1.15 size (CFClient::Layout self)
148 root 1.14 PPCODE:
149     {
150     int w, h;
151    
152 root 1.17 layout_update (self);
153 root 1.14 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    
160 root 1.17 int
161     xy_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    
174     void
175     cursor_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    
187 root 1.14 void
188 root 1.15 render (CFClient::Layout self)
189 root 1.14 PPCODE:
190     {
191 root 1.5 SV *retval;
192     int w, h;
193     FT_Bitmap bitmap;
194    
195 root 1.17 layout_update (self);
196 root 1.14 layout_get_pixel_size (self, &w, &h);
197 root 1.5
198     retval = newSV (w * h);
199     SvPOK_only (retval);
200     SvCUR_set (retval, w * h);
201    
202     bitmap.rows = h;
203     bitmap.width = w;
204     bitmap.pitch = w;
205     bitmap.buffer = (unsigned char*)SvPVX (retval);
206     bitmap.num_grays = 256;
207     bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
208    
209     memset (bitmap.buffer, 0, w * h);
210    
211 root 1.14 pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE);
212 root 1.1
213 root 1.5 EXTEND (SP, 3);
214     PUSHs (sv_2mortal (newSViv (w)));
215     PUSHs (sv_2mortal (newSViv (h)));
216     PUSHs (sv_2mortal (retval));
217     }
218 root 1.11
219 root 1.15 MODULE = CFClient PACKAGE = CFClient::Texture
220 root 1.11
221     void
222 root 1.12 draw_quad (SV *self, double x, double y, double w = 0, double h = 0)
223     PROTOTYPE: $$$;$$
224 root 1.11 CODE:
225     {
226 root 1.12 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 root 1.18 w = SvNV (*hv_fetch (hv, "w", 1, 1));
234     h = SvNV (*hv_fetch (hv, "h", 1, 1));
235 root 1.12 }
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 ();
244 root 1.11 }