1

Every time I run my application the colors change, the display is as below

Display = SDL_SetVideoMode(WIDTH, HEIGHT, 16, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_ASYNCBLIT)

and then I do

SDL_FillRect(Display, &Woutput, colors[1]);

colors var info:

Uint32 colors[3];
colors[1] = SDL_MapRGB(Surf_Display->format, 32, 32, 255);

If I change SDL_FillRect(Display, &Woutput, colors[1]); to SDL_FillRect(Display, &Woutput, SDL_MapRGB(Surf_Display->format, 32, 32, 255)); it works correctly which makes me believe that it has something to do with the Uint32, but not sure what to change it to.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Elgoog
  • 2,205
  • 7
  • 36
  • 48
  • If `w` is anything but zero, then you're indexing beyond the bounds of your colors array of length one. What is `w` (or capital `W`?) supposed to be? – HostileFork says dont trust SE Oct 31 '11 at 00:38
  • @HostileFork you are correct, I have gone outside the bounds of the array but only for the example, I should have proofed my example see changes – Elgoog Oct 31 '11 at 00:48
  • 1
    According to the documentation, SDL_MapRGB returns a `Uint32`. It should not make a difference whether you are using an array element as an intermediary or passing it directly. So if this is all the code you've got--and your FillRect call actually passes `colors[1]` *after* the array has been initialized there should be no problem. Can you provide a little more code in precise line-ordering to look for other possible problems? – HostileFork says dont trust SE Oct 31 '11 at 01:01
  • 1
    What if you initialize your colors like `Uint32 colors[3] = {0}`? Maybe somehow you're not filling color in with MapRGB prior to use. When that happens colors will just have whatever random junk is in there and that could explain the color changing you're seeing. – greatwolf Oct 31 '11 at 01:17
  • +1 @VictorT. maybe you should post an answer ;) – Elgoog Oct 31 '11 at 03:25
  • @Elgoog there you go, hope that helps =) – greatwolf Oct 31 '11 at 04:14

1 Answers1

1

I'm adding this comment as an answer as suggested.

Your colors array probably isn't initialized with your expected values when you call SDL_FillRect. When that happens colors will have whatever random junk is in there and that would explain the color changing on each run.

To see if this is really the case try zero-init'ing your array straight-off the bat like this:

Uint32 colors[3] = {0};

Now if you run your program and find the colors no longer change (it'll probably just be black since colors contains all zero's now) -- well you know what's wrong at this point.

greatwolf
  • 20,287
  • 13
  • 71
  • 105