______________________________________________________________
___
___API glutBitmapCharacter
___
URL: https://www.opengl.org/documentation/specs/glut/spec3/node76.html
______________________________________________________________
___
___ VARIOUS WAYS WITH SOURCE DEMO
___
URL: https://www.opengl.org/archives/resources/features/fontsurvey/
______________________________________________________________
___
___ ONLY LETTERS
___
URL: https://mycodelog.com/tag/glutbitmapcharacter/
This can be done by using C’s va_list type, which is C’s approach to defining functions with variables number of arguments.
- In the function prototype, place ellipsis (…) as the last argument
- Define a variable argument list: va_list args;
- Call va_start on the args list and the first real argument in the function prototype preceding the ellipsis:va_start(args, format);
- Use _vscprintf to get the number of characters that would be generated if the string pointed to by the list of arguments was printed using the specified format
- Allocate memory for a string with the specified number of characters
- Call vsprintf_s to build the string we want from the list of arguments
- Call va_end to end the use of the variables argument list
- Draw our beautified string
Function implementation with full comments shown below:
- Free allocated memory
//-------------------------------------------------------------------------
// Draws a string at the specified coordinates.
//-------------------------------------------------------------------------
#include <stdio.h> // Standard Input\Output C Library#include <stdarg.h> // To use functions with variables arguments
#include <stdlib.h> // for malloc
#include <gl/glut.h> // Include GLUT, OpenGL, and GLU libraries
//-------------------------------------------------------------------------
// Draws a string at the specified coordinates.
//-------------------------------------------------------------------------
void
printw (
float
x,
float
y,
float
z,
char
* format, ...)
{
va_list
args;
// Variable argument list
int
len;
// String length
int
i;
// Iterator
char
* text;
// Text
// Initialize a variable argument list
va_start
(args, format);
// Return the number of characters in the string
// referenced the list of arguments.
// _vscprintf doesn't count terminating '\0' (that's why +1)
len = _vscprintf(format, args) + 1;
// Allocate memory for a string of the specified size
text =
malloc
(len *
sizeof
(
char
));
// Write formatted output using a pointer to the list of arguments
vsprintf_s(text, len, format, args);
// End using variable argument list
va_end
(args);
// Specify the raster position for pixel operations.
glRasterPos3f (x, y, z);
// Draw the characters one by one
for
(i = 0; text[i] !=
'\0'
; i++) {
glutBitmapCharacter(font_style, text[i]);
}
// Free the allocated memory for the string
free
(text);
}
No comments:
Post a Comment