Hi!
hehe, I wanna add a lens effect to my game but I only found this example for DBP

it can be translated to DBC?
` This code was downloaded from The Game Creators
` It is reproduced here with full permission
` http://www.thegamecreators.com
REMSTART
***********************************************
Simple Lens Flare code
(with helpful explanation for beginning coders)
By Philip Young
***********************************************
What you need to use this code:
1. A DB Pro object which is the Sun in your code. In the code below I have stored this DB Pro object number in
a global variable called gSun_object_no. So, for example, if your Sun is DB Pro object number 3 you would say somewhere in
your set-up routines "gSun_object_no = 3"
2. Suitable media. In other words, at least an image of a corona, some images of halos and some "spots" or "dots". These
will be the individual elements that make up your lens flare. If you don't want to make your own you can get example media
from the lens flare articles on gamedev.net
3. Sentience. If you don't have this, I'm baffled how you are able to understand that you don't have this. (Think about this:
its a paradox!)
4. Beer. Actually you don't need this - but I find its helpful.
How does this code work?
I'm assuming that you've seen a lens flare and know roughly what one is. Now then: think of a position for your Sun on the screen.
Got that? Good. Now draw an imaginary line in your mind between that position to and straight through the exact centre point of
the screen. Now extend this line to both sides of the screen. So now you've got an imaginary line that goes right across the screen
through the place where the Sun object is and also through the centre of the screen. Now what this code does is plot and draw the
various DB Pro objects which make up the complete flare in the world 3d coordinates corresponding to somewhere on that line on the
camera screen. Yep, its that easy.
Its also quite a nice use of the reasonably new put screen command.
REMEND
REMSTART
PJY - the following global variable exists to determine if lens flare is switched on or off
PJY - (I mean I use it to toggle the lens flare on and off in my main display routine
PJY - i.e. I have a "if gLensflare = 1 then update_lensflare()" in my main display routine)
PJY - to use it obviously put a "gLensflare = 1" line in your main setup code to turn
PJY - it on by default. Then include a command to turn the "gLensflare = 0" in your user input code
REMEND
global gLensflare
Rem PJY - this is the User Defined Type to hold the basic lens flare data
TYPE lensflare
image_number AS integer
Rem PJY - this will hold the DB Pro image number of each individual flare
obj_number AS integer
Rem PJY - this will hold the DB Pro object number of each individual flare
distance AS integer
Rem PJY - this will hold the distance of the flare across the screen from the Sun
world_distance AS integer
Rem PJY - this will hold the Z distance of the flare object into the screen (i.e. away from the camera)
ENDTYPE
REMSTART
PJY - this is a function to set up the lens flare. You need to run this only once. Call it somewhere in your
PJY - setup code, i.e. put "setup_lens_flare()" in your setup routine.
REMEND
FUNCTION setup_lens_flare()
REMSTART
PJY - we declare an array to hold all the data for all the flares that make up the lens flare
PJY - arrays are your friend. Use them.
PJY - note it is an array with 10 elements (an array includes a 0 element plus, in this case, 9 more)
PJY - this number must correspond with the number of data statements seen below
REMEND
dim lens(9) AS lensflare
REMSTART
PJY - the following basic data and source files come from Mikkel Fredborg's lens flare routine for DB Classic.
PJY - although I've not used any of his other code, I've borrowed this as I like the simplicity of it
PJY - what each data command does is contain the path to the image file, i.e. c:DarkBasic ProProjectslens flareflare1.bmp"
PJY - the first number following it is going to be the size (width and height) of the DB Pro object onto which the image is pasted
PJY - the second number is the distance along the imaginary line across the screen that the object is to be drawn
PJY - hence for the second number you can go up to the length of the screen. Say your screen is 1024 x 768. You could say 900 or even 1000.
PJY - Also, negative values for this second number mean that the DB Pro object will be drawn somewhere on the imaginary line
PJY - inbetween the Sun and the nearest side of the screen to it
PJY - the third number is the actual Z distance into the screen (away from the camera) that the object will be drawn
PJY - The first flare data is for the volumetric effect
REMEND
lensdata:
data "c:DarkBasic ProProjectsCool GameFlare1.bmp", 10, 650, 98
data "c:DarkBasic ProProjectsCool GameFlare2.bmp", 10, 950, 94
data "c:DarkBasic ProProjectsCool GameFlare3.bmp", 30, 800, 96
data "c:DarkBasic ProProjectsCool GameFlare4.bmp", 20, 200, 92
data "c:DarkBasic ProProjectsCool GameFlare5.bmp", 30, 500, 91
data "c:DarkBasic ProProjectsCool GameFlare6.bmp", 30, 350, 90
data "c:DarkBasic ProProjectsCool GameFlare7.bmp", 30, 700, 88
data "c:DarkBasic ProProjectsCool GameFlare8.bmp", 30, 300, 86
data "c:DarkBasic ProProjectsCool GameFlare9.bmp", 5, -300, 84
data "c:DarkBasic ProProjectsCool GameFlare10.bmp", 10, 500, 82
REMSTART
PJY - note that we had 10 data statements. That corresponds with the 9 in the array above.
PJY - you can add or remove flares by adding or removing data statements. If you do, you need to make sure you increase
PJY - or decrease the size of the array above. You also need to make sure the for/next loops below match this. See below.
REMEND
Rem PJY - now restore the lensdata data statements in case we have other data commands elsewhere in the programme
restore lensdata
Rem PJY - this for/next loop creates the actual DB Pro objects that comprise the lens flare
Rem PJY - note that the for/next loop is running 10 times. This corresponds with the size of the array and the number of data
Rem PJY - statements. THIS IS IMPORTANT.
for i = 0 to 9
Rem PJY - read the data from the data statements and put it in appropriate variables
read name$, size, lens(i).distance, lens(i).world_distance
Rem PJY - this is my function for getting the next unused DB Pro object number. I've included it below.
lens(i).obj_number = get_free_object_number()
Rem PJY - make a plain. each of the flares is a plain you see
make object plain lens(i).obj_number, size, size
Rem PJY - this is just lazy of me. Its always best to turn culling on.
set object cull lens(i).obj_number, 0
Rem PJY - this is my function for getting the next unused DB Pro image number. I've included it below.
lens(i).image_number = get_free_image_number()
Rem PJY - load in the image
load image name$, lens(i).image_number
Rem PJY - now slap it on the corresponding DB Pro object
texture object lens(i).obj_number, lens(i).image_number
Rem PJY - and ghost it
ghost object on lens(i).obj_number
Rem PJY - and stop it being affected by the general lighting of the scene
set object light lens(i).obj_number, 0
set object ambient lens(i).obj_number, 0
next i
ENDFUNCTION
REMSTART
PJY - this is a function to update the lensflare. You need to run this each time you
PJY - update the graphics in your programme. I normally put it immediately before my sync
PJY - command. You could write "update_lensflare()" but if you wanted to use the gLensflare
PJY - toggle you'd say "if gLensflare = 1 then update_lensflare()"
REMEND
FUNCTION update_lensflare()
Rem PJY - first test to see if the Sun is in the screen or not
Rem PJY - if it isn't, no point in drawing the flare
if object in screen(gSun_object_no) = 1
REMSTART
PJY - next get the centre coordinates of the screen
PJY - this is sloppy coding as the variables will be the same each time unless you
PJY - are madly changing the screen resolution. I include it only to show what is
PJY - going on. It would be better to have a global variable or a CONSTANT in your
PJY - setup routines that already saves this information, rather than have your computer
PJY - recalculate it each time
REMEND
centre_x = screen width() / 2
centre_y = screen height() / 2
Rem PJY - get the screen coordinates of the Sun
object_screen_x = object screen x(gSun_object_no)
object_screen_y = object screen y(gSun_object_no)
REMSTART
PJY - calculate magnification factor
PJY - what is this? Well, look at it this way. When the Sun gets close to the centre of the
PJY - screen you want all the objects in the lens flare to get closer and closer to each other
PJY - until they are superimposed on the sun. To achieve that you need to scale the distance
PJY - along the imaginary line in the data statements above. The following does that by applying
PJY - a percentage figure to the distance variables in each data statement.
REMEND
percent_x# = abs(centre_x - object_screen_x)
percent_y# = abs(centre_y - object_screen_y)
percent_x# = percent_x# / centre_x
percent_y# = percent_y# / centre_y
if percent_x# > 1 then percent_x# = 1 : Rem PJY - this is a sanity check
if percent_y# > 1 then percent_y# = 1 : Rem PJY - this is also a sanity check
Rem PJY - now you calculate the direction vector between the Sun's screen coordinates and the centre of the screen itself
direction_vector_x# = centre_x - object_screen_x
direction_vector_y# = centre_y - object_screen_y
REMSTART
PJY - next you normalize this direction vector
PJY - to normalise a vector you divide it by its magnitude (i.e. its length)
PJY - you do this because you can then apply the direction number from the data statements
PJY - to the normalised vector - this will give you the length along the imaginary line to the
PJY - point at which you want to plot each element of the lens flare
REMEND
temp_vector_x# = direction_vector_x# * direction_vector_x#
temp_vector_y# = direction_vector_y# * direction_vector_y#
temp_magnitude# = temp_vector_x# + temp_vector_y#
temp_magnitude# = sqrt(temp_magnitude#)
normal_x# = direction_vector_x# / temp_magnitude#
normal_y# = direction_vector_y# / temp_magnitude#
Rem PJY - now you scale the normalized vector for each lens flare element
Rem PJY - as we have 10 data statements above we run a for/next loop for each element in turn
for i = 0 to 9
Rem PJY - note the way that the percentage is being applied to the distance, then
Rem PJY - the resulting figure is being multiplied by the normalized vector
scale_x# = normal_x# * (lens(i).distance * percent_x#)
scale_y# = normal_y# * (lens(i).distance * percent_y#)
Rem PJY - now apply the scaled normalized vector to the Sun's screen coordinates to find
Rem PJY - the destination screen coordinates for the lens flare element
screen_x# = object_screen_x + scale_x#
screen_y# = object_screen_y + scale_y#
Rem PJY - now we need to translate the destination screen coordinates into 3d worldspace
Rem PJY - coordinates. To do this we use the pick screen command
pick screen screen_x#, screen_y#, lens(i).world_distance
vector_x# = get pick vector x()
vector_y# = get pick vector y()
vector_z# = get pick vector z()
Rem PJY - pick screen gives you a new vector. You have to add that to the camera position to
Rem PJY - get the actual 3d world coordinates
world_x# = vector_x# + camera position x(0)
world_y# = vector_y# + camera position y(0)
world_z# = vector_z# + camera position z(0)
Rem PJY - now you've got the 3d world coordinates you can position the lens flare object there
position object lens(i).obj_number, world_x#, world_y#, world_z#
Rem PJY - and point it towards the camera so that it can be seen clearly
point object lens(i).obj_number, camera position x(), camera position y(), camera position z()
Rem PJY - and show it if it is presently hidden
if object visible(lens(i).obj_number) = 0
show object lens(i).obj_number
endif
next i
Rem PJY - but what if the Sun is not in the screen? Well then, for the sake of efficiency, hide all the
Rem PJY - objects in the lens flare
else
for i = 0 to 9
if object visible(lens(i).obj_number) = 1
hide object lens(i).obj_number
endif
next i
endif
ENDFUNCTION
Rem PJY - function to get the first free DBPro image number available
FUNCTION get_free_image_number()
local number
for i = 1 to 1000
if image exist(i) = 0
number = i
i = 1001
endif
next i
ENDFUNCTION number
Rem PJY - function to get the first free DBPro object number available
FUNCTION get_free_object_number()
local number
for i = 1 to 1000
if object exist(i) = 0
number = i
i = 1001
endif
next i
ENDFUNCTION number
Rem PJY - function to get the first free DBPro bitmap number available
FUNCTION get_free_bitmap_number()
local number
for i = 1 to 1000
if bitmap exist(i) = 0
number = i
i = 1001
endif
next i
ENDFUNCTION number
Thanks in Advance

-DARKGuy
:: Pentium 300 Mhz, 8Mb video card, 64Mb RAM, 5 gb & 1.6 gb HD's, W98SE, Sound Blaster AWE 32 ::
