ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/Client.xs
Revision: 1.18
Committed: Tue Apr 11 19:31:17 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.17: +2 -2 lines
Log Message:
*** empty log message ***

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.17 layout_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    
34     static void
35 root 1.15 layout_get_pixel_size (CFClient__Layout self, int *w, int *h)
36 root 1.14 {
37 root 1.16 PangoFontDescription *font = pango_context_get_font_description (context);
38     pango_font_description_set_absolute_size (font, self->base_height * PANGO_SCALE);
39    
40 root 1.14 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    
47 root 1.15 MODULE = CFClient PACKAGE = CFClient
48 root 1.1
49 root 1.11 PROTOTYPES: ENABLE
50    
51 root 1.5 BOOT:
52     {
53     fontmap = pango_ft2_font_map_new ();
54 root 1.8 context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap);
55 root 1.5 }
56    
57 root 1.10 void
58     lowdelay (int fd, int val = 1)
59     CODE:
60     setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val));
61    
62 root 1.3 char *
63 root 1.4 gl_version ()
64     CODE:
65 root 1.5 RETVAL = (char *)glGetString (GL_VERSION);
66 root 1.4 OUTPUT:
67     RETVAL
68    
69     char *
70 root 1.3 gl_extensions ()
71     CODE:
72 root 1.5 RETVAL = (char *)glGetString (GL_EXTENSIONS);
73 root 1.3 OUTPUT:
74     RETVAL
75    
76 root 1.5 void
77 root 1.13 add_font (char *file)
78     CODE:
79     FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */
80    
81     void
82 root 1.9 set_font (char *file)
83 root 1.8 CODE:
84     {
85 root 1.9 int count;
86     FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)file, 0, 0, &count);
87 root 1.10 PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0);
88 root 1.9 FcPatternDestroy (pattern);
89 root 1.8 pango_context_set_font_description (context, font);
90     }
91    
92 root 1.15 MODULE = CFClient PACKAGE = CFClient::Layout
93 root 1.14
94 root 1.15 CFClient::Layout
95 root 1.14 new (SV *class, int base_height = 10)
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    
104     void
105 root 1.15 DESTROY (CFClient::Layout self)
106 root 1.14 CODE:
107     g_object_unref (self->pl);
108     Safefree (self);
109 root 1.13
110 root 1.8 void
111 root 1.15 set_markup (CFClient::Layout self, SV *text_)
112 root 1.14 CODE:
113 root 1.5 {
114     STRLEN textlen;
115     char *text = SvPVutf8 (text_, textlen);
116 root 1.14
117     pango_layout_set_markup (self->pl, text, textlen);
118     }
119    
120     void
121 root 1.16 set_height (CFClient::Layout self, int base_height)
122     CODE:
123     self->base_height = base_height;
124    
125     void
126 root 1.15 set_width (CFClient::Layout self, int max_width = -1)
127 root 1.14 CODE:
128     pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE);
129    
130     void
131 root 1.15 size (CFClient::Layout self)
132 root 1.14 PPCODE:
133     {
134     int w, h;
135    
136 root 1.17 layout_update (self);
137 root 1.14 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    
144 root 1.17 int
145     xy_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    
158     void
159     cursor_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    
171 root 1.14 void
172 root 1.15 render (CFClient::Layout self)
173 root 1.14 PPCODE:
174     {
175 root 1.5 SV *retval;
176     int w, h;
177     FT_Bitmap bitmap;
178    
179 root 1.17 layout_update (self);
180 root 1.14 layout_get_pixel_size (self, &w, &h);
181 root 1.5
182     retval = newSV (w * h);
183     SvPOK_only (retval);
184     SvCUR_set (retval, w * h);
185    
186     bitmap.rows = h;
187     bitmap.width = w;
188     bitmap.pitch = w;
189     bitmap.buffer = (unsigned char*)SvPVX (retval);
190     bitmap.num_grays = 256;
191     bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
192    
193     memset (bitmap.buffer, 0, w * h);
194    
195 root 1.14 pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE);
196 root 1.1
197 root 1.5 EXTEND (SP, 3);
198     PUSHs (sv_2mortal (newSViv (w)));
199     PUSHs (sv_2mortal (newSViv (h)));
200     PUSHs (sv_2mortal (retval));
201     }
202 root 1.11
203 root 1.15 MODULE = CFClient PACKAGE = CFClient::Texture
204 root 1.11
205     void
206 root 1.12 draw_quad (SV *self, double x, double y, double w = 0, double h = 0)
207     PROTOTYPE: $$$;$$
208 root 1.11 CODE:
209     {
210 root 1.12 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 root 1.18 w = SvNV (*hv_fetch (hv, "w", 1, 1));
218     h = SvNV (*hv_fetch (hv, "h", 1, 1));
219 root 1.12 }
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 root 1.11 }