File: ScreenGraphicsLinux.cpp

package info (click to toggle)
transcend 0.3.dfsg2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,784 kB
  • sloc: cpp: 26,886; ansic: 693; sh: 210; makefile: 99; perl: 67
file content (97 lines) | stat: -rw-r--r-- 2,135 bytes parent folder | download | duplicates (6)
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
/*
 * 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.
 */
 
#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();
	}



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 );	
	}