【发布时间】:2014-03-18 15:53:47
【问题描述】:
这是我的代码。
谁能帮帮我?
#include <SDL/SDL.h>
#include <stdlib.h>
#include <stdio.h>
int blit_image(char chemin[15],SDL_Surface *fenetre,int posx,int posy) {
SDL_Surface *temp;
SDL_Surface *image;
SDL_Rect position;
temp = SDL_LoadBMP(chemin);
image= SDL_DisplayFormat(temp);
position.x = posx;
position.y = posy;
position.w = image->w;
position.h = image->h;
SDL_BlitSurface(image,NULL,fenetre,&position); SDL_Flip(fenetre);
SDL_FreeSurface(temp);
SDL_FreeSurface(image);
}
int main(int argc, char *argv[]) {
SDL_Surface *fenetre;
SDL_Event event; int done=1; SDL_Init(SDL_INIT_VIDEO);
fenetre = SDL_SetVideoMode(910,600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
blit_image("resources/images/fondblanc.bmp",fenetre,0,0);
blit_image("resources/images/chat.bmp",fenetre,0,0);//cat
blit_image("resources/bouttons/chatbt.bmp",fenetre,820,25);//dog
blit_image("resources/bouttons/chienbt.bmp",fenetre,820,140); //horse
blit_image("resources/bouttons/chevalbt.bmp",fenetre,820,255); //tiger
blit_image("resources/bouttons/tigrebt.bmp",fenetre,820,370);//hen
blit_image("resources/bouttons/poulebt.bmp",fenetre,820,485);
while (done) {
SDL_WaitEvent(&event);
switch(event.type) {
case SDL_QUIT:
{
done=0;
break;
}
case SDL_MOUSEBUTTONDOWN:
{
if ((event.button.x>820)&&(event.button.x<890)&&(event.button.y<115)&& (event.button.y>25))
{
blit_image("resources/images/chat.bmp",fenetre,0,0);
}
if ((event.button.x>820)&&(event.button.x<890)&&(event.button.y<230)&& (event.button.y>140))
{
blit_image("resources/images/chien.bmp",fenetre,0,0);
}
if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<345)&&(event.motion.y>255))
{
blit_image("resources/images/cheval.bmp",fenetre,0,0);
}
if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<460)&& (event.motion.y>370))
{
blit_image("resources/images/tigre.bmp",fenetre,0,0);
}
if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<575)&&(event.motion.y>485))
{
blit_image("resources/images/poule.bmp",fenetre,0,0);
}
}
}
if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<115)&& (event.motion.y>25))
{
blit_image("resources/bouttons/chatbtClic.bmp",fenetre,820,25);
blit_image("resources/bouttons/chienbt.bmp",fenetre,820,140);
blit_image("resources/bouttons/chevalbt.bmp",fenetre,820,255);
blit_image("resources/bouttons/tigrebt.bmp",fenetre,820,370);
blit_image("resources/bouttons/poulebt.bmp",fenetre,820,485);
}
if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<230)&&(event.motion.y>140))
{
blit_image("resources/bouttons/chienbtClic.bmp",fenetre,820,140);
blit_image("resources/bouttons/chatbt.bmp",fenetre,820,25);
blit_image("resources/bouttons/chevalbt.bmp",fenetre,820,255);
blit_image("resources/bouttons/tigrebt.bmp",fenetre,820,370);
blit_image("resources/bouttons/poulebt.bmp",fenetre,820,485);
}
if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<345)&& (event.motion.y>255))
{
blit_image("resources/bouttons/chevalbtClic.bmp",fenetre,820,255);
blit_image("resources/bouttons/chatbt.bmp",fenetre,820,25);
blit_image("resources/bouttons/chienbt.bmp",fenetre,820,140);
blit_image("resources/bouttons/tigrebt.bmp",fenetre,820,370);
blit_image("resources/bouttons/poulebt.bmp",fenetre,820,485);
}
if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<460)&&(event.motion.y>370)) {
blit_image("resources/bouttons/tigrebtClic.bmp",fenetre,820,370);
blit_image("resources/bouttons/chatbt.bmp",fenetre,820,25);
blit_image("resources/bouttons/chienbt.bmp",fenetre,820,140);
blit_image("resources/bouttons/chevalbt.bmp",fenetre,820,255);
blit_image("resources/bouttons/poulebt.bmp",fenetre,820,485);
}
if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<575)&&(event.motion.y>485))
{
blit_image("resources/bouttons/poulebtClic.bmp",fenetre,820,485);
blit_image("resources/bouttons/chatbt.bmp",fenetre,820,25);
blit_image("resources/bouttons/chienbt.bmp",fenetre,820,140);
blit_image("resources/bouttons/chevalbt.bmp",fenetre,820,255);
blit_image("resources/bouttons/tigrebt.bmp",fenetre,820,370);
}
}
SDL_Quit();
return 0;
}
我使用gcc game.c -o prog -lSDL -lSDL_mixer -lSDL_ttf编译它
代码编译成功,但在输入 ./prog 以执行代码时它显示给我 "Segmentation fault (core dumped)"
但使用的文件存在于目录中
【问题讨论】:
-
在像 gdb 这样的调试器中运行程序,看看它在哪里崩溃
-
"Segmentation fault (core dumped)" 不是编译器错误(除非您发现编译器错误,这不太可能)。相反,您更有可能遇到 run time 错误。描述您的确切步骤。你编译代码了吗?你链接代码了吗?你运行代码了吗?您究竟是从哪里得到错误的,您是如何尝试调试的?
-
我使用 gcc game.c -o prog -lSDL -lSDL_mixer -lSDL_ttf 编译它
-
@sanaEsprit:编译的结果是什么?*你收到错误了吗?还是编译成功?你接下来做了什么? (您必须提供信息,否则我们无能为力)
-
VTC: “请说明您的具体问题或添加其他详细信息以准确突出您的需要。”