|
Post by aurel on Feb 13, 2022 10:50:43 GMT -6
Recently folks on FreeBasic forum rambling about double buffering and one of them make example with rotating line over axis. I don't want to post here FB code because depend a lot of win api so i post my version written in microA..easier to translate to RCBasic well i am surprised that microA progie execute faster than FB.. 'Rotate Line var x,y,x1,y1,pi,t,w,h ,r, w2, h2,fPi,fRad w = 640 : h = 480 : w2 = w / 2 : h2 = h / 2 fPi = -Cos(1) : fRad = fPi / 180 wcolor 0,0,0 t = 0 r=200
fcolor 0,100,200 '---------------------------------- label start fcolor 0,0,0 : rect 0,0,640,480 x = w2 + r * Cos(t) : y = h2 + r * Sin(t) x1 = w2 + r * Cos(t + fRad * 90) : y1 = h2 + r * Sin(t + fRad * 90) fcolor 0,100,200 : line x,y,x1,y1 : circle x,y,4 swap t = t + 0.01666
swap goto start
|
|
|
Post by johnno56 on Feb 13, 2022 12:43:01 GMT -6
Translated just fine...
xmax = 640 ymax = 480 title$ = "Rotate Line by Aurel" center_x = windowpos_centered center_y = windowpos_centered WindowOpen(1,title$,center_x,center_y,xmax,ymax,WindowMode(1,0,0,0,0),1) SetWindowAutoClose(1, 0) CanvasOpen(1,xmax,ymax,0,0,xmax,ymax,0) ClearCanvas
w = 640 : h = 480 : w2 = w / 2 : h2 = h / 2 fPi = -Cos(1) : fRad = fPi / 180
t = 0 r=200
setcolor(rgb(0,100,200 )) '---------------------------------- do clearcanvas x = w2 + r * Cos(t) y = h2 + r * Sin(t) x1 = w2 + r * Cos(t + fRad * 90) y1 = h2 + r * Sin(t + fRad * 90) line(x, y, x1, y1) : circle(x, y, 4) t = t + 0.01666 if Not WindowExists(1) Or WindowEvent_Close(1) Then end end if update() loop until key(27) = 1
|
|
|
Post by aurel on Feb 13, 2022 13:03:18 GMT -6
thanks johnno yyes work fine and smooth ...but i still cannot understand why in microA work little bit faster..ok rcb version use sdl bu FB version use same GDI windows api and is so damn slow..ahh ok
|
|
|
Post by Tomaaz on Feb 13, 2022 14:41:26 GMT -6
Aurel, I've translated your code to... FreeBasic
dim as integer w, h, w2, h2, r dim as double x, y, x2, y2, t, f
w = 640 h = 480 w2 = w / 2 h2 = h / 2 f = -cos(1) / 180 t = 0 r = 200
screenres w, h, 32 windowtitle "Rotate Line" color rgb(0, 100, 200), rgb(0, 0, 0) do cls screenlock() x = w2 + r * cos(t) y = h2 + r * sin(t) x2 = w2 + r * cos(t + f * 90) y2 = h2 + r * sin(t + f * 90) line (x, y)-(x2, y2) circle (x, y), 4 t = t + 0.01666 screenunlock() loop
I got rid of some unused/unnecessary variables (pi, fPi), renamed some others (x1, y1 to x2, y2, fRad to f) and organized the code (shouldn't you be using rect 0, 0, w, h?). This code is so fast that it doesn't make sense to run it without sleep. I also don't understand why anybody would bother to use WinApi for this simple example. Thy must have been talking about something completely different and you must have misunderstood the topic...
|
|
|
Post by aurel on Feb 13, 2022 16:09:41 GMT -6
wow...it seems that you forget something for example how to exit from program ?? we talking on FB forum about double buffering using windows and one member add example in fbgx mode your code is not one of this...and not work properly on my windows machine drawing artefacts instead of flying line so you do sommething wrong !
|
|
|
Post by aurel on Feb 13, 2022 16:11:53 GMT -6
Also i posted code in my microA because is simplier than FB code posted in FB forum my is GDI and win api but instead of CLS i am using rectangle function to mimic CLS
|
|
|
Post by aurel on Feb 13, 2022 16:13:22 GMT -6
Here is next case looks like small moon orbiter over earth with names : 'Rotate Line 2 var x,y,x1,y1,pi,t,w,h ,r, w2, h2,fPi,fRad,r2 var x2,y2 w = 640 : h = 480 : w2 = w / 2 : h2 = h / 2 fPi = Cos(0.98) : fRad = fPi / 180 wcolor 0,0,0 t = 0 r=200 r2 = 50 fcolor 0,100,200 '---------------------------------- label start fcolor 0,0,0 : rect 0,0,640,480 x = w2 + r * Cos(t) : y = h2 + r * Sin(t) x1 = w2 + r * Cos(t + fRad * 90) : y1 = h2 + r * Sin(t + fRad * 90)
fcolor 0,100,200 : line x,y,x1,y1 : circle x,y,10 : circle x1,y1,10 x2 = w2 + r2 * Cos(t + fRad * 90) : y2 = h2 + r2 * Sin(t + fRad * 90) fcolor 200,180,0 : line x1,y1,x2,y2 : circle x1,y1,10 : circle x2,y2,10 print x2,y2,"earth" : print x,y,"moon" t = t + 0.001666
swap goto start
|
|
|
Post by johnno56 on Feb 13, 2022 16:14:08 GMT -6
I'm not sure if this will have much of a change... Back in the mid to late 80's I had an Amstrad CPC464 which used Locomotive Basic. CPU speed was only 4mhz and most graphic routines were pretty slow. Calculating Sin() and Cos() within a loop was way too slow. The work-around was to pre-calculate Sin() and Cos() angles, store in an arrays, then use the arrays to draw the graphics. The speed change, back then, was a good improvement.
Just a thought...
I would imagine that today's CPU and math co-processors could easily calculate Sin() and Cos() angles 'on the fly'...
|
|
|
Post by Tomaaz on Feb 13, 2022 16:56:04 GMT -6
It is drawing the flying line, but, like I told you, it's to fast for a monitor's refresh rate and that's why, instead of a smoothly moving line, you can see only some steps of this movement. I just wanted to show you how fast the calculations and drawing in FreeBasic are. Just add sleep 1 or sleep 10 after screenunclock(): dim as integer w, h, w2, h2, r dim as double x, y, x2, y2, t, f
w = 640 h = 480 w2 = w / 2 h2 = h / 2 f = -cos(1) / 180 t = 0 r = 200
screenres w, h, 32 windowtitle "Rotate Line" color rgb(0, 100, 200), rgb(0, 0, 0) do cls screenlock() x = w2 + r * cos(t) y = h2 + r * sin(t) x2 = w2 + r * cos(t + f * 90) y2 = h2 + r * sin(t + f * 90) line (x, y)-(x2, y2) circle (x, y), 4 t = t + 0.01666 screenunlock() sleep 1 loop
This is pure FreeBasic (no Raylib, IUP etc.) and yes - the documentation says that there are faster methods to clear the screen that CLS, but, in this case CLS is more than enough. And no - I didn't forget anything. I've translated your code and in your code there is no way to exit program. If you're unable to find out how to to exit my program, I can explain it to you. But we both know that you already know it, so let's not change the subject to something irrelevant.
|
|
|
Post by aurel on Feb 13, 2022 17:15:35 GMT -6
In my code there is no need to exit program because my program internaly use this : 'peek messagges if PeekMessage (wm,0,0,0,Pm_Remove)>0 ' //peek TranslateMessage (wm) DispatchMessage (wm) end if
so i can intercept WM_CLOSE by clicking X of window . your code shoud look like this :
dim as integer w, h, w2, h2, r dim as double x, y, x2, y2, t, f w = 640 h = 480 w2 = w / 2 h2 = h / 2 f = -cos(1) / 180 t = 0 r = 200 screenres w, h, 32 windowtitle "Rotate Line" color rgb(0, 100, 200), rgb(0, 0, 0)
'Dim evt As EVENT
do cls screenlock() x = w2 + r * cos(t) y = h2 + r * sin(t) x2 = w2 + r * cos(t + f * 90) y2 = h2 + r * sin(t + f * 90) line (x, y)-(x2, y2) circle (x, y), 4 t = t + 0.01666 screenunlock()
Sleep(10) Loop Until InKey = Chr(27)
of course that i know how to make exit but because of others it is correct to add it . and Yes i know that FB is fast ..for the sake it is compiler ...but what the guy on fB forum posted is not very well option for such a program ... he use two time BitBlt...
and here is his code :
'coded by UEZ #Include "fbgfx.bi" #Include "windows.bi"
Using FB
Const As UInteger iW = 1000, iH = 800, iWh = iW \ 2, iHh = iH \ 2 Const As Single fPi = ACos(-1), fRad = fPi / 180
ScreenControl FB.SET_DRIVER_NAME, "GDI" ScreenRes iW, iH, 24, 1, GFX_HIGH_PRIORITY Or GFX_NO_SWITCH
Dim As String sTitle = "GDI Double Buffering" WindowTitle sTitle
Dim as HWND hHwnd ScreenControl(FB.GET_WINDOW_HANDLE, Cast(Integer, hHWND))
Dim As Ulong Ptr aBitmap Dim As BITMAPINFO tBITMAP With tBITMAP.bmiheader .biSize = Sizeof(BITMAPINFOHEADER) .biWidth = iW .biHeight = -iH .biPlanes = 1 .biBitCount = 32 .biCompression = BI_RGB End With
Dim As HDC hDC = GetDC(hHwnd) Dim As HDC hDC_backbuffer = CreateCompatibleDC(0) 'Dim As Any Ptr hHBitmap = CreateDIBSection(hDC_backbuffer, @tBITMAP, DIB_RGB_COLORS, @aBitmap, NULL, NULL) Dim As any ptr hHBitmap = CreateCompatibleBitmap( hDC , iw, ih) Dim As HDC hCanvas, hPen Var hObjOld = SelectObject(hDC_backbuffer, hHBitmap) 'SetGraphicsMode(hDC, 2) 'SetStretchBltMode(hDC_backbuffer, 4) 'high quality mode
hPen = SelectObject(hDC_backbuffer, GetStockObject(DC_Pen))
Dim As Single r = 200, t = 0 Dim evt As EVENT
Do BitBlt(hDC_backbuffer, 0, 0, iW, iH, hDC, 0, 0, WHITENESS) ' BitBlt(hDC, 0, 0, iW, iH, hDC, 0, 0, ) MoveToEx(hDC_backbuffer, iWh + r * Cos(t), iHh + r * Sin(t), NULL) LineTo(hDC_backbuffer, iWh + r * Cos(t + fRad * 90), iHh + r * Sin(t + fRad * 90)) t += 0.01666666 BitBlt(hDC, 0, 0, iW, iH, hDC_backbuffer, 0, 0, SRCCOPY)
Sleep(10) Loop Until ((InKey = Chr(27)) Or (evt.Type = EVENT_WINDOW_CLOSE))
SelectObject(hDC_backbuffer, hObjOld) DeleteDC(hDC_backbuffer) ReleaseDC(hHWND, hDC) DeleteObject(hHBitmap) SelectObject(hDC_backbuffer, hPen) DeleteObject(hPen)
..as you may see he even create DiBbitmap section ..and in the same time he don't use WM_PAINT message to properly process back/front buffers fliping. that is all.
|
|
|
Post by johnno56 on Feb 13, 2022 17:42:50 GMT -6
You wrote, "in your code there is no way to exit program"... But the very last line of my conversion states, "loop until key(27) = 1". Meaning: Continue the loop until key(27) (escape key) is pressed... My mistake... I thought you were referring to me... I failed to notice the conversion by Tomaaz...
A lot of that FB listing I can follow but some of those command/function are all Greek to me... and 'you' managed to reduce all that to 20+ lines of code?... Cool...
|
|
|
Post by Tomaaz on Feb 13, 2022 19:26:44 GMT -6
Aurel, you can simple click "x" on that black window that appears behind the graphics window and, since I doubt my code will be run by anyone else, there is no need to make any changes to it. BTW What is the window title in your program!? Come on - fix it! The future of IT depends on it.
|
|
|
Post by aurel on Feb 14, 2022 1:35:25 GMT -6
Please tomaaz...stop "eat the shit" and post proper program if you are capable to post it .. In your code there is no any response to WM_CLOSE message and i don't know what kind of back windows you see ...ghost windows maybe
|
|
|
Post by rosy on Feb 14, 2022 2:38:32 GMT -6
You don't need sin to draw circles. This last program does what? Rotates the entire image?
|
|
|
Post by aurel on Feb 14, 2022 2:56:33 GMT -6
rosy i don't investigate what i need or not I investigate how is program made : look: #Include "fbgfx.bi" #Include "windows.bi" he use combination of winapi windows.bi include and fb wrapper for gdi to make program in general bad idea and not present proper way for double buffering with GDI that i a whole story. no program just rotate line over circle center
|
|