Question about menu system

im making a menu system to change the color of pixels in a gravity engine i made. the engine works fine, and the menu colors the particles, but will only ever turn them to red, no matter what color i tell it to change it to. i'm relatively new to c++ and have been working on this problem for hours and have decided to post it here lol. any help would be appreciated, thanks in advance :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

bool running = true;
bool colorChosen = false;
Uint32 particleColor;
Uint32 randomColor;


void menuScreen(SDL_Surface *screens, TTF_Font *fonts, SDL_Color colors)
{
    SDL_Surface *directionsColor;
    directionsColor = TTF_RenderText_Solid(fonts, "Choose what color you want", colors);

    //making the colors
    //white, red, yellow, blue, orange, green, purple
    Uint32 colorsOfPixels[7];
    colorsOfPixels[0] = SDL_MapRGB(screens->format, 255, 255, 255);
    colorsOfPixels[1] = SDL_MapRGB(screens->format, 255, 0, 0);
    colorsOfPixels[2] = SDL_MapRGB(screens->format, 255, 255, 0);
    colorsOfPixels[3] = SDL_MapRGB(screens->format, 0, 0, 255);
    colorsOfPixels[4] = SDL_MapRGB(screens->format, 255, 165, 0);
    colorsOfPixels[5] = SDL_MapRGB(screens->format, 0, 255, 0);
    colorsOfPixels[6] = SDL_MapRGB(screens->format, 148, 0, 211);

    int boxWidth = 200;
    int boxHeight = 200;

    //rects for printing boxes to screen;
    SDL_Rect ColorRects[8];
    ColorRects[0].x = 50;
    ColorRects[0].y = 50;
    ColorRects[1].x = 300;
    ColorRects[1].y = 50;
    ColorRects[2].x = 550;
    ColorRects[2].y = 50;
    ColorRects[3].x = 800;
    ColorRects[3].y = 50;
    ColorRects[4].x = 50;
    ColorRects[4].y = 400;
    ColorRects[5].x = 300;
    ColorRects[5].y = 400;
    ColorRects[6].x = 550;
    ColorRects[6].y = 400;
    ColorRects[7].x = 800;
    ColorRects[7].y = 400;

    for(int i = 0;i < 8; ++i)
    {
        ColorRects[i].w = boxWidth;
        ColorRects[i].h = boxHeight;
    }

    const int menuFPS = 30;
    Uint32 menuStart;
    int colorChooseX;
    int colorChooseY;
    SDL_Event colorChooseEvent;

    while(!colorChosen)
    {
        menuStart = SDL_GetTicks();
        while(SDL_PollEvent(&colorChooseEvent))
        {
            switch(colorChooseEvent.type)
            {
                case SDL_QUIT:
                    colorChosen = true;
                    running = false;
                    break;
                case SDL_MOUSEBUTTONUP:
                    colorChooseX = colorChooseEvent.button.x;
                    colorChooseY = colorChooseEvent.button.y;
                    break;
            }
        }

        SDL_FillRect(screens, &screens->clip_rect, SDL_MapRGB(screens->format, 0, 0, 0));

        SDL_BlitSurface(directionsColor, NULL, screens, NULL);

        for(int i = 0; i < 8; ++i)
        {
            SDL_FillRect(screens, &ColorRects[i], colorsOfPixels[i]);
        }

        SDL_Flip(screens);

        for(int i = 0; i < 8; ++i)
        {
            if(colorChooseX > ColorRects[i].x and colorChooseX < (ColorRects[i].x + ColorRects[i].w) and colorChooseY > ColorRects[i].y and colorChooseY < (ColorRects[i].y + ColorRects[i].h))
            {
                particleColor = colorsOfPixels[i];
                colorChosen = true;
            }
            else if(colorChooseX > ColorRects[7].x and colorChooseX < ColorRects[7].x + ColorRects[7].y and colorChooseY > ColorRects[7].y and colorChooseY < ColorRects[7].y + ColorRects[7].h)
            {
                particleColor = randomColor;
                colorChosen = true;
            }
        }


        if(1000/menuFPS>SDL_GetTicks()-menuStart)
                SDL_Delay(1000/menuFPS-(SDL_GetTicks()-menuStart));
    }

    SDL_FreeSurface(directionsColor);
}
MAYBE the problem is that your colorsOfPixels[] array is only 7 fields long but your for() loops go through 8 loops (0..7). The 'maybe' is in caps because I don't really think that's the problem but that's what caught my eye.
Usually, accessing an array beyond it's boundaries causes strange errors.
Topic archived. No new replies allowed.