Sample l3d Program

Before looking at the l3d classes themselves, let's first look at a sample program which uses l3d. This will give you a practical perspective on l3d before looking at the following sections, which go into more detail on the specific l3d classes.

The following sample program is called drawdot and illustrates usage of the l3d library classes in order to move a green dot around the screen, thereby forming a simple drawing program. This program works with visuals of any color depth and in both TrueColor or indexed color modes. Notice that this program is rather short and declares only one class. This is because the l3d library has already declared several useful classes to simplify application programs.

Figure 1-7: Output from sample program drawdot.

Listing 1-1:

#include <stdlib.h> #include <stdio.h>

#include "../lib/tool_2d/screen.h" #include "../lib/tool_os/dispatch.h" #include "../lib/raster/rasteriz.h" #include "../lib/tool_2d/scrinfo.h"

#include "../lib/system/factorys.h"




void choose_factories(void) { factory_manager_v_0_1.choose_factories();




class my_pipeline : public l3d_pipeline { protected: l3d_rasterizer_2d_imp *ri; l3d_rasterizer_2d *r;

public: l3d_screen *s; my_pipeline(void); virtual ~my_pipeline(void);

void key_event(int ch); //- from dispatcher void update_event(void); //- from dispatcher void draw_event(void); //- from dispatcher

my_pipeline::my_pipeline(void) {

s = factory_manager_v_0_1.screen_factory->create(320,200);

ri = factory_manager_v_0_1.ras_2d_imp_factory->create(320,200,s->sinfo);

r = new l3d_rasterizer_2d(ri);

s->sinfo->ext_max_red = s->sinfo->ext_max_green = s->sinfo->ext_max_blue = 255;

s->sinfo->ext_to_native(0, 0, 0); //- allocate background color color = s->sinfo->ext_to_native(0, 255, 128); s->refresh_palette();

my_pipeline::~my_pipeline(void) { delete s; delete ri; delete r;

void my_pipeline::key_event(int ch) { switch(ch) {



dx=-1; dy=0; break



dx=1; dy=0; break;



dx=0; dy=1; break;



dx=0; dy=-1; break


dx=0;dy=0; break;



void my_pipeline::update_event() { x += dx; y += dy;

if(x > s->xsize-1) x = s->xsize-1; if(y < 0) y = 0;

void my_pipeline::draw_event(void) { r->draw_point(x,y, color); s->blit_screen();


l3d_dispatcher *d;

my_pipeline *p;




d = factory_manager_v_0_1.dispatcher_factory->create();



//- plug our custom behavior pipeline into the dispatcher p = new my_pipeline();



d->pipeline = p; //- polymorphic assignment d->event_source = p->s;

delete d; delete p;

<jy NOTE The following instructions assume you have already installed the source code as ^^ described in the Appendix. In particular, the installation instructions require you to set the $L3D environment variable to point to the installation directory. Also, you should execute these commands in a shell window under the X Window System. See the Appendix for details on initial installation and configuration of the source code.

First, let's look at compiling the program. Then, we look at the structure of the program itself. Finally, we discuss the l3d classes.

The source code for the sample program is located in directory $L3D/source/app/ drawdot. The source and binary files are located in different directory trees; the next section discusses this in detail. For now, compile and run the program as follows:

1. To compile the l3d library, type cd $L3D/source/app/lib, press Enter, type make -f makeall.lnx, and press Enter. Notice that this makefile has a different filename than the standard name of Makefile; therefore, we specify the -f flag to tell the make command which file is the makefile.

2. Change to the source directory for drawdot by typing cd $L3D/source/app/drawdot and press Enter. Compile drawdot: type make -f makeall.lnx and press Enter.

3. Type cd $L3D/binaries/linux_x/float/app/drawdot to change to the binaries directory and press Enter.

4. Notice the object and executable files from the compilation process are placed in the corresponding binary directory. Type drawdot and press Enter to run the program.

5. Notice the question "which configuration?" Type 1 for now to select a normal X11 window, and press Enter.

6. Notice the empty black window which appears. Type l. Notice the green line which moves from left to right across the very top of the display, and that the line continues moving after you release the key.

7. Type j. Notice that the green line moves downward.

8. Control the movement of the line with the following keys: h to move left, l to move right, j to move down, k to move up, and Space to stop movement.

9. Type q to end the program.

Having now successfully executed the program, let's now take a look at the organization of the l3d directory structure, then examine the drawdot program itself.

Was this article helpful?

0 0
Photoshop CS Mastery

Photoshop CS Mastery

Artists, photographers, graphic artists and designers. In fact anyone needing a top-notch solution for picture management and editing. Set Your Photographic Creativity Free. Master Adobe Photoshop Once and For All - Create Flawless, Dramatic Images Using The Tools The Professionals Choose. Get My Video Tutorials and Retain More Information About Adobe Photoshop.

Get My Free Videos

Post a comment