SDL_Surface* load_image( std::string filename ) Question

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
GameDevver4Evr
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Mon Mar 08, 2010 9:34 pm

SDL_Surface* load_image( std::string filename ) Question

Post by GameDevver4Evr »

Hey guys, this might sound stupid, but im not sure exactly on this.

is there any hope of using this function, or similar within a class?, as in defining it within a class.

SDL_Surface* load_image( std::string filename )
{
return 0;
}

or will it always need to remain Global?, sorry if it's a stupid question.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: SDL_Surface* load_image( std::string filename ) Question

Post by Ginto8 »

The short answer: it depends. You can have it hidden somewhere in the depths of your graphics engine, lurking like a troll under a bridge, or you could lightly encapsulate it in some other class that adds certain functionality to your surfaces. I could go even more into the possibilities, but I'm tired and don't really want to right now; but you get the point. ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: SDL_Surface* load_image( std::string filename ) Question

Post by eatcomics »

Well like ginto said, it depends. I would usually have a function to load them like that, then load the textures I need at the beginning so that I can access them with my class which would have a pointer to that surface
Image
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: SDL_Surface* load_image( std::string filename ) Question

Post by dandymcgee »

Yes, I always put it into a class. In the past I've used something like

Code: Select all

Renderer.LoadSurface( "example.png" );
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: SDL_Surface* load_image( std::string filename ) Question

Post by XianForce »

What I did is encapsulated an SDL_Surface* within it's own class so I had something like this:

Code: Select all

class Surface
{
private:
     SDL_Surface* m_pSurface;
public:

bool LoadImage(std::string filename);
};
So basically I'd have it load in the file, then assign whatever it would normally return, to m_pSurface. If m_pSurface is NULL, return false, otherwise return true =D
GameDevver4Evr
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Mon Mar 08, 2010 9:34 pm

Re: SDL_Surface* load_image( std::string filename )[Solved]

Post by GameDevver4Evr »

Cool, thanks all for the replies, and thanks Xian, that was kinda of what I was looking for!
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: SDL_Surface* load_image( std::string filename ) Question

Post by dandymcgee »

XianForce wrote:What I did is encapsulated an SDL_Surface* within it's own class so I had something like this:

Code: Select all

class Surface
{
private:
     SDL_Surface* m_pSurface;
public:

bool LoadImage(std::string filename);
};
So basically I'd have it load in the file, then assign whatever it would normally return, to m_pSurface. If m_pSurface is NULL, return false, otherwise return true =D
I have personally used this approach as well, and I've found it works great for smaller projects where one doesn't necessarily need a complete rendering or video class. :)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: SDL_Surface* load_image( std::string filename ) Question

Post by XianForce »

dandymcgee wrote:
XianForce wrote:What I did is encapsulated an SDL_Surface* within it's own class so I had something like this:

Code: Select all

class Surface
{
private:
     SDL_Surface* m_pSurface;
public:

bool LoadImage(std::string filename);
};
So basically I'd have it load in the file, then assign whatever it would normally return, to m_pSurface. If m_pSurface is NULL, return false, otherwise return true =D
I have personally used this approach as well, and I've found it works great for smaller projects where one doesn't necessarily need a complete rendering or video class. :)
Well I have a Video class, but it just takes a reference to my surface class instead of an SDL_Surface*... I decided to split it, because I thought that loading the files had less to do with the video system, than actually rendering them =p
User avatar
WSPSNIPER
Chaos Rift Regular
Chaos Rift Regular
Posts: 145
Joined: Sun Jan 03, 2010 6:19 pm
Current Project: top down shooter
Favorite Gaming Platforms: ps3
Programming Language of Choice: c++

Re: SDL_Surface* load_image( std::string filename ) Question

Post by WSPSNIPER »

ya you can use it in a class, i always usto but using it as a global may be simpler if you dont have much exp. with oo programming. but on the other hand how will you learn. I use to think the same way i always wanted things global but once i learned about oo programming i always use it and i find it makes life easier
Post Reply