Author Topic: &H colors dont work as CONST  (Read 3110 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
&H colors dont work as CONST
« on: March 03, 2022, 11:49:47 am »
Use to work with POINT, what happened?

Doesn't appear to work way back before 1.4 even.

« Last Edit: March 03, 2022, 12:38:47 pm by bplus »

FellippeHeitor

  • Guest
Re: &H colors dont work as CONST
« Reply #1 on: March 03, 2022, 11:54:23 am »
No clue what you mean. Still waiting for reply at Discord.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: &H colors dont work as CONST
« Reply #2 on: March 03, 2022, 11:57:12 am »
This code from 4 years ago use to work with CONST SkyC = &Hffwhatever

Now it has to be DIM Shared as_Unsigned Long and then assigned for POINT to match correctly.
Code: QB64: [Select]
  1. _Title "Tanks Battle! by bplus 2018-02-03"
  2. 'from: Tanks Battle.sdlbas (B+=MGA) 2016-10-29
  3. ' let the projectiles fly!
  4.  
  5.  
  6. 'screen stuff
  7. Const SW = 1200
  8. Const SH = 720
  9.  
  10.  
  11. 'tank stuff
  12. Const tN = 15 'number of tanks
  13. Const tNm1 = tN - 1 ' for loops and arrays
  14. Const tW = 20 'width of tank
  15. Const tH = 8 'height of tank
  16. Type tank
  17.     x As Single
  18.     y As Single
  19.     da As Single
  20.     v As Single 'velocity
  21.     c As _Integer64 'color
  22.     bx As Single 'barrel
  23.     by As Single
  24.     f As _Byte 'finished
  25.  
  26. 'hole stuff
  27. Const hR = tW + 3
  28. Const topHole = 1000
  29. Type hole
  30.     x As Integer
  31.     y As Integer
  32.  
  33. 'projectile stuff
  34. Const rightA = -10
  35. Const leftA = -170
  36. Const lVel = 7
  37. Const hVel = 22
  38. Const pC = &HFFFFFF00
  39. Const gravity = .35
  40.  
  41. Screen _NewImage(SW, SH, 32)
  42.  
  43.  
  44. Const skyC = &HFF848888 ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< this  used to work !
  45. 'Dim Shared As _Unsigned Long skyC ' >>>>>>>>>>>>>>>>>>>>> fix for 2.0
  46. 'skyC = &HFF848888
  47.  
  48. Common Shared rad, deg ' yeah don't need these with _D2R and _R2D but this was 4 years ago
  49. deg = 180 / _Pi
  50. rad = _Pi / 180
  51.  
  52. ff$ = "arial" ' I have loaded and tested many fonts from Windows Folder and disappointed how few work with QB64
  53. 'load and check Big size font
  54. bArial& = _LoadFont("C:\windows\fonts\" + ff$ + ".ttf", 48, "BOLD")
  55. If bArial& <= 0 Then Print "Trouble with " + ff$ + ".ttf size 48 file, goodbye.": Sleep: End
  56. 'bFW = _FONTWIDTH(bArial&): bFH = _FONTHEIGHT(bArial&)
  57. '_FONT bArial&
  58. 'LOCATE 1, 1: PRINT "This is BIG font."
  59. 'SLEEP
  60.  
  61.  
  62. Dim Shared tanks(tNm1) As tank, holes(topHole) As hole
  63.  
  64. 'get holes set up
  65. holeIndex = -1
  66.  
  67. land& = _NewImage(SW, SH, 32)
  68. _Dest land&
  69. drawLandscape
  70.  
  71. initializeTanks
  72. hotTank = tNm1
  73.  
  74. change = 1
  75. While change 'get tanks landed before start shooting
  76.     change = 0
  77.     Cls
  78.     _PutImage , land&, 0 'land the tanks and reland the tanks if the dirt is shot out under them
  79.     For i = 0 To tNm1
  80.         If Point(tanks(i).x + tW / 2, tanks(i).y + tH + 1) = skyC Then
  81.             tanks(i).y = tanks(i).y + 2
  82.             change = 1
  83.         End If
  84.         drawTank i
  85.     Next
  86.     _Display
  87.  
  88. While 1 '< main loop start
  89.     Cls
  90.     _PutImage , land&, 0
  91.  
  92.     'the land with holes
  93.     If holeIndex > -1 Then
  94.         For ii = 0 To holeIndex
  95.             drawHole holes(ii).x, holes(ii).y
  96.         Next
  97.     End If
  98.  
  99.     'reland the tanks if the dirt is shot out under them
  100.     For i = 0 To tNm1
  101.         If tanks(i).f = 0 Then
  102.             While Point(tanks(i).x + tW / 2, tanks(i).y + tH + 1) = skyC
  103.                 tanks(i).y = tanks(i).y + 2
  104.             Wend
  105.  
  106.             'repoint barrels and reset velocitys
  107.             If Rnd < .5 Then 'avoid straight up and down  suicide shots
  108.                 tanks(i).da = rand(leftA, -92)
  109.             Else
  110.                 tanks(i).da = rand(rightA, -88)
  111.             End If
  112.             tanks(i).v = rand(lVel, hVel) 'velocity
  113.             drawTank i
  114.         End If
  115.     Next
  116.     _Display
  117.     _Delay .1
  118.  
  119.  
  120.     ''whose turn to shoot
  121.     lastMan = hotTank
  122.     hotTank = hotTank + 1
  123.     hotTank = hotTank Mod tN
  124.     While tanks(hotTank).f = 1 'look for a tank still alive
  125.         hotTank = hotTank + 1 'whose turn to shoot
  126.         hotTank = hotTank Mod tN
  127.         'did we cycle through all the dead tanks?
  128.         If hotTank = lastMan Then 'game over, last man standing
  129.             Color _RGB32(220, 255, 0), skyC
  130.             _Font bArial&
  131.             _PrintString (SW / 2 - 120, SH / 2 - 80), "Game Over!"
  132.             _Display
  133.             _Delay 5
  134.             _Font 16
  135.             Sleep
  136.             End
  137.         End If
  138.     Wend
  139.  
  140.     'setup hotTank's shot
  141.     rAngle = tanks(hotTank).da * rad 'convert here to radians for SIN and COS
  142.     pX = tanks(hotTank).bx
  143.     pY = tanks(hotTank).by
  144.     pX_change = tanks(hotTank).v * Cos(rAngle) 'this is the cuurent  X vector of the projectile
  145.     pY_change = tanks(hotTank).v * Sin(rAngle) ' this is the current Y vector of the projectile
  146.     pActive = 0 ' do not Activate until projectile sees the skyC
  147.  
  148.     While 1
  149.         pY_change = pY_change + gravity ' pY starts in upward direction but will eventually fall due to gravity
  150.         pX = pX + pX_change
  151.         pY = pY + pY_change
  152.  
  153.         'show projectile progress, hit or air
  154.         If pX >= 0 And pX <= SW And pY <= SH Then ' still active
  155.             'check for tank hit
  156.             For iTank = 0 To tNm1
  157.                 If tanks(iTank).f <> 1 And pActive Then 'tanks can blow up themselves
  158.                     If dist(pX, pY, tanks(iTank).x + tW / 2, tanks(iTank).y + tH / 2) < hR Then
  159.                         tanks(iTank).f = 1
  160.                         Color _RGB32(255, 0, 0)
  161.                         For rr = 1 To hR
  162.                             fcirc pX, pY, rr
  163.                             _Display
  164.                             _Delay .01
  165.                             If rr Mod 2 Then
  166.                                 Color _RGB32(128, 255, 0)
  167.                             Else
  168.                                 Color _RGB32(255, 0, 0)
  169.                             End If
  170.                         Next
  171.                         If holeIndex < topHole Then
  172.                             holeIndex = holeIndex + 1
  173.                             holes(holeIndex).x = pX
  174.                             holes(holeIndex).y = pY
  175.                             drawHole pX, pY
  176.                             _Display
  177.                         End If
  178.                         pX = SW + 10
  179.                         pY = SH + 10
  180.                         Exit While
  181.                     End If
  182.                 End If
  183.             Next
  184.  
  185.             If Point(pX, pY) = skyC Then
  186.                 pActive = 1
  187.                 Color pC
  188.                 fcirc pX, pY, 2 ' <<<<<<<<<<<<<<<< to see round projectiles that could be replaced by image
  189.             ElseIf pY < 0 Then
  190.                 'still hot but cant see
  191.             ElseIf Point(pX, pY) <> skyC And Point(pX, pY) <> pC And pActive Then 'hit ground?
  192.                 Color _RGB(255, 0, 0)
  193.                 For rr = 1 To hR
  194.                     fcirc pX, pY, rr
  195.                     _Display
  196.                     _Delay .01
  197.                     If rr Mod 2 Then
  198.                         Color _RGB32(128, 255, 0)
  199.                     Else
  200.                         Color _RGB32(255, 0, 0)
  201.                     End If
  202.                 Next
  203.                 If holeIndex < topHole Then
  204.                     holeIndex = holeIndex + 1
  205.                     holes(holeIndex).x = pX
  206.                     holes(holeIndex).y = pY
  207.                     drawHole pX, pY
  208.                     _Display
  209.                 End If
  210.                 pX = SW + 10
  211.                 pY = SH + 10
  212.                 Exit While
  213.             End If
  214.         Else 'not active
  215.             Exit While
  216.         End If
  217.         _Display
  218.         _Delay .03
  219.     Wend
  220.  
  221. Sub drawHole (xx, yy)
  222.     Color skyC
  223.     For i = yy To 300 Step -1
  224.         fcirc xx, i, hR
  225.     Next
  226.  
  227. Sub drawLandscape
  228.     'the sky
  229.     Line (0, 0)-(SW, SH), skyC, BF
  230.  
  231.     'the land
  232.     startH = SH - 100
  233.     rr = 70: gg = 70: bb = 90
  234.     For mountain = 1 To 6
  235.         Xright = 0
  236.         y = startH
  237.         While Xright < SW
  238.             ' upDown = local up / down over range, change along Y
  239.             ' range = how far up / down, along X
  240.             upDown = (Rnd * (.8) - .35) * (mountain * .5)
  241.             range = Xright + rand%(15, 25) * 2.5 / mountain
  242.             For x = Xright - 1 To range
  243.                 y = y + upDown
  244.                 Line (x, y)-(x + 1, SH), _RGB32(rr, gg, bb), BF
  245.             Next
  246.             Xright = range
  247.         Wend
  248.         rr = rand(rr - 15, rr): gg = rand(gg - 15, gg): bb = rand(bb - 25, bb)
  249.         If rr < 0 Then rr = 0
  250.         If gg < 0 Then gg = 0
  251.         If bb < 0 Then bb = 0
  252.         startH = startH + rand(5, 20)
  253.     Next
  254.  
  255. Sub initializeTanks ' x, y, barrel angle,  velocity, color
  256.     tl = (SW - tW) / tN: tl2 = tl / 2: tl4 = .8 * tl2
  257.     For i = 0 To tNm1
  258.         tanks(i).x = rand%(tl2 + tl * i - tl4 - tW, tl2 + tl * i + tl4 - tW)
  259.         tanks(i).y = 300 '<<<<<<<<<<<<<<<<<<<<<<<<<< for testing
  260.         tanks(i).da = rand%(-180, 0) 'degree Angle
  261.         tanks(i).v = rand%(10, 20) 'velocity
  262.         If tanks(i).da < -90 Then 'barrel  is pointed left
  263.             tanks(i).v = -1 * tanks(i).v
  264.         End If
  265.         tc = i * Int(200 / (3 * tN)) 'maximize color difference between tanks
  266.         tanks(i).c = _RGB32(55 + 2 * tc, 13 + tc, 23 + tc) ' first tank is darkest
  267.     Next
  268.     'shuffle color order
  269.     For i = tNm1 To 1 Step -1
  270.         r = rand%(0, i)
  271.         Swap tanks(i).x, tanks(r).x
  272.     Next
  273.  
  274. Sub drawTank (i)
  275.     'ink(tanks(i, "c"))
  276.     Color tanks(i).c
  277.     'turret
  278.     fEllipse tanks(i).x + tW / 2, tanks(i).y + tH / 3, tW / 4 + 1, tH / 4 + 1
  279.     bX = tW / 2 * Cos(rad * tanks(i).da)
  280.     bY = tW / 2 * Sin(rad * tanks(i).da)
  281.     Line (tanks(i).x + tW / 2, tanks(i).y + tH / 3)-(tanks(i).x + tW / 2 + bX, tanks(i).y + tH / 4 + bY)
  282.     Line (tanks(i).x + tW / 2 + 1, tanks(i).y + tH / 3 + 1)-(tanks(i).x + tW / 2 + bX + 1, tanks(i).y + tH / 4 + bY + 1)
  283.     tanks(i).bx = tanks(i).x + tW / 2 + bX
  284.     tanks(i).by = tanks(i).y + tH / 4 + bY
  285.     fEllipse tanks(i).x + tW / 2, tanks(i).y + .75 * tH, tW / 2, tH / 4
  286.     Color _RGB32(0, 0, 0)
  287.     ellipse tanks(i).x + tW / 2, tanks(i).y + .75 * tH, tW / 2 + 1, tH / 4 + 1
  288.     ellipse tanks(i).x + tW / 2 + 1, tanks(i).y + .75 * tH, tW / 2 + 1, tH / 4 + 1
  289.  
  290. Function rand% (lo%, hi%)
  291.     rand% = (Rnd * (hi% - lo% + 1)) \ 1 + lo%
  292.  
  293. Function rdir% ()
  294.     If Rnd < .5 Then rdir% = -1 Else rdir% = 1
  295.  
  296. Function dist# (x1%, y1%, x2%, y2%)
  297.     dist# = ((x1% - x2%) ^ 2 + (y1% - y2%) ^ 2) ^ .5
  298.  
  299. 'Steve McNeil's  copied from his forum   note: Radius is too common a name
  300. Sub fcirc (CX As Long, CY As Long, R As Long)
  301.     Dim subRadius As Long, RadiusError As Long
  302.     Dim X As Long, Y As Long
  303.  
  304.     subRadius = Abs(R)
  305.     RadiusError = -subRadius
  306.     X = subRadius
  307.     Y = 0
  308.  
  309.     If subRadius = 0 Then PSet (CX, CY): Exit Sub
  310.  
  311.     ' Draw the middle span here so we don't draw it twice in the main loop,
  312.     ' which would be a problem with blending turned on.
  313.     Line (CX - X, CY)-(CX + X, CY), , BF
  314.  
  315.     While X > Y
  316.         RadiusError = RadiusError + Y * 2 + 1
  317.         If RadiusError >= 0 Then
  318.             If X <> Y + 1 Then
  319.                 Line (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  320.                 Line (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  321.             End If
  322.             X = X - 1
  323.             RadiusError = RadiusError - X * 2
  324.         End If
  325.         Y = Y + 1
  326.         Line (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  327.         Line (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  328.     Wend
  329.  
  330. Sub fEllipse (CX As Long, CY As Long, xRadius As Long, yRadius As Long)
  331.     Dim scale As Single, x As Long, y As Long
  332.     scale = yRadius / xRadius
  333.     Line (CX, CY - yRadius)-(CX, CY + yRadius), , BF
  334.     For x = 1 To xRadius
  335.         y = scale * Sqr(xRadius * xRadius - x * x)
  336.         Line (CX + x, CY - y)-(CX + x, CY + y), , BF
  337.         Line (CX - x, CY - y)-(CX - x, CY + y), , BF
  338.     Next
  339.  
  340. Sub ellipse (CX As Long, CY As Long, xRadius As Long, yRadius As Long)
  341.     Dim scale As Single, xs As Long, x As Long, y As Long
  342.     Dim lastx As Long, lasty As Long
  343.     scale = yRadius / xRadius: xs = xRadius * xRadius
  344.     PSet (CX, CY - yRadius): PSet (CX, CY + yRadius)
  345.     lastx = 0: lasty = yRadius
  346.     For x = 1 To xRadius
  347.         y = scale * Sqr(xs - x * x)
  348.         Line (CX + lastx, CY - lasty)-(CX + x, CY - y)
  349.         Line (CX + lastx, CY + lasty)-(CX + x, CY + y)
  350.         Line (CX - lastx, CY - lasty)-(CX - x, CY - y)
  351.         Line (CX - lastx, CY + lasty)-(CX - x, CY + y)
  352.         lastx = x: lasty = y
  353.     Next
  354.  
  355.  

This part:
Code: QB64: [Select]
  1. Const skyC = &HFF848888 ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< this  used to work !
  2. 'Dim Shared As _Unsigned Long skyC ' >>>>>>>>>>>>>>>>>>>>> fix for 2.0
  3. 'skyC = &HFF848888
  4.  
« Last Edit: March 03, 2022, 12:39:17 pm by bplus »

FellippeHeitor

  • Guest
Re: &H colors dont work as CONST
« Reply #3 on: March 03, 2022, 11:59:43 am »
Might wanna force type, or else it'll be unsigned:

Code: QB64: [Select]
  1. Const skyC˜& = &HFF848888

FellippeHeitor

  • Guest
Re: &H colors dont work as CONST
« Reply #4 on: March 03, 2022, 12:00:40 pm »
a& = _rgb32(255) results in -1 <-- that'll fail when compared with POINT()
a˜& = _rgb32(255) results in  4294967295 <-- that'll work when compared with POINT()

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: &H colors dont work as CONST
« Reply #5 on: March 03, 2022, 12:06:08 pm »
Just saying this use to work and Const's didn't have to be typed.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: &H colors dont work as CONST
« Reply #6 on: March 03, 2022, 12:07:35 pm »
Nope
Code: QB64: [Select]
  1. Const skyC~& = &HFF848888 ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< this  used to work !          
  2.  

Doesn't do it either.

FellippeHeitor

  • Guest
Re: &H colors dont work as CONST
« Reply #7 on: March 03, 2022, 12:08:01 pm »
It's an old discussion that pops up eventually (Steve has many posts explaining the issue). One must be aware that POINT() returns unsigned values. CONST will store the required value in whatever it sees fit, which is usually unsigned by default.

Store your POINT() returns in a signed variable first and it'll match.

FellippeHeitor

  • Guest
Re: &H colors dont work as CONST
« Reply #8 on: March 03, 2022, 12:09:06 pm »
Nope
Code: QB64: [Select]
  1. Const skyC~& = &HFF848888 ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< this  used to work !          
  2.  

Doesn't do it either.

What does this return?

Code: QB64: [Select]
  1.  PRINT skyC˜&, &HFF848888

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
Re: &H colors dont work as CONST
« Reply #9 on: March 03, 2022, 12:11:51 pm »
I'd give it a try to make the value a specific type, not the const name, as CONSTs typically get its type from the given value, so does it work this way?
Code: QB64: [Select]
  1.     Const skyC = &HFF848888~& ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< this  used to work !          
  2.      
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: &H colors dont work as CONST
« Reply #10 on: March 03, 2022, 12:16:15 pm »
Here's what I get when I also compare to POINT

  [ You are not allowed to view this attachment ]  

FellippeHeitor

  • Guest
Re: &H colors dont work as CONST
« Reply #11 on: March 03, 2022, 12:17:48 pm »
Here's what I get when I also compare to POINT

  [ You are not allowed to view this attachment ]  

In that scenario skyC˜& has to be equal to Point(10, 10) - does the comparison fail?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: &H colors dont work as CONST
« Reply #12 on: March 03, 2022, 12:22:32 pm »
Yes Const SkyC is failing to match to POINT(x,y) either as SkyC or SkyC~&

Wait Rho's works! This
Const skyC = &HFF848888~&

But not this:
Const skyC~& = &HFF848888

So I will have to add ~& to &H color numbers in future and fix past.
« Last Edit: March 03, 2022, 12:29:18 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: &H colors dont work as CONST
« Reply #13 on: March 03, 2022, 12:46:57 pm »
It is odd that Rho's Const assignment and test prints that same exact numbers but works in program!
  [ You are not allowed to view this attachment ]  


The print line matches Fellippes but Fellippes Const SkyC~& does not work in program and Rho's Const SkyC = &Hnumber~&

Wait do I have to compare to SkyC~& at the crucial POINT check! Ah that might be it! (And all other, 10 places!)

Well I changed all 10 places where SkyC was used to SkyC~& and still errors but error is different now.
Rho's solution better, only have to change the &H color number once and works.
« Last Edit: March 03, 2022, 01:03:18 pm by bplus »

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
Re: &H colors dont work as CONST
« Reply #14 on: March 03, 2022, 01:12:36 pm »
That is because the explicit type suffix is ignored on the const name:

CONST skyC~& = whatever can be recalled using PRINT skyC~& as well as PRINT skyC, both will print the same.

Only the contents/value designates the type here, think of it like for functions behavior, you define eg. FUNC UglyFunc&(whatever%) to declare a LONG result, but in the program you can call it either as UglyFunc& or without suffix UglyFunc, both will work, because the function internally "knows" how it was defined. Same is true for CONSTs.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack