Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - Ashish

Pages: [1] 2 3 4
1
Programs / JSON Reader
« on: August 03, 2021, 07:00:49 am »
Hello everyone!
I have coded a JSON reader. It can read a JSON string and it supports nested list, nested JSON objs and the mix of both
JSON obj and list.

Here is the code which contain JSON reader function -
Code: QB64: [Select]
  1. Dim test$, l$
  2.  
  3. Open "test.json" For Binary As #1
  4. test$ = Space$(LOF(1))
  5. Get #1, , test$
  6.  
  7. 'label$ is case-sensitive
  8.     Input "label : ", l$ 'try .server-name or .roles.special.devs or roles.common.regulars.[1]
  9.     Print json$(test$, l$)
  10.  
  11. 'JSON Reader in QB64
  12. 'By AshishKingdom
  13. Function json$ (json_data_0$, l$)
  14.     If Len(json_data_0$) = 0 Or Len(l$) = 0 Then Exit Function
  15.     If _Trim$(l$) = "." Then json$ = json_data_0$: Exit Function
  16.  
  17.     Dim As Long startPos, endPos, label_pos1, label_pos2, num_of_br
  18.     Dim As Long f0, f1, f2, f3, f4, f5, f6, list_obj_pos
  19.     Dim As _Byte value_type, in_quote '1=object, 2=array, 0=string or number
  20.     Dim As String sep_type, curr_label, ca, label, json_data, tmp_str
  21.     Dim As Long list_pos1, list_pos2, list_index
  22.  
  23.     label$ = l$
  24.     json_data$ = json_data_0$
  25.  
  26.     json_list_sec:
  27.  
  28.     label_pos1 = InStr(label$, ".")
  29.     If label_pos1 = 0 Then json$ = json_data$: Exit Function
  30.     label_pos2 = InStr(label_pos1 + 1, label$, ".")
  31.  
  32.  
  33.     list_pos1 = InStr(label$, "[")
  34.     If list_pos1 Then
  35.         If label_pos2 = 0 Then
  36.             list_pos2 = InStr(list_pos1 + 1, label$, "]")
  37.  
  38.             If list_pos1 < label_pos1 Then Print "json_parser_error: '.' inside [] ": Exit Function
  39.             If list_pos2 = 0 Then Print "json_parser_error: missing ] in label": Exit Function
  40.  
  41.             list_index = Val(Mid$(label$, list_pos1 + 1, list_pos2 - list_pos1 - 1))
  42.             tmp_str = _Trim$(json_list(json_data$, list_index))
  43.             json_data$ = tmp_str
  44.             label$ = Right$(label$, Len(label$) - list_pos2)
  45.             GoTo json_list_sec 'for nested list (if present), it will keep on parsing it
  46.         Else
  47.             If list_pos1 > label_pos1 And list_pos1 < label_pos2 Then
  48.                 'list index present
  49.                 list_pos2 = InStr(list_pos1 + 1, label$, "]")
  50.                 If list_pos2 = 0 Then Print "json_parser_error: missing ] in label": Exit Function
  51.                 If list_pos2 > label_pos2 Then Print "json_parser_error: '.' inside [] ": Exit Function
  52.                 list_index = Val(Mid$(label$, list_pos1 + 1, list_pos2 - list_pos1 - 1))
  53.                 tmp_str = _Trim$(json_list(json_data$, list_index))
  54.                 json_data$ = tmp_str
  55.                 label$ = Right$(label$, Len(label$) - list_pos2)
  56.                 GoTo json_list_sec 'for nested list (if present), it will keep on parsing it
  57.             End If
  58.         End If
  59.     End If
  60.  
  61.     startPos = getAbsCharPos(json_data$, "{", 1)
  62.     If startPos = 0 Then json$ = json_data$: Exit Function 'its not a json object then
  63.     list_obj_pos = getAbsCharPos(json_data$, "[", 1)
  64.     If startPos > list_obj_pos And list_obj_pos <> 0 Then Exit Function 'its probably a list
  65.     endPos = charFind(json_data$, "}", 1, 1, 1)
  66.     f0 = startPos + 1
  67.     While 1
  68.  
  69.         f2 = getAbsCharPos(json_data$, ":", f0)
  70.  
  71.         If f2 = 0 Then Exit Function
  72.         f1 = getAbsCharPos(json_data$, ",", f2 + 1)
  73.  
  74.         f5 = getAbsCharPos(json_data$, "[", f0)
  75.         f6 = getAbsCharPos(json_data$, "{", f0)
  76.         sep_type = ""
  77.         If f5 = 0 And f6 = 0 Then
  78.             f3 = 0
  79.         ElseIf f5 = 0 And f6 > 0 Then
  80.             f3 = f6
  81.             sep_type = "}"
  82.         ElseIf f6 = 0 And f5 > 0 Then
  83.             f3 = f5
  84.             sep_type = "]"
  85.         ElseIf f5 < f6 And f5 <> 0 Then
  86.             f3 = f5
  87.             sep_type = "]"
  88.         ElseIf f6 < f5 And f6 <> 0 Then
  89.             f3 = f6
  90.             sep_type = "}"
  91.         End If
  92.  
  93.         If f3 = 0 Then
  94.             value_type = 0
  95.         Else
  96.             'check if ',' was before [ or {
  97.  
  98.             If f1 < f3 And f1 <> 0 Then
  99.                 ' comma present before [ or {
  100.                 value_type = 0
  101.             Else
  102.                 'comma present after.. so it may be inside {}/[] or outside of it.
  103.                 'finding end of position of json_obj/list present
  104.                 num_of_br = 1
  105.                 f4 = f3
  106.                 in_quote = 1
  107.                 While num_of_br > 0
  108.                     f4 = f4 + 1
  109.                     If f4 > endPos Then Print "json_parser_error: missing "; sep_type: Exit Function
  110.  
  111.                     ca = Mid$(json_data$, f4, 1)
  112.                     If ca = Chr$(34) Then in_quote = in_quote * -1
  113.                     If in_quote = 1 Then
  114.                         If ca = "{" Or ca = "[" Then num_of_br = num_of_br + 1
  115.                         If ca = "}" Or ca = "]" Then num_of_br = num_of_br - 1
  116.                     End If
  117.                 Wend
  118.                 If sep_type = "}" Then value_type = 1 Else value_type = 2
  119.             End If
  120.         End If
  121.  
  122.  
  123.         If f2 > f1 And f1 <> 0 Then
  124.             Print "json_parser_error: ',' before ':'"
  125.             Exit Function
  126.         End If
  127.         'Print value_type, f3, f4
  128.         'Sleep
  129.         If value_type = 0 Then
  130.  
  131.             If getData(Mid$(json_data$, f0, f2 - 1 - f0)) = Right$(label$, Len(label$) - 1) Then
  132.                 If f1 = 0 Then
  133.                     json$ = getData(Mid$(json_data$, f2 + 1, endPos - f2 - 1))
  134.                 Else
  135.                     json$ = getData(Mid$(json_data$, f2 + 1, f1 - f2 - 1))
  136.                 End If
  137.                 Exit Function
  138.             Else
  139.                 If f1 = 0 Then Exit Function Else f0 = f1 + 1
  140.             End If
  141.         Else
  142.             If label_pos2 = 0 Then
  143.                 curr_label = Right$(label$, Len(label$) - 1)
  144.             Else
  145.                 curr_label = Mid$(label$, label_pos1 + 1, label_pos2 - label_pos1 - 1)
  146.             End If
  147.             'Print curr_label, getData(Mid$(json_data$, f0, f2 - 1 - f0))
  148.             'Sleep
  149.             If getData(Mid$(json_data$, f0, f2 - 1 - f0)) = curr_label Then
  150.                 If label_pos2 = 0 Then
  151.                     json$ = Mid$(json_data$, f3, f4 - f3 + 1)
  152.                     Exit Function
  153.                 Else
  154.                     'Print Mid$(json_data$, f3, f4 - f3 + 1)
  155.                     'Print Right$(label$, Len(label$) - label_pos2 + 1)
  156.                     json$ = json$(Mid$(json_data$, f3, f4 - f3 + 1), Right$(label$, Len(label$) - label_pos2 + 1))
  157.                     Exit Function
  158.                 End If
  159.             Else
  160.                 f1 = getAbsCharPos(json_data$, ",", f4 + 1)
  161.                 If f1 = 0 Then f0 = f4 + 1 Else f0 = f1 + 1
  162.                 'Print "hha ", f1, f0
  163.             End If
  164.         End If
  165.     Wend
  166.  
  167. Function json_list$ (list_data$, index As _Unsigned Long)
  168.     If list_data$ = "" Then Exit Function
  169.  
  170.     Dim As Long startPos, endPos, curr_index, json_obj_pos, value_type
  171.     Dim As Long f0, f1, f2, f3, f4, f5, f6, i
  172.     Dim As Long num_of_br
  173.     Dim As String ca, sep_type
  174.     Dim As _Byte element_present, in_quote
  175.  
  176.     startPos = InStr(list_data$, "[")
  177.     endPos = charFind(list_data$, "]", 0, 1, 1)
  178.     If startPos = 0 Then json_list$ = list_data$: Exit Function 'its not a list
  179.     json_obj_pos = getAbsCharPos(list_data$, "{", 1)
  180.     If startPos > json_obj_pos And json_obj_pos <> 0 Then Exit Function 'probably its a json object
  181.  
  182.     f0 = startPos + 1
  183.     curr_index = 0
  184.     While 1
  185.         f1 = getAbsCharPos(list_data$, ",", f0)
  186.         '  f2 = getAbsCharPos(list_data$, "[", f0)
  187.         f5 = getAbsCharPos(list_data$, "[", f0)
  188.         f6 = getAbsCharPos(list_data$, "{", f0)
  189.         sep_type = ""
  190.         If f5 = 0 And f6 = 0 Then
  191.             f3 = 0
  192.         ElseIf f5 = 0 And f6 > 0 Then
  193.             f3 = f6
  194.             sep_type = "}"
  195.         ElseIf f6 = 0 And f5 > 0 Then
  196.             f3 = f5
  197.             sep_type = "]"
  198.         ElseIf f5 < f6 And f5 <> 0 Then
  199.             f3 = f5
  200.             sep_type = "]"
  201.         ElseIf f6 < f5 And f6 <> 0 Then
  202.             f3 = f6
  203.             sep_type = "}"
  204.         End If
  205.  
  206.         If f3 = 0 Then
  207.             value_type = 0
  208.         Else
  209.             'check if ',' was before [ or {
  210.  
  211.             If f1 < f3 And f1 <> 0 Then
  212.                 ' comma present before [ or {
  213.                 value_type = 0
  214.             Else
  215.                 'comma present after.. so it may be inside {}/[] or outside of it.
  216.                 'finding end of position of json_obj/list present
  217.                 num_of_br = 1
  218.                 f4 = f3
  219.                 in_quote = 1
  220.                 While num_of_br > 0
  221.                     f4 = f4 + 1
  222.                     If f4 > endPos Then Print "json_parser_error: missing "; sep_type: Exit Function
  223.  
  224.                     ca = Mid$(list_data$, f4, 1)
  225.                     If ca = Chr$(34) Then in_quote = in_quote * -1
  226.                     If in_quote = 1 Then
  227.                         If ca = "{" Or ca = "[" Then num_of_br = num_of_br + 1
  228.                         If ca = "}" Or ca = "]" Then num_of_br = num_of_br - 1
  229.                     End If
  230.                 Wend
  231.                 f1 = f4 + 1
  232.                 If sep_type = "}" Then value_type = 1 Else value_type = 2
  233.             End If
  234.         End If
  235.         'Print f0, f1, ca, curr_index, index
  236.  
  237.         If f1 = 0 Then
  238.             'check if there's any value here
  239.             If curr_index = index Then
  240.                 element_present = 0
  241.  
  242.                 For i = f0 To endPos - 1
  243.                     If Asc(Mid$(list_data$, i, 1)) <> 32 Then element_present = 1
  244.                 Next
  245.                 If element_present = 1 Then
  246.                     If value_type = 0 Then
  247.                         json_list$ = getData(Mid$(list_data$, f0, endPos - f0))
  248.                     Else
  249.                         json_list$ = Mid$(list_data$, f0, endPos - f0 + 1)
  250.                     End If
  251.                 End If
  252.             End If
  253.             Exit Function
  254.         Else
  255.             If curr_index = index Then
  256.                 If value_type = 0 Then
  257.                     json_list$ = getData(Mid$(list_data$, f0, f1 - f0))
  258.                 Else
  259.                     json_list$ = Mid$(list_data$, f0, f1 - f0)
  260.                 End If
  261.                 Exit Function
  262.             Else
  263.                 f0 = f1 + 1
  264.             End If
  265.         End If
  266.         curr_index = curr_index + 1
  267.     Wend
  268.  
  269. Function getAbsCharPos~& (main_str As String, char As String, s As _Unsigned Long) 'give position of char which is outside of quotes
  270.     Dim As _Unsigned Long i, qp
  271.     Dim ca$, q$
  272.  
  273.     qp = 1
  274.     For i = s To Len(main_str)
  275.         ca$ = Mid$(main_str, i, 1)
  276.         If ca$ = Chr$(34) Then q$ = ca$: qp = qp * -1
  277.         If ca$ = char And qp = 1 Then
  278.             getAbsCharPos~& = i
  279.             Exit Function
  280.         End If
  281.     Next
  282. Function getData$ (main_str As String) 'quote_type=1("), 2=(')
  283.     Dim quote$, i As _Unsigned Long, j As _Unsigned Long
  284.     Dim c$
  285.     If InStr(main_str, Chr$(34)) = 0 Then
  286.         getData$ = _Trim$(main_str)
  287.         'Print "yes"
  288.         'Print main_str
  289.         Exit Function
  290.     End If
  291.     For i = 1 To Len(main_str)
  292.         c$ = Mid$(main_str, i, 1)
  293.         If c$ = Chr$(34) Then
  294.             quote$ = c$
  295.             For j = i + 1 To Len(main_str)
  296.                 c$ = Mid$(main_str, j, 1)
  297.                 If c$ = quote$ Then Exit Function
  298.                 getData$ = getData$ + c$
  299.             Next
  300.         End If
  301.     Next
  302. Function charFind~& (main_str As String, search As String, p As _Unsigned Long, fromEnd As _Byte, ignore_quote As _Byte)
  303.     If Len(main_str) = 0 Or Len(search) = 0 Then Exit Function
  304.     If fromEnd Then
  305.         Dim reversed$
  306.         For i = Len(main_str) To 1 Step -1
  307.             reversed$ = reversed$ + Mid$(main_str, i, 1)
  308.         Next
  309.         'Print reversed$
  310.         If ignore_quote Then charFind~& = -getAbsCharPos(reversed$, search, p) + 1 + Len(main_str) Else charFind~& = -InStr(p, reversed$, search) + 1 + Len(main_str)
  311.     Else
  312.         If ignore_quote Then charFind~& = getAbsCharPos(main_str, search, p) Else charFind~& = InStr(p, main_str, search)
  313.     End If
  314.  

Here is the test.json file which you need. Save the below JSON string into a file named test.json
Code: Text: [Select]
  1. {
  2.     "server-name" : "QB64",
  3.     "description" : "Server for qb64, qbasic and gw-basic fans.",
  4.     "roles" : {
  5.         "special": {
  6.             "devs" : ["Fellippe", "Luke", "Spriggsy", "SteveMcNeill"],
  7.             "Crew-Members" : ["Ashish", "Cobalt", "Qwerky", "STxAxTIC"]
  8.         },
  9.         "common" : {
  10.             "regulars" : ["Shalin", "Monke", "ChiaPet", "onecoding", "garbage", "loudar"]
  11.         }
  12.     },
  13.     "total-members" : 296
  14. }
  15.  

Use "." followed a by label to get value. Use "[ index ]" for a list to get a value.

2
Programs / 3D World Game Engine
« on: May 29, 2021, 08:13:15 am »
Hey everyone! I started working on this first person engine 1 year ago... It can load 3d maps with objects to which you can interact..
I will love to see your feedbacks!

Instructions:
  • Move mouse to look around
  • Use W,S,A,D to move forward, backward, left and right respectively
  • Press SPACEBAR to pickup keys and to unlock the doors.
  • All game engine related debugging information is displayed in the console window... you can check for the cause of possible errors in the console window.
  • You will need world_data.cdf file in the same folder where executable is generated in order to work it properly. Download it from the attachment below

Here's the code -

Code: QB64: [Select]
  1. '3D World Game Engine v1.0
  2. 'by Ashish
  3.  
  4. $Let DEBUG = -1
  5.  
  6. Declare Library 'camera control function
  7.     Sub gluLookAt (ByVal eyeX#, Byval eyeY#, Byval eyeZ#, Byval centerX#, Byval centerY#, Byval centerZ#, Byval upX#, Byval upY#, Byval upZ#)
  8.  
  9. Type vec3
  10.     x As Single
  11.     y As Single
  12.     z As Single
  13.  
  14. Type map_element
  15.     x As Single
  16.     y As Single
  17.     typ As Integer
  18.     'below element can be use to store data for any application. (additional data)
  19.     d_1 As Single
  20.     d_2 As Single
  21.     d_3 As Single
  22.     d_4 As Single
  23. Type map_file_header
  24.     signature As String * 8 '3DM@QB64
  25.     'objects
  26.     num_of_wall As _Unsigned Integer
  27.     num_of_floor As _Unsigned Integer
  28.     num_of_ceiling As _Unsigned Integer
  29.     num_of_door As _Unsigned Integer
  30.     num_of_key As _Unsigned Integer
  31.     num_of_point As _Unsigned Integer
  32.     num_of_detail As _Unsigned Integer
  33.     num_of_trap As _Unsigned Integer
  34.     'dimension
  35.     mapW As _Unsigned Integer
  36.     mapH As _Unsigned Integer
  37.     'player starting position
  38.     px As Single
  39.     py As Single
  40.     'player destination position
  41.     destX As Single
  42.     destY As Single
  43.     'global directional light settings
  44.     light_amb As vec3 'ambient color
  45.     light_diff As vec3 'diffuse color
  46.     light_spec As vec3 'specular color
  47.     light_dir As vec3 'direction of light
  48.  
  49. Type game_texture_type
  50.     img_handle As Long
  51.     gl_handle As Long
  52. 'For doors :-
  53. '* d_2 specify the state of the door. 0=closed, 1=opening, 2=opened
  54. '* d_1 specify which side of it is fixed. So, the door rotates from that side when being
  55. ' opened by the user. ±1=left ±2=right ±3=up ±4=down where sign (±) describe the direction of rotation when being open
  56. '* d_3 specify the ID of the key from which it can be unlock and open
  57. '* d_4 specify the angle of rotation when it is being open.
  58. 'For keys :-
  59. '* d_1 specify its ID. Each key have a unique ID.
  60. '* d_2 specify whether it has taken by the user or not. If taken, disable rendering of it.
  61. 'For traps :-
  62. '* Type 1 [laser]
  63. '  * d_4 is used as clock variable in timer
  64. '  * d_2 is frequecncy of on/off of laser for every 4 seconds.
  65.  
  66.  
  67. 'Global Variables
  68. Dim Shared worldStats As map_file_header
  69. ReDim Shared wallMap(0) As map_element, floorMap(0) As map_element, ceilingMap(0) As map_element
  70. ReDim Shared doorMap(0) As map_element, keyMap(0) As map_element, pointMap(0) As map_element
  71. ReDim Shared detailMap(0) As map_element, trapMap(0) As map_element
  72.  
  73. Dim Shared worldTextures(7, 15) As game_texture_type '8 objects, each coming in maximum of 16 varities
  74.  
  75. Dim Shared game_state, worldRotX, worldRotY, playerPos As vec3, playerFarPoint
  76. ReDim Shared collisionMap(0, 0, 1) As _Byte '2 layers, one for collision and second one for interaction with objects
  77.  
  78.  
  79. 'Local Variables
  80. ' dim preMouseX
  81. Dim keyPressed As Long
  82.  
  83. _Title "3D World Game Engine v1.0"
  84. Screen _NewImage(800, 600, 32)
  85. $If DEBUG = -1 Then
  86.     $Console
  87. echo "3D World Game Engine v1.0"
  88. echo "Preparing..."
  89.  
  90. 'LOAD stuffs
  91. loadResource
  92. ' for i = 0 to 15
  93. ' _putimage (0,0),worldTextures(0,i).img_handle
  94. ' sleep
  95. ' next
  96. ' worldTextures(0, 0).img_handle = _LOADIMAGE("Textures/wall/wall_1.jpg")
  97. ' worldTextures(1, 0).img_handle = _LOADIMAGE("Textures/floor/floor_1.jpg")
  98. ' worldTextures(2, 0).img_handle = _LOADIMAGE("Textures/ceiling/ceil_2.jpg")
  99. ' worldTextures(3, 0).img_handle = _LOADIMAGE("Textures/door/door_1.jpg")
  100.  
  101. ' loadWorld "among_us_world.3dm"
  102. playerPos.x = worldStats.px
  103. playerPos.y = 0
  104. playerPos.z = worldStats.py
  105. playerFarPoint = 0.5
  106.  
  107. game_state = 1
  108.     Wend
  109.    
  110.     keyPressed = _KeyHit
  111.    
  112.     worldRotX = map(_MouseY, 0, 600, 60, -60)
  113.    
  114.     ' if _MOUSEX >= _WIDTH - 1 then _mousemove 0,_mousey : preMouseX = 0
  115.    
  116.     worldRotY = worldRotY + (_MOUSEX - preMouseX)
  117.     ' worldRotY = _D2R(_MouseX)
  118.    
  119.     check_key_input:
  120.     __x1 = near_int(playerPos.x + 0.5 * Sin(_D2R(worldRotY)))
  121.     __y1 = near_int(playerPos.z - 0.5 * Cos(_D2R(worldRotY)))
  122.     ' If collisionMap(__x1, __y1, 1) = 1 Then echo "Ouch!!"
  123.     If _KeyDown(Asc("w")) Or _KeyDown(Asc("W")) Then 'forward
  124.         If collisionMap(__x1, __y1, 0) = 0 Then
  125.             playerPos.x = playerPos.x + 0.08 * Sin(_D2R(worldRotY))
  126.             playerPos.z = playerPos.z - 0.08 * Cos(_D2R(worldRotY))
  127.         End If
  128.     End If
  129.     __x1 = near_int(playerPos.x - 0.5 * Sin(_D2R(worldRotY)))
  130.     __y1 = near_int(playerPos.z + 0.5 * Cos(_D2R(worldRotY)))
  131.     ' If collisionMap(__x1, __y1, 1) = 1 Then echo "Ouch!!"
  132.     If _KeyDown(Asc("s")) Or _KeyDown(Asc("S")) Then 'backward, just doing opposite of forward
  133.         If collisionMap(__x1, __y1, 0) = 0 Then
  134.             playerPos.x = playerPos.x - 0.08 * Sin(_D2R(worldRotY))
  135.             playerPos.z = playerPos.z + 0.08 * Cos(_D2R(worldRotY))
  136.         End If
  137.     End If
  138.     __x1 = near_int(playerPos.x - 0.5 * Sin(_D2R(worldRotY + 90)))
  139.     __y1 = near_int(playerPos.z + 0.5 * Cos(_D2R(worldRotY + 90)))
  140.     ' If collisionMap(__x1, __y1, 1) = 1 Then echo "Ouch!!"
  141.     If _KeyDown(Asc("a")) Or _KeyDown(Asc("A")) Then 'leftward
  142.         If collisionMap(__x1, __y1, 0) = 0 Then
  143.             playerPos.x = playerPos.x - 0.08 * Sin(_D2R(worldRotY + 90))
  144.             playerPos.z = playerPos.z + 0.08 * Cos(_D2R(worldRotY + 90))
  145.         End If
  146.     End If
  147.     __x1 = near_int(playerPos.x + 0.5 * Sin(_D2R(worldRotY + 90)))
  148.     __y1 = near_int(playerPos.z - 0.5 * Cos(_D2R(worldRotY + 90)))
  149.     ' If colslisionMap(__x1, __y1, 1) = 1 Then echo "Ouch!!" <--- temporarily commented
  150.     If _KeyDown(Asc("d")) Or _KeyDown(Asc("D")) Then 'rightward, opposite of leftward
  151.         If collisionMap(__x1, __y1, 0) = 0 Then
  152.             playerPos.x = playerPos.x + 0.08 * Sin(_D2R(worldRotY + 90))
  153.             playerPos.z = playerPos.z - 0.08 * Cos(_D2R(worldRotY + 90))
  154.         End If
  155.     End If
  156.     'animation stuffs (khaaskar traps ke liye)
  157.     If worldStats.num_of_trap > 0 Then
  158.         For i = 1 To worldStats.num_of_trap
  159.             If trapMap(i).typ = 1 Then 'laser
  160.                 If (Timer - trapMap(i).d_4) > (4 / trapMap(i).d_2) Then
  161.                     If trapMap(i).d_3 = 1 Then
  162.                         trapMap(i).d_3 = 0
  163.                         collisionMap(trapMap(i).x, trapMap(i).y, 1) = 0 'no damage
  164.                     Else
  165.                         trapMap(i).d_3 = 1
  166.                         collisionMap(trapMap(i).x, trapMap(i).y, 1) = 1 'damage
  167.                     End If
  168.                     trapMap(i).d_4 = Timer
  169.                 End If
  170.             End If
  171.         Next
  172.     End If
  173.     'interaction with objects
  174.     If keyPressed = Asc(" ") Then
  175.         __x1 = near_int(playerPos.x): __y1 = near_int(playerPos.z)
  176.         __x2 = near_int(playerPos.x + 0.5 * Sin(_D2R(worldRotY))): __y2 = near_int(playerPos.z - 0.5 * Cos(_D2R(worldRotY)))
  177.         'interaction with key
  178.         If collisionMap(__x1, __y1, 1) = 3 Then
  179.             'find by which key user have interacted?
  180.             For i = 1 To worldStats.num_of_key
  181.                 If keyMap(i).d_2 = 0 Then
  182.                     If keyMap(i).x = __x1 And keyMap(i).y = __y1 Then
  183.                         keyMap(i).d_2 = 1 'disable rendering of the key as it is now taken by the user.
  184.                         echo "KeyMap(" + _Trim$(Str$(i)) + ").d_2 = 1"
  185.                         Exit For
  186.                     End If
  187.                 End If
  188.             Next
  189.         End If
  190.         'interaction with door
  191.         If collisionMap(__x2, __y2, 1) = 2 Then
  192.             'finding by which door user have interacted
  193.             doorFound = 0
  194.             For i = 1 To worldStats.num_of_door
  195.                 If doorMap(i).d_2 = 0 Then
  196.                     If doorMap(i).x = __x2 And doorMap(i).y = __y2 Then
  197.                         doorIndex = i
  198.                         doorFound = 1
  199.                         echo "door is found at index : " + Str$(doorIndex)
  200.                     End If
  201.                 End If
  202.             Next
  203.             'checking if user has the key for this door
  204.             If doorFound = 1 Then
  205.                 For i = 1 To worldStats.num_of_key
  206.                     If doorMap(doorIndex).d_3 = keyMap(i).d_1 Then
  207.                         If keyMap(i).d_2 = 1 Then
  208.                             'okay, so the user do have the key for this door and the door is not yet opened.
  209.                             'now, I change the state of this door from "closed" to "opening"
  210.                             doorMap(doorIndex).d_2 = 1
  211.                             echo "state of door is now changed."
  212.                             Exit For
  213.                         else
  214.                             echo "You don't have key for this door... Try it find it somewhere"
  215.                         End If
  216.                     End If
  217.                 Next
  218.             End If
  219.         End If
  220.     End If
  221.    
  222.    
  223.     _Limit 30
  224.    
  225.     preMouseX = _MouseX
  226. ' for i = 1 to worldStats.num_of_wall
  227. ' pset (wallMap(i).x,wallMap(i).y)
  228. ' next
  229. ' for i = 1 to worldStats.num_of_floor
  230. ' pset (floorMap(i).x, floorMap(i).y), _RGB(255,0,0)
  231. ' next
  232.  
  233. Sub _GL ()
  234.     Static tex_load_type
  235.     If game_state = 0 Then Exit Sub
  236.     If game_state = 1 Then
  237.         ' If tex_load_type > 4 Then
  238.             ' For i = 0 To UBound(playerTextures)
  239.                 ' playerTextures(i).gl_handle = feedGLTexture(playerTextures(i).img_handle)
  240.                 ' _FreeImage playerTextures(i).img_handle
  241.             ' Next
  242.             ' game_state = game_state + 1
  243.             ' initPlayerObject
  244.             ' Exit Sub
  245.         ' End If
  246.         For i = 0 To 15
  247.             If worldTextures(tex_load_type, i).img_handle < -1 Then
  248.                 worldTextures(tex_load_type, i).gl_handle = feedGLTexture(worldTextures(tex_load_type, i).img_handle)
  249.                 _FreeImage worldTextures(tex_load_type, i).img_handle
  250.             End If
  251.         Next
  252.         tex_load_type = tex_load_type + 1
  253.         if tex_load_type>4 then game_state = game_state + 1
  254.         Exit Sub
  255.     End If
  256.  
  257.     If game_state = 2 Then 'world rendering
  258.         _glViewport 0, 0, _Width, _Height
  259.  
  260.         _glEnable _GL_DEPTH_TEST
  261.         _glEnable _GL_TEXTURE_2D
  262.         _glEnable _GL_LIGHTING
  263.         _glEnable _GL_BLEND
  264.         _glDisable _GL_MULTISAMPLE
  265.        
  266.         'Global Light Settings of World
  267.         _glEnable _GL_LIGHT0
  268.         _glLightfv _GL_LIGHT0, _GL_AMBIENT, glVec4(worldStats.light_amb.x, worldStats.light_amb.y, worldStats.light_amb.z, 1)
  269.         _glLightfv _GL_LIGHT0, _GL_DIFFUSE, glVec4(worldStats.light_diff.x, worldStats.light_diff.y, worldStats.light_diff.z, 1)
  270.         _glLightfv _GL_LIGHT0, _GL_SPECULAR, glVec4(worldStats.light_spec.x, worldStats.light_spec.y, worldStats.light_spec.z, 1)
  271.         _glLightfv _GL_LIGHT0, _GL_POSITION, glVec4(worldStats.light_dir.x, worldStats.light_dir.y, worldStats.light_dir.z, 1)
  272.  
  273.         _glMatrixMode _GL_PROJECTION
  274.         _glLoadIdentity
  275.         _gluPerspective 60, _width/_Height, 0.1, worldStats.mapW
  276.        
  277.         _glMatrixMode _GL_MODELVIEW
  278.         _glLoadIdentity
  279.         '_glTranslatef playerPos.x, 1, playerPos.z
  280.         ' _glRotatef worldRotX, 1, 0, 0
  281.         ' _glRotatef worldRotY+180, 0, 1, 0
  282.        
  283.         gluLookAt playerPos.x , playerPos.y + 0.4, playerPos.z,_
  284. playerPos.x + playerFarPoint * Cos(_D2R(worldRotY-90)), playerPos.y+0.4*sin(_d2r(worldRotX)) , playerPos.z + playerFarPoint * Sin(_D2R(worldRotY-90)),_
  285. 0, 1, 0
  286.  
  287.         drawWalls
  288.         drawDoors
  289.         drawFloors
  290.         drawCeilings
  291.         drawKeys
  292.         drawBonusObj
  293.         drawTraps
  294.  
  295.         _glFlush
  296.     End If
  297.  
  298. Sub loadResource ()
  299.     Dim n, i, j, k, f$
  300.     n = 15
  301.     echo "------------In SUB loadResource()------------------"
  302.     echo "extracting map and textures"
  303.    
  304.     extract_file_all "world_data.cdf", "./"
  305.    
  306.     extract_file_all "wall.cdf", "./"
  307.    
  308.     'wall textures
  309.     For i = 0 To n
  310.         ' worldTextures(0, i).img_handle = _LoadImage("Textures/wall/wall_" + _Trim$(Str$(i + 1)) + ".png")
  311.         f$ = "wall_" + _Trim$(Str$(i + 1)) + ".png"
  312.         worldTextures(0, i).img_handle = _LoadImage(f$)
  313.         kill f$
  314.     Next
  315.     kill "wall.cdf"
  316.     echo "wall... DONE"
  317.    
  318.     'floor textures
  319.     extract_file_all "floor.cdf", "./"
  320.     For i = 0 To n
  321.         f$ = "floor_" + _Trim$(Str$(i + 1)) + ".jpg"
  322.         ' worldTextures(1, i).img_handle = _LoadImage("Textures/floor/floor_" + _Trim$(Str$(i + 1)) + ".jpg")
  323.         worldTextures(1, i).img_handle = _LoadImage(f$)
  324.         kill f$
  325.     Next
  326.     kill "floor.cdf"
  327.     echo "floor... DONE"
  328.     'ceiling textures
  329.     extract_file_all "ceil.cdf","./"
  330.     n = 2
  331.     For i = 0 To n
  332.         f$ = "ceil_" + _Trim$(Str$(i + 1)) + ".jpg"
  333.         ' worldTextures(2, i).img_handle = _LoadImage("Textures/ceiling/ceil_" + _Trim$(Str$(i + 1)) + ".jpg")
  334.         worldTextures(2, i).img_handle = _LoadImage(f$)
  335.         kill f$
  336.     Next
  337.     kill "ceil.cdf"
  338.     echo "ceiling... DONE"
  339.     'door textures
  340.     extract_file_all "door.cdf", "./"
  341.     n = 6
  342.     For i = 0 To n
  343.         f$ = "door_" + _Trim$(Str$(i + 1)) + ".jpg"
  344.         ' worldTextures(3, i).img_handle = _LoadImage("Textures/door/door_" + _Trim$(Str$(i + 1)) + ".jpg")
  345.         worldTextures(3, i).img_handle = _LoadImage(f$)
  346.         kill f$
  347.     Next
  348.     kill "door.cdf"
  349.     echo "door... DONE"
  350.     'key textures
  351.     extract_file_all "key.cdf", "./"
  352.     n = 7
  353.     For i = 0 To n
  354.         For j = 1 To 2
  355.             f$ = "key_" + _Trim$(Str$(i + 1)) + "_" + _Trim$(Str$(j)) + ".png"
  356.             ' worldTextures(4, k).img_handle = _LoadImage("Textures/key/key_" + _Trim$(Str$(i + 1)) + "_" + _Trim$(Str$(j)) + ".png")
  357.             worldTextures(4, k).img_handle = _LoadImage(f$)
  358.             k = k + 1
  359.             kill f$
  360.         Next
  361.         kill "key_"+_Trim$(Str$(i + 1))+".png"
  362.     Next
  363.     kill "key.cdf"
  364.     echo "key... DONE"
  365.     echo ""
  366.     echo "Loading Map"
  367.     loadWorld "test.3dm"
  368.     kill "test.3dm"
  369.    
  370.    
  371.  
  372. Sub drawWalls ()
  373.     For i = 1 To worldStats.num_of_wall
  374.         selectTexture worldTextures(0, wallMap(i).typ - 1).gl_handle
  375.         ' drawBox wallMap(i).x, 0, wallMap(i).y, 1, 2, 1
  376.         drawXYPlane wallMap(i).x, 0, wallMap(i).y - 0.5, 1, 2, -1 'front face
  377.         drawXYPlane wallMap(i).x, 0, wallMap(i).y + 0.5, 1, 2, 1 'back face
  378.         drawYZPlane wallMap(i).x - 0.5, 0, wallMap(i).y, 2, 1, -1 'left face
  379.         drawYZPlane wallMap(i).x + 0.5, 0, wallMap(i).y, 2, 1, 1 'right face
  380.     Next
  381.  
  382. Sub drawFloors ()
  383.     _glMaterialfv _GL_FRONT_AND_BACK, _GL_SHININESS, glVec4(0.5 * 128, 0, 0, 0)
  384.     For i = 1 To worldStats.num_of_floor
  385.         selectTexture worldTextures(1, floorMap(i).typ - 1).gl_handle
  386.         drawXZPlane floorMap(i).x, -1, floorMap(i).y, 1, 1, 1
  387.     Next
  388.  
  389. Sub drawCeilings ()
  390.     For i = 1 To worldStats.num_of_ceiling
  391.         selectTexture worldTextures(2, ceilingMap(i).typ - 1).gl_handle
  392.         drawXZPlane ceilingMap(i).x, 1, ceilingMap(i).y, 1, 1, -1
  393.     Next
  394.  
  395. Sub drawDoors ()
  396.     '±1,±2,±3,,
  397.     For i = 1 To worldStats.num_of_door
  398.         selectTexture worldTextures(3, doorMap(i).typ - 1).gl_handle
  399.         If doorMap(i).d_2 = 0 Then 'closed?
  400.             If Abs(doorMap(i).d_1) = 1 Or Abs(doorMap(i).d_1) = 2 Then
  401.                 'it must be slim in XY depth
  402.                 drawXYPlane doorMap(i).x, 0, doorMap(i).y - 0.1, 1, 2, -1 'front face
  403.                 drawXYPlane doorMap(i).x, 0, doorMap(i).y + 0.1, 1, 2, 1 'back face
  404.                
  405.                 drawYZPlane doorMap(i).x - 0.5, 0, doorMap(i).y, 2, 0.2, -1 'left face
  406.                 drawYZPlane doorMap(i).x + 0.5, 0, doorMap(i).y, 2, 0.2, 1 'right face
  407.             Else
  408.                 drawXYPlane doorMap(i).x, 0, doorMap(i).y - 0.5, 0.2, 2, -1 'front face
  409.                 drawXYPlane doorMap(i).x, 0, doorMap(i).y + 0.5, 0.2, 2, 1 'back face
  410.                
  411.                 'it must be slim in YZ along X
  412.                 drawYZPlane doorMap(i).x - 0.1, 0, doorMap(i).y, 2, 1, -1 'left face
  413.                 drawYZPlane doorMap(i).x + 0.1, 0, doorMap(i).y, 2, 1, 1 'right face
  414.             End If
  415.         ElseIf doorMap(i).d_2 = 1 Then 'opening
  416.             If doorMap(i).d_1 > 0 Then s = 1 Else s = -1
  417.             If Abs(doorMap(i).d_1) = 1 Then 'left
  418.                 _glPushMatrix
  419.                 _glTranslatef doorMap(i).x - 0.5, 0, doorMap(i).y
  420.                 _glRotatef doorMap(i).d_4, 0, 1, 0
  421.  
  422.                 If doorMap(i).d_4 = 90 * s Then collisionMap(doorMap(i).x, doorMap(i).y, 0) = 0 Else doorMap(i).d_4 = doorMap(i).d_4 + 2 * s
  423.                 drawXYPlane 0.5, 0, -0.1, 1, 2, -1 'front face
  424.                 drawXYPlane 0.5, 0, 0.1, 1, 2, 1 'back face
  425.                
  426.                 drawYZPlane 0, 0, 0, 2, 0.2, -1 'left face
  427.                 drawYZPlane 1, 0, 0, 2, 0.2, 1 'right face
  428.                 _glPopMatrix
  429.             ElseIf Abs(doorMap(i).d_1) = 2 Then 'right
  430.                 _glPushMatrix
  431.                 _glTranslatef doorMap(i).x + 0.5, 0, doorMap(i).y
  432.                 _glRotatef doorMap(i).d_4, 0, 1, 0
  433.                
  434.                 If doorMap(i).d_4 = 90 * s Then collisionMap(doorMap(i).x, doorMap(i).y, 0) = 0 Else doorMap(i).d_4 = doorMap(i).d_4 + 2 * s
  435.                 drawXYPlane -0.5, 0, -0.1, 1, 2, -1 'front face
  436.                 drawXYPlane -0.5, 0, 0.1, 1, 2, 1 'back face
  437.                
  438.                 drawYZPlane -1, 0, 0, 2, 0.2, -1 'left face
  439.                 drawYZPlane 0, 0, 0, 2, 0.2, 1 'right face
  440.                 _glPopMatrix
  441.             ElseIf Abs(doorMap(i).d_1) = 3 Then 'up
  442.                 _glPushMatrix
  443.                 _glTranslatef doorMap(i).x, 0, doorMap(i).y - 0.5
  444.                 _glRotatef doorMap(i).d_4, 0, 1, 0
  445.                 If doorMap(i).d_4 = 90 * s Then collisionMap(doorMap(i).x, doorMap(i).y, 0) = 0 Else doorMap(i).d_4 = doorMap(i).d_4 + 2 * s
  446.                 drawXYPlane 0, 0, 0, 0.2, 2, -1 'front face
  447.                 drawXYPlane 0, 0, 1, 0.2, 2, 1 'back face
  448.                
  449.                 drawYZPlane -0.1, 0, 0.5, 2, 1, -1 'left face
  450.                 drawYZPlane 0.1, 0, 0.5, 2, 1, 1 'right face
  451.                 _glPopMatrix
  452.             Else 'down
  453.                 _glPushMatrix
  454.                 _glTranslatef doorMap(i).x, 0, doorMap(i).y + 0.5
  455.                 _glRotatef doorMap(i).d_4, 0, 1, 0
  456.  
  457.                 If doorMap(i).d_4 = 90 * s Then collisionMap(doorMap(i).x, doorMap(i).y, 0) = 0 Else doorMap(i).d_4 = doorMap(i).d_4 + 2 * s
  458.                 drawXYPlane 0, 0, -1, 0.2, 2, -1 'front face
  459.                 drawXYPlane 0, 0, 0, 0.2, 2, 1 'back face
  460.                
  461.                 drawYZPlane -0.1, 0, -0.5, 2, 1, -1 'left face
  462.                 drawYZPlane 0.1, 0, -0.5, 2, 1, 1 'right face
  463.                 _glPopMatrix
  464.             End If
  465.         End If
  466.     Next
  467.  
  468.  
  469. Sub drawKeys ()
  470.     Static keyRotY
  471.     keyRotY = keyRotY + 1
  472.     _glDisable _GL_LIGHTING
  473.     For i = 1 To worldStats.num_of_key
  474.         If keyMap(i).d_2 = 0 Then 'd_2 store whether it has been taken by the user or not.
  475.             _glPushMatrix
  476.             _glTranslatef keyMap(i).x, -.98, keyMap(i).y
  477.             _glRotatef keyRotY, 0, 1, 0
  478.             selectTexture worldTextures(4, keyMap(i).typ - 1).gl_handle
  479.             drawXZPlane 0, 0, 0, .25, .15, 1
  480.             _glPopMatrix
  481.         End If
  482.     Next
  483.     _glEnable _GL_LIGHTING
  484.  
  485.  
  486. Sub drawBonusObj ()
  487.     Static calc_done As _Byte, sin120, cos120, sin240, cos240, rotY
  488.     If calc_done = 0 Then
  489.         calc_done = 1
  490.         sin120 = Sin(_D2R(120))
  491.         cos120 = Cos(_D2R(120))
  492.         sin240 = Sin(_D2R(240))
  493.         cos240 = Cos(_D2R(240))
  494.     End If
  495.     rotY = rotY + 2
  496.     r = 0.2: yOff = -0.6
  497.     _glDisable _GL_LIGHTING
  498.     For i = 1 To worldStats.num_of_point
  499.         _glPushMatrix
  500.         _glTranslatef pointMap(i).x, 0, pointMap(i).y
  501.         _glRotatef rotY, 0, 1, 0
  502.         _glBegin _GL_TRIANGLES
  503.         'down open pyramid
  504.         _glColor3f 0, 1, 1
  505.         _glVertex3f 0, -r + yOff, 0
  506.         _glVertex3f r, yOff, 0
  507.         _glVertex3f r * cos120, yOff, r * sin120
  508.         _glColor3f 1, 0, 1
  509.         _glVertex3f 0, -r + yOff, 0
  510.         _glVertex3f r * cos120, yOff, r * sin120
  511.         _glVertex3f r * cos240, yOff, r * sin240
  512.         _glColor3f 1, 1, 0
  513.         _glVertex3f 0, -r + yOff, 0
  514.         _glVertex3f r, yOff, 0
  515.         _glVertex3f r * cos240, yOff, r * sin240
  516.         'upper open pyramid, combined together with to form a diamond like structure
  517.         _glColor3f 1, 0, 1
  518.         _glVertex3f 0, r + yOff, 0
  519.         _glVertex3f r, yOff, 0
  520.         _glVertex3f r * cos120, yOff, r * sin120
  521.         _glColor3f 1, 1, 0
  522.         _glVertex3f 0, r + yOff, 0
  523.         _glVertex3f r * cos120, yOff, r * sin120
  524.         _glVertex3f r * cos240, yOff, r * sin240
  525.         _glColor3f 0, 1, 1
  526.         _glVertex3f 0, r + yOff, 0
  527.         _glVertex3f r, yOff, 0
  528.         _glVertex3f r * cos240, yOff, r * sin240
  529.         _glEnd
  530.         _glPopMatrix
  531.     Next
  532.     _glEnable _GL_LIGHTING
  533.  
  534. Sub drawTraps ()
  535.     _glDisable _GL_LIGHTING
  536.     If worldStats.num_of_trap > 0 Then
  537.         For i = 1 To worldStats.num_of_trap
  538.             Select Case trapMap(i).typ
  539.                 Case 1 'laser trap
  540.                     If trapMap(i).d_3 = 1 Then
  541.                         If trapMap(i).d_1 = 1 Then 'aage piche w.r.t. XY plane
  542.                             _glColor3f 1, 0, 0
  543.                             selectTexture 0
  544.                             _glLineWidth 3.0
  545.                             _glBegin _GL_LINES
  546.                             For j = -0.8 To 0.8 Step 0.4
  547.                                 _glVertex3f trapMap(i).x - 0.5, j, trapMap(i).y - 0.5
  548.                                 _glVertex3f trapMap(i).x - 0.5, j, trapMap(i).y + 0.5
  549.                                
  550.                                 _glVertex3f trapMap(i).x + 0.5, j, trapMap(i).y - 0.5
  551.                                 _glVertex3f trapMap(i).x + 0.5, j, trapMap(i).y + 0.5
  552.                             Next
  553.                             _glEnd
  554.                         Else 'agal bagal w.r.t. XY Plane
  555.                             _glColor3f 1, 0, 0
  556.                             selectTexture 0
  557.                             _glLineWidth 3.0
  558.                             _glBegin _GL_LINES
  559.                             For j = -0.8 To 0.8 Step 0.4
  560.                                 _glVertex3f trapMap(i).x - 0.5, j, trapMap(i).y - 0.5
  561.                                 _glVertex3f trapMap(i).x + 0.5, j, trapMap(i).y - 0.5
  562.                                
  563.                                 _glVertex3f trapMap(i).x - 0.5, j, trapMap(i).y + 0.5
  564.                                 _glVertex3f trapMap(i).x + 0.5, j, trapMap(i).y + 0.5
  565.                             Next
  566.                             _glEnd
  567.                         End If
  568.                     End If
  569.             End Select
  570.         Next
  571.     End If
  572.     _glEnable _GL_LIGHTING
  573.  
  574. Sub drawBox (x, y, z, w, h, d) '(x,y,z)->location to draw, w->width, h->height, d-depth of the box
  575.     'XY
  576.     drawXYPlane x, y, z - d / 2, w, h, -1
  577.     drawXYPlane x, y, z + d / 2, w, h, 1
  578.     'YZ
  579.     drawYZPlane x - w / 2, y, z, h, d, -1
  580.     drawYZPlane x + w / 2, y, z, h, d, 1
  581.     'XZ
  582.     drawXZPlane x, y - h / 2, z, w, d, -1
  583.     drawXZPlane x, y + h / 2, z, w, d, 1
  584.  
  585. Sub drawXZPlane (x, y, z, w, d, n) '(x,z)->location to draw, w->width, d->depth of the plane, n->normal direction in Y direction
  586.     _glBegin _GL_QUADS
  587.     _glTexCoord2f 1, 0
  588.     _glNormal3f 0, n, 0
  589.     _glVertex3f x - (w / 2), y, z - d / 2
  590.     _glTexCoord2f 1, 1
  591.     _glNormal3f 0, n, 0
  592.     _glVertex3f x + (w / 2), y, z - d / 2
  593.     _glTexCoord2f 0, 1
  594.     _glNormal3f 0, n, 0
  595.     _glVertex3f x + (w / 2), y, z + d / 2
  596.     _glTexCoord2f 0, 0
  597.     _glNormal3f 0, n, 0
  598.     _glVertex3f x - (w / 2), y, z + d / 2
  599.     _glEnd
  600.  
  601. Sub drawXYPlane (x, y, z, w, h, n) '(x,y)-> location to draw, w->width, h->height, n->normal direction in Z direction
  602.     _glBegin _GL_QUADS
  603.     _glTexCoord2f 0, 0
  604.     _glNormal3f 0, 0, n
  605.     _glVertex3f x - (w / 2), y - h / 2, z
  606.     _glTexCoord2f 1, 0
  607.     _glNormal3f 0, 0, n
  608.     _glVertex3f x + (w / 2), y - h / 2, z
  609.     _glTexCoord2f 1, 1
  610.     _glNormal3f 0, 0, n
  611.     _glVertex3f x + (w / 2), y + h / 2, z
  612.     _glTexCoord2f 0, 1
  613.     _glNormal3f 0, 0, n
  614.     _glVertex3f x - (w / 2), y + h / 2, z
  615.     _glEnd
  616.  
  617. Sub drawYZPlane (x, y, z, h, d, n) '(y,z)->location to draw, h->height, d->depth, n->normal direction in X direction
  618.     _glBegin _GL_QUADS
  619.     _glTexCoord2f 0, 0
  620.     _glNormal3f n, 0, 0
  621.     _glVertex3f x, y - (h / 2), z - d / 2
  622.     _glTexCoord2f 0, 1
  623.     _glNormal3f n, 0, 0
  624.     _glVertex3f x, y + (h / 2), z - d / 2
  625.     _glTexCoord2f 1, 1
  626.     _glNormal3f n, 0, 0
  627.     _glVertex3f x, y + (h / 2), z + d / 2
  628.     _glTexCoord2f 1, 0
  629.     _glNormal3f n, 0, 0
  630.     _glVertex3f x, y - (h / 2), z + d / 2
  631.     _glEnd
  632.  
  633. Sub selectTexture (tex&)
  634.     _glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_LINEAR
  635.     _glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_LINEAR
  636.     _glBindTexture _GL_TEXTURE_2D, tex&
  637.  
  638. Function feedGLTexture& (img As Long)
  639.     If img < -1 Then
  640.         Dim m As _MEM
  641.         m = _MemImage(img)
  642.  
  643.         _glGenTextures 1, _Offset(feedGLTexture&)
  644.         _glBindTexture _GL_TEXTURE_2D, feedGLTexture&
  645.  
  646.         _glTexImage2D _GL_TEXTURE_2D, 0, _GL_RGBA, _Width(img&), _Height(img&), 0, _GL_BGRA_EXT, _GL_UNSIGNED_BYTE, m.OFFSET
  647.  
  648.         _MemFree m
  649.         _glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_LINEAR
  650.         _glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_NEAREST
  651.  
  652.     Else
  653.         echo "FUNCTION feedGlTexture&() : invalid image handle passed"
  654.     End If
  655.  
  656. Sub loadWorld (map_file$) 'load the given .3dm world
  657.     If Not _FileExists(map_file$) Then echo "SUB loadWorld() : map file not found - " + map_file$: Exit Sub
  658.  
  659.     f = FreeFile
  660.     Open map_file$ For Binary As #f
  661.     Get #f, , worldStats
  662.  
  663.     If worldStats.signature <> "3DM@QB64" Then echo "SUB loadWorld() : Invalid Signature - " + map_file$: Exit Sub
  664.    
  665.     'show map info in debug mode
  666.     echo "SUB loadWorld() : Map file loaded - " + map_file$
  667.     echo "World size : " + Str$(worldStats.mapW) + "x" + Str$(worldStats.mapH)
  668.     echo "Player initial position : " + Str$(worldStats.px) + "," + Str$(worldStats.py)
  669.     echo "Number of wall block(s) : " + Str$(worldStats.num_of_wall)
  670.     echo "Number of floor block(s) : " + Str$(worldStats.num_of_floor)
  671.     echo "Number of ceiling block(s) : " + Str$(worldStats.num_of_ceiling)
  672.     echo "Number of door(s) : " + Str$(worldStats.num_of_door)
  673.     echo "Number of key(s) : " + Str$(worldStats.num_of_key)
  674.     echo "Number of point(s) : " + Str$(worldStats.num_of_point)
  675.     echo "Number of detail(s)/interior(s) : " + Str$(worldStats.num_of_detail)
  676.     echo "Number of trap(s) : " + Str$(worldStats.num_of_trap)
  677.     echo "Player destination : " + Str$(worldStats.destX) + "," + Str$(worldStats.destY)
  678.     echo "Global Light Settings :- "
  679.     echo "Ambient Color : (" + Str$(worldStats.light_amb.x) + "," + Str$(worldStats.light_amb.y) + "," + Str$(worldStats.light_amb.z) + ")"
  680.     echo "Diffuse Color : (" + Str$(worldStats.light_diff.x) + "," + Str$(worldStats.light_diff.y) + "," + Str$(worldStats.light_diff.z) + ")"
  681.     echo "Specular Color : (" + Str$(worldStats.light_spec.x) + "," + Str$(worldStats.light_spec.y) + "," + Str$(worldStats.light_spec.z) + ")"
  682.     echo "Direction : (" + Str$(worldStats.light_dir.x) + "," + Str$(worldStats.light_dir.y) + "," + Str$(worldStats.light_dir.z) + ")"
  683.     'wall
  684.     If worldStats.num_of_wall > 0 Then
  685.         ReDim wallMap(worldStats.num_of_wall) As map_element
  686.         For i = 1 To worldStats.num_of_wall
  687.             Get #f, , wallMap(i)
  688.         Next
  689.     End If
  690.     'floor
  691.     If worldStats.num_of_floor > 0 Then
  692.         ReDim floorMap(worldStats.num_of_floor) As map_element
  693.         For i = 1 To worldStats.num_of_floor
  694.             Get #f, , floorMap(i)
  695.         Next
  696.     End If
  697.     'ceiling
  698.     If worldStats.num_of_ceiling > 0 Then
  699.         ReDim ceilingMap(worldStats.num_of_ceiling) As map_element
  700.         For i = 1 To worldStats.num_of_ceiling
  701.             Get #f, , ceilingMap(i)
  702.         Next
  703.     End If
  704.     'door
  705.     If worldStats.num_of_door > 0 Then
  706.         ReDim doorMap(worldStats.num_of_door) As map_element
  707.         For i = 1 To worldStats.num_of_door
  708.             Get #f, , doorMap(i)
  709.         Next
  710.     End If
  711.     'keys
  712.     If worldStats.num_of_key > 0 Then
  713.         ReDim keyMap(worldStats.num_of_key) As map_element
  714.         For i = 1 To worldStats.num_of_key
  715.             Get #f, , keyMap(i)
  716.         Next
  717.     End If
  718.     'point
  719.     If worldStats.num_of_point > 0 Then
  720.         ReDim pointMap(worldStats.num_of_point) As map_element
  721.         For i = 1 To worldStats.num_of_point
  722.             Get #f, , pointMap(i)
  723.         Next
  724.     End If
  725.    
  726.     If worldStats.num_of_detail > 0 Then
  727.         ReDim detailMap(worldStats.num_of_detail) As map_element
  728.         For i = 1 To worldStats.num_of_detail
  729.             Get #f, , detailMap(i)
  730.         Next
  731.     End If
  732.  
  733.     If worldStats.num_of_trap > 0 Then
  734.         ReDim trapMap(worldStats.num_of_trap) As map_element
  735.         For i = 1 To worldStats.num_of_trap
  736.             Get #f, , trapMap(i)
  737.         Next
  738.     End If
  739.  
  740.     Close #f
  741.    
  742.     'generate new collision map
  743.     ReDim collisionMap(worldStats.mapW, worldStats.mapH, 1) As _Byte
  744.     For i = 1 To worldStats.num_of_wall
  745.         collisionMap(wallMap(i).x, wallMap(i).y, 0) = 1
  746.     Next
  747.     For i = 1 To worldStats.num_of_door
  748.         collisionMap(doorMap(i).x, doorMap(i).y, 0) = 1
  749.         collisionMap(doorMap(i).x, doorMap(i).y, 1) = 2
  750.     Next
  751.     For i = 1 To worldStats.num_of_key
  752.         collisionMap(keyMap(i).x, keyMap(i).y, 1) = 3
  753.     Next
  754.    
  755.  
  756. Function near_int (c)
  757.     If (c - Int(c)) >= 0.5 Then near_int = Int(c) + 1 Else near_int = Int(c)
  758.  
  759. Sub echo (a$)
  760.     $If DEBUG = -1 Then
  761.         _Echo a$
  762.     $End If
  763.  
  764. Function glVec4%& (x, y, z, w)
  765.     Static internal_vec4(3)
  766.     internal_vec4(0) = x
  767.     internal_vec4(1) = y
  768.     internal_vec4(2) = z
  769.     internal_vec4(3) = w
  770.     glVec4%& = _Offset(internal_vec4())
  771.  
  772. Function map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  773.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  774.  
  775.  
  776. 'supportive functions
  777.  
  778. '##################################################################################
  779. 'syntax: extract_file_all cdf_file_name$, path_where_file_is_to_be_extracted$
  780. '* extract all the files present in the given cdf file.
  781. '  path_where_file_is_to_be_extracted$ must end either with '/' or '\'
  782. '##################################################################################
  783. sub extract_file_all (f_name$, path$)
  784.     ' $CHECKING:OFF
  785.     DIM header AS cdf_file_header, element AS cdf_file_element
  786.     dim fileToBeExtracted$
  787.  
  788.     IF _FILEEXISTS(f_name$) THEN
  789.         f = FREEFILE
  790.         OPEN f_name$ FOR BINARY AS #f
  791.         GET #f, , header
  792.         IF header.signature <> "cdf@qb64" THEN CLOSE #f: echo "In SUB extract_file_all(): Invalid file - "+f_name$: EXIT SUB 'verify if it is even a cdf_file or not. Then only proceed.
  793.  
  794.         FOR i = 1 TO header.num_of_files
  795.             GET #f, , element
  796.  
  797.                         a$ = SPACE$(element.compressed_bytes)
  798.                         GET #f, , a$ 'get content
  799.             fileToBeExtracted$ = _TRIM$(element.file_name)
  800.                         ff = FREEFILE
  801.                         IF _FILEEXISTS(path$ + fileToBeExtracted$) THEN KILL path$ + fileToBeExtracted$ 'delete if there already exists a file with the same name
  802.                         ea$ = _INFLATE$(a$, element.original_bytes) 'decompress
  803.             echo path$+fileToBeExtracted$
  804.                         OPEN path$ + fileToBeExtracted$ FOR BINARY AS #ff 'create the file at the output path
  805.                         PUT #ff, , ea$
  806.                         CLOSE #ff
  807.         NEXT
  808.         CLOSE #f
  809.         else
  810.                 echo "In SUB extract_file_all(): File "+f_name$+" not found."
  811.     END IF
  812.     ' $CHECKING:ON
  813.  
  814. '####################################################################################
  815. 'syntax: extract_file cdf_file_name$, name_of_file_To_Be_Extracted$, output_path$
  816. '* extract the given file from cdf_file if it exists in it to the required
  817. '  output path. output_path$ must end with either '/' or '\'.
  818. '#####################################################################################
  819. SUB extract_file (f_name$, fileToBeExtracted$, path$)
  820.     ' $CHECKING:OFF
  821.     DIM header AS cdf_file_header, element AS cdf_file_element
  822.  
  823.     IF _FILEEXISTS(f_name$) THEN
  824.         f = FREEFILE
  825.         OPEN f_name$ FOR BINARY AS #f
  826.         GET #f, , header
  827.         IF header.signature <> "cdf@qb64" THEN CLOSE #f: echo "In SUB extract_file(): Invalid file - "+f_name$: EXIT SUB 'verify if it is even a cdf_file or not. Then only proceed.
  828.  
  829.         FOR i = 1 TO header.num_of_files
  830.             GET #f, , element
  831.             IF RTRIM$(element.file_name) = fileToBeExtracted$ THEN 'extract the file, if condition become true
  832. ' ?"ok"
  833.                 a$ = SPACE$(element.compressed_bytes)
  834.                 GET #f, , a$ 'get content
  835.                 ff = FREEFILE
  836.                 IF _FILEEXISTS(path$ + fileToBeExtracted$) THEN KILL path$ + fileToBeExtracted$ 'delete if there already exists a file with the same name
  837.                 ea$ = _INFLATE$(a$, element.original_bytes) 'decompress
  838.                 OPEN path$ + fileToBeExtracted$ FOR BINARY AS #ff 'create the file at the output path
  839.                 PUT #ff, , ea$
  840.                 CLOSE #ff
  841.                 EXIT SUB
  842.             ELSE
  843.                 SEEK f, SEEK(f) + element.compressed_bytes 'file name is not one which user looking for, we just skip to next entry
  844.             END IF
  845.         NEXT
  846.         CLOSE #f
  847.         else
  848.                 echo "In SUB extract_file(): File not found - "+f_name$
  849.     END IF
  850.     ' $CHECKING:ON
  851.  
  852. SUB __internal_dummy_cdf_sub () 'declare required types
  853.     TYPE cdf_file_header
  854.         signature AS STRING * 8 'cdf@qb64
  855.         num_of_files AS INTEGER
  856.     END TYPE
  857.  
  858.     TYPE cdf_file_element
  859.         file_name AS STRING * 128
  860.         original_bytes AS _UNSIGNED _INTEGER64
  861.         compressed_bytes AS _UNSIGNED _INTEGER64
  862.     END TYPE
  863.  



3
Programs / Old Skool Plasma Effect
« on: May 24, 2021, 08:06:51 am »
Hey guys! Currently, I'm learning GLSL and I recently created shader of plasma
effect - https://www.shadertoy.com/view/NllGRM which ChiaPet asked me convert it to QB64 at Discord
So, I converted it to QB64. :)

Code: QB64: [Select]
  1. 'conversion of https://www.shadertoy.com/view/NllGRM
  2. 'in QB64 By Ashish for Richard
  3. '24 May, 2021
  4. _Title "PLASMA EFFECT"
  5. Screen _NewImage(400, 400, 32)
  6. Type vec3
  7.     As Single x, y, z
  8. Dim clr As vec3, final As vec3
  9. iTime = 0
  10.     For i = 0 To 400
  11.         For j = 0 To 400
  12.             u = j / 400
  13.             v = i / 400
  14.             t = Abs(Sin(15 * ((u - 0.5 + 0.3 * Sin(iTime * 2)) ^ 2 + (v - 0.5 + 0.3 * Cos(iTime * 2)) ^ 2) ^ 0.5 - iTime * 5))
  15.             clr.x = t: clr.y = 1 - t: clr.z = 1 - t
  16.             final = clr
  17.             t = Cos(u * 20 + iTime * 5) ^ 2
  18.             clr.x = 1 - t: clr.y = t: clr.z = 0
  19.             final.x = final.x + clr.x
  20.             final.y = final.y + clr.y
  21.             final.z = final.z + clr.z
  22.             PSet (j, i), _RGB32(final.x * 255, final.y * 255, final.z * 255)
  23.         Next j
  24.     Next i
  25.     _Display
  26.     iTime = iTime + 0.05
  27.  

4
Programs / Music Visualiser 2D
« on: May 19, 2021, 01:59:39 am »
Hey everyone! When I learned about _MEMSOUND by watching Fellippe's video on QB64 Team channel.. I had an
idea to visualise the sound...

REPLACE the value of file_name$ to your sound file.

Have fun!!
Code: QB64: [Select]
  1. '#####################
  2. ' Music Visualiser 2D
  3. ' By Ashish
  4. ' 18 May, 2021
  5. '#####################
  6.  
  7. file_name$ = "rolling.mp3" '<<---- replace with your file name with a sound file you have
  8. $Let DEBUG = 1
  9.  
  10. $If DEBUG Then
  11.     $Console
  12.     _Echo "loading sound"
  13. Dim song&
  14.  
  15. song& = _SndOpen(file_name$)
  16. If song& = 0 Then Print "Load failed.": End
  17.  
  18. _Title "Music Visualiser 2D"
  19. Type vec3
  20.     As Single x, y, z
  21. Type vec2
  22.     As Single x, y
  23.     Sub gluLookAt (ByVal eyeX#, Byval eyeY#, Byval eyeZ#, Byval centerX#, Byval centerY#, Byval centerZ#, Byval upX#, Byval upY#, Byval upZ#)
  24. Screen _NewImage(800, 600, 32)
  25. Dim Shared leftChannel As _MEM, rightChannel As _MEM
  26. Dim Shared stereo As _Byte, glAllow As _Byte
  27. Dim Shared leftData(0 To 360) As Single, rightData(0 To 360) As Single, colorData(0 To 360) As vec3
  28. Dim tmp_color As _Unsigned Long
  29. For i = 0 To 360
  30.     tmp_color = hsb~&(i * (255 / 360), 255, 128, 255)
  31.     colorData(i).x = _Red(tmp_color) / 255
  32.     colorData(i).y = _Green(tmp_color) / 255
  33.     colorData(i).z = _Blue(tmp_color) / 255
  34.  
  35. leftChannel = _MemSound(song&, 1)
  36. rightChannel = _MemSound(song&, 2)
  37. If leftChannel.SIZE = 0 Then
  38.     Print "An error occurred."
  39.     End
  40. If rightChannel.SIZE = 0 Then
  41.     $If DEBUG Then
  42.         _Echo "sound is mono"
  43.     $End If
  44.     stereo = 0
  45.     $If DEBUG Then
  46.         _Echo "sound is stereo"
  47.     $End If
  48.     stereo = 1
  49.  
  50. i = 0
  51. glAllow = 1
  52. _FPS 60
  53. _SndPlay song&
  54.  
  55.  
  56.  
  57.     For n = 0 To 360
  58.         _MemGet leftChannel, leftChannel.OFFSET + i + j, a1% 'get sound data
  59.         leftData(n) = a1% * 0.00001
  60.         If stereo = 1 Then
  61.             _MemGet rightChannel, rightChannel.OFFSET + i + j, a2%
  62.             rightData(n) = a2% * 0.00001
  63.         End If
  64.         j = j + 2
  65.         If i + j + 2 > leftChannel.SIZE Then Exit Do
  66.         If stereo = 1 And i + j + 2 > rightChannel.SIZE Then Exit Do
  67.     Next
  68.     i = i + 2 * (44100 / 60)
  69.     j = 0
  70.     If i + 2 > leftChannel.SIZE Then Exit Do
  71.     If stereo = 1 And i + j + 2 > rightChannel.SIZE Then Exit Do
  72.     _Limit 60
  73.  
  74.  
  75. _SndClose song& 'closing the sound releases the mem blocks
  76.  
  77. Sub _GL ()
  78.     Static glInit
  79.     If glAllow = 0 Then
  80.         Exit Sub
  81.     End If
  82.     If glInit = 0 Then
  83.         glInit = 1
  84.         _glViewport 0, 0, _Width, _Height
  85.         $If DEBUG Then
  86.             _Echo "_GL() : initialization done. glInit set to 1"
  87.         $End If
  88.     End If
  89.  
  90.     'projection setup
  91.     _glMatrixMode _GL_PROJECTION
  92.     _gluPerspective 50, _Width / _Height, 0.1, 50
  93.  
  94.     _glMatrixMode _GL_MODELVIEW
  95.     gluLookAt 0, 0, 3, 0, 0, 0, 0, 1, 0
  96.  
  97.  
  98.  
  99.     _glBegin _GL_LINES
  100.     For i = 0 To 360
  101.         c = Int(map(Abs(leftData(i)), 0, 0.7, 0, 360))
  102.         _glColor3f colorData(c).x, colorData(c).y, colorData(c).z
  103.         _glVertex2f -1, 0
  104.         _glVertex2f -1 + (0.5 + leftData(i)) * Cos(_D2R(i)), (0.5 + leftData(i)) * Sin(_D2R(i))
  105.         _glVertex2f 1, 0
  106.         If stereo = 0 Then
  107.  
  108.             _glVertex2f 1 + (0.5 + leftData(i)) * Cos(_D2R(i)), (0.5 + leftData(i)) * Sin(_D2R(i))
  109.         Else
  110.             _glVertex2f 1 + (0.5 + rightData(i)) * Cos(_D2R(i)), (0.5 + rightData(i)) * Sin(_D2R(i))
  111.         End If
  112.     Next
  113.     _glEnd
  114.     _glFlush
  115.  
  116.  
  117. 'function from
  118. 'p5js.bas
  119. 'http://bit.ly/p5jsbas
  120. Function map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  121.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  122.  
  123.  
  124. 'method adapted form http://stackoverflow.com/questions/4106363/converting-rgb-to-hsb-colors
  125. Function hsb~& (__H As _Float, __S As _Float, __B As _Float, A As _Float)
  126.     Dim H As _Float, S As _Float, B As _Float
  127.  
  128.     H = map(__H, 0, 255, 0, 360)
  129.     S = map(__S, 0, 255, 0, 1)
  130.     B = map(__B, 0, 255, 0, 1)
  131.  
  132.     If S = 0 Then
  133.         hsb~& = _RGBA32(B * 255, B * 255, B * 255, A)
  134.         Exit Function
  135.     End If
  136.  
  137.     Dim fmx As _Float, fmn As _Float
  138.     Dim fmd As _Float, iSextant As Integer
  139.     Dim imx As Integer, imd As Integer, imn As Integer
  140.  
  141.     If B > .5 Then
  142.         fmx = B - (B * S) + S
  143.         fmn = B + (B * S) - S
  144.     Else
  145.         fmx = B + (B * S)
  146.         fmn = B - (B * S)
  147.     End If
  148.  
  149.     iSextant = Int(H / 60)
  150.  
  151.     If H >= 300 Then
  152.         H = H - 360
  153.     End If
  154.  
  155.     H = H / 60
  156.     H = H - (2 * Int(((iSextant + 1) Mod 6) / 2))
  157.  
  158.     If iSextant Mod 2 = 0 Then
  159.         fmd = (H * (fmx - fmn)) + fmn
  160.     Else
  161.         fmd = fmn - (H * (fmx - fmn))
  162.     End If
  163.  
  164.     imx = _Round(fmx * 255)
  165.     imd = _Round(fmd * 255)
  166.     imn = _Round(fmn * 255)
  167.  
  168.     Select Case Int(iSextant)
  169.         Case 1
  170.             hsb~& = _RGBA32(imd, imx, imn, A)
  171.         Case 2
  172.             hsb~& = _RGBA32(imn, imx, imd, A)
  173.         Case 3
  174.             hsb~& = _RGBA32(imn, imd, imx, A)
  175.         Case 4
  176.             hsb~& = _RGBA32(imd, imn, imx, A)
  177.         Case 5
  178.             hsb~& = _RGBA32(imx, imn, imd, A)
  179.         Case Else
  180.             hsb~& = _RGBA32(imx, imd, imn, A)
  181.     End Select
  182.  
  183.  



5
Programs / Colorful Triangle Geometry
« on: May 04, 2021, 02:01:02 am »
Hi everyone!
I made a program which makes colorful and beautiful pattern full of triangles.
I hope you will like like it. :)

Code: QB64: [Select]
  1. 'Coded By Ashish
  2. '4 may, 2021
  3. 'edited : 9 may,2021
  4. _Title "Triangular Geometry"
  5. Screen _NewImage(800, 600, 32)
  6. 404 pattern 400, 0, 0, 600, 800, 600, 600, Int(Rnd * 255), 50 + Int(Rnd * 205)
  7. _Delay 0.25
  8. GoTo 404
  9. Sub pattern (x0, y0, x1, y1, x2, y2, d_max, s_min, s_width)
  10.     mx = (x1 + x2) / 2: my = (y1 + y2) / 2
  11.     d = Int(Sqr((mx - x0) ^ 2 + (my - y0) ^ 2))
  12.     drawTriangleSolid x0, y0, x1, y1, x2, y2, hsb(map(d, 10, d_max, s_min, (s_max + s_width) Mod 255), 255, 128, map(d, 10, d_max, 50, 255))
  13.     If d > 10 Then
  14.         If d >= d_max Then
  15.             pattern mx, my, x0, y0, x1, y1, d_max, s_min, s_width
  16.             pattern mx, my, x0, y0, x2, y2, d_max, s_min, s_width
  17.  
  18.         Else
  19.             If Rnd > 0.5 Then pattern mx, my, x0, y0, x1, y1, d_max, s_min, s_width Else pattern mx, my, x0, y0, x2, y2, d_max, s_min, s_width
  20.         End If
  21.     End If
  22.  
  23. Sub drawTriangleSolid (x1, y1, x2, y2, x3, y3, c~&)
  24.     Static temp_color_img As Long, drawTriangle_init As _Byte
  25.     If drawTriangle_init = 0 Then
  26.         temp_color_img = _NewImage(1, 1, 32)
  27.         drawTriangle_init = 1
  28.     End If
  29.     preDest = _Dest
  30.     _Dest temp_color_img
  31.     PSet (0, 0), c~&
  32.     _Dest preDest
  33.  
  34.     _MapTriangle (0, 0)-(0, 0)-(0, 0), temp_color_img To(x1, y1)-(x2, y2)-(x3, y3), 0, _Smooth
  35.  
  36.  
  37. Sub drawTriangle (x1, y1, x2, y2, x3, y3, c~&)
  38.     Line (x1, y1)-(x2, y2), c~&
  39.     Line (x2, y2)-(x3, y3), c~&
  40.     Line (x3, y3)-(x1, y1), c~&
  41.  
  42. 'method adapted form http://stackoverflow.com/questions/4106363/converting-rgb-to-hsb-colors
  43. Function hsb~& (__H As _Float, __S As _Float, __B As _Float, A As _Float)
  44.     Dim H As _Float, S As _Float, B As _Float
  45.  
  46.     H = map(__H, 0, 255, 0, 360)
  47.     S = map(__S, 0, 255, 0, 1)
  48.     B = map(__B, 0, 255, 0, 1)
  49.  
  50.     If S = 0 Then
  51.         hsb~& = _RGBA32(B * 255, B * 255, B * 255, A)
  52.         Exit Function
  53.     End If
  54.  
  55.     Dim fmx As _Float, fmn As _Float
  56.     Dim fmd As _Float, iSextant As Integer
  57.     Dim imx As Integer, imd As Integer, imn As Integer
  58.  
  59.     If B > .5 Then
  60.         fmx = B - (B * S) + S
  61.         fmn = B + (B * S) - S
  62.     Else
  63.         fmx = B + (B * S)
  64.         fmn = B - (B * S)
  65.     End If
  66.  
  67.     iSextant = Int(H / 60)
  68.  
  69.     If H >= 300 Then
  70.         H = H - 360
  71.     End If
  72.  
  73.     H = H / 60
  74.     H = H - (2 * Int(((iSextant + 1) Mod 6) / 2))
  75.  
  76.     If iSextant Mod 2 = 0 Then
  77.         fmd = (H * (fmx - fmn)) + fmn
  78.     Else
  79.         fmd = fmn - (H * (fmx - fmn))
  80.     End If
  81.  
  82.     imx = _Round(fmx * 255)
  83.     imd = _Round(fmd * 255)
  84.     imn = _Round(fmn * 255)
  85.  
  86.     Select Case Int(iSextant)
  87.         Case 1
  88.             hsb~& = _RGBA32(imd, imx, imn, A)
  89.         Case 2
  90.             hsb~& = _RGBA32(imn, imx, imd, A)
  91.         Case 3
  92.             hsb~& = _RGBA32(imn, imd, imx, A)
  93.         Case 4
  94.             hsb~& = _RGBA32(imd, imn, imx, A)
  95.         Case 5
  96.             hsb~& = _RGBA32(imx, imn, imd, A)
  97.         Case Else
  98.             hsb~& = _RGBA32(imx, imd, imn, A)
  99.     End Select
  100.  
  101.  
  102.  
  103. 'from p5js.bas
  104. Function map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  105.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  106.  
  107.  


SCRST_1.png

6
Programs / Pyramid with tile texture
« on: March 26, 2021, 10:25:51 am »
Hey everyone! I've a new opengl program for you!

Code: QB64: [Select]
  1. _Title "Pyramid with tile texture"
  2. 'Coded by Ashish
  3. '26 march, 2021
  4. Screen _NewImage(600, 600, 32)
  5.  
  6. Type vec3
  7.     As Single x, y, z
  8.  
  9.     'for camera
  10.     Sub gluLookAt (ByVal eyeX#, Byval eyeY#, Byval eyeZ#, Byval centerX#, Byval centerY#, Byval centerZ#, Byval upX#, Byval upY#, Byval upZ#)
  11.  
  12.  
  13. Dim Shared glAllow As _Byte
  14. Dim Shared texture_data As Long, pyramid_coord(3) As vec3, texcoord(2) As vec3
  15.  
  16. 'coordinate data
  17. pyramid_coord(0).x = 0: pyramid_coord(0).y = 2: pyramid_coord(0).z = 0
  18. pyramid_coord(1).x = -2: pyramid_coord(1).y = -2: pyramid_coord(1).z = -2
  19. pyramid_coord(2).x = 2: pyramid_coord(2).y = -2: pyramid_coord(2).z = -2
  20. pyramid_coord(3).x = 0: pyramid_coord(3).y = -2: pyramid_coord(3).z = 2
  21. texcoord(0).x = 0: texcoord(0).y = 0
  22. texcoord(1).x = 0.5: texcoord(1).y = 1
  23. texcoord(2).x = 1: texcoord(2).y = 0
  24.  
  25. texture_data = _NewImage(400, 400, 32)
  26. texture_data_r = _CopyImage(texture_data)
  27. glAllow = 1
  28. 'set these shades values as per your requirement
  29. shade1& = _RGB(15, 190, 200)
  30. shade2& = _RGB(240, 10, 70)
  31.     _Dest texture_data_r
  32.     Line (0, 0)-(_Width(texture_data_r), _Height(texture_data_r)), shade1&, BF
  33.     i = 1
  34.     For y = 0 To _Height(texture_data_r) Step 40
  35.         i = i * -1
  36.         For x = -40 + 20 * i To _Width(texture_data_r) + 40 Step 80
  37.             CircleFill x + offx, y, 20, shade2&
  38.         Next
  39.     Next
  40.     offx = offx + 3
  41.     If offx > 80 Then offx = 0
  42.     _Dest 0
  43.     _PutImage , texture_data_r, texture_data
  44.     Cls , shade1&
  45.     For x = -100 To 700 Step 100
  46.         Line (x + ax, 0)-(x + ax, 700), shade2&
  47.         Line (x + ax + 50, 0)-(x + ax + 50, 700), shade2&
  48.         Paint (x + ax + 49, 0), shade2&, shade2&
  49.         Paint (x + ax + 1, 0), shade2&, shade2&
  50.     Next
  51.     If ax > 100 Then ax = 0
  52.     ax = ax + 2
  53.     _Limit 60
  54.     _Display
  55.  
  56. Sub _GL ()
  57.     If glAllow = 0 Then Exit Sub
  58.     Static glInit, tex As Long, m As _MEM, t
  59.     If glInit = 0 Then
  60.         glInit = 1
  61.         _glViewport 0, 0, _Width, _Height
  62.         'texture
  63.         _glGenTextures 1, _Offset(tex)
  64.         m = _MemImage(texture_data)
  65.  
  66.     End If
  67.  
  68.     _glEnable _GL_TEXTURE_2D
  69.     _glEnable _GL_DEPTH_TEST
  70.     _glEnable _GL_BLEND
  71.  
  72.     'projection settings
  73.     _glMatrixMode _GL_PROJECTION
  74.     _gluPerspective 60, _Width / _Height, 0.1, 10
  75.  
  76.     _glMatrixMode _GL_MODELVIEW
  77.     gluLookAt 0, 0, 6, 0, 0, 0, 0, 1, 0
  78.     _glRotatef t, 0, 1, 0.8
  79.  
  80.     'drawing pyramid
  81.     _glBindTexture _GL_TEXTURE_2D, tex
  82.     _glTexImage2D _GL_TEXTURE_2D, 0, _GL_RGB, _Width(texture_data), _Height(texture_data), 0, _GL_BGRA_EXT, _GL_UNSIGNED_BYTE, m.OFFSET
  83.     _glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_LINEAR
  84.     _glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_NEAREST
  85.  
  86.     For i = 0 To 3
  87.         _glBegin _GL_TRIANGLES
  88.         For j = i To i + 2
  89.             _glTexCoord2f texcoord(j Mod 3).x, texcoord(j Mod 3).y
  90.             _glVertex3f pyramid_coord(j Mod 4).x, pyramid_coord(j Mod 4).y, pyramid_coord(j Mod 4).z
  91.         Next
  92.         _glEnd
  93.     Next
  94.     t = t + 1
  95.  
  96.  
  97. Sub CircleFill (x As Long, y As Long, R As Long, C As _Unsigned Long)
  98.     Dim x0 As Single, y0 As Single
  99.     Dim e As Single
  100.  
  101.     x0 = R
  102.     y0 = 0
  103.     e = -R
  104.     Do While y0 < x0
  105.         If e <= 0 Then
  106.             y0 = y0 + 1
  107.             Line (x - x0, y + y0)-(x + x0, y + y0), C, BF
  108.             Line (x - x0, y - y0)-(x + x0, y - y0), C, BF
  109.             e = e + 2 * y0
  110.         Else
  111.             Line (x - y0, y - x0)-(x + y0, y - x0), C, BF
  112.             Line (x - y0, y + x0)-(x + y0, y + x0), C, BF
  113.             x0 = x0 - 1
  114.             e = e - 2 * x0
  115.         End If
  116.     Loop
  117.     Line (x - R, y)-(x + R, y), C, BF
  118.  
  119.  
  120.  

7
Programs / NEON PEN
« on: November 19, 2020, 09:53:19 am »
INSTRUCTION : Drag with mouse on screen to draw stuff. :)

Code: QB64: [Select]
  1. _TITLE "NEON PEN"
  2. SCREEN _NEWIMAGE(800, 600, 32)
  3.  
  4. TYPE vec2
  5.     x AS SINGLE
  6.     y AS SINGLE
  7.  
  8. REDIM SHARED vert(1024) AS vec2, max_v_index
  9. DIM SHARED rFactor!, gFactor!, bFactor!
  10. rFactor! = 0.5: gFactor! = 1: bFactor! = 0.5
  11.     CLS
  12.     PRINT "VRAM Usage : "; vram; "KB"
  13.     PRINT "Vertices Used : "; max_v_index; "/"; UBOUND(vert)
  14.     vram = (UBOUND(vert) * 4) / 1024
  15.         mx = _MOUSEX: my = _MOUSEY
  16.         px = mx: py = my
  17.         WHILE _MOUSEBUTTON(1) AND max_v_index < UBOUND(vert)
  18.             WHILE _MOUSEINPUT: WEND
  19.             mx = _MOUSEX: my = _MOUSEY
  20.             IF ABS(px - mx) >= ABS(py - my) THEN
  21.                 IF mx >= px THEN s = 1 ELSE s = -1
  22.                 FOR i = px TO mx STEP s
  23.                     vert(max_v_index).x = i
  24.                     vert(max_v_index).y = map(i, px, mx, py, my)
  25.                     max_v_index = max_v_index + 1
  26.                     IF max_v_index > INT(UBOUND(vert) * 0.8) THEN REDIM _PRESERVE vert(max_v_index * 2) AS vec2
  27.                 NEXT
  28.             ELSE
  29.                 IF my >= py THEN s = 1 ELSE s = -1
  30.                 FOR i = py TO my STEP s
  31.                     vert(max_v_index).x = map(i, py, my, px, mx)
  32.                     vert(max_v_index).y = i
  33.                     max_v_index = max_v_index + 1
  34.                     IF max_v_index > INT(UBOUND(vert) * 0.8) THEN REDIM _PRESERVE vert(max_v_index * 2) AS vec2
  35.                 NEXT
  36.  
  37.             END IF
  38.             px = mx: py = my
  39.             _LIMIT 30
  40.         WEND
  41.     END IF
  42.  
  43.     _LIMIT 60
  44.  
  45. SUB _GL ()
  46.     STATIC glInit
  47.     IF glInit = 0 THEN
  48.         glInit = 1
  49.  
  50.     END IF
  51.     'set the gl screen so that it can work normal screen coordinates
  52.     _glTranslatef -1, 1, 0
  53.     _glScalef 1 / 400, -1 / 300, 1
  54.  
  55.     _glEnable _GL_BLEND
  56.  
  57.     _glBlendFunc _GL_SRC_ALPHA, _GL_ONE
  58.     _glEnableClientState _GL_VERTEX_ARRAY
  59.     _glVertexPointer 2, _GL_FLOAT, 0, _OFFSET(vert())
  60.     FOR j = 1 TO 15
  61.         _glColor4f rFactor!, gFactor!, bFactor!, 0.015
  62.         _glPointSize j
  63.         _glDrawArrays _GL_POINTS, 10, max_v_index
  64.     NEXT
  65.     _glFlush
  66.  
  67. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  68.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  69.  
  70.  

Thank you for running it. :)

EDIT : CODE UPDATED. Thanks to @Dav for finding a glitch in which it stopped drawing sometimes (when both max_v_index equal to Ubound(vert) )

8
Programs / Re: poem generator
« on: July 18, 2020, 05:33:28 am »
Note: This message is awaiting approval by a moderator.
Nice! :)

9
Programs / COVID-19 in QB64
« on: June 01, 2020, 03:56:30 am »
WARNING : Use mask. Sanitize your hands after closing the program.

Code: QB64: [Select]
  1. 'COVID-19 in QB64
  2. 'By Ashish
  3. '1 Jun, 2020
  4. '
  5. 'WARNING:Use mask. Apply santizer after closing the program.
  6.  
  7. _TITLE "I'm Covid-19"
  8. SCREEN _NEWIMAGE(600, 600, 32)
  9.  
  10.     SUB glutSolidSphere (BYVAL radius AS DOUBLE, BYVAL slices AS LONG, BYVAL stack AS LONG)
  11.     SUB gluLookAt (BYVAL eyeX#, BYVAL eyeY#, BYVAL eyeZ#, BYVAL centerX#, BYVAL centerY#, BYVAL centerZ#, BYVAL upX#, BYVAL upY#, BYVAL upZ#)
  12.  
  13.  
  14. DIM SHARED glAllow AS _BYTE
  15. TYPE vec3
  16.     x AS SINGLE
  17.     y AS SINGLE
  18.     z AS SINGLE
  19.  
  20. TYPE COVID19
  21.     pos AS vec3
  22.     r AS SINGLE
  23.  
  24. DIM SHARED virus AS COVID19
  25.  
  26. virus.r = 0.3
  27.  
  28. glAllow = -1
  29.     _LIMIT 1
  30.  
  31. SUB _GL ()
  32.     STATIC init, aspect, rotY
  33.  
  34.     IF glAllow = 0 THEN EXIT SUB
  35.     IF init = 0 THEN
  36.         init = 1
  37.         aspect = _WIDTH / _HEIGHT
  38.         _glViewport 0, 0, _WIDTH, _HEIGHT
  39.     END IF
  40.  
  41.     _glEnable _GL_DEPTH_TEST
  42.     _glEnable _GL_LIGHTING
  43.     _glEnable _GL_LIGHT0
  44.  
  45.     _glLightfv _GL_LIGHT0, _GL_AMBIENT, glVec4(0.0, 0.0, 0.0, 1)
  46.     _glLightfv _GL_LIGHT0, _GL_DIFFUSE, glVec4(0.8, 0.8, 0.8, 1)
  47.     _glLightfv _GL_LIGHT0, _GL_SPECULAR, glVec4(1, 1, 1, 1)
  48.     _glLightfv _GL_LIGHT0, _GL_POSITION, glVec4(0, 0, 10, 1)
  49.  
  50.  
  51.  
  52.     _glMatrixMode _GL_PROJECTION
  53.     _gluPerspective 50, aspect, 0.1, 10
  54.  
  55.     _glMatrixMode _GL_MODELVIEW
  56.     gluLookAt 0, 0, 2, 0, 0, 0, 0, 1, 0
  57.  
  58.  
  59.     _glLineWidth 2.0
  60.     _glPointSize 10.0
  61.  
  62.     _glTranslatef virus.pos.x, virus.pos.y, virus.pos.z
  63.     _glRotatef rotY, 0.5, 1, 0
  64.     rotY = rotY + 1
  65.  
  66.     _glMaterialfv _GL_FRONT_AND_BACK, _GL_DIFFUSE, glVec4(0, 0.9, 0, 1)
  67.  
  68.     glutSolidSphere virus.r, 20, 20
  69.  
  70.     _glDisable _GL_LIGHTING
  71.     _glColor3f 1, 0, 0
  72.     _glBegin _GL_LINES
  73.     FOR phi = 0.4 TO _PI(2) STEP .5
  74.         FOR theta = 0.3 TO _PI STEP .5
  75.             _glVertex3f virus.r * SIN(theta) * COS(phi), virus.r * SIN(theta) * SIN(phi), virus.r * COS(theta)
  76.             _glVertex3f (0.1 + virus.r) * SIN(theta) * COS(phi), (0.1 + virus.r) * SIN(theta) * SIN(phi), (0.1 + virus.r) * COS(theta)
  77.         NEXT
  78.     NEXT
  79.     _glEnd
  80.     _glBegin _GL_POINTS
  81.     FOR phi = 0.4 TO _PI(2) STEP .5
  82.         FOR theta = 0.3 TO _PI STEP .5
  83.             _glVertex3f (0.1 + virus.r) * SIN(theta) * COS(phi), (0.1 + virus.r) * SIN(theta) * SIN(phi), (0.1 + virus.r) * COS(theta)
  84.         NEXT
  85.     NEXT
  86.     _glEnd
  87.     _glEnable _GL_LIGHTING
  88.  
  89.  
  90.  
  91.  
  92. FUNCTION glVec4%& (x, y, z, w) 'give the offset of the given vector
  93.     STATIC internal_vec4(3)
  94.     internal_vec4(0) = x
  95.     internal_vec4(1) = y
  96.     internal_vec4(2) = z
  97.     internal_vec4(3) = w
  98.     glVec4%& = _OFFSET(internal_vec4())
  99.  

10
Programs / Space Filling with Polygons
« on: April 28, 2020, 07:42:44 am »
Hey everyone!

This program fills the space with a polygon (triangles, squares, etc) in such a way that any two polygon must not overlap/collide.
The polygon size gradually reduces and it try to fill as much space as possible.
The color of polygon also changes gradually, resulting in a beautiful pattern. :)

I took some help for concepts behind this from here - http://paulbourke.net/fractals/randomtile/

Run the program & Enjoy.

Instruction :  Enter the number of sides of the polygon you want to fill with (of course, it must greater than 2). Higher values will result
in slow speed. :)

Code: QB64: [Select]
  1. 'Space Filling with Polygon
  2. 'By Ashish
  3.  
  4. TYPE XY
  5.     x AS SINGLE
  6.     y AS SINGLE
  7.  
  8. _TITLE "Space Filling with Polygon"
  9. SCREEN _NEWIMAGE(800, 600, 32)
  10.  
  11. INPUT "Enter the number of side for a polygon (must be greater than 2) : ", x%
  12. IF x% < 3 THEN PRINT "It must be greate than 2!, taking default value of 3. Hit ENTER.": SLEEP: x% = 3
  13. polySides = x%
  14. DIM tempPoly1(polySides - 1) AS XY, tempPoly2(polySides - 1) AS XY
  15. REDIM polys(polySides - 1) AS XY
  16.  
  17.  
  18. init = 0
  19. poly_index = 0
  20. i = 1
  21. c = 1.1
  22. failed = 0
  23.     IF init = 0 THEN
  24.         r = p5random(150, 200)
  25.         ox = p5random(r, _WIDTH - r)
  26.         oy = p5random(r, _HEIGHT - r)
  27.         offAng = p5random(-_PI, _PI)
  28.         FOR j = 0 TO polySides - 1
  29.             polys(j).x = ox + COS(j * (_PI(2 / polySides)) + offAng) * r
  30.             polys(j).y = oy + SIN(j * (_PI(2 / polySides)) + offAng) * r
  31.         NEXT
  32.         clr~& = midColor(_RGB(255, 255, 0), _RGB(255, 100, 0), map(r, 200, 3, 0, 1))
  33.         drawPoly polys(), clr~&
  34.         init = 1
  35.         poly_index = poly_index + polySides
  36.     ELSE
  37.         r = 200 / (c ^ i)
  38.         ox = p5random(r, _WIDTH - r)
  39.         oy = p5random(r, _HEIGHT - r)
  40.         collided = 0
  41.         offAng = p5random(-_PI, _PI)
  42.         FOR j = 0 TO polySides - 1
  43.             tempPoly1(j).x = ox + COS(j * (_PI(2 / polySides)) + offAng) * r
  44.             tempPoly1(j).y = oy + SIN(j * (_PI(2 / polySides)) + offAng) * r
  45.         NEXT
  46.         FOR j = 0 TO UBOUND(polys) STEP polySides
  47.             FOR k = j TO j + polySides - 1
  48.                 tempPoly2(k - j) = polys(k)
  49.             NEXT
  50.             IF polyCollide(tempPoly1(), tempPoly2()) THEN collided = -1: EXIT FOR
  51.         NEXT
  52.         IF NOT collided THEN
  53.             ' _echo "yes"
  54.             REDIM _PRESERVE polys(UBOUND(polys) + polySides) AS XY
  55.             FOR j = poly_index TO poly_index + polySides - 1
  56.                 polys(j) = tempPoly1(j - poly_index)
  57.             NEXT
  58.             clr~& = midColor(_RGB(255, 255, 0), _RGB(255, 100, 0), map(r, 200, 3, 0, 1))
  59.             drawPoly tempPoly1(), clr~&
  60.             poly_index = poly_index + polySides
  61.         ELSE
  62.             failed = failed + 1
  63.             IF failed > (i * 60) THEN i = i + 1: failed = 0
  64.         END IF
  65.     END IF
  66. LOOP UNTIL r < 3
  67.  
  68. FUNCTION midColor~& (clr1 AS _UNSIGNED LONG, clr2 AS _UNSIGNED LONG, v)
  69.     midColor~& = _RGB(map(v, 0, 1, _RED(clr1), _RED(clr2)), map(v, 0, 1, _GREEN(clr1), _GREEN(clr2)), map(v, 0, 1, _BLUE(clr1), _BLUE(clr2)))
  70.  
  71. SUB drawPoly (vert() AS XY, clr AS _UNSIGNED LONG)
  72.     n = UBOUND(vert)
  73.     DIM cx, cy
  74.     FOR i = 0 TO n
  75.         LINE (vert(i).x, vert(i).y)-(vert((i + 1) MOD (n + 1)).x, vert((i + 1) MOD (n + 1)).y), clr
  76.         cx = cx + vert(i).x: cy = cy + vert(i).y
  77.     NEXT
  78.     cx = cx / (n + 1): cy = cy / (n + 1)
  79.     PAINT (cx, cy), clr, clr
  80.  
  81.  
  82. FUNCTION polyCollide (vert1() AS XY, vert2() AS XY)
  83.     DIM n1 AS INTEGER, n2 AS INTEGER
  84.     n1 = UBOUND(vert1): n2 = UBOUND(vert2)
  85.     'checking if any point of polygon 1 inside polygon 2
  86.     FOR i = 0 TO n1
  87.         IF pointInsidePoly(vert1(i), vert2()) THEN polyCollide = -1: EXIT FUNCTION
  88.     NEXT
  89.     'checking if any point of polygon 2 inside polygon 1
  90.     FOR i = 0 TO n2
  91.         IF pointInsidePoly(vert2(i), vert1()) THEN polyCollide = -1: EXIT FUNCTION
  92.     NEXT
  93.     ' checking the edge intersection
  94.     FOR i = 0 TO n1
  95.         FOR j = 0 TO n2
  96.             IF lineIntersect(vert1(i).x, vert1(i).y, vert1((i + 1) MOD (n1 + 1)).x, vert1((i + 1) MOD (n1 + 1)).y, vert2(j).x, vert2(j).y, vert2((j + 1) MOD (n2 + 1)).x, vert2((j + 1) MOD (n2 + 1)).y) THEN polyCollide = -1: EXIT FUNCTION
  97.         NEXT
  98.     NEXT
  99.  
  100. 'from paulbourke.net
  101. FUNCTION pointInsidePoly (p AS XY, polyVert() AS XY)
  102.     DIM ax1, ax2, ay1, ay2
  103.     n = UBOUND(polyVert)
  104.     FOR i = 0 TO n
  105.         ax1 = polyVert(i).x - p.x
  106.         ay1 = polyVert(i).y - p.y
  107.         ax2 = polyVert((i + 1) MOD (n + 1)).x - p.x
  108.         ay2 = polyVert((i + 1) MOD (n + 1)).y - p.y
  109.         ang = ang + angle2D(ax1, ay1, ax2, ay2)
  110.     NEXT
  111.     pointInsidePoly = (ABS(ang) >= _PI)
  112.  
  113. 'from paulbourke.net
  114. FUNCTION angle2D (x1, y1, x2, y2)
  115.     DIM theta1, theta2, ftheta
  116.     theta1 = _ATAN2(y1, x1)
  117.     theta2 = _ATAN2(y2, x2)
  118.     ftheta = theta1 - theta2
  119.     WHILE ftheta > _PI: ftheta = ftheta - _PI(2): WEND
  120.     WHILE ftheta < -_PI: ftheta = ftheta + _PI(2): WEND
  121.     angle2D = ftheta
  122.  
  123. FUNCTION lineIntersect (__x1, __y1, __x2, __y2, __x3, __y3, __x4, __y4)
  124.     DIM x1 AS INTEGER, y1 AS INTEGER, x2 AS INTEGER, y2 AS INTEGER, x3 AS INTEGER, y3 AS INTEGER, x4 AS INTEGER, y4 AS INTEGER
  125.     DIM a1, b1, c1, a2, b2, c2
  126.     DIM a1b2_minus_a2b1, b1c2_minus_b2c1, a2c1_minus_a1c2
  127.     DIM inter_x AS INTEGER, inter_y AS INTEGER
  128.  
  129.     x1 = __x1: x2 = __x2: x3 = __x3: x4 = __x4
  130.     y1 = __y1: y2 = __y2: y3 = __y3: y4 = __y4
  131.    
  132.     IF x1 > x2 THEN SWAP x1, x2
  133.     IF x3 > x4 THEN SWAP x3, x4
  134.     IF y1 > y2 THEN SWAP y1, y2
  135.     IF y3 > y4 THEN SWAP y3, y4
  136.    
  137.     a1 = __y2 - __y1: b1 = -(__x2 - __x1): c1 = (__x2 * __y1) - (__x1 * __y2)
  138.     a2 = __y4 - __y3: b2 = -(__x4 - __x3): c2 = (__x4 * __y3) - (__x3 * __y4)
  139.  
  140.     'check if lines are perfectly vertical or horizontal
  141.     IF a1 = 0 AND a2 = 0 THEN lineIntersect = (__y1 = __y3): _DEST 0: EXIT FUNCTION
  142.     IF b1 = 0 AND b2 = 0 THEN lineIntersect = (__x1 = __x3): _DEST 0: EXIT FUNCTION
  143.     IF a2 = 0 OR b2 = 0 THEN GOTO skip_component_ratio
  144.    
  145.     'check if the whole line segments coincide with each other.
  146.     IF c2 <> 0 THEN
  147.         IF (a1 / a2) = (b1 / b2) AND (b1 / b2) = (c1 / c2) THEN
  148.             lineIntersect = -1: EXIT FUNCTION
  149.         END IF
  150.     END IF
  151.     skip_component_ratio:
  152.     'check if the line segments have same slope.
  153.     IF a1 * b2 = a2 * b1 THEN
  154.         lineIntersect = (c1 = c2) 'special case, when they still coincide (as c1=c2)
  155.         EXIT FUNCTION
  156.     END IF
  157.     'check if the line do intersect between the segments.
  158.     a1b2_minus_a2b1 = (a1 * b2) - (a2 * b1): b1c2_minus_b2c1 = (b1 * c2) - (b2 * c1): a2c1_minus_a1c2 = (a2 * c1) - (a1 * c2)
  159.     inter_x = b1c2_minus_b2c1 / a1b2_minus_a2b1: inter_y = a2c1_minus_a1c2 / a1b2_minus_a2b1
  160.    
  161.     lineIntersect = ((inter_x >= x1 AND inter_x <= x2 AND inter_y >= y1 AND inter_y <= y2)) and ((inter_x >= x3 AND inter_x <= x4 AND inter_y >= y3 AND inter_y <= y4))
  162.  
  163. 'taken from p5js.bas
  164. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  165.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  166.  
  167. FUNCTION p5random! (mn!, mx!)
  168.     IF mn! > mx! THEN
  169.         SWAP mn!, mx!
  170.     END IF
  171.     p5random! = RND * (mx! - mn!) + mn!
  172.  


Screenshot_1.png

Program output for the input value of 3, i.e. for a triangle.

11
Programs / AI : Digit Recognizer
« on: April 13, 2020, 10:01:42 am »
Hi everyone! I have been working on this program for a while.. I decided to share with you.
What does the program do?
You have to SLOWLY draw number with single stroke. The program will try to guess what number wrote.
It will work great if you have awesome handwriting!!

The program can not run without "trained files". Yes, it was made it to learn.

The attachment contain the required files along with the source code. Please make sure that the "trained files" are in the same directory
with the source code.

Download the zip from attachment.


Here are some of the screenshots of running program -

Screenshot_1.png


 
Screenshot_4.png


 
Screenshot_5.png


12
Programs / Re: a test or experiment in epidemiology
« on: March 24, 2020, 05:09:55 am »
Note: This message is awaiting approval by a moderator.
According to your program, it will take 32 days to end people around 7 billion.
Quite dangerous.

13
Programs / Re: Time/Date Functions
« on: March 23, 2020, 08:45:28 am »
Note: This message is awaiting approval by a moderator.
Hi! It runs fine.

14
Programs / Re: Not again! Another Time Piece
« on: March 16, 2020, 07:21:05 am »
Note: This message is awaiting approval by a moderator.
Oh WOW! I never knew that last digit of Fibonacci had any pattern.

15
Programs / 3D Graph Plotter
« on: March 13, 2020, 07:02:50 am »
Hi everyone!
This program input the expression for Z =  something and plot the graph in 3D "something" consist of terms having X and Z variables or constant. For example X+Y or X+Y*5 + 10, etc. It even support trigonometric functions.

Thanks to @STxAxTIC and @FellippeHeitor

Controls : Click & Drag on screen with mouse for rotation.

Note : You need to download additional sxript.bi & sxript.bm from attachment.

EDIT: Code updated. Now, it accept expression for Z = ... which is generally accepted.
Code: QB64: [Select]
  1. '##############################################################################################
  2. '3D Grapher By Ashish Kushwaha
  3. '----------------------------------------------------------------------------------------------
  4. '* Thanks to STxAxTIC. Without his sxript, coding this would be harder.
  5. '* Thanks to FellipeHeitor. His INPUTBOX() come handy when I need QB64 Input & OpenGL together.
  6. '----------------------------------------------------------------------------------------------
  7. 'Description: Give an expression for z = ... containing terms of x, y (any power) & constants
  8. 'With the power of sxript, it also support *trigonometric functions* in the expression.
  9. 'Click on Ok. Then the Graph is plotted in 3D Space & shown in 2D screen.
  10. 'Drag on screen with mouse for rotation.
  11. '----------------------------------------------------------------------------------------------
  12. 'Friday the 13th, 2020
  13.  
  14. '$INCLUDE:'sxript.bi'
  15.  
  16.     SUB gluLookAt (BYVAL eyeX#, BYVAL eyeY#, BYVAL eyeZ#, BYVAL centerX#, BYVAL centerY#, BYVAL centerZ#, BYVAL upX#, BYVAL upY#, BYVAL upZ#)
  17.  
  18.  
  19. _TITLE "3D Grapher"
  20. SCREEN _NEWIMAGE(600, 600, 32)
  21.  
  22. dummy = INPUTBOX("Enter the expression for Z = ", "Enter the expression for Z = (ex. X*Y)", "X+Y-1", e$, -1)
  23. IF dummy = 2 THEN SYSTEM
  24. PRINT "Generating... Just a moment"
  25. TYPE rgb
  26.     r AS SINGLE
  27.     g AS SINGLE
  28.     b AS SINGLE
  29. DIM SHARED vert(100, 100), glAllow, xRot, yRot, colArr(100, 100) AS rgb
  30.  
  31. FOR x = -50 TO 50
  32.     FOR z = -50 TO 50
  33.         expression$ = ""
  34.         FOR i = 1 TO LEN(e$)
  35.             ca$ = MID$(e$, i, 1)
  36.             IF LCASE$(ca$) = "x" THEN ca$ = _TRIM$(STR$(x / 10))
  37.             IF LCASE$(ca$) = "y" THEN ca$ = _TRIM$(STR$(z / 10))
  38.             expression$ = expression$ + ca$
  39.         NEXT
  40.         vert(x + 50, z + 50) = VAL(SxriptEval(expression$))
  41.         'PRINT expression$, VAL(SxriptEval(expression$))
  42.         'SLEEP
  43.         c~& = hsb(map(z, -50, 50, 0, 255), 255, 128, 255)
  44.         colArr(x + 50, z + 50).r = _RED(c~&) / 255
  45.         colArr(x + 50, z + 50).g = _GREEN(c~&) / 255
  46.         colArr(x + 50, z + 50).b = _BLUE(c~&) / 255
  47.     NEXT
  48. glAllow = 1
  49. 'SLEEP
  50.         x = _MOUSEX: y = _MOUSEY
  51.         WHILE _MOUSEBUTTON(1)
  52.             WHILE _MOUSEINPUT: WEND
  53.             yRot = yRot + (_MOUSEX - x)
  54.             xRot = xRot + (_MOUSEY - y)
  55.             x = _MOUSEX: y = _MOUSEY
  56.         WEND
  57.     END IF
  58.     _LIMIT 60
  59.  
  60. SUB _GL () STATIC
  61.     IF glAllow = 0 THEN EXIT SUB
  62.  
  63.     _glClear _GL_COLOR_BUFFER_BIT OR _GL_DEPTH_BUFFER_BIT
  64.     _glEnable _GL_DEPTH_TEST
  65.     _glEnable _GL_BLEND
  66.  
  67.  
  68.     _glMatrixMode _GL_PROJECTION
  69.     _gluPerspective 50, 1, 0.1, 40
  70.  
  71.     _glMatrixMode _GL_MODELVIEW
  72.  
  73.     gluLookAt 0, 7, 15, 0, 0, 0, 0, 1, 0
  74.     _glRotatef xRot, 1, 0, 0
  75.     _glRotatef yRot, 0, 1, 0
  76.     _glLineWidth 2.0
  77.     'draw axis
  78.     _glBegin _GL_LINES
  79.     'x-axis
  80.     _glColor3f 1, 0, 0
  81.     _glVertex3f -5, 0, 0
  82.     _glVertex3f 5, 0, 0
  83.     'z-axis
  84.     _glColor3f 0, 1, 0
  85.     _glVertex3f 0, -5, 0
  86.     _glVertex3f 0, 5, 0
  87.     'y-axis
  88.     _glColor3f 0, 0, 1
  89.     _glVertex3f 0, 0, -5
  90.     _glVertex3f 0, 0, 5
  91.  
  92.     _glEnd
  93.     _glColor3f 1, 1, 1
  94.     _glLineWidth 1.0
  95.     FOR z = -50 TO 49
  96.         _glBegin _GL_TRIANGLE_STRIP
  97.         FOR x = -50 TO 50
  98.             _glColor4f colArr(x + 50, z + 50).r, colArr(x + 50, z + 50).g, colArr(x + 50, z + 50).b, 0.7
  99.             _glVertex3f map(x, -50, 50, -5, 5), vert(x + 50, z + 50), map(z, -50, 50, 5, -5)
  100.             _glVertex3f map(x, -50, 50, -5, 5), vert(x + 50, z + 51), map(z + 1, -50, 50, 5, -5)
  101.         NEXT
  102.         _glEnd
  103.     NEXT
  104.  
  105.  
  106.  
  107. 'By Fellipe Heitor
  108. FUNCTION INPUTBOX (tTitle$, tMessage$, InitialValue AS STRING, NewValue AS STRING, Selected)
  109.     'INPUTBOX ---------------------------------------------------------------------
  110.     'Show a dialog and allow user input. Returns 1 = OK or 2 = Cancel.            '
  111.     '                                                                             '
  112.     '- tTitle$ is the desired dialog title. If not provided, it'll be "Input"     '
  113.     '                                                                             '
  114.     '- tMessage$ is the prompt that'll be shown to the user. You can show         '
  115.     '   a multiline message by adding line breaks with CHR$(10).                  '
  116.     '                                                                             '
  117.     ' - InitialValue can be passed both as a string literal or as a variable.     '
  118.     '                                                                             '
  119.     '- Actual user input is returned by altering NewValue, so it must be          '
  120.     '   passed as a variable.                                                     '
  121.     '                                                                             '
  122.     '- Selected indicates wheter the initial value will be preselected when the   '
  123.     '   dialog is first shown. -1 preselects the whole text; positive values      '
  124.     '   select only part of the initial value (from the character position passed '
  125.     '   to the end of the initial value).                                         '
  126.     '                                                                             '
  127.     'Intended for use with 32-bit screen modes.                                   '
  128.     '------------------------------------------------------------------------------
  129.  
  130.     'Variable declaration:
  131.     DIM Message$, Title$, CharW AS INTEGER, MaxLen AS INTEGER
  132.     DIM lineBreak AS INTEGER, totalLines AS INTEGER, prevlinebreak AS INTEGER
  133.     DIM Cursor AS INTEGER, Selection.Start AS INTEGER, InputViewStart AS INTEGER
  134.     DIM FieldArea AS INTEGER, DialogH AS INTEGER, DialogW AS INTEGER
  135.     DIM DialogX AS INTEGER, DialogY AS INTEGER, InputField.X AS INTEGER
  136.     DIM TotalButtons AS INTEGER, B AS INTEGER, ButtonLine$
  137.     DIM cb AS INTEGER, DIALOGRESULT AS INTEGER, i AS INTEGER
  138.     DIM message.X AS INTEGER, SetCursor#, cursorBlink%
  139.     DIM DefaultButton AS INTEGER, k AS LONG
  140.     DIM shiftDown AS _BYTE, ctrlDown AS _BYTE, Clip$
  141.     DIM FindLF%, s1 AS INTEGER, s2 AS INTEGER
  142.     DIM Selection.Value$
  143.     DIM prevCursor AS INTEGER, ss1 AS INTEGER, ss2 AS INTEGER, mb AS _BYTE
  144.     DIM mx AS INTEGER, my AS INTEGER, nmx AS INTEGER, nmy AS INTEGER
  145.     DIM FGColor AS LONG, BGColor AS LONG
  146.  
  147.     'Data type used for the dialog buttons:
  148.     TYPE BUTTONSTYPE
  149.         ID AS LONG
  150.         CAPTION AS STRING * 120
  151.         X AS INTEGER
  152.         Y AS INTEGER
  153.         W AS INTEGER
  154.     END TYPE
  155.  
  156.     'Color constants. You can customize colors by changing these:
  157.     CONST TitleBarColor = _RGB32(0, 178, 179)
  158.     CONST DialogBGColor = _RGB32(255, 255, 255)
  159.     CONST TitleBarTextColor = _RGB32(0, 0, 0)
  160.     CONST DialogTextColor = _RGB32(0, 0, 0)
  161.     CONST InputFieldColor = _RGB32(200, 200, 200)
  162.     CONST InputFieldTextColor = _RGB32(0, 0, 0)
  163.     CONST SelectionColor = _RGBA32(127, 127, 127, 100)
  164.  
  165.     'Initial variable setup:
  166.     Message$ = tMessage$
  167.     Title$ = RTRIM$(LTRIM$(tTitle$))
  168.     IF Title$ = "" THEN Title$ = "Input"
  169.     NewValue = RTRIM$(LTRIM$(InitialValue))
  170.     DefaultButton = 1
  171.  
  172.     'Save the current drawing page so it can be restored later:
  173.     FGColor = _DEFAULTCOLOR
  174.     BGColor = _BACKGROUNDCOLOR
  175.     PCOPY 0, 1
  176.  
  177.     'Figure out the print width of a single character (in case user has a custom font applied)
  178.     CharW = _PRINTWIDTH("_")
  179.  
  180.     'Place a color overlay over the old screen image so the focus is on the dialog:
  181.     LINE (0, 0)-STEP(_WIDTH - 1, _HEIGHT - 1), _RGBA32(170, 170, 170, 170), BF
  182.  
  183.     'Message breakdown, in case CHR$(10) was used as line break:
  184.     REDIM MessageLines(1) AS STRING
  185.     MaxLen = 1
  186.     DO
  187.         lineBreak = INSTR(lineBreak + 1, Message$, CHR$(10))
  188.         IF lineBreak = 0 AND totalLines = 0 THEN
  189.             totalLines = 1
  190.             MessageLines(1) = Message$
  191.             MaxLen = LEN(Message$)
  192.             EXIT DO
  193.         ELSEIF lineBreak = 0 AND totalLines > 0 THEN
  194.             totalLines = totalLines + 1
  195.             REDIM _PRESERVE MessageLines(1 TO totalLines) AS STRING
  196.             MessageLines(totalLines) = RIGHT$(Message$, LEN(Message$) - prevlinebreak + 1)
  197.             IF LEN(MessageLines(totalLines)) > MaxLen THEN MaxLen = LEN(MessageLines(totalLines))
  198.             EXIT DO
  199.         END IF
  200.         IF totalLines = 0 THEN prevlinebreak = 1
  201.         totalLines = totalLines + 1
  202.         REDIM _PRESERVE MessageLines(1 TO totalLines) AS STRING
  203.         MessageLines(totalLines) = MID$(Message$, prevlinebreak, lineBreak - prevlinebreak)
  204.         IF LEN(MessageLines(totalLines)) > MaxLen THEN MaxLen = LEN(MessageLines(totalLines))
  205.         prevlinebreak = lineBreak + 1
  206.     LOOP
  207.  
  208.     Cursor = LEN(NewValue)
  209.     Selection.Start = 0
  210.     InputViewStart = 1
  211.     FieldArea = _WIDTH \ CharW - 4
  212.     IF FieldArea > 62 THEN FieldArea = 62
  213.     IF Selected > 0 THEN Selection.Start = Selected: Selected = -1
  214.  
  215.     'Calculate dialog dimensions and print coordinates:
  216.     DialogH = _FONTHEIGHT * (6 + totalLines) + 10
  217.     DialogW = (CharW * FieldArea) + 10
  218.     IF DialogW < MaxLen * CharW + 10 THEN DialogW = MaxLen * CharW + 10
  219.  
  220.     DialogX = _WIDTH / 2 - DialogW / 2
  221.     DialogY = _HEIGHT / 2 - DialogH / 2
  222.     InputField.X = (DialogX + (DialogW / 2)) - (((FieldArea * CharW) - 10) / 2) - 4
  223.  
  224.     'Calculate button's print coordinates:
  225.     TotalButtons = 2
  226.     DIM Buttons(1 TO TotalButtons) AS BUTTONSTYPE
  227.     B = 1
  228.     Buttons(B).ID = 1: Buttons(B).CAPTION = "< OK >": B = B + 1
  229.     Buttons(B).ID = 2: Buttons(B).CAPTION = "< Cancel >": B = B + 1
  230.     ButtonLine$ = " "
  231.     FOR cb = 1 TO TotalButtons
  232.         ButtonLine$ = ButtonLine$ + RTRIM$(LTRIM$(Buttons(cb).CAPTION)) + " "
  233.         Buttons(cb).Y = DialogY + 5 + _FONTHEIGHT * (5 + totalLines)
  234.         Buttons(cb).W = _PRINTWIDTH(RTRIM$(LTRIM$(Buttons(cb).CAPTION)))
  235.     NEXT cb
  236.     Buttons(1).X = _WIDTH / 2 - _PRINTWIDTH(ButtonLine$) / 2
  237.     FOR cb = 2 TO TotalButtons
  238.         Buttons(cb).X = Buttons(1).X + _PRINTWIDTH(SPACE$(INSTR(ButtonLine$, RTRIM$(LTRIM$(Buttons(cb).CAPTION)))))
  239.     NEXT cb
  240.  
  241.     'Main loop:
  242.     DIALOGRESULT = 0
  243.     _KEYCLEAR
  244.     DO: _LIMIT 500
  245.         'Draw the dialog.
  246.         LINE (DialogX, DialogY)-STEP(DialogW - 1, DialogH - 1), DialogBGColor, BF
  247.         LINE (DialogX, DialogY)-STEP(DialogW - 1, _FONTHEIGHT + 1), TitleBarColor, BF
  248.         COLOR TitleBarTextColor
  249.         _PRINTSTRING (_WIDTH / 2 - _PRINTWIDTH(Title$) / 2, DialogY + 1), Title$
  250.  
  251.         COLOR DialogTextColor, _RGBA32(0, 0, 0, 0)
  252.         FOR i = 1 TO totalLines
  253.             message.X = _WIDTH / 2 - _PRINTWIDTH(MessageLines(i)) / 2
  254.             _PRINTSTRING (message.X, DialogY + 5 + _FONTHEIGHT * (i + 1)), MessageLines(i)
  255.         NEXT i
  256.  
  257.         'Draw the input field
  258.         LINE (InputField.X - 2, DialogY + 3 + _FONTHEIGHT * (3 + totalLines))-STEP(FieldArea * CharW, _FONTHEIGHT + 4), InputFieldColor, BF
  259.         COLOR InputFieldTextColor
  260.         _PRINTSTRING (InputField.X, DialogY + 5 + _FONTHEIGHT * (3 + totalLines)), MID$(NewValue, InputViewStart, FieldArea)
  261.  
  262.         'Selection highlight:
  263.         GOSUB SelectionHighlight
  264.  
  265.         'Cursor blink:
  266.         IF TIMER - SetCursor# > .4 THEN
  267.             SetCursor# = TIMER
  268.             IF cursorBlink% = 1 THEN cursorBlink% = 0 ELSE cursorBlink% = 1
  269.         END IF
  270.         IF cursorBlink% = 1 THEN
  271.             LINE (InputField.X + (Cursor - (InputViewStart - 1)) * CharW, DialogY + 5 + _FONTHEIGHT * (3 + totalLines))-STEP(0, _FONTHEIGHT), _RGB32(0, 0, 0)
  272.         END IF
  273.  
  274.         'Check if buttons have been clicked or are being hovered:
  275.         GOSUB CheckButtons
  276.  
  277.         'Draw buttons:
  278.         FOR cb = 1 TO TotalButtons
  279.             _PRINTSTRING (Buttons(cb).X, Buttons(cb).Y), RTRIM$(LTRIM$(Buttons(cb).CAPTION))
  280.             IF cb = DefaultButton THEN
  281.                 COLOR _RGB32(255, 255, 0)
  282.                 _PRINTSTRING (Buttons(cb).X, Buttons(cb).Y), "<" + SPACE$(LEN(RTRIM$(LTRIM$(Buttons(cb).CAPTION))) - 2) + ">"
  283.                 COLOR _RGB32(0, 178, 179)
  284.                 _PRINTSTRING (Buttons(cb).X - 1, Buttons(cb).Y - 1), "<" + SPACE$(LEN(RTRIM$(LTRIM$(Buttons(cb).CAPTION))) - 2) + ">"
  285.                 COLOR _RGB32(0, 0, 0)
  286.             END IF
  287.         NEXT cb
  288.  
  289.         _DISPLAY
  290.  
  291.         'Process input:
  292.         k = _KEYHIT
  293.         IF k = 100303 OR k = 100304 THEN shiftDown = -1
  294.         IF k = -100303 OR k = -100304 THEN shiftDown = 0
  295.         IF k = 100305 OR k = 100306 THEN ctrlDown = -1
  296.         IF k = -100305 OR k = -100306 THEN ctrlDown = 0
  297.  
  298.         SELECT CASE k
  299.             CASE 13: DIALOGRESULT = 1
  300.             CASE 27: DIALOGRESULT = 2
  301.             CASE 32 TO 126 'Printable ASCII characters
  302.                 IF k = ASC("v") OR k = ASC("V") THEN 'Paste from clipboard (Ctrl+V)
  303.                     IF ctrlDown THEN
  304.                         Clip$ = _CLIPBOARD$
  305.                         FindLF% = INSTR(Clip$, CHR$(13))
  306.                         IF FindLF% > 0 THEN Clip$ = LEFT$(Clip$, FindLF% - 1)
  307.                         FindLF% = INSTR(Clip$, CHR$(10))
  308.                         IF FindLF% > 0 THEN Clip$ = LEFT$(Clip$, FindLF% - 1)
  309.                         IF LEN(RTRIM$(LTRIM$(Clip$))) > 0 THEN
  310.                             IF NOT Selected THEN
  311.                                 IF Cursor = LEN(NewValue) THEN
  312.                                     NewValue = NewValue + Clip$
  313.                                     Cursor = LEN(NewValue)
  314.                                 ELSE
  315.                                     NewValue = LEFT$(NewValue, Cursor) + Clip$ + MID$(NewValue, Cursor + 1)
  316.                                     Cursor = Cursor + LEN(Clip$)
  317.                                 END IF
  318.                             ELSE
  319.                                 s1 = Selection.Start
  320.                                 s2 = Cursor
  321.                                 IF s1 > s2 THEN SWAP s1, s2
  322.                                 NewValue = LEFT$(NewValue, s1) + Clip$ + MID$(NewValue, s2 + 1)
  323.                                 Cursor = s1 + LEN(Clip$)
  324.                                 Selected = 0
  325.                             END IF
  326.                         END IF
  327.                         k = 0
  328.                     END IF
  329.                 ELSEIF k = ASC("c") OR k = ASC("C") THEN 'Copy selection to clipboard (Ctrl+C)
  330.                     IF ctrlDown THEN
  331.                         _CLIPBOARD$ = Selection.Value$
  332.                         k = 0
  333.                     END IF
  334.                 ELSEIF k = ASC("x") OR k = ASC("X") THEN 'Cut selection to clipboard (Ctrl+X)
  335.                     IF ctrlDown THEN
  336.                         _CLIPBOARD$ = Selection.Value$
  337.                         GOSUB DeleteSelection
  338.                         k = 0
  339.                     END IF
  340.                 ELSEIF k = ASC("a") OR k = ASC("A") THEN 'Select all text (Ctrl+A)
  341.                     IF ctrlDown THEN
  342.                         Cursor = LEN(NewValue)
  343.                         Selection.Start = 0
  344.                         Selected = -1
  345.                         k = 0
  346.                     END IF
  347.                 END IF
  348.  
  349.                 IF k > 0 THEN
  350.                     IF NOT Selected THEN
  351.                         IF Cursor = LEN(NewValue) THEN
  352.                             NewValue = NewValue + CHR$(k)
  353.                             Cursor = Cursor + 1
  354.                         ELSE
  355.                             NewValue = LEFT$(NewValue, Cursor) + CHR$(k) + MID$(NewValue, Cursor + 1)
  356.                             Cursor = Cursor + 1
  357.                         END IF
  358.                         IF Cursor > FieldArea THEN InputViewStart = (Cursor - FieldArea) + 2
  359.                     ELSE
  360.                         s1 = Selection.Start
  361.                         s2 = Cursor
  362.                         IF s1 > s2 THEN SWAP s1, s2
  363.                         NewValue = LEFT$(NewValue, s1) + CHR$(k) + MID$(NewValue, s2 + 1)
  364.                         Selected = 0
  365.                         Cursor = s1 + 1
  366.                     END IF
  367.                 END IF
  368.             CASE 8 'Backspace
  369.                 IF LEN(NewValue) > 0 THEN
  370.                     IF NOT Selected THEN
  371.                         IF Cursor = LEN(NewValue) THEN
  372.                             NewValue = LEFT$(NewValue, LEN(NewValue) - 1)
  373.                             Cursor = Cursor - 1
  374.                         ELSEIF Cursor > 1 THEN
  375.                             NewValue = LEFT$(NewValue, Cursor - 1) + MID$(NewValue, Cursor + 1)
  376.                             Cursor = Cursor - 1
  377.                         ELSEIF Cursor = 1 THEN
  378.                             NewValue = RIGHT$(NewValue, LEN(NewValue) - 1)
  379.                             Cursor = Cursor - 1
  380.                         END IF
  381.                     ELSE
  382.                         GOSUB DeleteSelection
  383.                     END IF
  384.                 END IF
  385.             CASE 21248 'Delete
  386.                 IF NOT Selected THEN
  387.                     IF LEN(NewValue) > 0 THEN
  388.                         IF Cursor = 0 THEN
  389.                             NewValue = RIGHT$(NewValue, LEN(NewValue) - 1)
  390.                         ELSEIF Cursor > 0 AND Cursor <= LEN(NewValue) - 1 THEN
  391.                             NewValue = LEFT$(NewValue, Cursor) + MID$(NewValue, Cursor + 2)
  392.                         END IF
  393.                     END IF
  394.                 ELSE
  395.                     GOSUB DeleteSelection
  396.                 END IF
  397.             CASE 19200 'Left arrow key
  398.                 GOSUB CheckSelection
  399.                 IF Cursor > 0 THEN Cursor = Cursor - 1
  400.             CASE 19712 'Right arrow key
  401.                 GOSUB CheckSelection
  402.                 IF Cursor < LEN(NewValue) THEN Cursor = Cursor + 1
  403.             CASE 18176 'Home
  404.                 GOSUB CheckSelection
  405.                 Cursor = 0
  406.             CASE 20224 'End
  407.                 GOSUB CheckSelection
  408.                 Cursor = LEN(NewValue)
  409.         END SELECT
  410.  
  411.         'Cursor adjustments:
  412.         GOSUB CursorAdjustments
  413.     LOOP UNTIL DIALOGRESULT > 0
  414.  
  415.     _KEYCLEAR
  416.     INPUTBOX = DIALOGRESULT
  417.  
  418.     'Restore previous display:
  419.     PCOPY 1, 0
  420.     COLOR FGColor, BGColor
  421.     EXIT SUB
  422.  
  423.     CursorAdjustments:
  424.     IF Cursor > prevCursor THEN
  425.         IF Cursor - InputViewStart + 2 > FieldArea THEN InputViewStart = (Cursor - FieldArea) + 2
  426.     ELSEIF Cursor < prevCursor THEN
  427.         IF Cursor < InputViewStart - 1 THEN InputViewStart = Cursor
  428.     END IF
  429.     prevCursor = Cursor
  430.     IF InputViewStart < 1 THEN InputViewStart = 1
  431.     RETURN
  432.  
  433.     CheckSelection:
  434.     IF shiftDown = -1 THEN
  435.         IF Selected = 0 THEN
  436.             Selected = -1
  437.             Selection.Start = Cursor
  438.         END IF
  439.     ELSEIF shiftDown = 0 THEN
  440.         Selected = 0
  441.     END IF
  442.     RETURN
  443.  
  444.     DeleteSelection:
  445.     NewValue = LEFT$(NewValue, s1) + MID$(NewValue, s2 + 1)
  446.     Selected = 0
  447.     Cursor = s1
  448.     RETURN
  449.  
  450.     SelectionHighlight:
  451.     IF Selected THEN
  452.         s1 = Selection.Start
  453.         s2 = Cursor
  454.         IF s1 > s2 THEN
  455.             SWAP s1, s2
  456.             IF InputViewStart > 1 THEN
  457.                 ss1 = s1 - InputViewStart + 1
  458.             ELSE
  459.                 ss1 = s1
  460.             END IF
  461.             ss2 = s2 - s1
  462.             IF ss1 + ss2 > FieldArea THEN ss2 = FieldArea - ss1
  463.         ELSE
  464.             ss1 = s1
  465.             ss2 = s2 - s1
  466.             IF ss1 < InputViewStart THEN ss1 = 0: ss2 = s2 - InputViewStart + 1
  467.             IF ss1 > InputViewStart THEN ss1 = ss1 - InputViewStart + 1: ss2 = s2 - s1
  468.         END IF
  469.         Selection.Value$ = MID$(NewValue, s1 + 1, s2 - s1)
  470.  
  471.         LINE (InputField.X + ss1 * CharW, DialogY + 5 + _FONTHEIGHT * (3 + totalLines))-STEP(ss2 * CharW, _FONTHEIGHT), _RGBA32(255, 255, 255, 150), BF
  472.     END IF
  473.     RETURN
  474.  
  475.     CheckButtons:
  476.     'Hover highlight:
  477.     mb = _MOUSEBUTTON(1): mx = _MOUSEX: my = _MOUSEY
  478.     FOR cb = 1 TO TotalButtons
  479.         IF (mx >= Buttons(cb).X) AND (mx <= Buttons(cb).X + Buttons(cb).W) THEN
  480.             IF (my >= Buttons(cb).Y) AND (my < Buttons(cb).Y + _FONTHEIGHT) THEN
  481.                 LINE (Buttons(cb).X, Buttons(cb).Y)-STEP(Buttons(cb).W, _FONTHEIGHT - 1), _RGBA32(230, 230, 230, 235), BF
  482.             END IF
  483.         END IF
  484.     NEXT cb
  485.  
  486.     IF mb THEN
  487.         IF mx >= InputField.X AND my >= DialogY + 3 + _FONTHEIGHT * (3 + totalLines) AND mx <= InputField.X + (FieldArea * CharW - 10) AND my <= DialogY + 3 + _FONTHEIGHT * (3 + totalLines) + _FONTHEIGHT + 4 THEN
  488.             'Clicking inside the text field positions the cursor
  489.             WHILE _MOUSEBUTTON(1)
  490.                 _LIMIT 500
  491.                 mb = _MOUSEINPUT
  492.             WEND
  493.             Cursor = ((mx - InputField.X) / CharW) + (InputViewStart - 1)
  494.             IF Cursor > LEN(NewValue) THEN Cursor = LEN(NewValue)
  495.             Selected = 0
  496.             RETURN
  497.         END IF
  498.  
  499.         FOR cb = 1 TO TotalButtons
  500.             IF (mx >= Buttons(cb).X) AND (mx <= Buttons(cb).X + Buttons(cb).W) THEN
  501.                 IF (my >= Buttons(cb).Y) AND (my < Buttons(cb).Y + _FONTHEIGHT) THEN
  502.                     DefaultButton = cb
  503.                     WHILE _MOUSEBUTTON(1): _LIMIT 500: mb = _MOUSEINPUT: WEND
  504.                     mb = 0: nmx = _MOUSEX: nmy = _MOUSEY
  505.                     IF nmx = mx AND nmy = my THEN DIALOGRESULT = cb
  506.                     RETURN
  507.                 END IF
  508.             END IF
  509.         NEXT cb
  510.     END IF
  511.     RETURN
  512.  
  513.  
  514. 'method adapted form http://stackoverflow.com/questions/4106363/converting-rgb-to-hsb-colors
  515. FUNCTION hsb~& (__H AS _FLOAT, __S AS _FLOAT, __B AS _FLOAT, A AS _FLOAT)
  516.     DIM H AS _FLOAT, S AS _FLOAT, B AS _FLOAT
  517.  
  518.     H = map(__H, 0, 255, 0, 360)
  519.     S = map(__S, 0, 255, 0, 1)
  520.     B = map(__B, 0, 255, 0, 1)
  521.  
  522.     IF S = 0 THEN
  523.         hsb~& = _RGBA32(B * 255, B * 255, B * 255, A)
  524.         EXIT FUNCTION
  525.     END IF
  526.  
  527.     DIM fmx AS _FLOAT, fmn AS _FLOAT
  528.     DIM fmd AS _FLOAT, iSextant AS INTEGER
  529.     DIM imx AS INTEGER, imd AS INTEGER, imn AS INTEGER
  530.  
  531.     IF B > .5 THEN
  532.         fmx = B - (B * S) + S
  533.         fmn = B + (B * S) - S
  534.     ELSE
  535.         fmx = B + (B * S)
  536.         fmn = B - (B * S)
  537.     END IF
  538.  
  539.     iSextant = INT(H / 60)
  540.  
  541.     IF H >= 300 THEN
  542.         H = H - 360
  543.     END IF
  544.  
  545.     H = H / 60
  546.     H = H - (2 * INT(((iSextant + 1) MOD 6) / 2))
  547.  
  548.     IF iSextant MOD 2 = 0 THEN
  549.         fmd = (H * (fmx - fmn)) + fmn
  550.     ELSE
  551.         fmd = fmn - (H * (fmx - fmn))
  552.     END IF
  553.  
  554.     imx = _ROUND(fmx * 255)
  555.     imd = _ROUND(fmd * 255)
  556.     imn = _ROUND(fmn * 255)
  557.  
  558.     SELECT CASE INT(iSextant)
  559.         CASE 1
  560.             hsb~& = _RGBA32(imd, imx, imn, A)
  561.         CASE 2
  562.             hsb~& = _RGBA32(imn, imx, imd, A)
  563.         CASE 3
  564.             hsb~& = _RGBA32(imn, imd, imx, A)
  565.         CASE 4
  566.             hsb~& = _RGBA32(imd, imn, imx, A)
  567.         CASE 5
  568.             hsb~& = _RGBA32(imx, imn, imd, A)
  569.         CASE ELSE
  570.             hsb~& = _RGBA32(imx, imd, imn, A)
  571.     END SELECT
  572.  
  573.  
  574.  
  575. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  576.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  577.  
  578.  
  579. '$INCLUDE:'sxript.bm'
  580.  
  581.  


Graph for Z = X+Y-2
 
Screenshot_1.png

Graph for Z = sin(X)+cos(Y)
 
Screenshot_2.png


Pages: [1] 2 3 4