PageRenderTime 53ms CodeModel.GetById 12ms app.highlight 38ms RepoModel.GetById 1ms app.codeStats 0ms

/t/gfx_primitives2.t

http://github.com/PerlGameDev/SDL
Perl | 306 lines | 274 code | 23 blank | 9 comment | 9 complexity | 4405df84fb9da2275c6663ee45dedb8c MD5 | raw file
  1#!perl
  2use strict;
  3use warnings;
  4use SDL;
  5use SDL::Rect;
  6use SDL::Config;
  7use SDL::Video;
  8use SDL::Version;
  9use SDL::Surface;
 10use SDL::PixelFormat;
 11use SDL::GFX;
 12use SDL::GFX::Primitives;
 13use Test::More;
 14
 15use lib 't/lib';
 16use SDL::TestTool;
 17
 18my $videodriver = $ENV{SDL_VIDEODRIVER};
 19$ENV{SDL_VIDEODRIVER} = 'dummy' unless $ENV{SDL_RELEASE_TESTING};
 20
 21if ( !SDL::TestTool->init(SDL_INIT_VIDEO) ) {
 22	plan( skip_all => 'Failed to init video' );
 23} elsif ( !SDL::Config->has('SDL_gfx_primitives') ) {
 24	plan( skip_all => 'SDL_gfx_primitives support not compiled' );
 25}
 26
 27my $v = SDL::GFX::linked_version();
 28isa_ok( $v, 'SDL::Version', '[linked_version]' );
 29printf( "got version: %d.%d.%d\n", $v->major, $v->minor, $v->patch );
 30
 31my $display = SDL::Video::set_video_mode( 640, 480, 32, SDL_SWSURFACE );
 32my $pixel = SDL::Video::map_RGB( $display->format, 0, 0, 0 );
 33SDL::Video::fill_rect(
 34	$display,
 35	SDL::Rect->new( 0, 0, $display->w, $display->h ), $pixel
 36);
 37
 38if ( !$display ) {
 39	plan skip_all => 'Couldn\'t set video mode: ' . SDL::get_error();
 40}
 41
 42# ellipse/aaellipse/filled_ellipse tests
 43is( SDL::GFX::Primitives::ellipse_color( $display, 3, 245, 1, 2, 0xFF0000FF ),
 44	0, 'ellipse_color'
 45); # red
 46is( SDL::GFX::Primitives::ellipse_RGBA( $display, 7, 245, 1, 2, 0x00, 0xFF, 0x00, 0xFF ),
 47	0,
 48	'ellipse_RGBA'
 49); # green
 50is( SDL::GFX::Primitives::aaellipse_color( $display, 11, 245, 1, 2, 0x0000FFFF ),
 51	0,
 52	'aaellipse_color'
 53); # blue
 54is( SDL::GFX::Primitives::aaellipse_RGBA( $display, 15, 245, 1, 2, 0xFF, 0xFF, 0x00, 0xFF ),
 55	0,
 56	'aaellipse_RGBA'
 57); # yellow
 58is( SDL::GFX::Primitives::filled_ellipse_color( $display, 19, 245, 1, 2, 0x00FFFFFF ),
 59	0,
 60	'filled_ellipse_color'
 61); # cyan
 62is( SDL::GFX::Primitives::filled_ellipse_RGBA( $display, 23, 245, 1, 2, 0xFF, 0x00, 0xFF, 0xFF ),
 63	0,
 64	'filled_ellipse_RGBA'
 65); # magenta
 66
 67# ellipse/aaellipse/filled_ellipse demo
 68SDL::GFX::Primitives::aaellipse_color(
 69	$display, 65,     249 + 2 * $_,
 70	60,       2 * $_, 0xFFFFFF80
 71) for ( 1 .. 25 );
 72SDL::GFX::Primitives::filled_ellipse_RGBA(
 73	$display, 65, 405, 60 - 1.2 * $_,
 74	50 - $_, 0xFF, 0x00, 0x00, 0x05
 75) for ( 0 .. 30 );
 76SDL::GFX::Primitives::filled_ellipse_RGBA(
 77	$display, 65,   405, 12, 10, 0x00,
 78	0x00,     0x00, 0xFF
 79);
 80SDL::GFX::Primitives::aaellipse_RGBA(
 81	$display, 65, 405, 12, 10, 0x00, 0x00,
 82	0x00,     0xFF
 83);
 84
 85# trigon/aatrigon/filled_trigon tests
 86is( SDL::GFX::Primitives::trigon_color( $display, 130, 243, 132, 245, 130, 247, 0xFF0000FF ),
 87	0,
 88	'trigon_color'
 89); # red
 90is( SDL::GFX::Primitives::trigon_RGBA( $display, 134, 243, 136, 245, 134, 247, 0x00, 0xFF, 0x00, 0xFF ),
 91	0,
 92	'trigon_RGBA'
 93); # green
 94is( SDL::GFX::Primitives::aatrigon_color( $display, 138, 243, 140, 245, 138, 247, 0x0000FFFF ),
 95	0,
 96	'aatrigon_color'
 97); # blue
 98is( SDL::GFX::Primitives::aatrigon_RGBA( $display, 142, 243, 144, 245, 142, 247, 0xFF, 0xFF, 0x00, 0xFF ),
 99	0,
100	'aatrigon_RGBA'
101); # yellow
102is( SDL::GFX::Primitives::filled_trigon_color( $display, 146, 243, 148, 245, 146, 247, 0x00FFFFFF ),
103	0,
104	'filled_trigon_color'
105); # cyan
106is( SDL::GFX::Primitives::filled_trigon_RGBA( $display, 150, 243, 152, 245, 150, 247, 0xFF, 0x00, 0xFF, 0xFF ),
107	0,
108	'filled_trigon_RGBA'
109); # magenta
110
111# polygon/aapolygon/filled_polygon/textured_polygon/MT/ tests
112
113my $surf = SDL::Video::load_BMP('test/data/pattern_red_white_2x2.bmp');
114
115is( SDL::GFX::Primitives::polygon_color(
116		$display,
117		[ 262, 266, 264, 266, 262 ],
118		[ 243, 243, 245, 247, 247 ],
119		5, 0xFF0000FF
120	),
121	0,
122	'polygon_color'
123); # red
124is( SDL::GFX::Primitives::polygon_RGBA(
125		$display,
126		[ 268, 272, 270, 272, 268 ],
127		[ 243, 243, 245, 247, 247 ],
128		5, 0x00, 0xFF, 0x00, 0xFF
129	),
130	0,
131	'polygon_RGBA'
132); # green
133is( SDL::GFX::Primitives::aapolygon_color(
134		$display,
135		[ 274, 278, 276, 278, 274 ],
136		[ 243, 243, 245, 247, 247 ],
137		5, 0x0000FFFF
138	),
139	0,
140	'aapolygon_color'
141); # blue
142is( SDL::GFX::Primitives::aapolygon_RGBA(
143		$display,
144		[ 280, 284, 282, 284, 280 ],
145		[ 243, 243, 245, 247, 247 ],
146		5, 0xFF, 0xFF, 0x00, 0xFF
147	),
148	0,
149	'aapolygon_RGBA'
150); # yellow
151is( SDL::GFX::Primitives::filled_polygon_color(
152		$display,
153		[ 286, 290, 288, 290, 286 ],
154		[ 243, 243, 245, 247, 247 ],
155		5, 0x00FFFFFF
156	),
157	0,
158	'filled_polygon_color'
159); # cyan
160is( SDL::GFX::Primitives::filled_polygon_RGBA(
161		$display,
162		[ 292, 296, 294, 296, 292 ],
163		[ 243, 243, 245, 247, 247 ],
164		5, 0xFF, 0x00, 0xFF, 0xFF
165	),
166	0,
167	'filled_polygon_RGBA'
168); # magenta
169SKIP:
170{
171	skip( 'Version 2.0.14 needed', 1 ) if $v < 2.0.14;
172	isnt(
173		SDL::GFX::Primitives::textured_polygon(
174			$display,
175			[ 298, 302, 300, 302, 298 ],
176			[ 243, 243, 245, 247, 247 ],
177			5, $surf, 0, 0
178		),
179		-1,
180		'textured_polygon'
181	); # texture
182}
183SKIP:
184{
185	skip( 'Version 2.0.17 needed', 3 ) if $v < 2.0.17;
186	is( SDL::GFX::Primitives::filled_polygon_color_MT(
187			$display,
188			[ 304, 308, 306, 308, 304 ],
189			[ 243, 243, 245, 247, 247 ],
190			5, 0xFF0000FF, 0, 0
191		),
192		0,
193		'filled_polygon_color_MT'
194	); # red
195	is( SDL::GFX::Primitives::filled_polygon_RGBA_MT(
196			$display,
197			[ 310, 314, 312, 314, 310 ],
198			[ 243, 243, 245, 247, 247 ],
199			5, 0x00, 0xFF, 0x00, 0xFF, 0, 0
200		),
201		0,
202		'filled_polygon_RGBA_MT'
203	); # green
204	isnt(
205		SDL::GFX::Primitives::textured_polygon_MT(
206			$display,
207			[ 316, 320, 318, 320, 316 ],
208			[ 243, 243, 245, 247, 247 ],
209			5, $surf, 0, 0, 0, 0
210		),
211		-1,
212		'textured_polygon_MT '
213	); # texture
214}
215
216# polygon demo
217SDL::GFX::Primitives::filled_polygon_color(
218	$display,
219	[ 311, 331, 381, 301, 311, 351 ],
220	[ 293, 293, 378, 378, 361, 361 ],
221	6, 0xFF000080
222);     # red
223SDL::GFX::Primitives::filled_polygon_color(
224	$display,
225	[ 381, 371, 271, 311, 321, 301 ],
226	[ 378, 395, 395, 327, 344, 378 ],
227	6, 0x00FF0080
228);     # green
229SDL::GFX::Primitives::filled_polygon_color(
230	$display,
231	[ 271, 261, 311, 351, 331, 311 ],
232	[ 395, 378, 293, 361, 361, 327 ],
233	6, 0x0000FF80
234);     # blue
235
236# bezier test
237is( SDL::GFX::Primitives::bezier_color(
238		$display,
239		[ 390, 392, 394, 396 ],
240		[ 243, 255, 235, 247 ],
241		4, 20, 0xFF00FFFF
242	),
243	0,
244	'polygon_color'
245);     # red
246is( SDL::GFX::Primitives::bezier_RGBA(
247		$display,
248		[ 398, 400, 402, 404 ],
249		[ 243, 255, 235, 247 ],
250		4, 20, 0x00, 0xFF, 0x00, 0xFF
251	),
252	0,
253	'polygon_RGBA'
254);     # green
255
256#character/string tests
257is( SDL::GFX::Primitives::character_color( $display, 518, 243, 'A', 0xFF0000FF ),
258	0,
259	'character_color'
260);     # red
261is( SDL::GFX::Primitives::character_RGBA( $display, 526, 243, 'B', 0x00, 0xFF, 0x00, 0xFF ),
262	0,
263	'character_RGBA'
264);     # green
265is( SDL::GFX::Primitives::string_color( $display, 534, 243, 'CD', 0x0000FFFF ),
266	0, 'string_color'
267);     # blue
268is( SDL::GFX::Primitives::string_RGBA( $display, 550, 243, 'DE', 0xFF, 0xFF, 0x00, 0xFF ),
269	0,
270	'string_RGBA'
271);     # yellow
272
273SKIP:
274{
275	skip ' test font not found', 1 unless -e 'test/data/5x7.fnt';
276	my $font = '';
277	open( FH, '<', 'test/data/5x7.fnt' );
278	binmode(FH);
279	read( FH, $font, 2048 );
280	close(FH);
281
282	is( SDL::GFX::Primitives::set_font( $font, 5, 7 ), undef, 'set_font' );
283}
284
285#chracater demo
286SDL::GFX::Primitives::character_RGBA(
287	$display,
288	518 + ( $_ % 17 ) * 7,
289	251 + int( $_ / 17 ) * 8,
290	chr($_), 0x80 + $_ / 2,
291	0xFF, 0x00, 0xFF
292) for ( 0 .. 255 );
293
294SDL::Video::update_rect( $display, 0, 0, 640, 480 );
295
296SDL::delay(3000);
297
298if ($videodriver) {
299	$ENV{SDL_VIDEODRIVER} = $videodriver;
300} else {
301	delete $ENV{SDL_VIDEODRIVER};
302}
303
304pass 'Are we still alive? Checking for segfaults';
305
306done_testing;