I am doing a simple star field.. problem is that the stars are drawn "twice".. so if the star is at 0, 0, it is drawn, then what is supposed to happen is that the 0,0 star gets erased and it''s supposed to be drawn at a new location.. let''s say the velocity is 5.. It should start at 0,0 then 5,0 then 10,0.. etc. but I see 2 dots, one at 0,0 and one at 5,0, then one at 5,0 and one at 10,0.. etc. So there is 2 dots following each other. I am using FillRect() to draw the black BG.. here is that part of the code:
// Black out the screen for redrawing.
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
// Redraw screen.
draw_scene(screen);
SDL_Flip(screen);
The whole code is below (don''t comment on code design/style please, this is very early, I like to get things working before optimizing/cleaning, I just threw variables all over the place):
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <SDL/SDL.h>
using namespace std;
void DrawPixel(SDL_Surface *screen, Uint8 R, Uint8 G, Uint8 B, int x, int y)
{
Uint32 color = SDL_MapRGB(screen->format, R, G, B);
if ( SDL_MUSTLOCK(screen) ) {
if ( SDL_LockSurface(screen) < 0 ) {
return;
}
}
switch (screen->format->BytesPerPixel) {
case 1: { /* Assuming 8-bpp */
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
*bufp = color;
}
break;
case 2: { /* Probably 15-bpp or 16-bpp */
Uint16 *bufp;
bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
*bufp = color;
}
break;
case 3: { /* Slow 24-bpp mode, usually not used */
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
*(bufp+screen->format->Rshift/8) = R;
*(bufp+screen->format->Gshift/8) = G;
*(bufp+screen->format->Bshift/8) = B;
}
break;
case 4: { /* Probably 32-bpp */
Uint32 *bufp;
bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
*bufp = color;
}
break;
}
if ( SDL_MUSTLOCK(screen) ) {
SDL_UnlockSurface(screen);
}
SDL_UpdateRect(screen, x, y, 1, 1);
}
inline int err() {
cerr << SDL_GetError() << ''\n'';
exit(1);
}
class star {
private:
int vx, vy;
int colour;
int dir;
bool moving;
int x, y;
public:
star(int velx, int vely, int color, int direction,
int xi, int yi)
: vx(velx), vy(vely), colour(color), dir(direction),
moving(false), x(xi), y(yi) {}
void setv(int velx, int vely) { vx=velx; vy=vely; }
void setd(int direction) { dir=direction; }
void setx(int xx) { x=xx; }
void sety(int yy) { y=yy; }
void start() { moving=true; }
void stop() { moving=false; }
int getx() { return x; }
int gety() { return y; }
int getvx() { return vx; }
int getvy() { return vy; }
};
vector<star*> stars;
#define MAXSTARS 1
void draw_scene(SDL_Surface* screen) {
if (stars.size()<MAXSTARS) {
// Get random values for velocity and initial location.
int rvx=(rand()%5)+1;
int rvy=(rand()%5)+1;
int ry=(rand()%480)+1;
int rx=(rand()%640)+1;
cout << rvx << '' '' << rvy << '' '' << ry << '' '' << rx << endl;
stars.push_back(new star(rvx, rvy, 123, 0, rx, ry));
stars[stars.size()-1]->start();
}
// Move the stars & check if any star is off-screen.
for (unsigned int i=0; i<stars.size(); ++i) {
stars[i]->setx(stars[i]->getx()+stars[i]->getvx());
if (stars[i]->getx() > 640)
stars[i]->setx(0);
// The actual drawing.
SDL_LockSurface(screen);
DrawPixel(screen, 255, 255, 255, stars[i]->getx(), stars[i]->gety());
SDL_UnlockSurface(screen);
// cout << "Drew star(" << i << ") point at (" << stars->getx() << ", " << stars->gety() << '')'' << endl;
</font>
}
}
<font color=blue>int</font> main(<font color=gray>/*if DEVC++, uncomment*/</font> <font color=gray>/*int argc, char** argv*/</font>) {
srand(time(0));
<font color=blue>if</font> (SDL_Init(SDL_INIT_VIDEO)<0)
<font color=blue>return</font> err();
atexit(SDL_Quit);
SDL_WM_SetCaption(<font color=darkred>"S.T.A.R.S"</font>, 0);
const <font color=blue>int</font> SW=640;
const <font color=blue>int</font> SH=480;
const <font color=blue>int</font> SD=16;
SDL_Surface* screen=SDL_SetVideoMode(SW,SH,SD,SDL_HWSURFACE|SDL_DOUBLEBUF);
<font color=blue>if</font> (!screen) <font color=blue>return</font> err();
<font color=blue>bool</font> gogogo=<font color=blue>true</font>;
SDL_Event event;
<font color=blue>while</font> (gogogo) {
<font color=blue>while</font> (SDL_PollEvent(&event)) {
<font color=blue>if</font> (SDL_QUIT==event.type)
gogogo=<font color=blue>false</font>;
<font color=blue>else</font> <font color=blue>if</font> (SDL_KEYDOWN==event.type)
<font color=blue>if</font> (SDLK_ESCAPE==event.key.keysym.sym)
gogogo=<font color=blue>false</font>;
}
<font color=gray>// Black out the screen for redrawing.
</font>
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
<font color=gray>// Redraw screen.
</font>
draw_scene(screen);
SDL_Flip(screen);
}
SDL_FreeSurface(screen);
<font color=blue>return</font> 0;
}
</pre><!–ENDSCRIPT–>