C++ SDL question

Dec 18, 2013 at 1:18am
This is probably a newbish type of question but I was wondering if anyone out there new.

Why is it possible in SDL that when creating an SDL_Surface you are forced to create it as a pointer? If you try to create it without making your var a pointer it will give an error. Why is this? Is there a way when I create classes I can force all object to be pointers only?

Thanks!

Erock
Dec 18, 2013 at 12:20pm
Are you saying this program doesn't work?
1
2
3
4
5
6
#include "SDL.h"

int main()
{
	SDL_Surface surface;
}
Dec 18, 2013 at 8:41pm
Maybe I am an idiot
Last edited on Dec 18, 2013 at 8:41pm
Dec 18, 2013 at 10:27pm
I have virtually no experience with GUI programming but your problem sounds vaguely similar to mine when setting up wxWidgets (both SDL and wxWidgets use the Zlib library so I'm guessing they're somewhat similar). If it's the same problem I had you may have installed the wrong version of SDL for your system and need to install an older/newer version, or update the appropriate library(ies).

Of course it could likely be something completely different so maybe someone with more experience can offer other possibilities. Some code would help too.
Dec 18, 2013 at 10:48pm
Why is it possible in SDL that when creating an SDL_Surface you are forced to create it as a pointer?


It's C's way of enforcing encapsulation. SDL_Surface is a black box. It has some members that you can access, but it has other members that are "hidden" and created in a super-secret special way.

Creating a SDL_Surface without using SDL's API (like Peter's post illustrates) may compile, but it is a BAD IDEA because the SDL_Surface object can never work properly. You are bypassing all the behind-the-scenes initialization that SDL has to set up to get the Surface created properly.

It's the same reason fopen() and similar functions return a FILE* pointer rather than you just creating a FILE object yourself.



TLDR:

That's just how OOP is done in C.
Dec 19, 2013 at 12:08am
Oh okay! Thanks
Topic archived. No new replies allowed.