So im learning SDL to get an idea on game programming for my career as a game developer.
I havent done any learning for a couple of months after getting stuck on this problem.
So im using a lazyfoo.net tutorial engine. that moves a player and has a collision with a wall.
All that works fine.
But then i tried adding my own feature, when you press space. It displays a box where you were when you pressed space and there is a collision with that box. I called the function "dropBox". But it doesnt work? i get no errors it just doesnt seem to like my code or something.
I appreciate all help, thanks so much so heres the MAIN parts of my scripts:d

//The attributes of the block
const int BLOCK_WIDTH = 30;
const int BLOCK_HEIGHT = 30;



bool load_files()
{
    //Load the square image
    square = load_image( "square.bmp" );
    block = load_image( "block.bmp" );
    //If there was a problem in loading the square
    if( square == NULL )
    {
        return false;
    }

    //If everything loaded fine
    return true;// makes the bool true meaning load_files() is true(completed succesfully)
}



Block::Block()
{
    //Initialize the offsets
    blockBox.x = 200;
    blockBox.y = 300;

    //Set the blocks dimentions
    blockBox.w = BLOCK_WIDTH;
    blockBox.h = BLOCK_HEIGHT;

    bool drop = false;
}

void Block::input_drop()
{
    //If a key was pressed
    if( event.type == SDL_KEYDOWN )
    {
        //Adjust the velocity
        switch( event.key.keysym.sym )
        {
            case SDLK_SPACE: drop == true; break;
        }
    }
    //If a key was released
    else if( event.type == SDL_KEYUP )
    {
        //Adjust the velocity
        switch( event.key.keysym.sym )
        {
            case SDLK_SPACE: drop == false; break;
        }
    }
}

void Block::doDrop(){
    oldx == 100;
    oldy == 130;
    //bool dropped = true;
}

void Block::showBlock(){
    if (drop == true) {
    apply_surface( blockBox.x, blockBox.y, block, screen );
    drop == false;
    }
}





int main( int argc, char* args[] )
{
    //Quit flag
    bool quit = false;

    //The square
    Square mySquare;

    //The block
    Block myBlock;

    //The frame rate regulator
    Timer fps;

    //Initialize
    if( init() == false )
    {
        return 1;
    }

    //Load the files
    if( load_files() == false )
    {
        return 1;
    }

    //Set the right wall
    wall.x = 300;
    wall.y = 40;
    wall.w = 40;
    wall.h = 400;

    //Set the top wall
    wall2.x = 100;
    wall2.y = 40;
    wall2.w = 200;
    wall2.h = 40;

    //While the user hasn't quit
    while( quit == false )
    {
        //Start the frame timer//COUNTS EACH FRAME?? SO THIS CODE RUNS ONCE PER FRAME?
        fps.start();

        //While there's events to handle
        while( SDL_PollEvent( &event ) )
        {
            //Handle events for the square
            mySquare.handle_input();
            myBlock.input_drop();

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

        //Move the square
        mySquare.move();
        //Do the drop stuff
        myBlock.doDrop();

        //Fill the screen white
        SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );

        //Show the wall
        SDL_FillRect( screen, &wall, SDL_MapRGB( screen->format, 0x77, 0x77, 0x77 ) );

        //Show the other wall
        SDL_FillRect( screen, &wall2, SDL_MapRGB( screen->format, 0x77, 0x77, 0x77 ) );

        //Show the square on the screen
        mySquare.show();
        //Show the dropped block on the screen
        myBlock.showBlock();

        //Update the screen
        if( SDL_Flip( screen ) == -1 )
        {
            return 1;
        }

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

    //Clean up
    clean_up();

    return 0;
}

and my drop class file

#include "SDL_image.h"
//The block
class Block
{
    private:
    //The collision box of the square
    SDL_Rect blockBox;

    int oldx, oldy;
    bool drop;

    public:
    //Initializes the variables
    Block();

    //Takes key presses
    void input_drop();

    //Does the drop
    void doDrop();

    //Shows the dropped block
    void showBlock();
};

So they are just snippets of code from my game that resemble the dropping of the block, the rest of my code is timers and junk, if you want the rest of the code its here: http://www.daniweb.com/software-development/cpp/threads/418054/need-help-with-simple-game
If anyone can figure this out, it will be absolutely amazing and will only fuel my ambitions to be a game developer.
Im 16 by the way and i suck at programming LOL but im alot better than when i started, i know this game script is a bit more complicated then what i should be learning at the moment but i cannot learn anything else until this is solved(im that kind of person) i need to know whats wrong or else il never learn and make the same mistake in the future.

please someone? any help is appreciated? il upload the files if you want me to

I have attached a rewrite of your program because if you ever want future help your code has to be structured.

I would recommend having main.cpp with pretty much just main() in it so you can read the flow of the program and if you want to know what happens in a function then you go open up that header/source file.

Obviously this is just the way I like to structure small games like this and others would disagree/change aspects of it; however, I would hope everyone would agree that having 99% of your code in main.cpp just makes finding errors extremely difficult.

There were many areas where you used the comparison operator (==) rather than the assignment operator (=) which doesn't do what you want and if you have warnings turned off it doesn't tell you that it does nothing.

As far as fixing the problem with the box goes, I changed it so that the dropping of the box is handled by the player class rather than having a seperate handle for the box. The main issue with the box in the first place was that you were making it visible by setting drop to true then right after you released the key you set it to false (which makes the box disappear).

If you have any questions feel free to ask.

commented: THANK YOU!!!! +0

I have attached a rewrite of your program because if you ever want future help your code has to be structured.
I would recommend having main.cpp with pretty much just main() in it so you can read the flow of the program and if you want to know what happens in a function then you go open up that header/source file.
Obviously this is just the way I like to structure small games like this and others would disagree/change aspects of it; however, I would hope everyone would agree that having 99% of your code in main.cpp just makes finding errors extremely difficult.
There were many areas where you used the comparison operator (==) rather than the assignment operator (=) which doesn't do what you want and if you have warnings turned off it doesn't tell you that it does nothing.
As far as fixing the problem with the box goes, I changed it so that the dropping of the box is handled by the player class rather than having a seperate handle for the box. The main issue with the box in the first place was that you were making it visible by setting drop to true then right after you released the key you set it to false (which makes the box disappear).
If you have any questions feel free to ask.

that worked perfectly, thanks for changing the image of the player aswell lol, ive never had an actual image in anything ive made. What youve done is exactly what ive always wanted, putting classes in seperate files. I love how it looks because its so much easier to find things. But i never knew how to do it.
But however there is 1 small bug(not with your code, but with lazyfoo.net's i believe)
You can move diagonally in all directions fine, but when you hold left and up while holding space it only moves up, not diagonally. Any suggestions on why thats occurring?
Thanks so so much for your help you have no idea how much this means to me, you rule!!

Instead of using a switch to manage what key is down just use if statements.

if( event.key.keysym.sym == SDLK_SPACE )
{
    //drop box
}
if( event.key.keysym.sym == SDLK_UP )
{
    //move up
}
if( event.key.keysym.sym == SDLK_DOWN )
{
    //move down
}
if( event.key.keysym.sym == SDLK_RIGHT )
{
    //move right
}
if( event.key.keysym.sym == SDLK_LEFT )
{
    //move left
}

Instead of using a switch to manage what key is down just use if statements.

ok sweet thanks now i have 1 more question. How would i go about making the user being able to place more than 1 block? rather than just moving the current one around. Would i need to use some sort of array that adds the x and y value to the array everytime the space is pressed and somehow show a box at each coordinate.
Would this be really complicated? should i even attempt it or is it just dead hard. thanks again

You would just need to change a few things.

In this case I would use a vector container for the blocks because it can grow dynamically (arrays can but it is a bit more work). This means you need to include the <vector> header within "lib.h" and since you are not using namespace std globally you need to refer to vector as std::vector (unless you want to just place using namespace std; or using std::vector; in "lib.h").

So instead of having myBlock of type BLOCK in globals you would replace that with vector<BLOCK> blocks; in both globals.h and globals.cpp.

Within the BLOCK class I would just strip it down to

//block.h
class BLOCK
{
    private:
    //The collision box of the square
    SDL_Rect blockBox;

    public:
    //Initializes the variables
    BLOCK(){};
    BLOCK(SDL_Rect pos);

    //Shows the dropped block
    void ShowBlock();
};

//block.cpp
BLOCK::BLOCK(SDL_Rect pos)
{
    //Initialize the offsets
    blockBox.x = pos.x;
    blockBox.y = pos.y;

    //Set the blocks dimentions
    blockBox.w = BLOCK_WIDTH;
    blockBox.h = BLOCK_HEIGHT;
}

void BLOCK::ShowBlock()
{
    ApplySurface( blockBox.x, blockBox.y, blockTexture, screen );
}

Since the block is not going to exist until you "drop" it then you don't really need a boolean variable to keep track of if it has been dropped or not. In this method of managing the blocks you move the block into position by creating it with the constructor, rather than moving it after it has been created.

In order to create the block you should go into the HandleInput() within player.cpp and change the line to drop the block to blocks.push_back(BLOCK(box)); (this adds a new block at the players position to the end of the blocks vector).

The last thing to do is displaying the blocks in the vector container, to do this you need to create a for loop that goes from 0 to blocks.size() and then index each box and call its ShowBlock() function (indexing vectors is the same as indexing arrays).

If you want to clean up the whole wall part of your code you add the walls onto the vector of blocks (this will make them have the same texture as the dropped blocks but you can either add in a "type" variable that tells the block if it is going to use the drop block texture or just a solid grey (or a different texture for the wall).

This can easily be done by adding a char or int to the constructor that can be 'w' for wall and 'b' for block and then in the display function you just put an if() statement in.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.