• 13
• 18
• 19
• 27
• 10

# Tetris, Pt. 2

This topic is 3695 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Well I finished (actually, a while ago), my game that I made. However, I would appreciate someone briefly looking through my code (which is un-commented, but I try to write code so that it should make sense, so many functions), and telling me where I should improve. It doesn't use OO, because I dislike OO programming. It has its uses, but I disagree fundamentally with it (I can't follow the code easily when I do stuff like that). Maybe when I start making larger games I'll use it, but I wasn't sure if there was any performance hit with it (so that if I learn it, its useless), so that's my first question: Is there any performance loss in intense 3D applications which involve classes? I could do a google search, of course, but would love some real-people experience. Secondly, do you have examples of how you set up a framerate? Do you use Sleep or usleep? If so, how do you calculate it (I'm using SDL, so right now, I don't even use these functions, I do a while loop, which is why I'm asking this question). Sleep tends to result in feeling less smooth, maybe it's my fault. I'm looking for code here. Finally, if you want to see the code, please post first, as it is quite long, and I don't want to post it online before I'm done (maybe I'll post it somewhere with a GPL). Some cool stuff about it so far: Basic particle system when you get a line. I didn't use textures, because I liked the look of the "blocky" rectangles. I came up with this by reading the first paragraph in the Nehe tutorial, looking at his struct, and then doing the rest myself, so I'm not sure if I'm missing a lot of performance here. Uses the tango color scheme, because I'm too lazy to think of my own, and I didn't want to use primaries. Is cross platform, I developed this on both ubuntu and windows, it was fun (getting it to work on Windows took a whole day, see below, coding was maybe 2 days!) Uses freetype to draw text. Please post and I'll PM you some code, or binaries, but I doubt that's allowed here. I want to GPL it, but I have no idea how to, so for now, I will PM people. EDIT: Finally, I'm making a 3D car game, mostly focusing on collision detection. I want to make un-even ground (otherwise it would be so easy to create, it wouldn't be worth the effort). To do this, I need to detect collisions on all four wheels, then rotate and transform the car appropriately. However, I have little idea how to do this, other than making the track and then generating a height-map of the track in blender later. However, I don't want to take this route, so please help me! Thanks.

##### Share on other sites
Quote:
 I could do a google search, of course, but would love some real-people experience.

And google search would lead you to some articles published by some very smart people who discuss impact of C++ features on memory alignment and L1 cache performance, as well as strategies to resolve them.

I found those articles quite educational, since they were written by some pretty experienced people based on their real-world experience with optimization of high-profile titles.

Yes - C++ language features can produce overhead compared to equivalent in C.
No - these bottle-necks are not where you'd expect them.
Maybe - performing communication between many classes needs to pass this pointers around, which is where C wouldn't necessarily have this "overhead"

But above all - it's the absolutely last place to look for when resolving performance issues.

##### Share on other sites
Quote:
Original post by Antheus
Quote:
 I could do a google search, of course, but would love some real-people experience.

And google search would lead you to some articles published by some very smart people who discuss impact of C++ features on memory alignment and L1 cache performance, as well as strategies to resolve them.

I found those articles quite educational, since they were written by some pretty experienced people based on their real-world experience with optimization of high-profile titles.

Yes - C++ language features can produce overhead compared to equivalent in C.
No - these bottle-necks are not where you'd expect them.
Maybe - performing communication between many classes needs to pass this pointers around, which is where C wouldn't necessarily have this "overhead"

But above all - it's the absolutely last place to look for when resolving performance issues.

I had a nice post in reply, but took too long, so I'll just say that I cannot find any good resources. Could you please point me to specific ones? Maybe I'm not using the right keywords...

##### Share on other sites
PPT presentation

There is essentially no instruction overhead between C++ and C. The differences however come from cache performance due to classes and abstractions.

Note that most of the above is applicable only to some very tight loops and special cases, not as a way to design entire application.

But as said, these are some very low-level details.

##### Share on other sites
Zomg, I started programming Tetris too, also with Rects instead of textures and also with the tango colour scheme! When I was reading this post I was like "wow" haha. I do use OOP though, since I wrote this as a practise to learn more about OOP. :) Where is your code though? I can't look briefly in it like this :P

##### Share on other sites
Quote:
 Original post by solinentIs there any performance loss in intense 3D applications which involve classes?

1. Using C++ classes surely won't be slower than emulating class behaviour yourself with C. So if you want to use OO at some point, just use the C++ classes. That's what they're for.

2. You are writing a tetris game and worry about performance? Are you serious? You should have no problem to gain 1000 fps on today's hardware, even with little to no "optimization" at all.

##### Share on other sites
Quote:
Original post by DevFred
Quote:
 Original post by solinentIs there any performance loss in intense 3D applications which involve classes?

1. Using C++ classes surely won't be slower than emulating class behaviour yourself with C. So if you want to use OO at some point, just use the C++ classes. That's what they're for.

1. I wouldn't emulate it, I would simply write structured code (functions, not classes/methods). My question is more of: does c++ simplify your code when you use classes? ie. it would be the same file for two codes that do equivalent things (logic, etc) but one uses classes.
Quote:
 2. You are writing a tetris game and worry about performance? Are you serious? You should have no problem to gain 1000 fps on today's hardware, even with little to no "optimization" at all.

2. Ha! No, my code runs fine, especially since I'm using opengl. I plan on making a car game, and maybe I'll push my 8800GT to its polygonal limits (maybe 50k polys per car, only one car for now, maybe I'll add some basic AI later), but I'm asking as I'm interested in the process.

Finally, I'll PM you, c4c0d3m0n. I'll include linux-x86 binaries, but if you require Windows binaries I also have those. Let me just zip it up. I'll have the code, but don't expect too much. I'll add a some sort of licence maybe, I'm proud of it.

Oh, the game doesn't end because I wasn't sure what the logic was to that, and I'm too lazy to add in menus and stuff to restart and pause, since I'm moving on to 3D already.

You'll have to install some libs:
freetype2
SDL
SDL_image
and opengl, of course.

Let me just zip it up nicely. Maybe I'll make a make file if I can get that to work.

##### Share on other sites
Quote:
Original post by solinent
Quote:
 Original post by DevFred1. Using C++ classes surely won't be slower than emulating class behaviour yourself with C. So if you want to use OO at some point, just use the C++ classes. That's what they're for.

1. I wouldn't emulate it, I would simply write structured code (functions, not classes/methods).

I assume you have some kind of data structure for several things in your game, for example the tetraminoes. Many beginners write code to rotate a tetramino, which might look like this in your code:

void rotate(Tetramino& tetramino){    // do magic to rotate the tetramino}

If you chose to go OO and make a class Tetramino, then the function becomes a member function which might look like this:

void Tetramino::rotate(){    // do magic to rotate the tetramino}

What is important to realize here is that in both cases you pass a reference (a pointer) to the tetramino you want to rotate. So the common argument "Uh C++ is slower than C because the this-pointer has to be passed" is irrelevant, because without C++ you explicitly have to pass the reference yourself, WHICH IS JUST AS "SLOW".

Same goes for virtual functions: "Uh there's one level of indirection, so your game will be slow". If you emulate virtual functions by some means of switch/table/whatever in C, it will be JUST AS "SLOW" as the virtual functions built into C++. But if you need that kind of behavior, it's kinda pointless to discuss how slow it is.

Ever heard of the von-Neumann-bottleneck? Reading memory is slow. Variables reside in memory. So you should not use variables in your game, or it will be slow, right? ;)

Quote:
 Original post by solinentMy question is more of: does c++ simplify your code when you use classes?

If you know how to write good, clean OO code, then yes.

##### Share on other sites
Quote:
Original post by DevFred
Quote:
Original post by solinent
Quote:
 Original post by DevFred1. Using C++ classes surely won't be slower than emulating class behaviour yourself with C. So if you want to use OO at some point, just use the C++ classes. That's what they're for.

1. I wouldn't emulate it, I would simply write structured code (functions, not classes/methods).

I assume you have some kind of data structure for several things in your game, for example the tetraminoes. Many beginners write code to rotate a tetramino, which might look like this in your code:

void rotate(Tetramino& tetramino){    // do magic to rotate the tetramino}

If you chose to go OO and make a class Tetramino, then the function becomes a member function which might look like this:

void Tetramino::rotate(){    // do magic to rotate the tetramino}

What is important to realize here is that in both cases you pass a reference (a pointer) to the tetramino you want to rotate. So the common argument "Uh C++ is slower than C because the this-pointer has to be passed" is irrelevant, because without C++ you explicitly have to pass the reference yourself, WHICH IS JUST AS "SLOW".

Same goes for virtual functions: "Uh there's one level of indirection, so your game will be slow". If you emulate virtual functions by some means of switch/table/whatever in C, it will be JUST AS "SLOW" as the virtual functions built into C++. But if you need that kind of behavior, it's kinda pointless to discuss how slow it is.

Ever heard of the von-Neumann-bottleneck? Reading memory is slow. Variables reside in memory. So you should not use variables in your game, or it will be slow, right? ;)

Quote:
 Original post by solinentMy question is more of: does c++ simplify your code when you use classes?

If you know how to write good, clean OO code, then yes.

Thanks. I'm not really interested in using C, as I never actually learned it (went directly to C++). My code probably compiles in C, maybe not the stucts, but yeah.

You're probably right, OO has its advantages.

I do have structs (which are just classes, right? just shorthand) in my code:
 struct tetrominoe {    int x,y;    int width, height;    bool grid [4][4];    TILECOLOR color; };

and another one for the particles.

Thanks for the insight. I'll start designing classes for my car game then. I'm still very confused (more of just I don't know what to do) on how to make collision detection work outside of bounding boxes (which wouldn't work for ground-car collisions, and would be cumbersome for car-collision detection, since it is much longer than it is wide).

I have the tetris Makefile ready, btw. I learned about that in the last 20 mins, and also about why object files are useful (before I never used a make, I always compiled with g++).

So I think first I'll try splitting up my files independant parts (so that I may reuse some of the stuff I made already), and then start 3D stuff.

This is all really awesome, I could never get my head around 3D game-design so far, but making Tetris in 2D with ogl really helped me.