Author Topic: Difference between long, int and sys  (Read 1560 times)

0 Members and 1 Guest are viewing this topic.

Arnold

  • Guest
Difference between long, int and sys
« on: October 23, 2019, 07:25:25 AM »
Hi Charles,

while I was adopting theForger's anim_one example for 64-bit, I noticed that I have to use the bitmap structure as indicated in the MSDN docu:

type BITMAP
  long bmType
  long bmWidth
  long bmHeight
  long bmWidthBytes
  word bmPlanes
  word bmBitsPixel
  sys bmBits
end type

I can replace long with int, but not with sys for 64-bit. Is it safe to say long and int are identical (32-bit number)? I often see code which apply long in some cases which should be sys in 64-bit. Is there a rule of thumb when to apply sys instead of long? Probably in case of a pointer/address.

Roland

Code: OxygenBasic
  1. $ filename "HelloWeen.exe"
  2. 'uses rtl32
  3. 'uses rtl64
  4.  
  5. uses winutil
  6.  
  7. 'Adapt OxygenBasic path
  8. string o2dir="d:\Oxygenbasic\"
  9. string aBitmap=o2dir + "projectsB\SDL\bmp\pumpa.bmp"
  10.  
  11. % SRCCOPY   = 0xCC0020
  12. % SRCINVERT = 0x660046
  13. % SRCAND    = 0x8800C6
  14. % SRCPAINT  = 0xEE0086
  15. % LR_LOADFROMFILE  = 16
  16. % IMAGE_BITMAP     = 0
  17. % WM_GETMINMAXINFO = 36
  18.  
  19. type BITMAP
  20.   long bmType
  21.   long bmWidth
  22.   long bmHeight
  23.   long bmWidthBytes
  24.   word bmPlanes
  25.   word bmBitsPixel
  26.   sys bmBits
  27. end type
  28.  
  29. macro RGB(r,g,b) {r+(g<<8)+(b<<16)}
  30.  
  31. macro setMinSize(xMin, yMin)
  32.    MINMAXINFO *mm
  33.    @mm = lParam
  34.  
  35.    mm.ptMinTrackSize.x = xMin
  36.    mm.ptMinTrackSize.y = yMin
  37. end macro
  38.  
  39.  
  40. int ID_TIMER = 1
  41. int BALL_MOVE_DELTA = 2
  42.  
  43. type BALLINFO
  44.         int width
  45.         int height
  46.         int x
  47.         int y
  48.         int dx
  49.         int dy
  50. end type
  51.  
  52. BALLINFO g_ballInfo
  53. sys g_hbmBall = null
  54. sys g_hbmMask = null
  55.  
  56. --------------------------------------------------------------------------
  57. MainWindow(640,320, WS_OVERLAPPEDWINDOW)
  58. --------------------------------------------------------------------------
  59.  
  60. function CreateBitmapMask(sys hbmColour, COLORREF crTransparent) as sys
  61.         sys hdcMem, hdcMem2
  62.         sys hbmMask
  63.         BITMAP bm
  64.  
  65.         GetObject(hbmColour, sizeof(BITMAP), bm)
  66.         hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, null)
  67.  
  68.         hdcMem = CreateCompatibleDC(0)
  69.         hdcMem2 = CreateCompatibleDC(0)
  70.  
  71.         SelectObject(hdcMem, hbmColour)
  72.         SelectObject(hdcMem2, hbmMask)
  73.  
  74.         SetBkColor(hdcMem, crTransparent)
  75.  
  76.         BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY)
  77.  
  78.         BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT)
  79.  
  80.         DeleteDC(hdcMem)
  81.         DeleteDC(hdcMem2)
  82.  
  83.         return hbmMask
  84. end function
  85.  
  86.  
  87. sub DrawBall(sys hdc, RECT *prc)
  88.         sys hdcBuffer = CreateCompatibleDC(hdc)
  89.         sys hbmBuffer = CreateCompatibleBitmap(hdc, prc.right, prc.bottom)
  90.         sys hbmOldBuffer = SelectObject(hdcBuffer, hbmBuffer)
  91.  
  92.         sys hdcMem = CreateCompatibleDC(hdc)
  93.         sys hbmOld = SelectObject(hdcMem, g_hbmMask)
  94.  
  95.         FillRect(hdcBuffer, prc, GetStockObject(WHITE_BRUSH))
  96.  
  97.         BitBlt(hdcBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width, g_ballInfo.height, hdcMem, 0, 0, SRCAND)
  98.  
  99.         SelectObject(hdcMem, g_hbmBall)
  100.         BitBlt(hdcBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width, g_ballInfo.height, hdcMem, 0, 0, SRCPAINT)
  101.  
  102.         BitBlt(hdc, 0, 0, prc.right, prc.bottom, hdcBuffer, 0, 0, SRCCOPY)
  103.  
  104.         SelectObject(hdcMem, hbmOld)
  105.         DeleteDC(hdcMem)
  106.  
  107.         SelectObject(hdcBuffer, hbmOldBuffer)
  108.         DeleteDC(hdcBuffer)
  109.         DeleteObject(hbmBuffer)  
  110. end sub
  111.  
  112.  
  113. sub UpdateBall(RECT *prc)
  114.         g_ballInfo.x += g_ballInfo.dx
  115.         g_ballInfo.y += g_ballInfo.dy
  116.  
  117.         if g_ballInfo.x < 0 then
  118.                 g_ballInfo.x = 0
  119.                 g_ballInfo.dx = BALL_MOVE_DELTA
  120.         elseif (g_ballInfo.x + g_ballInfo.width) > prc.right then      
  121.                 g_ballInfo.x = prc.right - g_ballInfo.width
  122.                 g_ballInfo.dx = -BALL_MOVE_DELTA
  123.  
  124.     endif
  125.  
  126.         if g_ballInfo.y < 0 then       
  127.                 g_ballInfo.y = 0
  128.                 g_ballInfo.dy = BALL_MOVE_DELTA
  129.         elseif (g_ballInfo.y + g_ballInfo.height) > prc.bottom then
  130.                 g_ballInfo.y = prc.bottom - g_ballInfo.height
  131.                 g_ballInfo.dy = -BALL_MOVE_DELTA
  132.  
  133.         endif    
  134. end sub
  135.  
  136.  
  137. function WndProc(sys hwnd, uint msg, sys wParam, lParam) as sys callback
  138.  
  139.         select msg     
  140.                 case WM_CREATE
  141.             SetWindowText(hwnd, "Hello Ween")
  142.                
  143.                         BITMAP bm
  144.  
  145.                         g_hbmBall = LoadImage(hInst, aBitmap, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE)
  146.                         if g_hbmBall = null then
  147.                                 mbox "Could not load ball.bmp!"
  148.             endif
  149.            
  150.                         g_hbmMask = CreateBitmapMask(g_hbmBall, RGB(255, 0, 255)) 'magenta
  151.                         if g_hbmMask = null then
  152.                                 mbox "Could not create mask!"
  153.             endif
  154.            
  155.                         GetObject(g_hbmBall, sizeof(bm), bm)
  156.  
  157.                         g_ballInfo.width = bm.bmWidth
  158.                         g_ballInfo.height = bm.bmHeight
  159.  
  160.                         g_ballInfo.dx = BALL_MOVE_DELTA
  161.                         g_ballInfo.dy = BALL_MOVE_DELTA
  162.  
  163.                         if SetTimer(hwnd, ID_TIMER, 1, null) = 0 then
  164.                                 mbox "Could not SetTimer()!"
  165.                         endif
  166.                
  167.                 case WM_CLOSE
  168.                         DestroyWindow(hwnd)
  169.                
  170.                 case WM_PAINT          
  171.                         RECT rcClient
  172.                         PAINTSTRUCT ps
  173.                         sys hdc = BeginPaint(hwnd, ps)
  174.  
  175.                         GetClientRect(hwnd, rcClient)
  176.                         DrawBall(hdc, rcClient)
  177.  
  178.                         EndPaint(hwnd, ps)
  179.  
  180.         case WM_GETMINMAXINFO
  181.            setMinSize 200,200
  182.                                
  183.                 case WM_TIMER          
  184.                         RECT rcClient
  185.                         sys hdc = GetDC(hwnd)
  186.  
  187.                         GetClientRect(hwnd, rcClient)
  188.  
  189.                         UpdateBall(rcClient)
  190.                         DrawBall(hdc, rcClient)
  191.  
  192.                         ReleaseDC(hwnd, hdc)
  193.                                
  194.                 case WM_DESTROY
  195.                         KillTimer(hwnd, ID_TIMER)
  196.  
  197.                         DeleteObject(g_hbmBall)
  198.                         DeleteObject(g_hbmMask)
  199.  
  200.                         PostQuitMessage(0)
  201.                
  202.                 case else
  203.                         return DefWindowProc(hwnd, msg, wParam, lParam)            
  204.         end select
  205.    
  206.         return 0
  207. end function
  208.  

Charles Pegge

  • Guest
Re: Difference between long, int and sys
« Reply #1 on: October 24, 2019, 12:35:12 AM »
Thanks Roland,

int and long are identical 32bit integer types in o2.

To get the correct size and alignments in a structure sys should only be used for handles and pointers.

Here is the MS definition of BITMA:

Code: [Select]
typedef struct tagBITMAP {
  LONG   bmType;
  LONG   bmWidth;
  LONG   bmHeight;
  LONG   bmWidthBytes;
  WORD   bmPlanes;
  WORD   bmBitsPixel;
  LPVOID bmBits;
} BITMAP, *PBITMAP, *NPBITMAP, *LPBITMAP;