Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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