Thread: A few questions on game programming

  1. #1
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121

    A few questions on game programming

    Hey all, I'm working on a game with a few friends but I'm the one that is doing pretty much all the actual programming. The others are doing art and some other stuff that we need for the game. My main question is:

    I know that the main() should never really be that big but I am at a loss of how to shorten it up. Is it a huge standard to keep the main() down under 100 or is there an exception in game programming?

    This being my first full game, I want it to be cross platform. Is allegro fully compatible with both linux and windows. I am programming in FC 10 and my friends are using windows. Is there any way to be sure that when i send them the code it will work in windows as long as I am not using any linux specific functions.

    If there is any other comments you would like to make on standard game programming practice please feel free to do so.

    Thanks all.
    -- Will you show me how to c++?

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    My main:

    Code:
    //
    // WinMain
    //
    int WINAPI WinMain(HINSTANCE hInstance,
                       HINSTANCE prevInstance, 
                       PSTR cmdLine,
                       int showCmd)
    {
        //Create window for app
        //Load settings from INI
        INIReader *pReader = 0;
        try
        {
            pReader = new INIReader();
            pReader->LoadAndParse("..\\INI\\Options.ini");
        }    
        catch (INIReader::INIFileError &err)
        {
            err;
    		::MessageBox(0,"Cannot open INI file",0,0);
            return 0;
        }
        
        int iWidth = pReader->GetInt("Display","ScreenWidth");
        int iHeight = pReader->GetInt("Display","ScreenHeight");   
    
        //Really need to make an ini function to get a bool from the file
        bool bWindowed = false;
        int result = ::MessageBox(0,"Do you wish to run in fullscreen mode?","Screen mode",
                                  MB_YESNO | MB_ICONQUESTION);
    
        if (result == IDNO)
        {
            bWindowed = true;
        }
        
        pReader->Cleanup();
        delete pReader;
        
        SpaceXApp::CreateInstance();
    
        //Start the app by initializing D3D
        if(!SpaceXApp::GetInstance()->InitD3D(hInstance,
            iWidth, 
            iHeight, 
            bWindowed, 
            D3DDEVTYPE_HAL,
            "SpaceX application",
            true))
        {
    
            ::MessageBox(0, "InitD3D() - FAILED", 0, 0);
            return 0;
        }
    
        try
        {
            //Setup the app			
            if(!SpaceXApp::GetInstance()->Setup())
            {
                ::MessageBox(0, "Setup() - FAILED", 0, 0);
                return 0;
            }
        }
        catch (X3DException &e)
        {
            e.ShowError();
        }
        catch (std::exception &e)
        {
            SpaceXApp::GetInstance()->Cleanup();
            ::MessageBox(0,e.what(),"UNKNOWN EXCEPTION CAUGHT",MB_OK | MB_ICONEXCLAMATION);
            std::cout << e.what() << std::endl;
        }
        
        //Start the loop
        SpaceXApp::GetInstance()->EnterMsgLoop();
    
        //If we get here, we are done
        //Clean up the memory and COM objects
        SpaceXApp::GetInstance()->Cleanup();
    
        //Ok Windows, it's all yours
        return 0;
    }
    I should break out the sections into functions but haven't yet. My main then would consist of about 4 functions.

    Is there any way to be sure that when i send them the code it will work in windows as long as I am not using any linux specific functions.
    The APIs are different and the calls are different. It will be more of a 'how do I do this in Linux/Windows' question when porting between the two. API calls for Win32 won't work in Linux.

    The best way in my opinion is to define an interface that will not change and will define the behavior of the application. Then the implementation can be in Linux or Windows and as long as the interface does not change it will not break either of you.
    Last edited by VirtualAce; 01-09-2009 at 05:13 PM.

  3. #3
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    Thanks for the quick reply. I'm am working now into further breaking my main down into functions. Since I am using allegro It seems a lot of the stuff you have in your main is taken care of for me. Is that right?
    -- Will you show me how to c++?

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I'm not familiar with Allegro so I cannot say one way or the other. My startup is quite simple:

    • Grab basic config options from an ini file
    • Create the application singleton object
    • Initialize Direct3D
    • Setup the game (X3DApp::Setup() is a pure virtual function that must be defined in the derived application class)
    • Enter the message loop - this begins the render/update and Windows message loop
    • Cleanup when the loop exits
    • Return no error code to Windows (this is incorrect since I really should return different values depending on the error encountered)

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    wouldent mind seeing this ini parser


    int iWidth = pReader->GetInt("Display","ScreenWidth");
    int iHeight = pReader->GetInt("Display","ScreenHeight");

    how do you make it read titles from a ini and not have to loop through the whole thing?

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    169
    Quote Originally Posted by Anddos View Post
    how do you make it read titles from a ini and not have to loop through the whole thing?
    You basically read the whole file and store the values in a struct.
    This struct may look something like:
    Code:
    struct Value {
        std::string Header;
        std::string Name;
        int Value;
    };
    Wouldn't be difficult to search a value by its Header + Name fields.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    There are several approaches, however, I posted the main so you could see a sample of what one 'might' look like. Don't worry about my implementation as much as what is being done in the main.

    There is ini support in the Win32 API. I just wrote my own b/c I didn't need all of it's functionality. All you need to do is load in a file and then find the requested string and then find the right value of the requested string. You can either do this by loading in the entire file or you can open the existing file and parse it each time.

    Since it's only done during load time I don't think performance is a huge issue here.

  8. #8
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Main:
    - Use a class to process command line options.
    - Use a class to process ini settings.
    - Use a class to initialize/store game components (keyboard, mouse, gui, graphics, database, network, files).
    - Start the game loop (usually a method of the last class).
    - Cleanup

    Game Loop:
    - Logic, graphics, input, etc.

    It's all abstracted, so it should be pretty short. Shorter if you use and wrap wxWidgets or something instead of win32 api.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  2. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  3. Try my game
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-15-2004, 11:58 AM
  4. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  5. Ideas for my c++ game for semester project
    By Leeman_s in forum Game Programming
    Replies: 9
    Last Post: 12-30-2002, 01:57 AM