~ubuntu-branches/ubuntu/precise/primrose/precise

« back to all changes in this revision

Viewing changes to minorGems/graphics/linux/SDLTest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Paul Wise
  • Date: 2009-04-06 19:26:56 UTC
  • Revision ID: james.westby@ubuntu.com-20090406192656-cri7503gebyvfl8t
Tags: upstream-5+dfsg1
ImportĀ upstreamĀ versionĀ 5+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
//#include <SDL/SDL.h>
 
3
 
 
4
#include "minorGems/graphics/ScreenGraphics.h"
 
5
#include "minorGems/graphics/GraphicBuffer.h"
 
6
#include "minorGems/graphics/Color.h"
 
7
 
 
8
#include "minorGems/ui/Mouse.h"
 
9
 
 
10
#include <stdio.h>
 
11
#include <stdlib.h>
 
12
#include <signal.h>
 
13
 
 
14
int numIcons = 200;
 
15
 
 
16
int currentStep = 0;
 
17
int numSteps = 1000;
 
18
 
 
19
void catch_int(int sig_num) {
 
20
        printf( "Quiting..." );
 
21
        currentStep = numSteps;
 
22
        signal( SIGINT, catch_int);
 
23
        }
 
24
 
 
25
int main() {
 
26
        int i, j;
 
27
        
 
28
        // let catch_int handle interrupt (^c)
 
29
    signal( SIGINT, catch_int);
 
30
        
 
31
        ScreenGraphics *graphics = new ScreenGraphics( 640, 480 );
 
32
        Mouse *mouse = new Mouse(2);
 
33
        
 
34
        
 
35
        unsigned long *pixelBuff = new unsigned long[ 640 * 480 ];
 
36
        
 
37
        GraphicBuffer *buffer = new GraphicBuffer( pixelBuff, 640, 480 );
 
38
        
 
39
        IconMap **maps = new IconMap*[numIcons];
 
40
        
 
41
        IconMap *mouseMap;
 
42
        
 
43
        IconMap *mouseMap1 = new IconMap( 10, 10 );
 
44
        Color mouseColor1( 1.0, 0.0, 0.0, 1.0 );
 
45
        for( int y=0; y<10; y++ ) {
 
46
                for( int x=0; x<10; x++ ) {     
 
47
                        mouseMap1->imageMap[ mouseMap1->yOffset[y] + x ] = 
 
48
                                mouseColor1.composite;
 
49
                        }
 
50
                }
 
51
        IconMap *mouseMap2 = new IconMap( 10, 10 );
 
52
        Color mouseColor2( 0.0, 1.0, 0.0, 1.0 );
 
53
        for( int y=0; y<10; y++ ) {
 
54
                for( int x=0; x<10; x++ ) {     
 
55
                        mouseMap2->imageMap[ mouseMap2->yOffset[y] + x ] = 
 
56
                                mouseColor2.composite;
 
57
                        }
 
58
                }
 
59
        
 
60
        IconMap *mouseMap3 = new IconMap( 10, 10 );
 
61
        Color mouseColor3( 0.0, 0.0, 1.0, 1.0 );
 
62
        for( int y=0; y<10; y++ ) {
 
63
                for( int x=0; x<10; x++ ) {     
 
64
                        mouseMap3->imageMap[ mouseMap3->yOffset[y] + x ] = 
 
65
                                mouseColor3.composite;
 
66
                        }
 
67
                }
 
68
                
 
69
        mouseMap = mouseMap1;   
 
70
                
 
71
        Color c( 0.0f, 0.0f, 0.0f, 1.0f );
 
72
        buffer->fill( c );
 
73
        
 
74
        float *xPos = new float[numIcons];
 
75
        float *yPos = new float[numIcons];
 
76
        float *xDelta = new float[numIcons];
 
77
        float *yDelta = new float[numIcons];
 
78
        
 
79
        for( i=0; i<numIcons; i++ ) {
 
80
                xPos[i] =  (float)640 * rand() / RAND_MAX;
 
81
                yPos[i] = (float)480 * rand() / RAND_MAX;
 
82
                xDelta[i] = 10.0f * rand() / RAND_MAX;
 
83
                yDelta[i] = 10.0f * rand() / RAND_MAX;
 
84
                maps[i] = new IconMap( 10, 10 );
 
85
                Color randColor( (float)rand() / RAND_MAX,
 
86
                        (float)rand() / RAND_MAX, (float)rand() / RAND_MAX, 1.0 );
 
87
                for( int y=0; y<10; y++ ) {
 
88
                        for( int x=0; x<10; x++ ) {     
 
89
                                maps[i]->imageMap[ maps[i]->yOffset[y] + x ] = 
 
90
                                        randColor.composite;
 
91
                                }
 
92
                        }
 
93
                }
 
94
        int mouseX = 0;
 
95
        int mouseY = 0; 
 
96
        char buttonDown1 = false;
 
97
        char buttonDown2 = false;
 
98
        for( currentStep=0; currentStep<numSteps; currentStep++ ) {
 
99
                buffer->eraseIconMap( mouseMap, mouseX, mouseY, c );
 
100
                
 
101
                mouse->getLocation( &mouseX, &mouseY );
 
102
                
 
103
                buffer->drawIconMap( mouseMap, mouseX, mouseY );
 
104
                
 
105
                if( !buttonDown1 && mouse->isButtonDown(0) ) {
 
106
                        buttonDown1 = true;
 
107
                        mouseMap = mouseMap2;
 
108
                        }
 
109
                else if( buttonDown1 && !mouse->isButtonDown(0) ) {
 
110
                        buttonDown1 = false;
 
111
                        mouseMap = mouseMap1;
 
112
                        }
 
113
                
 
114
                else if( !buttonDown2 && mouse->isButtonDown(1) ) {
 
115
                        buttonDown2 = true;
 
116
                        mouseMap = mouseMap3;
 
117
                        }
 
118
                else if( buttonDown2 && !mouse->isButtonDown(1) ) {
 
119
                        buttonDown2 = false;
 
120
                        mouseMap = mouseMap1;
 
121
                        }
 
122
                        
 
123
                
 
124
                
 
125
                for( i=0; i<numIcons; i++ ) {
 
126
                        buffer->eraseIconMap( maps[i], (int)( xPos[i] - 5 ), 
 
127
                                (int)( yPos[i] - 5 ), c );
 
128
                        if( xPos[i] > 640 || xPos[i] < 0 ) {
 
129
                                xDelta[i] = -xDelta[i];
 
130
                                }
 
131
                        xPos[i] += xDelta[i];
 
132
                        if( yPos[i] > 480 || yPos[i] < 0 ) {
 
133
                                yDelta[i] = -yDelta[i];
 
134
                                }
 
135
                        yPos[i] += yDelta[i];
 
136
 
 
137
                        buffer->drawIconMap( maps[i], (int)( xPos[i] - 5 ), 
 
138
                                (int)( yPos[i] - 5 ) );
 
139
                        }
 
140
                
 
141
                graphics->swapBuffers( buffer );
 
142
                }
 
143
                
 
144
        
 
145
        /*
 
146
        for( int i=0; i<100; i++ ) {
 
147
                if( i%2 == 0 ) {
 
148
                        Color c( 1.0f, 0.0f, 0.0f, 1.0f );
 
149
                        buffer->fill( c );
 
150
                        }
 
151
                else {
 
152
                        Color c( 0.0f, 1.0f, 0.0f, 1.0f );
 
153
                        buffer->fill( c );
 
154
                        }
 
155
                graphics->swapBuffers( buffer );
 
156
                }
 
157
        */
 
158
        printf( "Done.\n" );
 
159
        return 0;
 
160
        /*
 
161
        if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
 
162
                printf( "Couldn't initialize SDL: %s\n", SDL_GetError() );
 
163
                exit(1);
 
164
                }
 
165
        
 
166
        //atexit( SDL_Quit );
 
167
        
 
168
        SDL_Surface *screen = SDL_SetVideoMode( 640, 480, 
 
169
                32, SDL_HWSURFACE | SDL_DOUBLEBUF );//| SDL_FULLSCREEN );
 
170
                
 
171
        if ( screen == NULL ) {
 
172
                printf( "Couldn't set 640x480x32 video mode: %s\n",
 
173
                                       SDL_GetError() );
 
174
                exit(1);
 
175
                }
 
176
        
 
177
        for( int i=0; i< 100; i++ ) {
 
178
                if ( SDL_MUSTLOCK(screen) ) {
 
179
                        if ( SDL_LockSurface(screen) < 0 ) {
 
180
                                printf( "Couldn't lock screen: %s\n",
 
181
                                           SDL_GetError());
 
182
                                exit(1);
 
183
                                }
 
184
                        }
 
185
 
 
186
                Uint32 value;
 
187
                if( i%2 == 0 ) {
 
188
                        value = 0x0;
 
189
                        }
 
190
                else {
 
191
                        value = 0xFFFFFFFF;
 
192
                        }
 
193
 
 
194
                Uint32 *buffer = (Uint32 *)( screen->pixels );
 
195
                for ( int y=0; y<screen->h; y++ ) {
 
196
                        for ( int x=0; x<screen->w; x++ ) {
 
197
 
 
198
                                int r = ( ( ( x * 255 ) / screen->w ) + i ) % 255;
 
199
                                int g = ( ( ( y * 255 ) / screen->h ) + i ) % 255;
 
200
                                int b = 0;
 
201
                                int a = 255;
 
202
                                
 
203
                                //buffer[ y * screen->w + x ] = (a << 24) | (r << 16) | (g << 8) | b;
 
204
                                buffer[ y * screen->w + x ] = value;
 
205
                                }
 
206
                        }
 
207
 
 
208
                if ( SDL_MUSTLOCK(screen) ) {
 
209
                        SDL_UnlockSurface(screen);
 
210
                        }
 
211
 
 
212
                //SDL_UpdateRect( screen, 0, 0, screen->w, screen->h );
 
213
                SDL_Flip( screen );
 
214
                }
 
215
        SDL_Quit();
 
216
        
 
217
        SDL_Delay(2000);
 
218
 
 
219
        exit(0);
 
220
        */
 
221
        }