I'm using a physics library called Chipmunk for a game that I'm writing.
In my initialize
function I initialize the global variable cpSpace space
. Then in update I call cpSpaceStep(space, timestep)
. The prototype for this function is void cpSpaceStep(cpSpace *space, cpFloat dt);
. I'm getting a segfault on this function call. I've marked those two function calls in the code below.
The full code is below:
#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() );
}
}
}