Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't redraw an image in space
    text
    copied!<p>I am getting some practice with SDL. I currently am drawing space with classes star, sun, and earth. I can get everything to render to the screen but when I move the sun with the right arrow key, I can't figure out how to delete the old sun to simulate that the view is rotating around the sun. It just creates a big line across the screen. I think I need to clear the screen, redraw the stars, sun, and earth every time the key is down. However, I can't redraw the stars because they fall out of scope and if I recreate them they will be in a different position since I use rand() to randomize their locations. I have hit a big wall and would appreciate any advice. </p> <p>The code:</p> <pre><code>// Planet.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include &lt;SDL.h&gt; #include &lt;SDL_gfxPrimitives.h&gt; #include &lt;stdlib.h&gt; #include &lt;time.h&gt; class Star { private: int x,y,rad,r,g,b,a; SDL_Surface* screen; public: Star(SDL_Surface* sc, int xx, int yy, int ra, int rr, int gg, int bb, int aa){screen=sc;x=xx;y=yy;rad=ra;r=rr;g=gg;b=bb;a=aa;} ~Star(){} void Show() { filledCircleRGBA(screen, x, y, rad, r, g, b, a); } }; class Sun { private: int x,y,rad,r,g,b,a; SDL_Surface* screen; public: Sun(SDL_Surface* sc, int xx, int yy, int ra, int rr, int gg, int bb, int aa){screen=sc;x=xx;y=yy;rad=ra;r=rr;g=gg;b=bb;a=aa;} ~Sun(){} void Show() { for (int light=10;light&lt;241;light+=10){ filledCircleRGBA(screen, x, y, rad+(light/7), r, g, b+(light/2), a-light); } } void Move(int a, int b) { x+=a; y+=b; } }; class Earth { private: int x,y,rad,r,g,b,a; SDL_Surface* screen; public: Earth(SDL_Surface* sc, int xx, int yy, int ra, int rr, int gg, int bb, int aa){screen=sc;x=xx;y=yy;rad=ra;r=rr;g=gg;b=bb;a=aa;} ~Earth(){} void Show() { for (int light=10;light&lt;241;light+=10){ filledCircleRGBA(screen, x, y, rad+(light/7), r, g, b+(light/2), a-(light/2)); } for (double light=0;light&lt;.25;light+=.01){ filledEllipseRGBA(screen, x, y-(rad*1.05), rad*(.60+light), rad*(.1+light), 170+(light*100), 170+(light*100), 170+(light*100), 255-(light*1000)); } } }; int main( int argc, char* args[] ) { //Start SDL SDL_Init( SDL_INIT_EVERYTHING ); SDL_Surface* screen = NULL; screen = SDL_SetVideoMode( 1366, 768, 32, SDL_FULLSCREEN | SDL_SWSURFACE); srand((unsigned)time(NULL)); //Stars for (int x=0;x&lt;1361;x+=25){ for (int y=0;y&lt;766;y+=25){ int Special = rand()%3; if(Special==0||Special==1){ Star star(screen, x+(rand()%30), y+(rand()%30), 0+(rand()%1), 235+rand()%21, 235+rand()%21, 235+rand()%21, 205+rand()%51); star.Show(); } if(Special==2){ Star star(screen, x+(rand()%30), y+(rand()%30), 0+(rand()%2), 235+rand()%21, 235+rand()%21, 235+rand()%21, 205+rand()%51); star.Show(); } } } //Sun Sun sun(screen, 200, 300, 10, 255, 255, 0, 255); sun.Show(); //Earth Earth earth(screen, 700, 400, 100, 0, 80, 255, 255); earth.Show(); SDL_Event event; bool Running=1; bool keysHeld[323]={false}; while (Running) { // Handle input if (SDL_PollEvent(&amp;event)) { if (event.type == SDL_QUIT) { Running = false; } if (event.type == SDL_KEYDOWN) { keysHeld[event.key.keysym.sym] = true; } if (event.type == SDL_KEYUP) { keysHeld[event.key.keysym.sym] = false; } } if ( keysHeld[SDLK_ESCAPE] ) { Running = false; } if ( keysHeld[SDLK_LEFT] ) { } if ( keysHeld[SDLK_RIGHT] ) { SDL_FillRect(screen, NULL, SDL_MapRGB(screen-&gt;format, 0, 0, 0)); sun.Move(4,0); sun.Show(); } if ( keysHeld[SDLK_UP] ) { } if (keysHeld[SDLK_DOWN]) { } SDL_Flip(screen); } //Quit SDL SDL_Quit(); return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload