Sign in to follow this  

SDL_Surface into script

This topic is 4379 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I've got AngelScript up and working; and am happily passing global variables and functions for the scripts to access. I'm starting SDL from within my script - and have passed in SDL_Init - no problems, it runs. But to setup a screen in SDL, you call a function that returns a 'SDL_Surface'. I have declared this in my C code - SDL_Surface *mainScreen. How do I pass this into my script so it can pass on the pointer from the SDL_CreateSurface function. At the moment I have:
      result = scriptEngine->RegisterObjectType("SDL_Surface", sizeof(SDL_Surface), asOBJ_CLASS);
      if (result < 0)
      {
              conPrint(GL_TRUE, "Script: Error: Couldn't register object type");
              return false;
      }
and then later I pass 'mainScreen' as a global variable. But I get a seg fault when calling 'RegisterObjectType' - how do I go about passing in this structure? Thanks

Share this post


Link to post
Share on other sites
Since you work with pointers to SDL_Surface when doing SDL normally, it would be best to not register it as a full fledged type. If you register it with a size of zero, and give it proper addref/release behaviors, you could use handles. Or you can register a smartpointer type.

Eitherway, you're going to have to register more than just the type. It won't work right without the proper behaviors.

Share this post


Link to post
Share on other sites
Instead of allowing the scripts handle the SDL_Surface pointer directly, you may want to wrap it in a slightly safer type, e.g:


class ScriptSDLSurface
{
public:
ScriptSDLSurface() { refCount = 1; surface = ... }
~ScriptSDLSurface() { if( surface ) ... }

void Addref() {refCount++;}
void Release() {if(--refCount == 0) delete this;}

SDL_Surface *surface;
int refCount;
};


Using this the scripts won't have to worry about freeing surfaces as they will be released when no longer referenced by any variable. Of course it would require extra work on your part, as you would have to write wrapper functions where the SDL_Surface pointer is used.

Or if you prefer a lighter type, but that requires manual management, you could represent the SDL_Surface * with an integer type, or perhaps a registered type that has the size of a pointer. In this case there would not be any need for wrapper functions.

Share this post


Link to post
Share on other sites

Thanks - I ended up doing it a different way. After a bit more thought - the scripts never really need to touch the allocated SDL_Surface. So, the script just calls a host application function that manages the SDL_Surface, and returns a value telling the script the result.

My next challenge is how to expose an array of structs to the scripts.


struct
{
char levelName[];
int enemysAlive;
int *tiles;
} _level;

_level alllevels[NUM_LEVELS];


So that I can write the level loading code in AngelScript.

cheers

Share this post


Link to post
Share on other sites

This topic is 4379 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this