(七)OpenGL显示字母与汉字

https://blog.csdn.net/qq_35040828/article/details/51758258
具体解释直接看这个链接吧

#include<windows.h>
#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

void drawstring(const char* str){
    static int isfirstcall = 1;
    static GLuint lists;
    if(isfirstcall){
        isfirstcall = 0;
        lists = glGenLists(128);
        wglUseFontBitmaps(wglGetCurrentDC(),0,128,lists);
    }
    for( ; *str !='\0';++str){
        glCallList(lists + *str);
    }
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0,1,0);
    glRasterPos2f(0.5,0.5);
    drawstring("hello, world");
    glutSwapBuffers();
}

int main(int argc,char * argv[]){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowSize(512,512);
    glutCreateWindow("word");
    glutDisplayFunc(display);

    glutMainLoop();
    return 0;

}

运行结果
在这里插入图片描述

写中文字符(原链接找不到了,只能先这么发了,如果有谁找到的话麻烦评论一下我改掉)

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>

void selectFont(int size, int charset, const 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 drawCNString(const char* str){
    int len, i;
    wchar_t* wstring;
    HDC hDC = wglGetCurrentDC();
    GLuint lista = glGenLists(1);

    // 计算字符的个数
    // 如果是双字节字符的(比如中文字符),两个字节才算一个字符
    // 否则一个字节算一个字符
    len = 0;
    for (i = 0; str[i] != '\0'; ++i)
    {
        if (IsDBCSLeadByte(str[i]))
            ++i;
            ++len;
        }
    // 将混合字符转化为宽字符
    wstring = (wchar_t*)malloc((len + 1) * sizeof(wchar_t));
    MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, -1, wstring, len);
    wstring[len] = L'\0';
    // 逐个输出字符
    for (i = 0; i<len; ++i)
    {
        wglUseFontBitmapsW(hDC, wstring[i], 1, lista);
        glCallList(lista);
    }
    // 回收所有临时资源
    free(wstring);
    glDeleteLists(lista, 1);
}

void display(){
    glColor3f(0.1,1,0.1);
    glRectf(-0.3,0.1,0.3,-0.15);
    glRectf(-0.3,-0.3,0.3,-0.55);

    glColor3f(1,1,1);
    selectFont(40,DEFAULT_CHARSET,"华文仿宋");
    glRasterPos2f(-0.3,-0.03);
    drawCNString("开始游戏");
    glRasterPos2f(-0.1,-0.45);
    drawCNString("exit");


    glutSwapBuffers();
}

int main(int argc, char *argv[]){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowSize(500,700);
    glutCreateWindow("plane");
    glutDisplayFunc(display);

    glutMainLoop();
    return 0;
}

运行结果:
在这里插入图片描述

  • 2
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值