Public Group

# Another LazyFoo Tutorial Q

This topic is 3365 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been learning C++/graphics/SDL from his tutorials (which are awesome) but I stumbled upon something I don't understand. Some background is that I am trying to make a gravity simulator and eventually plan on having a bunch of small objects flying around and planets of different sizes pretty much fixed in place (at least at first). I was having trouble figuring out how I'd implement different sizes, since in his tutorial he loads a bitmap for his main "object" on screen, and just has SDL draw a white screen as the background:

//Fill the screen white
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );

//Show the dot on the screen
myDot.show();

void Dot::show() {
//Show the dot
apply_surface( x, y, dot, screen ); }

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;

//Give the offsets to the rectangle
offset.x = x;
offset.y = y;

//Blit the surface
SDL_BlitSurface( source, NULL, destination, &offset );
}

*From tutorials on Blitting and Motion*

So in any case, my point is that for the background he just says "Make white!" and for the actual dot he loads a bitmap and gives the coordinates for it. This means that if I wanted a different-sized dot, I'd have to include a bitmap of that dot in the folder as well. However, he also makes a rectangle "on the fly" this way in his tutorial on collisions. Could I not just create my dots on the fly like that as well? Or does that take much more power than just copy-pasta-ing an image onto the screen? Or at least, is there some way to give it just one bitmap and have it adjust the size on its own? That way I could give it a large bitmap and it could just scale it down to whatever it needed.

##### Share on other sites
You can draw rectangles using the SDL_FillRect function. Look at how the white background was filled, the first parameter is the destination surface, the second parameter is the rectangle area to fill and the last parameter being the colour.

//To draw a red rectangle 50 pixels wide/tall at x:20,y:30SDL_Rect pos = {20, 20, 50, 50}; // left. top, right, bottomSDL_FillRect(screen, &pos, 0xFF0000);

##### Share on other sites

Can you draw circles and other things with SDL?
* misses Java *

##### Share on other sites
Quote:
 Can you draw circles and other things with SDL?* misses Java *

SDL is pretty low level, they only include features that you cannot implement yourself using their API or features that can be hardware accelerated.

The SDL_gfx add-on library has functions for drawing lots of different shapes.
Quote:
 However, he also makes a rectangle "on the fly" this way in his tutorial on collisions. Could I not just create my dots on the fly like that as well? Or does that take much more power than just copy-pasta-ing an image onto the screen? Or at least, is there some way to give it just one bitmap and have it adjust the size on its own? That way I could give it a large bitmap and it could just scale it down to whatever it needed.

SDL_gfx also allows surface scaling.

##### Share on other sites
Hooray!

Although, it almost feels like "cheating" that I'm using someone else's implementation. I mean, I understand that SDL in general is just a short-hand for interfacing with the computer at the lower level and that it's been optimized beyond anything that I can even understand at this point, but still, I kind of feel like Batman and his anti-shark spray, instead of just using basic tools to do things myself. Oh well, I guess the next best thing to that is just using their libraries and then figuring out how they work so that I know what I'm doing and not just using them as a black box. :)

Thanks everyone!

1. 1
2. 2
3. 3
4. 4
frob
13
5. 5

• 16
• 13
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632170
• Total Posts
3004550

×