Sign in to follow this  

Stupid Simple Question.

This topic is 4138 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
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

This topic is 4138 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this