PLMBase Component : 2D Video
This section deals with graphic output. So here, you
will find classes to initialise the screen (in
fullscreen or windowed mode), to draw some basic
primitives and write some text, and to create and
copy images.
-
PLMColor : Simple type definition (in fact a
32 bits integer). Can be used to store a color in
any color depth.
-
PLMDrawable : Virtual class that defines an
interface with drawing primitives (lines,
rectangle, circle, text and blits).
-
PLMScreen : Main class to access video
hardware (singleton, only one instance). Derives
from PLMDrawable.
-
PLMImage : Usually called a surface, this
object is used to store any graphic data out of the
screen, like sprites, textures... Derives from
PLMDrawable. Can load (and save) data from
different file formats (PCX, GIF, BMP, TGA).
-
PLMFont : Internal format, bitmap font, to
be used with PLMDrawable objects.
-
PLMPal : Stores any number of color
definitions (32 bits RGBA).
-
PLMVideoMode : A simple class to query the
system for available video modes (fullscreen).
This first example shows how to initialise a screen
from command-line arguments :
bool init_screen (int argc, char *argv[])
{
u32 flags = 0;
u32 width = 640, height = 480, depth = 8;
int i;
// parse command line to set screen depth and fullscreen
for (i = 1; i < argc; ++i)
{
if (strcmp (argv[i], "-full") == 0)
flags |= PLMScreen::FULL;
else if (strcmp (argv[i], "-8") == 0)
depth = 8;
else if (strcmp (argv[i], "-16") == 0)
depth = 16;
else if (strcmp (argv[i], "-24") == 0)
depth = 24;
else if (strcmp (argv[i], "-32") == 0)
depth = 32;
else
cout << "> arg " << argv[i] << " ignored\n";
}
if (argc > 1)
{
cout << "You asked: " << width << " x " << height
<< " x " << depth << " bpp, ";
if (flags & PLMScreen::FULL)
cout << "fullscreen\n";
else
cout << "windowed\n";
}
// get an instance of the screen object
PLMScreen *screen = PLMScreen::GetInstance ();
ASSERT (screen);
// init screen with command-line parameters
if (! screen->Init (width, height, flags, depth))
{
cerr << "Can not init screen\n";
return false;
}
// look at the parameters we actually got
width = screen->Width ();
height = screen->Height ();
depth = screen->Depth ();
cout << "You got: " << width << " x " << height
<< " x " << depth << " bpp\n";
cout << "SDL flags: " << screen->Flags () << endl;
return true;
}
|
The second example shows how to draw something on a
PLMDrawable object, here a screen :
// lines
for (i = 0; i < 40; i++)
{
screen->HLine (10, 10+i*2, 300, i);
screen->HLine (10, 11+i*2, 300, i);
screen->VLine (410+i*2, 10, 80, i);
screen->VLine (411+i*2, 10, 80, i);
screen->Line (470, 280, 550, 100+i*9, i);
screen->Line (630, 280, 550, 100+i*9, i);
}
// shapes
PLMColor col = screen->RGB2Col (255, 0, 0); // red
screen->Frame (20, 380, 80, 80, col);
screen->Rectangle (120, 380, 80, 80, col);
screen->Circle (280, 420, 40, col);
screen->Disk (380, 420, 40, col);
// gradients
PLMColor col2 = screen->RGB2Col (255, 255, 0); // yellow
screen->Gradient (20, 320, 80, 40, red, yellow, PLMDrawable::GRAD_WE);
screen->Gradient (120, 320, 80, 40, red, yellow, PLMDrawable::GRAD_NS);
screen->Refresh();
|
Do not forget to call Refresh() to update the screen
content, otherwise you won't see anything. Another
usual error is to forget to delete the screen when
the program is terminated.
Then we can write some text :
// load the font gui.font (in the current directory)
PLMFont font ("gui.font");
font.PrintInfo (false);
// write some text at the edge of the screen to test clipping
PLMColor col = screen->RGB2Col (255, 0, 0); // red
char *text = "0123456789";
screen->PutPixel (9, 3, col);
screen->DrawText (10, 3, text, col, &font, FONT_LINE);
screen->DrawText (10, height-9, text, col, &font, FONT_TOP);
screen->DrawText (-3, 182, text, col, &font, FONT_TOP);
screen->DrawText (width-37, 182, text, col, &font, FONT_TOP);
screen->Refresh();
|
And finally load an image and copy it to the screen :
// load a PCX image from the file sprite.pcx
PLMImage img;
// img needs a PLMResFile object to read its data from
PLMResFile f ("sprite.pcx");
if (! f.Good() || ! img.Load (f))
{
cerr << "Error loading image sprite.pcx\n";
return;
}
// read the color of the first pixel (0,0)
PLMColor key = img.GetPixel (0, 0);
// copy the image on the screen at position (10,100),
// using color 'key' as transparent color
screen->CopyFrom (10, 100, key, &img);
screen->Refresh();
|
|