Skip to main content
GameDev.net gamedev.net
🔒 Locked

SDL Coordinates

Started by Kenny77 Feb 23, 2009 at 3:31 PM 2 replies 1.9k views
Original Post
Kenny77
Kenny77
I'm having some trouble with getting my images to draw properly. I have an Image class that gets passed the filepath, x and y position, and whether or not the image should be drawn. But if I pass it 100 for both the x and y, it draws at -100, -100.

// Drawing the image
SDL_BlitSurface(
m_playGameButton->GetSurface(), 
&m_playGameButton->GetRect(), 
game->GetScreen(), 
NULL);

// In the header
SDL_Rect GetRect() { return m_rect; }

// In the Image.cpp file
Image::Image(char *filepath, int x, int y, bool visible)
{
	m_filepath = filepath;

	m_rect.x = x;
	m_rect.y = y;
	m_visible = visible;

	m_image = IMG_Load(filepath);
}



I can't see anything wrong that would make this happen, unless there is something about coordinates in SDL that is doing this. EDIT: Also, I can't use: m_rect.w = m_image.w; For some reason this works (it doesn't draw with the other code): m_rect.w = m_image.w + m_rect.w; What I don't get is how this works, shouldn't m_rect.w be some random value from memory? This has no effect on my negative coordinates problem though. [Edited by - Kenny77 on February 23, 2009 8:28:53 PM]
Kylotan
Kylotan
You return a temporary copy of the SDL_Rect, which you then take a reference to. This might work in many cases but is bad practice as you end up with references to data that has disappeared. You probably want to return a const reference to the actual member of the class.

Similarly I see you're assigning the char* file path - again, this doesn't take a copy, just copies the pointer, so if the original string goes out of memory, m_filepath becomes invalid.

I don't know what your m_rect problem is about but if you initialise it to the correct values (eg. taking the width and height from the image) then any failure to render will be elsewhere.
Kenny77
Kenny77
Quote:
Original post by Kylotan
You return a temporary copy of the SDL_Rect, which you then take a reference to. This might work in many cases but is bad practice as you end up with references to data that has disappeared. You probably want to return a const reference to the actual member of the class.


What do you mean by this?

I ran my code through the debugger and under the Locals (Visual Studio) section in the treeview found two 'objects' (I'm guessing). There's the 'game' object I'm using to manage resources and states and the game loop, but for some reason all the variables in this node are just random values, or question marks.

There's also a 'this' node, which shows all the legit game information. What is this 'this' object?
Kylotan
Kylotan
By the first thing, I mean that your GetRect() returns a copy of a SDL_Rect. That is a temporary object, and will become invalid eventually, so you shouldn't store pointers or references to it. You then pass a reference to it to SDL_BlitSurface, via the & operator. This is probably fine in this case since SDL_BlitSurface won't store that value internally, but generally it's a bad idea. Instead you'd have GetRect() return a reference in the first place, typically const.

The 'this' object is the current object that the method you're in is working with. If you're in 'Image::Image' for example, 'this' will refer to the Image instance in question.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.