Equivalent CHAR

BlitzMax Forums/BlitzMax Beginners Area/Equivalent CHAR

Snarty(Posted 2005) [#1]
Hi all, as the title says

C:
CHAR buffer[32];

Bmax?
buffer:Byte[32] '< don't work as expected
buffer:Byte Ptr=MemAlloc(32) '< same as above

Only way I could duplicate it is mess the memory up completely by using.
MemCopy VarPtr(buffer), "Some Text", Len("Some Text")

Then the API function I passed to read it fine, but obviously then BMax fell over eventually.


Snarty(Posted 2005) [#2]
I guess I must point out it appears in a Type. So Field Buffer[32].. etc


Perturbatio(Posted 2005) [#3]
wouldn't that be a string?

so:
Local buffer:String
buffer=buffer[..32]
Print Len(buffer)


or am I completely missing things (quite possible)?


Snarty(Posted 2005) [#4]
hmm, nope, that doesn't seem to work either.

What I'm wrapping is the Win32 API ChooseFont() which includes a CHOOSEFONT struct and a LOGFONT stuct. Everything works apart from the .lfFaceName. Unless, I use the last method in my original post (and then shaft the memory in the process).

You should be able to pass a face name to the ChooseFont() function via the CHOOSEFONT struct, but, BMax don't like it and the API don't like BMax.

Been driving me nuts for the last couple of days now (on and off).


Snarty(Posted 2005) [#5]
Ok, from the the way I understand it, using the VarPtr(lfont.lfFaceName) method with a memcopy, that would be writing the contents of the string starting at the address of lfont.lfFaceName.

If the above is true, lfont.lfFaceName would need to be a 32 byte continual block of memory (with lfont.lfFaceName being the first byte), extending the LOGFONT struct.

Or is that not what VarPtr(bleh) would be returning?


Perturbatio(Posted 2005) [#6]
qhat about:
Local buffer:String
buffer=buffer[..32]
Local pBuffer : Byte Ptr = Buffer.ToCString()

?


Snarty(Posted 2005) [#7]
Nope.. sigh. You would'nt believe the amount of permutations I've been through now (including your methods).


Snarty(Posted 2005) [#8]
Below is the reference the LOGFONT struct

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fontext_1wmq.asp

The *only* part which is causing grief I've quoted below.

lfFaceName
A null-terminated string that specifies the typeface name of the font. The length of this string must not exceed 32 characters, including the terminating null character. The EnumFontFamiliesEx function can be used to enumerate the typeface names of all currently available fonts. If lfFaceName is an empty string, GDI uses the first font that matches the other specified attributes.



Perturbatio(Posted 2005) [#9]
lfFaceName$z ?

*Edit*
i.e.:
Type TLogFont
	Field lfHeight:Long
	Field lpWidth : Long
	Field lfOrientation : Long
	Field lfWeight : Long
	Field lfItalic : Byte
	Field lfStrikeOut : Byte
	Field lfCharSet : Byte
	Field lfOutPRecision : Byte
	Field lfClipPrecision : Byte
	Field lfQuality : Byte
	Field lfPitchAndFamily : Byte
	Field lfFaceName$z
End Type



Snarty(Posted 2005) [#10]
Compile Error: Unable to cast 'Null' to 'CString'

o_O

I take it this means messing around with a .New() method constructor and deconstructor?


Perturbatio(Posted 2005) [#11]
I have to go out for now, but does this look like it's on the right path?
Type TLogFont
	Field lfHeight:Long
	Field lpWidth : Long
	Field lfOrientation : Long
	Field lfWeight : Long
	Field lfItalic : Byte
	Field lfStrikeOut : Byte
	Field lfCharSet : Byte
	Field lfOutPRecision : Byte
	Field lfClipPrecision : Byte
	Field lfQuality : Byte
	Field lfPitchAndFamily : Byte
	Field lfFaceName :Byte Ptr
End Type

Type TChooseFontStruct
	Field StructSize : Int
	Field Owner:Int
	Field HDC : Int
	Field LogFont : TLogFont Ptr
    Field iPointSize: Int
    Field Flags : Int
    Field rgbColors : Int
	Field lCustData : Int Ptr
    Field lpfnHook()
    Field lpTemplateName : Byte Ptr Ptr
    Field hInstance : Int
    Field lpszStyle$z Ptr
    Field nFontType : Int
    Field ___MISSING_ALIGNMENT__:Int
    Field nSizeMin:Int
	Field nSizeMax:Int

End Type


Extern "Win32"
	Function ChooseFontA:Int(lpcf:TChooseFontStruct Ptr)
End Extern



Snarty(Posted 2005) [#12]
yeah, all that works fine. That wasn't the issue though, the issue is with the lfFaceName field. Thanks for feedback though.

Anyhow, from running some extensive memory leak reports this problem is the least of my worries. I'll be posting the code soon.


Snarty(Posted 2005) [#13]
ok, on quick inspection, I've totally missed out "Release", should be getting somewhere now :D


skidracer(Posted 2005) [#14]
BlitzMax does not have a way of defining embedded arrays such as the square bracket arrays in Blitz3D, so if this is the C code:

typedef struct tagLOGFONTA
    {
    LONG lfHeight;
    LONG lfWidth;
    LONG lfEscapement;
    LONG lfOrientation;
    LONG lfWeight;
    BYTE lfItalic;
    BYTE lfUnderline;
    BYTE lfStrikeOut;
    BYTE lfCharSet;
    BYTE lfOutPrecision;
    BYTE lfClipPrecision;
    BYTE lfQuality;
    BYTE lfPitchAndFamily;
    CHAR lfFaceName[ 32 ];
    } 	LOGFONTA;

you need to use something like the following with varptr lfFaceName0 being the address of the string
Type TLogFont
	Field lfHeight
	Field lfWidth
	Field lfEscapement
	Field lfOrientation
	Field lfWeight

	Field lfItalic:Byte
	Field lfUnderline:Byte
	Field lfStrikeOut:Byte
	Field lfCharSet:Byte
	Field lfOutPrecision:Byte
	Field lfClipPrecision:Byte
	Field lfQuality:Byte
	Field lfPitchAndFamily:Byte

	Field lfFaceName0:Byte	
	Field lfFaceName1:Byte	
	Field lfFaceName2:Byte	
	Field lfFaceName3:Byte	
	Field lfFaceName4:Byte	
	Field lfFaceName5:Byte	
	Field lfFaceName6:Byte	
	Field lfFaceName7:Byte	
	Field lfFaceName8:Byte	
	Field lfFaceName9:Byte	
	Field lfFaceName10:Byte	
	Field lfFaceName11:Byte	
	Field lfFaceName12:Byte	
	Field lfFaceName13:Byte	
	Field lfFaceName14:Byte	
	Field lfFaceName15:Byte	
	Field lfFaceName16:Byte	
	Field lfFaceName17:Byte	
	Field lfFaceName18:Byte	
	Field lfFaceName19:Byte	
	Field lfFaceName20:Byte	
	Field lfFaceName21:Byte	
	Field lfFaceName22:Byte	
	Field lfFaceName23:Byte	
	Field lfFaceName24:Byte	
	Field lfFaceName25:Byte	
	Field lfFaceName26:Byte	
	Field lfFaceName27:Byte	
	Field lfFaceName28:Byte	
	Field lfFaceName29:Byte	
	Field lfFaceName30:Byte	
	Field lfFaceName31:Byte	
End Type

and to set the name:
lf:TLogFont=New TLogFont
a$="hello"
MemCopy Varptr lf.lfFaceName0,a,a.length
Print lf.lfFaceName0
Print lf.lfFaceName1
Print lf.lfFaceName2
Print lf.lfFaceName3
Print lf.lfFaceName4
Print lf.lfFaceName5



Snarty(Posted 2005) [#15]
Cheers Skids, I was hoping I wouldn't need to resort to that, but.. it'll do for now.

Any idea if it will be implimented? (Or at least a cleaner way)

Thanks again for the reply, I can stop chasing my own tail now.


skidracer(Posted 2005) [#16]
From looking at the roadmap, we should get embedded arrays at the same time we get inline assembler, heh...


Snarty(Posted 2005) [#17]
lol :)