Tutorial: Tetris in c++ from scratch

Started by
38 comments, last by Loover 15 years, 3 months ago
Quote:Original post by MJP
Quote:Original post by lmelior
And lastly, technically your code is Windows-only, so to say that I can "use the [compiler/IDE] of my choice" is not really accurate. :) If you need any clarification for the corrections above, feel free to ask.

Since he's using SDL all of the code *should* work fine on the other supported platforms...well except for that "WinMain" anyway. It's been a long time since I used SDL but I used to just declare an "int main" and SDL would map that to its internal WinMain. I can't imagine they would take that out, and it's an easy change to make if the tutorial is going to be considered multi-platform.

True enough, it should, though I believe those #pragma preprocessor directives are also windows-only. I'm not good enough to spot anything else just by looking, and I haven't tried compiling the code to look for more, as the proprietary RAR archive format is not friendly to Unix either.

EDIT: I guess those would just be ignored anyway, so perhaps WinMain is the only thing.
Advertisement
lmelior, that's what I call a a stunning and featureful effort! Thank you very much! "Gracias"! :) I've just fixed all the mistakes you found. What a beautiful feedback!

MJO, I've changed this first paragraph and the title. What do you think about this one: "Tetris tutorial in C++ focused in game logic"

About the platform... I have added this paragraph:

Quote:
Here it is the complete sourcecode (including SDL) ready to compile in Visual C++ Express Edition under Windows platforms. In "Release" folder there is also an executable file just in case you want to try it directly.

Other platforms:

The sourcecode itselfs is almost platform independent but it dosen't come with a "makefile". However, the first version of this tutorial (that was written in plain c) was working perfectly under Linux, so you can try to port it yourself. In order to port it to linux:

- Quit the "pragma preprocessor" directives and properly link the "SDL" and "SDL_GfxPrimitives" libs in the makefile.
- Change the WinMain method to "int main()"


Thank you very much!


Quote:the proprietary RAR archive format is not friendly to Unix either.


Now I'm using zip format, but I can't remember if it is possible to open a zip file under linux easily. I remember the .gz / .gzip extensions under linux, but I don't know if they are just the same zip files that Winrar or Winzip create.
Quote:Original post by Loover
Now I'm using zip format, but I can't remember if it is possible to open a zip file under linux easily. I remember the .gz / .gzip extensions under linux, but I don't know if they are just the same zip files that Winrar or Winzip create.

Zip is no problem, at least not if you use the default deflate compression. It supports other compression algorithms that may give the Linux implementation of zip trouble, I'm not sure. I've never had a problem though, and I can verify that it doesn't have a problem with yours.

When I get a chance I'll tinker around to see what it takes to compile. I don't think I can link to the provided SDL files, but I've got them installed. SDL_gfxPrimitives is deprecated and replaced by SDL_gfx. It seems like it should be just a drop-in replacement, however, since you only seem to use boxColor. Should be fun.
Quote:Zip is no problem, at least not if you use the default deflate compression. It supports other compression algorithms that may give the Linux implementation of zip trouble, I'm not sure. I've never had a problem though, and I can verify that it doesn't have a problem with yours.

When I get a chance I'll tinker around to see what it takes to compile. I don't think I can link to the provided SDL files, but I've got them installed. SDL_gfxPrimitives is deprecated and replaced by SDL_gfx. It seems like it should be just a drop-in replacement, however, since you only seem to use boxColor. Should be fun.


Here you have the first version I made of this Tetris clone, under Linux. It is plain c. I'm not using linux currently so I can't check if it stills works, but it should.

The history is as follows:
- I made the first version for a subject I had 2 years ago.
- Last saturday: I found it in a lost folder. I was bored and I just ported it to c++ and tried to compile it using vc++ express edition.
- Last sunday: I said myself... what about writting a tutorial? Then bought "gametuto.com" in Dreamhost and I created the blog using wordpress and some plugins. Then I started the hard process of making the tutorial.
- Today I posted it here and started to receive your valuable feedback :) I think I've fixed almost all the details all you mentioned, except using "const". I think is too late for me to start using consts in c++ :P

If you are able to compile the current version in linux, please tell me, I will credit you in the tutorial and you will become rich :)

Good night!
Quote:Original post by Loover
Here you have the first version I made of this Tetris clone, under Linux. It is plain c. I'm not using linux currently so I can't check if it stills works, but it should.


That works as-is, with the small exception that I used gcc instead of bgcc. More importantly, I wanted to report that the tutorial version works with these changes exactly:

1. Comment out the instances of the following include in Board.h and Game.cpp:
//#include "windows.h"

2. Change the WinMain line as you've already mentioned in your tutorial to:
int main()

3. Change the SDL includes in IO.h to:
#include <SDL/SDL.h>#include <SDL/SDL_gfxPrimitives.h>

So I was mistaken in a previous post above, SDL_gfxPrimitives the package is deprecated, but it has retained the same header file name. Also as I mentioned the #pragma comment directives are just ignored, so no need to do anything about those.

And that's it! Well, almost, you do need to have libsdl1.2-dev and libsdl-gfx1.2-dev installed, and you need a Makefile:
CC=g++CFLAGS=-cLDFLAGS=-lSDL -lSDL_gfxSOURCES=Main.cpp Game.cpp Board.cpp IO.cpp Pieces.cppOBJECTS=$(SOURCES:.cpp=.o)EXECUTABLE=tetrisall: $(SOURCES) $(EXECUTABLE)$(EXECUTABLE): $(OBJECTS)	$(CC) $(LDFLAGS) $(OBJECTS) -o $@.cpp.o:	$(CC) $(CFLAGS) $< -o $@clean:	-rm -f $(OBJECTS) $(EXECUTABLE)


The proof:

Free Image Hosting at www.ImageShack.us
Great! :) Linux users will be happy.

Can you please make a gzip version ready so I can upload it? Please modify the header files crediting you and give me the link to your webpage so I can write a post and link to you.

Did you changed something in the sourcecode? The pieces hasn't got "squares". Have you noticed? I used a "-1" substraction for that, maybe you changed that, or maybe the new version of the primitives is different.
				mIO->DrawRectangle	(mPixelsX + i * BLOCK_SIZE, 									mPixelsY + j * BLOCK_SIZE, 									(mPixelsX + i * BLOCK_SIZE) + BLOCK_SIZE - 1, 									(mPixelsY + j * BLOCK_SIZE) + BLOCK_SIZE - 1, 									mColor);


Thank you very much!
I cant imagine showing you guys some of my game code, Id be bombarded.
Quote:Original post by ortsa
I cant imagine showing you guys some of my game code, Id be bombarded.


And your code would be better for it [smile]

Quote:
... cross platform stuff ...


According to the documentation, you should use "int main(int, char **)" as your main declaration - even if you don't use the parameters.

This is the most portable way to do it. Under windows, SDL #defines main to SDL_main, and then calls this function from SDL_main.lib (which provides WinMain()). I believe something similar happens under on Macs.

Quote:
3. Change the SDL includes in IO.h to:
#include <SDL/SDL.h>
#include <SDL/SDL_gfxPrimitives.h>


The portable way to include SDL headers is (according to the documentation):
#include "SDL.h"

The path to the SDL include directory can be passed to the compiler (e.g. -I with gcc).
Thank you rip-off, I've uploaded a new version with "ifdefs". Now it's not necessary to change anything in order to compile it under Linux. You just have to run make ;)

The same for the windows version, unless for vc 2008 express. But it should work with other compilers too.

Now, I'm currently working hard in a new tutorial. This time I'm using IndieLib in order to create a map editor that dosen't use tiles, but directly images that you can tint, rotate, scale in different parallax layers, with zooms, etc. Like in Braid or Aquaria games.

Thanks again to all you for your valuable feedback!


This topic is closed to new replies.

Advertisement