www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/12/03/14:31:36

Message-ID: <3DED0701.5040209@rubyridge.com>
From: Legion <kilroy AT rubyridge DOT com>
User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.0.0) Gecko/20020530
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: DJGPP Allegro sprite troubles
Lines: 172
X-Complaints-To: abuse AT easynews DOT com
Organization: EasyNews, UseNet made Easy! - Test our service with our FREE trial at https://www.easynews.com/trial/trial.phtml
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Tue, 03 Dec 2002 19:28:22 GMT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Shown below is a small program that's designed to break a large bitmap 
up into an array of 32x32 sprites (fairly simple, right?). Of course it 
doesn't work, or I wouldn't be here asking.

The problem child is the function "loadsprites". This function is
supposed to load a bitmap (Windows or OS/2 .BMP) into the orig bitmap,
then blit 32x32 sections of it into the individual members of the
bitmaps[] array. However, when I run the program, it segfaults and gives
me this symify output:

    Shutting down Allegro
    Exiting due to signal SIGSEGV
    Page fault at eip=0000d670, error=0004
    eax=652f6e69 ebx=000f33d4 ecx=00000020 edx=00000181 esi=00000000
	edi=00000020 ebp=00000000 esp=000f23f0 
program=C:\DEVELO~1\PCDEVE~1\DJGPP\PROJECTS\WALKIN~1\W
    ALK.EXE
    cs: sel=00a7  base=86004000  limit=fd880fff
    ds: sel=00af  base=86004000  limit=fd880fff
    es: sel=00af  base=86004000  limit=fd880fff
    fs: sel=00af  base=86004000  limit=fd880fff
    gs: sel=00c7  base=00000000  limit=0010ffff
    ss: sel=00af  base=86004000  limit=fd880fff
    App stack: [000f2504..00072504]  Exceptn stack: [0007245c..0007051c]

    Call frame traceback EIPs:
       0x0000d670 _blit+80

    Now it's fairly obvious that it's got something to do with how I'm 
using/passing the bitmap pointers to blit() since it's a page fault, but
  I don't know what I'm doing wrong! The bitmaps array comes to the
function initialized as thus:

    BITMAP **bitmaps = malloc(sizeof(BITMAP * 12));

    because it's a 12-tile sprite. Now, I had previously been using a
function which loaded each sprite from its own individual file into a
member of bitmaps[], and that one worked fine, so I know the
initialization of bitmaps isn't a problem. (I've still got that function
  commented out in the source, if you wanna see it too.)

    I've tried using specifically memory bitmaps for orig via
create_system_bitmap, I've tried locking orig while I'm using it, I've
tried everything I can think of. HELP!!

    TESTING PLATFORM :
    Cyrix 6x86 MII 266
    48 mb RAM (1 32mb SDRAM stick, 1 16mb SIMM stick)
    S3 Trio64V2 Video
    Crystal PNP Sound
    Windows 98 OSR2
    Compiled on DJGPP V2 under a DOS box
    ran under Windows 98 DOS box */



    Note: This is part of a much larger (3000+ line) program, but this 
is where the error is. As such, I've only included the function causing 
the problem, and built a simple main() around it. If you want the full 
source, you can have it, but I don't see why anyone would wanna wade 
into that unless they've got a shield and a big debugger. ;-)

------------------------ BEGIN CODE HERE -------------------------------


// I've commented this as well as I can... Thanks for ANY help or
// insights!

#include <allegro.h>
#include <malloc.h>

int loadsprites(char *file, BITMAP **bitmaps, PALETTE pal,
		int numsprites, int rows)
{
    int count = 0;
    int blitx = 0;
    int blity = 0;
    BITMAP *orig = create_bitmap((numsprites * 32), (rows * 32));
    // Use any size image you want, just change the above initializer
    // to reflect the new image's size, it doesn't matter, so long as
    // it's no less than 384x32 (due to the number of images I'm asking
    // it to grab)
    clear_bitmap(orig);
    // clear the bitmap (I added this at the last build, hoping maybe
    // it'd help. It didn't.)

    if ( !bitmaps[0] )
    {
    	return 1;
    }

    orig = load_bitmap(file, pal);
    if (!orig)
    {
    	return 1;
    }

    for ( count = 0; count <= (numsprites - 1) ; count++ )
    // keep looping until we've got all the sprites
    {
       // see if we're at the end of this row. A "row" in this
       // instance is the width of orig, since each sprite is 32x32,
       // we check 32 * count to see if we're at the width of a row
       // yet. Rows needs to be greater than one, or we only have
       // one row to deal with, and skip this.
       if ( rows > 1 && (32 * count) < orig->w
		&& (count <= (numsprites - 1) ))
       {
       	blity += 32;
       	blitx = 0;
         // blity and blitx's functions should be obvious. They
         // are the offsets within orig at which we begin grabbing
         // our next sprite. If all the above conditions were met,
         // here we move the row down one, and reset the X counter.
       }
       blit(orig, bitmaps[count], blitx, blity, 0, 0, 32, 32);
       // This is one of the possible death points, as symify just
       // told me it died at blit() - however it didn't tell me WHICH
       // blit() it was dying at (that's real helpful...). Here
       // all I'm doing is grabbing the sprite at blitx, blity inside
       // of orig, and putting it in the bitmaps array at index "count"
       if ( !bitmaps[count] )
       {
       	return count;
       }
       blitx += 32;
       // move our blitx counter up to the next sprite. I had this set to
       // += 31, because I was thinking maybe my counting was off
       // (start at 0, you know), and I was fetching into memory that
       // wasn't mine - however, that didn't help, so now I'm back at 32.
    }
    destroy_bitmap(orig);
    // don't want useless memory buckets floating around........
    return 0;
}

int main(void)
{
	BITMAP **bitmaps = malloc(sizeof(BITMAP) * 12);
	PALETTE pal;
	// this is for a sprite of a little wizard that walks,
	// and his walking animation has 12 frames (hence the
	// array of 12 malloc'd BITMAPs

	if( allegro_init() ) {
		return 1;
	}

	if ( set_gfx_mode(GFX_SAFE, 640, 480, 0, 0) ) {
		return 1;
	}

	loadsprites("image.bmp", bitmaps, pal, 12, 1);
	// call loadsprites for the image, point it to the bitmaps array
	// tell it to use the PALETTE specified above. We want to get 12
	// images out of it, all of which are on one row.
	if ( !bitmaps[0] )
	{
		return 1;
	}

	blit(bitmaps[0], screen, 0, 0,
		(SCREEN_W / 2), (SCREEN_H / 2), 32, 32);
	// this is where it throws up my page fault (at least, I THINK
	// this is where it throws it up - if it's not here, then it's
         // in the blit() in loadsprite().

	allegro_exit();

	return 0;
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019