File: ScreenGraphicsLinux.cpp

package info (click to toggle)
primrose 6%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 5,232 kB
  • ctags: 3,287
  • sloc: cpp: 27,302; php: 765; ansic: 636; objc: 272; makefile: 139; sh: 136; perl: 67
file content (153 lines) | stat: -rw-r--r-- 3,459 bytes parent folder | download | duplicates (24)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Modification History
 *
 * 2000-November-18		Jason Rohrer
 * Created.
 *
 * 2000-November-19		Jason Rohrer
 * Change so that it doesn't use SDL's interrupt parachute, i.e.,
 *   so it will quit on ^c.
 *
 * 2001-May-1   Jason Rohrer
 * Changed to use more standard SDL include location.
 *
 * 2006-April-28		Jason Rohrer
 * Added functions for more direct access to screen pixels.
 *
 * 2006-June-26		Jason Rohrer
 * Added support for dirty rectangle.
 * Added resize support.
 */


#include "minorGems/graphics/ScreenGraphics.h"

#include <SDL/SDL.h>

/**
 * Note:  Linux implementation uses windowed mode.
 */

ScreenGraphics::ScreenGraphics( int inWidth, int inHeight )
	: mWidth( inWidth ), mHeight( inHeight ) {
	
	if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 ) {
		printf( "Couldn't initialize SDL: %s\n", SDL_GetError() );
		return;
		}
	
	SDL_Surface *screen = SDL_SetVideoMode( mWidth, mHeight, 
		32, SDL_SWSURFACE );
	
	if ( screen == NULL ) {
		printf( "Couldn't set %dx%dx32 video mode: %s\n", mWidth, mHeight,
			SDL_GetError() );
		return;
		}
		
	
	mNativeObjectPointer = (void*)screen;
	}



ScreenGraphics::~ScreenGraphics() {
	SDL_Quit();
	}



void ScreenGraphics::resize( int inNewWidth, int inNewHeight ) {

    mWidth = inNewWidth;
    mHeight = inNewHeight;
    
    
    SDL_Surface *screen = SDL_SetVideoMode( mWidth, mHeight,
                                            32, SDL_SWSURFACE );
	
	if ( screen == NULL ) {
		printf( "Couldn't set %dx%dx32 video mode: %s\n", mWidth, mHeight,
                SDL_GetError() );
		return;
		}
			
	mNativeObjectPointer = (void*)screen;
    }



char ScreenGraphics::isResolutionAvailable( int inWidth, int inHeight ) {
	if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 ) {
		printf( "Couldn't initialize SDL: %s\n", SDL_GetError() );
		return false;
		}
		
	// ask SDL if this screen size is supported
	char videoModeOk = 
		(char)SDL_VideoModeOK( inWidth, inHeight, 32, SDL_SWSURFACE );
	
	SDL_Quit();
	
	
	return videoModeOk;
	}	



void ScreenGraphics::swapBuffers( GraphicBuffer *inOutBuffer ) {
	if( inOutBuffer->getHeight() != mHeight ||
		inOutBuffer->getWidth() != mWidth ) {
		printf( "Buffer of incorrect size passed to screen.\n" );
		return;
		}
	
	SDL_Surface *screen = (SDL_Surface*)mNativeObjectPointer;
	
	// check if we need to lock the screen
	if( SDL_MUSTLOCK( screen ) ) {
		if( SDL_LockSurface( screen ) < 0 ) {
			printf( "Couldn't lock screen: %s\n", SDL_GetError() );
			return;
			}
		}
		
	Uint32 *buffer = (Uint32 *)( screen->pixels );
	memcpy( (void*)buffer, (void*)( inOutBuffer->getBuffer() ), 
		mWidth * mHeight * 4 );

	// unlock the screen if necessary
	if ( SDL_MUSTLOCK(screen) ) {
		SDL_UnlockSurface(screen);
		}
	
	SDL_Flip( screen );	
	}



GraphicBuffer *ScreenGraphics::getScreenBuffer() {
    SDL_Surface *screen = (SDL_Surface*)mNativeObjectPointer;
    
    Uint32 *buffer = (Uint32 *)( screen->pixels );

    return new GraphicBuffer( (unsigned long *)buffer, mWidth, mHeight ); 
    }



void ScreenGraphics::flipDirtyRectangle( int inTopLeftX, int inTopLeftY,
                                         int inWidth, int inHeight ) {

    SDL_Surface *screen = (SDL_Surface*)mNativeObjectPointer;
    
    SDL_UpdateRect( screen, inTopLeftX, inTopLeftY, inWidth, inHeight );    
    }



void ScreenGraphics::flipScreen() {
    SDL_Surface *screen = (SDL_Surface*)mNativeObjectPointer;

    SDL_Flip( screen );
    }