Jump to content

  • Log In with Google      Sign In   
  • Create Account

Help Needed With SDL_Rect Arrays


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 A1P4A_0M3GA   Members   -  Reputation: 89

Like
-1Likes
Like

Posted 20 April 2011 - 10:30 PM

I'm not entirely sure if this code is logical, but I am trying to create an array of randomly generated SDL_Rects. Then I need them applied to the screen, only once, however. (I somehow managed to blit the Rects every frame, before - I think I was calling make_rects() every frame). My time and array declarations are below, along with my main function and make_rects().


//array of rects
SDL_Rect rects[11];

//randomly generate the rectangle's properties
void make_rects(SDL_Rect)
{
	for(int n = 10; n>0; n--)
	{
		SDL_Rect faller;

		faller.y = rand() % 450 + 1;
		faller.x = rand() % 560 + 1;
		faller.w = rand() % 80 + 1;
		faller.h = 30;

		rects[n] = faller;
		SDL_FillRect(screen, &rects[n], SDL_MapRGB(screen->format, 0x77, 0x77, 0x77));
	}
}


int main(int argc, char* args[])
{	
	bool quit = false;

	//seed for random numbers
	srand(time(0));

	//class objects
	Square mySquare;
	Timer fps;
	Timer image;
	Button youLose(170, 120, 320, 240);

	//TODO: place in declarations
	int bxVel = 7;

	if(init() == false)	
	{
		return 1;
	}

	if(load_files() == false)
	{
		return 1;
	}

	//successfully applied surfaces
	wall.x = rand() % 640+1;
	wall.y = rand() % 460+1;
	wall.w = 40;
	wall.h = 20;
	
	brick.x = 50;
	brick.y = 20;
	brick.w = 300;
	brick.h = 20;

	while(quit == false)
	{
		fps.start();

		while(SDL_PollEvent(&event))
		{
			mySquare.handle_input();

			if(event.type == SDL_QUIT)
			{
				quit = true;
			}
		}
		mySquare.move();

		//apply screen
		SDL_FillRect(screen, &brick, SDL_MapRGB(screen->format, 0x77, 0x77, 0x77));

		//currently unneccessary image
		apply_surface(500, 0, square, screen, NULL);

		//call make rects
		make_rects(rects[11]);

		//make brick move
		brick.x += bxVel;
		if((brick.x < 0) || (brick.x + brick.w > SCREEN_WIDTH))
		{
			brick.x -= bxVel;
			bxVel *= -1;
		}

		//show white screen, grey wall
		SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
		SDL_FillRect(screen, &wall, SDL_MapRGB(screen->format, 0x77, 0x77, 0x77));
		SDL_FillRect(screen, &brick, SDL_MapRGB(screen->format, 0x77, 0x77, 0x77));

		mySquare.show();
		
		if(SDL_Flip(screen) == -1)
		{
			return 1;
		}

		if(fps.get_ticks() < 1000 / FRAME_RATE)
		{
			SDL_Delay((1000 / FRAME_RATE) - fps.get_ticks());
		}
	}

	clean_up();

	return 0;
}

Thanks in advance.
-A1P4A 0M3GA
Lead script writer on Scutum [http://www.gamedev.n...-entertainment/]
Team Member of Forcas Entertainment
Amateur programmer with C++ and SDL knowledge
Game Enthusiast

Sponsor:

#2 rip-off   Moderators   -  Reputation: 8727

Like
1Likes
Like

Posted 21 April 2011 - 02:59 AM

Call make_rects once before the start of your loop. Make rects should not contain any drawing code. Then, provide a function draw_rects. Also note that make_rects currently doesn't require any parameters, and that passing rects[11] is an out-of-bounds array access (an array with 11 elements has indices 0..10).

You don't need globals here:
void make_rects(SDL_Rect *array, int N)
{
        for(int i = 0 ; i < N ; ++i)
        {
                SDL_Rect &faller = array[i];

                faller.y = rand() % 450 + 1;
                faller.x = rand() % 560 + 1;
                faller.w = rand() % 80 + 1;
                faller.h = 30;
        }
}

void draw_rects(SDL_Surface *screen, SDL_Rect *array, int N)
{
    Uint32 pixel = SDL_MapRGB(screen->format, 0x77, 0x77, 0x77);
    for(int i = 0 ; i < N ; ++i)
    {
        SDL_Rect copy = array[i];
        SDL_FillRect(screen, &copy, pixel);
    }
}
Note I make a copy of the rectangle in the draw function because SDL_FillRect will clip the rectangle it is passed in-place, which means it might modify your original array if you are not careful.

#3 Zahlman   Moderators   -  Reputation: 1682

Like
0Likes
Like

Posted 21 April 2011 - 02:59 AM

... So what exactly isn't working / do you need help with / comments on?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS