Demo Tutorial 1

This tutorial will be the first of a series I’ll write about creating demos. I’m going to start off with simple topics and introduce concepts and ideas you’ll need when coding graphics on a computer.

What’ll we do then?
I’m going to use the PTC library to demonstrate how to do graphical effects on your computer. I’m developing this tutorial on Linux so any instructions on compiling the example programs will obviously be for that platform. If anyone feels like contributing or changing anything please feel free to do so!
Before we get started you’ll have to download the PTC library from the link above. Do that now and click back to get back here when it’s downloading.
Unpack and compile the library somewhere (Make sure you read the INSTALL file. There are notes there on optimising the compilation.)
Assuming that all went well, change directory to the PTC “examples” directory and create a directory there called “tut01″.
What about writing a point to the screen? Cut and paste the following into a file called Tut01.cc in the tut01 directory.

#include “ptc.h”

int main()
{
    try {  
        // create format
        Format format(32,0x00FF0000,0x0000FF00,0x000000FF);
    
        // create console
        Console console;
        console.open(“tut01″,320,200,format);
    
        // create surface
        Surface surface(320,200,format);

        // loop until a key is pressed
        while (!console.key()) {
            // lock surface
            int32 *buffer = (int32*) surface.lock();
 
            // draw a white pixel at (160,100).
            buffer[160+100*320]=(255< <16)+(255<<8)+255;

            // unlock surface
            surface.unlock();

            // copy to console
            surface.copy(console);

            // update console
            console.update();
        }
        // exit
        return 0;
    } catch (Error error) {
        // report error
        error.report();
    }
}

The following is the Makefile you’ll use:

# GNU Makefile for the X11 part of the PTC 2.0 C++ API
# Copyright (c) 1998 Christian Nentwich (brn@eleet.mcb.at)
# The PTC 2.0 C++ API is (c) 1998 Glenn Fiedler (ptc@gaffer.org)
# This package is licensed under the GNU LGPL
#
# Please refer to the file COPYING.LIB contained in the
# distribution for licensing conditions

include ../../Makefile.config

PROG1 = Tut01

all: $(PROG1)

$(PROG1): $(PROG1).o

$(CC) $(PROG1).o $(PTCLIB) $(PTC_X_LIBRARIES) -o $(PROG1)

.cc.o:

$(CC) -c $(CFLAGS) $< clean:

rm -f *.o

distclean:

rm -f *.o

rm -f $(PROG1)

Now compile Tut01.cc by typing make.
If you have previously compiled the example programs that came with PTC this should compile without problems.
Now run it from an xterm by typing:

er see a small window with a white dot in the middle of it, or your screen will go black and a white dot will appear in the middle.
Press a key to escape out of the program

You’ve started on the road to creating a demo!

How it works.
(lots of info taken from the PTC Intro)
Format format(32,0x00FF0000,0x0000FF00,0x000000FF); – The Format class holds information about how pixels are composed. ie. 8 bit, 16 bit, 32 bit colour. The above code creates a 32 bit colour format.

Console console; – This is “your interface to the world. It contains the routines to access the framebuffer, and some simple keyboard routines.”

console.open(“tut01″,320,200,format); – This either opens a window or sets the screen resolution to 320 pixels across by 200 down. PTC will attempt to change screen colour depth to the closest one available to match your format.

Surface surface(320,200,format); – “A Surface is a memory area (in system memory, not in video memory) that you can draw into if you don’t want to draw directly to the screen.”
This command creates a 32 bit colour surface which is 320 pixels across and 200 pixels down

while (!console.key()) {

}

– While no key is pressed do the following…
When you lock the surface to write to it you get a pointer to it in memory. Then the program draws a single white pixel on it.
The surface is then unlocked, and copied to the console before the console is finally updated (meaning the image in “buffer” is drawn to the screen.

Notes

I presume you know how to code in C++. If you don’t you’ll be lost. Mail me if you want to know the _very_ basics and I’ll include some docs on that.
The above example was derived from examples in the PTC examples/ directory. Take a look there!
You do all your drawing to a special part of memory called a buffer. When all your drawing is finished that buffer is copied to the screen to be displayed. This way of drawing avoids the choppy updates you see in some demos and games where the image you see appears to update right in the middle of the screen.
The buffer you draw to is a 32 bit colour image of the screen. Even if your video mode isn’t 32 bit the conversion will be handled by PTC before it displays your buffer on the screen.
Make sure you read the docs on the PTC homepage. There isn’t much there, but it’s informative, and of course, you have the source code to the library to play with too. Make sure you take advantage of the “GPL” and learn from the code!


You might also like

If you like this post then please subscribe to my full RSS feed. You can also click here to subscribe by email. There are also my fabulous photos to explore too!

Published by

Donncha

Donncha Ó Caoimh is a software developer at Automattic and WordPress plugin developer. He posts photos at In Photos and can also be found on Google+ and Twitter.

One thought on “Demo Tutorial 1”

Leave a Reply