File Coverage

blib/lib/SDLx/App.pm
Criterion Covered Total %
statement 92 147 62.5
branch 12 62 19.3
condition 27 67 40.3
subroutine 17 29 58.6
pod 0 13 0.0
total 148 318 46.5


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2             #
3             # App.pm
4             #
5              
6             package SDLx::App;
7              
8 5     5   4150 use strict;
  5         11  
  5         146  
9 5     5   25 use warnings;
  5         10  
  5         135  
10 5     5   25 use Carp;
  5         11  
  5         312  
11 5     5   36 use SDL;
  5         11  
  5         362  
12              
13 5     5   1228 use SDL::Rect;
  5         20  
  5         880  
14 5     5   1327 use SDL::Video;
  5         14  
  5         1944  
15 5     5   1614 use SDL::Event;
  5         15  
  5         3613  
16 5     5   1636 use SDL::Events;
  5         16  
  5         11620  
17 5     5   950 use SDL::Surface;
  5         15  
  5         1096  
18 5     5   46 use SDL::PixelFormat;
  5         10  
  5         1299  
19 5     5   1643 use SDL::VideoInfo;
  5         16  
  5         1534  
20 5     5   2256 use SDLx::Surface;
  5         13  
  5         225  
21 5     5   2533 use Data::Dumper;
  5         27711  
  5         363  
22 5     5   50 use Scalar::Util 'refaddr';
  5         12  
  5         243  
23 5     5   32 use base qw/SDLx::Surface SDLx::Controller/;
  5         11  
  5         2474  
24              
25             our $VERSION = 2.548;
26              
27             my $screen_w;
28             my $screen_h;
29             my $screen_d;
30              
31             sub new {
32 3     3 0 32874 my $proto = shift;
33 3   33     26 my $class = ref($proto) || $proto;
34 3         17 my %options = @_;
35              
36 3 0 33     16 unless($screen_w && $screen_h && $screen_d) {
      33        
37 3         37 my $video_info = SDL::Video::get_video_info();
38 3 100       29 if($video_info) {
39 2         12 $screen_w = $video_info->current_w;
40 2         8 $screen_h = $video_info->current_h;
41 2         31 $screen_d = $video_info->vfmt->BitsPerPixel;
42             }
43             }
44              
45             # SDL_INIT_VIDEO() is 0, so check defined instead of truth.
46 3 50       21 unless ( exists( $options{noinit} ) ) # we shouldn't do init always
47             {
48             my $init =
49             defined $options{init}
50             ? $options{init}
51 3 50       15 : SDL::SDL_INIT_EVERYTHING;
52              
53 3         119 SDL::init($init);
54             }
55              
56 3   33     29 my $t = $options{title} || $options{t} || $0;
57 3   33     28 my $it = $options{icon_title} || $options{it} || $t;
58 3   50     26 my $ic = $options{icon} || $options{i} || "";
59 3   50     16 my $w = $options{width} || $options{w} || 800;
60 3   50     17 my $h = $options{height} || $options{h} || 600;
61 3   50     44 my $d = $options{depth} || $options{d} || 16;
62 3   50     27 my $f = $options{flags} || $options{f} || SDL::Video::SDL_ANYFORMAT;
63 3   50     23 my $r = $options{red_size} || $options{r} || 5;
64 3   50     24 my $g = $options{green_size} || $options{g} || 5;
65 3   50     24 my $b = $options{blue_size} || $options{b} || 5;
66 3   50     27 my $a = $options{alpha_size} || $options{a} || 0;
67 3   50     22 my $ras = $options{red_accum_size} || $options{ras} || 0;
68 3   50     25 my $gas = $options{green_accum_size} || $options{gas} || 0;
69 3   50     22 my $bas = $options{blue_accum_size} || $options{bas} || 0;
70 3   50     32 my $aas = $options{alpha_accum_size} || $options{aas} || 0;
71 3   50     44 my $db = $options{double_buffer} || $options{db} || 0;
72              
73 3   50     38 my $bs = $options{buffer_size} || $options{bs} || 0;
74 3   50     30 my $st = $options{stencil_size} || $options{st} || 0;
75 3   50     15 my $async = $options{asyncblit} || 0;
76              
77 3 50 33     17 $f |= SDL::Video::SDL_OPENGL if ( $options{gl} || $options{opengl} );
78             $f |= SDL::Video::SDL_FULLSCREEN
79 3 50 33     17 if ( $options{fullscreen} || $options{full} );
80 3 50       10 $f |= SDL::Video::SDL_RESIZABLE if ( $options{resizeable} );
81 3 50       26 $f |= SDL::Video::SDL_DOUBLEBUF if ($db);
82 3 50       10 $f |= SDL::Video::SDL_ASYNCBLIT if ($async);
83              
84 3 50       14 if ( $f & SDL::Video::SDL_OPENGL ) {
85 0         0 $SDLx::App::USING_OPENGL = 1;
86 0 0       0 SDL::Video::GL_set_attribute( SDL::Constants::SDL_GL_RED_SIZE(), $r )
87             if ($r);
88 0 0       0 SDL::Video::GL_set_attribute( SDL::Constants::SDL_GL_GREEN_SIZE(), $g )
89             if ($g);
90 0 0       0 SDL::Video::GL_set_attribute( SDL::Constants::SDL_GL_BLUE_SIZE(), $b )
91             if ($b);
92 0 0       0 SDL::Video::GL_set_attribute( SDL::Constants::SDL_GL_ALPHA_SIZE(), $a )
93             if ($a);
94              
95 0 0       0 SDL::Video::GL_set_attribute(
96             SDL::Constants::SDL_GL_RED_ACCUM_SIZE(),
97             $ras
98             ) if ($ras);
99 0 0       0 SDL::Video::GL_set_attribute(
100             SDL::Constants::SDL_GL_GREEN_ACCUM_SIZE(),
101             $gas
102             ) if ($gas);
103 0 0       0 SDL::Video::GL_set_attribute(
104             SDL::Constants::SDL_GL_BLUE_ACCUM_SIZE(),
105             $bas
106             ) if ($bas);
107 0 0       0 SDL::Video::GL_set_attribute(
108             SDL::Constants::SDL_GL_ALPHA_ACCUM_SIZE(),
109             $aas
110             ) if ($aas);
111              
112 0 0       0 SDL::Video::GL_set_attribute(
113             SDL::Constants::SDL_GL_DOUBLEBUFFER(),
114             $db
115             ) if ($db);
116 0 0       0 SDL::Video::GL_set_attribute(
117             SDL::Constants::SDL_GL_BUFFER_SIZE(),
118             $bs
119             ) if ($bs);
120 0         0 SDL::Video::GL_set_attribute( SDL::Constants::SDL_GL_DEPTH_SIZE(), $d );
121             } else {
122 3         8 $SDLx::App::USING_OPENGL = 0;
123             }
124              
125 3 50       3292 my $surface = SDL::Video::set_video_mode( $w, $h, $d, $f )
126             or Carp::confess SDL::get_error();
127 3         14 $options{surface} = $surface;
128              
129 3         33 my $self = SDLx::Surface->new(%options);
130              
131 3 50 33     21 if ( $ic and -e $ic ) {
132 0         0 my $icon = SDL::Video::load_BMP($ic);
133 0         0 SDL::Video::wm_set_icon($icon);
134             }
135              
136 3         17 SDL::Video::wm_set_caption( $t, $it );
137 3         35 $self = $self->SDLx::Controller::new(%options);
138 3         9 bless $self, $class;
139              
140 3         14 return $self;
141             }
142              
143             sub resize {
144 0     0 0 0 my ( $self, $w, $h ) = @_;
145 0         0 my $flags = $self->flags;
146 0 0       0 if ( $flags & SDL::Video::SDL_RESIZABLE ) {
147 0         0 my $bpp = $self->format->BitsPerPixel;
148 0 0       0 $self = SDL::Video::set_video_mode( $w, $h, $bpp, $flags )
149             or die "SDL cannot set video:" . SDL::get_error;
150             } else {
151 0         0 die "Application surface not resizable";
152             }
153             }
154              
155             sub title {
156 0     0 0 0 my $self = shift;
157 0         0 my ( $title, $icon );
158 0 0       0 if (@_) {
159 0         0 $title = shift;
160 0   0     0 $icon = shift || $title;
161 0         0 SDL::Video::wm_set_caption( $title, $icon );
162             }
163 0         0 return SDL::Video::wm_get_caption();
164             }
165              
166             sub delay {
167 0     0 0 0 my $self = shift;
168 0         0 my $delay = shift;
169 0         0 SDL::delay($delay);
170             }
171              
172             sub ticks {
173 0     0 0 0 return SDL::get_ticks();
174             }
175              
176             sub error {
177 0     0 0 0 return SDL::get_error();
178             }
179              
180             sub warp {
181 0     0 0 0 my $self = shift;
182 0         0 SDL::Mouse::warp_mouse(@_);
183             }
184              
185             sub fullscreen {
186 0     0 0 0 my $self = shift;
187 0         0 SDL::Video::wm_toggle_fullscreen($self);
188             }
189              
190             sub iconify {
191 0     0 0 0 my $self = shift;
192 0         0 SDL::Video::wm_iconify_window();
193             }
194              
195             sub grab_input {
196 0     0 0 0 my ( $self, $mode ) = @_;
197 0         0 SDL::Video::wm_grab_input($mode);
198             }
199              
200             sub sync {
201 0     0 0 0 my $self = shift;
202 0 0       0 if ($SDLx::App::USING_OPENGL) {
203 0         0 SDL::Video::GL_swap_buffers();
204             } else {
205 0         0 $self->flip();
206             }
207             }
208              
209             sub attribute {
210 0     0 0 0 my ( $self, $mode, $value ) = @_;
211 0 0       0 return undef unless ($SDLx::App::USING_OPENGL);
212 0 0       0 if ( defined $value ) {
213 0         0 SDL::Video::GL_set_attribute( $mode, $value );
214             }
215 0         0 my $returns = SDL::Video::GL_get_attribute($mode);
216 0 0       0 Carp::confess "SDLx::App::attribute failed to get GL attribute"
217             if ( $$returns[0] < 0 );
218 0         0 $$returns[1];
219             }
220              
221              
222              
223             my %_stash;
224             sub stash :lvalue{
225 0     0 0 0 my $ref = refaddr($_[0]);
226 0 0       0 $_stash{ $ref } = {} unless $_stash{ $ref };
227 0         0 return $_stash{ $ref }
228             }
229              
230             sub DESTROY {
231 3 0 33 3   2224583 if($screen_w && $screen_h && $screen_d) {
      33        
232 0           SDL::Video::set_video_mode( $screen_w, $screen_h, $screen_d, SDL_ANYFORMAT );
233             }
234             }
235              
236             1;