C++/SDL中的奇怪行为
Odd behavior in C++/SDL?
我一直在用C++/SDL开发一个游戏,但我很困惑为什么我的程序在"while"循环结束之前就关闭了。我找了找漏洞,但一无所获。然而,在试用了这个程序之后,我怀疑罪魁祸首在我的"menu.h"文件中。这是游戏中的所有代码。到目前为止,Player.h只是一堆定义。
include_file.h
#ifndef INCLUDE_FILE_H_INCLUDED
#define INCLUDE_FILE_H_INCLUDED
#include <map>
#include <string>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_mixer.h>
using namespace std;
struct image_struct
{
SDL_Surface *donkey;
SDL_Surface *donkey_select;
SDL_Surface *kawasaki;
SDL_Surface *kawasaki_select;
SDL_Surface *desert;
SDL_Surface *desert_select;
};
struct sound_struct
{
Mix_Music *menu_theme;
};
struct clip_struct
{
SDL_Rect level_select[1];
SDL_Rect player_select[1];
SDL_Rect bike_select[1];
};
void applysurface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *source_clip = NULL)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, source_clip, destination, &offset);
};
int quit;
string level;
image_struct image;
sound_struct sound;
clip_struct clip;
SDL_Surface *screen;
SDL_Event event;
void variable_setvalue(void)
{
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096);
clip.level_select[0].x = 0;
clip.level_select[0].y = 0;
clip.level_select[0].w = 128;
clip.level_select[0].h = 128;
clip.level_select[1].x = 128;
clip.level_select[1].y = 0;
clip.level_select[1].w = 256;
clip.level_select[1].h = 128;
clip.player_select[0].x = 0;
clip.player_select[0].y = 0;
clip.player_select[0].w = 50;
clip.player_select[0].h = 100;
clip.player_select[1].x = 50;
clip.player_select[1].y = 0;
clip.player_select[1].w = 100;
clip.player_select[1].h = 100;
clip.bike_select[0].x = 0;
clip.bike_select[0].y = 0;
clip.bike_select[0].w = 100;
clip.bike_select[0].h = 50;
clip.bike_select[1].x = 0;
clip.bike_select[1].y = 50;
clip.bike_select[1].w = 100;
clip.bike_select[1].h = 100;
SDL_Surface *screen = SDL_SetVideoMode(512, 512, 32, SDL_SWSURFACE);
image.donkey = IMG_Load("images/donkey.png");
image.donkey_select = IMG_Load("images/donkey_select.png");
image.kawasaki = IMG_Load("images/kawasaki.png");
image.kawasaki_select = IMG_Load("images/kawasaki_select.png");
image.desert = IMG_Load("images/desert.png");
image.desert_select = IMG_Load("images/desert_select.png");
sound.menu_theme = Mix_LoadMUS("sounds/death_desert_racing.wav");
level = "desert";
quit = 0;
};
#endif // INCLUDE_FILE_H_INCLUDED
main.cpp
#include "include_file.h"
#include "player.h"
#include "menu.h"
int main(int argc, char *argv[])
{
variable_setvalue();
while (quit == 0)
{
menu_action();
if (level == "desert")
{
SDL_WM_SetCaption("Death Desert Racing", NULL);
};
};
SDL_FreeSurface(screen);
Mix_CloseAudio();
SDL_Quit();
return 0;
};
menu.h
#ifndef MENU_H_INCLUDED
#define MENU_H_INCLUDED
#include "include_file.h"
#include "player.h"
struct menu_struct
{
int desert_x;
int desert_y;
int donkey_x;
int donkey_y;
int kawasaki_x;
int kawasaki_y;
SDL_Rect *desert;
SDL_Rect *donkey;
SDL_Rect *kawasaki;
};
menu_struct menu;
void menu_action(void)
{
SDL_Event menu_event;
string level_select = "desert";
string character_select = "donkey";
string vehical_select = "kawasaki";
menu.desert = &clip.level_select[0];
menu.donkey = &clip.player_select[0];
menu.kawasaki = &clip.bike_select[0];
menu.desert_x = 0;
menu.desert_y = 0;
menu.donkey_x = 0;
menu.donkey_y = 0;
menu.kawasaki_x = 0;
menu.kawasaki_y = 0;
int menu_number = 0;
int done = 0;
int x = 0;
int y = 0;
while (done == 0)
{
while (menu_number == 0)
{
if(SDL_PollEvent(&menu_event))
{
x = menu_event.motion.x;
y = menu_event.motion.y;
switch(menu_event.type)
{
case SDL_MOUSEMOTION:
if ((x > menu.desert_x) and (x < menu.desert_x + 128) and (y > menu.desert_y) and (y < menu.desert_y + 128))
{menu.desert = &clip.level_select[1];}
else {menu.desert = &clip.level_select[0];};
break;
};
};
applysurface(menu.desert_x,menu.desert_y,image.desert_select,screen,menu.desert);
SDL_Flip(screen);
};
while (menu_number == 1)
{
if(SDL_PollEvent(&menu_event))
{
x = menu_event.motion.x;
y = menu_event.motion.y;
switch(menu_event.type)
{
case SDL_MOUSEMOTION:
if ((x > menu.donkey_x) and (x < menu.donkey_x + 128) and (y > menu.donkey_y) and (y < menu.donkey_y + 128))
{menu.donkey = &clip.player_select[1];}
else {menu.donkey = &clip.player_select[0];};
break;
};
applysurface(menu.donkey_x,menu.donkey_y,image.donkey,screen,menu.donkey);
SDL_Flip(screen);
};
};
};
};
#endif // MENU_H_INCLUDED
编辑:经过一步的调试,我收到了这些消息。
#0 6812A21B SDL_Flip() (e:projectsdeath_desert_racingbindebugSDL.dll:??)
#1 00401748 menu_action() (E:/Projects/Death_Desert_Racing/menu.h:69)
#2 004019BD SDL_main(argc=1, argv=0x9e0750) (E:ProjectsDeath_Desert_Racingmain.cpp:11)
#3 00402887 console_main(argc=1, argv=0x9e0750) (./src/main/win32/SDL_win32_main.c:315)
#4 00402944 WinMain(hInst=0x400000, hPrev=0x0, szCmdLine=0x241f22 "", sw=10) (./src/main/win32/SDL_win32_main.c:398)
#5 0040205A main(argc=1746172080, argv=0x68134e90, __p__environ=0x68134540 <SDL_RegisterApp+15408>) (../mingw/main.c:73)
我怀疑@John做到了,并且应该为这个答案而受到赞扬。当您开始为clip.level_select[1].x
赋值时,您只是无意中开始压缩属于其他变量的内存。实际结果可能与编译器有关,但在过程中以某种方式更改quit
变量的可能性也不大。尝试更改变量声明:
struct clip_struct
{
SDL_Rect level_select[2];
SDL_Rect player_select[2];
SDL_Rect bike_select[2];
};
看看会发生什么。
我知道我在回答自己的问题,但我终于想明白了!所以我使用了Joachim关于调试步骤的提示,然后它说有一个错误,上面写着:"Program received signal SIGSEGV,Segmentation fault。"因此,我在www.unix.com/programming/27967-Program-received-signal-sigsegov-Segmentation-fault.html上查找了这意味着什么。因此,我仔细查看了SDL_Surface屏幕的声明位置并分配了一个值。事实证明,在variable_setvalue()内部的"include_file.h"中,我不小心将"SDL_Surface*"放在了屏幕之前,这意味着我第一次使用SDL_Flip(屏幕)(位于"menu_action()"中的"menu.h"中)时,程序在没有冻结的情况下立即关闭而崩溃。
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- SDL 窗口不会弹出
- SDL 操纵杆按钮按下/C++
- VSCode IntelliSense无法识别SDL框架的SDL_image扩展库
- C++/SDL "initial value of reference to a non-const must be an lvalue"
- SDL 映像:无法打开映像,仅显示错误消息
- 如何在 3D OpenGL 场景上绘制 SDL 2D 矩形?
- "Undefined symbols for architecture x86_64" 在 SDL 2 (macOS) 中
- 为什么 SDL 在 Mac 上比 Linux 慢得多?
- 在 OSX 上打开 SDL 窗口时不要打开终端窗口
- SDL + OpenGL ES 2.0 使用着色器显示纹理
- SDL GPU 为什么将两个图像分成两个单独的循环更快?
- 在 cmake 中找不到 SDL 库
- SDL/C++: 如何平滑但随机地移动矩形?
- 如何与 SDL 库链接?
- 为什么我的C++代码不呈现 SDL 矩形?
- 为什么要在初始化SDL之前在SDL中声明指针
- C++/SDL 如何覆盖数组?
- 在 SDL 中水平减小静态图像进度条