Sign in to follow this  
Gink

Tetris with SDL problems

Recommended Posts

I am using BMP images to display the falling blocks. THe problem Im having is the image is left behind when the image moves. How can I get rid of that? Right now it's like a paint program gone wrong.

Share this post


Link to post
Share on other sites
You are going to have to do one of two things:

1) redraw everything on the entire screen every frame.

2) Draw over only the parts of the screen that changed on the last frame. That also means that you need to redraw any object previous overlapped by the object you are moving.

All in all, option #1 is certainly easier because you don't have to keep track of what is moving and what is not. However, Option #2 will, if wisely implemented, yield a higher frame rate.

Think of the screen buffer as actually being like a paint program, like you said. The screen has no concept of "objects" or "pictures". It's just an array of colored pixels. Always remember that a pixel stays a color until you tell it otherwise.

These tutorials may be of some help if you have not already seen them.

Share this post


Link to post
Share on other sites
You should maintain the board state in some form. Maybe like a matrix.

This way the only thing you do in your game is maintain the board matrix updated. (and this has nothing to do with graphics!)
Every turn, you must update the falling block position, witch can be done by moving it’s representation in the board one line down. (or to the sides, according to the user input...)
Every time your falling block reaches another block, or the bottom line, you create a new falling block and you forget about the old one.
...BTW, this is the right moment to check the board matrix for full lines, game over condition, etc...

Now, to display the board, you may call board.draw()(or something like it...) each turn, witch only objective is to represent your board in a graphical way (you can even represent your board as text if you want (and you should, at least in the beginning to test/debug)...).
This way you will need to redraw every turn, but your game state will be safely maintained elsewhere.

Hope this helps.

Share this post


Link to post
Share on other sites
What is a matrix? I couldnt even find it in my C++ Reference book.

Soem questions on SDL: I have this book called Focus on SDL, but it keeps talking about different methods. Is this book a reference book or a tutorial? I really cant tell. Anyway, it seems like alot of topics in the book arent even explained. There is alot of terminology in the book but the back cover says all you need to know is C++.

Are there any books that explain basic graphics programming while introducing the API as well? Seems like the SDL book is just "call this method to do this, call that one to do that" rather than explaining it. Also there are no game concepts introduced, it just tells you some of the API functions that seem to come right from the SDL webpage.

Should I just learn windows programming and openGL or something else rather than SDL?

Share this post


Link to post
Share on other sites
Quote:
Original post by Gink
What is a matrix? I couldnt even find it in my C++ Reference book.


A matrix is just an array of arrays (jagged array). This is a char matrix: char matrix[3][3]; You specify it with two [] operators. You can do that or use a std::vector< std::vector< char > >.

Quote:
Soem questions on SDL: I have this book called Focus on SDL, but it keeps talking about different methods. Is this book a reference book or a tutorial? I really cant tell.


It's kind of both, just a no-non sense approach to focusing on what SDL is. Note that the book was written for SDL 1.2.3, which SDL is now at 1.2.8, so quite a few things have been updated since then.

Quote:
Anyway, it seems like alot of topics in the book arent even explained. There is alot of terminology in the book but the back cover says all you need to know is C++.


Given the books size, the author, TANSTAAFL, only focused on a select set of things, which I think he says somewhere in there. It's not going to teach you a lot of the stuff it covers because if you venture into that kind of stuff, then you are expected to know about it arleady. So for example, if all you knew was C++, then you can easily learn how to use sound though SDL_Mixer, but doing the raw mixing with the SDL routines is beyond your knowledge.

Quote:
Are there any books that explain basic graphics programming while introducing the API as well? Seems like the SDL book is just "call this method to do this, call that one to do that" rather than explaining it. Also there are no game concepts introduced, it just tells you some of the API functions that seem to come right from the SDL webpage.


One thing that I must point out that has to be said when this always comes up is that the book is called "Focus on SDL" [smile] It's not a focus on graphics programming nor focus on game programming (which the other books in the series are for) so that's why it tells you what this method does and what that method does. As for explanation, SDL being open source software everything is avaliable for you to take a look if you need more explanation to see for yourself. There aren't really any resources that say the "Why" of SDL.

Quote:
Should I just learn windows programming and openGL or something else rather than SDL?


If you do just give up, good luck. SDL is by far one of the easiest as well as funnest APIs to work with and learn. It is a bit dated since it's written in C style, but if you know C++, you should be able to get how it works. It just takes time and patience. [wink] Best of luck!

Share this post


Link to post
Share on other sites
SDL seems easy to use, but how often is it used in real games? I want to make some 2d games, which SDL seems like it would be good at, but im having trouble just making a cheap tetris game.

Is it possible to make a tetris game without using matrixes? Thats what I was trying. I was going to scan the pixel colors in the whole image after every block dropped, but it doesnt erase the position it was previously at.

right now I have it so it drops the blocks at random positions, and you can flip the blocks using the up arrow like in the real one. Its really frustrating though because Im not even sure how to erase the leftover image. I know I could just call a function that writes over it, but it seems that would cause problems if i move a block under another block and it ends up erasing a block that isnt its leftover image

Share this post


Link to post
Share on other sites
Quote:
Original post by GinkSDL seems easy to use, but how often is it used in real games?


Unreal 2003 and 2004 both use SDL for the linux/mac ports [smile] That's more than enough a reason for me to use it [wink]. Of course it uses OpenGL, OpenAL, and a few other libraries as well, so you might not be using the 2D features for that.

Quote:
I want to make some 2d games, which SDL seems like it would be good at, but im having trouble just making a cheap tetris game.


I think it's a game design problem and not a SDL API problem. You will probabally run into similar problems with other API's as well. Have a look at this neat little tetris tutorial from Aaron Cox's site.

Quote:
Is it possible to make a tetris game without using matrixes? Thats what I was trying. I was going to scan the pixel colors in the whole image after every block dropped, but it doesnt erase the position it was previously at.


Absolutly! There are a lot of different ways to make tetris. There is no universal way to do it. That is one of the big challenges of game programming, getting how you want to design your game for how you want it.

Quote:
right now I have it so it drops the blocks at random positions, and you can flip the blocks using the up arrow like in the real one. Its really frustrating though because Im not even sure how to erase the leftover image. I know I could just call a function that writes over it, but it seems that would cause problems if i move a block under another block and it ends up erasing a block that isnt its leftover image


Sounds like you will have to do some more reinventing [smile]. Just don't give up! Try something new. If you still have problems, start simple then. Just make it so you can load one block and then move it around and have it land how it should. Then make it so you can stack those single blocks. Then make it so you can clear a line. After that, it's just a matter of adding in more blocks.

It's no easy task by any means, so don't get frustrated. I've never done tetris before [grin] - I didn't even want to think about it. Instead I did breakout and pong and stuff like that. And don't listen when it's said you have to make tetris to be a game programmer - how many genres out there relate to tetris for crying out loud.

So don't give up on SDL, just you may want to try something simpler first to learn the SDL API to begin with, then on the side plan out tetris for later.

Share this post


Link to post
Share on other sites
Quote:
right now I have it so it drops the blocks at random positions, and you can flip the blocks using the up arrow like in the real one. Its really frustrating though because Im not even sure how to erase the leftover image. I know I could just call a function that writes over it, but it seems that would cause problems if i move a block under another block and it ends up erasing a block that isnt its leftover image


Okay, hold everything... It sounds like your "pieces" are just raw pixel data and you are "moving" the pixels when you move a piece.

That's bad.

The screen is just a representation of some internal data. It sounds like you have no internal data from the way you describe it. Is this correct? What you'll want to do is create some data structures that represent pieces and blocks, then draw that data to the screen using SDL graphics routines. I think that's why you are having trouble.

You see, a pixel is just a colored square (and i know you already knew that). When an object "moves" it doesn't "move" pixels. It just changes the colors of the pixels that are already there. So in a game, you just paint the pixels according to the current internal state of the data that represents the game. Theoretically, you should be able to program the internal guts of Tetris and wire it up to any number of drawing APIs. You could take the same Tetris guts and make it 2D with SDL, 3D with OpenGL, or even ASCII output direct to the console ala "Hello World". Heck, you could even have a tetris game that has no graphical output whatsoever! Everything would still work, you just wouldn't see it (and would have a rather hard time playing :-)

The internal mechanics should not change at all between the different ways of displaying it. But if my assumption is correct, you have little or no internal mechanics at this point. That is what you need to work on.

(my sincere apologies if i misdiagnosed your situation)

Feel free to ask more questions and we will help you with that.

Share this post


Link to post
Share on other sites
Hi again,

If you really don't want to use arrays (or any other way of maintaing the board state...), you should "clean" the moving block last position.
You can do this by paiting the current position of the block in the background color at the same time that you are painting the new block position.

But, once again, I do think that's no way of doin'g it...
How will you make the "contact" test? (to test if a falling block have collided with another or the bottom limit)
Will you get the next pixel color and compare pixel colors??!?!?

By the way, How do you erase lines?

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