PageRenderTime 13ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/cookbook/pdl.pl

http://github.com/PerlGameDev/SDL
Perl | 92 lines | 73 code | 19 blank | 0 comment | 6 complexity | dc2fa19974c636e4f3cbabe8744475d2 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. use strict;
  2. use warnings;
  3. use SDL 2.408;
  4. use SDLx::App; #this is in the github repo.
  5. use SDL::Event;
  6. use SDL::Events;
  7. use SDL::Rect;
  8. use SDL::Video;
  9. use SDL::Surface;
  10. use SDL::PixelFormat;
  11. use SDL::Palette;
  12. use SDL::Color;
  13. use Devel::Peek;
  14. use PDL;
  15. my $app = SDLx::App->new(
  16. title => 'Application Title',
  17. width => 640,
  18. height => 480,
  19. depth => 32
  20. );
  21. my $mapped_color = SDL::Video::map_RGB( $app->format(), 50, 50, 50 ); # blue
  22. load_app();
  23. my ( $piddle, $surface ) = surf_piddle();
  24. my $ref = $surface->get_pixels_ptr();
  25. my $event = SDL::Event->new; # create a new event
  26. while (1) {
  27. SDL::Events::pump_events();
  28. while ( SDL::Events::poll_event($event) ) {
  29. my $type = $event->type(); # get event type
  30. exit if $type == SDL_QUIT;
  31. }
  32. update($piddle);
  33. SDL::Video::update_rect( $app, 0, 0, $app->w, $app->h );
  34. }
  35. sub load_app {
  36. SDL::Video::fill_rect(
  37. $app, SDL::Rect->new( 0, 0, $app->w, $app->h ),
  38. $mapped_color
  39. );
  40. return $app;
  41. }
  42. sub surf_piddle {
  43. my ( $bytes_per_pixel, $width, $height ) = ( 4, 400, 200 );
  44. my $piddle = zeros( byte, $bytes_per_pixel, $width, $height );
  45. my $pointer = $piddle->get_dataref();
  46. my $surface = SDL::Surface->new_from(
  47. $pointer, $width, $height, 32,
  48. $width * $bytes_per_pixel
  49. );
  50. warn "Made surface of $width, $height and " . $surface->format->BytesPerPixel;
  51. return ( $piddle, $surface );
  52. }
  53. sub update {
  54. my $piddle = shift;
  55. load_app();
  56. SDL::Video::lock_surface($surface);
  57. $piddle->mslice(
  58. 'X',
  59. [ rand(400), rand(400), 1 ],
  60. [ rand(200), rand(200), 1 ]
  61. ) .= pdl( rand(225), rand(225), rand(255), 255 );
  62. SDL::Video::unlock_surface($surface);
  63. my $b = SDL::Video::blit_surface(
  64. $surface,
  65. SDL::Rect->new( 0, 0, $surface->w, $surface->h ),
  66. $app,
  67. SDL::Rect->new(
  68. ( $app->w - $surface->w ) / 2, ( $app->h - $surface->h ) / 2,
  69. $app->w, $app->h
  70. )
  71. );
  72. die "Could not blit: " . SDL::get_error() if ( $b == -1 );
  73. }