Skip to content

Commit acfeb03

Browse files
committedFeb 12, 2013
Merge pull request #12 from KohaAloha/master
add a palette cycling example
2 parents 48fdb0e + c544657 commit acfeb03

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
 

‎code_listings/froggs.png

127 KB
Loading

‎code_listings/palette_cycle.pl

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use Modern::Perl;
2+
use SDL;
3+
use SDL::Video;
4+
use SDL::Color;
5+
use SDL::Image;
6+
use SDL::Event;
7+
use SDL ':init';
8+
use SDL::Event;
9+
use SDL::Events ':all';
10+
my $screen_width = 300;
11+
my $screen_height = 340;
12+
my $x = 0;
13+
my $quit = 0;
14+
SDL::init(SDL_INIT_VIDEO);
15+
16+
# make a screen surface
17+
my $screen_surface
18+
= SDL::Video::set_video_mode( $screen_width, $screen_height, 8,
19+
SDL_SWSURFACE | SDL_HWPALETTE );
20+
21+
# load and blit an image
22+
my $img = SDL::Image::load('froggs.png');
23+
24+
# this image blit only needs to be done *once* :)
25+
SDL::Video::blit_surface( $img, undef, $screen_surface, undef );
26+
27+
28+
while ( !$quit ) {
29+
30+
# check for a quit?
31+
get_events();
32+
#
33+
set_palette();
34+
}
35+
36+
sub get_events {
37+
my $event = SDL::Event->new();
38+
39+
#Pump the event queue
40+
SDL::Events::pump_events;
41+
while ( SDL::Events::poll_event($event) ) {
42+
$quit = 1 if $event->type == SDL_QUIT;
43+
}
44+
}
45+
46+
sub set_palette {
47+
my @clrs;
48+
49+
#push 256 color objects into an array
50+
foreach my $i ( 0 .. 255 ) { $clrs[$i] = SDL::Color->new( $x, 0, $i ); }
51+
52+
# update surfaces' palette with 256 new colors
53+
my $rc
54+
= SDL::Video::set_palette( $screen_surface, SDL_PHYSPAL, 0, @clrs );
55+
$x++;
56+
$x = 0 if $x == 255;
57+
}

0 commit comments

Comments
 (0)
Please sign in to comment.