Thread: Curious anomoly in allegro.

  1. #1
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320

    Question Curious anomoly in allegro.

    Ok so I decided to write some code to place a bunch of bitmap files into one FGD(fixed game data) file just for fire transfer's sake from a server to client update kinda deal I figured plain text would work great. This code though once we kick into the compression function decides to rename my filename variable say I pass in Slime.bmp I end up with S< at the end and a program crash. Anyone see something I am missing? Kinda late so maybe I am overlooking something simple...


    Code:
    #include <iostream>
    #include <fstream>
    #include <string.h>
    #include <allegro.h>
    #include <winalleg.h>
    
    using namespace std;
    void compress(char *);
    
    ofstream FGD_FILE;
    
    int main(int argc, char *argv[])
    {
        FGD_FILE.open("Compressed.fgd");
        FGD_FILE<<argc-1<<endl;
        if(!install_allegro(SYSTEM_NONE,&errno,atexit))
        if(argc > 1)
        {
            for(int comp = 1;comp < argc;comp++)
            {
                compress(argv[comp]);
            }
        }else
        {
            cout<<"Files to compress are not specified."<<endl;
        }
        FGD_FILE.close();
    	return 0;
    }
    END_OF_MAIN();      //allegro specific to correct for winapi's main() function.
    
    void compress(char *filename)
    {
        BITMAP *bmp;
        RGB *palette;
        char *isgoodfile = strstr(filename,".bmp");
        
        if(isgoodfile != NULL)//non null if it is a bitmap or has .bmp in it at least.
        {
            bmp = load_bitmap(filename,palette);
            //CHANGES THE NAME HERE
            if (!bmp)
            {
                cout<<"Problem loading bitmap: "<<filename<<endl
                    <<"Compression aborted check file extention is a .bmp file"<<endl;
            }else
            {
                int color_locked = -1;      //technically valid for r=255 g=255 b=255 and a=255
                int looped = 1;
                FGD_FILE<<filename<<" "<<bmp->w<<" "<<bmp->h<<endl;
                for(int y = 0;y < bmp->h;y++)
                {
                    for(int x = 0;x < bmp->w;x++)
                    {
                        if(color_locked != getpixel(bmp,x,y))
                        {
                            if(color_locked != -1)FGD_FILE<<looped<<" "<<color_locked<<" ";
                            color_locked = getpixel(bmp,x,y);
                            looped = 1;
                        }else looped++;
                    }
                }
                FGD_FILE<<looped<<" "<<color_locked<<endl<<endl<<endl;
            }
            cout<<"Compressed and added file: "<<filename<<endl;
            destroy_bitmap(bmp);
        }else cout<<"Unable to verify file maybe it isn't a bitmap?"<<endl;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I've never used allegro, but notice that while the second parameter to load_bitmap() is actually an RGB*, the example in the docs actually uses a PALETTE. Allegro Manual: Loading image files

    Clicking on the links, a PALETTE is actually an array instead of just a pointer. What's probably happening is that since you pass in just an RGB* pointer, which could be pointing anywhere, you get memory corruption. Try using a PALETTE or, if you don't care about the palette at all, pass in a NULL pointer (according to the docs this is allowed).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    While this is true you can actually pass NULL to it and have it work fine... Yea ok so you were cut short on my e-mail note lol NULL works fine more often than not I don't remember what I did to fix it. but it is fixed moved onto other things sorry...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Programming FAQ
    By TechWins in forum Game Programming
    Replies: 5
    Last Post: 09-29-2004, 02:00 AM
  2. problem in allegro with sprite leaving trail
    By Leeman_s in forum C++ Programming
    Replies: 18
    Last Post: 09-13-2002, 03:04 PM
  3. double buffering for allegro
    By Leeman_s in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2002, 02:45 PM
  4. Special Allegro Information
    By TechWins in forum Game Programming
    Replies: 12
    Last Post: 08-20-2002, 11:35 PM
  5. Allegro programming in a window
    By Person Man in forum Windows Programming
    Replies: 0
    Last Post: 11-16-2001, 03:23 PM

Tags for this Thread