#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;
}
Stupid Simple Question.
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:
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.
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));
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement