Public Group

# Tetris, Pt. 2

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

## Recommended Posts

##### 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.

##### Share on other sites
Quote:
 Original post by solinentI'm not really interested in using C, as I never actually learned it (went directly to C++).

Can you post some relevant code? Just so we can see how "C++" your code really is. (Only if you're interested in feedback of course.)

1. 1
2. 2
Rutin
19
3. 3
khawk
19
4. 4
5. 5
A4L
11

• 9
• 12
• 16
• 26
• 10
• ### Forum Statistics

• Total Topics
633771
• Total Posts
3013763
×