Tutorial: Create an OpenGL window in C

In this tutorial, we show you how to create a window that you can use with OpenGL ES. When you create a project in the Momentics IDE for BlackBerry and choose the OpenGL ES 1.1 or 2.0 project template, the result is an OpenGL ES app that displays a multicolored square on the screen. The app sets up an OpenGL ES window, and this tutorial is a walkthrough of that template (and the files main.c and bbutil.c). We've removed the code to draw the multicolored square so we can focus on just the required setup code.

You will learn to:

  • Create a native window
  • Initialize and bind EGL to the native window
  • Draw graphics using OpenGL ES
  • Clean up graphics when you're done
Device image showing an OpenGL ES app.

Before you begin

You should have the following things ready:

  • The BlackBerry 10 Native SDK
  • A device or simulator running BlackBerry 10

Set up your project

Because we don't want to write all of the setup code from scratch, let's create a project using the BlackBerry 10 OS OpenGL ES 1.1 template.

  1. In the Momentics IDE, on the File menu, click New > BlackBerry Project.
  2. In the Project Type section, select Core Native. Click Next.
  3. In the Templates section, select the version of OpenGL that you want to use. Click Next.
  4. In the Basic Settings section, enter a name for your project (for example, MyProject). Click Next.
  5. In the API Level section, select the API level that you want to use.
  6. Click Finish.

There are two important files that you get with the template: main.c and bbutil.c. The main.c file is where you put all of your app-specific code. The bbutil.c file contains a set of utility functions to do things that are required by apps that use OpenGL ES. Here, we jump back and forth between the two files so that you understand the key steps of creating a native window within the context of the template.

To create an OpenGL ES window, we first have to create a native window. The bbutil.c API contains a function to create a native window: bbutil_init_egl(). This function contains a lot of EGL setup code. To create a native window, we first declare a place to store the window, screen_win, and then call the screen_create_window() function.

screen_window_t screen_win;

screen_create_window(&screen_win, screen_ctx);

The other parameter is a screen context variable. A context is just some information about the screen. Having the information in a single variable makes it easy to get at when it's needed. Because the screen context is required by functions that are specific to our app (for example, event handling), it is declared and initialized in main.c:

screen_context_t screen_ctx;

The screen context is passed in to bbutil_init_egl() later in main(). So, to set up a native window, you create a screen context and a screen window. The result is that screen_win refers to a native window that has some associated information about it stored in screen_ctx.

Last modified: 2015-03-31

Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus