PDA

View Full Version : Mirage



peter
12-10-2012, 16:52
Hi,

A little bit mirage.


Uses "ui"
Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Declare Function SetPixelV Lib "gdi32" Alias "SetPixelV" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long


DWord hdc,hwin
Long dx,cx,col
Long buf(128,128)
Single wAmp,wFreq,x,m
wAmp=6:wfreq=.3


hwin=Canvas_Window("Canvas Test",1,1,640,480)
Canvas_Attach(hwin,0,%TRUE)
hdc=Canvas_GetDC()
Canvas_Clear(Rgb(0,0,255))
Canvas_Font("georgia",24,%CANVAS_FONTSTYLE_BOLD)


Canvas_BitmapRender("angel.bmp",0,0,128,128)
For cx=1 To 128
For dx=1 To 128
buf(dx,cx)=GetPixel(hdc,dx,cx)
Next
Next


While Not GetAsyncKeyState(27)
Canvas_Clear(Rgb(0,0,0))
x -= .5
m = x
For cx=1 To 128
m += wfreq
For dx=1 To 128
col=buf(dx,cx)
'SetPixelV(hdc,180+dx*2+Sin(m)*wAmp+1,120+cx*2,col)
Canvas_Circle(180+dx*2+Sin(m)*wAmp+1,120+cx*2,2,col)
Next
Next


Canvas_Color(Rgb(255,255,255),0)
Canvas_SetPos(160,10)
Canvas_Print("MIRAGE ANGEL")
Canvas_Redraw
Wend
Canvas_Window End

Petr Schreiber
13-10-2012, 09:32
Looks great :eusaclap:

Using this instead of Canvas_Circle:


Canvas_SetPixel(180+dx*2+Sin(m)*wAmp+1,120+cx*2,col)


... results in faster animation.


Petr

peter
13-10-2012, 12:39
Thank you Petr,


There is a misterious thing here.


The help says:
nColor = Canvas_GetPixel(X1, Y1)


But here is it an unknown keyword, error code 18
I have no clue what this means!

ErosOlmi
13-10-2012, 13:20
http://www.thinbasic.com/community/project.php?issueid=361#note2265

Sorry peter, my mistake.
I wrongly internally called that keyword Canvas_Pixel instead of Canvas_GetPixel

Fix is already done but will be present in next thinBasic release.
If you want, you can use Canvas_Pixel but later it will be changed.

Sorry.
Eros

peter
13-10-2012, 17:03
Thank you Eros,


Good to know that it is funktionale keyword.


I have some problems with the mouse handling. I found nothing over it.
I need Mousebuttons and mouse positions.


I am missing some Mathematics routines: ArcCos, ArcSin, Atan2 and so on.
And division by zero isn't intercepted here!


By the way, If I click on the windows (X-BOX) then I get an memory leak.
The application is still running in the background.


What I am doing wrong?

ErosOlmi
13-10-2012, 17:21
Regarding missing native math functions, please check MATH module

USES "MATH"

Than uses some of the additional math functions inside Math module:
http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?arccos.htm

On problem on exit, maybe the loop should be governed by a

While IsWindow(hwin)
...
Wend

or

While Not GetAsyncKeyState(27) and IsWindow(hwin)
...
Wend



Basic Division by zero should be trapped. Have you an example?

peter
13-10-2012, 18:57
Thank you Eros,


IsWindow(hwin) is okay.
Math is okay, need to try it yet.


Divison by zero was in an own ArcCos routine.


Function ArcCos( x as single) as single
Return Atn(-x / Sqr(-x*x+1))+2*Atn(1)
End Function


But isn't now necessary anymore!


What about the mouse?

ErosOlmi
13-10-2012, 20:19
Handling mouse move over Canvas at the moment is not handled directly by thinBasic.
You need to create a standard window with a canvas control and that do the job inside window callback.
There are examples in thinBasic sample script installation and here in forum
http://www.thinbasic.com/community/showthread.php?11529-canvas-move-control-experiment&highlight=canvas+mouse
http://www.thinbasic.com/community/showthread.php?11079-clicking-on-Geometry-Figures&highlight=click+figure

I will see what I can add for the future.
What mouse handling do you need to have?

peter
13-10-2012, 21:36
Hi Eros,


I need all Mouse Button, the middle button isn't that important.
Further I need the x-postion of mouse and the y-position of mouse


I did a little experiment, but is not satisfying for me. I will try some other tricks.


Function xMouse() As Long
Dim mRect As POINTAPI
Win_GetCursorPos (mRect)
Return mRect.x
End Function


Function yMouse() As Long
Dim mRect As POINTAPI
Win_GetCursorPos (mRect)
Return mRect.y
End Function

Here is TBGL Circle Demo for CANVAS.



Uses "ui"

DWord hdc,hwin
hwin=Canvas_Window("Canvas Circle",1,1,640,480)
Canvas_Attach(hwin,0,%TRUE)
hdc=Canvas_GetDC()
Canvas_Font("georgia",24,%CANVAS_FONTSTYLE_BOLD)
Randomize()


Long mx,my,c
Dim cx(120),cy(120),cd(120) As Single
Dim c1(120),c2(120),c3(120) As Byte


For c=1 To 60
cx(c)=400
cy(c)=300
cd(c)=Rnd(1,8)
c1(c)=Rgb(Rnd(64,255),Rnd(64,255),Rnd(32,255))
c2(c)=Rgb(Rnd(64,255),Rnd(64,255),Rnd(32,255))
c3(c)=Rgb(Rnd(64,255),Rnd(64,255),Rnd(32,255))
Next

While IsWindow(hwin)
Canvas_Clear(0)
For c=1 To 60
If cd(c)=1 Then
cx(c)=cx(c)+2
bound()
ElseIf cd(c)=2 Then
cx(c)=cx(c)-2
bound()
ElseIf cd(c)=3 Then
cy(c)=cy(c)-2
bound()
ElseIf cd(c)=4 Then
cy(c)=cy(c)+2
bound()
ElseIf cd(c)=5 Then
cy(c)=cy(c)-2
cx(c)=cx(c)-2
bound()
ElseIf cd(c)=6 Then
cy(c)=cy(c)-2
cx(c)=cx(c)+2
bound()
ElseIf cd(c)=7 Then
cy(c)=cy(c)+2
cx(c)=cx(c)-2
bound()
ElseIf cd(c)=8 Then
cy(c)=cy(c)+2
cx(c)=cx(c)+2
bound()
End If
Next
circles()
Canvas_Redraw
If GetAsyncKeyState(27) Then Exit While
Wend
Canvas_Window End


Sub circles()
For c=1 To 60
Canvas_Circle(cx(c),cy(c),40,0,Rgb(c1(c),c2(c),c3(c)))
Next
End Sub


Sub bound()
Dim i As DWord
For i=1 To 60
If cx(i) <=40 Then
cd(i) = Rnd(1,8)
cx(i) =40
End If
If cx(i) >=600 Then
cd(i) = Rnd(1,8)
cx(i) =600
End If
If cy(i) >=440 Then
cd(i) = Rnd(1,8)
cy(i) =440
End If
If cy(i) <=40 Then
cd(i) = Rnd(1,8)
cy(i) =40
End If
Next
End Sub

Function Key(xkey As Word) As Word
Return GetAsyncKeyState(xkey)
End Function

ErosOlmi
13-10-2012, 22:58
petr,

I forgot I already developed something. Check command:

lClick = Canvas_Window Click [hWnd] TO X, Y

Help can be found here: http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?canvas_window_click.htm
See if it can help.