cpu_bitmap结构体解析

来源:互联网 发布:万学海文考研 知乎 编辑:程序博客网 时间:2024/06/05 16:03

#ifndef __CPU_BITMAP_H__
#define __CPU_BITMAP_H__


#include "gl_helper.h"


struct CPUBitmap {
    unsigned char    *pixels;
    int     x, y;
    void    *dataBlock;
    void (*bitmapExit)(void*);


//CPUBitmap结构体的构造函数
    CPUBitmap( int width, int height, void *d = NULL ) {
        pixels = new unsigned char[width * height * 4];//像素点申请
        x = width;//图像宽度
        y = height;//图像长度
        dataBlock = d;//数组块声明(cpu_aini中使用)
    }
    //CPUBitmap结构体的析构函数
    ~CPUBitmap() {
        delete [] pixels;
    }
//获取其像素所在的指针
    unsigned char* get_ptr( void ) const   { return pixels; }
//获取其像素的个数
long image_size( void ) const { return x * y * 4; }
//显示并退出
//该函数的参数为函数指针。函数指针声明: void(*e)(void*),默认为NULL
    void display_and_exit( void(*e)(void*) = NULL ) {
        CPUBitmap**   bitmap = get_bitmap_ptr();
        *bitmap = this;
        bitmapExit = e;
        // a bug in the Windows GLUT implementation prevents us from
        // passing zero arguments to glutInit()
        int c=1;
        char* dummy = "";
        glutInit( &c, &dummy );
        glutInitDisplayMode( GLUT_SINGLE | GLUT_RGBA );
        glutInitWindowSize( x, y );
        glutCreateWindow( "bitmap" );
        glutKeyboardFunc(Key);
        glutDisplayFunc(Draw);
        glutMainLoop();
    }
//静态函数,用于glut的回调,其返回g指向Bitmap指针的指针
     // static method used for glut callbacks
    static CPUBitmap** get_bitmap_ptr( void ) {
        static CPUBitmap   *gBitmap;
        return &gBitmap;
    }
//静态函数,用于glut的回调,其返回g指向Bitmap指针的指针
   // static method used for glut callbacks
    static void Key(unsigned char key, int x, int y) {
        switch (key) {
            case 27:
                CPUBitmap*   bitmap = *(get_bitmap_ptr());
                if (bitmap->dataBlock != NULL && bitmap->bitmapExit != NULL)
                    bitmap->bitmapExit( bitmap->dataBlock );
                exit(0);
        }
    }
//静态函数,用于glut的回调,其返回g指向Bitmap指针的指针
    // static method used for glut callbacks
    static void Draw( void ) {
        CPUBitmap*   bitmap = *(get_bitmap_ptr());
        glClearColor( 0.0, 0.0, 0.0, 1.0 );
        glClear( GL_COLOR_BUFFER_BIT );
        glDrawPixels( bitmap->x, bitmap->y, GL_RGBA, GL_UNSIGNED_BYTE, bitmap->pixels );
        glFlush();
    }
};


#endif  // __CPU_BITMAP_H__
0 0
原创粉丝点击