/** * @brief Renders the overlays. */ void omsg_render( double dt ) { int i, j; double x, y; omsg_t *omsg; glFont *font; glColour col; /* Case nothing to do. */ if (omsg_array == NULL) return; /* Center. */ x = omsg_center_x - omsg_center_w/2.; y = omsg_center_y; /* Render. */ for (i=0; i<array_size(omsg_array); i++) { omsg = &omsg_array[i]; /* Check if time to erase. */ omsg->duration -= dt; if (omsg->duration < 0.) { omsg_free( omsg ); array_erase( &omsg_array, &omsg[0], &omsg[1] ); i--; continue; } /* Must have a message. */ if (omsg->msg == NULL) continue; /* Render. */ font = omsg_getFont( omsg->font ); memcpy( &col, omsg->col, sizeof(glColour) ); if (omsg->duration < 1.) col.a = omsg->duration; for (j=0; j<omsg->nlines; j++) { y -= font->h * 1.5; if (j>0) gl_printRestoreLast(); gl_printMidRaw( font, omsg_center_w, x, y, &col, omsg->msg[j] ); } } }
/** * @brief Sets the message for an omsg. */ static void omsg_setMsg( omsg_t *omsg, const char *msg ) { int i, l, n, s, m; glFont *font; /* Clean up after old stuff. */ if (omsg->msg != NULL) { for (i=0; i<omsg->nlines; i++) free( omsg->msg[i] ); free( omsg->msg ); omsg->msg = 0; omsg->nlines = 0; } /* Create data. */ l = strlen( msg ); font = omsg_getFont( omsg->font ); /* First pass size. */ n = 0; m = 0; while (n < l) { s = gl_printWidthForText( font, &msg[n], omsg_center_w ); n += s+1; m++; } /* Avoid zero-length malloc. */ if (m == 0) return; /* Second pass allocate. */ omsg->msg = malloc( m * sizeof(char*) ); omsg->nlines = m; n = 0; m = 0; while (n < l) { s = gl_printWidthForText( font, &msg[n], omsg_center_w ); omsg->msg[m] = malloc( s+1 ); nsnprintf( omsg->msg[m], s+1, "%s", &msg[n] ); m++; n += s+1; } }