Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6116153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:10:25+00:00 2026-05-23T15:10:25+00:00

I am getting some practice with SDL. I currently am drawing space with classes

  • 0

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.

The code:

// Planet.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <SDL.h>
#include <SDL_gfxPrimitives.h>
#include <stdlib.h>
#include <time.h>

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<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<241;light+=10){
        filledCircleRGBA(screen, x, y, rad+(light/7), r, g, b+(light/2), a-(light/2));
        }
        for (double light=0;light<.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<1361;x+=25){
        for (int y=0;y<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(&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->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; 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:10:26+00:00Added an answer on May 23, 2026 at 3:10 pm

    EDIT

    Since my last answer was rubbish, I try again:

    Yes, indeed, the stars are going out of scope since they are declared in the loop. What you could do is to save every star in an array, but this is a huge waste of bytes.

    A better approach in my opinion is to draw all stars in a surface, and blit it on the screen every time you need it. This should be fast and memory efficient. I’ll sho a short sample:

    Uint32 rmask, gmask, bmask, amask;
    
        /* SDL interprets each pixel as a 32-bit number, so our masks must depend
           on the endianness (byte order) of the machine */
    #if SDL_BYTEORDER == SDL_BIG_ENDIAN
        rmask = 0xff000000;
        gmask = 0x00ff0000;
        bmask = 0x0000ff00;
        amask = 0x000000ff;
    #else
        rmask = 0x000000ff;
        gmask = 0x0000ff00;
        bmask = 0x00ff0000;
        amask = 0xff000000;
    #endif
    SDL_Surface* star_surface = SDL_CreateRGBSurface(SDL_SWSURFACE, 1361, 766, 32, rmask, gmask, bmask, amask);
    

    And now, you draw your stars into this surface:

    ...
    Star star(star_surface, x+(rand()%30), y+(rand()%30), 0+(rand()%1), 235+rand()%21, 235+rand()%21, 235+rand()%21, 205+rand()%51);
    star.Show();
    ...
    

    Any time you need it, you blit this surface as a background to your screen using this function:

    int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Getting through some practice exam papers but in regards to the relational algebra I'm
I'm making a forum software right now, just for some practice, and I'm getting
I'm getting some strange, intermittent, data aborts (< 5% of the time) in some
I am getting some errors thrown in my code when I open a Windows
I'm getting some objects from an external library and I need to store those
I am interested in getting some Python code talking to some Ruby code on
I'm getting some really wierd linking errors from a class I wrote. I am
I'm getting some weird behaviour recompiling some applications in 2009 that used widestrings at
I'm getting some confusing results around myCustom.ashx handler. If i visit the handler via
I am getting some junk data returned from a ToString() call on a DateTime

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.