Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Opinions wanted - SpriteManager

This thread is locked; no one can reply to it. rss feed Print
 1   2   3   4 
Opinions wanted - SpriteManager
Stas B.
Member #9,615
March 2008

surely you don't mean that everyone here is crazy.

I don't know anymore. :o

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

L0L0lolololol You guys are nuts....

I can't believe this thread hasn't keeled over and died yet....

Sorry Itachiro I didn't really follow your code, but I have some feedback on what I used for myself if you would like....
Here is what I use in my Allegro 4 library for a resource manager - no shared_ptr's necessary. Resource objects own their resources, and a resource manager tracks them...
Resource Management zip file

And just to show you a basic resource object capabilites, here's the header for the Resource class :

#SelectExpand
1/* 2 * 3 * _______ ___ ____ __ _______ 4 * /\ ____\ /| \ / __\ /\ \ /\ ____\ 5 * \ \ ___/_ || _ \ | /__/____\ \ \ \ \ ___/_ 6 * \ \ ____\ || |_\ \ |\ \ /_ _\\ \ \ \ \ ____\ 7 * \ \ ___/_ || ___ \ \ \ \//\ / \ \ ____\ \ ___/_ 8 * \ ______\||_|__/_\ \ \ _/ | \ _____\\ ______\ 9 * /______/|/_/ /_/ ______/ /_____/ /______/ 10 * 11 * 12 * EAGLE 13 * Edgar's Allegro Gui Library and Extensions 14 * 15 * Copyright 2009-2011 by Edgar Reynaldo 16 * 17 * See EagleLicense.txt for allowed uses of this library. 18 * 19 */ 20 21 22#ifndef Resource_H 23#define Resource_H 24 25 26#include <allegro.h> 27#include <string> 28#include <map> 29#include <vector> 30 31#include "Eagle/ResourceRegistry.hpp" 32 33 34 35/** Loading a bitmap's palette is not presently supported */ 36 37enum RES_SRC { 38 RES_SRC_NONE = 0, 39 RES_SRC_FILE = 1, 40 RES_SRC_DATAFILE = 2 41}; 42 43enum RES_TYPE { 44 RES_TYPE_NONE = 0, 45 RES_TYPE_BITMAP = 1, 46 RES_TYPE_SAMPLE = 2, 47 RES_TYPE_MIDI = 3, 48 RES_TYPE_DATAFILE = 4 49}; 50 51 52/// Not designed to be container friendly or copied often. Use pointers instead. 53/// Copy constructor may make a full 'copy', either loading a fresh (full) copy of the resource or 54/// obtaining a new copy of the pointer from the datafile (shallow copy). Destructor frees the resources 55/// so if you have global Resource objects, you need to call Free() on them before main exits and 56/// don't call Acquire() until allegro is initialized. You can still have global Resource objects by 57/// setting acquire to false in the Resource constructor calls. 58 59/// The resources that a Resource object holds are freed automatically when the object goes out of scope. 60/// In the case of RES_SRC_DATAFILE type objects, no memory is actually freed. However, you can have a 61/// RES_SRC_FILE type object that holds a RES_TYPE_DATAFILE, and the datafile would be freed then. 62 63class ResourceRegistry; 64 65class Resource { 66private : 67// std::string name; 68 RES_SRC src; 69 RES_TYPE type; 70 71 std::string location;// For RES_SRC_FILE's 72 73 DATAFILE** pdatfile;// For RES_SRC_DATAFILE's , the address of the datafile is taken so that it may be reloaded if necessary 74 unsigned int dat_index;// For RES_SRC_DATAFILE's 75 76 void* resource; 77 RGB* palette; 78 79 ResourceRegistry* registry; 80 81public : 82 83 // The acquire parameter in the constructors allows for delayed loading and global resources 84 85 Resource(const std::string& file , RES_TYPE res_type , 86 bool acquire = true , ResourceRegistry* reg = &resource_registry); 87 88 Resource(DATAFILE** dat_addr , unsigned int datafile_index , RES_TYPE res_type , 89 bool acquire = true , ResourceRegistry* reg = &resource_registry); 90 91 Resource(const Resource& datafile_res , unsigned int datafile_index , RES_TYPE res_type , 92 bool acquire = true , ResourceRegistry* reg = &resource_registry); 93 94 ~Resource(); 95 96 void Free(); 97 bool Acquire(); 98 99 100 inline bool Ready(); 101 102 inline BITMAP* BitmapRes(); 103 inline RGB* PaletteRes(); 104 inline SAMPLE* SampleRes(); 105 inline MIDI* MidiRes(); 106 inline DATAFILE* DatafileRes(); 107 108 inline operator BITMAP*() {return BitmapRes();} 109 inline operator RGB*() {return PaletteRes();} 110 inline operator SAMPLE*() {return SampleRes();} 111 inline operator MIDI*() {return MidiRes();} 112 inline operator DATAFILE*() {return DatafileRes();} 113 inline operator bool() {return (bool)resource;} 114 115/// void* ResourceAddress() {return &resource;} // TODO : This would have to be available to 116 /// use datafile Resources as a DATAFILE source. 117 /// The Resource constructor that takes another Resource 118 /// as a parameter will handle this for you though. 119}; 120 121/* Usage examples 122 123 Resource bgpic("PrettyPicture.bmp" , RES_TYPE_BITMAP);// Loads automatically 124 Resource bgpic("AnotherPrettyPicture.bmp" , RES_TYPE_BITMAP , false);// Does not load until Acquire is called. 125 126 Resource datfile("resources.dat" , RES_TYPE_DATAFILE); 127 Resource spritesheet(datfile , PLAYER_SPRITE , RES_TYPE_BITMAP);// Grabs the PLAYER_SPRITE BITMAP* from the datafile 128 // just loaded from 'resources.dat' in the previous line. 129 Resource sound_of_waves(datfile , WAVES_SOUND , RES_TYPE_SAMPLE); 130 131 /// You can use a Resource object mostly like it's actual counter part - 132 // Show off the spritesheet - 133 blit(spritesheet , screen , 0 , 0 , 0 , 0 , ((BITMAP*)spritesheet)->w , ((BITMAP*)spritesheet)->h); 134 // to the soothing sound of ocean waves. Why not? 135 play_sample(sound_of_waves , 255 , 127 , 1000 , 1); 136 137//*/ 138 139 140class ResourceManager { 141 142 typedef std::map<std::string , Resource*> RESMAP; 143 typedef RESMAP::iterator RMIT; 144 145private : 146 RESMAP resmap; 147 std::vector<Resource*> resvec; 148 149public : 150 ResourceManager() : resmap() , resvec() {} 151 ~ResourceManager() {Clear();} 152 153 void Clear(); 154 void Clear(const std::string& name); 155 void FreeAllResources(); 156 void FreeResource(const std::string& name); 157 bool AcquireAllResources(bool stop_on_failure); 158 bool AcquireResource(const std::string& name); 159 160 bool LoadFileResource(const std::string& name , const std::string& file , RES_TYPE res_type , 161 bool acquire = true , ResourceRegistry* reg = &resource_registry); 162 bool LoadDataFileResource(const std::string& name , DATAFILE** dat_addr , unsigned int datafile_index , RES_TYPE res_type , 163 bool acquire = true , ResourceRegistry* reg = &resource_registry); 164 bool LoadDataFileResource(const std::string& name , const Resource& datafile_res , unsigned int datafile_index , RES_TYPE res_type , 165 bool acquire = true , ResourceRegistry* reg = &resource_registry); 166 167 Resource* GetResourceByName(const std::string& name); 168 BITMAP* GetBitmapByName(const std::string& name); 169 SAMPLE* GetSampleByName(const std::string& name); 170 MIDI* GetMidiByName(const std::string& name); 171 DATAFILE* GetDatafileByName(const std::string& name); 172 173}; 174 175 176/** Inline Resource class methods */ 177 178 179inline bool Resource::Ready() {return (bool)resource;} 180 181inline BITMAP* Resource::BitmapRes() { 182 ASSERT(type == RES_TYPE_BITMAP); 183 if (type == RES_TYPE_BITMAP) { 184 return (BITMAP*)resource; 185 } 186 return 0; 187} 188 189inline RGB* Resource::PaletteRes() { 190 ASSERT((type == RES_TYPE_BITMAP) && (src == RES_SRC_FILE)); 191 if ((type == RES_TYPE_BITMAP) && (src == RES_SRC_FILE)) { 192 return palette; 193 } 194 return 0; 195} 196 197inline SAMPLE* Resource::SampleRes() { 198 ASSERT(type == RES_TYPE_SAMPLE); 199 if (type == RES_TYPE_SAMPLE) { 200 return (SAMPLE*)resource; 201 } 202 return 0; 203} 204 205inline MIDI* Resource::MidiRes() { 206 ASSERT(type == RES_TYPE_MIDI); 207 if (type == RES_TYPE_MIDI) { 208 return (MIDI*)resource; 209 } 210 return 0; 211} 212 213inline DATAFILE* Resource::DatafileRes() { 214 ASSERT(type == RES_TYPE_DATAFILE); 215 if (type == RES_TYPE_DATAFILE) { 216 return (DATAFILE*)resource; 217 } 218 return 0; 219} 220 221 222 223 224 225#endif // Resource_H

Type safety is assured by asserts, Resource objects handle creation and destruction of resources, and a ResourceManager can track them all and clear and free them all at once or reload them or whatever you want it to do with them.

So, in accordance with how you say your game works, you could load one level, load the next level without duplicating resources and then free whatever are unused now. It would just involve comparing two lists of files, which you would need to keep.

AMCerasoli
Member #11,955
May 2010
avatar

He is just an arrogant person, which love to teach everybody how to do stuff and at the end he have done nothing... He was the one that convinced Microsoft to make Windows 7 the way it's now. Oh and the iPhone? that was Stas B's idea, and Steve Job was 'stupid' when he suggested doing something else...

45% of the grates ideas in this worlds comes from Stas B, the guy that half of the time is better than others at everything. Just take a look at his website and his company, you won't find anything better.

Edit:

Oh and BTW, Edgar your resource manager is stupid, mine is much better. The way you code is stupid, the "L0L0lolololol" at the star is stupid.

I can't believe this thread hasn't keeled over and died yet....

That's just stupid.

Quote:

Sorry Itachiro I didn't really follow your code

Ok, that is reeealllly stupid.

Quote:

Here is what I use in my Allegro 4 library

Allegro 4? HA! that is... stupid

Quote:

Resource objects own their resources

Super stupid!!!

WTF is happening here?? everyone is stupid except me? What a losers!

Stas B.
Member #9,615
March 2008

You sure are one angry, incompetent little man, AMCerasoli. Just read your own post again. If you think that was clever sarcasm, or funny, or anything but embarassing for you, you are clearly an idiot who shouldn't be programming. You should try getting a new hoby, like knitting.

I'm under attack here for offering perfectly valid critique in a thread that asks for it. And don't try to pull that "you called his ideas stupid" crap. I called his clearly stupid idea stupid after he started attacking me personally. This is absurd. Screw you. :-/

Itachihro
Member #12,001
May 2010

no shared_ptr's necessary

I wonder why people keep pointing this out as if it was some kind of relevant insight ???. I'm aware people managed their resources before C++11 was released folks. I used shared_ptr's because it was convenient (for me), not because it was necessary.

Quote:

Type safety is assured by asserts, Resource objects handle creation and destruction of resources, and a ResourceManager can track them all and clear and free them all at once or reload them or whatever you want it to do with them.So, in accordance with how you say your game works, you could load one level, load the next level without duplicating resources and then free whatever are unused now. It would just involve comparing two lists of files, which you would need to keep.

I'm sure this works - it's just that my approach is different.

Stas B. said:

I called his clearly stupid idea stupid after he started attacking me personally.

Are we talking about the same thread here? I don't remember doing that.

Stas B.
Member #9,615
March 2008

Itachihro said:

Are we talking about the same thread here? I don't remember doing that.

Yeah. You were clearly implying that I'm biased and narrow-minded before I called your idea stupid. I take that as a personal attack. You guys can feel free to talk about my shortcomings behind my back. I won't be reading this spam any longer. ::)

Itachihro
Member #12,001
May 2010

Well, your posts before you started calling me stupid were all about how I had no idea what my class was doing because it didn't do what you wanted it to do. Your entire criticism was about how this implementation enforced a particular way of using the class - which is true, but irrelevant. And something about shared_ptr's. I still have no idea what your problem with those was.

Sirocco
Member #88
April 2000
avatar

The problem with threads like this, is someone walks in and asks "what do you think?" regarding a piece of code, or design. No one will ever say "That's great," or "That's just what I would have done!" or anything else of a similar fashion because programming is a terribly subjective thing. People garner experience, then get a lot of goofy ideas in their heads about what is and is not good practice. Then those ideas turn to dogma, and they eventually find people who hold similar delusions, which invites much ugliness in threads like this.

Now get back to work!

-->
Graphic file formats used to fascinate me, but now I find them rather satanic.

Itachihro
Member #12,001
May 2010

That's why I didn't ask whether anyone liked the idea, but whether there was something wrong with the code. At least I intended to, but my original post probably wasn't too clear about that.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Luiji99
Member #12,254
September 2010

WTF is happening here?? everyone is stupid except me? What a losers!

"What a losers?" That's just grammatically stupid!

Honestly, I have to agree with Stas B.'s opinion of OP's resource manager being written in a sub-par fashion and that Stas's system is (at least somewhat) better, but it didn't take him (you, depending on reader) to give in to personal attack. From my review of this post, OP did start it, but you immediately continued and escalated it. At the same time, he seemed to immediately reject your opinion without giving a valid explanation for why.

Take what the rest of these posters. It took them at least five posts before they began personal attacks on you, if at all.

But, my God, how can a discussion of sprite managers can on for this long? Who in their right mind would hold this strong of an opinion on a God damn sprite manager?!

And lastly an emoticon to make my seem good-humored. :P

Programming should be fun. That's why I hate Java.

Specter Phoenix
Member #1,425
July 2001
avatar

Stas B. said:

You were clearly implying that I'm biased and narrow-minded before I called your idea stupid.

Implied? Thought everyone said it flat out to your face? ??? Guess they need to work on that. :P

Stas B. said:

I don't care if you take me seriously. You guys are crazy. ???

What does that make the person that sticks around knowing they are crazy? ???

Now, on to topic, Itachihro...As anyone will tell you there is no right or wrong way to do something. There are some methods that are better than others, but so long as the program or chunk of code does what it is intended to do, then it is fine. Take these critiques with a grain of salt and don't stress over it.

Itachihro
Member #12,001
May 2010

I wasn't intending to stress over it in the first place. This right now is a learning experience for me - I didn't get to work on many larger projects in the past - so if someone tells me something is wrong I won't take it at face value (unless it's something that's immediately obvious to me too) until I've seen for myself exactly why it's supposed to be bad.

Luiji99 said:

Honestly, I have to agree with Stas B.'s opinion of OP's resource manager being written in a sub-par fashion and that Stas's system is (at least somewhat) better, but it didn't take him (you, depending on reader) to give in to personal attack. From my review of this post, OP did start it, but you immediately continued and escalated it. At the same time, he seemed to immediately reject your opinion without giving a valid explanation for why.

I still don't see just what exactly I did wrong (interaction wise). I also don't see why Stas's one would be better - from what I gather, it does mostly the same thing as mine, except that you only have option to either manually free singly resources or free all of them at once. How is this less problematic than what I did?

Specter Phoenix
Member #1,425
July 2001
avatar

Itachihro said:

I still don't see just what exactly I did wrong (interaction wise). I also don't see why Stas's one would be better - from what I gather, it does mostly the same thing as mine, except that you only have option to either manually free singly resources or free all of them at once. How is this less problematic than what I did?

Doesn't matter, like Thomas pointed out, StasB has the attitude in every point he makes that he is always right and everyone else is always wrong. There have been times that people have proved him wrong and he quickly starts peddling his point from a different angle to achieve his point. Don't take much stock in his point of view due to this fact and don't pay much attention to anyone that agrees with his point of view either.

Audric
Member #907
January 2001

Itachihro said:

How is this less problematic than what I did?

If I understand correctly, your system is a bit more obscure/unusual.
A maintainer who doesn't know it will see the class Level build a private collection of Sprites and do absolutely nothing with it. The purpose of this collection had better be commented in the program :
// pre-caching of sprites and their continued existence for the duration of the whole level

Itachihro
Member #12,001
May 2010

Audric said:

If I understand correctly, your system is a bit more obscure/unusual.

I suppose it is, but it's just intended for my private use anyways.

Quote:

A maintainer who doesn't know it will see the class Level build a private collection of Sprites and do absolutely nothing with it. The purpose of this collection had better be commented in the program

Rather than that, I was going to create game objects from prototypes - not really in the same sense as in the prototype pattern, more like "I have a list of encounter descriptions that are created at the beginning of the level, and each of those is able to spawn off a concrete encounter". That's the direction I was thinking in.

Luiji99
Member #12,254
September 2010

Specter Fenix said:

and don't pay much attention to anyone that agrees with his point of view either

I'm sorry, but have I said something wrong? I've agreed that his engine seems to be better, though I don't agree with how he's expressed his opinion, why should he simply ignore me?

Programming should be fun. That's why I hate Java.

Specter Phoenix
Member #1,425
July 2001
avatar

Luiji99 said:

I've agreed that his engine seems to be better, though I don't agree with how he's expressed his opinion, why should he simply ignore me?

Agreeing with his engine isn't agree with his point of view. Agreeing with a point of view is "All <ethnicity here> should be shipped to <location here>." or "A programmer that uses system(); for anything is stupid and should be shot." Lastly, I said don't pay much attention to them, again a huge difference in not paying attention to someone and ignoring them. You don't pay much attention to people you walk by everyday, but if you ignore them you would likely run into them.

This post is the best example between ignoring and not paying attention. If I was ignoring you I wouldn't have replied. Though I wasn't paying attention to anything on your post except your reply so I missed that you spelled Phoenix as Fenix, just missing my Blade now.

Luiji99
Member #12,254
September 2010

Pardon my spelling error. The confusing thing for me is/was, you state "and don't pay much attention to anyone that agrees with his point of view either." I haven't seen anyone agree with his point of view, though I've seen a few (very few) agree with his engine, so I interpreted it differently than it might have meant.

Programming should be fun. That's why I hate Java.

Specter Phoenix
Member #1,425
July 2001
avatar

Oh okay, well what I meant was anyone that had the point of view that Itachihro was stupid for the way he had coded it which is how StasB had come across with his remarks. Everyone codes differently and it was rude of StasB to insult Itachihro's method of doing it. Are there better ways of doing it? Sure, but no need to attack one's style just because he thinks his way is better. StasB has a history of doing this on A.cc though and I can't imagine ML just sitting by while a member puts down another member for their coding preferences on repeated occasions.

Also, don't worry about the spelling error, made me smirk as it instantly made me think of Sirocco's Fenix Blade game when I read it :).

Dizzy Egg
Member #10,824
March 2009
avatar

I wasn't going to put my two cents in, as this thread went mad early on. But I'm torn; Stas.B usually you ARE a nightmare...you obviously know a LOT about C++, and the theory of coding well (it's obvious from your previous offers of 'help') but also usually you do come across aggressive and 'I'M RIGHT YOU'RE WRONG THE END'......

....BUT on this occasion I agree with you. Itachihro you posted asking for comments....Stas gave them...you went on defend. My advice? Don't post snippets here, finish a complete project and wow us. Getting your snippets commented will divert you mid-project...sod us, make it work! Then ask us for comments!

(go Stas) ;D

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Specter Phoenix
Member #1,425
July 2001
avatar

Odd, guess ML thought he was out of line too or just coincidental that StasB's profile now goes to a 404 page.

Though, I did have an issue with StasB's remark:

Stas B. said:

Read my posts again untill you understand that.

Those kind of lines come across more like an insult toward your intellect which I think is where the OP went on the defense.

Sirocco
Member #88
April 2000
avatar

There are no absolutes in programming, just as one cannot expect two writers to pen identical novels by pure chance. Some approaches do require more diligence to maintain their effectiveness, however.

I would rather see a person finish something, achieving a measure of success, and learn something from it--than encourage them to stop and redirect their efforts. It's so easy to be distracted, we should not stand in the way of those who have the will to persevere.

The older I grow, the more I chuckle at the folly of my youth.

-->
Graphic file formats used to fascinate me, but now I find them rather satanic.

Specter Phoenix
Member #1,425
July 2001
avatar

Are there really any absolutes in life outside of "you live" "you die"? Though I agree completely with that point of view Sirocco. :)

Luiji99
Member #12,254
September 2010

Taxes, too. :P (Benjamin Franklin FTW)

So...was Stas B. terminated? Is that what profile 404s mean at this forum? 'Cause man, that was a silent kill.

It's also possible that he got tired of his lack of worshipers and decided to leave, I guess. But he strikes me as the kind of guy who would make a grand announcement about that. ;D

Programming should be fun. That's why I hate Java.

 1   2   3   4 


Go to: