hardcoder wrote:
I am not insulting anybody. Just commenting what I was given. You didn’t wrote it is partial code and what you already tried. That said, give us real code that fails, not the part you think is relevant. Also, I see what loadImage TRIES to do not what it does, since it returns no value to a caller (gameInit function) has no idea if image was correctly loaded. Do not get upset just because I asked questions.
Alright. I’ll try to be more thorough in my explanation. I do have to get up for work in the morning though, its almost 11:30 at night here in Taiwan.
I have made some changes to my code since my previous post. I’ll try to explain everything I’ve done and why. I can’t post ALL my code because, even at this early stage, theres quite a lot of code. Heres what I have now:
Code:
#include <SDL/SDL.h>
struct image
{
const char *fname;
SDL_Surface *surface;
};
struct animation
{
struct image image;
struct animation *next;
};
struct stats
{
char name[32];
unsigned int attack,
defense,
health,
max_ammo;
};
struct physics
{
double x, y,
dir;
};
struct character
{
struct stats status;
struct physics physics;
struct image image;
struct animation *left,*right,*up,*down,*active;
};
struct character Main, BG;
void windowInit(const char * title, short unsigned int width, short unsigned int height)
{
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption(title, title);
screen = SDL_SetVideoMode(width, height, 16, SDL_DOUBLEBUF);
// screen = SDL_SetVideoMode(width, height, 16, SDL_DOUBLEBUF | SDL_FULLSCREEN);
return;
}
void display()
{
SDL_Flip(screen);
return;
}
void show(double x, double y, struct image obj)
{
SDL_Rect src, dest;
printf("%d:%d\n",obj,obj.surface);
src.x = 0;
src.y = 0;
printf("1\n");
src.w = obj.surface->w;
src.h = obj.surface->h;
printf("2\n");
printf("3\n");
dest.x = x;
dest.y = y;
printf("4\n");
dest.w = obj.surface->w;
dest.h = obj.surface->h;
SDL_BlitSurface(obj.surface, &src, screen, &dest);
return;
}
struct animation *animation_create_frames(unsigned short int frames)
{
int i;
struct animation *tmp, *start;
start=malloc(sizeof(struct animation));
tmp=start;
for (i=1;i<frames;i++)
{
tmp->next=malloc(sizeof(struct animation));
tmp=tmp->next;
}
tmp->next=start;
return start;
}
void delay(Uint32 i)
{
SDL_Delay(i);
return;
}
void loadImage(const char *fname, struct image *obj)
{
obj->fname=fname;
SDL_Surface *temp = SDL_LoadBMP(fname);
obj->surface = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
//SDL_SetAlpha(obj->surface, SDL_SRCALPHA | SDL_RLEACCEL, 0xFF);
SDL_SetColorKey(obj->surface, SDL_SRCCOLORKEY, SDL_MapRGB(obj->surface->format, 0x0, 0xFF, 0x0));
printf("%d::%d\n",obj,obj->surface);
return;
}
void gameInit()
{
BG.active=malloc(sizeof(struct animation));
loadImage("./images/testbg.bmp",&BG.active->image);
Main.down=animation_create_frames(4);
loadImage("./images/c1_1.bmp", &Main.down->image);
loadImage("./images/c1_2.bmp", &Main.down->next->image);
loadImage("./images/c1_1.bmp", &Main.down->next->next->image);
loadImage("./images/c1_3.bmp", &Main.down->next->next->next->image);
Main.active=Main.down;
Main.physics.x=100;
Main.physics.y=200;
return;
}
void gameRender()
{
printf("%d:%d\n",Main.active->image,Main.active->image.surface);
show(Main.physics.x, Main.physics.y, Main.active->image);
Main.active=Main.active->next;
printf("%d:%d\n",BG.active->image,BG.active->image.surface);
show(0,0,BG.active->image);
display();
return;
}
int quit=false;
int main ( int argc, char *argv[] )
{
windowInit(“The Lesser Evil”,640,480);
gameInit();
#ifdef DEBUG
SDL_version ver;
// Prints the compile time version
SDL_VERSION(&ver);
printf("SDL Version: %u.%u.%u\n", ver.major, ver.minor, ver.patch);
// Prints the run-time version
ver = *SDL_Linked_Version();
printf("SDL Runtime Version: %u.%u.%u\n", ver.major, ver.minor, ver.patch);
#endif
while (!quit)
{
gameRender();
delay(1000/5);
}
return 0x14;
}
Ok, there it is. I added a bunch of printf to determine where and why this is happening. I changed “struct image backdrop” to “struct character BG” so I could do the exact same thing with Main and BG. Main works, BG still does not. I also use a circularly-linked list for an animation. Here is some sample output from the program:
Code:
13733488::13808336
13733760::13734000
13733792::13734240
13733824::13739360
13733856::13739600
SDL Version: 1.2.13
SDL Runtime Version: 1.2.13
4199076:13734000
4199076:13734000
1
2
3
4
4199056:13808336
0:0
1
Segmentation fault
The first few lines confirm that the images are getting loaded correctly. Then my SDL version numbers. Then the memory addresses of Main.active and Main.active->image before and after it gets passed to show(). Then 1 2 3 4 showing that show() didn’t crash that time.
As you can see from the last 3 lines before the segfault, the problem is occurring in the show function. I try to pass BG to it, but the function sees it as 0. weird because when I do the exact same thing with Main it works fine. If I just comment out the line where I show(BG), then it runs fine and I see my little walking guy on a black screen.