Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 65 lines (46 sloc) 1.275 kb
2a8b618 @kthakore Adding 2nd example for event handling
kthakore authored
1 use strict;
2 use warnings;
3 use SDL;
4 use SDL::Event;
5 use SDLx::App;
6
7 my $app = SDLx::App->new( w => 200, h => 200, d => 32, title => "Quit Events");
8 sub quit_event {
9
10 my $event = shift;
11 return 0 if $event->type == SDL_QUIT;
12 return 1;
13 }
14
15 my $drawing = 0;
b823c12 @kthakore Updated 2nd example for keyboard input.
kthakore authored
16
17 my @colors = ( 0xFF0000FF, 0x00FF00FF,
18 0x0000FFFF, 0xFFFF00FF,
19 0xFF00FFFF, 0x00FFFFFF,
20 0xCCFFCCFF, 0xFFCC33FF,
21 0x000000FF, 0xFFFFFFFF );
22
23 my $brush_color = 0;
2a8b618 @kthakore Adding 2nd example for event handling
kthakore authored
24 sub mouse_event {
b823c12 @kthakore Updated 2nd example for keyboard input.
kthakore authored
25
26 my $event = shift;
27
28 if($event->type == SDL_MOUSEBUTTONDOWN || $drawing)
29 {
30 # now you can handle the details;
31 $drawing = 1;
32 my $x = $event->button_x;
33 my $y = $event->button_y;
34 $app->[$x][$y] = $colors[$brush_color];
35 $app->update();
36 }
37 $drawing = 0 if($event->type == SDL_MOUSEBUTTONUP );
38
39
40 return 1;
2a8b618 @kthakore Adding 2nd example for event handling
kthakore authored
41 }
42
43 sub keyboard_event {
44
45 my $event = shift;
46
47 if ( $event->type == SDL_KEYDOWN )
b823c12 @kthakore Updated 2nd example for keyboard input.
kthakore authored
48 {
49 my $key_name = SDL::Events::get_key_name( $event->key_sym );
50
51 $brush_color = $key_name if $key_name =~ /\d/;
2a8b618 @kthakore Adding 2nd example for event handling
kthakore authored
52
b823c12 @kthakore Updated 2nd example for keyboard input.
kthakore authored
53 warn 'Brush color is '.$brush_color;
54 }
2a8b618 @kthakore Adding 2nd example for event handling
kthakore authored
55 return 1;
56 }
b823c12 @kthakore Updated 2nd example for keyboard input.
kthakore authored
57
58
59 $app->add_event_handler( \&quit_event );
60 $app->add_event_handler( \&mouse_event );
61 $app->add_event_handler( \&keyboard_event );
62 $app->run();
63
64
Something went wrong with that request. Please try again.