Thread: SDL error

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

    SDL error

    I'm following this tutorial for sdl that is kind of not clear in some points so I tried to do what I could and it gave me this error
    Code:
    C:\Users\Bijan\Documents\C++ Programs\SDL tutorial\main.cpp|158|error: request for member `x' in `((Button*)this)->Button::box', which is of non-class type `SDL_Rect*'|
    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("TTF Test", 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;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    box is pointer

    use box->x = x;

    good luck!

    (and btw, unless you edited the SDL libraries or have an old version ... SDL_Image has a lowercase i in its name, not Image.. its image)
    Last edited by rodrigorules; 05-02-2010 at 10:09 AM.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  2. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  3. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  4. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM