/t/core_surface.t

http://github.com/PerlGameDev/SDL · Perl · 136 lines · 98 code · 27 blank · 11 comment · 4 complexity · 5686ab298e7b2889e79df91d263a68b6 MD5 · raw file

  1. #!perl -w
  2. # Copyright (C) 2009 kthakore
  3. #
  4. # Spec tests for SDL::Surface
  5. #
  6. BEGIN {
  7. unshift @INC, 'blib/lib', 'blib/arch';
  8. }
  9. use strict;
  10. use warnings;
  11. use SDL;
  12. use SDL::Config;
  13. use SDL::Surface;
  14. use SDLx::App;
  15. use SDL::Rect;
  16. use SDL::Color;
  17. use SDL::Video;
  18. use SDL::PixelFormat;
  19. use Test::More;
  20. use lib 't/lib';
  21. use SDL::TestTool;
  22. my $videodriver = $ENV{SDL_VIDEODRIVER};
  23. $ENV{SDL_VIDEODRIVER} = 'dummy' unless $ENV{SDL_RELEASE_TESTING};
  24. if ( !SDL::TestTool->init(SDL_INIT_VIDEO) ) {
  25. plan( skip_all => 'Failed to init video' );
  26. } else {
  27. plan( tests => 41 );
  28. }
  29. my $surface = SDL::Surface->new( SDL_ANYFORMAT, 640, 320, 8, 0, 0, 0, 0 );
  30. isa_ok( $surface, 'SDL::Surface' );
  31. is( $surface->w, 640, 'surface has width' );
  32. is( $surface->h, 320, 'surface has height' );
  33. is( $surface->pitch, 640, 'surface has pitch' );
  34. my $clip_rect = SDL::Rect->new( 0, 0, 0, 0 );
  35. SDL::Video::get_clip_rect( $surface, $clip_rect );
  36. isa_ok( $clip_rect, 'SDL::Rect' );
  37. is( $clip_rect->x, 0, 'clip_rect has x' );
  38. is( $clip_rect->y, 0, 'clip_rect has y' );
  39. is( $clip_rect->w, 640, 'clip_rect has width' );
  40. is( $clip_rect->h, 320, 'clip_rect has height' );
  41. my $image = SDL::Video::load_BMP('test/data/icon.bmp');
  42. is( $image->w, 32, 'image has width' );
  43. is( $image->h, 32, 'image has height' );
  44. my $pixel_format = $image->format;
  45. isa_ok( $pixel_format, 'SDL::PixelFormat' );
  46. is( $pixel_format->BitsPerPixel, 8, ' BitsPerPixel' );
  47. is( $pixel_format->BytesPerPixel, 1, ' BytesPerPixel' );
  48. is( $pixel_format->Rloss, 8, ' Rloss' );
  49. is( $pixel_format->Gloss, 8, ' Gloss' );
  50. is( $pixel_format->Bloss, 8, ' Bloss' );
  51. is( $pixel_format->Aloss, 8, ' Aloss' );
  52. is( $pixel_format->Rshift, 0, ' Rshift' );
  53. is( $pixel_format->Gshift, 0, ' Gshift' );
  54. is( $pixel_format->Bshift, 0, ' Bshift' );
  55. is( $pixel_format->Ashift, 0, ' Ashift' );
  56. is( $pixel_format->Rmask, 0, ' Rmask' );
  57. is( $pixel_format->Gmask, 0, ' Gmask' );
  58. is( $pixel_format->Bmask, 0, ' Bmask' );
  59. is( $pixel_format->Amask, 0, ' Amask' );
  60. is( $pixel_format->colorkey, 0, ' colorkey' );
  61. is( $pixel_format->alpha, 255, ' alpha' );
  62. my $pixel = SDL::Video::map_RGB( $pixel_format, 255, 127, 0 );
  63. is( $pixel, 2, 'maping RGB to surface' );
  64. SDL::Video::fill_rect( $surface, SDL::Rect->new( 0, 0, 32, 32 ), $pixel );
  65. ok( 1, 'Managed to fill_rect' );
  66. my $small_rect = SDL::Rect->new( 0, 0, 64, 64 );
  67. SDL::Video::blit_surface( $image, $small_rect, $surface, $small_rect );
  68. ok( 1, 'Managed to blit' );
  69. #my $image_format = $surface->display;
  70. #$surface->update_rect( 0, 0, 32, 32 );
  71. #ok( 1, 'Managed to update_rect' );
  72. #$surface->update_rects( SDL::Rect->new( 0, 0, 32, 32 ) );
  73. #ok( 1, 'Managed to update_rects' );
  74. my $app = SDLx::App->new(
  75. title => "Test",
  76. width => 640,
  77. height => 480,
  78. init => SDL_INIT_VIDEO
  79. );
  80. pass 'did this pass';
  81. my $image_format = SDL::Video::display_format($image);
  82. isa_ok( $image_format, 'SDL::Surface' );
  83. my $image_format_alpha = SDL::Video::display_format_alpha($image);
  84. isa_ok( $image_format_alpha, 'SDL::Surface' );
  85. my $app_pixel_format = $app->format;
  86. my $rect = SDL::Rect->new( 0, 0, $app->w, $app->h );
  87. my $blue_pixel = SDL::Video::map_RGB( $app_pixel_format, 0x00, 0x00, 0xff );
  88. SDL::Video::fill_rect( $app, $rect, $blue_pixel );
  89. SDL::Video::update_rect( $app, 0, 0, 0, 0 );
  90. SDL::Video::update_rects( $app, $small_rect );
  91. my $ref = $surface->get_pixels_ptr;
  92. my $other_surface = SDL::Surface->new_from( $ref, 640, 320, 8, $surface->pitch, 0, 0, 0, 0 );
  93. my $get_pixel = $surface->get_pixel(0);
  94. ok( $get_pixel >= 0, "[get_pixel] returns integer ($get_pixel)" );
  95. $surface->set_pixels( 0, 42 );
  96. pass '[set_pixel] first pixel to 42';
  97. is( $surface->get_pixel(0), 42, '[get_pixel] returns integer (42)' );
  98. isa_ok( $other_surface, 'SDL::Surface' );
  99. is( $other_surface->w, $surface->w, '[new_form] have same w' );
  100. is( $other_surface->h, $surface->h, '[neh_form] have same h' );
  101. #TODO: Added more comparison stuff
  102. if ($videodriver) {
  103. $ENV{SDL_VIDEODRIVER} = $videodriver;
  104. } else {
  105. delete $ENV{SDL_VIDEODRIVER};
  106. }
  107. pass 'Final SegFault test';
  108. SDL::delay(100);
  109. sleep(2);