A First Program

In this section, we will create our first LVT program, simple. simple is a very basic (and very boring) program that displays a window on the screen, and then exits when the window is closed.

simple.cpp:

#include <lvt/lvt.h>

int main(int argc, char *argv[])
{
    LVT::App myApp;
    LVT::Wnd myWnd;

    myApp.Init(&argc, &argv);

    myWnd.Create("My Window");

    myApp.Run(myWnd);

    return 0;
}

Let's examine the individual pieces of the code.

    #include <lvt/lvt.h>

Most LVT programs will begin by including lvt/lvt.h. This header contains the most common class definitions and is sufficient for most programs.

    LVT::App myApp;
    LVT::Wnd myWnd;

Every LVT program contains exactly one App object and one or more Wnd objects. Notice that both objects are in the LVT namespace, so we must qualify them. Alternatively, we could have brought them into scope with a using namespace LVT statement.

    myApp.Init(&argc, &argv);

The first thing any LVT program must do is call the App::Init function. Init establishes a connection to the windowing system and initializes the windowing portion of the library.We pass Init the address of the argc and argv variables. This will parse the command line for common options such as --display that well-behaved X11 programs are expected to understand. You must call Init before calling any other LVT object member function.

    myWnd.Create("My Window");

Wnd::Create creates the window and sets its title (in this case, the title is "My Window"). Create does not show (or present) the window. Create will return false if the creation was not successful. Once the call to Create succeeds, OpenGL is initialized, and we are ready to begin drawing.

    myApp.Run(myWnd);

App::Run initializes the message loop and begins processing messages. Run takes one optional argument: a reference to the applications "main" window. Run will also show the window passed to it. When this window is closed, the messageloop will end, and Run will return.

Compiling simple

To compile simple on UNIX and UNIX-Like systems, we will make use of the pkg-config utility to manage the library dependencies. We can compile the program like this:

$ g++ -o simple simple.cpp `pkg-config --cflags --libs liblvt`

To compile simple on Windows systems, we link against either liblvt.lib, or liblvtd.lib (for debugging). LVT provides a default implementation of WinMain that calls a user-supplied main function. This ensures that any LVT program will compile on any LVT-supported platform. If you wish to use your own WinMain instead of main include a

#define LVT_NO_DEF_WINMAIN
statement before including lvt/lvt.h. Some LVT classes contain inlined calls to OpenGL, so we will also need to link against opengl32.lib. The LVT source distribution contains Visual Studio project files for all sample programs. You can use these as templates for creating your own LVT programs.

If all went well, we should be able to run our new program and see a very basic, very boring window that should look something like this.

In the next section, we'll look at handling user input, so that our window can actually do something useful.