Sign in to follow this  
PoopLoops

Another LazyFoo Tutorial Q

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 this post


Link to post
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:30
SDL_Rect pos = {20, 20, 50, 50}; // left. top, right, bottom
SDL_FillRect(screen, &pos, 0xFF0000);

Share this post


Link to post
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 this post


Link to post
Share on other sites
Hooray!

[img]http://images.google.com/imgres?imgurl=http://images.encyclopediadramatica.com/images/thumb/a/a3/Lulz_warrior_turtle.jpg/180px-Lulz_warrior_turtle.jpg&imgrefurl=http://encyclopediadramatica.com/Lulz&usg=__FhLT0p0cbGpAnKypgmtdNC3vFyU=&h=240&w=180&sz=9&hl=en&start=14&um=1&tbnid=D-pOuluaVSNO6M:&tbnh=110&tbnw=83&prev=/images%3Fq%3Dlulz%2Bturtle%26hl%3Den%26safe%3Doff%26client%3Dfirefox-a%26rls%3Dcom.ubuntu:en-US:unofficial%26hs%3DehW%26sa%3DN%26um%3D1[/img]

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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this