I've hit a road block. SDL

I am a few days into c++ and have hit a road block.

I am trying to fill the screen with randomly colored rectangles using SDL.

I built the array and assigned a random number from 1-6 to the array for later use assigning the color to be filled in SDL.

I am having trouble with for loops they worked fine for assigning the random int's for colors but I am having issues getting them to display and fill the full screen.

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
#include <SDL/SDL.h>
#include <iostream>
#include <string.h>
#include <sstream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include "board.h"
using namespace std;
FILE * ctt = fopen("CON", "w" );

int vmWidth=800;
int vmHeight=600;

int main(int argc, char** argv)
{
    SDL_Init(SDL_INIT_EVERYTHING);
    ofstream ctt("CON");
    freopen( "CON", "w", stdout );
    freopen( "CON", "w", stderr );
    SDL_Surface *screen;
    SDL_putenv("SDL_VIDEO_CENTERED=center");
    screen=SDL_SetVideoMode(vmWidth,vmHeight,32,SDL_SWSURFACE);
    const char* WINDOW_TITLE = "O n E";
    SDL_WM_SetCaption(WINDOW_TITLE,0);
    bool running=true;
    const int FPS=100;
    Uint32 start;

         while(running)
            {

            start = SDL_GetTicks();
            SDL_Event event;

            while(SDL_PollEvent(&event))
                {
                switch(event.type)
                {
                case SDL_QUIT:
                running = false;
                break;
                }
                }

    const int boardRow =  10;
    const int boardCol =  10;
    int boarda[boardRow][boardCol] = { 0 };
    SDL_Rect rectB;
    int swdr = vmWidth / boardRow;
    int shdc = vmHeight / boardCol;


    int bRow = 0;
    int bCol = 0;
    static bool bOnce;
    rectB.x=0;
    rectB.y=0;
    rectB.w=swdr;
    rectB.h=shdc;

    Uint32 color = SDL_MapRGB(screen->format, 0xff,0xff,0xff);
    Uint32 color2 = SDL_MapRGB(screen->format, 0x00,0xff,0xff);


        if (!bOnce)
        {
            for (bRow = 0; bRow < boardRow; bRow++)
            for (bCol = 0; bCol < boardCol; bCol++)
            boarda[bRow][bCol] = rand() % 6+1;

            for (bRow = 0; bRow < boardRow; bRow++)
            {
            rectB.x=bRow*swdr;
            rectB.y=0;
            SDL_FillRect(screen,&rectB,color);
            for (bCol = 0; bCol < boardCol; bCol++)
            rectB.y=bCol*shdc;
            SDL_FillRect(screen,&rectB,color);
            }


        bOnce = true;
        }


//logic && render

    SDL_Flip(screen);

            if(1000/FPS > SDL_GetTicks()-start)
                {
                SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
                }
            }
ctt.close();
SDL_Quit();
return 0;
}

I stayed up all night switching the recB.x and y's trying different variations and this is as close as I can get. any help provided would be greatly appreciated.
Last edited on
Never did fall asleep but figured it out!!

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <SDL/SDL.h>
#include <iostream>
#include <string.h>
#include <sstream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include "board.h"
using namespace std;
FILE * ctt = fopen("CON", "w" );

int vmWidth=800;
int vmHeight=600;

int main(int argc, char** argv)
{
    SDL_Init(SDL_INIT_EVERYTHING);
    ofstream ctt("CON");
    freopen( "CON", "w", stdout );
    freopen( "CON", "w", stderr );
    SDL_Surface *screen;
    SDL_putenv("SDL_VIDEO_CENTERED=center");
    screen=SDL_SetVideoMode(vmWidth,vmHeight,32,SDL_SWSURFACE);
    const char* WINDOW_TITLE = "O n E";
    SDL_WM_SetCaption(WINDOW_TITLE,0);
    bool running=true;
    const int FPS=100;
    Uint32 start;
    static bool bOnce;

         while(running)
            {

            start = SDL_GetTicks();
            SDL_Event event;

            while(SDL_PollEvent(&event))
                {
                switch(event.type)
                {
                case SDL_QUIT:
                running = false;
                break;
                case SDL_MOUSEBUTTONDOWN:
                bOnce = false;

                }
                }

    const int boardRow =  80;
    const int boardCol =  60;
    int boarda[boardRow][boardCol] = { 0 };
    SDL_Rect rectB;
    int swdr = vmWidth / boardRow;
    int shdc = vmHeight / boardCol;


    int bRow = 0;
    int bCol = 0;
    rectB.x=0;
    rectB.y=0;
    rectB.w=swdr;
    rectB.h=shdc;

    Uint32 color0 = SDL_MapRGB(screen->format, 0x00,0x00,0x00);
    Uint32 color1 = SDL_MapRGB(screen->format, 0xff,0x00,0x00);
    Uint32 color2 = SDL_MapRGB(screen->format, 0xff,0xff,0x00);
    Uint32 color3 = SDL_MapRGB(screen->format, 0x00,0xff,0x00);
    Uint32 color4 = SDL_MapRGB(screen->format, 0x00,0xff,0xff);
    Uint32 color5 = SDL_MapRGB(screen->format, 0x00,0x00,0xff);
    Uint32 color6 = SDL_MapRGB(screen->format, 0xff,0xff,0x00);



        if (!bOnce)
        {
            for (bRow = 0; bRow < boardRow; bRow++)
            for (bCol = 0; bCol < boardCol; bCol++)
            boarda[bRow][bCol] = rand() % 6+1;


            for (bRow = 0; bRow < boardRow; bRow++)
            for (bCol = 0; bCol < boardCol; bCol++)
            {
            rectB.x=bRow*swdr;
            rectB.y=bCol*shdc;
            if (boarda[bRow][bCol] == 1)
            {
            SDL_FillRect(screen,&rectB,color1);
            }
            else if (boarda[bRow][bCol] == 2)
            {
            SDL_FillRect(screen,&rectB,color2);
            }
            else if (boarda[bRow][bCol] == 3)
            {
            SDL_FillRect(screen,&rectB,color3);
            }
            else if (boarda[bRow][bCol] == 4)
            {
            SDL_FillRect(screen,&rectB,color4);
            }
            else if (boarda[bRow][bCol] == 5)
            {
            SDL_FillRect(screen,&rectB,color5);
            }
            else if (boarda[bRow][bCol] == 6)
            {
            SDL_FillRect(screen,&rectB,color6);
            }else
            SDL_FillRect(screen,&rectB,color0);
            }



        bOnce = true;
        }


//logic && render

    SDL_Flip(screen);

            if(1000/FPS > SDL_GetTicks()-start)
                {
                SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
                }
            }
ctt.close();
SDL_Quit();
return 0;
}
Topic archived. No new replies allowed.