Skip to content

Commit f88494f

Browse files
committed
reduce auto-xs.inc by 80% using C macros
1 parent c3115fb commit f88494f

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

Modern.xs

+25
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,31 @@
1717
static int _done_glewInit = 0;
1818
static int _auto_check_errors = 0;
1919

20+
#define OGLM_CHECK_ERR(name) \
21+
if ( _auto_check_errors ) { \
22+
int err = GL_NO_ERROR; \
23+
int error_count = 0; \
24+
while ( ( err = glGetError() ) != GL_NO_ERROR ) { \
25+
warn( #name ": OpenGL error: %d %s", err, gl_error_string(err) ); \
26+
error_count++; \
27+
} \
28+
if( error_count ) \
29+
croak( #name ": %d OpenGL errors encountered.", error_count ); \
30+
}
31+
#define OGLM_GLEWINIT \
32+
if ( !_done_glewInit ) { \
33+
GLenum err; \
34+
glewExperimental = GL_TRUE; \
35+
err = glewInit(); \
36+
if (GLEW_OK != err) \
37+
croak("Error: %s", glewGetErrorString(err)); \
38+
_done_glewInit++; \
39+
}
40+
#define OGLM_AVAIL_CHECK(impl, name) \
41+
if ( !impl ) { \
42+
croak(#name " not available on this machine"); \
43+
}
44+
2045
/*
2146
Maybe one day we'll allow Perl callbacks for GLDEBUGPROCARB
2247
*/

utils/generate-XS.pl

+5-32
Original file line numberDiff line numberDiff line change
@@ -305,53 +305,26 @@ sub generate_glew_xs {
305305
$decl .= " $xs_args;\n";
306306
}
307307

308-
my $error_check = $name eq "glGetError" ? "" : <<"XS";
309-
if ( _auto_check_errors ) {
310-
int err = GL_NO_ERROR;
311-
int error_count = 0;
312-
while ( ( err = glGetError() ) != GL_NO_ERROR ) {
313-
/* warn( "OpenGL error: %d", err ); */
314-
warn( "$name: OpenGL error: %d %s", err, gl_error_string(err) );
315-
error_count++;
316-
}
317-
if( error_count )
318-
croak( "$name: %d OpenGL errors encountered.", error_count );
319-
}
320-
XS
321-
chomp $error_check; # trailing newline needs to be done conditionally
308+
my $error_check = $name eq "glGetError" ? "" : "OGLM_CHECK_ERR($name)";
322309

323310
my $res = $decl . <<XS;
324311
CODE:
325-
if ( ! _done_glewInit ) {
326-
GLenum err;
327-
glewExperimental = GL_TRUE;
328-
err = glewInit();
329-
if (GLEW_OK != err)
330-
croak("Error: %s", glewGetErrorString(err));
331-
_done_glewInit++;
332-
}
333-
$error_check
312+
OGLM_GLEWINIT@{[$error_check && "\n $error_check"]}
334313
XS
335314

336315
if ( $item->{glewtype} eq 'fun' and $glewImpl ) {
337-
$res .= <<XS;
338-
if ( ! $glewImpl ) {
339-
croak("$name not available on this machine");
340-
};
341-
XS
316+
$res .= " OGLM_AVAIL_CHECK($glewImpl, $name)\n";
342317
}
343318

344-
$error_check = "\n$error_check" if $error_check; # otherwise glGetError gets a stray newline
345-
346319
if ( $no_return_value ) {
347320
$res .= <<XS;
348-
$name($args);$error_check
321+
$name($args);@{[$error_check && "\n $error_check"]}
349322
XS
350323
}
351324
else {
352325
my $arg_list = $item->{glewtype} eq 'var' ? "" : "($args)";
353326
$res .= <<XS;
354-
RETVAL = $name$arg_list;$error_check
327+
RETVAL = $name$arg_list;@{[$error_check && "\n $error_check"]}
355328
OUTPUT:
356329
RETVAL
357330
XS

0 commit comments

Comments
 (0)