Skip to main content
GameDev.net gamedev.net
🔒 Locked

How to load in bitmaps using a text file as a reference?

Started by Gamester3333 Dec 9, 2008 at 2:39 PM 3 replies 1.5k views
Original Post
Gamester3333
Gamester3333
Hey guys, it's been a while... but I need help once again. I wrote some code earlier, and it doesn't work, but I can't figure out why for the life of me... I want to write a class that loads in individual, uncompressed bitmaps by reading in the list of file names to open from an editable text file. It's a class, with a vector container inside that holds the loaded bitmaps. Here's the code. If anyone can tell me why this doesn't work, that would be great. A working example would be better, but I won't expect that much.


class skillbitmaploader
    {
    public:
           
           skillbitmaploader(int a, FILE* b)
           {
                  id             = a;
                  textfile       = b;
                  
                  readstr(textfile, temp);
                  
                  sscanf(temp, "Number of bitmaps: %d\n", &iteration);
                  
                  

                  //Resize to contain 9 vectors of 22 members each.
                  bitmaps.resize(iteration, vector<BITMAP*>(1));
                  
                  for(int i = 0; i < iteration; i++)
                  {
                        readstr(textfile, temp);
                        
                        strncpy(temp2, temp, (string_length(temp) -1));
                        
                        bitmaps[0] = load_bitmap(temp2, NULL);
                        
                        if(bitmaps[0] == NULL)
                        {
                                         //exit(-1);
                        }
                        
                  }
                        
                        
                  
                  
           }
           
           ~skillbitmaploader()
           {
                   fclose(textfile);
                   
                   for(int i = 0; i < iteration; i++)
                   {
                         destroy_bitmap(bitmaps[0]);
                   }
           }
           
           BITMAP* returnbitmap(int c)
           {
                            return bitmaps[c][0];
                            
           }
           
           void* returntemp()
           {
                            return temp2;
                            
           }
           
              //Note the extra space between the >'s.
              vector< vector<BITMAP*> > bitmaps;
           
    protected:
              
              FILE* textfile;
              
              int iteration;
              int id;
              
              char temp[255];
              char temp2[255];
              
              
    };
    
    
    FILE* fireskillbitmaps = fopen("scripts\\skills\\fireskillbitmaps.zscript", "rt");
    
    skillbitmaploader fireskillloader(1, fireskillbitmaps);

Gamester3333
Gamester3333
Sorry, I forgot to mention that i'm using allegro 4.2.

I don't know why it wouldn't load in the bitmaps. I noticed that when I go to print the string, allegro leaves the newline in, so I chop it off in the loading function, but it still doesn't work...
Kylotan
Kylotan
Define "doesn't work". You should always say exactly what you had expected to happen, and what you see happening instead. You should also post your example data, in case the problem is in there.
Gamester3333
Gamester3333
Sorry, I guess I was a little vague. I want the class to load in a text file, read in each line, which is the complete filename of a bitmap, load them and store them in an easy to access vector. What happens when I try to load them is that the call fails and the bitmap pointer remains null, so that when i try to blit them the program crashes. I do believe that I have found the error however. My test data consisted of four lines, but only the third element would fail to load. I found this out by adding a line that checks for a failed load_bitmap() call, and writes the contents of temp to another file. The issue is that I'm not zeroing out the memory used by the previous string before loading in the next line. this causes any line that is shorter thatn the previous one to become corrupt. The bitmaps were loaded, but it recieved corrupt input on the third line, as it is shorter than the second. There may be other issues that I haven't foreseen, but I think this is the answer. I'm about to go change it, so I'll keep you guys posted.

And again, I'm sorry my code looks like a retarded 5 year old wrote it. I just came back from 6 months without a compiler...
Gage64
Gage64
Quote:
Original post by Gamester3333
The issue is that I'm not zeroing out the memory used by the previous string before loading in the next line. this causes any line that is shorter thatn the previous one to become corrupt.


Use std::string instead of character arrays, then you won't have to worry about stuff like that.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.