File: SDLTest.cpp

package info (click to toggle)
primrose 6%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,304 kB
  • sloc: cpp: 27,318; php: 765; ansic: 636; objc: 272; sh: 136; makefile: 92; perl: 67
file content (221 lines) | stat: -rw-r--r-- 5,080 bytes parent folder | download | duplicates (30)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

//#include <SDL/SDL.h>

#include "minorGems/graphics/ScreenGraphics.h"
#include "minorGems/graphics/GraphicBuffer.h"
#include "minorGems/graphics/Color.h"

#include "minorGems/ui/Mouse.h"

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

int numIcons = 200;

int currentStep = 0;
int numSteps = 1000;

void catch_int(int sig_num) {
	printf( "Quiting..." );
	currentStep = numSteps;
	signal( SIGINT, catch_int);
	}

int main() {
	int i, j;
	
	// let catch_int handle interrupt (^c)
    signal( SIGINT, catch_int);
	
	ScreenGraphics *graphics = new ScreenGraphics( 640, 480 );
	Mouse *mouse = new Mouse(2);
	
	
	unsigned long *pixelBuff = new unsigned long[ 640 * 480 ];
	
	GraphicBuffer *buffer = new GraphicBuffer( pixelBuff, 640, 480 );
	
	IconMap **maps = new IconMap*[numIcons];
	
	IconMap *mouseMap;
	
	IconMap *mouseMap1 = new IconMap( 10, 10 );
	Color mouseColor1( 1.0, 0.0, 0.0, 1.0 );
	for( int y=0; y<10; y++ ) {
		for( int x=0; x<10; x++ ) {	
			mouseMap1->imageMap[ mouseMap1->yOffset[y] + x ] = 
				mouseColor1.composite;
			}
		}
	IconMap *mouseMap2 = new IconMap( 10, 10 );
	Color mouseColor2( 0.0, 1.0, 0.0, 1.0 );
	for( int y=0; y<10; y++ ) {
		for( int x=0; x<10; x++ ) {	
			mouseMap2->imageMap[ mouseMap2->yOffset[y] + x ] = 
				mouseColor2.composite;
			}
		}
	
	IconMap *mouseMap3 = new IconMap( 10, 10 );
	Color mouseColor3( 0.0, 0.0, 1.0, 1.0 );
	for( int y=0; y<10; y++ ) {
		for( int x=0; x<10; x++ ) {	
			mouseMap3->imageMap[ mouseMap3->yOffset[y] + x ] = 
				mouseColor3.composite;
			}
		}
		
	mouseMap = mouseMap1;	
		
	Color c( 0.0f, 0.0f, 0.0f, 1.0f );
	buffer->fill( c );
	
	float *xPos = new float[numIcons];
	float *yPos = new float[numIcons];
	float *xDelta = new float[numIcons];
	float *yDelta = new float[numIcons];
	
	for( i=0; i<numIcons; i++ ) {
		xPos[i] =  (float)640 * rand() / RAND_MAX;
		yPos[i] = (float)480 * rand() / RAND_MAX;
		xDelta[i] = 10.0f * rand() / RAND_MAX;
		yDelta[i] = 10.0f * rand() / RAND_MAX;
		maps[i] = new IconMap( 10, 10 );
		Color randColor( (float)rand() / RAND_MAX,
			(float)rand() / RAND_MAX, (float)rand() / RAND_MAX, 1.0 );
		for( int y=0; y<10; y++ ) {
			for( int x=0; x<10; x++ ) {	
				maps[i]->imageMap[ maps[i]->yOffset[y] + x ] = 
					randColor.composite;
				}
			}
		}
	int mouseX = 0;
	int mouseY = 0;	
	char buttonDown1 = false;
	char buttonDown2 = false;
	for( currentStep=0; currentStep<numSteps; currentStep++ ) {
		buffer->eraseIconMap( mouseMap, mouseX, mouseY, c );
		
		mouse->getLocation( &mouseX, &mouseY );
		
		buffer->drawIconMap( mouseMap, mouseX, mouseY );
		
		if( !buttonDown1 && mouse->isButtonDown(0) ) {
			buttonDown1 = true;
			mouseMap = mouseMap2;
			}
		else if( buttonDown1 && !mouse->isButtonDown(0) ) {
			buttonDown1 = false;
			mouseMap = mouseMap1;
			}
		
		else if( !buttonDown2 && mouse->isButtonDown(1) ) {
			buttonDown2 = true;
			mouseMap = mouseMap3;
			}
		else if( buttonDown2 && !mouse->isButtonDown(1) ) {
			buttonDown2 = false;
			mouseMap = mouseMap1;
			}
			
		
		
		for( i=0; i<numIcons; i++ ) {
			buffer->eraseIconMap( maps[i], (int)( xPos[i] - 5 ), 
				(int)( yPos[i] - 5 ), c );
			if( xPos[i] > 640 || xPos[i] < 0 ) {
				xDelta[i] = -xDelta[i];
				}
			xPos[i] += xDelta[i];
			if( yPos[i] > 480 || yPos[i] < 0 ) {
				yDelta[i] = -yDelta[i];
				}
			yPos[i] += yDelta[i];

			buffer->drawIconMap( maps[i], (int)( xPos[i] - 5 ), 
				(int)( yPos[i] - 5 ) );
			}
		
		graphics->swapBuffers( buffer );
		}
		
	
	/*
	for( int i=0; i<100; i++ ) {
		if( i%2 == 0 ) {
			Color c( 1.0f, 0.0f, 0.0f, 1.0f );
			buffer->fill( c );
			}
		else {
			Color c( 0.0f, 1.0f, 0.0f, 1.0f );
			buffer->fill( c );
			}
		graphics->swapBuffers( buffer );
		}
	*/
	printf( "Done.\n" );
	return 0;
	/*
	if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
		printf( "Couldn't initialize SDL: %s\n", SDL_GetError() );
		exit(1);
		}
	
	//atexit( SDL_Quit );
	
	SDL_Surface *screen = SDL_SetVideoMode( 640, 480, 
		32, SDL_HWSURFACE | SDL_DOUBLEBUF );//| SDL_FULLSCREEN );
		
	if ( screen == NULL ) {
		printf( "Couldn't set 640x480x32 video mode: %s\n",
                                       SDL_GetError() );
		exit(1);
		}
	
	for( int i=0; i< 100; i++ ) {
		if ( SDL_MUSTLOCK(screen) ) {
			if ( SDL_LockSurface(screen) < 0 ) {
				printf( "Couldn't lock screen: %s\n",
                                    	   SDL_GetError());
				exit(1);
				}
			}

		Uint32 value;
		if( i%2 == 0 ) {
			value = 0x0;
			}
		else {
			value = 0xFFFFFFFF;
			}

		Uint32 *buffer = (Uint32 *)( screen->pixels );
		for ( int y=0; y<screen->h; y++ ) {
			for ( int x=0; x<screen->w; x++ ) {

				int r = ( ( ( x * 255 ) / screen->w ) + i ) % 255;
				int g = ( ( ( y * 255 ) / screen->h ) + i ) % 255;
				int b = 0;
				int a = 255;
				
				//buffer[ y * screen->w + x ] = (a << 24) | (r << 16) | (g << 8) | b;
				buffer[ y * screen->w + x ] = value;
				}
			}

		if ( SDL_MUSTLOCK(screen) ) {
			SDL_UnlockSurface(screen);
			}

		//SDL_UpdateRect( screen, 0, 0, screen->w, screen->h );
		SDL_Flip( screen );
		}
	SDL_Quit();
	
	SDL_Delay(2000);

	exit(0);
	*/
	}