|
Title: Making game resources global Post by: BlueSweatshirt on September 02, 2009, 10:19:01 AM Okay, so, here I am. Improving my engine. Making things a bit more organized, etc.
Life is well, until I decide I want to make my game resources load through a resources.h/resources.cpp file. I have resources.h that declares BITMAP* buffer; and prototypes load_resources() and unload_resources(). Then data is is placed into the bitmap, and prototypes are declared in my resources.cpp file. First off, when I try to include resources.h in both my main.cpp file, and my resources.cpp file, I get a redefinition error of 'buffer'. I have a feeling I'm going entirely the wrong way with this. Because if I wanted all the resource variables to be global I'd have to define them outside of a function. And at the moment, I'm calling my _resources() functions inside of main. I can't just call functions outside of a function, so how should I do this? Classes? Title: Re: Making game resources global Post by: Aquin on September 02, 2009, 10:34:21 AM I admit... I cheat.
For example, I would have a resources.h file, but it would simply define a list of methods that allow me to do what I need for the game. Inside the resources.cpp file, I place all the variables that will be used globally. I then alter/get those variables using the functions. --- Yeah you probably should be using static variables in your class and then wherever you need it you can just use the class anywhere. But where is the fun in that? ::) Title: Re: Making game resources global Post by: Ivan on September 02, 2009, 10:53:53 AM http://en.wikipedia.org/wiki/Singleton_pattern
Title: Re: Making game resources global Post by: Schtee on September 02, 2009, 10:54:54 AM Yeah, I'd have a class to manage your resources. I have a GetImage(string) function that checks a map<string, Image> to see if it's in it - if it isn't, it loads it in and returns it, if it is, it just returns it.
Also, with the redefinition problem, I believe you want to google "include guards". Title: Re: Making game resources global Post by: BlueSweatshirt on September 02, 2009, 10:57:54 AM Hahah! :D
Static variables+classes it is. After a bit of work, things are functioning properly. Thanks. :epileptic: @Schtee It's weird, my IDE(C::B) automatically places in include guards, but they don't ever seem to work. (ifndef, define, endif) Title: Re: Making game resources global Post by: BlueSweatshirt on September 02, 2009, 12:23:19 PM Hello everyone.
I'm back with more trouble~ In ship.cpp when I try to declare the following: sprite = *resources.gfx_player; I get an error, which is error: 'resources' was not declared in this scope. Even though I declared 'resources' in main.cpp as an instance of my resource manager class.(and not declared in the 'main' function) How can I solve this? Title: Re: Making game resources global Post by: Impossible on September 02, 2009, 12:26:03 PM http://en.wikipedia.org/wiki/Singleton_pattern Say no to Singleton. Title: Re: Making game resources global Post by: Average Software on September 02, 2009, 12:32:08 PM Okay, so, here I am. Improving my engine. Making things a bit more organized, etc. Life is well, until I decide I want to make my game resources load through a resources.h/resources.cpp file. I have resources.h that declares BITMAP* buffer; and prototypes load_resources() and unload_resources(). Then data is is placed into the bitmap, and prototypes are declared in my resources.cpp file. First off, when I try to include resources.h in both my main.cpp file, and my resources.cpp file, I get a redefinition error of 'buffer'. I have a feeling I'm going entirely the wrong way with this. Because if I wanted all the resource variables to be global I'd have to define them outside of a function. And at the moment, I'm calling my _resources() functions inside of main. I can't just call functions outside of a function, so how should I do this? Classes? If you want globals variables in this manner, you need to declare them as "extern" in the header, and put the actual variable in the cpp file, like so: Header: Code: #ifndef RESOURCE_H #define RESOURCE_H extern BITMAP whatever; #endif Source: Code: #include "Resource.h" BITMAP whatever; Quote from: Ivan Say no to Singleton. Seconded. Singleton is widely considered to be an anti-pattern now. Title: Re: Making game resources global Post by: BlueSweatshirt on September 02, 2009, 12:35:56 PM I'm not using Singleton, actually. 8)
resources.h Code: class resource_manager { public: BITMAP* buffer; BITMAP* gfx_player; void check_resource(BITMAP* bmp); void load_resources(); void unload_resources(); }; @Average Software Hmm, I could retrofit that for my resource_manager class. Thank you, I'll try it. Title: Re: Making game resources global Post by: Ivan on September 02, 2009, 12:36:00 PM You're an anti-pattern.
Title: Re: Making game resources global Post by: BlueSweatshirt on September 02, 2009, 12:55:07 PM If you want globals variables in this manner, you need to declare them as "extern" in the header, and put the actual variable in the cpp file, like so: Header: Code: #ifndef RESOURCE_H #define RESOURCE_H extern BITMAP whatever; #endif Source: Code: #include "Resource.h" BITMAP whatever; Quote from: Ivan Say no to Singleton. Seconded. Singleton is widely considered to be an anti-pattern now. I ended up having issues with my resource_manager class, and just using straight global variables. Thank you Average Software. :) |