Jump to content

Using Model Thumbnails


Haydenmango
 Share

Recommended Posts

SuperStrict

Import "FileAttributes.bmx"
Import brl.stream
Import brl.pixmap
Import brl.map
Import brl.filesystem
'Import BRL.StandardIO

Type TMetafile

Field path:String
Field thumbnail:TPixmap[9]
Field keys:TMap=New TMap

Method Save:Int()
	Local i:Int
	Local stream:TStream
	Local key:String
	Local countthumbs:Int

	'If path="C:/Leadwerks/Projects/DarknessAwaits/Materials/tree_branch.tex.meta"
	'	DebugStop
	'EndIf

	'Print "saving meta file "+path

	If FileType(ExtractDir(path))=0 CreateDir(ExtractDir(path),True)

	ShowFile path

	stream=WriteFile(path)
	If Not stream Return False
	stream.WriteString("META")
	stream.WriteInt 1'version

	Local keycount:Int=0
	For key=EachIn keys.keys()
		keycount:+1
	Next
	stream.WriteInt keycount
	For key=EachIn keys.keys()
		stream.WriteLine key
		'Print key+": "+String(keys.ValueForKey(key))
		stream.WriteLine String(keys.ValueForKey(key))
	Next
	For i=0 To thumbnail.length-1
		If thumbnail[i]
			countthumbs:+1
		EndIf
	Next
	'Print countthumbs+" thumbnails saved to file"

	stream.WriteInt countthumbs
	For i=0 To thumbnail.length-1
		If thumbnail[i]
			stream.WriteInt i
			stream.writebytes thumbnail[i].pixels,thumbnail[i].width*thumbnail[i].height*4
		EndIf
	Next

	stream.Close()

	HideFile path

	Return True
EndMethod

Function LoadThumbnail:TPixmap(path:String,thumbsize:Int)
	Local stream:TStream
	Local metafile:TMetafile
	Local keycount:Int
	Local thumbcount:Int
	Local i:Int
	Local size:Int
	Local width:Int
	Local key:String
	Local value:String
	Local pixmap:TPixmap

	ShowFile path
	stream=ReadFile(path)
	If Not stream Return Null
	If stream.Eof()
		stream.Close()
		Return Null
	EndIf

	'File tag
	If stream.ReadString(4)<>"META"
		stream.Close()
		HideFile path
		Return Null
	EndIf

	'Version
	Select stream.ReadInt()
	Case 1
	Default
		stream.Close()
		HideFile path
		Return Null
	EndSelect

	keycount=stream.ReadInt()
	For i=0 To keycount-1
		key=stream.ReadLine()
		value=stream.ReadLine()
	Next

	thumbcount:Int=stream.ReadInt()
?debug
	'Print thumbcount+" thumbnails in file."
?
	For i=0 To thumbcount-1
		size=stream.ReadInt()
?debug			
		'Print size+", "+thumbsize
?
		width=2^size
		If size=thumbsize			
			pixmap=CreatePixmap(width,width,PF_RGBA8888)
			stream.readbytes pixmap.pixels,width*width*4
			stream.Close()
			HideFile path
			Return pixmap
		Else
			stream.Seek stream.Pos()+width*width*4
		EndIf
	Next

	stream.Close()
	HideFile path
	Return Null		
EndFunction

Function Load:TMetaFile(path:String,thumbs:Int=True)
	Local stream:TStream
	Local metafile:TMetafile
	Local keycount:Int
	Local thumbcount:Int
	Local i:Int
	Local size:Int
	Local width:Int
	Local key:String
	Local value:String

	'Print "Loading metafile "+path

	showfile path
	stream=ReadFile(path)
	If Not stream Return Null
	If stream.Eof()
		stream.Close()
		HideFile path
		Return Null
	EndIf

	If stream.ReadString(4)<>"META"
		stream.Close()
		HideFile path
		Return Null
	EndIf

	Select stream.ReadInt()
	Case 1
	Default
		stream.Close()
		HideFile path
		Return Null
	EndSelect

	metafile=New TMetafile
	metafile.path=path
	keycount=stream.ReadInt()
	'Print keycount+" keys"
	For i=0 To keycount-1
		key=stream.ReadLine()
		value=stream.ReadLine()
		metafile.keys.Insert key,value
		'Print key+": "+value
	Next

	If thumbs
		thumbcount:Int=stream.ReadInt()
		For i=0 To thumbcount-1
			size=stream.ReadInt()
			width=2^size
			metafile.thumbnail[size]=CreatePixmap(width,width,PF_RGBA8888)
			stream.readbytes metafile.thumbnail[size].pixels,width*width*4
		Next
	EndIf

	stream.Close()
	HideFile path
	Return metafile
EndFunction

EndType

  • Upvote 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

That's the code that loads them. There's a file version, then a list of string keys / values, then all stored thumbnails. The size you want is not guaranteed to be in the file, but most of the time the 64x64 thumbnail will be there.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

It's in the meta file that is autogenerated, so you open them just like any other file (like you did with your save files for HFF). It looks like each color channel is one byte in this case, so you would just use ReadUChar() for that:

http://www.leadwerks.com/werkspace/page/api-reference/_/stream/streamreaduchar-r272

 

Basically follow this order of commands:

 

--This sequence avoids all error checking, so implement that if you want
file=FileSystem:ReadFile(path_to_file)
file:ReadString(4)
file:ReadInt()
keycount=ReadInt()
for x=0,keycount-1 do
 file:ReadLine()
end
thumbcount=file:ReadInt()
for x=0, thumbcount-1 do
 size=file:ReadInt()
 width=2^size
 for x1=0,width-1 do
   for y1=0,width-1 do
  red=file:ReadUChar()
  green=file:ReadUChar()
  blue=file:ReadUChar()
  alpha=file:ReadUChar()
  --Do something with these colors
   end
 end
end

  • Upvote 1
Link to comment
Share on other sites

Here is a quick and dirty thumbnail extractor program. Just drag and drop the meta file onto the canvas and it will show the last saved thumbnail in the meta file. Click File-->Save and it will save a *.PNG file into the meta file's directory.

 

thumbnail_extractor.zip

  • Upvote 3

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Thanks for the responses guys! I don't understand file handling very much so I've been pretty clueless.

 

@nick.ace - Ahhh this is possibly what Josh was getting at. I was just trying to open the .meta files by clicking on them... lol

Loading them through code makes a lot of sense and now I know!

 

@macklebee - That is just what I needed and it works great! Thank you very much for making that. :)

 

Now I have some code to learn from and instant gratification from macklebees app!

Thanks again!

  • Upvote 1

Check out my game Rogue Snowboarding- 

https://haydenmango.itch.io/roguesnowboarding

Link to comment
Share on other sites

It was fun dusting off my copy of blitzmax and playing around with that. even tho it took a couple cups of coffee to remember how to do anything, i remembered how nice it was to use this software. Makes me wish Josh would post the leadwerks module that he is using so I can make a LE3 game with it.

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...