Skip to content

Latest commit

 

History

History
143 lines (81 loc) · 4.71 KB

02-drawing.pod

File metadata and controls

143 lines (81 loc) · 4.71 KB

Preview

SDL provides several ways to draw on screen; these methods can be broken down into: Primitives, Images and Text. All three methods draw on a single object the Surface. The Surface is represented by SDLx::Surface. Even our SDLx::App is a SDLx::Surface. This means that we can draw directly on the SDLx::App, however there are several advantages to drawing on multiple surfaces. In this chapter we will explore these methods of drawing, and make a pretty picture.

Program

use SDL;
use SDLx::App;

my $app = SDLx::App->new( w => 400, h => 400, title => 'Drawing Examples' );

#Draw Code Starts Here


#Draw Code Ends Here

$app->update();
  
sleep(2);

Syntax Overview

Drawing in SDL are done on Surfaces. The SDLx::Surface object provides access to methods in the form of:

$surface->draw_{something}( .... );

Parameters are usually provided as array references, to define areas and colors.

Rectangular Parameters

Some parameters are just a quick definition of the positions and dimensions. For a rectangle that will be placed at (20, 20) pixel units on the screen, and a dimension of 40x40 pixel units the following would suffice.

my $rect = [20, 20, 40, 40];

Color

in SDL is described by 4 numbers. The first three numbers define the Red, Blue, Green intensity of the color. The final number defines the transparency of the Color.

my $color = [255, 255, 255, 255];

The values of the numbers range from 0-255 for 32 bit depth in RGBA format.

Primitives

Drawing are usually simples shapes that can be used for creating graphics dynamically.

Lines

$app->draw_line( [200,20], [20,200], [255, 255, 0, 255] );

This will draw a yellow line from positions (200,20) to (20,200) .

Rectangles

Rectangles are a common building blocks for games. In SDL rectangles are the most cost effective of the primitives to draw.

$app->draw_rect( [10,20, 40, 40 ], [255, 255, 255,255] );

The above will add a white square of size 40x40 onto the screen at the position (10,20).

Circles

Circles are drawn similarly either filled or unfilled.

$app->draw_circle( [100,100], 20, [255,0,0,255] );
$app->draw_circle_filled( [100,100], 19, [0,0,255,255] );

Now we will have a filled circle, colored blue and unfilled circle, colored as red.

More GFX functions

For more complex drawing functions have a look at SDL::GFX::Primitives.

Multiple Surfaces

Now we don't always use one surface. Most games use many Surfaces. These are held in memory. You can make blank ones and draw on them. Or you can make Surfaces from various data.

Creating Surfaces

Images

Images can be loaded as surfaces.

Sprites

Sprites is a lot better way to think of this.

Text

Default GFX

It is ugly but can be useful.

TrueType

This is probably the more common way to use text in SDL. However it has its limitations

Pango

Pango features several rendering options, and thus is heavy. It is good for multilingual support.

POD ERRORS

Hey! The above document had some coding errors, which are explained below:

Around line 1:

Unknown directive: =head0