ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/Client.xs
Revision: 1.22
Committed: Wed Apr 12 20:06:36 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.21: +4 -6 lines
Log Message:
even less gtk

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