help with a Galaga remake using SDL

Jan 15, 2012 at 10:45pm
My problem seems to be that my program crashes during

 
if( keys[SDLK_SPACE] ) {theBullets.BulletFired(bVY, bVX, bX, bY, getLocation, onScreen);}


inside of main. Here is the class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
void Bullet::BulletMove(int bVelY, int bVelX, int bullet_x, int bullet_y, Player getLocal)
{
	//players location (creates the bullet wherever the player is)

	bullet_x = getLocal.x;
	bullet_y = getLocal.y + 10;
		
	//bullets velocity
	bVelY = 3;
	bVelX = 0;

	//moves the bullet
	bullet_y -= bVelY;

	if( bullet_y <= 0 || bullet_y >= SCREEN_HEIGHT )
	{
		BulletShowHide( false ) ;
	}
	//else if( /*detects collision*/)
	else
		BulletShowHide( true );

}

void Bullet::BulletShowHide( bool onScreen )
{
	
	if( onScreen == true )
	{
		apply_surface( bullet_x, bullet_y, bullet, screen, NULL );
	}

	if( onScreen == false )
	{
		SDL_FreeSurface (bullet);
	}
}

void Bullet::BulletFired(int bVelY, int bVelX, int bullet_x, int bullet_y, Player getLocal, bool onScreen)
{
	BulletMove(bVelY, bVelX, bullet_x, bullet_y, getLocal);
	BulletShowHide(onScreen);
}


Other notes: this is class Bullet and its a friend of class Player. I was thinking that because I haven't fully implemented a timer to handle how many bullets per second is to be fired it was breaking so I add the Delay in BulletShowHide.

I also thought it might be that certain data members aren't being pointed correctly but alas I feel I have tried everything.

I'm new :)

edit: fixed the "=" and changed delete to FreeSurface



Last edited on Jan 16, 2012 at 9:20pm
Jan 16, 2012 at 12:21am
I am not sure if this is your only problem but in the "void Bullet::BulletShowHide( bool onScreen )" function you need to use == when checking if 2 things are equal to each other, not =.
(Lines 28 and 34)

If that doesn't solve your problem posting more of your code might help.
Jan 16, 2012 at 12:30am
I think the BulletShowHide function looks suspicious. First of all you use = when it should be ==. Second, why are you calling SDL_Delay inside that function. It shouldn't crash because of it but it doesn't make much sense. Also if bullet is a SDL_Surface* you should use SDL_FreeSurface instead of delete. If BulletShowHide is called again when the surface is destroyed you are in trouble. Why not free the surface in the destructor instead?
Jan 16, 2012 at 9:13pm
Thanks James and Peter. Rookie mistakes xD ... I just started learning SDL this week and have been experimenting with everything and have been so worried about SDL specific things I didn't notice "=".

thanks for letting me know about the difference between"delete bullet" and free surface. I'm still at a bit of a loss but you at least got me back on the right path and I appreciate that.

Unfortunately my program still crashes :(
-----------------------------------------------------------------------------
I suppose I should mention that Bullet is a friend of Player and here is the Player code and Time code as well

Player:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Player::Player()
{
    //Initialize the offsets
    x = 200;
    y = 550;

    //Initialize the velocity
    xVel = 0;
    yVel = 0;
}

void Player::handle_input()
{
    //If a key was pressed
    if( event.type == SDL_KEYDOWN )
    {
        //Adjust the velocity
        switch( event.key.keysym.sym )
        {
            case SDLK_LEFT: xVel -= 10 / 3; break;
            case SDLK_RIGHT: xVel += 10 / 3; break;
        }
    }
    //If a key was released
    else if( event.type == SDL_KEYUP )
    {
        //Adjust the velocity
        switch( event.key.keysym.sym )
        {
            case SDLK_LEFT: xVel += 10 / 3; break;
            case SDLK_RIGHT: xVel -= 10 / 3; break;
        }
    }
}

void Player::move()
{
    //Move the dot left or right
    x += xVel;

    //If the dot went too far to the left or right
    if( ( x < 0 ) || ( x + 100 > SCREEN_WIDTH ) )
    {
        //move back
        x -= xVel;
    }
}

void Player::show()
{
    //Show the dot
    apply_surface( x, y, fighter, screen, NULL );
}

I haven't fully implemented this yet but I don't think its the problem
Time:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Timer::Timer()
{
    //Initialize the variables
    startTicks = 0;
    pausedTicks = 0;
    paused = false;
    started = false;
}

void Timer::start()
{
    //Start the timer
    started = true;

    //Unpause the timer
    paused = false;

    //Get the current clock time
    startTicks = SDL_GetTicks();
}

void Timer::stop()
{
    //Stop the timer
    started = false;

    //Unpause the timer
    paused = false;
}

void Timer::pause()
{
    //If the timer is running and isn't already paused
    if( ( started == true ) && ( paused == false ) )
    {
        //Pause the timer
        paused = true;

        //Calculate the paused ticks
        pausedTicks = SDL_GetTicks() - startTicks;
    }
}

void Timer::unpause()
{
    //If the timer is paused
    if( paused == true )
    {
        //Unpause the timer
        paused = false;

        //Reset the starting ticks
        startTicks = SDL_GetTicks() - pausedTicks;

        //Reset the paused ticks
        pausedTicks = 0;
    }
}

int Timer::get_ticks()
{
    //If the timer is running
    if( started == true )
    {
        //If the timer is paused
        if( paused == true )
        {
            //Return the number of ticks when the timer was paused
            return pausedTicks;
        }
        else
        {
            //Return the current time minus the start time
            return SDL_GetTicks() - startTicks;
        }
    }

    //If the timer isn't running
    return 0;
}

bool Timer::is_started()
{
    return started;
}

bool Timer::is_paused()
{
    return paused;
}

Jan 17, 2012 at 6:34pm
I am a silly, silly programmer. I wasn't utilizing private data members fully and that screwed up my compilation. Problem solved.
Topic archived. No new replies allowed.