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