Using NextFile to scroll back

Blitz3D Forums/Blitz3D Beginners Area/Using NextFile to scroll back

fox95871(Posted 2009) [#1]
Hi, can someone please show me how I can make my mesh viewer scroll back through the meshes instead of just forward? To run it, you just need a folder on the desktop named Meshes with some 3ds, b3d, or x files in it.




_PJ_(Posted 2009) [#2]
NextFile only reads one-way.. there's no "LastFile" equivalent.

I'd recommend running a quick scan through the DIR first, and storing ALL the (relevant) files in a Custom Type or Array.

When you then come to do your actual Mesh file exploration, reference the Type/Array rather than the DIR.

I'll get an example done in a few if you're still stuck.


fox95871(Posted 2009) [#3]
That'd be good, Blitz is only my first language! Just alter mine, okay? Thanks for offering to do that.


_PJ_(Posted 2009) [#4]
Alrighty... try this:


;-- removed (See below)




fox95871(Posted 2009) [#5]
Holy codebox, Malice! Just kidding. Thanks, I look forward to trying it. And feel free to keep the viewer if it's helpful, I've been meaning to make one for a while now.


fox95871(Posted 2009) [#6]
I'm just getting a blackscreen now, not even any text. Any idea why? I tried adding CameraClsColor camera,100,100,100 but it stayed black, so I know it's not some camera positioning bug.


_PJ_(Posted 2009) [#7]
Okay, there was a coupla problems with my code.
LoadMesh apparently needs complete paths or single strings, not any concatenations it seems.

Anyway all fixed now :
Graphics3D 640,480
SetBuffer BackBuffer()
SeedRnd MilliSecs()
Const fps=30
blitz=LoadFont("blitz",14,1,0,0)
SetFont blitz
ClearTextureFilters
TextureFilter "alpha",2
period=1000/fps
time=MilliSecs()-period
elapsed%=0
ticks%=0
tween#=0
rt%=0

AmbientLight 100,100,100

;Make a couple of globals to keep track of things
Global numberofmeshes=0
Global directory$=("C:\Documents and Settings\Owner\Desktop\Meshes\")
Global currentmesh=0

Global Mesh

; Make an array to store mesh filenames in.
; In  the unlikely event you have more than 99 meshes in the folder,  just change the number :) 
Dim Meshfiles$(99)
;Now populate the array with relevant mesh files (.md2, .b3d, .3ds and .x are included here for completeness)
Global folder=ReadDir(directory$)
Global file$=NextFile$(folder)
While (Not(file$=""))
	If ((Right$(Lower$(file$),4)=".md2") + (Right$(Lower$(file$),4)=".b3d") + (Right$(Lower$(file$),4)=".3ds") + (Right$(Lower$(file$),2)=".x"))
		numberofmeshes=numberofmeshes+1
		Meshfiles$(numberofmeshes)=(directory$+file$)
	End If
	file$=NextFile$(folder)
Wend

; Safety check, just in case.
If (Meshfiles$(1)="")
	RuntimeError "No Meshes In Folder "+directory$
Else
	;Default the starting mesh to number '1'
	currentmesh=1
End If

Color 100,100,100

camera=CreateCamera()
CameraZoom camera,1.6
PositionEntity camera,0,+50,-200

light=CreateLight()

;start with the default 'currentmesh' which is set to number 1
filename$=Meshfiles$(currentmesh)
Mesh=LoadMesh(filename$)

EntityColor Mesh,100,100,100

While Not KeyHit(1)
	
;right control loads the next mesh
	If KeyHit(157)
		FreeEntity Mesh 
		; Ensure we wrap-around' once the last mesh is reached. Preventing errors if we run out of meshes in the folder.
		If (currentmesh=numberofmeshes)
			currentmesh=0
		End If
		currentmesh=currentmesh+1
		filename$=Meshfiles$(currentmesh)
		Mesh=LoadMesh(filename$)
		EntityColor Mesh,100,100,100
	EndIf
	
;left control loads the previous
	If KeyHit(29)
		FreeEntity Mesh 
		; Ensure we wrap-around' once the first mesh is reached. Preventing errors if we try to go back to '0'.
		If (currentmesh=1) And (numberofmeshes>1)
			currentmesh=(numberofmeshes+1)
		End If
		currentmesh=currentmesh-1
		filename$=Meshfiles$(currentmesh)
		Mesh=LoadMesh(filename$)
		EntityColor Mesh,100,100,100
	EndIf

	PositionMesh Mesh,(KeyDown(205)-KeyDown (203)) Shl True,KeyDown(200) -KeyDown(208) Shl True, 0
	RotateMesh  Mesh, 0,KeyDown(045)-KeyDown(044), 0

							
	If KeyDown(054)
		ScaleMesh Mesh,0.9,0.9,0.9
	EndIf
							
	If KeyDown(028)
		ScaleMesh Mesh,1.1,1.1,1.1
	EndIf
							
	UpdateWorld
	RenderWorld
							
	Text 0, 0,"Controls: up down left right z x shift enter and control" 
	Text 0,15,"File: " +Right$((Meshfiles(currentmesh)),(Len(Meshfiles$(currentmesh))-Len(directory$)))
							
	Flip
	Wend
End



fox95871(Posted 2009) [#8]
Okay, I'll check it out in the next few days. Wow, you wrote that whole thing without testing it? I usually have to hit F5 after just about everything I add to make sure it worked.


_PJ_(Posted 2009) [#9]
Well the 'guts' of the program were yours and the additions I made were something quite common that I've made use of before. The main reason I didn't test it was because the filepath you used "C:\Documents and Settings\Owner\Desktop\Meshes\" is quite specific and I weas too lazy to change this and ut in some meshes of my own to test at first.

Not testing though, is bad practice and I really ought to have done, hence the bugs that crept in! :)

I made sure to test it before re-posting the final draft though!