Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Bus Error?


  • Please log in to reply

#1
Spidy

Spidy

    Member

  • Member
  • PipPip
  • 11 posts
Hello all,

I've been working on learning how to use SDL, by working through some of Marius' tutorials. However, in his third tutorial on using sprites, I'm having an issues. I've been pseudo-converting his code to valid C++ instead of C, and this loop is causing me a problem. It will run through 3 times, and then exit with a bus error. Does anyone have any idea why? If I don't comment out the else clause at the bottom, it will always exit with that error. Anyone have any ideas?

Thanks,

Spidy


	char buffer[255];
	char filename[255];
	char name[255];
	int pause = 0;
	int red = 0;
	int green = 0;
	int blue = 0;
	cout << "creating spriteBase..." << endl;
	FILE *fp;
	sprintf(filename, "%s/info", dir);
	if((fp = fopen(filename, "r")) == NULL) {
		cout << "Error opening file " << filename << endl;
		exit(1); 
	}
	fgets(buffer, 255, fp);
	sscanf(buffer, "FILES: %d", &itsNumberOfFrames);
	itsAnimation = new spriteFrame[itsNumberOfFrames];
	itsBuilt = 1;
	int count = 0;
	
	cout << "Looping through file for properties..." << endl;
	while(!feof(fp) && count < itsNumberOfFrames) {
	cout << "Gettting property " << count << " of " << dir << "/" << filename << endl;
		fgets(buffer, 255, fp);
		// if we aren't at the end of the file, and the line doesn't start with a comment...
		if(buffer[0] != '#' && buffer[0] != '\r' && buffer[0] != '\0' && buffer[0] != '\n' && strlen(buffer) != 0) {
			// extract name of that frame's image, pause in milliseconds, red green blue values
			sscanf(buffer, "%s %d %d %d %d", name, &pause, &red, &green, &blue);
			sprintf(filename, "%s/%s", dir, name);
			// create a temporary surface to use for loading the image
			SDL_Surface *temp;
			// load in the image, checking to see if it worked correctly.
			if((temp = SDL_LoadBMP(filename)) == NULL) {
				cout << "Error Loading file" << filename << ": " << SDL_GetError() << endl;
				exit(1);
			}
			// if red is greater than or equal to zero, make that transparent
			if(red >= 0) {
				SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, red, green, blue));
			}
			itsAnimation[count].image = SDL_DisplayFormat(temp);
			// free the surface, so it can be used in the next iteration
			SDL_FreeSurface(temp);
			
			// store the pause we got out of the file into the pause value for the frame
			itsAnimation[count].pause = pause;
			
			//also, set the height/width values if they haven't been already
			if(!itsWidth) {
				itsWidth = itsAnimation[count].image->w;
			}
			if(!itsHeight) {
				itsHeight = itsAnimation[count].image->w;
			}		   
			count++;
			cout << "Got property " << count << " of " << dir << endl;
		} else {
//			cout << "Error loading \"" << filename << "\": " << SDL_GetError() << endl;
//			exit(1);
		}
	}
	fclose(fp);
	cout << "spriteBase created." << endl;

  • 0

Advertisements


#2
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
Hi Spidy,

have you figured out the problem, yet?

it looks like the 'info' file might be causing a problem, since the program crashes before the end of file is found.

what are the values of the following when it crashes?
buffer[0]
buffer
name
pause
red
green
blue
count
itsNumberOfFrames

does the contents of buffer agree with the 'info' file?

bdlt
  • 0

#3
Spidy

Spidy

    Member

  • Topic Starter
  • Member
  • PipPip
  • 11 posts
Hi bdlt,

I tried adding some debug prints, and it's apparently having an issue getting the value for name, because name is always being printed out as "B??KI?KI?M??????".

Do you have any ideas on why it's not working? Here's the code that fills in name:

sprintf(filename, "%s/%s", dir, name);

filename is the actual path to the info file, and dir is the directory that info is inside. Any ideas?

Spidy
  • 0

#4
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
here's the code that fills 'name' from the 'info' file.

sscanf(buffer, "%s %d %d %d %d", name, &pause, &red, &green, &blue);

is name displayed as "B??KI?KI?M??????" on every pass or just the crashing pass?

can you see what the name is supposed to be by looking at the 'info' file?

what is the value of 'buffer' immediately prior to the code above?

is 'itsNumberOfFrames' correct?

it would help if you posted the 1st 7 lines of the 'info' file.

Edited by bdlt, 14 November 2005 - 09:39 PM.

  • 0

#5
Spidy

Spidy

    Member

  • Topic Starter
  • Member
  • PipPip
  • 11 posts
Name is displayed as "B??KI?KI?M??????" every single pass, and the info file says:

FILES: 9
# SYNTAX:
# filename of the frame, milliseconds to pause afterwards,
# the RGB of the transparent part.
1.bmp	   50	0 255 0
2.bmp	   50	0 255 0
3.bmp	   50	0 255 0
4.bmp	   50	0 255 0
5.bmp	   50	0 255 0
6.bmp	   50	0 255 0
7.bmp	   50	0 255 0
8.bmp	   50	0 255 0
9.bmp	   50	0 255 0

itsNumberOfFrames IS being filled in with the correct number.

And right before having name be "B??KI?KI?M??????", buffer is line 1, 2, or 3 of the info file. At that point it will have the bus error.
  • 0

#6
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
for some reason, there appears to be a problem here. this line seems to work ok using dev-c++.
sscanf(buffer, "%s %d %d %d %d", name, &pause, &red, &green, &blue);

try:
sscanf(buffer, "%s %d %d %d %d", &name[0],&pause, &red, &green, &blue);

if you haven't done so, please display 'name' and 'buffer' immediately after the sscanf line of code.
  • 0

#7
Spidy

Spidy

    Member

  • Topic Starter
  • Member
  • PipPip
  • 11 posts
Alright, I changed the code like you said, and now it's doing this:

[code
Gettting property 0 of ****/info
name:1.***
buffer:1.*** 50 0 255 0

Bus error
[/code]

It seems to be having no other issues, because all of the other variables are right, now. Any other ideas?

The error is somehow in loading the image, but the tutorial code had no issues loading the image, and I've essentially copied it and melded it all into one file instead of 3.

Edited by Spidy, 14 November 2005 - 10:15 PM.

  • 0

#8
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts

And right before having name be "B??KI?KI?M??????", buffer is line 1, 2, or 3 of the info file. At that point it will have the bus error.


when you say buffer is line 1, 2, or 3 - is it these lines

# SYNTAX:
# filename of the frame, milliseconds to pause afterwards,
# the RGB of the transparent part.

or these?

1.bmp 50 0 255 0
2.bmp 50 0 255 0
3.bmp 50 0 255 0
  • 0

#9
Spidy

Spidy

    Member

  • Topic Starter
  • Member
  • PipPip
  • 11 posts
The one's with # at the beginning.
  • 0

#10
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
do you have files named 1.bmp, 2.bmp, ...?
  • 0

Advertisements


#11
Spidy

Spidy

    Member

  • Topic Starter
  • Member
  • PipPip
  • 11 posts
Yes.
  • 0

#12
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
let's comment out the following code and see if we can read the entire 'info' file(add /* prior to the 1st line shown and */ after the last line shown):


			sprintf(filename, "%s/%s", dir, name);
			// create a temporary surface to use for loading the image
			SDL_Surface *temp;
			// load in the image, checking to see if it worked correctly.
			if((temp = SDL_LoadBMP(filename)) == NULL) {
				cout << "Error Loading file" << filename << ": " << SDL_GetError() << endl;
				exit(1);
			}
			// if red is greater than or equal to zero, make that transparent
			if(red >= 0) {
				SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, red, green, blue));
			}
			itsAnimation[count].image = SDL_DisplayFormat(temp);
			// free the surface, so it can be used in the next iteration
			SDL_FreeSurface(temp);
			
			// store the pause we got out of the file into the pause value for the frame
			itsAnimation[count].pause = pause;
			
			//also, set the height/width values if they haven't been already
			if(!itsWidth) {
				itsWidth = itsAnimation[count].image->w;
			}
			if(!itsHeight) {
				itsHeight = itsAnimation[count].image->w;
			}
		  

  • 0

#13
Spidy

Spidy

    Member

  • Topic Starter
  • Member
  • PipPip
  • 11 posts
That seems to have worked...it's encountering another bus error at the end of the next info file, however.
  • 0

#14
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
what we did was comment out the 'SDL_Surface' code and are only reading the info file.

does it finish reading the info file? one way to tell is if the value of 'name' is shown as 1.bmp, 2.bmp, ..., 9.bmp.

are the .bmp files large? if not, can you attach a zip file with the 3 source code files and the .bmp files?
  • 0

#15
Spidy

Spidy

    Member

  • Topic Starter
  • Member
  • PipPip
  • 11 posts
It seems to be having an issue counting...the 8th property will be the 9th bmp, and then it will move on to the other file, which is supposed to have 4 bmp's, but dies with the bus error before opening the 4th.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP