Give stars different colours, but keep it barely noticeable.
[gdvolley.git] / src / starfield.cpp
blob85e9685d5c5a8ff206eb2c71e725a083b15edfb4
1 //
2 // C++ Implementation: Starfield
3 //
4 // Description:
5 //
6 //
7 // Author: Öyvind Johannessen <gathers@gmail.com>, (C) 2008
8 //
9 // Copyright: See COPYING file that comes with this distribution
12 #include "starfield.h"
14 void Starfield::positionStar(int star)
16 bool done=false;
17 int newx, newy;
18 while(!done){
19 newx=int(640.0*randy());
20 newy=int(215.0*randy());
21 done=true;
22 for(int n=0;n<nr_of_stars;n++){
23 if(starx[n]==newx && stary[n]==newy){
24 done=false;
25 break;
29 starx[star]=newx;
30 stary[star]=newy;
31 starc[star]=0;
32 starRed[star]=0.7+randy()*0.3;
33 starGreen[star]=0.7+randy()*0.3;
34 starBlue[star]=1;
38 Starfield::Starfield()
40 for(int n=0;n<nr_of_stars;n++){
41 positionStar(n);
46 void Starfield::updateStars()
48 for(int n=0;n<nr_of_stars;n++){
49 starc[n]=starc[n]+float(4.0*randy()-4.1*randy());
50 if(int(starc[n])>=256)
51 starc[n]=255;
52 if(int(starc[n])<=0){
53 positionStar(n);
59 void Starfield::drawStarsOn(SDL_Surface *surf)
61 // Uint32 pixel;
62 if(SDL_LockSurface(surf)){
63 fprintf(stderr,"Couldn't lock surface to draw stars..\n");
64 return;
66 for(int n=0;n<nr_of_stars;n++){
67 putpixel(surf,starx[n],stary[n],(0xFF<<24)|(int(starc[n]*starRed[n])<<16)|(int(starc[n]*starGreen[n])<<8)|int(starc[n]*starBlue[n]));
68 /* pixel=(0xFF<<24)|(int(50)<<16)|(int(140)<<8)|int(200);
69 putpixel(surf,starx[n],stary[n],pixel);*/
71 SDL_UnlockSurface(surf);