A small SDL and array problem

Hello.

I'm still quite a beginner with C++ (or any programming), but I thought I'd take a bit of a break from the everyday learning of boring data types, operations 'n stuff, so I tried SDL just a little bit.

I tried to make a program that draws random rectangles all over the screen.

Here is what I have

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
#include <iostream>
#include "SDL.h"
#include <cstdlib>

using namespace std;

int main(int argc, char *argv[])
{
	unsigned int rectC = 0;
	srand(time(NULL));

	SDL_Surface* screen = NULL;
	//Start SDL
	SDL_Init(SDL_INIT_VIDEO);

	const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo();

	int systemX = videoInfo->current_w;
	int systemY = videoInfo->current_h;

	Uint8 bpp = videoInfo->vfmt->BitsPerPixel;

	//Set up the screen
	screen = SDL_SetVideoMode(systemX, systemY, bpp, SDL_FULLSCREEN);
	if (!screen)
	{
		cout << "SDL_SetVideoMode failed.\n";
		return 0;
	}

	int halfscreenX = (systemX / 2);
	int halfscreenY = (systemY / 2);
	
        cout << "Enjoy rectangles :D" << endl;

	//Hide cursor
	SDL_ShowCursor(0);
	SDL_Flip(screen);

        //Main loop
	for (int ctr = 10; ctr; ctr--)
	{
		SDL_Rect randRect[] = {(halfscreenX - (rand() % halfscreenX)), (halfscreenY - (rand() % halfscreenY)), (rand() % halfscreenX), (rand() % halfscreenY)};
		SDL_FillRect(screen, &randRect[rectC], SDL_MapRGB(screen->format, (rand() % 256), (rand() % 256), (rand() % 256)));
		SDL_Flip(screen);
		SDL_Delay(500);
                rectC++;
	}
	
        //Quit SDL
	SDL_Quit();

	return 0;
}



But that doesn't work like I expected!?

The screen gets completely filled with random colors and the delay seems random, rather than half a second.

What am I doing wrong?

Is it the array?
Can't I use it like that?

Can someone please explain.

Thanks.
Hmm, interesting. I wouldn't though the line
SDL_Rect randRect[] = {(halfscreenX - (rand() % halfscreenX)), (halfscreenY - (rand() % halfscreenY)), (rand() % halfscreenX), (rand() % halfscreenY)};
compiles, but it does. It simply creates an array of size 1.

Anyway, what you're doing wrong is accessing nonexistant elements of randRect[].
Oh, thanks! So my asumption on the empy brackets was wrong :)
How do I make an infinite array, then?
Or is there another way to do this?
Why do you even need an array? You are never using the SDL_Rects again.
Wat?
I'm using them in a loop, one after another.

:/
Topic archived. No new replies allowed.