PDA

View Full Version : SetText Problem



peter
24-10-2012, 00:43
Hi Eros,


Canvas blocked my SetText routine. Has really strange behaviour.
It doesn't like the xPosition and yPosition of the SetText function.


I tried it directly, without A.dll. and there was no problem. But this isn't what I want.
Do you have an explanation for this predicament ?



Uses "ui","A"


Declare Function LoadByte Lib "A.dll" Alias "LoadByte" (ByVal nName As String, ByRef Destination As Any) As Long
Declare Function SaveByte Lib "A.dll" Alias "SaveByte" (ByVal nName As String, ByRef Destination As Any,ByVal count As Long) As Long
Declare Function FileExits Lib "A.dll" Alias "FileExits" (ByVal nName As String) As Long
Declare Function FilePointer Lib "A.dll" Alias "FilePointer" (ByVal FileHandle As Long,ByVal count As Long) As Long
Declare Function OpenFile Lib "A.dll" Alias "OpenFile" (ByVal nName As String) As Long
Declare Function FileSize Lib "A.dll" Alias "FileSize" (ByVal FileHandle As Long) As Long
Declare Function CloseFile Lib "A.dll" Alias "CloseFile" (ByVal FileHandle As Long) As Long
Declare Function ReadLong Lib "A.dll" Alias "ReadLong" (ByVal FileHandle As Long,ByRef Destination As Any,ByVal count As Long) As Long
Declare Function ReadInt Lib "A.dll" Alias "ReadInt" (ByVal FileHandle As Long,ByRef Destination As Any,ByVal count As Long) As Long
Declare Function ReadByte Lib "A.dll" Alias "ReadByte" (ByVal FileHandle As Long,ByRef Destination As Any,ByVal count As Long) As Long
Declare Function WriteLong Lib "A.dll" Alias "WriteLong" (ByVal FileHandle As Long,ByRef Destination As Any,ByVal count As Long) As Long
Declare Function WriteInt Lib "A.dll" Alias "WriteInt" (ByVal FileHandle As Long,ByRef Destination As Any,ByVal count As Long) As Long
Declare Function WriteByte Lib "A.dll" Alias "WriteByte" (ByVal FileHandle As Long,ByRef Destination As Any,ByVal count As Long) As Long
Declare Function PlayWav Lib "A.dll" Alias "PlayWav" (ByVal WavFile As String) As Long
Declare Function SetFont Lib "A.dll" Alias "SetFont" (ByVal hdc As Long,ByVal Width As Long,ByVal Height As Long,ByVal Style As Long,ByVal FontName As String) As Long
Declare Function SetText Lib "A.dll" Alias "SetText" (ByVal hdc As Long,ByVal xPos As Long,ByVal yPos As Long,ByVal lpText As String,ByVal Colour As Long) As Long


'for testing
Declare Function GetDC Lib "user32.dll" Alias "GetDC" (ByVal hwnd As Long) As Long
Declare Function TextOut Lib "gdi32.dll" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
Declare Function TextColor Lib "gdi32.dll" Alias "SetTextColor"(ByVal hdc As Long, ByVal crColor As Long) As Long
Declare Function SetBkMode Lib "gdi32.dll" Alias "SetBkMode" (ByVal hdc As Long, ByVal nBkMode As Long) As Long


%BOLD =700
DWord hwnd,hdc,hdc1
hwnd=Canvas_Window("Dll Test",320,240,640,480)
Canvas_Attach(hwnd,0,%TRUE)
hdc=Canvas_GetDC()
hdc1=GetDC(hwnd)
SetFont(hdc,20,88,%BOLD,"")


Dim map(1200) As Byte
Long L


Asciiz s="HELLO YOU"
Asciiz a=" "


SaveByte("hello.txt",s,Len(s))
LoadByte("hello.txt",a)

L=LoadByte("map1-1.txt",map)
PlayWav("success.wav")


While IsWindow(hwnd)
Canvas_Clear(&hFF0000)


SetText(hdc,80,80,a + " ",&h64F080)
SetText(hdc,180,140,L + " BYTES LOADED",&hFFFFFF)


'Test
SetFont(hdc1,64,188,%BOLD,"")
TextColor(hdc1,Rgb(255,255,0))
TextOut(hdc1,100,100,"HELLO",5)


Canvas_Redraw
If GetAsyncKeyState(27) Then Exit While
Wend
Canvas_Window End


'Sub SetText(x,y As Long, txt As String, xkol,ykol As Long)
' Canvas_Color(xkol,ykol)
' Canvas_SetPos(x,y)
' Canvas_Print(txt)
'End Sub

ErosOlmi
24-10-2012, 06:52
It seems you do not get a correct DC using GetDC(hwnd)

Try to change the line

hdc1=GetDC(hwnd)
to

hdc1=Canvas_GetDC

Also

USES "A"
is not needed for external DLL. USES loads DLLs created using thinBasic SDK. Those DLLs are called "modules" and are special DLLs automatically loaded by thinBasic Core engine developed to automatically implement thinBasic language.
What programming language have you used to create A.DLL ?

peter
24-10-2012, 13:19
Hi Eros,


A.dll is necessary it contains all my functions. This has nothing to do with Api declaration.
A.dll was written with O2B. Full name is OxygenBasic. Thanks to Charles.
It seems that Canvas doesn't work correctly with the Window Api.

ErosOlmi
24-10-2012, 14:41
Hi peter,

I suspected it was an Oxygen DDL :)
I follow Charles forum every now and than.

I was just saying USES "a" has no effect. It instruct thinBasic to load "a" module but because A is not a thinbasis module it will just be ignored.

Cavas Window is not just a window that you can can its DC using window handle but it has a control (or something like that) above it.
That's why you need to get the DC using Canvas_GetDC (after a proper Canvas_Atatch) dedicated function.

Eros

omarpta
24-10-2012, 15:39
USES keyword loads thinbasic core modules, designed/created with the thinbasic sdk.
When you use:

Declare Function LoadByte Lib "A.dll" Alias "LoadByte"

You are already referencing your physical dll. (Lib "A.dll")


Omar

peter
24-10-2012, 16:37
Yes I know it, but without USES "A.dll" I get a time problem!
Watch this demo and you will see what i mean. I have an irregularly speed.


Uses "ui"
#INCLUDE "A.inc"


DWord hwnd, hdc
hwnd=Canvas_Window("Icon Animation Test",320,240,640,480)
Canvas_Attach(hwnd,0,%TRUE)
hdc=Canvas_GetDC()
'SetFont(hdc,20,88,%BOLD,"")


Long acorn,alien,balls,heart,c,b,h,j
Single v1,v2,v3

acorn=LoadIcon("acorn.ico")
alien=LoadIcon("alien.ani")
balls=LoadIcon("balls.ani")
heart=LoadIcon("heart.ani")


While IsWindow(hwnd)
Canvas_Clear(&hFF0000)


For j=0 To 9
DrawIcon(hdc,balls,j*64,0,64-j*2,64-j*2,b)
Next


DrawIcon(hdc,acorn,100,100,128,128,0)
DrawIcon(hdc,alien,230,100,128,128,c)
DrawIcon(hdc,balls,230,230,128,128,b)
DrawIcon(hdc,heart,384,200,256,256,h)


v1 +=.1
v2 +=.4
v3 +=.2
If v1 >=1 Then
v1=0
c +=1
If c=19 Then c=0
End If
If v2 >=1 Then
v2=0
b +=1
If b=10 Then b=0
End If
If v3 >=1 Then
v3=0
h +=1
If h=15 Then h=0
End If


Canvas_Redraw
If GetAsyncKeyState(27) Then Exit While
'Sleep 1
Wend
Canvas_Window End

Petr Schreiber
24-10-2012, 19:50
Hi Peter,

you cannot expect to have consistent speed on any system with multitasking - you never know which other program will suddenly eat more CPU and block the others a bit. And then there is another factor - different people have different PCs of varied performance.

To solve this, I cannot recommend enough timing each frame and scale the animations according to last render time. This can be further enhanced with more advanced mathematical models, but timing is the base ;)

So instead of:


v1 +=.1


it is better to use


v1 += SpeedOfAnimationPerSecond * TimeOfLastWholeDrawing


I remember some DOS games didn't have this check, which resulted in making them unplayable on modern PC. The mentioned approach fixes this issue.

One observation - your last example runs perfectly fine on my PC, but once the script ends it crashes.


Petr

peter
24-10-2012, 22:51
Hi Petr,
Thank you for your advices here.


I know, I know, but this is proto type and Canvas is new for me..
I have written with QueryPerformsPerformance and QueryPerformanceFrequency a Subroutine, called SetFrame (frame).
Therewith should every user have the same speed, I think.


But In the meantime I changed my strategies. I began a new project.
About the crash: I can say, it runs fine with me here.