In this chapter we will look at how to use pixel effects in Perl. Pixel effects are operations that are done directly on the bank of a SDL_Surface
's pixel. These effects are used to do visual effects in games and applications, most notably by Frozen Bubble
.
These effects can be done in purely in Perl, for 1 passes and non real time applications. Effects that need to be done real time will have to be done in C via XS. This chapter will show two methods of doing this.
For our first pixel effect we will be doing is a ripple effect from a well known SDL resource, HTTP://Sol.Gfxile.Net/gp/ch02.html. This effects uses SDL::get_ticks
to animate a ripple effect across the surface as seen in the following figure.
First lets make the effect in pure Perl. To do any operations with a SDL::Surface
we must do SDL::Video::lock_surface()
call as seen below. Locking the surface prevents other process in SDL from accessing the surface. The surface pixels can be accessed several ways from Perl. Here we are using the SDL::Surface::set_pixels
which takes an offset for the SDL_Surface
pixels array, and sets a value there for us. The actual pixel effect is just a time dependent (using SDL::get_ticks
for time) render of a function. See HTTP://Sol.Gfxile.Net/gp/ch02.html for a deeper explanation.
One you run this program you will find it pretty much maxing out the CPU and not running very smoothly. At this point running a loop through the entire pixel bank of a 640x480
sized screen is too much for Perl. We will need to move the intensive calculations to C
.
In the below example we use Inline
to write Inline C
code to handle the pixel effect for us. SDL
now provides support to work with Inline
. The render
callback is now moved to C
code, using Inline C
. When the program first runs it will compile the code and link it in for us.
Hey! The above document had some coding errors, which are explained below:
- Around line 1:
-
Unknown directive: =head0
- Around line 14:
-
Deleting unknown formatting code U<>
- Around line 23:
-
Deleting unknown formatting code U<>