ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/Client.xs
Revision: 1.13
Committed: Tue Apr 11 12:27:51 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.12: +7 -1 lines
Log Message:
add typemap and bold/?italic fonts

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.1 MODULE = Crossfire::Client PACKAGE = Crossfire::Client
23    
24 root 1.11 PROTOTYPES: ENABLE
25    
26 root 1.5 BOOT:
27     {
28     fontmap = pango_ft2_font_map_new ();
29 root 1.8 context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap);
30 root 1.5 }
31    
32 root 1.10 void
33     lowdelay (int fd, int val = 1)
34     CODE:
35     setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val));
36    
37 root 1.3 char *
38 root 1.4 gl_version ()
39     CODE:
40 root 1.5 RETVAL = (char *)glGetString (GL_VERSION);
41 root 1.4 OUTPUT:
42     RETVAL
43    
44     char *
45 root 1.3 gl_extensions ()
46     CODE:
47 root 1.5 RETVAL = (char *)glGetString (GL_EXTENSIONS);
48 root 1.3 OUTPUT:
49     RETVAL
50    
51 root 1.5 void
52 root 1.13 add_font (char *file)
53     CODE:
54     FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */
55    
56     void
57 root 1.9 set_font (char *file)
58 root 1.8 CODE:
59     {
60 root 1.9 int count;
61     FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)file, 0, 0, &count);
62 root 1.10 PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0);
63 root 1.9 FcPatternDestroy (pattern);
64 root 1.8 pango_context_set_font_description (context, font);
65     }
66    
67 root 1.13 MODULE = Crossfire::Client PACKAGE = Crossfire::Client
68    
69 root 1.8 void
70 root 1.7 font_render (SV *text_, int height = 10)
71 root 1.5 PPCODE:
72     {
73     STRLEN textlen;
74     char *text = SvPVutf8 (text_, textlen);
75     SV *retval;
76     int w, h;
77     FT_Bitmap bitmap;
78     PangoLayout *layout;
79     PangoFontDescription *font = pango_context_get_font_description (context);
80 root 1.7 pango_font_description_set_absolute_size (font, height * PANGO_SCALE);
81 root 1.5
82     layout = pango_layout_new (context);
83     pango_layout_set_markup (layout, text, textlen);
84     pango_layout_get_pixel_size (layout, &w, &h);
85    
86     w = (w + 3) & ~3;
87 root 1.6 if (!w) w = 1;
88     if (!h) h = 1;
89 root 1.5
90     retval = newSV (w * h);
91     SvPOK_only (retval);
92     SvCUR_set (retval, w * h);
93    
94     bitmap.rows = h;
95     bitmap.width = w;
96     bitmap.pitch = w;
97     bitmap.buffer = (unsigned char*)SvPVX (retval);
98     bitmap.num_grays = 256;
99     bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
100    
101     memset (bitmap.buffer, 0, w * h);
102    
103     pango_ft2_render_layout (&bitmap, layout, 0 * PANGO_SCALE, 0 * PANGO_SCALE);
104     g_object_unref (layout);
105 root 1.1
106 root 1.5 EXTEND (SP, 3);
107     PUSHs (sv_2mortal (newSViv (w)));
108     PUSHs (sv_2mortal (newSViv (h)));
109     PUSHs (sv_2mortal (retval));
110     }
111 root 1.11
112     MODULE = Crossfire::Client PACKAGE = Crossfire::Client::Texture
113    
114     void
115 root 1.12 draw_quad (SV *self, double x, double y, double w = 0, double h = 0)
116     PROTOTYPE: $$$;$$
117 root 1.11 CODE:
118     {
119 root 1.12 HV *hv = (HV *)SvRV (self);
120     double s = SvNV (*hv_fetch (hv, "s", 1, 1));
121     double t = SvNV (*hv_fetch (hv, "t", 1, 1));
122     int name = SvIV (*hv_fetch (hv, "name", 4, 1));
123    
124     if (items < 5)
125     {
126     w = SvNV (*hv_fetch (hv, "width", 5, 1));
127     h = SvNV (*hv_fetch (hv, "height", 6, 1));
128     }
129    
130     glBindTexture (GL_TEXTURE_2D, name);
131     glBegin (GL_QUADS);
132     glTexCoord2d (0, 0); glVertex2d (x , y );
133     glTexCoord2d (0, t); glVertex2d (x , y + h);
134     glTexCoord2d (s, t); glVertex2d (x + w, y + h);
135     glTexCoord2d (s, 0); glVertex2d (x + w, y );
136     glEnd ();
137 root 1.11 }