Hi there,
I have a couple of questions I'd like to ask about the usage of pointers. I wanted to experiment a bit with SDL but noticed right away that the SDL_Surfaces for example are created as pointers :S
So my first question would be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
class Image
{
public:
Image(SDL_Surface *surface);
//Destructor?
FloatRect rect;
SDL_Surface *getSurface();
void setSurface(SDL_Surface *surface);
void loadImage(string path);
private:
SDL_Surface *surface_;
};
|
In this code snippet I would probably need to create a destructor because the surface_ is a pointer, right? What would this destructor consist of? Calling SDL_FreeSurface(surface_)?
My next questions are related to the function definitions of my class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
//1. What if surface goes out of scope?
Image::Image(SDL_Surface *surface):
rect(SDL_Rect())
{
surface_=surface;
rect=FloatRect(surface_->clip_rect);
}
SDL_Surface *Image::getSurface()
{
return surface_;
}
//2. Memory leak with previous surface_?
void Image::setSurface(SDL_Surface *surface)
{
surface_=surface;
}
//2. Memory leak with previous surface_?
void Image::loadImage(string path)
{
surface_=IMG_Load(path.c_str());
}
|
1.: If I construct the Image with a SDL_Surface that goes out of scope earlier than my Image itself, what happens to surface_? (Because I think it would still point to a memory address but probably not the one of the original surface I created the Image with, right?)
Or a similar thing I think would be if I saved a surface I got with getSurface() (from an Image image) as SDL_Surface *surface. If this surface went out of scope before image would, what happens to surface_?
2.: Finally the methods I marked with 2. would cause memory leaks if surface_ was previously assigned to something else, right? Would I first free the previous surface_(if there was one)?
I'd also like to ask an additional question about makefiles:
If I have the following structure:
FloatRect.cpp includes FloatRect.h
Image.cpp includes Image.h which includes FloatRect.h
main.cpp includes Image.h
Currently I have my makefile structured like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
CFLAGS=-m32 -Wall
SDL=C:\Users\fuerchter\Downloads\SDL-devel-1.2.15-mingw32\SDL-1.2.15
LIBS=-lmingw32 -lSDLmain -lSDL -lSDL_image
all: main.o FloatRect.o Image.o
g++ $(CFLAGS) main.o FloatRect.o Image.o -L$(SDL)\lib $(LIBS)
main.o: main.cpp
g++ -c $(CFLAGS) main.cpp -I$(SDL)\include
Image.o: Image.h Image.cpp
g++ -c $(CFLAGS) Image.cpp -I$(SDL)\include
FloatRect.o: FloatRect.h FloatRect.cpp
g++ -c $(CFLAGS) FloatRect.cpp -I$(SDL)\include
|
But I am first of all not sure whether I'm supposed to put the Header files into the prerequisites. Also if I changed FloatRect.h the Image.o target wouldn't be compiled again. Is this supposed to happen? Can I still improve this makefile? (because I'm just not sure whether I am writing them in the proper way >.<)
I would appreciate it very much if anyone could help me clear up these matters :D
//EDIT:
Oh and by the way is there a way for me to write programs with SDL while still having a console to write to because at the moment I am writing all my testing stuff in the caption of the window ^^