'Cutting a Hole' in a Surface

The name should say it all but I will explain what it is that I’m trying to do.

I am trying to create the illusion of a point light on a flat surface. I’m sure
I could do this somehow with OpenGL but, frankly, I hate OpenGL and I want to
stick with 2D hardware as it is.

The way I had thought to do this is to create a ‘darkening mask’ by filling a
Surface with a color, let’s say Black ( rgb=000) to the same size as my primary
video surface. This would then be blitted on top of everything making the
screen appear black. (This illusion, of course, could be made even better by
working with Alpha’s but let’s ignore that for now).

Then, the create the illusion of a point light, I would cut a ‘hole’ in the
surface so that when blitted, there’s the appearance of a square ‘hole’ (e.g.,
the light).

I’ve tried a few things that don’t seem to be working as well as I would have
liked. Any suggestion on how I can ‘cut a hole’ in a surface?

Also, I’ve thought of just using a few Rect’s or a Rect list to kind of draw
around the ‘hole’ but I don’t see this as being too feasible in a real-time
sense with the light source bouncing around.

[…]

I’ve tried a few things that don’t seem to be working as well as I
would have liked. Any suggestion on how I can ‘cut a hole’ in a
surface?

You have to use colorkeying, or create the surface with an alpha
channel, otherwise the only thing you can draw into the surface is
opaque color.

Also, I’ve thought of just using a few Rect’s or a Rect list to kind
of draw around the ‘hole’ but I don’t see this as being too feasible
in a real-time sense with the light source bouncing around.

This is probably faster, actually. Indeed, if you’re using RLE, the
transparent area is just skipped, so that’s the fastest way you can
do it - but accelerated backends still have to read the transparent
pixels.

Either way, the number of rectangles aren’t going to make a difference
unless you have hundreds of them - or thousands, if we’re talking
about current generation PCs.

But, why don’t you just clear the screen, set up a clip rect and
render through that…?

//David Olofson - Programmer, Composer, Open Source Advocate

.------- http://olofson.net - Games, SDL examples -------.
| http://zeespace.net - 2.5D rendering engine |
| http://audiality.org - Music/audio engine |
| http://eel.olofson.net - Real time scripting |
’-- http://www.reologica.se - Rheology instrumentation --'On Tuesday 27 November 2007, Leeor wrote:

David Olofson <david olofson.net> writes:

[…]

I’ve tried a few things that don’t seem to be working as well as I
would have liked. Any suggestion on how I can ‘cut a hole’ in a
surface?

You have to use colorkeying, or create the surface with an alpha
channel, otherwise the only thing you can draw into the surface is
opaque color.

Also, I’ve thought of just using a few Rect’s or a Rect list to kind
of draw around the ‘hole’ but I don’t see this as being too feasible
in a real-time sense with the light source bouncing around.

This is probably faster, actually. Indeed, if you’re using RLE, the
transparent area is just skipped, so that’s the fastest way you can
do it - but accelerated backends still have to read the transparent
pixels.

Either way, the number of rectangles aren’t going to make a difference
unless you have hundreds of them - or thousands, if we’re talking
about current generation PCs.

But, why don’t you just clear the screen, set up a clip rect and
render through that…?

//David Olofson - Programmer, Composer, Open Source Advocate

.------- http://olofson.net - Games, SDL examples -------.
| http://zeespace.net - 2.5D rendering engine |
| http://audiality.org - Music/audio engine |
| http://eel.olofson.net - Real time scripting |
’-- http://www.reologica.se - Rheology instrumentation --’

Thank you for your response! I’m not sure why I didn’t think of it before but
using a color key to cut a hole in a layer makes a lot of sense! I’m going to
try it now… actually, I expect it to work. May not be the fastest of all
methods but it should certainly do the trick (considering the sections to cut
out will never be more than about 128x128.)

As for clip rects, that also seems feasible… problem is I’ve never been too
sure how to use them. Go figure… *:slight_smile: I’m not too terribly concerned about
performance anyway because I don’t see a couple of the ‘holes’ causing much of
a performance bottleneck… of course, I could be wrong! :slight_smile:

Thank you for taking the time to reply!> On Tuesday 27 November 2007, Leeor wrote: