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.9 by root, Sun Apr 9 01:35:40 2006 UTC vs.
Revision 1.16 by root, Tue Apr 11 13:27:48 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_get_pixel_size (CFClient__Layout self, int *w, int *h)
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 pango_layout_get_pixel_size (self->pl, w, h);
34
35 *w = (*w + 3) & ~3;
36 if (!*w) *w = 1;
37 if (!*h) *h = 1;
38}
39
16MODULE = Crossfire::Client PACKAGE = Crossfire::Client 40MODULE = CFClient PACKAGE = CFClient
41
42PROTOTYPES: ENABLE
17 43
18BOOT: 44BOOT:
19{ 45{
20 fontmap = pango_ft2_font_map_new (); 46 fontmap = pango_ft2_font_map_new ();
21 context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap); 47 context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap);
22} 48}
49
50void
51lowdelay (int fd, int val = 1)
52 CODE:
53 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val));
23 54
24char * 55char *
25gl_version () 56gl_version ()
26 CODE: 57 CODE:
27 RETVAL = (char *)glGetString (GL_VERSION); 58 RETVAL = (char *)glGetString (GL_VERSION);
34 RETVAL = (char *)glGetString (GL_EXTENSIONS); 65 RETVAL = (char *)glGetString (GL_EXTENSIONS);
35 OUTPUT: 66 OUTPUT:
36 RETVAL 67 RETVAL
37 68
38void 69void
70add_font (char *file)
71 CODE:
72 FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */
73
74void
39set_font (char *file) 75set_font (char *file)
40 CODE: 76 CODE:
41{ 77{
42 int count; 78 int count;
43 FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)file, 0, 0, &count); 79 FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)file, 0, 0, &count);
44 FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */
45 PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern); 80 PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0);
46 FcPatternDestroy (pattern); 81 FcPatternDestroy (pattern);
47 pango_context_set_font_description (context, font); 82 pango_context_set_font_description (context, font);
48} 83}
49 84
85MODULE = CFClient PACKAGE = CFClient::Layout
86
87CFClient::Layout
88new (SV *class, int base_height = 10)
89 CODE:
90 New (0, RETVAL, 1, struct cf_layout);
91 RETVAL->base_height = base_height;
92 RETVAL->pl = pango_layout_new (context);
93 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR);
94 OUTPUT:
95 RETVAL
96
50void 97void
51font_render (SV *text_, int height = 10) 98DESTROY (CFClient::Layout self)
52 PPCODE: 99 CODE:
100 g_object_unref (self->pl);
101 Safefree (self);
102
103void
104set_markup (CFClient::Layout self, SV *text_)
105 CODE:
53{ 106{
54 STRLEN textlen; 107 STRLEN textlen;
55 char *text = SvPVutf8 (text_, textlen); 108 char *text = SvPVutf8 (text_, textlen);
109
110 pango_layout_set_markup (self->pl, text, textlen);
111}
112
113void
114set_height (CFClient::Layout self, int base_height)
115 CODE:
116 self->base_height = base_height;
117
118void
119set_width (CFClient::Layout self, int max_width = -1)
120 CODE:
121 pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE);
122
123void
124size (CFClient::Layout self)
125 PPCODE:
126{
127 int w, h;
128
129 layout_get_pixel_size (self, &w, &h);
130
131 EXTEND (SP, 2);
132 PUSHs (sv_2mortal (newSViv (w)));
133 PUSHs (sv_2mortal (newSViv (h)));
134}
135
136void
137render (CFClient::Layout self)
138 PPCODE:
139{
56 SV *retval; 140 SV *retval;
57 int w, h; 141 int w, h;
58 FT_Bitmap bitmap; 142 FT_Bitmap bitmap;
59 PangoLayout *layout;
60 PangoFontDescription *font = pango_context_get_font_description (context);
61 pango_font_description_set_absolute_size (font, height * PANGO_SCALE);
62 143
63 layout = pango_layout_new (context);
64 pango_layout_set_markup (layout, text, textlen);
65 pango_layout_get_pixel_size (layout, &w, &h); 144 layout_get_pixel_size (self, &w, &h);
66
67 w = (w + 3) & ~3;
68 if (!w) w = 1;
69 if (!h) h = 1;
70 145
71 retval = newSV (w * h); 146 retval = newSV (w * h);
72 SvPOK_only (retval); 147 SvPOK_only (retval);
73 SvCUR_set (retval, w * h); 148 SvCUR_set (retval, w * h);
74 149
79 bitmap.num_grays = 256; 154 bitmap.num_grays = 256;
80 bitmap.pixel_mode = FT_PIXEL_MODE_GRAY; 155 bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
81 156
82 memset (bitmap.buffer, 0, w * h); 157 memset (bitmap.buffer, 0, w * h);
83 158
84 pango_ft2_render_layout (&bitmap, layout, 0 * PANGO_SCALE, 0 * PANGO_SCALE); 159 pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE);
85 g_object_unref (layout);
86 160
87 EXTEND (SP, 3); 161 EXTEND (SP, 3);
88 PUSHs (sv_2mortal (newSViv (w))); 162 PUSHs (sv_2mortal (newSViv (w)));
89 PUSHs (sv_2mortal (newSViv (h))); 163 PUSHs (sv_2mortal (newSViv (h)));
90 PUSHs (sv_2mortal (retval)); 164 PUSHs (sv_2mortal (retval));
91} 165}
166
167MODULE = CFClient PACKAGE = CFClient::Texture
168
169void
170draw_quad (SV *self, double x, double y, double w = 0, double h = 0)
171 PROTOTYPE: $$$;$$
172 CODE:
173{
174 HV *hv = (HV *)SvRV (self);
175 double s = SvNV (*hv_fetch (hv, "s", 1, 1));
176 double t = SvNV (*hv_fetch (hv, "t", 1, 1));
177 int name = SvIV (*hv_fetch (hv, "name", 4, 1));
178
179 if (items < 5)
180 {
181 w = SvNV (*hv_fetch (hv, "width", 5, 1));
182 h = SvNV (*hv_fetch (hv, "height", 6, 1));
183 }
184
185 glBindTexture (GL_TEXTURE_2D, name);
186 glBegin (GL_QUADS);
187 glTexCoord2d (0, 0); glVertex2d (x , y );
188 glTexCoord2d (0, t); glVertex2d (x , y + h);
189 glTexCoord2d (s, t); glVertex2d (x + w, y + h);
190 glTexCoord2d (s, 0); glVertex2d (x + w, y );
191 glEnd ();
192}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines