Thread: Returning 3 for no reason

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    Returning 3 for no reason

    when this program compiles it return 3

    here is my code
    Code:
    #include <SDL/SDL.h>
    #include <SDL/SDL_Image.h>
    #include <string>
    
    //screen atributes
    const int SCREEN_WIDTH = 640;
    const int SCREEN_HEIGHT = 480;
    const int SCREEN_BPP = 32;
    
    //the button states in the sprite sheet
    int CLIP_MOUSEOVER = 0;
    int CLIP_MOUSEOUT = 1;
    int CLIP_MOUSEDOWN = 2;
    int CLIP_MOUSEUP = 3;
    
    //surfaces
    SDL_Surface *buttonSheet = NULL;
    SDL_Surface *screen = NULL;
    
    //the event
    SDL_Event event;
    
    //the clip region of the sprite sheet
    SDL_Rect clips[4];
    
    //the button
    class Button
    {
        private:
        //the atributes of the button
        SDL_Rect *box;
        //the part of the button's sprite sheet that will be shown
        SDL_Rect *clip;
    
        public:
        //initialize the variables
        Button(int x, int y, int w, int h);
        //handles events and set the button's sprite region
        void handle_events();
        //show button on screen
        void show();
    };
    
    SDL_Surface *load_image(std::string filename){
        //the image that's loaded
        SDL_Surface *loaded_image = NULL;
    
        //The optimized image that will be used
        SDL_Surface *optimizedImage = NULL;
    
        //load the image
        loaded_image = IMG_Load(filename.c_str());
    
        //if image is loaded
        if(loaded_image != NULL){
            //create optimized image
            optimizedImage = SDL_DisplayFormat(loaded_image);
    
            //free old surface
            SDL_FreeSurface(loaded_image);
    
        }
        if(optimizedImage != NULL){
            //map the color key
            Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 0xFF, 0xFF, 0);
            //set color key of color R 0xFF, G 0xFF, and B 0 to be transparent
            SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorkey);
        }
        return optimizedImage;
    }
    
    void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip){
        SDL_Rect offset;
    
        offset.x = x;
        offset.y = y;
    
        SDL_BlitSurface(source, NULL, destination, &offset);
    }
    
    bool init(){
        if(SDL_Init(SDL_INIT_EVERYTHING) == -1){
            return false;
        }
        screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
    
        if(screen == NULL){
            return false;
        }
    
        //set window caption
        SDL_WM_SetCaption("Mouse Events", NULL);
    
        return true;
    }
    
    bool load_files(){
        //load image
        buttonSheet = load_image("button.png");
    
        if(buttonSheet == NULL){
            return false;
        }
    
        return true;
    }
    
    void clean_up(){
        SDL_FreeSurface(buttonSheet);
    
        SDL_Quit();
    }
    
    void set_clips()
    {
        //clip the sprite sheet
        clips[CLIP_MOUSEOVER].x = 0;
        clips[CLIP_MOUSEOVER].y = 0;
        clips[CLIP_MOUSEOVER].w = 320;
        clips[CLIP_MOUSEOVER].h = 240;
    
        clips[CLIP_MOUSEOUT].x = 320;
        clips[CLIP_MOUSEOUT].y = 0;
        clips[CLIP_MOUSEOUT].w = 320;
        clips[CLIP_MOUSEOUT].h = 240;
    
        clips[CLIP_MOUSEDOWN].x = 0;
        clips[CLIP_MOUSEDOWN].y = 240;
        clips[CLIP_MOUSEDOWN].w = 320;
        clips[CLIP_MOUSEDOWN].h = 240;
    
        clips[CLIP_MOUSEUP].x = 320;
        clips[CLIP_MOUSEUP].y = 240;
        clips[CLIP_MOUSEUP].w = 320;
        clips[CLIP_MOUSEUP].h = 240;
    }
    
    Button::Button(int x, int y, int w, int h){
        //set the button's attributes
        box->x = x;
        box->y = y;
        box->w = w;
        box->h = h;
    
        clip = &clips[CLIP_MOUSEOUT];
    }
    
    void Button::handle_events()
    {
        //the mouse offsets
        int x = 0, y = 0;
    
        //if mouse is moved
        if(event.type = SDL_MOUSEMOTION){
            x = event.motion.x;
            y = event.motion.y;
    
            if( (x > box->x) && (x < box->x + box->w) && (y > box->y) && (y < box->y + box->h)){
                clip = &clips[CLIP_MOUSEOVER];
            }
            else{
            clip = &clips[CLIP_MOUSEOUT];
            }
            //if mouse button was pressed
            if(event.type == SDL_MOUSEBUTTONDOWN){
                //if the left button was pressed
                if(event.button.button == SDL_BUTTON_LEFT){
                    //get mouse offsets
                    x = event.button.x;
                    y = event.button.y;
    
                    //if the mouse is over button
                    if( (x > box->x) && (x < box->x + box->w) && (y > box->y) && (y < box->y + box->h)){
                        //set button sprite
                        clip = &clips[CLIP_MOUSEDOWN];
                    }
                }
            }
            //if mouse was released
            if(event.type == SDL_MOUSEBUTTONUP){
                //if left mouse button was released
                if(event.button.button == SDL_BUTTON_LEFT){
                    //get mouse offsets
                    x = event.button.x;
                    y = event.button.y;
    
                    //if the mouse was over the button
                    if( (x > box->x) && (x < box->x + box->y) && (y > box->y) && (y < box->y + box->h)){
                        //set button to sprite
                        clip = &clips[CLIP_MOUSEUP];
                    }
                }
            }
        }
    }
    
    void Button::show(){
        //show sprite
        apply_surface(box->x, box->y, buttonSheet, screen, clip);
    }
    
    int main(int argc, char *args[]){
        //makes sure program waits for a quit
        bool quit = false;
    
        //initialize
        if(init() == false){
            return 1;
        }
        //load files
        if(load_files() == false){
            return 1;
        }
    
    
        // clip the sprite sheet
        set_clips();
        //make the button
        Button myButton(170, 120, 320, 240);
    
        //while the user hasn't quit yet
        while(quit == false){
            //while there is an event to handle
            while(SDL_PollEvent(&event)){
                //handle button events
                myButton.handle_events();
                //if a key was pressed
                }
                //if the users Xs out of the window
                if(event.type == SDL_QUIT){
                    quit = true;
                }
                //fill the screen white
                SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
               //update screen
               if(SDL_Flip(screen) == -1){
                   return 1;
               }
               myButton.show();
            }
    
        clean_up();
    
        return 0;
    }
    I cannot figure out why it's doing that, please help


    p.s
    I'm pretty sure I posted a thread like this but i can't seem to find it, if you could give me a link to that it would be nice

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    When you say "when it compiles it returns 3" do you mean you get three compile errors when you try to run it, or that it compiles but something bad happens when you try to run it?

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    it says
    "Process terminated with status 3 (0 minutes, 1 seconds)"

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    confirm these things

    -the image button.png exists in the same folder as your program
    -you are not blipping a null surface (you assigned the surface correctly before using it)

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    ok I made some changes in the first one but now I'm having the problem in this code
    Code:
    #include <SDL/SDL.h>
    #include <SDL/SDL_image.h>
    #include <SDL/SDL_ttf.h>
    #include <string>
    
    using namespace std;
    
    //if the user has not quit
    bool quit = false;
    //intergers
    int x;
    int y;
    int player = 1;
    
    //font stuff
    TTF_Font *font = NULL;
    SDL_Color textColor = {43, 250, 250};
    
    //surfaces
    SDL_Surface *screen;
    SDL_Surface *background;
    SDL_Surface *writing;
    SDL_Surface *O;
    SDL_Surface *X;
    SDL_Surface *b1;
    SDL_Surface *b2;
    SDL_Surface *b3;
    SDL_Surface *b4;
    SDL_Surface *b5;
    SDL_Surface *b6;
    SDL_Surface *b7;
    SDL_Surface *b8;
    SDL_Surface *b9;
    
    //event
    SDL_Event event;
    
    void handle_events(){
        if(SDL_PollEvent(&event)){
                if(event.type == SDL_QUIT){
                    quit = true;
                }
                if(event.type == SDL_MOUSEMOTION){
                    x = event.motion.x;
                    y = event.motion.y;
                }
                if(event.type == SDL_MOUSEBUTTONUP){
                    if(event.button.button == SDL_BUTTON_LEFT){
                        if(x <= 200 && y <= 147){
                            if(b1 == NULL){
                                if(player == 1){
                                b1 = X;
                                player = 2;
                                }
                                else if(player == 2){
                                    b1 = O;
                                    player = 1;
                                }
                            }
                        }
                        if(x <= 411 && x >= 213 && y <= 147){
                            if(b2 == NULL){
                                if(player == 1){
                                b2 = X;
                                player = 2;
                                }
                                else if(player == 2){
                                    b2 = O;
                                    player = 1;
                                }
                            }
                        }
                        if(x <= 624 && x >= 424 && y <= 147){
                            if(b3 == NULL){
                                if(player == 1){
                                b3 = X;
                                player = 2;
                                }
                                else if(player == 2){
                                    b3 = O;
                                    player = 1;
                                }
                            }
                        }
                        if(x <= 200 && y <= 305 && y >= 160){
                            if(b4 == NULL){
                                if(player == 1){
                                b4 = X;
                                player = 2;
                                }
                                else if(player == 2){
                                    b4 = O;
                                    player = 1;
                                }
                            }
                        }
                        if(x <= 410 && x >= 213 && y <= 305 && y >= 160){
                            if(b5 == NULL){
                                if(player == 1){
                                b5 = X;
                                player = 2;
                                }
                                else if(player == 2){
                                    b5 = O;
                                    player = 1;
                                }
                            }
                        }
                        if(x <= 623 && x >= 423 && y <= 307 && y >= 160){
                            if(b6 == NULL){
                                if(player == 1){
                                b6 = X;
                                player = 2;
                                }
                                else if(player == 2){
                                    b6 = O;
                                    player = 1;
                                }
                            }
                        }
                        if(x <= 199 && y <= 464 && y >= 318){
                            if(b7 == NULL){
                                if(player == 1){
                                b7 = X;
                                player = 2;
                                }
                                else if(player == 2){
                                    b7 = O;
                                    player = 1;
                                }
                            }
                        }
                        if(x <= 410 && x >=  212 && y <= 465 && y >= 318){
                            if(b8 == NULL){
                                if(player == 1){
                                b8 = X;
                                player = 2;
                                }
                                else if(player == 2){
                                    b8 = O;
                                    player = 1;
                                }
                            }
                        }
                        if(x <= 623 && x >= 423 && y <= 466 && y >= 320){
                            if(b9 == NULL){
                                if(player == 1){
                                b9 = X;
                                player = 2;
                                }
                                else if(player == 2){
                                    b9 = O;
                                    player = 1;
                                }
                            }
                        }
                    }
                }
            }
    }
    
    SDL_Surface *load_image(string filename){
        SDL_Surface *surface = NULL;
    
        surface = IMG_Load(filename.c_str());
    
        return surface;
    }
    
    bool load_files(){
        //open font
        font = TTF_OpenFont("lazy.ttf", 28);
    
        if(font == NULL){
            return false;
        }
        return true;
    }
    
    void apply_surface(int X, int Y, SDL_Surface *image, SDL_Surface *destination){
        SDL_Rect offset;
    
        offset.x = X;
        offset.y = Y;
    
        SDL_BlitSurface(image, NULL, destination, &offset);
    }
    
    bool init(){
        if(SDL_Init(SDL_INIT_EVERYTHING) == -1){
            return false;
        }
    
        screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
    
        if(screen == NULL){
            return false;
        }
        if(TTF_Init() == -1){
            return false;
        }
    
        SDL_WM_SetCaption("Tic Tac Toe", NULL);
    
        return true;
    }
    
    void clean_up(){
        //free surface
        SDL_FreeSurface(background);
        SDL_FreeSurface(O);
        SDL_FreeSurface(X);
        SDL_FreeSurface(b1);
        SDL_FreeSurface(b2);
        SDL_FreeSurface(b3);
        SDL_FreeSurface(b4);
        SDL_FreeSurface(b5);
        SDL_FreeSurface(b6);
        SDL_FreeSurface(b7);
        SDL_FreeSurface(b8);
        SDL_FreeSurface(b9);
    
        TTF_CloseFont(font);
    
        SDL_Quit();
    
        TTF_Quit();
    }
    
    int main(int argc, char *args[]){
        if(init() == false){
            return 1;
        }
        if(load_files == false){
            return 1;
        }
    
        background = load_image("background.png");
        X = load_image("X.png");
        O = load_image("O.png");
    
        if(background == NULL){
            return 1;
        }
    
        while(!quit){
    
            if(player == 1){
                writing = TTF_RenderText_Solid(font, "It is Player 1's turn.", textColor);
                if(writing == NULL){
                return 1;
            }
                while(player == 1){
                    apply_surface(234, 18, writing, screen);
                }
                SDL_FreeSurface(writing);
            }
            else if(player == 2){
                writing = TTF_RenderText_Solid(font, "It is Player 2's turn.", textColor);
                if(writing == NULL){
                return 1;
            }
                while(player == 2){
                    apply_surface(234, 18, writing, screen);
                }
                SDL_FreeSurface(writing);
            }
    
    
            handle_events();
    
            //make screen color
            SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
            //draw picture on screen
            apply_surface(0, 0, background, screen);
            apply_surface(0,0, b1, screen);
            apply_surface(213, 0, b2, screen);
            apply_surface(424, 0, b3, screen);
            apply_surface(0, 160, b4, screen);
            apply_surface(213, 160, b5, screen);
            apply_surface(423, 160, b6, screen);
            apply_surface(0, 318, b7, screen);
            apply_surface(212, 318, b8, screen);
            apply_surface(423, 320, b9, screen);
    
            if(SDL_Flip(screen) == -1){
                return 1;
            }
        }
    
        clean_up();
    
        return 0;
    }
    something that might help pin point the problem is that the code worked fine until I added the text things.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    make sure font isn't null when using it

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    but before I draw it I put
    Code:
    if(writing == NULL){
                return 1;
            }

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    i meant your variable named font

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    that to
    Code:
    if(font == NULL){
            return false;
        }

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    Code:
        if(load_files == false){
            return 1;
        }
    this is wrong, font never loaded then

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with struct... not returning correct results.
    By drty2 in forum C Programming
    Replies: 7
    Last Post: 01-18-2009, 11:25 PM
  2. function returning hour in either 12 or 24 hour format
    By stanlvw in forum C Programming
    Replies: 4
    Last Post: 01-01-2008, 06:02 AM
  3. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM
  4. Function returning incorrect value
    By CHurst in forum C Programming
    Replies: 3
    Last Post: 12-13-2005, 01:27 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM