Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Why is stretch_sprite( ) crashing??

Credits go to LennyLen, ReyBrujo, and Richard Phipps for helping out!
This thread is locked; no one can reply to it. rss feed Print
Why is stretch_sprite( ) crashing??
Matt Kindy
Member #7,331
June 2006
avatar

1int dir=0;
2int num=0;
3int x_position=300;
4int y_position=300;
5int z=3;
6int u=2;
7int MK_SCREEN_WIDTH=1280;
8int MK_SCREEN_HEIGHT=1024;
9char char_bmp_name[30];
10BITMAP *PA[16][25]
11 
12 
13sprintf(char_bmp_name,".\\PaladinAtk\\PAD%d%d.bmp", dir, num);
14PA[0/*(dir)*/][0/*(num)*/] = load_bitmap(char_bmp_name,NULL);
15stretch_sprite(map, PA[dir][num], x_position, y_position, .26*(z/u)*(MK_SCREEN_WIDTH), .2*(z/u)*(MK_SCREEN_HEIGHT));

Why does this crash my program?

Richard Phipps
Member #1,632
November 2001
avatar

Are map and your PA arrays the same colour depth?
Is map loaded correctly?

Matt Kindy
Member #7,331
June 2006
avatar

yes., check updated code.

ReyBrujo
Moderator
January 2001
avatar

Compile in debug and see the variables to known which one is causing the crashes.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Matt Kindy
Member #7,331
June 2006
avatar

none are causing the crash... I have identical functions except for dir and they work fine, but I know that dir isn't causing the crash, because I changed it to the same as another, it still crashes...

EDIT: when i comment out stretch_sprite, it works, but of course, the sprite is not displayed so... :-/

Richard Phipps
Member #1,632
November 2001
avatar

sprintf(char_bmp_name,".\\PaladinAtk\\PAD%d%d.bmp", dir, num);
PA[0/*(dir)*/][0/*(num)*/] = load_bitmap(char_name_str,NULL);

You are using char_bmp_name there, and then char_name_str. Is char_name_str correct?

also where are you creating or loading map in your code?

Matt Kindy
Member #7,331
June 2006
avatar

oops, sorry, typed it wrong in here, they are both char_bmp_name
let me give you whole function:

1int dir, num, hp=100;
2 char char_bmp_name[30];
3 double x_position ;
4 double y_position ;
5 double z=1, u=1;
6 int deathcounter=0;
7
8 //background
9 mapbackloaded = load_bitmap("map.bmp",NULL);
10 map = create_bitmap(mapbackloaded->w, mapbackloaded->h);
11 blit(mapbackloaded, map,0,0,0,0, mapbackloaded->w, mapbackloaded->h);
12
13
14
15 //load bmps
16 for(dir=0; dir<14; dir+=2){
17 for(num=0;num<19;num++){
18 sprintf(char_bmp_name,".\\PaladinAtk\\PAD%d%d.bmp", dir, num);
19 PA[dir][num] = load_bitmap(char_bmp_name, NULL);
20 }//end of for
21 }//end of for
22 for(dir=0; dir<14; dir+=2){
23 for(num=0;num<10;num++){
24 sprintf(char_bmp_name,".\\PaladinWalk\\PWD%d%d.bmp", dir, num);
25 PW[dir][num] = load_bitmap(char_bmp_name, NULL);
26 }//end of for
27 }//end of for
28 for(dir=0;dir<14;dir+=2){
29 for(num=0;num<10;num++){
30 sprintf(char_bmp_name,".\\PaladinDie\\PDD%d%d.bmp", dir, num);
31 PD[dir][num] = load_bitmap(char_bmp_name, NULL);
32 }//end of for
33 }//end of for
34 for(dir=0; dir<14; dir+=2){
35 sprintf(char_bmp_name,".\\PaladinDead\\pdead%d.bmp", dir-1);
36 pdead[dir] = load_bitmap(char_bmp_name, NULL);
37 }
38 /*don't need this
39 mana = load_bitmap("mana.bmp",NULL);
40 health0 = load_bitmap("health0.bmp",NULL);
41 health25 = load_bitmap("health25.bmp",NULL);
42 health50 = load_bitmap("health50.bmp",NULL);
43 health75 = load_bitmap("health75.bmp",NULL);
44 health100 = load_bitmap("health100.bmp",NULL);
45 healed = load_bitmap("healed.bmp",NULL);
46 restore = load_bitmap("restore.bmp",NULL);
47 dead = load_bitmap("dead.bmp",NULL);
48 hit = load_bitmap("hit.bmp", NULL);
49 */
50
51 x_position = .5*(MK_SCREEN_WIDTH) - .5*(190);
52 y_position = .5*(MK_SCREEN_WIDTH) - .5*(115);
53 dir=0;
54 
55 
56while(key[KEY_SPACE] && !key[KEY_ESC]){
57 for(num=0;num<19 && key[KEY_SPACE] && !key[KEY_ESC];num++){
58
59 blit(mapbackloaded, map, 0, 0, 0, 0, mapbackloaded->w, mapbackloaded->h);
60 if(hp!=0){
61
62
63 if(!key[KEY_Z] && !key[KEY_U]){
64 stretch_sprite(map, PA[dir][num], x_position, y_position, .26*(z/u)*(MK_SCREEN_WIDTH), .2*(z/u)*(MK_SCREEN_HEIGHT));
65 }
66 else if(key[KEY_Z] || key[KEY_U]){
67 if(z<6 && key[KEY_Z]){
68 z+=.1;
69 y_position-=2;
70 }
71 if(u<6 && key[KEY_U]){
72 u+=.1;
73 y_position+=2;
74 }
75 if(z==u){
76 z=1;
77 u=1;
78 }
79
80 stretch_sprite(map, PA[dir][num], x_position, y_position, .26*(z/u)*(MK_SCREEN_WIDTH), .2*(z/u)*(MK_SCREEN_HEIGHT));
81
82 }//else if
83 }//if
84 else{
85 stretch_sprite(map, pdead[dir], x_position, y_position, .26*(z/u)*(MK_SCREEN_WIDTH), .2*(z/u)*(MK_SCREEN_HEIGHT));
86 }
87
88 if(key[KEY_RSHIFT]){
89 z=1;
90 u=1;
91 }
92 if(key[KEY_R]){
93 hp=100;
94 stretch_sprite(map, restore, 0, 0, MK_SCREEN_WIDTH, MK_SCREEN_HEIGHT);
95 }
96 if(key[KEY_G]){
97 hp-=10;
98 stretch_sprite(map, hit, 0, 0, MK_SCREEN_WIDTH, MK_SCREEN_HEIGHT);
99 }
100 if(key[KEY_H]){
101 hp+=10;
102 stretch_sprite(map, healed, 0, 0, MK_SCREEN_WIDTH, MK_SCREEN_HEIGHT);
103 }
104 if(hp==0){
105 if(deathcounter == 0){
106 for(num=0;num<28;num++){
107 blit(mapbackloaded, map, 0, 0, 0, 0, mapbackloaded->w, mapbackloaded->h);
108 stretch_sprite(map, PD[dir][num], x_position, y_position, .26*(z/u)*(MK_SCREEN_WIDTH), .2*(z/u)*(MK_SCREEN_HEIGHT));
109 stretch_blit(map, screen, 0,0,1280,1024 ,0,0,MK_SCREEN_WIDTH,MK_SCREEN_HEIGHT);
110 rest(75);
111 }
112 deathcounter++;
113 stretch_sprite(map, dead, 0, 0, MK_SCREEN_WIDTH, MK_SCREEN_HEIGHT);
114 rest(1000);
115 }//end of if
116 else{
117 stretch_sprite(map, pdead[dir], x_position, y_position, .26*(z/u)*(MK_SCREEN_WIDTH), .2*(z/u)*(MK_SCREEN_HEIGHT));
118 }
119 }//end of if
120
121 stretch_blit(map, screen, 0,0,1280,1024 ,0,0,MK_SCREEN_WIDTH,MK_SCREEN_HEIGHT);
122
123 }//for
124
125 }//while

Richard Phipps
Member #1,632
November 2001
avatar

You have error checked the load_bitmap to make sure the result is not NULL? why do you have \\ in the sprintf?

Matt Kindy
Member #7,331
June 2006
avatar

THe '\\' is the equivalent of a single backslash, but C sees a single backslash as part of an escape sequence like, '\n', so you put \\ for files (i.e. "C:\\Program Files\\Game.exe"), the '.' means the 'current directory'.

Richard Phipps
Member #1,632
November 2001
avatar

Hmm.. I didn't know that. :) I just normally use a single / slash.
If your check that the bitmap was loaded is ok, then try replacing the stretch_sprite with a normal blit command to see if that works (blit features more error checking).

Matt Kindy
Member #7,331
June 2006
avatar

well, draw_sprite is doing it too..., hold on

and blit()

Richard Phipps
Member #1,632
November 2001
avatar

add:
if (map && dir < 16 && num < 25 && PA[dir][num])
{
// Put the blit in here..
}

Matt Kindy
Member #7,331
June 2006
avatar

it's not loading... I just rechecked it, so why wouldn't it be loading?

Richard Phipps
Member #1,632
November 2001
avatar

Either the file path is wrong, or the image is in the wrong format.

I asked if you checked for the load_bitmap working earlier on.. :)

Matt Kindy
Member #7,331
June 2006
avatar

sorry :-[

OH MY GOSH!
I misnamed them doh smacks himself

:'(:'(:'(:'(:'(:'(:'(:o:o:o:o:o

Thank you!

Richard Phipps
Member #1,632
November 2001
avatar

;)
Always check your filepaths first. It saves a lot of more difficult testing later on.

Matt Kindy
Member #7,331
June 2006
avatar

I might have one more question for you, so stay here for a min please

EDIT:

ok

1 
2 
3for(dir=0; dir<14; dir+=2){
4 for(num=0;num<10;num++){
5 sprintf(char_bmp_name,".\\PaladinWalk\\PWD%d%d.bmp", dir, num);
6 PW[dir][num] = load_bitmap(char_bmp_name, NULL);
7 if (!PA[dir][num]){
8 textout_ex(map,font,"WTF!?",100,100,makecol(0, 0, 255), -1);
9 }
10 }//end of for
11 }//end of for
12 
13 
14 
15while(key[KEY_DOWN] && key[KEY_RIGHT] && !key[KEY_LEFT] && !key[KEY_UP] && !key[KEY_ESC]){
16 for(num=0; num<10 && key[KEY_DOWN] && key[KEY_RIGHT] && !key[KEY_LEFT] && !key[KEY_UP] && !key[KEY_ESC]; num++){
17 blit(mapbackloaded, map, 0, 0, 0, 0, mapbackloaded->w, mapbackloaded->h);
18 if(hp!=0){
19 dir=14;
20
21 if(y_position < .78*(MK_SCREEN_HEIGHT) && x_position < .86*(MK_SCREEN_WIDTH)){
22 y_position += (5+(.25)*(z/u));
23 x_position += (5+(.25)*(z/u));
24 }
25
26 if(!key[KEY_Z] && !key[KEY_U]){
27
28//**********************************************************************
29//**********************************************************************
30//THIS
31stretch_sprite(map, PW[dir][num], x_position, y_position, .26*(z/u)*(MK_SCREEN_WIDTH), .2*(z/u)*(MK_SCREEN_HEIGHT));
32//**********************************************************************
33//**********************************************************************
34}
35 else if(key[KEY_Z] || key[KEY_U]){
36 if(z<6 && key[KEY_Z]){
37 z+=.1;
38 y_position-=2;
39 }
40 if(u<6 && key[KEY_U]){
41 u+=.1;
42 y_position+=2;
43 }
44 if(z==u){
45 z=1;
46 u=1;
47 }
48
49 stretch_sprite(map, PW[dir][num], x_position, y_position, .26*(z/u)*(MK_SCREEN_WIDTH), .2*(z/u)*(MK_SCREEN_HEIGHT));
50
51 }//else if
52 }
53 else{
54 stretch_sprite(map, pdead[dir], x_position, y_position, .26*(z/u)*(MK_SCREEN_WIDTH), .2*(z/u)*(MK_SCREEN_HEIGHT));
55 }
56 if(key[KEY_RSHIFT]){
57 z=1;
58 u=1;
59 }
60 if(key[KEY_R]){
61 hp=100;
62 stretch_sprite(map, restore, 0, 0, MK_SCREEN_WIDTH, MK_SCREEN_HEIGHT);
63 }
64 if(key[KEY_G]){
65 hp-=10;
66 stretch_sprite(map, hit, 0, 0, MK_SCREEN_WIDTH, MK_SCREEN_HEIGHT);
67 }
68 if(key[KEY_H]){
69 hp+=10;
70 stretch_sprite(map, healed, 0, 0, MK_SCREEN_WIDTH, MK_SCREEN_HEIGHT);
71 }
72 if(hp==0){
73 if(deathcounter == 0){
74 for(num=0;num<28;num++){
75 blit(mapbackloaded, map, 0, 0, 0, 0, mapbackloaded->w, mapbackloaded->h);
76 stretch_sprite(map, PD[dir][num], x_position, y_position, .26*(z/u)*(MK_SCREEN_WIDTH), .2*(z/u)*(MK_SCREEN_HEIGHT));
77 stretch_blit(map, screen, 0,0,1280,1024 ,0,0,MK_SCREEN_WIDTH,MK_SCREEN_HEIGHT);
78 rest(75);
79 }
80 deathcounter++;
81 stretch_sprite(map, dead, 0, 0, MK_SCREEN_WIDTH, MK_SCREEN_HEIGHT);
82 rest(1000);
83 }//end of if
84 else{
85 stretch_sprite(map, pdead[dir], x_position, y_position, .26*(z/u)*(MK_SCREEN_WIDTH), .2*(z/u)*(MK_SCREEN_HEIGHT));
86 }
87 }//end of if
88 stretch_blit(map, screen, 0,0,1280,1024 ,0,0,MK_SCREEN_WIDTH,MK_SCREEN_HEIGHT);
89
90 }//for
91 }//while

Richard Phipps
Member #1,632
November 2001
avatar

Matt Kindy
Member #7,331
June 2006
avatar

I prechecked this one and it's not loading either, however, in this case, it is named correctly.(At least I'm 90% sure...)

Richard Phipps
Member #1,632
November 2001
avatar

You could do with adding some logging code to your project. Then you could write out more detailed error messages and information to a text file and track down the problem easier.

It might be you have an image as 405 instead of 45, or something like that..

Matt Kindy
Member #7,331
June 2006
avatar

.
{"name":"589667","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/2\/6226b9129e907ece08201f2630586ced.jpg","w":1053,"h":1387,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/2\/6226b9129e907ece08201f2630586ced"}589667

These are the images... I don't think they do.
The last number of the name is num, the first one or two is dir

Richard Phipps
Member #1,632
November 2001
avatar

Sorry, I've got to go now. Maybe someone else can help you figure this one out!

Matt Kindy
Member #7,331
June 2006
avatar

THanks for all your help!

LennyLen
Member #5,313
December 2004
avatar

Change: for(dir=0; dir<14; dir+=2)
To for(dir=0; dir<15; dir+=2)

Go to: