Author Topic: OpenGl2D Library  (Read 6222 times)

0 Members and 1 Guest are viewing this topic.

Patrice Terrier

  • Guest
Re: OpenGl2D Library
« Reply #15 on: October 09, 2015, 12:53:49 AM »
Mike

Maybe that rough PB SDK code translation, could help you to convert it into true compiled code.

ML.bas main code
Code: [Select]
#COMPILE EXE "ML.exe"

#INCLUDE "MiniPB.inc" '// Flat API declaration
'MACRO Pi = 3.141592653589793##
'MACRO HalfPI = 1.5707963267948965##
'MACRO PiDiv4 = 0.785398163397448##
MACRO M_TWOPI = 6.28318530718##

'SetWindowLong(ME, GWL_STYLE, &H6000000)
%ClientW            = 1024
%ClientH            = 768

TYPE PROP
    hMain           as dword
    MinTrackSizeW   as long
    MinTrackSizeH   as long
    col(1023, 767)  as long
    CC(128, 8)      as long
    NZ(511, 511)    as long
    WB(1024, 384 to 768) as single
    WX(1023, 384 to 767) as single
    WY(1023, 384 to 767) as single
    FC              as single
    SX              as single
    SY              as single
END TYPE

GLOBAL gP as PROP ''// Global class properties

function Lerp(byval c1 as single, byval c2 as single, byval k as single) as dword
    function = ((c1 and &HFF) * k + (c2 and &HFF) * (1 - k)) or (((c1 and &HFF00) * k + (c2 and &HFF00) * (1 - k)) and &HFF00) or (((c1 and &HFF0000) * k + (c2 and &HFF0000) * (1 - k)) and &HFF0000)
end function

function BC(byval x as single, byval y as single) as single
    local ix, iy as long
    ix = round(x, 0): iy = round(y, 0)
    gP.SX = x - ix: gP.SY = y - iy
    local c0, c1, c2, c3 as long
    local ixy, isyx, isxy, xy as single
    ixy = (1 - gP.SX) * (1 - gP.SY)
    isxy = gP.SX * (1 - gP.SY)
    isyx = gP.SY * (1 - gP.SX)
    xy = gP.SX * gP.SY

    c0 = gP.CC(ix and 127, iy mod 9)
    c1 = gP.CC((ix + 1) and 127, iy mod 9)
    c2 = gP.CC(ix and 127, (iy + 1) mod 9)
    c3 = gP.CC((ix + 1) and 127, (iy + 1) mod 9)

    function = (c0 and &HFF) * ixy + (c1 and &HFF) * isxy + (c2 and &HFF) * isyx + (c3 and &HFF) * xy + _
               ((c0 and &HFF00) * ixy + (c1 and &HFF00) * isxy + (c2 and &HFF00) * isyx + (c3 and &HFF00) * xy and &HFF00) + _
               ((c0 and &HFF0000) * ixy + (c1 and &HFF0000) * isxy + (c2 and &HFF0000) * isyx + (c3 and &HFF0000) * xy and &HFF0000)
end function

function BN(byval x as single, byval y as single) as long
    local ix, iy, idx, idy as long
    ix = round(x, 0): iy = round(y, 0)
    local ssx, ssy as single
    gP.SX = x - ix: gP.SY = y - iy
    ssx = 1 - gP.SX: ssy = 1 - gP.SY
    idx = (ix + 1) and 511: idy = (iy + 1) and 511
    ix = ix and 511: iy = iy and 511
    function = gP.NZ(ix, iy) * ssx * ssy + gP.NZ(idx, iy) * gP.SX * ssy + gP.NZ(ix, idy) * ssx * gP.SY + gP.NZ(idx, idy) * gP.SX * gP.SY
end function

Sub Air(byval hDC as dword)
    local x, y as long, c as dword, k1, k2, s as single
    'local gtc as dword: gtc = GetTickCount()
    'print "Running Air() ";
    for y = 0 to 767
        k1 = (1 - abs(383.5 - y) / 384) ^ 5
        for x = 0 to 1023
            if (y = gP.SY) then
                k2 = 0.25
            else
                k2 = atn((x - gP.SX) / (y - gP.SY)) / M_TWOPI + 0.25
            end if
            if (y - gP.SY < 0) then k2 = k2 + 0.5
            k2 = BN(k2 * 512, 0) * 0.03
            k2 = 0.2 - k2 ^ 2: if k2 < 0 then k2 = 0
            s = 30 / sqr((x - gP.SX) ^ 2 + (y - gP.SY) ^ 2)
            if (s > 1) then s = 1
            c = Lerp(&HFFFFFF, gP.FC, k2 * (1 - s))
            SetPixelV(hDC, x, y, Lerp(c, gP.col(x, y), k1))
        next
    next
    'Print GetTickCount() - gtc, " msec"
end sub

sub Water()
    local sx1, sy1, sx2, sy2 as single
    local x, y, x1, y1, k, kx as long
    'local gtc as dword: gtc = GetTickCount()
    'Print "Running Water() "

    for y = 767 to 384 step -1
        k = (y - 383) * 0.5: kx = (900 - y) / 580
        for x = 1023 to 0 step -1
            sy1 = 64000 / (y - 380)
            sx1 = (x - 511.5) * sy1 * 0.002
            sy2 = sy1 * 0.34 - sx1 * 0.71
            sx2 = sx1 * 0.34 + sy1 * 0.71
            sy1 = sy2 * 0.34 - sx2 * 0.21
            sx1 = sx2 * 0.34 + sy2 * 0.21
            gP.WB(x, y) = BN(sx1, sy1) - BN(sx2, sy2)
            gP.WX(x, y) = (gP.WB(x + 1, y) - gP.WB(x, y)) * k * kx
            gP.WY(x, y) = (gP.WB(x, y + 1) - gP.WB(x, y)) * k
            x1 = abs(x + gP.WX(x, y))
            y1 = 768 - y + gP.WY(x, y)
            if (y1 < 0) then
                y1 = 0
            elseif (y1 > 383) then
                y1 = 383
            end if
            gP.col(x, y) = Lerp(BC(x1 / 8 / 2, y1 / 48 / 2), &H251510, kx) ' water tint
        next
    next
    'Print GetTickCount() - gtc, " msec"
end sub

sub Sky()
    local x, y, k as long
    local c1, c2 as dword
    local s, sx1, sy1, dy as single
    'local gtc as dword: gtc = GetTickCount()
    'Print "Running Sky() ";

    gP.SX = 100 + rnd() * 824: gP.SY = 192 + rnd() * 157

    for y = 0 to 383
        sy1 = 100000 / (390 - y)
        for x = 0 to 1023
            sx1 = (x - 511.5) * sy1 * 0.0005
            k = BN(sx1, sy1) - BN(sx1 * 0.14 + sy1 * 0.21, sy1 * 0.14 - sx1 * 0.21)
            if (k < -8) then
                k = 0
            else
                k = (k + 8) * 0.02 ' cloud density
            end if
            if (k > 1) then k = 1
            dy = y / 384
            gP.FC = &H908000 + (gP.SY + 500) * 0.2 ' haze tint
            c1 = Lerp(gP.FC + 25, &H906050, dy)
            c2 = Lerp(&H807080, &HD0D0D0, dy)
            s = 30 / sqr((x - gP.SX) ^ 2 + (y - gP.SY) ^ 2) ' sun size
            if (s > 1) then s = 1
            c1 = Lerp(&HFFFFFF, c1, s)
            gP.col(x, y) = Lerp(c2, c1, k)
        next
    next
    'Print GetTickCount() - gtc, " msec"
end sub

sub Colorize()
    local x, y, xx, yy, c, r, g, b, nc as long
    'local gtc as dword: gtc = GetTickCount()
    'Print "Running Colorize() ";

    for x = 0 to 127
        for y = 0 to 7
            r = g = b = 0
            for yy = 0 to 47
                for xx = 0 to 7
                    c = gP.col(xx + x * 8, yy + y * 48)
                    r = r + (c and &HFF)
                    g = g + (c and &HFF00)
                    nc = c and &HFF0000: shift right nc, 8
                    b = b + nc
                next
            next
            nc = (b \ 384) and &HFF00: shift left nc, 8
            gP.CC(x, y) = r \ 384 + ((g \ 384) and &HFF00) + nc
        next
        gP.CC(x, 8) = gP.CC(x, 7)
    next
    'Print GetTickCount() - gtc, " msec"
end sub

sub Initialize()
    local x, y, d, d2 as long
    d = 64: d2 = 128

    'local gtc as dword: gtc = GetTickCount()
    'Print "Running Initialize() ";

    randomize
    do
        for y = 0 to 511 step d2
            for x = 0 to 511 step d2
                gP.NZ((x + d) and 511, y) = (gP.NZ(x, y) + gP.NZ((x + d2) and 511, y)) * 0.5 + d * (rnd() - 0.5)
                gP.NZ(x, (y + d) and 511) = (gP.NZ(x, y) + gP.NZ(x, (y + d2) and 511)) * 0.5 + d * (rnd() - 0.5)
                gP.NZ((x + d) and 511, (y + d) and 511) = (gP.NZ(x, y) + gP.NZ((x + d2) and 511, (y + d2) and 511) + gP.NZ(x, (y + d2) and 511) + gP.NZ((x + d2) and 511, y)) * 0.25 + d * (rnd() - 0.5)
            next
        next
        if d = 1 then exit do
        d = d \ 2: d2 = d + d
    loop
    'Print GetTickCount() - gtc, " msec"
end sub

sub RenderScene(byval hDC as dword)
    Initialize()
    Sky()
    Colorize()
    Water()
    Air(hDC)
end sub

function WndProc(byval hWnd as dword, byval Msg as dword, byval wParam as dword, byval lParam as dword) as long
    LOCAL ps as PAINTSTRUCT
    LOCAL rc as RECT

    SELECT CASE long Msg

    CASE %WM_GETMINMAXINFO
         LOCAL pMM as MINMAXINFO PTR
         pMM = lParam
         @pMM.ptMinTrackSize.x = gP.MinTrackSizeW
         @pMM.ptMinTrackSize.y = gP.MinTrackSizeH

    'case %WM_NCHITTEST
    '     function = 2 ' %HTCAPTION
    '     exit function

    CASE %WM_SIZE
         InvalidateRect(hWnd, byval(%NULL), %TRUE)

    CASE %WM_COMMAND
         LOCAL wmId, wmEvent as long
         wmID    = LOINT(wParam)
         wmEvent = HIINT(wParam)
         '//SELECT CASE long LOWRD(wParam)
         '//
         '//END SELECT

    CASE %WM_PAINT
         LOCAL hDC as dword
         'InvalidateRect(hWnd, %NULL, %FALSE)
         hDC = BeginPaint(hWnd, ps)
         '// Paint the window content here
         RenderScene(hDC)
         EndPaint(hWnd, ps)
         function = 0: EXIT function

    CASE %WM_DESTROY
         PostQuitMessage(0)
         function = 0: EXIT function

    END SELECT

    function = DefWindowProc(hWnd, Msg, wParam, lParam)

end function

function WinMain (byval hInstance     as long, _
                  byval hPrevInstance as long, _
                  byval lpCmdLine     as ASCIIZ PTR, _
                  byval iCmdShow      as long) as long

    LOCAL nRet     as dword
    LOCAL wcx      as WNDCLASSEXA
    LOCAL szClass  as ASCIIZ * 16
    szClass = "FLAT_API_POPUP" '// The class name of our popup window.

    wcx.cbSize = SIZEOF(wcx)
    LOCAL IsInitialized as long
    IsInitialized = GetClassInfoEx(hInstance, szClass, wcx)
    if IsInitialized&   = 0 then
        wcx.style         = %CS_HREDRAW OR %CS_VREDRAW
        wcx.lpfnWndProc   = CODEPTR(WndProc)
        wcx.cbClsExtra    = 0
        wcx.cbWndExtra    = 0 '// %EXTEND_EXTRA * 4
        wcx.hInstance     = hInstance
        wcx.hIcon         = %NULL
        wcx.hCursor       = LoadCursor(%NULL, byval %IDC_ARROW)
        wcx.hbrBackground = GetStockObject(%WHITE_BRUSH)
        wcx.lpszMenuName  = %NULL
        wcx.lpszClassName = VARPTR(szClass)
        wcx.hIconSm       = wcx.hIcon
        if RegisterClassEx(wcx) then IsInitialized = %TRUE
    end if

    if (IsInitialized) then
        LOCAL r as RECT
        LOCAL uMsg as TagMSG
        LOCAL dwExStyle, dwStyle, hMain as dword
        LOCAL x, y as long

        dwExStyle = %WS_EX_APPWINDOW OR %WS_EX_WINDOWEDGE
        dwStyle = %WS_POPUP OR %WS_CAPTION OR %WS_SYSMENU OR %WS_THICKFRAME OR %WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX OR %WS_CLIPSIBLINGS OR %WS_CLIPCHILDREN

        SetRect(r, 0, 0, %ClientW, %ClientH)
        AdjustWindowRectEx(r, dwStyle, %FALSE, dwExStyle)

        gP.MinTrackSizeW = r.nRight - r.nLeft
        gP.MinTrackSizeH = r.nBottom - r.nTop
        x = MAX&((GetSystemMetrics(%SM_CXSCREEN) - gP.MinTrackSizeW) \ 2, 0)
        y = MAX&((GetSystemMetrics(%SM_CYSCREEN) - gP.MinTrackSizeH) \ 2, 0)

        gP.hMain = CreateWindowEx(dwExStyle, szClass, "Popup window 32-bit", dwStyle, _
                                  x, y, gP.MinTrackSizeW, gP.MinTrackSizeH, 0, 0, hInstance, byval %NULL)

        if (gP.hMain) then

            ShowWindow(gP.hMain, iCmdShow)
            SetForegroundWindow(gP.hMain) '// Slightly Higher Priority

            WHILE GetMessage(uMsg, %NULL, 0, 0)
                 TranslateMessage(uMsg)
                 DispatchMessage(uMsg)
            WEND

            nRet = uMsg.wParam
        end if

    end if

    function = nRet
end function

MiniPB.inc include
Code: [Select]
'// Flat API declaration

%CS_VREDRAW             = &H0001
%CS_HREDRAW             = &H0002

%SM_CXSCREEN            = 0
%SM_CYSCREEN            = 1

%IDC_ARROW              = 32512

%WS_BORDER              = 8388608
%WS_POPUP               = -2147483648
%WS_CHILD               = 1073741824
%WS_VISIBLE             = 268435456
%WS_CLIPSIBLINGS        = 67108864
%WS_CLIPCHILDREN        = 33554432
%WS_CAPTION             = 12582912
%WS_SYSMENU             = 524288
%WS_THICKFRAME          = 262144
%WS_MINIMIZEBOX         = 131072
%WS_MAXIMIZEBOX         = 65536
%WS_TABSTOP             = %WS_MAXIMIZEBOX
%WS_VSCROLL             = 2097152

%WS_EX_APPWINDOW        = 262144
%WS_EX_WINDOWEDGE       = 256

%WM_CREATE              = &H1
%WM_DESTROY             = &H2
%WM_PAINT               = &HF
%WM_CLOSE               = &H10
%WM_COMMAND             = &H111
%WM_GETMINMAXINFO       = &H24

%TRUE                   = 1
%FALSE                  = 0
%NULL                   = 0

%SW_HIDE                = 0
%SW_SHOW                = 5

%WHITE_BRUSH            = 0

TYPE POINT
    x AS LONG   '// long x
    y AS LONG   '// long y
END TYPE

TYPE TagMSG
    hWnd     AS DWORD   '// HWND hwnd
    nMessage AS DWORD   '// UINT Message
    wParam   AS DWORD   '// WPARAM wParam
    lParam   AS DWORD   '// LPARAM lParam
    nTime    AS DWORD   '// DWORD time
    pt       AS POINT   '// POINT pt
END TYPE

TYPE RECT
    nLeft   AS LONG   '// long Left
    nTop    AS LONG   '// long top
    nRight  AS LONG   '// long Right
    nBottom AS LONG   '// long bottom
END TYPE

TYPE WNDCLASSEXA
    cbSize          AS DWORD        '//Type C : UINT
    style           AS DWORD        '//Type C : UINT
    lpfnWndProc     AS DWORD        '//Type C : WNDPROC
    cbClsExtra      AS LONG         '//Type C : int
    cbWndExtra      AS LONG         '//Type C : int
    hInstance       AS DWORD        '//Type C : HINSTANCE
    hIcon           AS DWORD        '//Type C : HICON
    hCursor         AS DWORD        '//Type C : HCURSOR
    hbrBackground   AS DWORD        '//Type C : HBRUSH
    lpszMenuName    AS DWORD        '//Type C : LPCSTR
    lpszClassName   AS DWORD        '//Type C : LPCSTR
    hIconSm         AS DWORD        '//Type C : HICON   
END TYPE

TYPE PAINTSTRUCT
    hdc             AS DWORD        '//Type C : HDC
    fErase          AS LONG         '//Type C : BOOL
    rcPaint         AS RECT         '//RECT est une autre structure
    fRestore        AS LONG         '//Type C : BOOL
    fIncUpdate      AS LONG         '//Type C : BOOL
    rgbReserved     AS ASCIIZ * 32  '//Type C : BYTE
END TYPE

TYPE MINMAXINFO
    ptReserved      AS POINT
    ptMaxSize       AS POINT
    ptMaxPosition   AS POINT
    ptMinTrackSize  AS POINT
    ptMaxTrackSize  AS POINT
END TYPE

DECLARE FUNCTION AdjustWindowRectEx LIB "USER32.DLL" ALIAS "AdjustWindowRectEx" (lpRect AS RECT, BYVAL dsStyle AS LONG, BYVAL bMenu AS LONG, BYVAL dwEsStyle AS DWORD) AS LONG
DECLARE FUNCTION BeginPaint LIB "USER32.DLL" ALIAS "BeginPaint" (BYVAL hWnd AS DWORD, lpPaint AS PAINTSTRUCT) AS LONG
DECLARE FUNCTION CreateWindowEx LIB "USER32.DLL" ALIAS "CreateWindowExA" (BYVAL dwExStyle AS DWORD, lpClassName AS ASCIIZ, lpWindowName AS ASCIIZ, BYVAL dwStyle AS DWORD, BYVAL x AS LONG, BYVAL y AS LONG, _
                 BYVAL nWidth AS LONG, BYVAL nHeight AS LONG, BYVAL hWndParent AS DWORD, BYVAL hMenu AS DWORD, BYVAL hInstance AS DWORD, lpParam AS ANY) AS DWORD
DECLARE FUNCTION DefWindowProc LIB "USER32.DLL" ALIAS "DefWindowProcA" (BYVAL hWnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG
DECLARE FUNCTION DispatchMessage LIB "USER32.DLL" ALIAS "DispatchMessageA" (lpMsg AS tagMSG) AS LONG
DECLARE FUNCTION EndPaint LIB "USER32.DLL" ALIAS "EndPaint" (BYVAL hWnd AS DWORD, lpPaint AS PAINTSTRUCT) AS LONG
DECLARE FUNCTION GetClassInfoEx LIB "USER32.DLL" ALIAS "GetClassInfoExA" (BYVAL hInst AS DWORD, lpszClass AS ASCIIZ, lpWndClass AS WNDCLASSEXA) AS LONG
DECLARE FUNCTION GetMessage LIB "USER32.DLL" ALIAS "GetMessageA" (lpMsg AS tagMSG, BYVAL hWnd AS DWORD, BYVAL uMsgFilterMin AS DWORD, BYVAL uMsgFilterMax AS DWORD) AS LONG
DECLARE FUNCTION GetStockObject LIB "GDI32.DLL" ALIAS "GetStockObject" (BYVAL nIndex AS LONG) AS DWORD
DECLARE FUNCTION GetSystemMetrics LIB "USER32.DLL" ALIAS "GetSystemMetrics" (BYVAL nIndex AS LONG) AS LONG
DECLARE FUNCTION InvalidateRect LIB "USER32.DLL" ALIAS "InvalidateRect" (BYVAL hWnd AS DWORD, lpRect AS RECT, BYVAL bErase AS LONG) AS LONG
DECLARE SUB      PostQuitMessage LIB "USER32.DLL" ALIAS "PostQuitMessage" (BYVAL nExitCode AS LONG)
DECLARE FUNCTION RegisterClassEx LIB "USER32.DLL" ALIAS "RegisterClassExA" (pcWndClassEx AS WNDCLASSEXA) AS WORD
DECLARE FUNCTION SetForegroundWindow LIB "USER32.DLL" ALIAS "SetForegroundWindow" (BYVAL hWnd AS DWORD) AS LONG
DECLARE FUNCTION SetRect LIB "USER32.DLL" ALIAS "SetRect" (lpRect AS RECT, BYVAL X1 AS LONG, BYVAL Y1 AS LONG, BYVAL X2 AS LONG, BYVAL Y2 AS LONG) AS LONG
DECLARE FUNCTION ShowWindow LIB "USER32.DLL" ALIAS "ShowWindow" (BYVAL hWnd AS DWORD, BYVAL nCmdShow AS LONG) AS LONG
DECLARE FUNCTION TranslateMessage LIB "USER32.DLL" ALIAS "TranslateMessage" (lpMsg AS tagMSG) AS LONG
DECLARE FUNCTION LoadCursor LIB "USER32.DLL" ALIAS "LoadCursorA" (BYVAL hInstance AS DWORD, lpCursorName AS ASCIIZ) AS DWORD
declare function SetPixelV lib "GDI32.DLL" alias "SetPixelV" (byval hDC as dword, byval x as long, byval y as long, byval crColor as dword) as long

This pure SDK code could be easily translated to C/C++, however this one will produce a small 16384 bytes standalone EXE (when compiled with PB 10), and 15872 bytes (with PB 9.05).  ;)

Note: for speed and to keep the code small,  avoid to use variant like the plague.

...
« Last Edit: October 09, 2015, 01:07:50 AM by Patrice Terrier »

Mike Lobanovsky

  • Guest
Re: OpenGl2D Library
« Reply #16 on: October 09, 2015, 04:23:16 AM »
Hi Patrice,

Thanks for the PB code! I've got the translation early in the morning but John's servers weren't accessible from this country for quite some time, hence the delay, for which I apologize.

Here comes the Dynamic C script. It uses FBSL's default window to draw to but some generic main window code may be easily added if the DynC portion is used with another static C compiler.

I used a 24-bit DIB and SetPixelV() because it doesn't add much overhead. It adds less than 100 ms extra as compared to setting the DIB pixel values directly but direct writes would require transposition of the array which would also take time and would drive me dizzy. I'm leaving SetPixelV() to you to substitute if necessary. :)

Overall startup time is approx. 0.5 sec on my PC. A precompiled executable is also attached in the zip below. Enjoy! :)

Code: C
  1. // VB6 code  (c)2014 Mikle           http://www.fbsl.net/phpbb2
  2. // FBSL port (c)2015 Mike Lobanovsky http://www.fbsl.net/phpbb2
  3.  
  4. #Include <Include/Windows.inc>
  5.  
  6. Type BITMAPINFOHEADER Align 2
  7.   %biSize
  8.   %biWidth
  9.   %biHeight
  10.   %biPlanes * 16
  11.   %biBitCount * 16
  12.   %biCompression
  13.   %biSizeImage
  14.   %biXPelsPerMeter
  15.   %biYPelsPerMeter
  16.   %biClrUsed
  17.   %biClrImportant
  18. End Type
  19.  
  20. Type BITMAPINFO
  21.   bmiHeader As BITMAPINFOHEADER
  22.   %bmiColors
  23. End Type
  24.  
  25. Dim %Pixdata[1023, 767]
  26. Dim Dib As BITMAPINFO
  27.  
  28. With Dib.bmiHeader
  29.   .biSize = SizeOf(BITMAPINFOHEADER)
  30.   .biWidth = 1024
  31.   .biHeight = 768
  32.   .biPlanes = 1
  33.   .biBitCount = 24
  34.   .biCompression = BI_RGB
  35. End With
  36.  
  37. Dim Bmp = CreateDIBSection(GetDC(ME), @Dib, DIB_RGB_COLORS, NULL, NULL, 0)
  38. Dim MemDC = CreateCompatibleDC(GetDC)
  39.  
  40. SetDIBits(GetDC, Bmp, 0, 768, @Pixdata, @Dib, DIB_RGB_COLORS)
  41. SelectObject(MemDC, Bmp)
  42. ReleaseDC(ME, GetDC)
  43.  
  44. Render(MemDC)
  45.  
  46. SetWindowLong(ME, GWL_STYLE, &H6000000)
  47. FbslTile(ME, Bmp)
  48. Resize(ME, 0, 0, 1024, 768)
  49. Center(ME): Show(ME)
  50.  
  51. Begin Events
  52.   Select Case CBMSG
  53.     Case WM_NCHITTEST
  54.       Return HTCAPTION
  55.     Case WM_COMMAND
  56.       If CBWPARAM = 2 Then PostMessage(ME, WM_CLOSE, 0, 0)
  57.     Case WM_DESTROY
  58.       DeleteDC(MemDC)
  59.       DeleteObject(Bmp)
  60.   End Select
  61. End Events
  62.  
  63. DynC Render(%dc)
  64.   #ifndef M_TWOPI
  65.     #define M_TWOPI 6.28318530717958647692 // Pi * 2
  66.   #endif
  67.   #ifdef RAND_MAX
  68.     #undef RAND_MAX
  69.   #endif
  70.   #define RAND_MAX 32767.0
  71.  
  72.   // *********************************************
  73.   // For other C compilers, replace this part with
  74.   // #include <windows.h>
  75.   // #include <math.h>
  76.   // Standard headers may require much stricter
  77.   // func parm and local var type definitions
  78.   // *********************************************
  79.  
  80.   #ifndef STDCALL
  81.     #define STDCALL __attribute__((stdcall))
  82.   #endif
  83.  
  84.   double pow(double, double);
  85.   double fabs(double);
  86.   double atan(double);
  87.   double sqrt(double);
  88.   double floor(double);
  89.  
  90.   int STDCALL RtlZeroMemory(void*, int);
  91.   int STDCALL GetTickCount(void);
  92.   int STDCALL SetPixelV(int, int, int, int);
  93.  
  94.   // *********************************************
  95.  
  96.   #define Rnd() rand() / RAND_MAX
  97.   #define Randomize() srand(GetTickCount())
  98.  
  99.   static int Col[1024][768], CC[128][8], NZ[512][512], WB[1024][768], WX[1024][768], WY[1024][768];
  100.   static double SX = 0.0, SY = 0.0, FC = 0.0;
  101.  
  102.   int Lerp(int c1, int c2, double k)
  103.   {
  104.     double d = 1.0 - k;
  105.    
  106.     return (int)((c1 & 0xFF)* k + (c2 & 0xFF)* d)
  107.       | ((int)((c1 & 0xFF00)* k + (c2 & 0xFF00)* d) & 0xFF00)
  108.       | ((int)((c1 & 0xFF0000)* k + (c2 & 0xFF0000)* d) & 0xFF0000);
  109.   }
  110.  
  111.   double BN(double x, double y) {
  112.     int ix = (int)floor(x), iy = (int)floor(y), dx = (ix + 1) & 511, dy = (iy + 1) & 511;
  113.     double SX = x - ix, SY = y - iy, isx = 1.0 - SX, isy = 1.0 - SY;
  114.    
  115.     ix &= 511; iy &= 511;
  116.     return NZ[ix][iy] * isx * isy + NZ[dx][iy] * SX * isy + NZ[ix][dy] * isx* SY + NZ[dx][dy] * SX * SY;
  117.   }
  118.  
  119.   int BC(double x, double y) {
  120.     int ix = (int)floor(x), iy = (int)floor(y), c0, c1, c2, c3;
  121.     double SX = x - ix, SY = y - iy, ixy = (1.0 - SX) * (1.0 - SY);
  122.     double isxy = SX * (1.0 - SY), isyx = SY * (1.0 - SX), xy = SX * SY;
  123.    
  124.     c0 = CC[ix & 127][iy % 9];
  125.     c1 = CC[(ix + 1) & 127][iy % 9];
  126.     c2 = CC[ix & 127][(iy + 1) % 9];
  127.     c3 = CC[(ix + 1) & 127][(iy + 1) % 9];
  128.    
  129.     return (c0 & 0xFF)* ixy + (c1 & 0xFF)* isxy + (c2 & 0xFF)* isyx + (c3 & 0xFF)* xy
  130.       + ((int)((c0 & 0xFF00)* ixy + (c1 & 0xFF00)* isxy + (c2 & 0xFF00)* isyx + (c3 & 0xFF00)* xy) & 0xFF00)
  131.       + ((int)((c0 & 0xFF0000)* ixy + (c1 & 0xFF0000)* isxy + (c2 & 0xFF0000)* isyx + (c3 & 0xFF0000)* xy) & 0xFF0000);
  132.   }
  133.  
  134.   void Initialize()
  135.   {
  136.     int x, y, d = 64, d2 = 128;
  137.    
  138.     Randomize();
  139.     while (1) {
  140.       for (y = 0; y < 512; y += d2) {
  141.         for (x = 0; x < 512; x += d2) {
  142.           NZ[(x + d) & 511][y] = (NZ[x][y] + NZ[(x + d2) & 511][y])* 0.5 + d * (Rnd() - 0.5);
  143.           NZ[x][(y + d) & 511] = (NZ[x][y] + NZ[x][(y + d2) & 511]) * 0.5 + d * (Rnd() - 0.5);
  144.           NZ[(x + d) & 511][(y + d) & 511] = (NZ[x][y] + NZ[(x + d2) & 511][(y + d2) & 511]
  145.             + NZ[x][(y + d2) & 511] + NZ[(x + d2) & 511][y]) * 0.25 + d * (Rnd() - 0.5);
  146.         }
  147.       }
  148.       if (d == 1) break;
  149.       d >>= 1; d2 = d + d;
  150.     }
  151.   }
  152.  
  153.   void Colorize()
  154.   {
  155.     int x, y, xx, yy, c, r, g, b;
  156.    
  157.     for (x = 0; x < 128; x++) {
  158.       for (y = 0; y < 8; y++) {
  159.         r = g = b = 0;
  160.         for (yy = 0; yy < 48; yy++) {
  161.           for (xx = 0; xx < 8; xx++) {
  162.             c = Col[xx + x * 8][yy + y * 48];
  163.             r += (c & 0xFF);
  164.             g += (c & 0xFF00);
  165.             b += ((c & 0xFF0000) >> 8);
  166.           }
  167.         }
  168.         CC[x][y] = r / 384 + ((g / 384) & 0xFF00) + (((b / 384) & 0xFF00) << 8);
  169.       }
  170.       CC[x][8] = CC[x][7];
  171.     }
  172.   }
  173.  
  174.   void Sky()
  175.   {
  176.     int x, y, c1, c2;
  177.     double k, s, sx1, sy1, dy;
  178.    
  179.     Initialize();
  180.    
  181.     SX = 100 + Rnd() * 824; SY = 192 + Rnd() * 157;
  182.     for (y = 0; y < 384; y++) {
  183.       sy1 = 100000.0 / (390.0 - y);
  184.       for (x = 0; x < 1024; x++) {
  185.         sx1 = (x - 511.5) * sy1 * 0.0005;
  186.         k = BN(sx1, sy1) - BN(sx1 * 0.14 + sy1 * 0.21, sy1 * 0.14 - sx1 * 0.21);
  187.         if (k < -8.0)
  188.           k = 0.0;
  189.         else
  190.           k = (k + 8.0) * 0.02; // cloud density
  191.         if (k > 1.0) k = 1.0;
  192.         dy = y / 384.0;
  193.         FC = 0x908000 + (SY + 500.0) * 0.2; // haze tint
  194.         c1 = Lerp(FC + 25, 0x906050, dy);
  195.         c2 = Lerp(0x807080, 0xD0D0D0, dy);
  196.         s = 30.0 / sqrt((x - SX) * (x - SX) + (y - SY) * (y - SY)); // sun size
  197.         if (s > 1.0) s = 1.0;
  198.         c1 = Lerp(0xFFFFFF, c1, s);
  199.         Col[x][y] = Lerp(c2, c1, k);
  200.       }
  201.     }
  202.   }
  203.  
  204.   void Water()
  205.   {
  206.     int x, y;
  207.     double x1, y1, k, kx, sx1, sy1, sx2, sy2;
  208.    
  209.     Colorize();
  210.    
  211.     for (y = 767; y >= 384; y--) {
  212.       k = (y - 383) * 0.5; kx = (900 - y) / 580.0;
  213.       for (x = 1023; x >= 0; x--) {
  214.         sy1 = 64000.0 / (y - 380);
  215.         sx1 = (x - 511.5) * sy1 * 0.002;
  216.         sy2 = sy1 * 0.34 - sx1 * 0.71;
  217.         sx2 = sx1 * 0.34 + sy1 * 0.71;
  218.         sy1 = sy2 * 0.34 - sx2 * 0.21;
  219.         sx1 = sx2 * 0.34 + sy2 * 0.21;
  220.         WB[x][y] = BN(sx1, sy1) - BN(sx2, sy2);
  221.         WX[x][y] = (WB[x + 1][y] - WB[x][y]) * k * kx;
  222.         WY[x][y] = (WB[x][y + 1] - WB[x][y]) * k;
  223.         x1 = fabs(x + WX[x][y]);
  224.         y1 = 768.0 - y + WY[x][y];
  225.         if (y1 < 0.0)
  226.           y1 = 0.0;
  227.         else if (y1 > 383.0)
  228.           y1 = 383.0;
  229.         Col[x][y] = Lerp(BC(x1 / 8, y1 / 48), 0x251510, kx); // water tint
  230.       }
  231.     }
  232.   }
  233.  
  234.   void Air(int hDC)
  235.   {
  236.     int x, y, c;
  237.     double k1, k2, s;
  238.    
  239.     for (y = 0; y < 768; y++) {
  240.       k1 = pow((1.0 - fabs(383.5 - y) / 384.0), 5.0);
  241.       for (x = 0; x < 1024; x++) {
  242.         if (y == SY)
  243.           k2 = 0.25;
  244.         else
  245.           k2 = atan((x - SX) / (y - SY)) / M_TWOPI + 0.25;
  246.         if (y - SY < 0) k2 = k2 + 0.5;
  247.         k2 = BN(k2 * 512.0, 0.0) * 0.03;
  248.         k2 = 0.2 - k2 * k2; if (k2 < 0.0) k2 = 0.0;
  249.         s = 30.0 / sqrt((x - SX) * (x - SX) + (y - SY) * (y - SY));
  250.         if (s > 1.0) s = 1.0;
  251.         c = Lerp(0xFFFFFF, FC, k2 * (1.0 - s));
  252.         SetPixelV(hDC, x, y, Lerp(c, Col[x][y], k1));
  253.       }
  254.     }
  255.   }
  256.  
  257.   void main(int hDC)
  258.   {
  259.     RtlZeroMemory(Col, 1024 * 768 * sizeof(int));
  260.     RtlZeroMemory(CC,  128  * 8   * sizeof(int));
  261.     RtlZeroMemory(NZ,  512  * 512 * sizeof(int));
  262.     RtlZeroMemory(WB,  1024 * 768 * sizeof(int));
  263.     RtlZeroMemory(WX,  1024 * 768 * sizeof(int));
  264.     RtlZeroMemory(WY,  1024 * 768 * sizeof(int));
  265.    
  266.     Sky();
  267.     Water();
  268.     Air(hDC);
  269.   }
  270. End DynC

[attachment deleted by admin]

JRS

  • Guest
Re: OpenGl2D Library
« Reply #17 on: October 09, 2015, 04:54:25 AM »
Quote
John's servers weren't accessible from this country for quite some time,

And why was that?
Why didn't you e-mail me with the issue on your end?

There was only a short outage a while back due to the All BASIC Blog (WP) with hacking issues turning the server into a spam server and eating resources. (China 'friends')


Mike Lobanovsky

  • Guest
Re: OpenGl2D Library
« Reply #18 on: October 09, 2015, 05:08:51 AM »
Hi John,

Why didn't you e-mail me with the issue on your end?

I would, if only the problem persisted for over 12 hours but it didn't. The outage does happen from time to time, probably once in two or three weeks for a few hours but of course not as often as in the BP case. At any rate, we aren't as active here now as we used to be, so that isn't a problem for me really. As it happens, fbsl dot net is usually out for a few hours late on Wednesday nights as well (it uses a U.S. server provider) but there it's usually due to maintenance hours.

No big deal, really. :)