Thread: Declaring SDL_Rect variables in a class?

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    6

    Declaring SDL_Rect variables in a class?

    Hey all, I've stumbled upon the SDL library, and lately I've been obsessed with c++.

    I'm making a simple game with a spaceship that orbits around the sun, currently I'm in the beginning stages. I want to declare a Spaceship Class, and each class I want to have 2 SLD_Rect structs.

    As of now I initialize the 2 SDl_Rect structs, and then in another function declare all the structs variables, but when I compile the program it just crashes.
    Here is the Space Ship class.
    Code:
    class Spaceship
    {
          public:
                      short int mass;
                      SDL_Rect dest, source;
                      bool initialize(short int locationX, short int locationY, short int imageheight, short int imagewidth)
                      {
                           dest.x = 100;
                           dest.y = 100;
                           dest.w = imagewidth;///this needs to either have image passed into it or needs to have images height and width
                           dest.y = imageheight;
        
                           source.x = 0;
                           source.y = 0;
                           source.w = imagewidth;
                           source.h = imageheight;
                           return 0;
                           }
                           private:
                                   short int stoopid;
    };
    Here is the Main code.

    Code:
    #include "SDL/SDL.h"
    #include "Setupf.h"
    #include "Setupf.cpp"//setup sdl video
    #include "Imageloadfunc.h"
    #include "Imageloadfunc.cpp"//function for loading and formatting images.
    #include "Imageloadfunc.h"
    #include "string"
    #include "SpaceShipClass.cpp"
    
    int main(int argc, char *argv[])//these are needed for sdl to work.
    {
        short int height=1000; 
        short int width=1000;
        short int pixels=32;
        SDL_Surface *screen, *image;
        *screen = (Setup(height,width,pixels));///intializes video
        *image = (FormatLoad("Bitty2.bmp"));//loads and formats bitmap
        short int imageheight;
        short int imagewidth;    
        imagewidth = image->w;
        imageheight = image->h//both of these are passed to Original.initialize;
        Spaceship Original;//the first spaceship member
        Original.initialize(100,100,imageheight,imagewidth);
        SDL_BlitSurface(image,&Original.source,screen,&Original.dest);
        SDL_Flip(screen);
        SDL_Delay(4444444);
        return 0;
    }
    Any help would be really helpful

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    It could be a number of things. I incorrect Video Surface, or whatever. Have you ran it through a debugger to see what it says?

    edit:
    Code:
    imageheight = image->h//both of these are passed to Original.initialize;
    that wont even compile.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    You're not allocating space for screen and image.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    Code:
    bool initialize(short int locationX, short int locationY, short int imageheight, short int imagewidth)
                      {
                           dest.x = 100;
                           dest.y = 100;
                           dest.w = imagewidth;                            
                           dest.y = imageheight;
        
                           source.x = 0;
                           source.y = 0;
                           source.w = imagewidth;
                           source.h = imageheight;
                           return 0;
                           }
    I think u there should be "dest.h".

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Quote Originally Posted by SONU View Post
    I think u there should be "dest.h".
    I didn't look close enough, but that shouldn't crash it, just screw up the values used.

    FlyingIsFun1217

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > #include "Setupf.cpp"//setup sdl video
    > #include "Imageloadfunc.cpp"//function for loading and formatting images.
    > #include "SpaceShipClass.cpp"
    Learn how to set up a project which compiles multiple source files. It might be nice and easy now, but shortly it will become cumbersome, then it will become broken.

    > *screen = (Setup(height,width,pixels));///intializes video
    > *image = (FormatLoad("Bitty2.bmp"));//loads and formats bitmap
    Neither of these point to any memory, so you just write over some random memory locations instead.
    And what's with the extra ( ) anyway?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    6
    Quote Originally Posted by Salem View Post
    > #include "Setupf.cpp"//setup sdl video
    > #include "Imageloadfunc.cpp"//function for loading and formatting images.
    > #include "SpaceShipClass.cpp"
    Learn how to set up a project which compiles multiple source files. It might be nice and easy now, but shortly it will become cumbersome, then it will become broken.

    > *screen = (Setup(height,width,pixels));///intializes video
    > *image = (FormatLoad("Bitty2.bmp"));//loads and formats bitmap
    Neither of these point to any memory, so you just write over some random memory locations instead.
    And what's with the extra ( ) anyway?
    Im sorry if I wasn' to clear, Setup() returns a pointer to the screen, and FormatLoad() returns a pointer to the formatted and loaded image.

    I think my problem might be that I messed up with the assigning the pointers, I'm not really that good with them, I changed dest.y to dest.h like SONU said, but it still crashes out when I run it.

    As for debugging I'm using dev-cpp and I don't get it that well, I understand the breakpoints somewhat,anyone know any tutorials or web sites for it, the book i learned c++ with never really explained it.
    Anyone know a website that explains it?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So do you get any warnings then?
    Because you should be doing.

    screen = (Setup(height,width,pixels));///intializes video
    Is what you want, from you last statement.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    6
    Quote Originally Posted by Salem View Post
    So do you get any warnings then?
    Because you should be doing.

    screen = (Setup(height,width,pixels));///intializes video
    Is what you want, from you last statement.
    Yes i just changed that now, and it still crashes!!

    I'm going to change it around so that the rect is declared in main() and get rid of the class to see if that's the problem.

    EDIT:
    I just did that, and it still crashed so the Rect isn't my problem, it must be either the FormatLoad() or the Setup() functions, can anyone find anything wrong with them?

    Here is the FormatLoad() function

    Code:
    SDL_Surface* FormatLoad(const char* ImageToLoad)
    {
                SDL_Surface *temp;
                temp = SDL_LoadBMP(ImageToLoad);
                SDL_Surface *Image;
                Image = (SDL_DisplayFormat(temp));
                SDL_FreeSurface(temp);
                return Image;
    }
    Possibly a problem is with the const char imagetoload????

    Here is the function Setup()

    Code:
    SDL_Surface* Setup(int height,int width,int pixels)
    {
                 SDL_Init(SDL_INIT_VIDEO);//initializes video
                 atexit(SDL_Quit);//?
                 SDL_Surface *screen;//pointer to the creen
                 screen = SDL_SetVideoMode(height,width,pixels,SDL_DOUBLEBUF);//sets video mode
                 return screen;
    }
    Last edited by pwnz32; 07-27-2008 at 11:19 AM. Reason: Cuz

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Why aren't you checking return codes/values for any of the SDL functions? How do you know they're working?

  11. #11
    Registered User
    Join Date
    Jul 2008
    Posts
    6
    I feel so stupid right now.

    medievalelks got me thinking what could not be working, and my first thought was that my picture, Bitty2.bmp (its a pun, get it bitty? ) might not be in the folder.

    And sure enough when I checked it wasn't there, when I moved it in the program displayed it perfectly.

    Well, thanks guys for the help, i would of never spotted that extra dest.y bug, and I probably wouldn't of ever figured out that the bmp wasn't there if it wasn't for medievalelks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  2. Functions in class and class variables.
    By Karakus in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2006, 03:29 AM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM
  5. variables in instances of a class
    By alloc(MEM_NEWBIE) in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2002, 01:35 PM