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

Author Topic: Help Understanding this imageManager  (Read 2980 times)

0 Members and 1 Guest are viewing this topic.

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Help Understanding this imageManager
« on: September 15, 2012, 02:19:08 pm »
Hey guys, ive been using this imageManager, now i can use it find and what not, but I dont "fully" understand it,  here is the header and cpp file

Everything with a //---// at the end i dont understand

imageManager.h
//Image manager header
#ifndef gIMAGEMANAGER_H
#define gIMAGEMANAGER_H
//Includes
#include <SFML/Graphics.hpp>
//Body
class gImageManager
{
public:
        gImageManager();
        ~gImageManager();

private:
        gImageManager( const gImageManager& ); //---//1a
        gImageManager& operator =( const gImageManager& ); //---//1b

public:
        const sf::Texture& get_image( const std::string& filename ); //---//1c

private:
        std::map< std::string, sf::Texture > images_;
        std::vector< std::string > resource_directories_;
};
//end body
#endif
 
1a = gImageManager passes a reference of another gImageManager???
1b = I have no idea with that function
1c = why does it use so many references? can i not just remove these?

imageManager.cpp
#include <map>
#include <iostream>
#include <SFML/Graphics.hpp>
#include "gImageManager.h"

gImageManager::gImageManager() : images_(), resource_directories_() //---//1a
{
}

gImageManager::~gImageManager() //---//1b
{
        images_.clear();
        resource_directories_.clear();
}

const sf::Texture& gImageManager::get_image( const std::string& filename )
{
        // Check, whether the image already exists
        for( std::map<std::string, sf::Texture>::const_iterator ci = images_.begin();
                 ci != images_.end();
                 ++ci)
        {
                if( filename == ci->first ) //---// 1c
                {
                        //std::cout << "DEBUG_MESSAGE: " << filename << " using existing image.\n";
                        return ci->second;
                }
        }
       
        // The image doesen't exists. Create it and save it.
        sf::Texture image;

        // Search project's main directory
        if( image.loadFromFile( filename ) )
        {
                images_[filename] = image;
                //std::cout << "DEBUG_MESSAGE: " << filename << " loading image.\n";
                return images_[filename];
        }

        /*// If the image has still not been found, search all registered directories
        for( std::vector< std::string >::iterator it = resource_directories_.begin();
                 it != resource_directories_.end();
                 ++it )
        {
                if( image.loadFromFile( (*it) + filename ) )
                {
                        images_[filename] = image;
                        //std::cout << "DEBUG_MESSAGE: " << filename << " loading image.\n";
                        return images_[filename];
                }

        }*/


        std::cout << "GAME_ERROR: Image was not found. It is filled with an empty image.\n";
        images_[filename] = image;
        return images_[filename];
}
 
1a = I just dont understand the : images_(), resource_directories_() after the gImageManager::gImageManager... what does that line do??? ": images_(), resource_directories_()"
1b = Im sure this is just the de-construction of the class, but im just making sure.
1c = ??? if it isnt the first item, then return the second item????

If anyone could just quickly explain these, it would be a great help :)

Canvas
« Last Edit: September 15, 2012, 02:28:59 pm by Canvas »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Help Understanding this imageManager
« Reply #1 on: September 15, 2012, 02:46:48 pm »
1a = gImageManager passes a reference of another gImageManager???
1b = I have no idea with that function
1c = why does it use so many references? can i not just remove these?
1a - It's called copy constructor (google it).
1b - It's a assign operator, so you can make IM1 = IM2 without have some bad side effects (if one implements it right).
1c - Why shouldn't it use many references? Why do you think reference would be bad? References are a good thing and should very often be preferred over raw pointers or values.

1a = I just dont understand the : images_(), resource_directories_() after the gImageManager::gImageManager... what does that line do??? ": images_(), resource_directories_()"
1b = Im sure this is just the de-construction of the class, but im just making sure.
1c = ??? if it isnt the first item, then return the second item????
1a - It's a initialization list (google it).
1b - Yes it's the destructor, although it's unnecessary here, because STL container use RAII and release the memory them-self once the object gets destroyed.
1c - The 'first' refers to std::pair which gets used by std::map. A pair holds a first and a second value (key, value). The check is within a loop that iterates over all already loaded images. If the image was already loaded once (= filename == ci->first) then one can immediately return the image, otherwise load it newly and return it then.

Most of your questions relate to basic of some parts of C++ (and STL), thus I advise you to go back to a C++ book and read again what this is all about (specially the class section and the STL containers section).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Help Understanding this imageManager
« Reply #2 on: September 15, 2012, 03:07:16 pm »
The assignment operator and copy c-tor are private and have no {} block to prevent copying and generate compiler and linker warning if you try to copy.
You can(but shouldn't) remove some references but if you remove the sf::Texture reference then you'll get white sprites.
Initialization list is something that let's you call c-tors of bases and members before your own.
first and second are from std pair, map stores pairs of keys and values, not just values.

This code is weird... Why is there constant iterator in get image(which is not const function), why doesn't it just do find() and then check against end()(cleaner, less writing, logarithmic complexity instead of linear), why does it clear map and vector in d-tor, why is there redundant init list that calls default c-tors, why does it load and copy texture instead of using operator [].

Quote
...go back to a C++ book...
:D
Back to C++ gamedev with SFML in May 2023

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Help Understanding this imageManager
« Reply #3 on: September 15, 2012, 05:00:53 pm »
Cheers for the help guys, I don't learn from books, I have tried to read many C++ and programming books and they just don't do it for me, I learn from actual trial and error.

They maybe basic, but basic comes to anything once you know how to do it.

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Help Understanding this imageManager
« Reply #4 on: September 15, 2012, 05:55:33 pm »
You need to be careful when choosing a book, there are many C++ books (Stroustrup's book for example) that aren't focused at beginners, but rather at experienced programmers who just thought of using C++ instead of "x" programming language. There are C++ books for beginners, like Deitel's C++ book and surely others, but if you don't look for those specific books you will have to learn many things the hard way.

I started programming C++ that way, with a very qualified university teacher that even though he had a PHD and was the first one in my country to focus on OOP didn't know how to teach, so I ended up learning forcefully by reading his code and going to google for anything I couldn't sort out myself. Of course I failed with him, he was a strict professor and me knowing little of the language didn't help.

If I think of it now, I could have avoided much of the hassle I suffered back then if I had just grabbed a book and focused on learning, but I didn't want to. I am no C++ expert, but now I've learned much more than I could have on my own. If a book is too complicated just get another one focused on making you learn from scratch and you'll get it faster.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Help Understanding this imageManager
« Reply #5 on: September 16, 2012, 01:42:26 pm »
Quote
...go back to a C++ book...
:D
Seriously, I still don't get what your problem is with me recommending to read books?
Just because you don't have one, doesn't mean nobody has. So either deal with it or give a useful explanation what you think is bad about such a statement (And yes "I don't have C++ book" isn't a useful answer). :-\

I learn from actual trial and error.
You can learn quite a bit that way, but you can't get to know new features or how to do things that way. Also you'll find yourself very often stuck on problems that you can't solve on your own and then you'll need help from outside. There are many people willing to help you out, but personally I'd rather learn things on my own and only use up my time rather than having to take other peoples time away from them, just because I didn't wanted to dig deeper on my own. ;)
That doesn't mean we won't help! :D
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Help Understanding this imageManager
« Reply #6 on: September 16, 2012, 01:53:55 pm »
Trial and error may help in many situations, but for C++ it is the worst approach you can take. This language is so complex and has so many special rules and behaviors, you won't understand them without theoretical backgrounds. Even if you manage to get some code working (which is not difficult), it is very probable that you do things more complicated than necessary, that you introduce bugs and performance bottlenecks of which you aren't aware, and that your code becomes a real mess as soon as you develop something bigger. In fact I have made the same experience, and when thinking back, it would have been much wiser to look once at a good book.

I really suggest to get a good book. Believe me, you invest much less time when you learn it once correctly, it will save you hours of needless debugging and Google searching in the future. If you don't want to buy a book, there are free e-books such as Thinking in C++. But don't learn C++ from tutorials, I haven't come across a single one yet which covers C++ in-depth.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Help Understanding this imageManager
« Reply #7 on: September 16, 2012, 02:23:38 pm »
Quote
I learn from actual trial and error.
You may run into undefined behavior which compilers may not warn against and which may seem like working code or introduce subtle bugs that compiler points out in other lines. It can look really innocent and give results you expect on your compiler like:
a[i]=i++;
or
i=i++ + i++;
These 2 lines are complete garbage (examples from Stroustrup's faq).
Stroustrup's faq explains that(and many other things) in depth, you should read it.
http://www.stroustrup.com/bs_faq2.html#evaluation-order
Back to C++ gamedev with SFML in May 2023

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Help Understanding this imageManager
« Reply #8 on: September 16, 2012, 07:17:26 pm »
Cheers for all the information and stuff guys, I would go for a book, but at the moment im in my final year at uni and I have quite abit to do for many modules, that's why I don't learn from books because I haven't had time :D but its ok, I have made quite a few programs and small graphic programs in C++ without problems, I understand that C++ is a huge complex language, but I have learn't one thing about C++, you can write a function with 20 lines, you go back weeks later and you can write that function in 10 lines, books and tutorials learn you a lot of the stuff, but I still feel just coding with C++ I have learn't so much, I "may" have learn maybe some complex before something basic.

But anyways that's quite off topic from the title, But cheers for all the help. I will take a look at the C++ Ebook.

Canvas