gpt4 book ai didi

c++ - 库调用的段错误

转载 作者:行者123 更新时间:2023-11-28 03:39:58 29 4
gpt4 key购买 nike

我正在为我正在编写的游戏使用一个名为 Chipmunk 的物理库。

在我的initialize 函数中,我初始化了全局变量cpSpace 空间。然后在更新中调用 cpSpaceStep(space, timestep)。此函数的原型(prototype)是 void cpSpaceStep(cpSpace *space, cpFloat dt);。我在这个函数调用中遇到了段错误。我在下面的代码中标记了这两个函数调用。

完整代码如下:

#include "../include/SDL/SDL_image.h"
#include "../include/SDL/SDL.h"
#include "../include/Player.h"
#include "../include/Timer.h"
#include "../include/Block.h"
#include "../include/ImageLoader.h"
#include "../include/chipmunk/chipmunk.h"
#include <string>

//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The frame rate
const int FRAMES_PER_SECOND = 60;

SDL_Event event;
SDL_Surface *screen = NULL;
SDL_Surface *player_img = NULL, *block_img = NULL;
Player *player;
Timer fps;
cpSpace *space;

bool quit = false;

void initialize();
void update();

int main( int argc, char* argv[] )
{
initialize();
update();

return 1;
}

void initialize()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{

}

//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

//If there was an error in setting up the screen
if( screen == NULL )
{

}

//Set the window caption
SDL_WM_SetCaption( "Move the Dot", NULL );

cpVect gravity = cpv(0, 100);

//******************cpSpacenew()*****************
//This is where space is init'ed
space = cpSpaceNew();
//***********************************************


}

void update()
{
//While the user hasn't quit
while( quit == false )
{
//Start the frame timer
fps.start();

while( SDL_PollEvent( &event ) )
{
//Handle events for the dot
player->handle_input( &event );

//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}

player->update();

cpFloat timeStep = 1.0/FRAMES_PER_SECOND;

//************************Segfault**********************************************
cpSpaceStep(space, timeStep);
//******************************************************************************

//Cap the frame rate
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
}

最佳答案

您的 cpInitChipmunk() 调用在哪里?否则,cpSpaceNew() 很可能会返回 NULL(或垃圾)。

很容易检查。在调用 cpSpaceNew() 之后,立即插入:

printf ("%p\n", space);

(或类似的东西,看看值是什么。

您可能还想在尝试使用它之前立即执行此操作,以防万一损坏它。

关于c++ - 库调用的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9578248/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com