]> git.mxchange.org Git - simgear.git/blob - simgear/screen/GLBitmaps.cxx
Alexander Powell:
[simgear.git] / simgear / screen / GLBitmaps.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include <simgear_config.h>
3 #endif
4
5 #if defined(__CYGWIN__)  /* && !defined(USING_X) */
6 #define WIN32
7 #endif
8
9 #if defined(WIN32)  /* MINGW and MSC predefine WIN32 */
10 # include <windows.h>
11 #endif
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <limits.h>
16 #include <string.h>             // memcpy()
17
18 #include <simgear/compiler.h>
19
20 #include SG_GL_H
21
22 #include "GLBitmaps.h"
23
24 GlBitmap::GlBitmap( GLenum mode, GLint width, GLint height, GLubyte *bitmap )
25 : m_bytesPerPixel(mode==GL_RGB?3:4), m_width(width), m_height(height), m_bitmap(NULL)
26 {
27         m_bitmapSize = m_bytesPerPixel*m_width*m_height;
28         if ( !m_bitmapSize )
29         {
30                 GLint vp[4];
31                 glGetIntegerv( GL_VIEWPORT, vp );
32                 m_width = vp[2];
33                 m_height = vp[3];
34                 m_bitmapSize = m_bytesPerPixel*m_width*m_height;
35         }
36         m_bitmap = (GLubyte *)malloc( m_bitmapSize );
37         if ( bitmap ) memcpy( m_bitmap, bitmap, m_bitmapSize );
38         else glReadPixels( 0,0, m_width,m_height, mode, GL_UNSIGNED_BYTE, m_bitmap );
39 }
40
41 GlBitmap::~GlBitmap( )
42 {
43         if ( m_bitmap ) free( m_bitmap );
44 }
45
46 GLubyte *GlBitmap::getBitmap()
47 {
48         return m_bitmap;
49 }
50
51 void GlBitmap::copyBitmap( GlBitmap *from, GLint at_x, GLint at_y )
52 {
53         GLint newWidth = at_x + from->m_width;
54         GLint newHeight = at_y + from->m_height;
55         if ( newWidth < m_width ) newWidth = m_width;
56         if ( newHeight < m_height ) newHeight = m_height;
57         m_bitmapSize = m_bytesPerPixel*newWidth*newHeight;
58         GLubyte *newBitmap = (GLubyte *)malloc( m_bitmapSize );
59         GLint x,y;
60         for ( y=0; y<m_height; y++ )
61         {
62                 GLubyte *s = m_bitmap + m_bytesPerPixel * (y * m_width);
63                 GLubyte *d = newBitmap + m_bytesPerPixel * (y * newWidth);
64                 memcpy( d, s, m_bytesPerPixel * m_width );
65         }
66         m_width = newWidth;
67         m_height = newHeight;
68         free( m_bitmap );
69         m_bitmap = newBitmap;
70         for ( y=0; y<from->m_height; y++ )
71         {
72                 GLubyte *s = from->m_bitmap + from->m_bytesPerPixel * (y * from->m_width);
73                 GLubyte *d = m_bitmap + m_bytesPerPixel * ((at_y+y) * m_width + at_x);
74                 for ( x=0; x<from->m_width; x++ )
75                 {
76                         d[0] = s[0];
77                         d[1] = s[1];
78                         d[2] = s[2];
79                         if ( m_bytesPerPixel == 4 )
80                         {
81                                 d[3] = (from->m_bytesPerPixel == 4) ? s[3] : 0;
82                         }
83                         s += from->m_bytesPerPixel;
84                         d += m_bytesPerPixel;
85                 }
86         }
87 }
88