Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved] Resource Manager  (Read 11491 times)

0 Members and 1 Guest are viewing this topic.

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Resource Manager
« on: January 03, 2011, 02:43:08 am »
I'm trying to create an RTS and I know I need a resource manager, but I'm not sure how to go about making one.
Anyone have any solutions? Algorithms?
Thanks. ;D
-Wander

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
[Solved] Resource Manager
« Reply #1 on: January 03, 2011, 03:17:18 am »
Could you elaborate? What do you mean with resource manager?

What are your concrete goals for the RTS? First try to have a better idea of what you want to manage, then find the optimal algorithm to do it : )

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Resource Manager
« Reply #2 on: January 03, 2011, 03:21:28 am »
My main goal is to have a 2D, top-down RTS with textures all around. Rather high-quality textures at that.
I need it to be quick and efficient.
It will need to load textures, fonts, audio, and sprite sheets.

My main goal for the RTS is to just accomplish the game itself and learn as much as I can while I'm doing it.
-Wander

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
[Solved] Resource Manager
« Reply #3 on: January 03, 2011, 03:34:53 am »
Good thinking !

You don't seem to require a very advanced resource manager for that!
And it is definetly good to keep it simple, stupid!

You could just implement a ResourceManager class, which holds in a few containers with the data your game uses.

Because you're game will be running on the PC, and will most likely be lightweight(assuming that :) ), you could load everything you need into memory at once, (really)preferably at inititialization, to avoid desnecessary memory allocations during gameplay.

My tip for the containers:

(example for sf::Image objects)
Code: [Select]
vector< pair<string, sf::Image*> > ImageList;

then you would have a list of images, associated with a name, so you could query it later.

(Inserting a new image)
Code: [Select]
sf::Image img;
img.LoadFromFile(FileName);
ImageList.insert(ImageList.end(), make_pair(FileName, &img));


You could then make a search function, to get a pointer to the image you need, by using its name!

Hope it helps!

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Resource Manager
« Reply #4 on: January 03, 2011, 03:38:01 am »
Is there a way to do this without pointers because I never did really understand them. :/
How would it know where to get the images from?
-Wander

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
[Solved] Resource Manager
« Reply #5 on: January 03, 2011, 04:02:57 am »
I will try to explain the use of pointers generally:
(If you have doubts, feel free to add me on msn)

So, when you declare a sf::Image:

Code: [Select]
sf::Image img;

... you are allocating some memory, you can check how much using the function sizeof, like this:

Code: [Select]
sizeof(img); //sizeof(sf::Image) is also valid! Function returns size in bytes

When you do this:

Code: [Select]
sf::Image *img = new sf::Image;

...you are also allocating memory, pretty much the same way as for how much you care!

After either declaration, there is a sf::Image in your RAM, now you only need to be careful about how you use it!

With the first way, the object is bound to the created sf::Image memory address automaticly, and you may call its methods without further trouble!

Now on what comes to pointers:

A pointer is a variable too! But because a pointer holds as its value an address, its value can be anything in the RAM.
Because the pointer has this capacity to point at different adresses, you can play at will, as long as your data have integrity of course!

Code: [Select]
sf::Image img1,img2; //just two images
sf::Image *pImg = 0; //Points at nothing, the NULL
pImg = &img1; //& meaning address-of, now our pointer knows about img1;
pImg = &img2;//and now, it knows where the img2 is!

img1.LoadFromFile(""); // Calling a method normally
pImg->LoadFromFile("") //Calling a method from a pointer! This would call the LoadFromFile of img2!

(*pImg); //the symbol on the left makes the pointer behave like a normal object, so you could do:
(*pImg).LoadFromFile(""); //and still be calling img2 method!

&pImg; // The address of a pointer even lets you acess the raw bytes!

//You don't need to point at sf::Image created staticly!
pImg = new sf::Image(); //This just throws a sf::Image into memory, and keep its address for future reference!
delete pImg; //This will destroy permanently the sf::Image pImg is pointing to!


Now applying to the resource manager example:

In the vector, you keep a pointer instead of the actual sf::Image because you can later reference to it, instead of copying , which is faster!

and the resources will all be centralized, everything used it there, and thats good for your organization, specially when a lot of resources are used!

Imagine your search function like this:
Code: [Select]

sf::Image* ResourceManager::SearchImage(string SearchFileName){
       for(unsigned int i = 0; i < ImageList.size(); i++){
              if(ImageList[i].first == SearchFileName){ // first is the string , in the pair
                      return ImageList[i].second; //you return the pointer to the sf::Image you are holding
              }
       }
}


Where you called the function, you just got a fresh pointer:
use it normally as a sf::Image
Code: [Select]
Sprite.SetImage(*pointerObtained);

I hope this clarifies it a bit for you!

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Resource Manager
« Reply #6 on: January 03, 2011, 04:11:06 am »
You saved two images in one pointer. Is the pointer acting like an array or did you overwrite the first one?
-Wander

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
[Solved] Resource Manager
« Reply #7 on: January 03, 2011, 04:14:01 am »
Overwrite, a pointer holds one address, no matter what!

Commonly, a pointer to any structure has 4 bytes size in a 32 bits system, if i'm remembering well.

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Resource Manager
« Reply #8 on: January 03, 2011, 04:18:17 am »
Okay. That last edit you put in confused me.

What is SearchFileName?

Can you really just say .first, . second, .third, etc to reference a spot in a vector?

Quote
if(ImageList.first == SearchFileName){ // first is the string , in the pair

What do you mean by 'the pair'?
-Wander

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
[Solved] Resource Manager
« Reply #9 on: January 03, 2011, 04:27:11 am »
SearchFileName is just a parameter that you would pass through the search function! Check now!

Notice this:

vector< pair<string, sf::Image*> > ImageList;

So, ImageList is a vector, a dynamic array which contains in each position a pair<string, sf::Image*>.

ImageList with 2 images loaded:
ImageList[0] is a pair<string, sf::Image*>
ImageList[1] is also a pair<string, sf::Image*>

When you reference the ImageList, being i a valid position, it counts as the pair, and you can check the pair values, using first and second, that will return objects you stored. Check STL and C++ documentation for a better information : )

http://www.cplusplus.com/reference/stl/vector/
http://www.cplusplus.com/reference/std/utility/pair/

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Resource Manager
« Reply #10 on: January 03, 2011, 04:50:13 am »
Okay knowing all of this. How would the program know where to look for images and files and such? I'd have to give it a directory right?
-Wander

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
[Solved] Resource Manager
« Reply #11 on: January 03, 2011, 05:26:23 am »
Sure, implement a function that can load the images present in a whole folder! : )

http://msdn.microsoft.com/en-us/library/aa365200(VS.85).aspx

Check if that helps : ) Good night!

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Resource Manager
« Reply #12 on: January 03, 2011, 05:27:21 am »
Okay, thank you very much! I appreciate you time and help! :)
-Wander

heishe

  • Full Member
  • ***
  • Posts: 121
    • View Profile
[Solved] Resource Manager
« Reply #13 on: January 03, 2011, 05:18:05 pm »
Advice: Learn C++ first, then program games.

Sentences like "can I do this without pointers? I never really understood them" should NEVER come out of the mouth of someone who programs games.

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
[Solved] Resource Manager
« Reply #14 on: January 03, 2011, 06:16:37 pm »
Thats absolutely true! You should never skip that : )

Read and Learn a lot about everything related to gamedev until you have some confidence in your skills, then make games, and remember, start small.

But if you have time to spend in your self-taught education, i encourage you to prototype your RTS, then you will know what you need to learn from your failures!

Best of luck ;D