大家新年好!
刚学OpenGL,过年了,拼凑个小玩意向大家拜年!祝大家新春快乐,吉祥如意!程序代码:
#include "windows.h" #include "GL/glut.h" #include "stdlib.h" #include "stdio.h" void myDisplay (void); void ChangeSize (int w, int h); void SelectFont (int size, int charset,char* face); void myString (char* str); void myPlay (void); int WINAPI WinMain (HINSTANCE h1, HINSTANCE h2, LPTSTR cmdline, int cmdshow){ /* 用 "main" 用不了 SelectFont ()中的3个函数 */ int argc = 1; char* argv[] = {"OpenGL"}; /* ? char** 报错 */ glutInit (&argc, argv); /* 初始化 */ glutInitDisplayMode (GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition (0, 0); glutInitWindowSize (800, 600); glutCreateWindow ("2013"); glutDisplayFunc (&myDisplay); glutMainLoop (); } void myDisplay (void){ glClearColor (0, 0, 0, 1); glClear (GL_COLOR_BUFFER_BIT); myPlay (); glClear (GL_COLOR_BUFFER_BIT); glColor3f (1, 0, 0); SelectFont(72, ANSI_CHARSET, "Comic Sans MS"); glRasterPos2f (-0.3, 0); myString ("Happy New Year"); glFlush (); } void ChangeSize (int w, int h){ float aspectRatio = 0; if (h == 0){ h = 1; } glViewport (0, 0, w, h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); aspectRatio = (float)w / (float)h; if (w <= h) { glOrtho (-100, 100, -100 / aspectRatio, 100 / aspectRatio, 1, -1); } else glOrtho (-100 * aspectRatio, 100 * aspectRatio, -100, 100, 1, -1); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); } void SelectFont (int size, int charset,char* face){ HFONT hFont = CreateFontA (size, 0, 0, 0, FW_MEDIUM, 0, 0, 0, charset, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, face); HFONT hOldFont = (HFONT)SelectObject (wglGetCurrentDC (), hFont); DeleteObject (hOldFont); } void myString (char* str){ static int isFirstCall = 1; static GLuint lists = 0; if (isFirstCall){ isFirstCall = 0; lists = glGenLists (128); wglUseFontBitmaps (wglGetCurrentDC (), 0, 128, lists); } for (; *str != '\0'; str++){ glCallList (lists + *str); } } void myPlay (void){ glColor3f (1, 1, 1); glRasterPos2f (-0.3, 0); SelectFont(48, ANSI_CHARSET, "Comic Sans MS"); myString ("Wait"); for (int i = 1; i <= 5; i++){ myString ("."); glFlush (); Sleep (500); } }
虽然有些地方还不太明白,不过我相信,
PS:Win API 的函数名真是不漂亮...
参考:http://
https://bbs.bccn.net/thread-67741-1-1.html
http://blog.
[ 本帖最后由 pycansi 于 2013-2-10 00:48 编辑 ]