Hi, guys i decided to share one of my projects here so i can get some feedback, so i know if i'm heading the right direction or this can go to trash.Anyway someone can benefit from this big mess of code i assembled/coded/linked myself.I write this because not entire code is mine and there is chuncks from examples or snippets i found usefull to decrease the development time by skip writing something that can fit my needs anyway.
Unfortunately i have the bad habit to forget where i get these chunks.Some parts are directly ripped from examples, other just modified.For example i'm pretty sure that camera system is pretty much the same as in the originial where i get it.Wherever it was...
I don't mind someone to claim credits on parts of this as i allways do when i know the work is not entirely mine.
Here it is the source:
`Include
#include "BBBGUI_CONSTANTS.dba"
`Set Up Display
set window on
set window title "Anarchy Level Editor"
load dll "user32.dll",1
Sw=call dll(1,"GetSystemMetrics",0)
Sh=call dll(1,"GetSystemMetrics",1)
delete dll 1
set display mode Sw,Sh,32,1
maximize window
sync on : sync rate 0
backdrop on
`BBB GUI
disable escapekey
start bbb gui "Media\Ruby.msstyles"
windows set font 7,"Arial",0,0,0,0
`This command sets the icon that will appear in top left corner of all windows
`that will be created later
windows set icon "Media\dbpro_icon.ico"
windows set visibility 1
dbpro_w=get dbpro window() : set main window dbpro_w
w=make window(0,0,width,45,"Anarchy Level Editor",dbpro_w, WS_CAPTION)
w2=make window((110,110,width/6,450,"Make Object",dbpro_w, WS_SYSMENU))
`This command makes empty menu bar
menu=make menu()
`File - 1
sub1=make submenu() : menu append submenu menu,sub1,"File"
submenu insert item sub1,"New",1
submenu insert separator sub1
submenu insert item sub1,"Open",2
submenu insert item sub1,"Save",3
submenu insert item sub1,"Save As...",4
submenu insert separator sub1
submenu insert item sub1,"Import",5
submenu insert item sub1,"Export",6
submenu insert separator sub1
submenu insert item sub1,"Exit",17
`Edit - 2
sub2=make submenu() : menu append submenu menu,sub2,"Edit"
submenu insert item sub2,"Undo",13
submenu insert item sub2,"Redo",14
menu enable item sub2,13,0
`Tools - 2
sub3=make submenu() : menu append submenu menu,sub3,"Tools"
submenu insert item sub3,"Objects",16
submenu insert item sub3,"Textures",18
sub4=make submenu()
submenu insert item sub4,"SubSubItem1",10 : menu check item sub4,10,1
link submenu sub3,9,sub4
`This command shows rhe menu in our window
`NOTE: It is important that this command is called after all
`submenus are added
apply menu w,menu
`Now we will create a popup menu (or context menu) - this is the menu
`that pops up when you right click on the screen
sub5=make submenu()
submenu insert item sub5,"Gadget",20
sub6=make submenu()
submenu insert item sub6,"Make Primitive",21
submenu insert item sub6,"Load Object",22
link submenu sub5,20,sub6
`This commands tells bbb gui plugin that we want this submenu to popup when
`right mouse button is clicked
show submenu sub5, dbpro_w
`If you don't want this menu to be shown on right click then put zero as
`window handle like this:
`show menu sub, 0
`w2=make window(20,20,100,100,"",w, WS_CHILD)
`window set color w2,0,200,0
`w3=make window(300,300,"Tool window",dbpro_w,WS_EX_TOOLWINDOW,WS_OVERLAPPED)
set window on
`hide window
window set style dbpro_w,window get style(dbpro_w)||WS_CLIPCHILDREN||WS_CLIPSIBLINGS
`We have to call this command after the SET WINDOW ON command
`because SET WINDOW ON command resets the icon to dbpro's default one
window set icon w3, "Media\dbpro_icon.ico"
`Load Media
load image "Media/Textures/grass.png",1
`Load SkyBox but very bad at the moment it need atention.
`load image "Media/Sky Boxes/cube1.dds",2
` make object cube 10000,-100
` convert object fvf 10000,0x002
`texture object 10000,2
`Load SkyBox
`load object "Media/Sky Boxes/cube1.dds",10000
`texture object 10000,1
`Free Object
find_free_object = find free object()
obj_max = 50000 `Bigger value makes FPS drops, if this number is not finite then make routine to inc obj_max with a value everytime when it`s capped.
`Globals
global vx# as double float
global vy# as double float
global vz# as double float
global CamDis# as integer
global gadget_obj as integer
`Set Text
set text size 20
set text to bold
`Default Modes
SlideMode$="NULL"
`Setup Camera and Spawn Gadget Coordinates
gadget_x as float
gadget_y as float
gadget_z as float
gadget_x=0.0
gadget_y=0.0
gadget_z=0.0
`Setup Camera
autocam off
position camera gadget_x,gadget_y,gadget_z-150
xrotate camera 30
CamDis#=-100
`Make Spawn Gadget Object
gadget_obj = 1000000
make object sphere gadget_obj,10,50,50
`rotate object 1,90,90,0
position object gadget_obj,0,0,0
fade object gadget_obj,200
`Make a sphere(remove it later dude :))
` make object cube 2,50
` position object 2,100,0,0
`Setup Matrix
make matrix 1,250,250,10,10
position matrix 1,gadget_x-125,gadget_y,gadget_z-125
set matrix wireframe on 1
ghost matrix on 1,1
FileID = FIND FREE DATAFILE(1,100)
Filename$ = "level_tmp.aem"
`Open Datafile to Write
if FileID>0 then open datafile to write FileID,"Levels\"+Filename$
do
Exists = DATAFILE EXIST ( FileID )
`Text
text 0,15,"Timer: "+str$(time)
text 0,30,"Spawn Gadget Position - "+"X: "+str$(object position x(gadget_obj))+" Y: "+str$(object position y(gadget_obj))+" Z: "+str$(object position z(gadget_obj))
text 0,45,"Camera Position - "+"Cam Pos X: "+str$(gadget_x)+" Cam Pos Y: "+str$(gadget_y)+" Cam Pos Z: "+str$(gadget_z)
text 0,60,"camera angle x: "+str$(camera angle x(0))
text 0,75,"Slide Mode(USE 1,2) "+SlideMode$
text 0,90,"mousex()= "+str$(mousex())+", mousey()= "+str$(mousey())
text 0,105,"ActualFreeObject = "+str$(ActualFreeObject)
text 0,120,"Selected Object = "+str$(Selected_Object)
text 0,135,"FPS: "+str$(screen fps())
text 0,150,"FileID: "+str$(FileID)
text 0,165,"Exists: "+str$(Exists)
`Camera Control
if SlideMode = 0
position camera gadget_x,gadget_y,gadget_z
if mouseclick()=1
xrotate camera camera angle x()-mousemovey()*0.25
yrotate camera camera angle y()+mousemovex()*0.25
endif
`if mouseclick()=2 then CamDis#=CamDis#-mousemovey()*0.25
CamDis#=CamDis#+mousemovez()*0.25
if camera angle x()>80 then xrotate camera 80
if camera angle x()<-60 then xrotate camera -60
`if upkey()=1 then CamDis#=CamDis#+2.5
`if downkey()=1 then CamDis#=CamDis#-2.5
if CamDis#>-70 then CamDis#=-70
if CamDis#<-300 then CamDis#=-300
move camera CamDis#
`remended line will become handy when creating first person camera(just rem off)
`position camera gadget_x,gadget_y,gadget_z
endif
if SlideMode = 1
MoveGadget()
positionCameraToObject(2)
position mouse Sw/2,Sh/2
endif
`Spawn Gadget Control
if keystate(16)=1
gadget_x = object position x(gadget_obj)
gadget_y=gadget_y+1
gadget_z = object position z(gadget_obj)
position object gadget_obj,gadget_x,gadget_y,gadget_z
endif
if keystate(18)=1
gadget_x = object position x(gadget_obj)
gadget_y=gadget_y-1
gadget_z = object position z(gadget_obj)
position object gadget_obj,gadget_x,gadget_y,gadget_z
endif
if inkey$()="1"
SlideMode=0;SlideMode$="OFF"
endif
if inkey$()="2"
SlideMode=1;SlideMode$="ON"
endif
if SlideMode=1
if pick object(mousex(),mousey(),gadget_obj,gadget_obj,1)>1
position object gadget_obj,gadget_x,gadget_y,gadget_z
endif
endif
`Make object when button is pressed
if ButtonPressed=0
`Find free object number
for i=1 to obj_max
if ActualFreeObject = 0
if object exist(i) = 0
ActualFreeObject= i
inc i
endif
endif
next i
find_free_object = find free object()
if ActualFreeObject > find_free_object then ActualFreeObject = find_free_object
`//if menu turning Active
If Keystate(4)<>0 and ObjectCreated=0
rnd_value = rnd(100)
make object cube ActualFreeObject,rnd_value
`DataFileOpened = OPEN DATAFILE TO APPEND( FileID, Filename$ )
if Exists = 0 then OPEN DATAFILE TO APPEND FileID, Filename$
`TYPE
write datafile byte FileID,1
`SIZE
write datafile integer FileID,rnd_value:
`POS X
write datafile float FileID,object position x(gadget_obj)
`POS Y
write datafile float FileID,object position y(gadget_obj)
`POS Z
write datafile float FileID,object position z(gadget_obj)
position object ActualFreeObject,object position x(gadget_obj),object position y(gadget_obj),object position z(gadget_obj)
` //Activate menu and set button as pressed
ObjectCreated=0
ButtonPressed=1
inc ActualFreeObject
endif
else
` //if key is unpressed reset button pressed
if Keystate(4)=0 then ButtonPressed=0
endif
`Pick Object - Rotate(add rotate statement)
if mouseclick()=1
selected_object = pick object(mousex(),mousey(),1,obj_max)
else
selected_object = 0
endif
`if selected_object > 0 then yrotate object selected_object,object angle y(selected_object)+0.5
`if selected_object > 0 then delete object selected_object
if selected_object > 0 then texture object selected_object,1
`Reset values
if scancode()=0 then nopress=0
mousemovex()=0
mousemovey()=0
`BBB GUI
`Menu Window Position
if window position x (w)<>0 and window position y (w)<>0
position window w,0,0
endif
`Menu Exit - working
ID = MENU GET SELECTED ITEM ID()
if ID = 17
get event
h=event get handle()
m=event get message()
a=make message box("Are you sure you want to quit?","BBB Gui plugin",MB_YESNO)
if a=IDYES then end bbb gui : end
endif
`New Map
if ID = 1
a=make message box("All unsaved work will be lost!Proceed?","BBB Gui plugin",MB_YESNO)
if a=IDYES
for n = 1 to 200000
if object exist(n)=1
delete object n
endif
next n
if datafile exist (FileID) then close datafile FileID
delete file Filename$
endif
endif
`Menu Import Object - not working
if ID = 5
open_file$=open file dialog("Select object to Import","DirectX Object (*.x)|*.x|3D Studio Max (*.3ds)|*.3ds|Dark Basic Pro (*.dbo)|*.dbo|")
if asc(mid$(open_file$,1))<>0
`This 2 lines must contain actual loading code
if image exist(1) then delete image 1
load image open_file$,1
file$=open_file$
if (right$(open_file$,3)="aem")=0
save image "_temp.aem",1
file$="_temp.aem"
endif
`This 2 lines must contain actual loading code
change static image b2, file$, 256, 256
if file exist("_temp.bmp") then delete file "_temp.bmp"
endif
endif
`Menu Open - not working
if ID = 2
a=make message box("Are you sure you want to leave current work?","BBB Gui plugin",MB_YESNO)
if a=IDYES
open_file$=open file dialog("Select Map to Open","Anarchy Editor Map (*.aem)|*.aem|")
if asc(mid$(open_file$,1))<>0
if datafile exist (FileID) then close datafile FileID
open datafile to read FileID,open_file$
for n = 1 to 200000
if object exist(n)=1
delete object n
endif
next n
for n = 1 to 200000
obj_type = datafile byte(1)
obj_size = datafile integer (1)
if obj_type = 1 `which is cube
make object cube n,obj_size
pos_x = datafile float(1):pos_y = datafile float(1):pos_z = datafile float(1)
position object n,pos_x,pos_y,pos_z
endif
next n
close datafile FileID
`Find free object number -Make a function for find free object and eliminate the need of copieng the code every time.
for i=1 to obj_max
if ActualFreeObject <> 0
if object exist(i) = 0
ActualFreeObject= i
inc i
endif
endif
next i
find_free_object = find free object()
if ActualFreeObject > find_free_object then ActualFreeObject = find_free_object
endif
endif
endif
remstart
`This 2 lines must contain actual loading code
if image exist(1) then delete image 1
load image open_file$,1
file$=open_file$
if (right$(open_file$,3)="aem")=0
save image "_temp.aem",1
file$="_temp.aem"
endif
`This 2 lines must contain actual loading code
change static image b2, file$, 256, 256
if file exist("_temp.bmp") then delete file "_temp.bmp"
remend
`Menu Save As - not working
if ID = 4
if image exist(1)
save_file$=save file dialog("Save as...","Anarchy Editor Map (*.aem)")
if asc(mid$(save_file$,1))<>0
save image save_file$,1
endif
else
a=make message box("Not implemented yet!","",0)
endif
endif
`Exit (X),ALT+F4, etc.
repeat
get event
h=event get handle()
m=event get message()
if h=get main window()
if m=WM_CLOSE
a=make message box("Are you sure you want to quit?","BBB Gui plugin",MB_YESNO)
if a=IDYES then end bbb gui : end
endif
endif
until m=0
`sync sleep 1
sync
loop
end
function MoveGadget()
rem rotate player with mouse
yrotate object gadget_obj,object angle y(gadget_obj)+mousemovex()/3.0
xrotate object gadget_obj,object angle x(gadget_obj)+mousemovey()/3.0
oldx# = object position x(gadget_obj)
oldy# = object position y(gadget_obj)
oldz# = object position z(gadget_obj)
angy# = object angle y(gadget_obj)
vx# = 0
vz# = 0
if keystate(32)=1 then vx# = vx# + cos(angy#) : vz# = vz# - sin(angy#)
if keystate(30)=1 then vx# = vx# - cos(angy#) : vz# = vz# + sin(angy#)
if keystate(31)=1 then vx# = vx# - sin(angy#) : vz# = vz# - cos(angy#)
if keystate(17)=1 then vx# = vx# + sin(angy#) : vz# = vz# + cos(angy#)
rem this would be the player's final position without collision
x# = oldx#+vx#
y# = oldy#+vy#
z# = oldz#+vz#
rem position the player
position object gadget_obj,x#,oldy#,z#
endfunction
function positionCameraToObject(obj)
position camera object position x(gadget_obj),object position y(gadget_obj),object position z(gadget_obj)
rotate camera object angle x(gadget_obj)+45,object angle y(gadget_obj),object angle z(gadget_obj)
CamDis#=CamDis#+mousemovez()*0.25
if camera angle x()>80 then xrotate camera 80
if camera angle x()<-60 then xrotate camera -60
`if upkey()=1 then CamDis#=CamDis#+2.5
`if downkey()=1 then CamDis#=CamDis#-2.5
if CamDis#>-70 then CamDis#=-70
if CamDis#<-300 then CamDis#=-300
move camera CamDis#
endfunction
I used BBB's plugin form 26.03.10.Second plugin update as i remember there could be drastic changes in it.I'm not the author so let me know if this don't work after you try to execute it.
EDIT: Matrix1's plugin too.I almost forget about this.I feel it like second nature to have it, so...
Run a search for BBB in the forum to find the original plugin thread.The plugin setup can be tricky i myself writed in the plugin thread so you can follow what problems i have back then and how i solved them if you are unable to do it properly without any help.Again let me know what's the issue.
There are few probable obstecles you can face when you try to run this.First of all the code changes colors wherever it wants to commented one in my editor, idk why.While i speak about comments i need to say that i put few lines to guide myself.You can erase them if they seem stupid to you or just ignore them.
The controls are really wierd...There is spawn gadget in the center of the screen which is controled by WASD in SlideMode 2 in which the mouse is locked at the gadget.Ascend with Q and descend with E in this mode.SlideMode 1 unlocks the mouse so you can interact with objects.Currently texturing spawned cubes with some dummy texture when LMB is pressed on the object.With "3" you actually make new cube at current position of the spawn gadget.
You may need to look carefully at the code because i really messed the part where the code is saved and loaded again.The file where the so called level is written is "level_tmp.aem" anything else won't work.If you manage ever to run this when you do click "File" in the Menu Then "Save as" and save some dummy file with extention ".aem" manually written else the program isn't gonna show this dummy file when you choose "Open" in the menu which is the next what you need to do.Once you Open this empty file anything you create in the level is gonna trigger making of "level_tmp.aem" file which can be loaded the way you make it with all cubes at the places where you position them.
This is really really big mess i have no time to put in order so i share it.Still something can be learned or copied from here.If there is someone willing to bang his head against the wall after trying to make this work properly, please welcome.
I think there can be some use to show you the other the other source file which is holding read/write routines order and the idea i've got how this is gonna develop further.
sync on : sync rate 60
set display mode 640,480,32,1
set window on
set dir "c:\01\"
FileID=0
`open datafile to write 1,"datafile.dat"
`for t=1 to 100
`inc FileID
`write datafile string 1,"obj"
`write datafile string 1,str$(FileID)
`write datafile string 1,""
`Index for number of objects, which must be made when this is loaded.So we know how many times to do for - next loop.
`with objects data as position, scale, angle and so on...
`write datafile integer 1,2
`write datafile string 1,""
`Object data of object 1, object type as byte(1 represents "cube", dont forget to make sheet with all types) and position for now
remstart
`TYPE
write datafile byte 1,1
`SIZE
write datafile integer 1,10:
`POS X
write datafile float 1,100.0
`POS Y
write datafile float 1,100.0
`POS Z
write datafile float 1,100.0
`TYPE
write datafile byte 1,1
`SIZE
write datafile integer 1,100:
`POS X
write datafile float 1,10.0
`POS Y
write datafile float 1,10.0
`POS Z
write datafile float 1,10.0
remend
`Next line is not nesecery.Just not sure, may become handy.Delete if not useful
`write datafile string 1,""
`Object data of object 1,, just position for now
`write datafile float 1,10.0 : write datafile float 1,10.0 : write datafile float 1,0.0
`Next line is not nesecery.Just not sure, may become handy.Delete if not useful
`write datafile string 1,""
`REMEMBER to make when there is object manipulation in the editor to buffer all the object data somehow(read about memblocks
`and memory banks) and write it to the map file at once, when there is safe used.
`close datafile 1
open datafile to read 1,"datafile.dat"
for n = 1 to 200000
obj_type = datafile byte(1)
obj_size = datafile integer (1)
if obj_type = 1 `which is cube
make object cube n,obj_size
pos_x = datafile float(1):pos_y = datafile float(1):pos_z = datafile float(1)
position object n,pos_x,pos_y,pos_z
endif
next n
close datafile 1
`close datafile 1
`pos_x = datafile float(1):pos_y = datafile float(1):pos_z = datafile float(1)
`position object 1,pos_x,pos_y,pos_z
`dummy_string$ = datafile string$(1)
`pos_x = datafile float(1):pos_y = datafile float(1):pos_z = datafile float(1)
`position object 2,pos_x,pos_y,pos_z
`dummy_string$ = datafile string$(1)
make camera 1
find_free_object = find free object()
FileID = FIND FREE DATAFILE(1,100)
do
control camera using arrowkeys 1,1,1
`text 0,0,"object position : "+"x "+str$(object position x(1))+" y "+str$(object position y(1))+" z "+str$(object position z(1))
`text 0,20,"object position : "+"x "+str$(object position x(2))+" y "+str$(object position y(2))+" z "+str$(object position z(2))
text 0,40,"end of file"+str$(find_free_object)
text 0,60,"free datafile"+str$(FileID)
sync
loop
remstart
for t=1 to 100
FileID = datafile integer (1)
open datafile to update 2,"datafile2.dat"
write datafile integer 2,FileID
close datafile 2
print FileID
next t
remend
wait key
This is a long one, luck reading all this.
Cheers!
Coding is My Kung Fu!
And My Kung Fu is better than Yours!