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

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

0 Members and 1 Guest are viewing this topic.

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Resource Manager
« Reply #15 on: January 04, 2011, 04:57:25 am »
Haha! Well, I think I understand pointers now that he explained it, but I might as well go back and check and make sure. ;D

On the other hand. I do not understand a thing going on inside that directory code you sent. Lol!

Is there a class in C++ that just gives some members that allow for directory mapping?

EDIT: Do you think this will work?

Code: [Select]
#define NumberImages 10

class ResourceManager
{
    std::string ImageDir;
    sf::Image Images[NumberImages];
    sf::Sprite Sprites[NumberImages];
    public:
    ResourceManager()
    {
        ImageDir = "./Images/*.png";
    }

    int loadImages()
    {
        for (int i = 0; i < NumberImages; i++)
        {
            if (!Images[i].LoadFromFile(ImageDir))
                return EXIT_FAILURE;
            Sprites[i].SetImage(Images[i]);
        }
    }
};
-Wander

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
[Solved] Resource Manager
« Reply #16 on: January 04, 2011, 09:50:15 pm »
No, i don't think it will work, you are kinda hardcoding the number of images, you dont need to.

There is a way that you load ALL the images in there, no matter the format, i'll try to describe it, but notice you must be careful as you might end trying to load another file type into sf::Image. Make sure there are only images in the directory you're selecting : )

About the sprites, you shouldn't do what you are doing, you are creating one sprite per image, instead , store only the images and no sprites.
And in the game, whenever you need the sprite you create it, or even create them all at initialization, just associate the sprites with images when you need the sprites, not on image loading time, this way you can use the same image for many sprites, and save resources : )

Write a function like this:

Code: [Select]
void ResourceManager::LoadImages(string Directory){
     WIN32_FIND_DATAA File; //Info about the file to read
     HANDLE FindHandle = INVALID_HANDLE_VALUE; //Handle to find files
     FindHandle = FindFirstFileA(string(Directory + "\\*.*")), &File); //find the first file in the directory(parameter 1) and store in file(parameter 2)

     if(FindHandle == INVALID_HANDLE_VALUE)
  return; //return because there was NO first file

     //By now, you have the first file
     string FileName = File.cFileName;
     //You HAVE to make sure it is a valid file, because the special directories "." and ".." may appear!
     //In case you obtained a good file, load it :)
     //Also, directories will be counted as files! so, they will appear!
     //No subfolders where you are reading : )
     
     if(FileName != "." && FileName != ".."){
        sf::Image img;
        img.LoadFromFile(FileName);
        ImageList.insert(ImageList.end(), &img);
     }

    //For the rest of files
    while(FindNextFileA(FindHandle, &File)){
FileName = FileData.cFileName;
        //Same verifications as the first file, and then load, pretty much the same
    }
     //Close handles
     FindClose(FindHandle);

}


Finish the function by yourself, make sure you understand, even try to make it more powerful, by googling and exploring more properties!

Hint: try to make it recursive if you need it : )

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Resource Manager
« Reply #17 on: January 04, 2011, 11:38:11 pm »
Thank you. Lol! I didn't understand any of the code on that webpage. It looked so intimidating haha. It looks a lot simpler when its put out like this, so I actually understand it now lol. XD

I tried the docs for the commands on that page but the explanations were ridiculous.

Anyway, I'm getting one error after I fixed what was there:
Quote
error: cannot convert 'std::string' to 'const CHAR*' for argument '1' to 'void* FindFirstFileA(const CHAR*, _WIN32_FIND_DATAA*)'


The code is this:
Code: [Select]
std::string Directory = dir;
Directory += "\\*.*";
FindHandle = FindFirstFileA(Directory, &File);


I can't think of any possible way to represent a directory with a char. :/
The only thing I can think of is maybe a pointer? Unless I'm mistaken.

EDIT: I tried this to try to fix it and I got a different error. I don't know how to fix this. Lol. Sorry for all the trouble I'm causing you. :/

Code: [Select]
std::string Directory = dir;
Directory += "\\*.*";
void * DirP;
DirP = &Directory;


Quote
error: 'void*' is not a pointer-to-object type
-Wander

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
[Solved] Resource Manager
« Reply #18 on: January 05, 2011, 12:08:29 am »
Code: [Select]
std::string Directory = dir;
Directory += "\\*.*";
FindHandle = FindFirstFileA(Directory.c_str(), &File);


It's as simple as that, my mistake : )
the string member function c_str() will return it as const char * !

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Resource Manager
« Reply #19 on: January 05, 2011, 12:10:31 am »
Okay. Thanks :D. Let me test it out in my code.
-Wander

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Resource Manager
« Reply #20 on: January 05, 2011, 12:24:29 am »
It works, but it keeps saying that its found a file and then I make it print the name of the file and it says the name is .

EDIT: Its in the wrong directory. I have the directory for my Images folder plugged into the function.

Code: [Select]
ResourceManager rm;
rm.loadImages("F:\\Programs\\OSP\\RTS\\v. 0.1.1");
-Wander

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
[Solved] Resource Manager
« Reply #21 on: January 05, 2011, 12:39:11 am »
Every directory contains two special files, which happen to be directories too!

.  is Itself (Current Directory)
.. is Parent Directory

if you do c:\Games\..\ you end up in C:\

if you do c:\Games\.\.\.\.\.\ you always end up in c:\Games\

if you do c:\Games\Crazy\..\Crazy2\.\..\ you end up in c:\Games\
being Crazy and Crazy2 subdirectories of Games

Hope it clarifies everything : )

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Resource Manager
« Reply #22 on: January 05, 2011, 03:11:21 am »
OH!! Okay!! I see. Thank you so much. I just finished programming the last bit of the manager. It works. I even understand pointers. ;D
-Wander

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
[Solved] Resource Manager
« Reply #23 on: January 05, 2011, 03:56:00 am »
Keep going! and keep posting news on your project :D

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Resource Manager
« Reply #24 on: January 05, 2011, 03:57:02 am »
In this thread?
-Wander

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
[Solved] Resource Manager
« Reply #25 on: January 05, 2011, 04:05:46 am »
You would get more feedback if you make a thread only for your game in SFML Projects : )

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Resource Manager
« Reply #26 on: January 05, 2011, 04:12:02 am »
Okay. If you're interested in my project, it is called The Core Passage
-Wander