I am trying to make a simple game engine that can display objects to the screen. I intend to implement a collection of Entity* where each Entity is the representation of an object, such as the image to use when drawing it on the screen, its various properties, what happens when it's clicked, etc. Of course, I'll have many more classes that inherit from Entity, but they will all follow that basic structure. My question is: should each instance of Entity (or a child class) be aware of its position on the screen, or should that be left to the class which owns the collection where the Entity* are stored, and which manages how all Entity are displayed?

Which design choice will give me the most flexibility and ease of use later on?
Thank you for your help.

Recommended Answers

All 12 Replies

Personally, I like the approach of having things as self-contained and self-aware as possible. This gives you greater flexibility because you are then essentially dealing with plug and play objects, instead of a monolithic overseer. I guess though, that the real question that should drive your decision.... is which design will require less system resources? I mean, you are building a game, that uses Entity's so the rendering engine (ogre3d maybe?) probably could use as much CPU time as possible. If you are not as concerned about resources (ie: the game will be run on pretty decent hardware), then certainly a cleaner design choice is the way to go, and having each Entity know about itself seems like the more OOP way to do things.

Thanks for the reply! Ironically, I'm using ncurses, and Entity will be a class I implement myself. Still, everything you said applies. I'll take it into consideration, but I'd like to see what other people have to say as well.

sorry for double post, bout could anyone tell me how to type tab (the TAB character) in the reply form? I'm running Firefox on Ubuntu 8.10, and whenever I hit TAB, my focus moves to the next item in the web page (submit reply button). What is the button combination to actually type the TAB character? So far I've simply typed it in gedit, and copied it to the reply form, that works.

I do it in Gvim and paste it in.... I think you'll find that it's a web-browser issue much more than the page/site. Firefox natively makes Tab move from field to field. You could consider pressing tab in gedit, and highlighting the indentation depth, and copy it to the clipboard. Then ctrl-v or whatever any time you want to tab.... a little tacky, but will do the job.

I can't think of an advantage of keeping coordinates outside of the entity, providing that you don't intend entities to be duplicated in the list. If you do intend that, it's better to split the entity into a flyweight with the position and other 'per-entity' properties and a 'resource' class with the duplicated data (which the entity holds a pointer/reference to).

But, if your using curses, I'm guessing you don't have masses of (graphical) data associated with entities.. =)

If you don't keep the co-ordinates in the class itself, you'll end up needing to give each entity it's own 'id' (and a pointer to its owner) that it can use to query its position if you ever do discover that an entity might need access to that information. if you have to do that, you might aswell of put the co-ordinates in the class to begin with.

In short, if you put the co-ordinates in the entity class, then you already have access to them from the entity _and_ from the container. If you only have the co-ordinates outside, then you might find at some point need to move them inside the class.

So, there shouldn't really be any question, if the only thing that you're worried about is future flexibility.

Immediate follow up; if the 'co-ordinates on screen' are just derived from some other properties of the entity, then there's probably no need to store them anywhere... e.g. (in OpenGL) actual screen co-ordinates are derived from object space co-ordinates and a perspective transform, so each 'entity' only needs keep it's object-space co-ordinates, the perspective transform is a 'global', and no-one keeps the screen space co-ordinates: because they get thrown away at the end of the frame, and because theres not much useful that can be done with them.

Thanks for all the replies, everyone! I am using C++ to implement this, but game development is just as relevant a topic, if not more so.
Have any of you ever used a little piece of software called GameMaker? It has the same basic architecture that I'm trying to implement. The game is composed of a World, where everything is contained, and a whole bunch of other things (like Entities). Each entity stores its position relative to the origin of the world and its x-velocity and y-velocity (this is just 2D). Each step, the Entity's x position is adjusted by its x-velocity, and similarly for the y position. Of course, the entity also stores other information, such as which image its using, the orientation of that image, other factors to consider when adjusting x-velocity and y-velocity such as friction, gravity, and maybe some forces if the game has physics.

But I'm still just a noob programmer, so I'm just trying to build an architecture for games like Pong, Tetris, Breakout, and maybe eventually a Mario clone in curses.

On a side note, I encountered an error as a result of trying to delete a size 0 array. I fixed it, but it took me much too long to trace it to the right pointer using NetBeans 6.5 debugger. On the other hand, I know that MSVC 8.0 would have pointed me there immediately. However, I'm reluctant to leave NetBeans because it is the only IDE that I've seen on Ubuntu that has code completion which can hold a candle to Microsoft's.
If anyone could give me any suggestions, whether it be another IDE with a good debugger, or a way to configure NetBeans' debugger to make it more like MSVC, I'd really appreciate it.

I've not used Gamemaker myself, but I'm quite aware of the setup. I think you might benefit from looking at SDL (http://www.libsdl.org/) rather than curses, it's got facility to create and work with a drawable surface (on any supported platform), handles audio and input methods and other kinds of useful event processing, handles image loading etc.. and it's pretty easy to get started with (somewhat easier than curses IMHO).

I develop on Linux also (Fedora 10). I use the KDevelop (http://www.kdevelop.org/) IDE. It has acceptable code completion (although it can get a bit confused sometimes) and acts as a frontend to the gdb debugger, and to the valgrind profiling suite.

I don't use the autocomplete/gdb integration myself: I dont use auto complete because on a big project it can start to get a bit memory intensive, and I don't use the gdb integration because it only does 'full project' debugging, and I usually need to debug a single target of the project at a time.

But anyway, it gets the job done, and I think for smaller projects (which would probably only have one executable target), the autocomplete+built in debugger would be quite acceptable.

If you don't find a complete IDE that works for you; you can use gdb (debugger) from the command line, or, use the ddd (http://www.gnu.org/software/ddd/) or kdbg (http://www.kdbg.org/screenshot.php) frontends to gdb.

I'm a huge fan of Code::Blocks.

Thank you all for your help so far, but I've encountered a problem. Using the getch(); function call in ncurses results in behaviour similar to that of a text editor or a normal console screen. That is, the rate at which getch(); can acquire input is linked to the typematic rate of the current platform. Furthermore, if I press and hold one key (let's say KEY_LEFT), and after several seconds, press and hold another key (KEY_UP) while still holding the first key, my program starts processing the new key, completely ignoring the fact that I'm still holding the first key.
Is there any function in ncurses that mimics the behaviour of getAsyncKeyState(VK_SOMETHING); from windows.h? Something that can check the current state of each key independently. Or, alternatively, a version of getch(); that receives KEY_DOWN and KEY_UP type events, similar to MIDI notes (if I receive a KEY_DOWN for a key, I assume it's pressed until I receive a KEY_UP for that key, and keys don't conflict with each other).
I know ncurses is capable of this because it provides similar functionality for mouse buttons. That is, it is able to differentiate between clicks, button down, and button up.

Thank you for any assistance you can offer.

use nodelay ( [yourscreen], true ); , apparently, to make getch asynchronous (i.e. it will return even if no key is pressed).

Thank you for the response, and I have tried doing that. However, though this does make getch(); return immediately regardless of the current state of the keyboard, I still encounter the problem I mentioned. If I press a key while holding another key, the program will accept the new key, but starts ignoring the old key I was holding down before. That's the problem I want to solve.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.