🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

SDL_Surface into script

Started by
2 comments, last by Gibbon_99 18 years, 6 months ago
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
Advertisement
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.
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.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game


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

This topic is closed to new replies.

Advertisement