Page 1 of 1

Rectangle problem with leaving screen (SDL)

Posted: Sat Jun 18, 2005 11:09 am
by Guest
After I finally got my ass up and began SDL learning, I made my first program. The whole thing is a rectangle based shooter, similar to my Blitz "Prick Fight".

The problem is, when I move a rectangle off screen, it permanently removes the portion that I moved off the screen. I do not yet know weather it actually modifies my width and height or if it stops drawing half the rectangle, but has anyone had a similar problem? If so, do you know what is wrong? Is my best bet just making a boundary 5 or so pixels from the edge of the screen to prevent peeps from leaving it? If so, what about bullets, which are also rectangles? That would look gay if they just disappear near the edge of the screen…

I'll do some fprintf's and tell you if it actually changes my x,y,w and h.

If it does change the size, I can just update it when the player re-enter's the screen, but that would make it a hassle to change the player size....

As I currently have it, the player does not have his own X and Y. The player class has a public SDL_Rect object inside it, which has X,Y,W,H as members. Do you think I should make the players have private X,Y,W,H and every time the rectangle's X,Y,W, and H are not equal to them, set them equal?

And, to make these new private members, should I just privately inherit the SDL_Rect class and add some accessors?

As I am sure you can all tell, I am not only new to SDL but also to C++ as a whole, so forgive my many questions. For anyone who actually replies, thanks a bunch. :D

Posted: Sat Jun 18, 2005 11:45 am
by Guest
Woah...check this out.
My output data wrote:X: 1
Y: 10
W: 32
H: 64

X: -2
Y: 10
W: 32
H: 64

X: -3
Y: 10
W: 30
H: 64

X: -3
Y: 10
W: 27
H: 64

X: -3
Y: 10
W: 24
H: 64

X: -3
Y: 10
W: 21
H: 64
That's weird...I think every frame that the x is negative, it'll add my negative value to my width...oh well. We now know it actually changes the size. I guess I will do two things.

1) Make it so you don't go off screen
2) Make my player class have X,Y,W,H and set the rect X,Y,W, and H equal to them if they are not already.

If someone has a better idea, I am open minded.

Oh, and DJ Yoshi, if you even think about coming in here and dicing my SDL skillz that I gained yesterday, I'll wedge my foot so far up your asshole that you'll never shit again. Oh, yeah, and I'll delete your post. :spin:

However, if you post relevant to the topic, then thanks for posting. :D

Posted: Sat Jun 18, 2005 3:05 pm
by MarauderIIC
Try making a button to set the x to negative and move the whole thing off screen and then a button to set x to center and see what happens.

Posted: Sat Jun 18, 2005 3:14 pm
by JS Lemming
Your best bet is to make your own classes that contain the x and y values. When it comes time to draw the rectangle, those values associated with the rect class should be set to yours. Don't worry about checking if they are set... just do it. The values in the rect class are actually only supposed to be temporary.

Posted: Sat Jun 18, 2005 11:51 pm
by Guest
But if I don't check if they are equal and I just set them to eachother every frame, won't that slow the program down? We must remember that I am on an antique...x.x

Mar, I'll try that in a few and post the results.

Posted: Sun Jun 19, 2005 1:46 am
by Falco Girgis
Arce wrote:But if I don't check if they are equal and I just set them to eachother every frame, won't that slow the program down? We must remember that I am on an antique...x.x

Mar, I'll try that in a few and post the results.
What in the hell? I hope I'm understanding this right.

You're telling me that you're checking if two values equal one another, and if they don't you're setting them equal to each other?

Dear god, that is much slower. You realize that by the time the logic is done to see if they are equal, you've wasted about (if not more) time and power than just setting them equal? Then, if they aren't equal, you set them equal.

At best your method will be as fast as JSL's (assuming evaluating conditions requires about the same amount of work as setting a variable, which I doubt), and potentially it can run 1/2 the speed it would because you'd also be setting the variable equal to another ontop of the normal check.

Posted: Sun Jun 19, 2005 1:14 pm
by Guest
What in the hell? I hope I'm understanding this right.

You're telling me that you're checking if two values equal one another, and if they don't you're setting them equal to each other?

Dear god, that is much slower. You realize that by the time the logic is done to see if they are equal, you've wasted about (if not more) time and power than just setting them equal? Then, if they aren't equal, you set them equal.

At best your method will be as fast as JSL's (assuming evaluating conditions requires about the same amount of work as setting a variable, which I doubt), and potentially it can run 1/2 the speed it would because you'd also be setting the variable equal to another ontop of the normal check.
Aight.

Hey, get this. I set the width and height every frame to the player's new width and height, and now it won't let me leave the screen. Every frame it pushes me back. I guess SDL cannot draw offscreen, so prevents from doing so by making the rectangle smaller. Now that I make it bigger after it makes it smaller, it just pushes me back on. o.o

Anyway, I'll get to that button Mar.

Posted: Sun Jun 19, 2005 9:21 pm
by JS Lemming
Post your code please... I've never heard of such hog-wash!

Posted: Fri Jun 24, 2005 7:05 pm
by Guest
I would post it, if my computer wasn't destroyed.

Edit: My mom is getting a new 3 GHZ Pentium 4 Dell, so I'll keep her antique old one. :)