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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
Copyright 1993-2010 NVIDIA Corporation.  All rights reserved.
NVIDIA Corporation and its licensors retain all intellectual property and
proprietary rights in and to this software and related documentation.
Any use, reproduction, disclosure, or distribution of this software
and related documentation without an express license agreement from
NVIDIA Corporation is strictly prohibited.

Please refer to the applicable NVIDIA end user license agreement (EULA)
associated with this source code for terms and conditions that govern
your use of this NVIDIA software.

#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( int width, int height, void *d = NULL ) {
      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*) = 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();
  }

   // static method used for glut callbacks

  static CPUBitmap** get_bitmap_ptr( void ) {
      static CPUBitmap   *gBitmap;
      return &gBitmap;
  }

 // 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);
      }
  }

  // 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__