Jump to content
  • Advertisement
Sign in to follow this  
OzzoNation

Stupid Simple Question.

This topic is 4359 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I made a simple little SDL app that blits a square in the center of the window of random color. Every time you make any mouse or keyboard event, it changes color. However, every time I start the application, the square is always a light green. I don't know why? Here's the source:
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>

int main(int argc, char* argv[])
{
    if (SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        fprintf (stderr, "Unable to initialize the SDL Video Subsystem.", SDL_GetError());
        
        exit (1);
    }
        else
    {
        fprintf (stdout, "SDL Video Subsystem initialized.");
        
        atexit(SDL_Quit);
    }
    
    SDL_Surface *window = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE);
    
    SDL_WM_SetCaption("SDL Shell", 0);
    
    Uint8 red, green, blue;
    Uint32 color;
    SDL_Rect rect;
    
    SDL_Event event;
    bool is_game_running = true;
    
    while (is_game_running == true)
    {
        if (SDL_PollEvent(&event))
        {
            red = rand() % 256;
            green = rand() % 256;
            blue = rand() % 256;
            color = SDL_MapRGB(window -> format, red, green, blue);
            
            rect.x = 200;
            rect.y = 150;
            rect.w = 200;
            rect.h = 200;
                
            SDL_FillRect(window, &rect, color);
                
            SDL_UpdateRect(window, 0, 0, 0, 0);
            
            if (event.type == SDL_QUIT)
            {
                is_game_running = false;
            }
        }
    }
          
    return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
It looks like you never seed the random number generator with srand(), so it always uses the default seed. If you use srand() with a number source that is different everytime you run the program, such as time() (well, within a second anyways), then you'll get different random numbers.

Share this post


Link to post
Share on other sites
rand is psuedo-random, theres no actual true random calls, but you can make it seem like it by calling srand and seeding it with the system time, like so: srand(time(NULL));

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!