Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 88 lines (59 sloc) 1.699 kB
2a8b618 @kthakore Adding 2nd example for event handling
kthakore authored
1 use strict;
2 use warnings;
3 use SDL;
36bc0ae @kthakore Added save_image function to paint example
kthakore authored
4 use Cwd;
2a8b618 @kthakore Adding 2nd example for event handling
kthakore authored
5 use SDL::Event;
6 use SDLx::App;
7
e7287bd @kthakore We are called Simple Paint
kthakore authored
8 my $app = SDLx::App->new( w => 200, h => 200, d => 32, title => "Simple Paint");
2a8b618 @kthakore Adding 2nd example for event handling
kthakore authored
9 sub quit_event {
10
11 my $event = shift;
12 return 0 if $event->type == SDL_QUIT;
13 return 1;
14 }
15
2ee131c @kthakore Added mouse section for paint
kthakore authored
16
b823c12 @kthakore Updated 2nd example for keyboard input.
kthakore authored
17
18 my @colors = ( 0xFF0000FF, 0x00FF00FF,
19 0x0000FFFF, 0xFFFF00FF,
20 0xFF00FFFF, 0x00FFFFFF,
21 0xCCFFCCFF, 0xFFCC33FF,
22 0x000000FF, 0xFFFFFFFF );
23
24 my $brush_color = 0;
2ee131c @kthakore Added mouse section for paint
kthakore authored
25
26
27 my $drawing = 0;
2a8b618 @kthakore Adding 2nd example for event handling
kthakore authored
28 sub mouse_event {
b823c12 @kthakore Updated 2nd example for keyboard input.
kthakore authored
29
30 my $event = shift;
31
32 if($event->type == SDL_MOUSEBUTTONDOWN || $drawing)
33 {
2ee131c @kthakore Added mouse section for paint
kthakore authored
34
b823c12 @kthakore Updated 2nd example for keyboard input.
kthakore authored
35 $drawing = 1;
36 my $x = $event->button_x;
37 my $y = $event->button_y;
73fd254 @kthakore Added an image, and more explaination to the code for simple paint
kthakore authored
38 $app->draw_rect( [$x,$y, 2, 2], $colors[$brush_color]);
2ee131c @kthakore Added mouse section for paint
kthakore authored
39 $app->update();
b823c12 @kthakore Updated 2nd example for keyboard input.
kthakore authored
40 }
41 $drawing = 0 if($event->type == SDL_MOUSEBUTTONUP );
42
43
44 return 1;
2a8b618 @kthakore Adding 2nd example for event handling
kthakore authored
45 }
46
36bc0ae @kthakore Added save_image function to paint example
kthakore authored
47
48 sub save_image {
49
50 if( SDL::Video::save_BMP( $app, 'painted.bmp' ) == 0 && -e 'painted.bmp')
51 {
52 warn 'Saved painted.bmp to '.cwd();
53 }
54 else
55 {
56 warn 'Could not save painted.bmp: '.SDL::get_errors();
57 }
58
59 }
60
61
2a8b618 @kthakore Adding 2nd example for event handling
kthakore authored
62 sub keyboard_event {
63
64 my $event = shift;
65
66 if ( $event->type == SDL_KEYDOWN )
b823c12 @kthakore Updated 2nd example for keyboard input.
kthakore authored
67 {
68 my $key_name = SDL::Events::get_key_name( $event->key_sym );
69
70 $brush_color = $key_name if $key_name =~ /\d/;
2a8b618 @kthakore Adding 2nd example for event handling
kthakore authored
71
36bc0ae @kthakore Added save_image function to paint example
kthakore authored
72 my $mod_state = SDL::Events::get_mod_state();
73fd254 @kthakore Added an image, and more explaination to the code for simple paint
kthakore authored
73 save_image if $key_name =~ /^s$/ && ($mod_state & KMOD_CTRL);
74
75 $app->draw_rect( [0,0,$app->w, $app->h], 0 ) if $key_name =~ /^c$/
b823c12 @kthakore Updated 2nd example for keyboard input.
kthakore authored
76 }
73fd254 @kthakore Added an image, and more explaination to the code for simple paint
kthakore authored
77 $app->update();
2a8b618 @kthakore Adding 2nd example for event handling
kthakore authored
78 return 1;
79 }
b823c12 @kthakore Updated 2nd example for keyboard input.
kthakore authored
80
81
82 $app->add_event_handler( \&quit_event );
83 $app->add_event_handler( \&mouse_event );
84 $app->add_event_handler( \&keyboard_event );
85 $app->run();
86
87
Something went wrong with that request. Please try again.