CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2010
    Posts
    60

    SDL Style Question

    I'm not sure if this should go here or in the graphics forum, but it's mostly a C++ style question. In my main function, I create a pointer to one instance of a class I made (representing a window). Is it acceptable to have this pointer be global? Otherwise I have to pass it to all the functions in main.cpp. In addition, do I use const in this case? Here's the code, if it helps.

    main.h:
    Code:
    #include <iostream>
    #include <string>
    
    #include "SDL/SDL.h"
    #include "SDL/SDL_image.h"
    #include "SDL/SDL_ttf.h"
    
    #include "card.h"
    #include "table.h"
    
    using namespace std;
    
    Window* gameWindow = NULL;   //Here's the global I'm unsure about
    
    int main(int argc, char** argv);
    
    bool init();
    
    int handleEvents();
    int handleLogic();
    int handleDisplay();
    
    void cleanup();
    main.cpp:
    Code:
    #include "main.h"
    
    /** Main... duh */
    int main(int argc, char* argv[]) {
        if(!init()) return -1;
    
        int runningCode = 0; //anything else signifies error
        while(runningCode == 0) {
            runningCode = handleEvents();
            if(runningCode != 0) break;
            runningCode = handleLogic();
            if(runningCode != 0) break;
            runningCode = handleDisplay();
        }
    
        cleanup();
        return 0;
    }
    
    /** Initializes SDL and the Window object */
    bool init() {
        if(SDL_Init(SDL_INIT_EVERYTHING) == -1) return false;
        gameWindow = new Window();
        return true;
    }
    
    int handleEvents() {
        SDL_Event event;
        while(SDL_PollEvent(&event)) {
            //TODO
        }
    }
    
    int handleLogic() {
        //TODO
    }
    
    int handleDisplay() {
        //TODO
    }
    
    void cleanup() {
        delete gameWindow;
    }

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: SDL Style Question

    I would pass it to all functions instead of having it as a global. Why? It's not that much work to have it as a parameter and I do think that if you start off by using a global it's hard to resist adding another one and so on.

    Frankly, the code snippet you posted gives a good old C feeling. Are you sure this is the proper structure for a game written in C++?

    I have no game programming experience but I imagine it being something like creating a game object in main, maybe calling some game object run method periodically and exit when the game object tells you so.

    The game object itself is responsible for creating/deleting/running the different game detail objects that are needed for the game to run. I.e. if you need 100 blinking stars in the game you just instantiate 100 star objects with the blinking property set and call their run method when called by main.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Oct 2010
    Posts
    60

    Re: SDL Style Question

    Okay. And yeah, there is a class for game objects already. It's a game of solitare, and I have a table class, with cards in it. The window just takes table's data and shows it. Is that too C-ish?

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: SDL Style Question

    I don't know I just got some kind of C feeling. Maybe because of the handleEvents, handleLogic and handleDisplay functions.

    As I said I have no experience in game programming but those functions didn't fit in my picture of it. Game entity objects handling their specific events, logic and of course the drawing they should do.

    All in all, don't take my comment to serious. You have the code and the overall picture, I don't. Also, if the structure isn't the most practical you will be the first to find out and do something about it and learn alot in the process.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured