Some Questions

Started by
1 comment, last by HVA310 16 years, 12 months ago
After spending some time learning standard practices and procedures for C++/OpenGL, I am finally ready to attempt making a simple game. While I understand about 90% of what I need to do, I still need a little help solidifying everything. I feel like I am almost there, but I am getting caught up on some of the details. Currently, I am just working through the brainstorming and designing phase. I haven't even begun to think about the specifics for coding. If I decided that I wanted to make a Tetris clone using OpenGL (2D), would I create each falling block out of squares and then texture each of them? Should each block be created out of a class that also has control of its display, texturing, and rotation? How should I store each individual block layout? In an array? I forget how the 1-hour Tetris tutorial does it, but since I would like to make this work in OpenGL, I will have to make some minor changes to fit everything together. Which sort of brings me to my next question. In terms of laying out the entire game, can I put all of the game functions within one "umbrella" class? I was thinking a general game class could be used to initialize, loop, keep score, restart and quit the game. Should I break up these functions? Should the creation and displaying of blocks be included or excluded from this class? Where does collision detection fit in with everything? Would those functions be grouped with the block functions or the game functions. If I am going completely in the wrong direction, please let me know. Any tips on the layout of my code, too, would be appreciated! Finally, I got a Win32 window and menu working and then separately, in another project, I have an OpenGL set up based around what NeHe created. If I wanted to combine these what is the best way in doing so? Am I correct to assume that the Win API opens and creates the window while OpenGL deals with what goes inside of it? I was thinking of creating a standard base class that contained all of the Win API stuff I needed. Then I could derive an OpenGL class off of that that would link the two together. Is this feasible or even correct all together? Should I do it a different way and shove it all into one class? Anyway, I have a *decent* grasp of C++ to the point where I understand classes, their functions, and inheritance (at an intermediate level). I guess I could use SDL and make my game a little bit simpler but I would really like to have the experience with OpenGL. Also, I think it might be a nice exercise to rewrite it down the road should I can switch to a 3d setup if I wanted to. Thanks for taking the time to read (and respond). Input is greatly appreciated. Excuse me if I seem to be asking a lot of the same questions everyone is used to seeing.
James
Advertisement
I think it's good that you're working through some design before starting to code. Also, I can confirm that's quite a lot of questions [smile]
But hey, remember that it can greatly help realizing how things can be done by coding short prototypes for the parts you're not sure about, to see how they work and can fit together.

Quote:Original post by JBS103
If I decided that I wanted to make a Tetris clone using OpenGL (2D), would I create each falling block out of squares and then texture each of them?


That's a good idea, OpenGL is capable of rendering textured squares/quads. Also think about how you want to move the blocks. OpenGL provides functions for moving subsequently rendered geometries, like glTranslate and glRotate. This means each block can have a single position to translate to, and an angle of rotation (for example about the z-axis). It's also possible to move the coordinates of the squares or rotate them about their center (using sin and cos functions) before sending the coordinates to the rendering calls. For a Tetris-like game which method to use is a matter of preference, rotating the coordinates yourself is intuitive (and you can use them for collision detection), if you want to hide the calculations the OpenGL functions can be used instead.

Quote:Original post by JBS103
Should each block be created out of a class that also has control of its display, texturing, and rotation?


It depends a bit on how you decide to go about the last question, if you move and rotate the coordinates yourself, then it'll make the code much cleaner if this is done encapsulated in a block class. Textures should be stored centrally (loaded once) to save texture memory, so a centralized render class that stores textures and sets OpenGL states is useful. Also, since OpenGL is state-based, meaning if a state like lighting is set when displaying one block, it'll remain when displaying the next block, if the display is made in a block class, it has to "clean up" states. Personally I would go for the centralized renderer, mostly because the rendering code can be changed under the hood without having to go through different block classes. For this game, you can have a function in the renderer like DisplayBlock that takes a reference to the block to display, gets it position/rotation/coordinates and type to determine texture. The function can be called for each visible block in the game loop.

Quote:Original post by JBS103
How should I store each individual block layout?


A fixed-size two-dimensional array, the Tetris clone in an hour topic shows this well in the NewBlock function.

Quote:Original post by JBS103
In terms of laying out the entire game, can I put all of the game functions within one "umbrella" class? I was thinking a general game class could be used to initialize, loop, keep score, restart and quit the game.


I agree, you can break up the initialization functions to within the objects being initialized. For example, the game class can decide when to initialize all blocks but the block class decides what an initialized block is like (size and rotation for example). game.Initialize calls block.Initialize (actually, constructor stuff). If the game class requires a specific set-up of the blocks, it can do this after the visible blocks have been individually initialized.

If you have a centralized renderer, the game class ties the visible blocks (passes on their data) to render to the renderer. The Loop function will do a lot, maybe more than the Initialize function, so to avoid code overflow (generally bad for the brain [grin]) break it up into smaller functions. To mention some examples; GetInput, MoveBlocks, CollideBlocks, UpdateScore, RenderBlocks.

In a Tetris-like game there are two important types of collision detection. Collision detection to detect if objects collides (touches) should be done at the moment of movement in the fashion "can this block move to there without overlapping any block that's there?". Collision detection to detect if colliding blocks changes score should be done when all the blocks have moved to detect the complete game state. If using a single block class, individual blocks don't know about each other, in that case collision detection should be done outside, in the game class. Another possibility, is that you to have a block supervisor class that holds all blocks and detects collisions between them, if you want more layers of abstraction, but don't over-complicate it!

Quote:Original post by JBS103
Am I correct to assume that the Win API opens and creates the window while OpenGL deals with what goes inside of it?


Yes, OpenGL is a graphics library only, it gets dedicated access to render graphics inside the gray client area of a window.

Lastly, I want to mention that these are opinions from my experiences, so like any solution it's not universal. And hey, good luck, it seems that you're on the right track!
Wow! Fantastic!

I appreciate the time it took to type that all out. Thats a big help.

(Yes I know I asked a lot of questions, but I wasn't expecting each one to be answered. I only wanted to give an idea as to where I was coming from :) )

Anyway... Thanks!
James

This topic is closed to new replies.

Advertisement