HiveBrain v1.2.0
Get Started
← Back to all entries
debugcMinor

Initializing an EGL display

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
displayeglinitializing

Problem

I'm somewhat new to writing real-world projects in C, so I'm not familiar with best practices and standards. I'm writing code dealing with EGL and OpenGL, and some setup calls might fail for reasons outside my control. I want to write an initialization function that performs all necessary setup for my application, but exits early and returns false if any call fails (essentially replicating simple exception handling behavior).

Given various philosophies regarding multiple returns, GOTOs, and so forth, I've written my function as follows:

```
int init_gl(EGLDisplay outDisplay, EGLContext outContext, EGLSurface* outSurface) {
int success = 1;

EGLConfig config;
EGLint num_config;

EGLDisplay display;
EGLContext context;
EGLSurface surface;

static const EGLint attribute_list[] =
{
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_NONE
};

static const EGLint context_attributes[] =
{
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};

bcm_host_init();

success = success && (display = eglGetDisplay(EGL_DEFAULT_DISPLAY)) != EGL_NO_DISPLAY;
check();

success = success && eglInitialize(display, NULL, NULL) != EGL_FALSE;
check();

success = success && eglChooseConfig(display, attribute_list, &config, 1, &num_config) != EGL_FALSE;
check();

success = success && eglBindAPI(EGL_OPENGL_ES_API) != EGL_FALSE;
check();

success = success && (context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attributes)) != EGL_NO_CONTEXT;
check();

success = success && (surface = eglCreateWindowSurface(display, config, NULL, NULL)) != EGL_NO_SURFACE;
check();

success = success && eglMakeCurrent(display, surface, surface, context) != EGL_FALSE;
check();

if(success) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

Solution

Certainly found the alternative (2nd version) immediately understandable. Not so with the first (both simplified below). The 2nd maps directly to the stated requirement "exits early and returns false if any call fails". This alone gives it a strong preference.

int success = 1;
....
success = success && func1();
success = success && func2();
success = success && func3();
....


versus

if (!func1()) {
  return 0;
}
if (!func2()) {
  return 0;
}
if (!func3()) {
  return 0;
}
....


Minor: Why isn't the first success = success && func1(); vs. int success = func1();. Just to maintain symmetry?

Why use int instead of bool? Portability? If C99 or later available as the minimum standard, better to use bool.

The success approach may have merit in select circumstances yet do not see that called for here. I would opt for the if () style as it matches the scant coding requirements.

Code Snippets

int success = 1;
....
success = success && func1();
success = success && func2();
success = success && func3();
....
if (!func1()) {
  return 0;
}
if (!func2()) {
  return 0;
}
if (!func3()) {
  return 0;
}
....

Context

StackExchange Code Review Q#111451, answer score: 2

Revisions (0)

No revisions yet.