Parrot SDL Objecty Goodness

chromatic on 2004-04-01T07:36:33

As seen in today's earlier Introducing Parrot SDL weblog (yes, that's my work weblog with a four-year old candid photo), I'm porting the existing Parrot SDL code to use real objects. Parrot barely had object support when I started this again several weeks ago. It's much nicer now.

After ninety minutes of hacking, including writing a small patch to improve subclassing exception messages, I can now load and display images and handle multiple surfaces simultaneously. Animation support is very near.

It's surprising that not only is PIR growing more and more familiar, but that it has workable idioms. Very few people have done or are doing anything large with the language, so I'm blazing a trail. How cool is that?

It's also surprising how nice the actual working code is. The bulk of the code in my example actually sets up parameters. Even considering that, aside from some function and method call sugar, you can only have one operation per line, this is surprisingly compact.

I still plan to port the two existing examples to the new code before checking anything in, but here's a followup to the blue rectangle example. This version displays the now-familiar Parrot logo.

.pcc_sub _main non_prototyped, @MAIN
	load_bytecode "library/SDL/App.imc"
	load_bytecode "library/SDL/Rect.imc"
	load_bytecode "library/SDL/Image.imc"

	.sym pmc app
	.sym int app_type

	find_type app_type, 'SDL::App'
	new app, app_type

	.sym pmc args
	new args, .PerlHash
	set args['height'], 480
	set args['width'],  640
	set args['bpp'],      0
	set args['flags'],    1

	app.'_new'( args )

	.sym int rect_type
	.sym pmc dest_rect
	.sym pmc source_rect

	find_type rect_type, 'SDL::Rect'
	new dest_rect,   rect_type
	new source_rect, rect_type

	new args, .PerlHash
	set args['height'], 100
	set args['width'],  100
	set args['x'],      270
	set args['y'],      190

	dest_rect.'_new'( args )

	set args['height'],  56
	set args['width'],  100
	set args['x'],        0
	set args['y'],        0

	source_rect.'_new'( args )

	.sym pmc image
	.sym int image_type

	find_type image_type, 'SDL::Image'
	new image, image_type

	image.'_new'( 'examples/sdl/parrot_small.png' )

	app.'blit'( image, source_rect, dest_rect )
	app.'update_rect'( dest_rect )

	sleep 2

	app.'quit'()
	end
.end


Even nicer syntax

Bernhard on 2004-04-01T09:51:31

There is even a choice of style in PIR. You can also write code like: .local pmc app .local int app_type app_type = find_type 'SDL::App' app = new app_type .local pmc args args = new PerlHash args['height'] = 480 args['width'] = 640 args['bpp'] = 0 args['flags'] = 1

What I'm missing though are curly brace for indicating scope. I like hitting '%' in vim.

Re:Even nicer syntax

Bernhard on 2004-04-01T09:55:36

Well, the above code doesn't look so nice after all. I'd better use the 'Preview' button before submitting.


Here is the nice PIR again:
  .local pmc app
  .local int app_type
  app_type = find_type 'SDL::App'
  app = new app_type

  .local pmc args
  args = new PerlHash
  args['height'] = 480
  args['width']  = 640
  args['bpp']    = 0
  args['flags']  = 1

Re:Even nicer syntax

chromatic on 2004-04-02T00:09:36

You're right; I'd forgotten about that. Thanks! The only remaining ugliness is in NCI calls, and apparently the new PIR syntax for invoking a Sub PMC works with NCI subs too.