// bitmap font draw sample on OpenGL // // Last updated: <2024/01/13 09:32:22 +0900> #include #include #include #include #include // include bitmap pattern // -------------------- // #include "fontdata.h" // #include "fontdata_courr18.h" // #include "fontdata_shnm8x16r.h" // #include "fontdata_shnm8x16rx2.h" // #include "fontdata_profont.h" #include "fontdata_ter-u24b.h" #define SCRW 512 #define SCRH 288 static int framerate = 60; static int count = 0; // draw text void drawText(char *str) { GLsizei w = (GLsizei)fontdata_width; GLsizei h = (GLsizei)fontdata_height; int clen = fontdata_chr_len; GLfloat xorig, yorig; GLfloat xmove, ymove; xorig = 0; yorig = 0; xmove = w; ymove = 0; glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // コレが無いと表示が崩れる int slen = strlen(str); for (int i = 0; i < slen; i++) { int c = str[i]; if (c == 0) break; if (c < 0x20 || c > 0x7f) c = 0x20; c -= 0x20; glBitmap(w, h, xorig, yorig, xmove, ymove, &(fontdata[c][0])); } } // draw OpenGL void display(void) { GLfloat xorig, yorig, xmove, ymove; GLsizei w, h; glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); // draw text glColor4f(1, 1, 1, 1); // set color glRasterPos2f(-0.8, 0.5); // set position drawText("Hello World !!"); { char buf[256]; sprintf(buf, "count %d", count); glRasterPos2f(-0.8, 0.0); drawText(buf); } glutSwapBuffers(); glFlush(); count++; } void onTimer(int value) { glutPostRedisplay(); // redraw glutTimerFunc((1000 / framerate), onTimer, 0); } void keyboard(unsigned char key, int x, int y) { switch (key) { case '\x1B': case 'q': // Exit on escape or 'q' key press glutLeaveMainLoop(); // exit(EXIT_SUCCESS); break; } } int main(int argc, char **argv) { count = 0; glutInit(&argc, argv); glutInitWindowSize(SCRW, SCRH); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutCreateWindow("bitmap font draw sample"); glutKeyboardFunc(keyboard); glutDisplayFunc(display); glutTimerFunc((1000 / framerate), onTimer, 0); glutMainLoop(); return EXIT_SUCCESS; }