Hello again
Expanding on my text labels in 3d space functions i have created a menu system that makes use of the labels.
I have included a wide variety of comands in the code to demonstarte there usage.
I have also tried my best to explain how the menu works with comments, however if there is any part of it you don't understand, please ask for assistance.
Info on text label functions can be found here
http://forum.thegamecreators.com/?m=forum_view&t=74883&b=6
set display mode 800, 600, 32
sync on; sync rate 60; autocam off; sync;
set image colorkey 0, 0, 0
global red as dword; red = rgb(255, 0, 0)
global green as dword; green = rgb(0, 255, 0)
global blue as dword; blue = rgb(0, 0, 255)
global white as dword; white = rgb(255,255,255)
type labeltype
active as boolean
caption as string
image as integer
object as integer
x as float
y as float
z as float
width as integer
height as integer
color as dword
endtype
global dim labels(0) as labeltype
`structure for holding current menu state
type menustatetype
position as integer `position in the menu
upkeydown as boolean `to record if the upkey has been held down
upkeyclick as boolean `to record if the upkey has clicked
downkeydown as boolean `to record if the downkey has been held down
downkeyclick as boolean `to record if the downkey has clicked
endtype
global menustate as menustatetype `define a global variable with the above structure
`preset the newly defined structure with it's values
menustate.position = 0;
menustate.upkeydown = 0; menustate.upkeyclick = 0;
menustate.downkeydown = 0; menustate.downkeyclick = 0;
global dim menuitems(4) as integer `create an array with five elements to store labels
restore menudata `set data pointer to menudata: label found at end of program
`the following code fills the menuitems(0) array with label ID's
`and creates the labels
for count = 4 to 0 step - 1
read caption$ `read data into caption$
menuitems(count) = new_label(caption$, 0, 2.0 - count, 0, red)
next count
`ok now in the above code we set every label color to red
`if we left it as it is you wouldn't be able to see which
`menu item is currently selected
change_label_color(menuitems(menustate.position), white) `show selected menu item
`position camera away from menu and point towards it
position camera 0, 0, -10
point camera 0, 0, 0
`game loop -----------------------------------------------------
do
process_menu() `process menu code to make the menu function
process_labels() `process text labels
if returnkey()
select menustate.position
case 0
`code here for first menu selection
endcase
case 1
`code here for second menu selection
endcase
case 2
`code here for third menu selection
endcase
case 3
`code here for fourth menu selection
endcase
case 4
`code here for anything before program end
end;
endcase
endselect
endif
sync
loop
`---------------------------------------------------------------
function process_menu()
`ok this function is the guts of your menu system and it doas two things
`first it checks the current state of both the up down keys
`if the key is not pressed but has been pressed in the past then it has clicked
`so we set the menustate.upkeyclick & menustate.downkeyclick variables to 1 "on"
if not upkey()
if menustate.upkeydown
menustate.upkeyclick = 1
menustate.upkeydown = 0
endif
else
menustate.upkeydown = 1
menustate.upkeyclick = 0
endif
if not downkey()
if menustate.downkeydown
menustate.downkeyclick = 1
menustate.downkeydown = 0
endif
else
menustate.downkeydown = 1
menustate.downkeyclick = 0
endif
`second; now that we know when the upkey and downkey has
`been clicked we now can check for it
if menustate.upkeyclick `if the up key has been clicked do the following
menustate.upkeyclick = 0 `we have used the switch and now we turn it off
change_label_color(menuitems(menustate.position), red) `color the currently selected menu item red
dec menustate.position, 1 `decrease menu position variable by 1
if menustate.position < 0 then menustate.position = 4 `if menu position less than 0 wrap around to 4
change_label_color(menuitems(menustate.position), white) `color newly selected menu item white
endif
if menustate.downkeyclick `if the down key has been clicked do the following
menustate.downkeyclick = 0 `we have used the switch and now we turn it off
change_label_color(menuitems(menustate.position), red) `color the currently selected menu item red
inc menustate.position, 1`increase menu position variable by 1
if menustate.position > 4 then menustate.position = 0 `if menu position less than 0 wrap around to 4
change_label_color(menuitems(menustate.position), white) `color newly selected menu item white
endif
endfunction
function process_labels()
arraycount = array count(labels(0)) - 1
if arraycount > -1
for count = 0 to arraycount
if labels(count).active
point object labels(count).object, camera position x(), camera position y(), camera position z()
endif
next count
endif
endfunction
function change_label_position(element as integer, x as float, y as float, z as float)
labels(element).x = x
labels(element).y = y
labels(element).z = z
position object labels(element).object, x, y, z
endfunction
function change_label_caption(element as integer, caption as string)
width = text width(caption)
height = text height(caption)
labels(element).caption = caption
labels(element).width = width
labels(element).height = height
delete image labels(element).image
delete object labels(element).object
currentbitmap = current bitmap()
bitmap = free_bitmap()
create bitmap bitmap, width, height
ink labels(element).color, 0
text 0, 0, caption
get image labels(element).image, 0, 0, width, height, 1
set current bitmap currentbitmap
delete bitmap bitmap
make object plain labels(element).object, 0.025 * width, 0.025 * height
texture object labels(element).object, labels(element).image
set object transparency labels(element).object, 1
position object labels(element).object, labels(element).x, labels(element).y, labels(element).z
endfunction
function change_label_color(element as integer, color as dword)
delete image labels(element).image
currentbitmap = current bitmap()
bitmap = free_bitmap()
create bitmap bitmap, labels(element).width, labels(element).height
ink color, 0
text 0, 0, labels(element).caption
get image labels(element).image, 0, 0, labels(element).width, labels(element).height, 1
set current bitmap curentbitmap
delete bitmap bitmap
texture object labels(element).object, labels(element).image
labels(element).color = color
endfunction
function new_label(caption as string, x as float, y as float, z as float, color as dword)
width = text width(caption)
height = text height(caption)
element = free_label()
labels(element).active = 1
labels(element).image = free_image()
labels(element).object = free_object()
labels(element).caption = caption
labels(element).x = x
labels(element).y = y
labels(element).z = z
labels(element).width = width
labels(element).height = height
labels(element).color = color
bitmap = free_bitmap()
currentbitmap = current bitmap()
create bitmap bitmap, width, height
ink color, 0
text 0, 0, caption
get image labels(element).image, 0, 0, width, height, 1
set current bitmap currentbitmap
delete bitmap bitmap
make object plain labels(element).object, 0.025 * width, 0.025 * height
texture object labels(element).object, labels(element).image
set object transparency labels(element).object, 1
position object labels(element).object, x, y, z
endfunction element
function delete_label(element)
labels(element).active = 0
delete object labels(element).object
delete image labels(element).image
endfunction
function label_x(element as integer)
result = labels(element).x
endfunction result
function label_y(element as integer)
result = labels(element).y
endfunction result
function lebel_z(element as integer)
result = labels(element).z
endfunction result
function free_image()
result = 1
while image exist(result)
inc result, 1
endwhile
endfunction result
function free_object()
result = 1
while object exist(result)
inc result, 1
endwhile
endfunction result
function free_bitmap()
count = 0
result = -1
repeat
inc count, 1
if not bitmap exist(count) then result = count
until count = 32 or result > 0
endfunction result
function free_label()
result = -1
arraycount = array count(labels(0)) - 1
if arraycount > -1
count = -1
repeat
inc count, 1
if not labels(count).active then result = count
until count = arraycount or result > -1
endif
if result = -1
array insert at bottom labels(0)
result = array count(labels(0)) - 1
endif
endfunction result
menudata:
data "EXIT"
data "Menu Item 4"
data "Menu Item 3"
data "Menu Item 2"
data "Menu Item 1"