//*************************************************************************
//
// File Name: printw.c
// Author : Ali Baderedine
//
// Description: A function similar to printf but prints to window...
//
//*************************************************************************
#include <gl/glut.h>
#include <stdarg.h>
#include <stdio.h>
// Link the lib files to the program. This is not necessary
// if you have added the lib names using Project/Settings/Link
#pragma comment (lib, "opengl32.lib")
#pragma comment (lib, "glut32.lib")
#pragma comment (lib, "glu32.lib")
// define the window position on screen
int window_X_position;
int window_Y_position;
// variables representing the window size
int window_width = 300;
int window_height = 100;
// variable representing the window title
char *window_title = "printw";
// Just a pointer to a font style..
// Fonts supported by GLUT are: GLUT_BITMAP_8_BY_13,
// GLUT_BITMAP_9_BY_15, GLUT_BITMAP_TIMES_ROMAN_10,
// GLUT_BITMAP_TIMES_ROMAN_24, GLUT_BITMAP_HELVETICA_10,
// GLUT_BITMAP_HELVETICA_12, and GLUT_BITMAP_HELVETICA_18.
GLvoid *font_style = GLUT_BITMAP_TIMES_ROMAN_24;
// draws a string (Just as intelligent as printf)
// If you're new to functions with varaible arguments
// then check the <stdarg.h> in some C book or
// type (man stdarg) on Unix terminal.
void printw (float x, float y, float z, char* format, ...);
void init (void);
void display (void);
// This function centers your window on the screen
void centerOnScreen (void);
void init ()
{
glClearColor (0.0, 0.0, 0.0, 0.0);
}
void display ()
{
glClear (GL_COLOR_BUFFER_BIT);
printw (-0.9, 0.3, 0, "%s", "Learn To Struggle");
printw (-0.4, -0.3, 0, "%s", "Struggle To Learn");
glFlush ();
}
void centerOnScreen (void)
{
// Set the window position such that the window becomes centered
window_X_position = (glutGet (GLUT_SCREEN_WIDTH) - window_width)/2;
window_Y_position = (glutGet (GLUT_SCREEN_HEIGHT) - window_height)/2;
}
void printw (float x, float y, float z, char* format, ...)
{
va_list arg_list;
char str[256];
int i;
va_start(arg_list, format);
vsprintf(str, format, arg_list);
va_end(arg_list);
glRasterPos3f (x, y, z);
for (i = 0; str[i] != '\0'; i++)
glutBitmapCharacter(font_style, str[i]);
}
void main (int argc, char **argv)
{
centerOnScreen ();
glutInit(&argc, argv);
glutInitWindowSize (window_width, window_height);
glutInitWindowPosition (window_X_position, window_Y_position);
glutInitDisplayMode (GLUT_RGB);
glutCreateWindow (window_title);
// Initialize stuff that remain constant throughout the
// program.
init();
// Set the display function
glutDisplayFunc (display);
glutMainLoop();
}