You copied the Doc URL to your clipboard.

2.2. Creating a simple application with just the System and Keyboard classes

The following steps describe how to create a System and Keyboard interface:

  1. Open the main.cpp file from the 01 - Introduction to the System Interface project in the MDE Tutorial Examples folder.

    Example 2.1 shows how to create a System interface to use with the other library components:

    Example 2.1. Create a System object and a context

    	
    #include <mde/mde.h>
    #include <cstdio>
    
    int main()
    {
    MDE::Managed<MDE::System> system = MDE::create_system_backend();
    
    #ifdef MDE_OS_PC_LINUX
        MDE::Context* context = system->createContext(320,240);
    #endif
    ...
    

    The Managed<> template provides automatic lifetime checking and resource deletion. ARM recommends that you always use it when creating library objects. If Managed<> is not used, there must be a corresponding call to release() for each object creation. For example:

    	
    MDE::System* system = MDE::system_create_backend();
    ...
    system->release();
    

    The createContext() parameters specify the width and height of the graphics window. For this example, the framebuffer is created but will not be used.

  2. Example 2.2 shows how to create a Keyboard interface:

    Example 2.2. Create a Keyboard object

    	
    ...
    #ifdef MDE_OS_PC_LINUX
        MDE::Keyboard* keyboard = system->createKeyboard(context);
    #else
        MDE::Keyboard* keyboard = system->createKeyboard();
    #endif
    ...    
    

    Note

    There are minor difference in the creation of inputs between Windows and X11 platforms. Input devices are linked to specific contexts on X11 systems, for example, to a window on the display that this input device is used with.

  3. Keyboard input sequences the program to the next stage. Example 2.3 shows how to prompt the user to press the Esc key:

    Example 2.3. Reading keyboard input

    	
    ...
        printf("Press ESC to continue\n");
        while (true) 
        {
            if (keyboard->getSpecialKeyState(MDE::Keyboard::KEY_ESCAPE)) break;
        }
    
        keyboard->release();
    ...   
    

    Because the Keyboard object in Example 2.2 was not created with the Managed<> template, you must explicitly call release() on the keyboard interface.

  4. Example 2.4 shows the next stage of the application that deliberately raises an exception by trying to create a non-existent file system:

    Example 2.4. Basic exception handling

    	
    ...
        try
        {
            MDE::Managed<MDE::FileSystem> fs = system->createFileSystem("non-existing root");
            fs->createInputStream("non-existing file");
        
        }
        catch (MDE::Exception& e)
        {
            printf("An expected exception was thrown:\n%s\n", e.getMessage().getCharString() );
        }
    }
    

    You can put the standard C++ try blocks around code that you are testing.

    Exceptions generated by the UI Engine have types derived from MDE::Exception. Use a catch(MDE::Exception) statement to catch all exceptions generated by the UI Engine. For example code, ARM recommends putting a try block around all of the code in main() as shown in Example 2.5:

    Example 2.5. Using a try catch block

    	
    main()
    { 
        try 
        {
        // all executable code
        }
        catch (MDE::Exception& e)
        {
        // exception handling code
        }
    }
    

  5. Compile and run the project to test keyboard input and exception trapping. Figure 2.1 shows the system window:

    Figure 2.1. System window showing exception trapping

    System window showing exception trapping

    Note

    There is not a graphical display window because nothing was drawn to a context.