Skip to main content
gamedev.net
🔒 Locked

2D engine

Started by SCV Mar 8, 2009 at 2:48 PM 1 replies 800+ views
Original Post
SCV
SCV
hi everyone, few days ago i started to doodle some structure of graphical core for 2d games, but somehow i got stuck at the part where the logic of graphical objects is forwarded to the instance of the engine that draws it on the screen. let's say i consider in my engine a class that contains all the sprites and some functions for adding them, removing them, drawing etc. then i have some graphical objects with logic like coordinates etc. and a sprite id. how do i forward these coordinates to the instance of the graphical core so that i wont have to care about it anymore ?(it will always forward itself and i wont have to write another line)? is there some object manager necessary? whats the convention?
Ruzhyo2000
Ruzhyo2000
I am trying to do something similar. My engine class, by itself, however, is completely seperate from any classess that directly associate with data or functions for creating, adding, or managing sprites. My state classes however do more closely associate with these functions and data, so what we are doing is probably very similar.

I will give you an example for how mine is working. For example, let's say I have an enemy that is at a certain tile on the map, and his appropriate sprite needs to be displayed. What is necessary here is the image that the sprite will be taken from, the x and y offset of the sprite, and the height and width of the sprite.

I keep a pointer to the SDL_Surface holding the image data (I am using SDL btw) inside an object of the Spritesheet class, which is a data member of the 'Leve_State : public State' class (since the image data will need to be in memory for as long as the player is in the level). The Spritesheet object manages getting the x and y, height and width for each sprite (since all sprites in the spritesheet are the same height and width).

When the enemies need to be drawn, they are gone through one by one (they are all in a vector). If the enemy is not on the screen, it is ignored and not rendered. When it renders the enemy, the function uses the enemies' x and y positions on the tiled level map to determine where to place the sprites, and looks at two ints, sprite_c and sprite_r (for sprite column and sprite row) to determine what sprite to place. All this information is passed as parameters to the Spritesheet::drawSprite() function (that is, where to place the sprite and which column and row on the Spritesheet's source image the Sprite is from). The Spritesheet handles determining, by knowing the height and width of it's sprites, where exactly on the source image the sprite to draw is located.

I don't know if this is the convention, or if this is even good practice, but it seems to be working really good for me. I designed Spritesheet so that it throws exceptions of a non-existant sprite is called for or if draw is called without an image being loaded, ect, and the enemies on the screen merely point to or use references to objects (that are loaded when the level is loaded) that contain not only the sprite information (column and row) but their behavioral data, base stats, ect. This way I do not have multiple of the same kind of enemies with data members that are exactly the same.
SCV
SCV
hmmmm, intresting. its maybe not exactly what i ment but it inspired me a lot.. thx

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.