Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 84 lines (60 sloc) 1.785 kb
2a8b6187 » kthakore
2010-09-26 Adding 2nd example for event handling
1 use strict;
2 use warnings;
3 use SDL;
36bc0ae0 » kthakore
2010-09-27 Added save_image function to paint example
4 use Cwd;
2a8b6187 » kthakore
2010-09-26 Adding 2nd example for event handling
5 use SDL::Event;
6 use SDLx::App;
7
74def651 » kthakore
2010-11-07 Added the PDL code example and part of the chapter
8 my $app = SDLx::App->new( w => 600, h => 480, d => 32, title => "Simple Paint");
2a8b6187 » kthakore
2010-09-26 Adding 2nd example for event handling
9 sub quit_event {
10
11 my $event = shift;
49f25f3a » kthakore
2010-10-03 Updated to use SDL 2.518
12 my $controller = shift;
13 $controller->stop if $event->type == SDL_QUIT;
14
2a8b6187 » kthakore
2010-09-26 Adding 2nd example for event handling
15 }
16
2ee131cd » kthakore
2010-09-27 Added mouse section for paint
17
b823c12f » kthakore
2010-09-27 Updated 2nd example for keyboard input.
18
19 my @colors = ( 0xFF0000FF, 0x00FF00FF,
20 0x0000FFFF, 0xFFFF00FF,
21 0xFF00FFFF, 0x00FFFFFF,
22 0xCCFFCCFF, 0xFFCC33FF,
23 0x000000FF, 0xFFFFFFFF );
24
25 my $brush_color = 0;
2ee131cd » kthakore
2010-09-27 Added mouse section for paint
26
27
28 my $drawing = 0;
2a8b6187 » kthakore
2010-09-26 Adding 2nd example for event handling
29 sub mouse_event {
b823c12f » kthakore
2010-09-27 Updated 2nd example for keyboard input.
30
31 my $event = shift;
32
33 if($event->type == SDL_MOUSEBUTTONDOWN || $drawing)
34 {
2ee131cd » kthakore
2010-09-27 Added mouse section for paint
35
b823c12f » kthakore
2010-09-27 Updated 2nd example for keyboard input.
36 $drawing = 1;
37 my $x = $event->button_x;
38 my $y = $event->button_y;
73fd254d » kthakore
2010-09-27 Added an image, and more explaination to the code for simple paint
39 $app->draw_rect( [$x,$y, 2, 2], $colors[$brush_color]);
2ee131cd » kthakore
2010-09-27 Added mouse section for paint
40 $app->update();
b823c12f » kthakore
2010-09-27 Updated 2nd example for keyboard input.
41 }
42 $drawing = 0 if($event->type == SDL_MOUSEBUTTONUP );
2a8b6187 » kthakore
2010-09-26 Adding 2nd example for event handling
43 }
44
36bc0ae0 » kthakore
2010-09-27 Added save_image function to paint example
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
2a8b6187 » kthakore
2010-09-26 Adding 2nd example for event handling
60 sub keyboard_event {
61
62 my $event = shift;
49f25f3a » kthakore
2010-10-03 Updated to use SDL 2.518
63 my $controller = shift;
2a8b6187 » kthakore
2010-09-26 Adding 2nd example for event handling
64 if ( $event->type == SDL_KEYDOWN )
b823c12f » kthakore
2010-09-27 Updated 2nd example for keyboard input.
65 {
66 my $key_name = SDL::Events::get_key_name( $event->key_sym );
67
68 $brush_color = $key_name if $key_name =~ /\d/;
2a8b6187 » kthakore
2010-09-26 Adding 2nd example for event handling
69
36bc0ae0 » kthakore
2010-09-27 Added save_image function to paint example
70 my $mod_state = SDL::Events::get_mod_state();
73fd254d » kthakore
2010-09-27 Added an image, and more explaination to the code for simple paint
71 save_image if $key_name =~ /^s$/ && ($mod_state & KMOD_CTRL);
72
49f25f3a » kthakore
2010-10-03 Updated to use SDL 2.518
73 $app->draw_rect( [0,0,$app->w, $app->h], 0 ) if $key_name =~ /^c$/;
74 $controller->stop() if $key_name =~ /^q$/
b823c12f » kthakore
2010-09-27 Updated 2nd example for keyboard input.
75 }
73fd254d » kthakore
2010-09-27 Added an image, and more explaination to the code for simple paint
76 $app->update();
2a8b6187 » kthakore
2010-09-26 Adding 2nd example for event handling
77 return 1;
78 }
b823c12f » kthakore
2010-09-27 Updated 2nd example for keyboard input.
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.