-
Notifications
You must be signed in to change notification settings - Fork 2
/
OpenGL.xs
228 lines (198 loc) · 4.78 KB
/
OpenGL.xs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <DeviceBitmap.h>
#include <Widget.h>
#include <Image.h>
#include <Icon.h>
#include <Application.h>
#include <Printer.h>
#include <GL/gl.h>
#include "prima_gl.h"
PWidget_vmt CWidget;
PDeviceBitmap_vmt CDeviceBitmap;
PImage_vmt CImage;
PIcon_vmt CIcon;
PApplication_vmt CApplication;
PPrinter_vmt CPrinter;
#define var (( PWidget) widget)
#ifndef GL_BGR
#define GL_BGR 0x80e0
#endif
#ifndef GL_BGRA
#define GL_BGRA 0x80e1
#endif
static void
parse_tristate_char( int * target, SV * item, char * key, char * state_for_1, char * state_for_2)
{
char * value = SvPV_nolen( item );
if ( strcmp( value, state_for_1) == 0 )
*target = 1;
else
if ( strcmp( value, state_for_2) == 0 )
*target = 2;
else
croak("attribute '%s' must either be '%s' or '%s'", key, state_for_1, state_for_2);
}
static void
parse_bit_depth( int * target, SV * item, char * key)
{
*target = SvIV( item );
if ( *target < 1 || *target > 32)
croak("attribute '%s' must be between 1 and 32", key);
}
#define PARSE_BOOL(s) \
if ( strcmp( key, # s) == 0 ) \
r-> s = SvIV(val) ? 1 : 0
#define PARSE_BITS(s) \
if ( strcmp( key, # s) == 0 ) \
parse_bit_depth( &r-> s, val, # s)
#define PARSE_STR(s,v1,v2) \
if ( strcmp( key, # s) == 0 ) \
parse_tristate_char( &r-> s, val, # s, v1, v2)
#define PARSE_INT(s) \
if ( strcmp( key, # s) == 0 ) \
r-> s = SvIV(val)
static void
parse( GLRequest * r, HV * attr)
{
HE *he;
memset( r, 0, sizeof(GLRequest));
hv_iterinit( attr);
while (( he = hv_iternext( attr)) != nil) {
char * key = HeKEY( he);
SV * val = HeVAL( he);
if ( !SvOK( val )) continue; /* undef is default */
PARSE_STR( render, "direct", "xserver");
else PARSE_STR( pixels, "rgba", "palette");
else PARSE_INT( layer);
else PARSE_BOOL( double_buffer);
else PARSE_BOOL( stereo);
else PARSE_BITS( color_bits);
else PARSE_BITS( aux_buffers);
else PARSE_BITS( red_bits);
else PARSE_BITS( green_bits);
else PARSE_BITS( blue_bits);
else PARSE_BITS( alpha_bits);
else PARSE_BITS( depth_bits);
else PARSE_BITS( stencil_bits);
else PARSE_BITS( accum_red_bits);
else PARSE_BITS( accum_green_bits);
else PARSE_BITS( accum_blue_bits);
else PARSE_BITS( accum_alpha_bits);
else croak("unknown attribute: '%s'", key);
}
}
MODULE = Prima::OpenGL PACKAGE = Prima::OpenGL
BOOT:
{
PRIMA_VERSION_BOOTCHECK;
CWidget = (PWidget_vmt)gimme_the_vmt( "Prima::Widget");
CDeviceBitmap = (PDeviceBitmap_vmt)gimme_the_vmt( "Prima::DeviceBitmap");
CImage = (PImage_vmt)gimme_the_vmt( "Prima::Image");
CIcon = (PIcon_vmt)gimme_the_vmt( "Prima::Icon");
CApplication = (PApplication_vmt)gimme_the_vmt( "Prima::Application");
CPrinter = (PPrinter_vmt)gimme_the_vmt( "Prima::Printer");
}
PROTOTYPES: ENABLE
SV*
context_create(sv,attributes)
SV *sv
HV *attributes
PREINIT:
Handle object;
Handle context;
GLRequest request;
Bool need_paint_state = 0;
CODE:
RETVAL = 0;
if ( !(object = gimme_the_mate(sv)))
croak("not a object");
parse( &request, attributes);
if ( kind_of( object, CApplication)) {
request. target = GLREQ_TARGET_APPLICATION;
need_paint_state = 1;
}
else if ( kind_of( object, CWidget))
request. target = GLREQ_TARGET_WINDOW;
else if ( kind_of( object, CDeviceBitmap))
request. target = GLREQ_TARGET_BITMAP;
else if ( kind_of( object, CImage)) {
request. target = GLREQ_TARGET_IMAGE;
need_paint_state = 1;
}
else if ( kind_of( object, CPrinter)) {
request. target = GLREQ_TARGET_PRINTER;
need_paint_state = 1;
}
else
croak("bad object");
if ( need_paint_state && !( PObject(object)-> options. optInDraw || PObject(object)-> options. optInDrawInfo))
croak("object not in paint state");
context = gl_context_create(object, &request);
RETVAL = newSViv(context);
OUTPUT:
RETVAL
void
context_destroy(context)
void *context
CODE:
if ( context) gl_context_destroy((Handle) context);
int
context_make_current(context)
void *context
CODE:
RETVAL = gl_context_make_current((Handle) context);
OUTPUT:
RETVAL
int
context_push()
CODE:
RETVAL = gl_context_push();
OUTPUT:
RETVAL
int
context_pop()
CODE:
RETVAL = gl_context_pop();
OUTPUT:
RETVAL
int
flush(context)
void *context
CODE:
RETVAL = context ? gl_flush((Handle) context) : 0;
OUTPUT:
RETVAL
SV *
last_error()
PREINIT:
char buf[1024], *ret;
CODE:
ret = gl_error_string(buf, 1024);
RETVAL = ret ? newSVpv(ret, 0) : &PL_sv_undef;
OUTPUT:
RETVAL
IV
gl_image_ptr(sv_obj,mask)
SV * sv_obj
int mask
PREINIT:
Handle object;
PIcon i;
CODE:
if ( !(object = gimme_the_mate(sv_obj)) || !kind_of(object, CImage))
croak("not an image");
i = PIcon(object);
RETVAL = PTR2IV(
((kind_of(object, CIcon) && mask) ?
i->mask : i->data));
OUTPUT:
RETVAL
int
is_direct(context)
void *context
CODE:
RETVAL = context ? gl_is_direct((Handle) context) : 0;
OUTPUT:
RETVAL