2.9 Porting World Programs to Universe
2.9.1 The World is Not Enough
With the June 2009 release, we started deprecating the world teachpack; instead we recommended the use of the universe teachpack. With the January 2010 release, we are also introducing a new image teachpack and, in support of this second teachpack, we have separated out the image functionality from the functionality for world programs.
World Style | Universe Style | ||
|
In the next section, we first explain how to port world programs so that they use the universe teachpack and the old image teachpack. In the section after that, we list suggestions for changing programs so that they no longer rely on the old image functionality but the new one.
In order to distinguish between the various pieces of functionality, we uniformly prefix old functionality with "htdp:" and new functionality with "2htdp:". There is no need to use these prefixes in your programs of course.
2.9.2 Porting World Programs
(require htdp/world) ; Number -> Scene (define (create-UFO-scene height) (htdp:place-image UFO 50 height (htdp:empty-scene 100 100))) ; Scene (define UFO (htdp:overlay (htdp:circle 10 'solid 'red) (htdp:rectangle 40 4 'solid 'red))) ; — run program run (htdp:big-bang 100 100 (/1 28) 0) (htdp:on-tick-event add1) (htdp:on-redraw create-UFO-scene)
the first one creates the 100 by 100 scene, specifies a rate of 28 images per second, and 0 as the initial world description;
the second one says that for each clock tick, the world (a number) is increased by 1; and
the last line tells drscheme to use create-UFO-scene as the function that renders the current world as a scene.
World Style | Universe Style | ||
|
World Style | Universe Style | ||||||||||||
|
|
World Style | Universe Style | ||||||||||||||
|
|
World Style | Universe Style | |||||||
|
|
The other big change concerns key event handling and mouse event handling. The respective handlers no longer accept symbols and chars but strings only. Here is the first key event handler from the documentation of the world teachpack:
World Style | Universe Style | ||||||||||||||||||||||||||||
|
|
If you ever recorded your programs’ work via an animated gif, you can still do so. Instead of adding a fifth argument to big-bang, however, you will need to add a clause of the shape (record? x).
Finally, the universe teachpack implements a richer functionality than the world teachpack.
2.9.3 Porting Image Programs
The universe library also comes with a new image library, 2htdp/image. Using the old image library still works fine with 2htdp/universe, but the new image library provides a number of improvements, including faster image comparison (especially useful in check-expect expressions), rotating images, scaling images, curves, a number of new polygon shapes, and more control over line drawing.
To use the new image library in isloation:
World Style | Universe Style |
and to use the new image library with the universe teachpack:
World Style | Universe Style | ||
|
Overlay vs Underlay
The htdp:overlay function places its first argument under its second (and subsequent) arguments and so in 2htdp/image, we decided to call that function 2htdp:underlay.
World Style | Universe Style | ||||||||||
|
|
No more pinholes
The concept of pinholes from htdp/image has no correspondance in 2htdp/image (we do expect to bring back pinholes in 2htdp/image eventually, but they will not be as pervasive as they are in htdp/image).
Instead of a special position in the image that overlay operations are sensitive to, 2htdp/image has a family of overlay operations, that overlay images based on their centers or their edges.
Since the default position of the pinhole is in the center for most images and the default for overlaying and underlaying images in 2htdp/image is based on the center, simple examples (like the one above) behave the same in both libraries.
But, consider this expression that overlays two images on their upper-left corners, written using both libraries.
World Style | Universe Style | ||||||||||||||
|
|
In the 2htdp/image version, the programmer uses 2htdp:underlay/align to specify where the images should be lined up, instead of using the pinhole.
Outlines in different places
The outline style shapes are now shifted by one pixel for 2htdp/image images as compared to htdp/image. This means that these two rectangles draw the same sets of pixels.
World Style | Universe Style | ||||
|
|
See also The Nitty Gritty of Pixels, Pens, and Lines.
Star changed
The 2htdp:star function is a completely different function from htdp:star. Both produce stars based, on polygons, but 2htdp:star always produces a five-pointed star. See also 2htdp:star-polygon for more general star shapes.