PageRenderTime 49ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/t_backcompat/testgfxprim.pl

http://github.com/PerlGameDev/SDL
Perl | 273 lines | 184 code | 59 blank | 30 comment | 15 complexity | 63928644505a6c9ab35882732ac03603 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. #!/usr/bin/env perl
  2. #
  3. # testgfxprim.pl
  4. #
  5. # This tests low level usage of the SDL_gfx extension.
  6. # Therefore, you should *not* rely on *any* part of this API.
  7. # It is subject to change, and will eventually
  8. # be encapsulated by something such as SDL::GraphicTool
  9. #
  10. # (plus, it's a bitch to use like this anyway)
  11. #
  12. # Usage: testgfxprm.pl [-bpp N] [-hw] [-fast] [-fullscreen]
  13. use strict;
  14. use Getopt::Long;
  15. use Data::Dumper;
  16. use SDL;
  17. use SDLx::App;
  18. use SDL::Event;
  19. use SDL::Surface;
  20. use SDL::Color;
  21. use SDL::Rect;
  22. use SDL::Config;
  23. use vars qw/ $app $app_rect $background $event $sprite $sprite_rect $videoflags /;
  24. die "Your system is not configured with SDL_gfx support!\n"
  25. unless ( SDL::Config->has('SDL_gfx') );
  26. ## User tweakable settings (via cmd-line)
  27. my %settings = (
  28. 'numsprites' => 10,
  29. 'screen_width' => 640,
  30. 'screen_height' => 480,
  31. 'video_bpp' => 8,
  32. 'fast' => 0,
  33. 'hw' => 0,
  34. 'fullscreen' => 0,
  35. 'bpp' => undef,
  36. );
  37. ## Process commandline arguments
  38. sub get_cmd_args {
  39. GetOptions(
  40. "width:i" => \$settings{screen_width},
  41. "height:i" => \$settings{screen_height},
  42. "bpp:i" => \$settings{bpp},
  43. "fast!" => \$settings{fast},
  44. "hw!" => \$settings{hw},
  45. "fullscreen!" => \$settings{fullscreen},
  46. "numsprites=i" => \$settings{numsprites},
  47. );
  48. }
  49. ## Initialize application options
  50. sub set_app_args {
  51. $settings{bpp} ||= 8; # default to 8 bits per pix
  52. $videoflags |= SDL_HWACCEL if $settings{hw};
  53. $videoflags |= SDL_FULLSCREEN if $settings{fullscreen};
  54. }
  55. ## Setup
  56. sub init_game_context {
  57. $app = SDLx::App->new(
  58. -width => $settings{screen_width},
  59. -height => $settings{screen_height},
  60. -title => "testsprite",
  61. -icon => "data/icon.bmp",
  62. -flags => $videoflags
  63. );
  64. $app_rect = SDL::Rect->new(
  65. -height => $settings{screen_height},
  66. -width => $settings{screen_width}
  67. );
  68. $background = $SDL::Color::black;
  69. $sprite = SDL::Surface->new( -name => "data/icon.bmp" );
  70. # Set transparent pixel as the pixel at (0,0)
  71. $sprite->set_color_key( SDL_SRCCOLORKEY, $sprite->pixel( 0, 0 ) );
  72. print STDERR "Got past that\n";
  73. $sprite->display_format();
  74. $sprite_rect = SDL::Rect->new(
  75. -x => 0,
  76. -y => 0,
  77. -width => $sprite->width,
  78. -height => $sprite->height
  79. );
  80. $event = SDL::Event->new();
  81. }
  82. ## Prints diagnostics
  83. sub instruments {
  84. if ( ( $app->flags & SDL_HWSURFACE ) == SDL_HWSURFACE ) {
  85. printf("Screen is in video memory\n");
  86. } else {
  87. printf("Screen is in system memory\n");
  88. }
  89. if ( ( $app->flags & SDL_DOUBLEBUF ) == SDL_DOUBLEBUF ) {
  90. printf("Screen has double-buffering enabled\n");
  91. }
  92. if ( ( $sprite->flags & SDL_HWSURFACE ) == SDL_HWSURFACE ) {
  93. printf("Sprite is in video memory\n");
  94. } else {
  95. printf("Sprite is in system memory\n");
  96. }
  97. # Run a sample blit to trigger blit (if posssible)
  98. # acceleration before the check just after
  99. $sprite->blit( 0, $app, 0 );
  100. if ( ( $sprite->flags & SDL_HWACCEL ) == SDL_HWACCEL ) {
  101. printf("Sprite blit uses hardware acceleration\n");
  102. }
  103. if ( ( $sprite->flags & SDL_RLEACCEL ) == SDL_RLEACCEL ) {
  104. printf("Sprite blit uses RLE acceleration\n");
  105. }
  106. }
  107. sub game_loop {
  108. my $surf = $$app;
  109. my $surfWidth = $settings{screen_width};
  110. my $surfHeight = $settings{screen_height};
  111. my $surfMidWidth = $settings{screen_width} >> 1;
  112. my $surfMidHeight = $settings{screen_height} >> 1;
  113. $app->fill( $app_rect, $background );
  114. # TODO: polygon's, GFX*Color
  115. #lines
  116. SDL::GFXHlineRGBA(
  117. $surf, 0, $surfWidth, $surfMidHeight, 255, 255, 255,
  118. 255
  119. );
  120. SDL::GFXVlineRGBA(
  121. $surf, $surfMidWidth, 0, $surfHeight, 255, 255, 255,
  122. 255
  123. );
  124. # rectangles
  125. SDL::GFXRectangleRGBA(
  126. $surf, 0, 0,
  127. $surfMidWidth / 2,
  128. $surfMidHeight / 2,
  129. 255, 0, 0, 255
  130. );
  131. SDL::GFXBoxRGBA(
  132. $surf, 0, 0,
  133. $surfMidWidth / 3,
  134. $surfMidHeight / 3,
  135. 0, 255, 0, 255
  136. );
  137. SDL::GFXLineRGBA( $surf, 0, 0, $surfWidth, $surfHeight, 0, 255, 255, 255 );
  138. SDL::GFXAalineRGBA(
  139. $surf, $surfWidth, 0, 0, $surfHeight, 0, 255, 255,
  140. 255
  141. );
  142. # circles
  143. SDL::GFXCircleRGBA(
  144. $surf, $surfMidWidth * .3,
  145. $surfMidHeight, $surfMidWidth * .3,
  146. 255, 255, 0, 255
  147. );
  148. SDL::GFXAacircleRGBA(
  149. $surf, $surfMidWidth * .6,
  150. $surfMidHeight, $surfMidWidth * .3,
  151. 255, 255, 0, 255
  152. );
  153. SDL::GFXFilledCircleRGBA(
  154. $surf, $surfMidWidth * .3,
  155. $surfMidHeight, $surfMidWidth * .25,
  156. 255, 255, 0, 255
  157. );
  158. # ellipses
  159. SDL::GFXEllipseRGBA(
  160. $surf, $surfWidth - $surfMidWidth * .3,
  161. $surfMidHeight,
  162. $surfMidWidth * .3,
  163. $surfMidHeight * .15,
  164. 255, 255, 0, 255
  165. );
  166. SDL::GFXAaellipseRGBA(
  167. $surf, $surfWidth - $surfMidWidth * .6,
  168. $surfMidHeight,
  169. $surfMidWidth * .3,
  170. $surfMidHeight * .15,
  171. 255, 255, 0, 255
  172. );
  173. SDL::GFXFilledEllipseRGBA(
  174. $surf, $surfWidth - $surfMidWidth * .3,
  175. $surfMidHeight,
  176. $surfMidWidth * .25,
  177. $surfMidHeight * .10,
  178. 255, 255, 0, 255
  179. );
  180. # pie slices
  181. SDL::GFXFilledPieRGBA(
  182. $surf, $surfMidWidth, $surfMidHeight,
  183. $surfMidWidth * .1,
  184. 0, 90, 0, 0, 255, 255
  185. );
  186. SDL::GFXFilledPieRGBA(
  187. $surf, $surfMidWidth, $surfMidHeight,
  188. $surfMidWidth * .1,
  189. 180, 270, 0, 0, 255, 255
  190. );
  191. # polygons
  192. # TBD...
  193. # characters & strings
  194. SDL::GFXCharacterRGBA( $surf, $surfMidWidth, 0, "!", 255, 255, 255, 255 );
  195. SDL::GFXStringRGBA(
  196. $surf, $surfMidWidth,
  197. $surfHeight * .75,
  198. "SDL_Perl Primitive Test.",
  199. 255, 255, 255, 255
  200. );
  201. $app->flip();
  202. $app->loop(
  203. { SDL_QUIT() => sub { exit(0); },
  204. SDL_KEYDOWN() =>
  205. sub { exit(0) if ( SDL::GetKeyState(SDLK_ESCAPE) ); },
  206. }
  207. );
  208. }
  209. ## Main program loop
  210. get_cmd_args();
  211. set_app_args();
  212. init_game_context();
  213. instruments();
  214. game_loop();