Use SDL_image without opening a window?

Started by
5 comments, last by aboothe726 16 years, 1 month ago
I've got a simple game running using SDL. In addition to the main "play mode", I'd like to add a second "edit mode" to the game that will allow users to make modifications to on-disk game data without knowing anything about the underlying formats for the game. In a perfect world, this will run transparently on the command line to enable users to create scripts or a GUI front-end to put together a level editor. For example, I'd like them to be able to say something like (in bash): $ game --edit add-monster monsterset.dat newmonster.dat ...to add a monster to a monster set for the game. Implementing this edit mode will require reading image files. In play mode, my game logic uses SDL_image to load sprites from image files, and it works perfectly. In the edit mode, I'd like to continue using SDL_image to read the image files, but I'd like not to pop up a window (for obvious reasons). Is there any way to use SDL_image without popping up a window? My impressions is that I can't because I have to call SDL_SetVideoMode before I can use SDL_image because SDL_image makes calls to SDL_CreateSurface*. I've poked around in the SDL source code (very little!) and it seems like there are two ways I could do this: 1. As far as I know, SDL_SetVideoMode always opens a window, which is what I'm trying to avoid. Can I call the SDL_CreateSurface* functions without calling SDL_SetVideoMode? I know it says right there in the documentation that I can't, but is there something I can do to fool SDL into letting me? I'm using software surfaces, so there's really no reason that I can think of that I can't just have a little software surface sitting in memory without opening a window. 2. Can I call SDL_SetVideoMode, but have it not create a window? I don't have a problem calling SDL_SetVideoMode if it doesn't open a window. Any comments welcome. Feel free to tell me something I may not have thought of! I'd really rather not mess around with libpng and friends.
Advertisement
Try using the dummy video mode (SDL_VIDEODRIVER).

However, in my experience it is SDL_Init(SDL_INIT_VIDEO) that matters, not SDL_SetVideoMode() (the docs might be out of date, or plain wrong). I might have a peek around the SDL source to see why that is. I imagine that as long as you don't request a hardware surface you should be fine.
That's excellent news! Thanks for the quick response. I'll have a look at both of those ideas as soon as I can and post back with the results.
Ok, looking at the SDL_CreateRGBSurface function code, it appears to check whether or not the screen surface is allocated (if you request a HW_SURFACE it actually removes that flag if the screen isn't present). It appears to be perfectly safe to call it when SDL_SetVideoMode has not been called, although SDL_INIT_VIDEO must have been passed to SDL_Init.

This section of code appears to be video-driver and platform independant. However, because it isn't the documented behaviour it really is up to you whether you choose to use this technique.
rip-off, you are a rock star.

right. because the behavior isn't documented, it could go away at any time, or even work differently between existing versions of SDL. certainly, it is risky.

You also mentioned the dummy video mode; that shows real potential because it looks like it won't open a window and it's documented. i'll have a look at that sometime before the weekend and post back.

rip-off, thanks again for the help.
I tried the dummy video mode, and unfortunately SDL_SetVideoMode() returns NULL (SDL_GetError() said "no available video device"), therefore one cannot rely on the documented behaviour of SDL_CreateRGBSurface() (which requires a successful call to SetVideoMode).
Hm. I see. That's unfortunate, I was hoping to use a documented behavior to get this working. I'll try contacting a developer to get an opinion on whether this behavior is accidental or intentional, and likely to go away.

rip-off, you made my job easy!

This topic is closed to new replies.

Advertisement