2.3. Creating an OpenGL ES rendering context

This example draws to the context:

  1. Open the main.cpp file from the 02 - Creating an OpenGL Rendering Context project.

    Example 2.6 shows the creation of the System, Context, and Keyboard interfaces:

    Example 2.6. Create a rendering context

    #include <mde/mde.h>
    using namespace MDE;
    int main()
        try // all of the code is in a try block
            Managed<System> system = create_system_backend();
            Managed<Context> context = system->createContext(320, 240);
            #ifdef MDE_OS_PC_LINUX
                MDE::Managed<MDE::Keyboard> keyboard = system->createKeyboard(context);
                MDE::Managed<MDE::Keyboard> keyboard = system->createKeyboard();

  2. Example 2.7 shows how to create a mouse object with the MDE library:

    Example 2.7. Creating a mouse

           int xClick, yClick;
           // create a mouse object
           Managed<Mouse> mouse= system->createMouse(context);

  3. Example 2.8 shows the draw loop:

    Example 2.8. Draw loop for empty context

                glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
                * The update() function will swap buffers to refresh the image on the screen. 
                * The function returns false if the context is told to close by the operating
                * system.
                if(!context->update()) break;
                if(keyboard->getSpecialKeyState(Keyboard::KEY_ESCAPE)) break;

    The clear color is specified as (1.0f, 0.0f, 0.0f, 1.0f) which is red.

    Each call to update() swaps the draw buffers and returns true if the context is still active.

    Drawing and swapping buffers continues until either:

    • the context is told to close by the operating system. If this happens, context->update() returns false.

    • the keyboard interface processes an Esc keypress.

  4. The mouse detection code is shown in Example 2.9:

    Example 2.9. Reacting to mouse events

                // Get X and Y coordinates
                xClick = mouse->getAxisPosition(Mouse::X_AXIS);
                yClick = mouse->getAxisPosition(Mouse::Y_AXIS);
                printf("Mouse click at: %i, %i \n", xClick, yClick);


    • The getAxisPosition() functions are called whenever the mouse button is down, so multiple printf() statements are called for each button depression.

    • If the mouse click is outside the draw window, an event is still generated.

    • You can use the Observer class to simplify using the mouse in an application.

  5. Example 2.10 shows the exception-handler code for the application:

    Example 2.10. Catching an exception from the draw loop

        catch(Exception& e)
            printf("An exception was thrown:\n%s\n", e.getMessage().getCharString() );

  6. Compile and run the project.

    Because the context is used in this example, the graphics output window shown in Figure 2.2 is displayed:

    Figure 2.2. Blank graphics context

    Blank graphics context

    The background is red because glClearColor() was called with red as the color parameter.

Copyright © 2010 ARM. All rights reserved.ARM DUI 0527A‑02a
Non-Confidential - Draft - BetaID070710