Sunday, September 24, 2006

Post over a couple of days because I just don't write

(written 13/9/2006)
I never write :(

Not entirely true, but mostly.


Some recent changes include city and state of residence, going to school for teaching certification, and pretending to own a home (sort of)

I think that taking classes has been really an eye-opener in a number of ways (i'll get to that). Not the least of which; I'm old(ish) I am not the same person that I was ten years ago; for that matter, yesterday.

----

much later now... I'm writing this in bits and pieces during the day (now late where I live)

-----

great. now it's 21st of sept. how annoying is that? i feel like i have no time, but the truth is, it's just that I'm lazy and museless. kinda fun typing for an audience of 0. :)
Or am I my own audience.


*yawn*



----

I have been having fun playing more Perl. It's a great way to be creative without having to rebuild/paint/deal with mistakes etc.. but i do miss drawing.. darnitall. oh. yes, dealing with mistakes is there, but not so painful since you can work with them and learn from them without too much suffering.

My wife is listening to music of various sorts while studying her CSS. All great fun.


Back to Perl. I've been playing with Perl SDL. (need to find links to SDL).
The following is an example of programming mediocrity. Still was fun to do.
Who cares that there are dozens of better ways to do this?




#!/usr/bin/perl
use diagnostics;
use strict;

use SDL::App;
use SDL::Rect;
use SDL::Color;
use SDL::Event;
use SDL::TTFont;

use Data::Dumper;#DEBUG
my $XSIZE = 76;
my $YSIZE = 105;

my %rule;



###rules---

# the values are modified in situ. return values are
# reserved for errorhandling



$rule{top} = sub{
my $needle = shift;
my $goal = shift;
my $top = 5; #dist from top of window.
if (($needle->{y} < ($top + 1)) and ($needle->{v} <>{v} = 0;
}

return 0; #for error handling.
};


$rule{bottom} = sub{
my $needle = shift;
my $goal = shift;

if (($needle->{y} > ($YSIZE-6)) and
($needle->{v} > 0)){
$needle->{v} = 0;
}

return 0
};


$rule{brakes} = sub{
#put on the brakes when the needle passes it's goal.

my $needle = shift;
my $goal = shift;
my $goal = $YSIZE - $goal;
if (3 > abs($needle->{y} - $goal) ){
#print "$goal ", $needle->{y},"\n";#DEBUG
$needle->{v} = ($needle->{v} / 1.1);

}

return 0
};

$rule{goal} = sub{
#move toward the level.

my $needle = shift;
my $goal = shift;


my $realgoal = $YSIZE - $goal;
if (.3 <>{y} - $realgoal) ){
if ($realgoal > $needle->{y}){
$needle->{v}+= .1
}

if ($realgoal < $needle->{y}) {
$needle->{v}-= .1
}
}
# if (.3 > abs($needle->{y} - $realgoal) ){
# $needle->{v} = $needle->{v} / 5;
#}
if ($needle->{v} > 30){$needle->{v} = 30};
if ($needle->{v} < -30){$needle->{v} = (-30)};

return 0
};

### END of rules.



my $needle;

$needle->{x}=3;
$needle->{y}=$YSIZE - 3;
$needle->{v}=0;

my $app = new SDL::App(
-title => 'Battery',
-width => $XSIZE,
-height => $YSIZE,
-depth => 32,
);

event_loop();

sub event_loop{
my $event = new SDL::Event;

MAIN:
while (1){
while ($event->poll){
my $type = $event->type();
last MAIN if ($type == SDL_QUIT);
last MAIN if ($type == SDL_KEYDOWN
&& $event->key_name() eq 'escape');

}


$app->delay(5);
draw($needle);
}
}





sub affect{
my $needle = shift;
my $rule = shift;
my $goal = shift;
$goal =~s/\%//;

for (keys(%{$rule})){
$rule->{$_}->($needle,$goal);

}

$needle->{y}+=$needle->{v};
}











#---------------------------

{ #configuration and drawing data

#shared variables for this area::


my ($c_bg,$c0,$c1);

my ($numberfont,$numberfont_b,$numbersize,@numberpos);
my ($textfont,$textsize,@textpos);
my ($errorfont,$errorsize,@errorpos);
my ($titlefont,$titlesize,@titlepos);
my ($debugfont,$debugsize,@debugpos);
my ($data1,$data2);
my ($oldneedlex,$oldneedley);
my ($rect,$oldrect);
my ($debugtext);
my (@oldlines);


sub init_display{
$debugtext = "debugging";

$data2 = 'crap';
@oldlines = ($data2,$data2,$data2,$data2);

$c_bg = SDL::Color->new(
-r => 0,
-b => 0,
-g => 0,
);

$c0 = SDL::Color->new(
-r => 0x80,
-b => 0x22,
-g => 0x22,
);

$c1 = SDL::Color->new(
-r => 0x50,
-b => 0xaa,
-g => 0xaa,
);
$oldrect = SDL::Rect->new(
-width=>25,
-height=>5,
-x =>$needle->{x},
-y =>$needle->{y},
);

$numberfont = new SDL::TTFont(
-name=>"/var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType/Vera.ttf",
-size=>8,
-bg=>$c_bg,
-fg=>$c0,
);
$numberfont_b = new SDL::TTFont(
-name=>"/var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType/Vera.ttf",
-size=>8,
-bg=>$c_bg,
-fg=>$c_bg,
);
@numberpos = (5,40);

$debugfont = new SDL::TTFont(
-name=>"/var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType/Vera.ttf",
-size=>8,
-bg=>$c_bg,
-fg=>$c1,
);
@debugpos = (20,20);

}

sub draw{
my $needle= shift;
init_display unless (defined($c_bg));

chomp($data1 = `apm`);
my @datalines = splitapm($data1);

affect($needle,\%rule, $datalines[2]);

draw_rects();

if ($data1 ne $data2){
$numberfont_b->print($app,$numberpos[0]+35,$numberpos[1],$oldlines[0]);
$numberfont_b->print($app,$numberpos[0]+35,$numberpos[1]+10,$oldlines[2]);
$numberfont_b->print($app,$numberpos[0]+35,$numberpos[1]+20,$oldlines[3]);
}
$data2 = $data1;
@oldlines = @datalines;

$app->grab_input(SDL_GRAB_OFF);
#$debugfont->print($app,$debugpos[0],$debugpos[1],$debugtext);
$numberfont->print($app,$numberpos[0]+35,$numberpos[1],$datalines[0]);
$numberfont->print($app,$numberpos[0]+35,$numberpos[1]+10,$datalines[2]);
$numberfont->print($app,$numberpos[0]+35,$numberpos[1]+20,$datalines[3]);



$app->sync();
}

sub draw_rects {

$rect = SDL::Rect->new(
-width=>25,
-height=>3,
-x =>$needle->{x},
-y =>$needle->{y},
);





$app->fill($oldrect,$c_bg);
$app->fill($rect,$c1);
$app->update($oldrect);
$app->update($rect);

$oldrect = $rect;

}






}



sub splitapm{
my $data = shift;

$data =~ /(.*?)\,.*?(\w+?)\:\s(\d+\%).*?\((.*)\)/;

my @array = ($1,$2,$3,$4);

@array
}

0 Comments:

Post a Comment

<< Home