Created
January 8, 2015 04:03
-
-
Save jameskerr/853d2af7a9e726abaa12 to your computer and use it in GitHub Desktop.
Font Assignment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <GLUT/glut.h> | |
#include <math.h> | |
/************************* | |
::: HELPER VARIABLES ::: * | |
*************************/ | |
enum FONT_TYPE { | |
BITMAP, | |
STROKE | |
}; | |
/************************************ | |
::: HELPER FUNCTIONS DECLARATIONS ::: | |
************************************/ | |
void initWindow(); | |
void initMenu(); | |
void menu(int); | |
void display(); | |
void printBitmap(GLfloat, GLfloat, char*); | |
void printStroke(GLfloat, GLfloat, char*); | |
void renderBitmapFont(); | |
void renderStrokeFont(); | |
/***************************** | |
::: MAIN PROGRAM EXECUTION ::: | |
******************************/ | |
int main(int argc, char** argv) { | |
glutInit(&argc, argv); | |
initWindow(); | |
initMenu(); | |
glutDisplayFunc(display); | |
glutMainLoop(); | |
return 0; | |
} | |
/*************************************** | |
::: HELPER FUNCTIONS IMPLEMENTATIONS ::: | |
***************************************/ | |
void initWindow() { | |
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); | |
glutInitWindowSize(800, 400); | |
glutInitWindowPosition(0, 0); | |
glutCreateWindow("Primitives"); | |
} | |
void initMenu() { | |
glutCreateMenu(menu); | |
glutAttachMenu(GLUT_RIGHT_BUTTON); | |
glutAddMenuEntry("Bitmap Font", BITMAP); | |
glutAddMenuEntry("Stroke Font", STROKE); | |
} | |
void menu(int item) { | |
glClear(GL_COLOR_BUFFER_BIT); | |
switch (item) { | |
case BITMAP: | |
renderBitmapFont(); | |
break; | |
case STROKE: | |
renderStrokeFont(); | |
break; | |
default: | |
break; | |
} | |
} | |
void display() { | |
glClear(GL_COLOR_BUFFER_BIT); | |
glMatrixMode(GL_PROJECTION); | |
gluOrtho2D(-10.0, 10.0, -10.0, 10.0); | |
glMatrixMode(GL_MODELVIEW); | |
glColor3ub(255, 0, 0); | |
glutSwapBuffers(); | |
} | |
void printStroke(GLfloat x, GLfloat y, char* string) { | |
glPushMatrix(); | |
glTranslatef(x, y, 0); | |
glScalef(1/252.38, 1/152.38, 1/252.38); | |
for (char* p = string; *p; p++) { | |
glutStrokeCharacter(GLUT_STROKE_ROMAN, *p); | |
} | |
glPopMatrix(); | |
} | |
void printBitmap(GLfloat x, GLfloat y, char* string) { | |
glPushMatrix(); | |
glRasterPos2s(x, y); | |
glScalef(1/152.38, 1/152.38, 1/152.38); | |
for (char* p = string; *p; p++) { | |
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *p); | |
} | |
glPopMatrix(); | |
} | |
void renderBitmapFont() { | |
printBitmap(-8.0, 2.0, "James the (not-anymore) Perm"); | |
printBitmap(-8.0, 0.0, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); | |
printBitmap(-8.0, -2.0, "abcdefghijklmnopqrstuvwxyz"); | |
glutSwapBuffers(); | |
} | |
void renderStrokeFont() { | |
printStroke(-8.0, 2.0, "James the (not-anymore) Perm"); | |
printStroke(-8.0, 0.0, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); | |
printStroke(-8.0, -2.0, "abcdefghijklmnopqrstuvwxyz"); | |
glutSwapBuffers(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment