LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Can't seem to find reason why case/if ControlsStructs aren't working properly... (http://www.linuxquestions.org/questions/programming-9/cant-seem-to-find-reason-why-case-if-controlsstructs-arent-working-properly-441126/)

RHLinuxGUY 05-03-2006 12:59 AM

Can't seem to find reason why case/if ControlsStructs aren't working properly...
 
Before I program my main game differently, I usualy create a side program so see if what I am doing is going to work. Well, I don't know what is wrong with this program I made, it makes absolutely no sense to me why it is going from one case or if statement to another even though it is not the correct statement. (I believe this is the correct term, correct me if I am wrong.) Here is the code:

Code:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <SDL/SDL.h>
using namespace std;

        SDL_Surface *screen;       

class X_Y
{
        public:
        //Good Guys location, should stay the same.
        int xg;
        int yg;
        int xgl;
        int ygl;

        //Bad Guys location, IS going to be changed in the future.
        int xb;
        int yb;
        int xbl;
        int ybl;

        int x_result;
        int y_result;

        void xyconstruct()
        {
                xg = 0;
                yg = 0;
                xgl = 0;
                ygl = 0;

                xb = 288;
                yb = 128;
                xbl = 9;
                ybl = 4;

                x_result = 0;
                y_result = 0;
        }

}xy;

void move_xy(int MOVE)
{
        cout << endl << MOVE << endl;
       
        switch (MOVE){
                case 1:
                        xy.xg = xy.xg - 32;
                        cout << endl << xy.xg << " = " << xy.xg << " - 32.\n";
                        if(xy.xg > 0){ xy.xg = 0; }
                        break;
                case 2:
                        xy.xg = xy.xg + 32;
                        cout << endl << xy.xg << " = " << xy.xg << " + 32.\n";
                        break;
                default:
                cout << "\n\tSomething fucked up as usual\n";
        }
               
       
}
               

int main(int nNumberofArgs, char* pszArgs[])
{       
        if(SDL_Init(SDL_INIT_VIDEO)==-1)
        {
                printf("\n\tCouldn't initialize Init_SDL(): %s", SDL_GetError());
                exit(-1);
        }

        xy.xyconstruct();

        screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);       

        bool done = 0;
       
        //No need for this.
        int DIRECTION;

        SDL_Event event;
       
        do{
                while(SDL_PollEvent(&event)){
                        switch(event.type){
                                case SDL_KEYDOWN:
                                switch(event.key.keysym.sym){
                                        case SDLK_LEFT:
                                                move_xy(1);
                                        case SDLK_RIGHT:
                                                move_xy(2);
                                        case SDLK_UP:
                                                //NOT IMPLEMENTED YET!
                                                break;
                                        case SDLK_DOWN:
                                                //NOT IMPLEMENTED YET!
                                                break;                                       
                                        case SDLK_ESCAPE:
                                                done = 1;
                                                break;
                                        default:
                                                printf("\n\tThis wasn't supposed to happened: Check line 62\n");
                                                break;
                                }
                                case SDL_KEYUP:
                                        switch (event.key.keysym.sym){
                                                case SDLK_LEFT:
                                                        break;
                                                case SDLK_RIGHT:
                                                        break;
                                                case SDLK_UP:
                                                        break;
                                                case SDLK_DOWN:
                                                        break;
                                        }
                                }
                        }
                }while(done == 0);
        SDL_Quit();

return 0;
}

I have tried using a if control structure but with the same outcome. It starts up just fine, but when I press the left key, it does not seem to want to subtract 32 from xy.xg. This does not seem complicated to me, but it frustrates me when things as simple as above should go smooth don't.

addy86 05-03-2006 01:52 AM

Code:

case SDLK_LEFT:
        move_xy(1);
case SDLK_RIGHT:
        move_xy(2);

You forgot the break-statements.
Tip: always compile with -Wall -Wextra. As far as I know, gcc will warn you about such things.

RHLinuxGUY 05-03-2006 02:09 AM

O wow, that is pretty pathetic. Thank you addy86!


All times are GMT -5. The time now is 05:42 PM.