SDL, handing Surface and Rect from an object to another?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
SDL, handing Surface and Rect from an object to another?
Greetings!
I want to get into C++ programming, using SDL to support me.
Now I have a seemingly simple problem which has been driving me nuts for hours and I seem to be stuck.
So here goes:
I have two instances of different structs. Let us call them Graphics and Entity. I use Graphics to initialize the video mode and to draw images. However to be able to have multiple Entities (players, whatever), I dumped the code to load the image and get its sizing attributes into the Entity struct.
The function containing this line is called from within a function in the Entity instance. Thus I have to hand the following as parameters to the Graphics.draw function:
Code:
SDL_Surface *img; SDL_Rect srect; SDL_Rect drect;
I have tried to use Entity's global (as it is a struct) variables from within Graphics, I have tried declaring new variables in the Graphics struct to which the parameters are saved and I have tried to do the same thing with pointers. The latter two solutions are giving me the error
Code:
invalid use of member <var- or pointername>
.
I am totally confused now, am I trying to do something wrong essentially? Or is it possible to do it this way and I'm just making a small mistake somewhere?
As you can see, I am not yet comfortable with the concept of C++ but I want to learn and I tried to state my problem as clearly as possible. If you have more questions, please feel free to ask! Any suggestions are highly appreciated!
Alright, I will, although it may be somewhat messed up right now because of all i tried to get it working.
The first struct is the graphics one:
Code:
class graphics
{
private:
SDL_Surface *display;
public:
void initialize(int, int, int);
void drawentity(SDL_Surface, SDL_Rect, SDL_Rect);
};
void graphics::drawentity(image, destrect, srcrect) //This is where the error occurs.
{
SDL_BlitSurface(image, &srcrect, display, &destrect);
SDL_UpdateRects(display,1,&destrect);
SDL_Delay(3000);
SDL_FreeSurface(image);
};
(The member function 'initialize' works, so I left it out)
Then the struct entity is defined:
Code:
struct entity
{
private:
int x_image, y_image;
SDL_Surface *img;
SDL_Rect drect, srect;
public:
void create(int, int);
};
void entity::create(x_image, y_image);
{
// Load image
img = IMG_Load("graphics.jpg");
if (img == NULL)
{
fprintf(stderr, "Could not load image: %s\n",SDL_GetError());
exit(-1);
}
fprintf(stdout,"Debug: Image-Surface-Size: %i x %i pixel\n",
img->w,img->h);
srect.x = 0;
srect.y = 0;
srect.w = img->w;
srect.h = img->h;
drect.x = x_img;
drect.y = y_img;
drect.w = img->w;
drect.h = img->h;
graphics.drawentity(img, drect, srect); //This should call graphics->drawentity to actually draw the graphics of this entity.
};
And finally this is part of my main function from where the instances of entity and graphics are made and their functions are called:
Code:
int main()
{
int x, y, d, x_image, y_image;
graphics Maindisplay;
entity Player;
Maindisplay.initialize(x, y, d);
Player.create(x_image, y_image);
return 0;
}
The strange thing is at first I had the code of entity::create in graphics::initialize and it worked perfectly. The image file would be drawn onto the screen. But now that I have a new class to be able to draw multiple images, I have trouble handing the image data to the function which is to draw it (graphics::drawentity).
What is it that I'm doing wrong? I have also tried pointers and making the variables inside entity::create global and access them from inside graphics::drawentity. But to no avail.
Regardless of how I try to hand over the information, I get
"invalid use of member <var- or pointername>"
or "<variablename> not declared in this scope>"
same goes for entity::create but that works due to a feature of C
im wondering why your using create and initialise instead of constructors, but as your beginning i'll leave you to look that up.
there is no difference between a struct and class in c++(well structs default to public access and classes default to private access) it is convention from C that if it only has data then its a struct else its a class. entity has functions so i would make it a class.
graphics is a class not an object so graphics.member makes no sense, you need entity to have access to an instance of graphics or make drawentity a static function which would then require a different way of handling display in graphics and would probably require a singleton.
dont bother trying to make drawentity static it would be a pain to implement and i dont think your c++ is ready for singletons yet.
your advice is appreciated, I now changed the structs to classes and made the appropriate functions public. I also made the necessary adjustments to the function definitions.
However I still have the same problem, namely:
As you can see I did not declare the variables image, destrect and srcrect in the class declaration as I thought this was not necessary. When I try to compile, I still get the error that 'image (and the others) are not declared in this scope'. When I do declare them, I get the errors 'invalid use of member graphics::image'.
Not only that, I just noticed that I now get the same error even with the int variables in graphics::iniialize, which worked only some time ago! I didn't modify anything there!
I'm starting to become desperate... maybe C++ is unpredictable as someone told me or I'm just really not made for coding... *lol*
ive fixed it(a few incorrect declarations, extra semicolons, etc) apart from the graphics.drawentity() problem, because fixing this will require a re-write. it seems to me your trying to run before you can walk. i suggest before you fiddle about with sdl try to get a solid C++ background. try the tutorial at www.cplusplus.com
Code:
#include <cstdio>
#include <cstdlib>
#include <SDL/SDL.h>
//sdl_image header whatever its called, i dont have it
using namespace std;
class graphics
{
private:
SDL_Surface *display;
public:
void initialize(int, int, int);
void drawentity(SDL_Surface *, SDL_Rect, SDL_Rect);
};
void graphics::drawentity(SDL_Surface *image, SDL_Rect destrect, SDL_Rect srcrect)
{
SDL_BlitSurface(image, &srcrect, display, &destrect);
SDL_UpdateRects(display,1,&destrect);
SDL_Delay(3000);
SDL_FreeSurface(image);
}
class entity
{
private:
int x_image, y_image;
SDL_Surface *img;
SDL_Rect drect, srect;
public:
void create(int, int);
};
void entity::create(int x_image, int y_image)
{
// Load image
img = IMG_Load("graphics.jpg");
if (img == NULL)
{
fprintf(stderr, "Could not load image: %s\n",SDL_GetError());
exit(-1);
}
fprintf(stdout,"Debug: Image-Surface-Size: %i x %i pixel\n",
img->w,img->h);
srect.x = 0;
srect.y = 0;
srect.w = img->w;
srect.h = img->h;
drect.x = x_image;
drect.y = y_image;
drect.w = img->w;
drect.h = img->h;
// graphics.drawentity(img, drect, srect);
}
int main()
{
int x, y, d, x_image, y_image;
graphics Maindisplay;
entity Player;
Maindisplay.initialize(x, y, d);
Player.create(x_image, y_image);
return 0;
}
I will save your code and will go through it thoroughly to see how it should be done!
Well I wrote two little test programs with 2 classes each and they both worked after some fiddling so I thought this would be the time to try something more complex. But maybe I should really get some more experience with the language first. I seem to have a lot of trouble with inter-class-communication if I may call it that way.
The only problem is now.. I'm not very creative... anyone want any simple problems solved? Just kidding...
Good bye and thanks again for all the help!
Last edited by gundelgauk; 09-18-2003 at 06:58 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.