1package SDL::Tutorial::Sol::Two;
2use strict;
3use warnings;
4use Carp;
5
6use SDL v2.3;
7use SDL::Video;
8use SDL::Event;
9use SDL::Events;
10use SDL::Surface;
11
12my $screen;
13
14sub putpixel {
15	my ( $x, $y, $color ) = @_;
16	my $lineoffset = $y * ( $screen->pitch / 4 );
17	$screen->set_pixels( $lineoffset + $x, $color );
18}
19
20sub render {
21	if ( SDL::Video::MUSTLOCK($screen) ) {
22		return if ( SDL::Video::lock_surface($screen) < 0 );
23	}
24
25	my $ticks = SDL::get_ticks();
26	my ( $i, $y, $yofs, $ofs ) = ( 0, 0, 0, 0 );
27	for ( $i = 0; $i < 480; $i++ ) {
28		for ( my $j = 0, $ofs = $yofs; $j < 640; $j++, $ofs++ ) {
29			$screen->set_pixels( $ofs, ( $i * $i + $j * $j + $ticks ) );
30		}
31		$yofs += $screen->pitch / 4;
32	}
33
34	putpixel( 10, 10, 0xff0000 );
35	putpixel( 11, 10, 0xff0000 );
36	putpixel( 10, 11, 0xff0000 );
37	putpixel( 11, 11, 0xff0000 );
38
39	SDL::Video::unlock_surface($screen) if ( SDL::Video::MUSTLOCK($screen) );
40
41	SDL::Video::update_rect( $screen, 0, 0, 640, 480 );
42
43	return 0;
44}
45
46sub main {
47	Carp::cluck 'Unable to init SDL: ' . SDL::get_error()
48		if ( SDL::init(SDL_INIT_VIDEO) < 0 );
49
50	$screen = SDL::Video::set_video_mode( 640, 480, 32, SDL_SWSURFACE );
51
52	Carp::cluck 'Unable to set 640x480x32 video' . SDL::get_error() if ( !$screen );
53
54	while (1) {
55
56		render();
57
58		my $event = SDL::Event->new();
59
60		while ( SDL::Events::poll_event($event) ) {
61			my $type = $event->type;
62			return 0 if ( $type == SDL_KEYDOWN );
63			return 0 if ( $type == SDL_QUIT );
64
65		}
66		SDL::Events::pump_events();
67
68	}
69
70}
71
72main;
73
74SDL::quit;
75
76