LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2003, 11:11 PM   #1
gundelgauk
Member
 
Registered: Jul 2003
Distribution: Gentoo
Posts: 168

Rep: Reputation: 30
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.

Essentially Graphics executes
Code:
SDL_BlitSurface(image, &srcrect, display, &destrect);
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!


Good bye!
 
Old 09-17-2003, 05:15 PM   #2
gundelgauk
Member
 
Registered: Jul 2003
Distribution: Gentoo
Posts: 168

Original Poster
Rep: Reputation: 30
Unhappy

Anyone please?
 
Old 09-17-2003, 05:57 PM   #3
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
can you post your code because i cant really follow what your doing.
 
Old 09-17-2003, 08:38 PM   #4
gundelgauk
Member
 
Registered: Jul 2003
Distribution: Gentoo
Posts: 168

Original Poster
Rep: Reputation: 30
Code

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>"

Thanks for any suggestions!
 
Old 09-18-2003, 06:07 AM   #5
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
ok, firstly when passing parameters both the function declaration and definition need the types so it should be
Code:
class graphics
{
        ...
	void drawentity(SDL_Surface, SDL_Rect, SDL_Rect);
};

void graphics::drawentity(SDL_Surface image, SDL_Rect destrect, SDL_Rect srcrect)
{
        ...
}
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.

if you didnt follow some of that please post back
 
Old 09-18-2003, 12:16 PM   #6
gundelgauk
Member
 
Registered: Jul 2003
Distribution: Gentoo
Posts: 168

Original Poster
Rep: Reputation: 30
Many thanks for your time!


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*


Bye!
 
Old 09-18-2003, 06:22 PM   #7
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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;
}
 
Old 09-18-2003, 06:57 PM   #8
gundelgauk
Member
 
Registered: Jul 2003
Distribution: Gentoo
Posts: 168

Original Poster
Rep: Reputation: 30
Thanks a lot!


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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux is handing gjagadish Linux - Networking 1 10-28-2005 11:54 AM
Trying to program an SDL application but cannot find the SDL.h file:SuSE 9.2&KDevelop pujolasdf Linux - Newbie 4 03-13-2005 07:50 AM
fetchmail not handing off ryedunn Linux - Software 1 12-21-2004 09:02 AM
Large Download handing, for FedoraDVD.iso gauntalus Linux - Software 8 10-13-2004 05:17 PM
Event driven object-to-object: C++ template class mecanism ( NOT STL or STDC++) bretzeltux Programming 2 12-23-2003 02:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration