Thread: global class object?

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    21

    global class object?

    Im creating a simple game. very simple. just as a test for the console library im working on, problem is, ive got zero experience with game writing in C++, so advice on the following would be appreciated.

    ive currently got the CApp class sorted, which is the main application. loop is setup and running too.

    Example main.cpp
    Code:
    #include "CApp.h"
    
    int main(int argc, char *argv[])
    {
        CApp *thapp = new CApp();
        delete theapp
    }
    example CApp.h
    Code:
    #include <ConLib.h>
    
    class CApp
    {
       public:
         constructor();
         destructor();
    
         ConLib c_cmd; // My own console library (just a class with functions e.g c_cmd.WriteToScreen(text) )
    
       Init();
       Execute(); <-- this is the loop, every cycle do whats in here
       Cleanup();
       Exit();
    }
    Now im wanting to add a CGame class which would have a functions such as InitGame, ShowMenu, GenerateMap, etc, think very simple roguelike. Ive properly setup this wrong, but how would i allow a function such as CGame::ShowMenu to output to the screen via c_cmd in CApp? Or should i just declare another console library object for it to use in the CGame class.

    Not sure if ive explained it right or not, kinda confused myself.

    For example my original thought was to do this:

    CApp.h -
    Code:
    #include "CGame.h"
    
    class CApp
    {
    ...
       CGame thegame;
    
    }
    CApp::OnExecute -
    Code:
    void CApp::OnExecute 
    {
    thegame.input();
    thegame.think();
    thegame.output();
    }
    CGame_Output.cpp
    Code:
    #include "CApp.h"
    void CGame::output()
    {
      // need to access ConLib c_cmd from CApp here.
    
       CApp::c_cmd.WriteToScreen("output here");
    }
    Is that last section for CGame_Output.cpp the right way to go? have i missed something?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You must must remember to model it like the real world. Since the output object is not part of the game, then game must ask: Should I create an output object? Or should the caller pass this in?
    Which makes more sense to your overall design?

    Does it make sense that whenever Game outputs something, that it should create an output object first? Or should it be told of where the output should be first?

    Btw, why are you using new in main?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    21
    You must must remember to model it like the real world. Since the output object is not part of the game, then game must ask: Should I create an output object? Or should the caller pass this in?
    Which makes more sense to your overall design?

    Does it make sense that whenever Game outputs something, that it should create an output object first? Or should it be told of where the output should be first?
    I have no idea which would be the correct method to use. In terms of it being simpler i could just create another ConLib object in the game class. But im also wondering if its possible to get er, a child class to communicate with its parent's objects. As you can tell im fairly new to C++ and im still trying to understand why this goes this way and that goes that way etc.

    Im hoping to eventually move towards game programming in general, but practicing my C++ at the same time. Now i know im not anywhere near the ability to make a fully fledged game yet, hence why i started on something relatively simple like a console library. Ive not even got to the point where i can a simple game lol, this is all a learning experience for me and im probably way over my head.


    Btw, why are you using new in main?
    Why am i using new in main.. no idea really. Came across some info on memory management. Got a little carried away. Ill switch back to previous method lol.

  4. #4
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Parent to Child communication can be done by declaring a function with a return value in the child. Then calling the function in the parent. And passing down is easy as well just requiring a variable for the function in the child. Each communication has to be declared and defined first of course.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    21
    Any chance of a quick example of that please? I learn from example.

  6. #6
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    You could write your ConLib class so that its methods are appropriately static. Then you can use a global ConLib object in your program and access its static methods everywhere like this:
    Code:
    ConLib::WriteToScreen("hello world");
    Last edited by BMJ; 09-13-2010 at 05:58 AM.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    21
    Quote Originally Posted by BMJ View Post
    You could write your ConLib class so that its methods are appropriately static; Its construction acquires a console resource, its destruction releases it. Then you can use a global ConLib object in your program and access its static methods everywhere like this:
    Code:
    ConLib::WriteToScreen("hello world");
    That's pretty much what im looking for, but no clue on how to implement it. Any keywords to google for? or helpful tutorial links or anything?

    EDIT:

    library has been edited so all the conlib class functions are now static. After including the header file, function is now listed in there as...

    static void WriteToScreen(string test); // example

    Now without instantiating the object how would i access this from anywhere?

    including the header file and doing ConLib::WriteToScreen("test"); doesnt work. or ive missed something...
    Last edited by AvidGamer; 09-13-2010 at 05:50 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Let's go back to basics then.
    What is the purpose of your output class? What purpose does it serve? How does it serve that purpose? What are you modeling it as? If it was a an object in real life, what would it be?

    Then you need to ask yourself: how does your application interact with this object?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    My guess would be call it anywhere outside of the main loop and function definitions, just like making any other variable global:

    Code:
     
    #include <stuff.h>
    ....
    ConLib c_cmd;
    
    ...
    int main(int argc, char *argv[])
    {
      ...
      ...
    }
    local:
    Code:
    #include <stuff.h>
    ....
    int main(int argc, char *argv[])
    {
        ConLib c_cmd;
      ...
      ...
    }
    never made a library before so could be wrong, but that would be where I would start.

  10. #10
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Quote Originally Posted by AvidGamer View Post
    including the header file and doing ConLib::WriteToScreen("test"); doesnt work. or ive missed something...
    It doesn't work... how? It fails to compile? It doesn't output anything?

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    21
    @Elysia -


    The conlib class is currently compiled into its own library on both windows and linux, i needed access to commands to use color in the console when using ASCII, so designed a library to provide commands.

    Win32 version of header:

    Code:
    extern "C" {
    
    	class ConLib
    	{
    	public:
    		ConLib();
    		~ConLib();
    
    		HANDLE hConsole;
    
    		static void ClearScreen();
    		static void ChangeFontColor(int background, int foreground);
    
    		static void WriteToScreen(const string& text);
    		static void WriteToScreen(int number);
    		static void WriteToScreen(const string& text, int background, int foreground);
    		static void WriteToScreen(int number, int background, int foreground);
    	
    		//void MoveCursor(int x, int y);
    
    	};
    }
    As you can see its only a very small simple library in its current state(currently rewriting as we speak, but still using same commands). On windows using MSVC10, Conlib is compiled as a .lib file, reference to the gametest project and include directories added. Generically when using ConLib c_cmds; works fine when inside the class i intend to use it in. However i would like to make c_cmds globally available, and have no concept of how to do that.

    So the question becomes, how to i make ConLib a globally available object and/or how do i access its commands via ConLib::WriteToScreen("test") for example ?

  12. #12
    Registered User
    Join Date
    Feb 2006
    Posts
    21
    Er, never mind. I had an utter noob moment. Sorted!

    ConLib::WriteToScreen("test") working now, just made some silly mistakes in my coding. *facepalm*

    Thanks for the help all, and sorry for driving you nuts Elysia :P

  13. #13
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Now if you want to get fancy you can overload the stream operators.

  14. #14
    Registered User
    Join Date
    Feb 2006
    Posts
    21
    Oh dear. Not a clue what you just said :P So that's gonna be a no then lol :P
    Gimme chance to learn lol. But yeah, care to elaborate? :P

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Object Oriented C
    By CSaw in forum C Programming
    Replies: 6
    Last Post: 07-06-2011, 05:06 AM
  2. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM