This hopefully should get you started using Pliant with SDL. The first thing we'll try is to initialize SDL, open a window and then blit an image onto it. Then we wait for an event, a key press or window close to quit SDL, then quit the program. The code for such a program is examined below and can also be found here.
I write this with the expectation that the reader has some knowledge of programming, probably in C or Java. But because Pliant is not widely known, I will try to explain a bit about Pliant as needed. Its recomended however that you refer to the official site for indepth information. I will also introduce a bit of SDL as well.
Here I go.
module "/pliant/language/context.pli" module "/pliant/SDL/SDL.pli" |
Import the SDL module
You always need to import the context.pli and SDL.pli module first.
Pliant modules can add new features and language elements. The context.pli module
imports many of the commonly used extensions in the Pliant Default Extension Environment
(PDEE) such as access to pointers. Its similar to |
function main -> ret arg Int ret var Pointer:SDL_Surface screen src #Display surfaces var SDL_Event event #Event structure |
SDL Surfaces & Events
The
Pliant PointersThePointer:
modifies a type to declare a pointer. Similar to saying SDL_Surface *screen in C. Pliant pointers are
automatically dereferenced, so you don't have to use *screen to get at the value as you would in C. The :> operator makes a pointer point to the address of a
variable; ptr :> variable in Pliant is similar
to ptr = &variable in C. See more
here.
Declaring Functions & Variables in PliantWe define a function called "main", which is our main function. In C and Java, you define a function called "main", which is the first function called when the program is executed. In Pliant however, we explicatley call main at the end of the program code. So the name "main" has no significance in Pliant, you could have easily called it bmpview if you liked. One thing to remember is that Pliant compiles executes your program top to down. One of the most important features of Pliant to know is its use of indentation. When defining functions , conditional constructs or loops ect, use an indent (one or more spaces, no tabs) to define the body or block of code. In C and Java, you would normally use brace brackets "{" and "}". Indentation replaces these brackets, and provide IMHO a cleaner look to the code. To declare functions: function [arg1 arg2 arg3 ...] [ -> retval] arg <type> arg1 arg2 ... arg <type> retval ; arg <type> arg3 # ";" is a seperator not a terminator. # All text beginning with "#" to the end of the line is a comment. # However, comments must not break indentation! So indent your comments too. ... ...
The "fake" argument retval, after
Variables
are declared using the |
if (SDL_Init SDL_INIT_VIDEO) < 0 console "Could not Initialize Video!" eol ret := 1 return |
Initializing SDL
Before doing anything with SDL, we need to initialize the system to use and
initialize supported devices (Video, Sound, Joystick, CDROM etc). This is done
by
Notice that in Pliant the assignment operator is |
#Create an 8-bit color surface, #screen :> (SDL_SetVideoMode 640 480 8 SDL_HWSURFACE .or. SDL_DOUBLEBUF) screen :> (SDL_SetVideoMode 640 480 8 SDL_SWSURFACE) if not (exists screen) console "Could not set video mode" ret := 1 SDL_Quit #Quit SDL return |
Also notice the way functions are called. The function is inside the parenthesis pair and no comas seperating the arguments.
Opening the Display
We make the surface pointer,
The SDL Display Surfaces
The
The last argument are display options. |
#Load sprite source image (SDL.bmp is an 8-bit bitmap file) #SDL.bmp can be found at http://pligame.sf.net/files/SDL.bmp src :> SDL_LoadBMP "SDL.bmp" |
Loading Bitmap Files
SDL has built-in support for standard, non-compressed (not RLE encoded) BMP image files.
The |
#Set screen's 8-bit palette to match sprite's SDL_SetColors screen src:format:palette:colors 0 src:format:palette:ncolors |
Setting the Color Palette (8-bit mode only)
8-bit graphics uses 256 colors, indexed 0 to 255. Each color is an index to a color palette.
This color palette can be modified by changing the red, blue and green components of each color
in the palette. We use Pliant Types (Structs)
SDL surfaces (type
In Pliant,like functions, we express structure fields as expression lists within
parenthesis. The width and height of surface The link operator, "... var uInt width height ... #Using parenthesis width := (src w) ; height := (src h) ... #Pliant automatically puts ( ) around all expresions: #<expr1> := <expr2>. width := src w ; height := src h ... #Using the "link" operator, ":". width := src:w ; height := src:h ... : " joins two
sub-expression inside a parenthesis pair. So what if we wanted field member
ncolors, three levels down? We can express this as:
If you find this confusing, then consider using " : " much like
". " in:
src.format.palette.ncolors in C or Java, in the context of accessing field members
in Pliant structures. Like indentation, the link operator substitues need of many parenthesis,
to make code appear more clean and elegant.
|
#Blit (fast copy) source image onto screen SDL_BlitSurface src null screen null SDL_UpdateRect screen 0 0 0 0 #SDL_Flip screen |
Blitting Graphics & Updating Display
A blit in SDL performs a fast copy of a rectangular section from one surface onto a
rectangular section of another surface. In this case, we are copying all of the
src surface onto all of the display screen. The
After all blits are made, we need to update the display. The function |
#CBool is generally faster than plain Bool var CBool done := false while not done while (SDL_PollEvent:event > 0) #Event handling inner loop #Wait till key goes up before quitting. Just feels right. if event:type = SDL_KEYUP done := true |
This program uses one-space indents, so look carefully to identify the nested blocks.
There is a Game Loop & Event Handling
The first
The next The link operator can save you a pair of parenthesis if your function takes only one argument. But its just a matter of taste. Some may disagree with it use, and decide that all functions, even with only one function should follow the same convention, enclosed in parenthesis.
In this program, we do not make any updates to the display because its static. We do however
handle user input. The " |
#Free surface memory (yes, Pliant has no garbage collection) SDL_FreeSurface src #Quit SDL. Important! SDL_Quit ret := 0 return |
Freeing Surface memory & Quiting SDL
The program has finished its game loop and is now preparing to exit. With exception for the
display surface, all other surfaces such as those created from
Before exiting the program, function |
main #Run the program! |
Running the Program
The last line invokes function "
You should then see the bitmap SDL.bmp being displayed on a 640x480 window. Press any key to exit.
|