Note that there are some explanatory texts on larger screens.

plurals
  1. POOrganization of code for a game using SDL
    primarykey
    data
    text
    <p>I've been using SDL for some days now, and I decided after following some tutorials to start developing my own clone of Galaga. However, I had some difficulty trying to find a proper layout for my code.</p> <p>For example, I have a Spaceship class defined as follows:</p> <pre><code>class Spaceship : public Sprite { public: Spaceship(SDL_Surface *surface); Spaceship(const char *filename); void handleEvent(SDL_Event *event); }; </code></pre> <p>where Sprite is a base class that holds the position on the screen and so on. My constructor would be something like:</p> <pre><code>Spaceship::Spaceship(SDL_Surface *surface) : Sprite(surface) { m_y = Game::screenHeight() - m_surface-&gt;h; //positions the ship at the bottom } </code></pre> <p>From what I've seen it's not possible to use Game::screenWidth() [static class] because I'd need to include "game.h", which is basically the main game class and includes "spaceship.h", creating basically an infinite loop (I've tried using #ifndef etc. with no success).</p> <p>So, here comes my questions:</p> <ul> <li>Is it possible to achieve this kind of result?</li> <li>If it isn't, how should I organize my code to avoid this kind of problems? Should I create a Screen class that I use to store information regarding the screen etc. and include it from both game.h and spaceship.h?</li> </ul> <p><strong>EDIT:</strong> I found a way to overcome the first problem (I just added the "game.h" include in the cpp file and not in the header file). However, I still wonder what is the best choice of design in this case.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload