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