Displaying SDL content in an existing Window

Hi all,
Can someone supply me with a code snippet that would allow SDL to be
constrained to a Window area I nominate? For example within my
application I may want to display a terrain that I have rendered in the
bottom half of the screen, while the top half contains standard Window
controls/widgets like Buttons and Checkboxes etc that manipulate the
rendered terrain. Does anyone have a code snippet or can point me to a
website that shows how this ( contraining SDL to a portion of an
existing Window ) can be done. Under windows I imagine I would need to
do something with the window handle SDL creates. How does this work
under Linux? Do they have window handles or something similar?

Any hints greatly appreciated. I could not find such an example in the
documentation hence the reason why I have posted it here.

Thanks

Dominique
http://www.DelphiGamer.com

Dominique Louis wrote:

Hi all,
Can someone supply me with a code snippet that would allow SDL to be
constrained to a Window area I nominate? For example within my
application I may want to display a terrain that I have rendered in the
bottom half of the screen, while the top half contains standard Window
controls/widgets like Buttons and Checkboxes etc that manipulate the
rendered terrain. Does anyone have a code snippet or can point me to a
website that shows how this ( contraining SDL to a portion of an
existing Window ) can be done. Under windows I imagine I would need to
do something with the window handle SDL creates. How does this work
under Linux? Do they have window handles or something similar?

Any hints greatly appreciated. I could not find such an example in the
documentation hence the reason why I have posted it here.

I have written 2 functions for doing excactly this (see below) -
but be careful - it might not work in all cases. You get a
new fake SDL_Surface - but just don’t try to free it
with the “real” SDL_FreeSurface function or anything…

You have my permission to use this code for whatever you
like :slight_smile:

#include <SDL.h>
#include <string.h>

SDL_Surface *mltHcP_MakeSubSurface(int surf, int x, int y, int w, int h)
{
SDL_Surface *subSurface;

subSurface = malloc(sizeof(SDL_Surface));
memcpy(subSurface, (void *)surf, sizeof(SDL_Surface));

subSurface->pixels += subSurface->pitch * y +

subSurface->format->BytesPerPixel * x;
subSurface->w = w;
subSurface->h = h;

SDL_SetClipRect(subSurface, NULL);

return subSurface;

}

void mltHcP_FreeSubSurface(SDL_Surface *surf)
{
if(surf)
free((void *)surf);
}

Just 10 minutes ago also I released an interactive showreel -
and it uses exactly this to do some 3D-rendering in only
a portion of the screen (but of course there’s no source
code for this extremely hightech showreel :slight_smile:
http://www.HardcoreProcessing.com/company/showreel/

Cheers–
http://www.HardcoreProcessing.com

Can someone supply me with a code snippet that would allow SDL to be
constrained to a Window area I nominate?

try setting the surface clip rectangle, if that is what you want.
SDL does not support sub-surfaces since hardware support for it is rare,
and it’s nothing the user cannot do in software

subSurface = malloc(sizeof(SDL_Surface));
memcpy(subSurface, (void *)surf, sizeof(SDL_Surface));

don’t do this — never create an SDL_Surface by other means than the
SDL calls (SDL_CreateRGBSurface etc)

Mattias Engdeg?rd wrote:

subSurface = malloc(sizeof(SDL_Surface));
memcpy(subSurface, (void *)surf, sizeof(SDL_Surface));

don’t do this — never create an SDL_Surface by other means than the
SDL calls (SDL_CreateRGBSurface etc)

Oh yes! :slight_smile: It resets the “coordinate system” to have (0, 0)
at the top-left corner of the sub-surface - perfect for
things like a GUI-toolkit and other things which need a
smaller embedded display area :slight_smile:

But of course it would be nice if SDL had the MakeSubSurface and
FreeSubSurface functions - then the “hack” could be maintained
along with SDL :wink:

Cheers–
http://www.HardcoreProcessing.com

don’t do this — never create an SDL_Surface by other means than the
SDL calls (SDL_CreateRGBSurface etc)

Oh yes! :slight_smile:

you can write as much crap code for your own consumption as you like,
but please don’t pass it on to others

But of course it would be nice if SDL had the MakeSubSurface and
FreeSubSurface functions - then the “hack” could be maintained
along with SDL :wink:

no chance of that

don’t do this — never create an SDL_Surface by other means than
the SDL calls (SDL_CreateRGBSurface etc)

Oh yes! :slight_smile:

you can write as much crap code for your own consumption as you like,
but please don’t pass it on to others

But of course it would be nice if SDL had the MakeSubSurface and
FreeSubSurface functions - then the “hack” could be maintained
along with SDL :wink:

Well, it should probably be called SDL_CreateWindow() or something, and
it should be ready for the advanced clipping that we might see in 1.3.

Then again, this has nothing to do with hardware acceleration, so I guess
there’s…

no chance of that

…being a part of the SDL core. Makes sense to me.

How about providing the “hooks” required to implement an add-on windowing
library? (For 1.3 of course - without “advanced clipping”, it’s a kludge
with limited usefullness IMHO…) Perhaps to early to tell if it’ll “fit
in”, though.

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------> http://www.linuxaudiodev.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -'On Thursday 26 July 2001 15:13, Mattias Engdeg?rd wrote:

Mattias Engdeg?rd wrote:

But of course it would be nice if SDL had the MakeSubSurface and
FreeSubSurface functions - then the “hack” could be maintained
along with SDL :wink:

no chance of that

Why not? As far as I can see this is a quite clean way of doing it.

But of course - we still also need that integration with the
native OS’es - so going with the completely general function
for creating both multiple windows and subwindows (as David Olofson
suggested from the beginning :slight_smile: is probably The Right Way ™.

If only I had more time I would have implemented this already… :frowning:

Cheers–
http://www.HardcoreProcessing.com

But of course it would be nice if SDL had the MakeSubSurface and
FreeSubSurface functions - then the “hack” could be maintained
along with SDL :wink:

You can actually already do this with the SDL_CreateRGBSurfaceFrom()
function. Just pass in the correct base address (precalculated from
the x/y address) and give the pitch for the original data.

SDL will not free the pixels data from a surface allocated this way,
and it should work as a normal surface in all other respects.

See ya!
-Sam Lantinga, Lead Programmer, Loki Software, Inc.

Sam Lantinga wrote:

You can actually already do this with the SDL_CreateRGBSurfaceFrom()
function. Just pass in the correct base address (precalculated from
the x/y address) and give the pitch for the original data.

SDL will not free the pixels data from a surface allocated this way,
and it should work as a normal surface in all other respects.

Wow! This is great - thanks! :slight_smile:

Cheers–
http://www.HardcoreProcessing.com