cpu_bitmap结构体解析
2016-04-03 22:18
113 查看
#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__
#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__
相关文章推荐
- Unity3d-Shader-UV移动
- 配置tomcat的compressableMimeType指定gzip的类型
- gdb调试带参数程序
- 【jdk1.8】PriorityQueue源码分析
- 蛇形数组实现
- Activiti 删除流程定义
- CornerStone
- no jlink device found
- AngularJs Controllers
- JavaScript的function和function()的区别
- nginx 414 Request-URI Too Large
- TCP客户/服务器程序概述
- 一道算法题
- 后端面试问题汇总——目录索引
- 【BZOJ1208】【HNOI2004】宠物收养所
- Python multiprocessing
- 4、Mysql主键索引创建的实例
- 模板方法模式
- iOS边练边学--(Quartz2D)基本图形的绘制#附加自定义进度控件的练习
- Impala之创建外部表