Permalink
Browse files

Merge pull request #12 from KohaAloha/master

add a palette cycling example
  • Loading branch information...
2 parents 48fdb0e + c544657 commit acfeb0306a98d506cc4897239ee64e79ee978332 @FROGGS FROGGS committed Feb 12, 2013
Showing with 57 additions and 0 deletions.
  1. BIN code_listings/froggs.png
  2. +57 −0 code_listings/palette_cycle.pl
View
BIN code_listings/froggs.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
57 code_listings/palette_cycle.pl
@@ -0,0 +1,57 @@
+use Modern::Perl;
+use SDL;
+use SDL::Video;
+use SDL::Color;
+use SDL::Image;
+use SDL::Event;
+use SDL ':init';
+use SDL::Event;
+use SDL::Events ':all';
+my $screen_width = 300;
+my $screen_height = 340;
+my $x = 0;
+my $quit = 0;
+SDL::init(SDL_INIT_VIDEO);
+
+# make a screen surface
+my $screen_surface
+ = SDL::Video::set_video_mode( $screen_width, $screen_height, 8,
+ SDL_SWSURFACE | SDL_HWPALETTE );
+
+# load and blit an image
+my $img = SDL::Image::load('froggs.png');
+
+# this image blit only needs to be done *once* :)
+SDL::Video::blit_surface( $img, undef, $screen_surface, undef );
+
+
+while ( !$quit ) {
+
+ # check for a quit?
+ get_events();
+ #
+ set_palette();
+}
+
+sub get_events {
+ my $event = SDL::Event->new();
+
+ #Pump the event queue
+ SDL::Events::pump_events;
+ while ( SDL::Events::poll_event($event) ) {
+ $quit = 1 if $event->type == SDL_QUIT;
+ }
+}
+
+sub set_palette {
+ my @clrs;
+
+ #push 256 color objects into an array
+ foreach my $i ( 0 .. 255 ) { $clrs[$i] = SDL::Color->new( $x, 0, $i ); }
+
+ # update surfaces' palette with 256 new colors
+ my $rc
+ = SDL::Video::set_palette( $screen_surface, SDL_PHYSPAL, 0, @clrs );
+ $x++;
+ $x = 0 if $x == 255;
+}

0 comments on commit acfeb03

Please sign in to comment.