-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpu_bitmap.h
61 lines (58 loc) · 1.42 KB
/
cpu_bitmap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef __CPU_BITMAP_H__
#define __CPU_BITMAP_H__
#include <gl/freeglut.h>
class CPUBitmap {
private:
unsigned char* pixels;
int x, y;
void* dataBlock;
void (*bitmapExit)(void*);
static CPUBitmap** get_bitmap_ptr(void) {
static CPUBitmap* gBitmap;
return &gBitmap;
}
static void Key(unsigned char key, int x, int y) {
switch (key) {
case 27:
CPUBitmap * bitmap = *(get_bitmap_ptr());
if (bitmap->dataBlock != nullptr && bitmap->bitmapExit != nullptr) {
bitmap->bitmapExit(bitmap->dataBlock);
}
exit(0);
}
}
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();
}
public:
CPUBitmap(int width, int height, void* d = nullptr) {
pixels = new unsigned char[width * height * 4];
x = width;
y = height;
dataBlock = d;
}
~CPUBitmap() {
delete[] pixels;
}
unsigned char* get_ptr(void) const { return pixels; }
long image_size(void) const { return x * y * 4; }
void display_and_exit(void(*e)(void*) = nullptr) {
CPUBitmap** bitmap = get_bitmap_ptr();
*bitmap = this;
bitmapExit = e;
int c = 1;
char* dummy = "";
glutInit(&c, &dummy);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(x, y);
glutCreateWindow("bitmap");
glutKeyboardFunc(Key);
glutDisplayFunc(Draw);
glutMainLoop();
}
};
#endif