Tetris with SDL problems

Started by
10 comments, last by jhenriques 18 years, 10 months ago
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.
Advertisement
Make sure that you are clearing the screen at the beginning of each frame. Also make sure that you are calling SDL_Flip() at the end of each frame -and using double buffering, for that matter.
If I clear the screen then wont I lose the current blocks that accumulated on the bottom?
Yes, you have to redraw everything. So you prob have to keep track of the current drawing state of the tetris grid.
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.
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.
Even a broken clock is right twice a day...
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?
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!
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
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.

This topic is closed to new replies.

Advertisement