]> git.mxchange.org Git - simgear.git/blob - simgear/screen/GLBitmaps.cxx
Attempt to fix the Cygwin build problem for once and for all
[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 <GL/gl.h>
19
20 #include "GLBitmaps.h"
21
22 GlBitmap::GlBitmap( GLenum mode, GLint width, GLint height, GLubyte *bitmap )
23 : m_bytesPerPixel(mode==GL_RGB?3:4), m_width(width), m_height(height), m_bitmap(NULL)
24 {
25         m_bitmapSize = m_bytesPerPixel*m_width*m_height;
26         if ( !m_bitmapSize )
27         {
28                 GLint vp[4];
29                 glGetIntegerv( GL_VIEWPORT, vp );
30                 m_width = vp[2];
31                 m_height = vp[3];
32                 m_bitmapSize = m_bytesPerPixel*m_width*m_height;
33         }
34         m_bitmap = (GLubyte *)malloc( m_bitmapSize );
35         if ( bitmap ) memcpy( m_bitmap, bitmap, m_bitmapSize );
36         else glReadPixels( 0,0, m_width,m_height, mode, GL_UNSIGNED_BYTE, m_bitmap );
37 }
38
39 GlBitmap::~GlBitmap( )
40 {
41         if ( m_bitmap ) free( m_bitmap );
42 }
43
44 GLubyte *GlBitmap::getBitmap()
45 {
46         return m_bitmap;
47 }
48
49 void GlBitmap::copyBitmap( GlBitmap *from, GLint at_x, GLint at_y )
50 {
51         GLint newWidth = at_x + from->m_width;
52         GLint newHeight = at_y + from->m_height;
53         if ( newWidth < m_width ) newWidth = m_width;
54         if ( newHeight < m_height ) newHeight = m_height;
55         m_bitmapSize = m_bytesPerPixel*newWidth*newHeight;
56         GLubyte *newBitmap = (GLubyte *)malloc( m_bitmapSize );
57         GLint x,y;
58         for ( y=0; y<m_height; y++ )
59         {
60                 GLubyte *s = m_bitmap + m_bytesPerPixel * (y * m_width);
61                 GLubyte *d = newBitmap + m_bytesPerPixel * (y * newWidth);
62                 memcpy( d, s, m_bytesPerPixel * m_width );
63         }
64         m_width = newWidth;
65         m_height = newHeight;
66         free( m_bitmap );
67         m_bitmap = newBitmap;
68         for ( y=0; y<from->m_height; y++ )
69         {
70                 GLubyte *s = from->m_bitmap + from->m_bytesPerPixel * (y * from->m_width);
71                 GLubyte *d = m_bitmap + m_bytesPerPixel * ((at_y+y) * m_width + at_x);
72                 for ( x=0; x<from->m_width; x++ )
73                 {
74                         d[0] = s[0];
75                         d[1] = s[1];
76                         d[2] = s[2];
77                         if ( m_bytesPerPixel == 4 )
78                         {
79                                 d[3] = (from->m_bytesPerPixel == 4) ? s[3] : 0;
80                         }
81                         s += from->m_bytesPerPixel;
82                         d += m_bytesPerPixel;
83                 }
84         }
85 }
86