Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added functions for managing resources, states, and fleshed out the main

loop and initialization functions
  • Loading branch information...
commit 79f2387a6c5168e216ffb0fecc142ab86f7680e0 1 parent 95bfb1e
@nerdshark authored
Showing with 99 additions and 83 deletions.
  1. +99 −83 lib/platformer.pm
View
182 lib/platformer.pm
@@ -1,84 +1,100 @@
-package platformer;
-
-use 5.010001;
-use strict;
-use warnings;
-
-require Exporter;
-
-our @ISA = qw(Exporter);
-
-# Items to export into callers namespace by default. Note: do not export
-# names by default without a very good reason. Use EXPORT_OK instead.
-# Do not simply export all your public functions/methods/constants.
-
-# This allows declaration use platformer ':all';
-# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
-# will save memory.
-our %EXPORT_TAGS = ( 'all' => [ qw(
-
-) ] );
-
-our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
-
-our @EXPORT = qw(
-
-);
-
-our $VERSION = '0.01';
-
-
-# Preloaded methods go here.
-
-1;
-__END__
-# Below is stub documentation for your module. You'd better edit it!
-
-=head1 NAME
-
-platformer - Perl extension for blah blah blah
-
-=head1 SYNOPSIS
-
- use platformer;
- blah blah blah
-
-=head1 DESCRIPTION
+# this package provides the main engine functionality
-Stub documentation for platformer, created by h2xs. It looks like the
-author of the extension was negligent enough to leave the stub
-unedited.
-
-Blah blah blah.
-
-=head2 EXPORT
-
-None by default.
-
-
-
-=head1 SEE ALSO
-
-Mention other useful documentation such as the documentation of
-related modules or operating system documentation (such as man pages
-in UNIX), or any relevant external documentation such as RFCs or
-standards.
-
-If you have a mailing list set up for your module, mention it here.
-
-If you have a web site set up for your module, mention it here.
-
-=head1 AUTHOR
-
-A. U. Thor, E<lt>dorkfish@elys.comE<gt>
-
-=head1 COPYRIGHT AND LICENSE
-
-Copyright (C) 2010 by A. U. Thor
-
-This library is free software; you can redistribute it and/or modify
-it under the same terms as Perl itself, either Perl version 5.10.1 or,
-at your option, any later version of Perl 5 you may have available.
-
-
-=cut
+package platformer;
+use base 'Badger::Base';
+
+
+use Badger::Class
+# mutators => 'config window clock';
+
+use SDL;
+use SDL::Video;
+use SDL::Surface;
+use SDL::Audio;
+use SDL::Event;
+use SDL::Image;
+
+use Cogent::Window;
+use platformer::Level;
+
+#my %config = {
+# window => {800, 600, 32, '*initflags*'},
+# levels => {
+# 'menu' => 'menu.dat',
+# 'level 1' => 'level1.dat',
+# 'level 2' => 'level2.dat',
+# },
+#};
+
+sub new {
+ my ($self) = @_;
+ SDL::init( SDL_INIT_VIDEO | SDL_INIT_AUDIO);
+ $self->{window} = Cogent::Window->new(800, 600, 32, SDL_DOUBLEBUF | SDL_SWSURFACE);
+ $self->{clock} = SDLx::Clock->new();
+
+ $self->{states} = { menu => platformer::Level->new( filename => 'data/levels/menu.dat' ),
+ level1 => platformer::Level->new(filename => 'data/levels/level1.dat') };
+
+
+
+}
+
+sub run {
+ my $self = shift;
+ $self->set_state('menu');
+
+ $self->{clock}->start;
+ my $prev_time_delta = $self->{clock}->get_ticks();
+ while (1) {
+ my $time_delta = $self->{clock}->get_ticks() - $prev_time_delta;
+ $self->handle_events();
+ $self->update($time_delta);
+ $self->{window}->draw();
+ $prev_time_delta += $time_delta;
+ }
+}
+
+# allows the engine to handle common events (e.g. window resizes, quitting the game), and pass off unhandled events to the current state
+sub handle_events() {
+ my $self = shift;
+ my $event = SDL::Event->new();
+
+}
+
+# runs the main engine logic and the current state's logic with the same update interval, keeping everything in sync
+sub update {
+ my ($self, $time_delta) = @_;
+ $self->{current_state}->update($time_delta);
+}
+
+sub set_window {
+ # TODO: Find out how to reinitialize SDL window
+}
+
+sub get_window {
+ my $self = shift;
+ return $self->{window};
+}
+
+sub set_state {
+ my ($self, $state_name) = @_;
+ $self->{current_state} = $self->{states}->{$state_name};
+}
+
+# Hash-based resource management. Allows for initialization and lookup of resources based on resource type and specific name.
+sub set_resource {
+ my ($self, $rsrc_type, $rsrc_name, $rsrc) = @_;
+ $self->{resources}->{$rsrc_type}->{$rsrc_name} = \$rsrc;
+}
+
+sub get_resource {
+ my ($self, $rsrc_type, $rsrc_name) = @_;
+ return $self->{resources}->{$rsrc_type}->{$rsrc_name};
+}
+
+sub clear_resources {
+ my $self = shift;
+ $self->{resources} = {};
+}
+
+1;
Please sign in to comment.
Something went wrong with that request. Please try again.