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
|
#include "SDL/SDL.h"
#include <time.h>
//setup the putpixel function
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
}
int main( int argc, char* args[] )
{ //Start SDL
SDL_Init( SDL_INIT_EVERYTHING );
//The attributes of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
const int NOOFFLAKES = 100;
int x[NOOFFLAKES];
int y[NOOFFLAKES];
int xo[NOOFFLAKES];
int yo[NOOFFLAKES];
//The surface that will be used
SDL_Surface *screen = NULL;
//Set up the screen
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_DOUBLEBUF|SDL_FULLSCREEN);
SDL_ShowCursor(SDL_DISABLE);
SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
SDL_WM_SetCaption( "Snow Test 1", NULL );
//If there was an error in setting up the screen
if( screen == NULL ) { return 1; }
Uint32 white;
Uint32 black;
//Map the color white to this display (R=0xff, G=0xff, B=0xff)
white = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
black = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
//Setup Snow Flakes
srand ( time (NULL));
for (int flake = 0; flake < NOOFFLAKES; flake ++)
{
x[flake] = rand () % SCREEN_WIDTH;
y[flake] = rand () % (SCREEN_HEIGHT / 2 );
xo[flake] = x[flake];
yo[flake] = y[flake];
/* Lock the screen for direct access to the pixels */
if ( SDL_MUSTLOCK(screen) ) {
if ( SDL_LockSurface(screen) < 0 ) {
fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
return 0;
}
}
if ( SDL_MUSTLOCK(screen) ) {
SDL_UnlockSurface(screen);
return 0;
}
// putpixel(screen, x[flake], y[flake], white);
/* Update just the part of the display that we've changed */
SDL_UpdateRect(screen, x[flake], y[flake], 1, 1);
}
int move, movex, movey;
for (int loop = 0; loop < 699; loop ++)
{
for (int flake = 0; flake < NOOFFLAKES; flake ++)
{
// choose whether to move left, right or straight down and by how many pixels
move = rand () % 3;
movex = (rand () % 2) + 1;
movey = (rand () % 5) + 1;
//backup the existing positions
xo[flake] = x[flake];
yo[flake] = y[flake];
// now draw again
/* Lock the screen for direct access to the pixels */
if ( SDL_MUSTLOCK(screen) ) {
if ( SDL_LockSurface(screen) < 0 ) {
fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
return 0;
}
}
if ( SDL_MUSTLOCK(screen) ) {
SDL_UnlockSurface(screen);
return 0;
}
putpixel(screen, xo[flake], yo[flake], black);
// move down the screen by the no of movey
if (y[flake] + (movey +1) < SCREEN_HEIGHT)
{y[flake] = y[flake] + (movey +1);}
else { putpixel(screen, x[flake], y[flake], white);
y[flake] = rand () % (SCREEN_HEIGHT / 2 );
}
if (x[flake] < 0)
{x[flake] = movex;}
if (x[flake] > SCREEN_WIDTH)
{x[flake] = (SCREEN_WIDTH - movex);}
// if 0 then left, 1 for right, else straight down
if (move == 0)
{x[flake] = x[flake] - movex;}
if (move == 1)
{x[flake] = x[flake] + movex;}
putpixel(screen, x[flake], y[flake], white);
/* Update just the part of the display that we've changed */
SDL_UpdateRect(screen, xo[flake], yo[flake], 1, 1);
SDL_UpdateRect(screen, x[flake], y[flake], 1, 1);
}
//set snowflake loop update delay
SDL_Delay (80);
}
//Wait 2 seconds
SDL_Delay( 2000 );
//Quit SDL
SDL_Quit(); return 0; }
|