Author Topic: Multiple objects/entities in the program: 2 ways to manage it  (Read 4640 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Multiple objects/entities in the program: 2 ways to manage it
« on: September 27, 2019, 03:13:30 pm »
Hi friends

to continue the discussion  about how controlling different objects/entities on the screen
https://www.qb64.org/forum/index.php?topic=1718.msg109801#msg109801

here my little experience
in this first code posted there is a poor structured and centralized manager of I/O that sends command to the engine of output of the program activating the object selected by user input by keyboard or mouse.

Code: QB64: [Select]
  1. _TITLE "multiple entities on the screen NOT overlapping"
  2. 'this is a little example of the 1st different concepts to manage
  3. 'multiple entities on the screen (or in the program but
  4. ' not visible like a timer or a counter)
  5. '
  6. '
  7. SCREEN _NEWIMAGE(800, 600, 32)
  8. CONST White = _RGBA32(255, 255, 255, 255)
  9. CONST Black = _RGBA32(0, 0, 0, 255)
  10. CONST Blu = _RGBA32(0, 0, 200, 255)
  11. CONST Red = _RGBA32(200, 0, 0, 255)
  12. CONST Green = _RGBA32(0, 200, 0, 255)
  13.  
  14. TYPE btn
  15.     x AS INTEGER
  16.     y AS INTEGER
  17.     H AS INTEGER
  18.     w AS INTEGER
  19.     T AS STRING * 30
  20.  
  21. DIM SHARED Height AS INTEGER, Widht AS INTEGER, Buttons(1 TO 10) AS btn, z AS INTEGER
  22.  
  23. Height = _FONTHEIGHT
  24. Widht = _FONTWIDTH
  25.  
  26. textBase$ = "This is a try to show different" ' 30 chars
  27.  
  28. ' graphic output/interface--------------------------------
  29. LINE (1, 1)-(799, 599), Green, B
  30. MakeScreen textBase$
  31. COLOR Red, Black
  32. _PRINTSTRING (100, 520), " Press from 1 to 10 for activate the button or left click on it"
  33. ' End graphic interface-----------------------------------
  34.  
  35. 'main manager of I/O----------------------------------
  36. ManageInput
  37.  
  38. SUB ManageInput
  39.     K$ = "Null"
  40.     DO WHILE _MOUSEINPUT OR K$ <> "Done"
  41.         ' keyboard manager
  42.         IF K$ = CHR$(27) THEN K$ = "Done"
  43.         IF K$ <> "" THEN
  44.             IF INSTR("123456789", K$) THEN
  45.                 ' command executor
  46.                 DrawButton Buttons(VAL(K$)).x, Buttons(VAL(K$)).y, RTRIM$(Buttons(VAL(K$)).T), Green, Black
  47.                 DO: LOOP UNTIL INKEY$ = ""
  48.                 SLEEP 1
  49.                 drawScreen
  50.             END IF
  51.         END IF
  52.         K$ = INKEY$
  53.         ' mouse manager
  54.         IF _MOUSEBUTTON(2) THEN K$ = "Done"
  55.         IF _MOUSEBUTTON(1) THEN K$ = LTRIM$(STR$(WhatEntity))
  56.     LOOP
  57.  
  58. FUNCTION WhatEntity
  59.     mX = _MOUSEX
  60.     mY = _MOUSEY
  61.     WhatEntity = false
  62.     FOR zz = 1 TO 9
  63.         IF Buttons(zz).x <= mX AND Buttons(zz).x + Buttons(zz).w >= mX THEN
  64.             IF Buttons(zz).y <= mY AND Buttons(zz).y + Buttons(zz).H >= mY THEN
  65.                 WhatEntity = zz
  66.                 EXIT FUNCTION
  67.             END IF
  68.         END IF
  69.     NEXT
  70.  
  71. SUB drawScreen
  72.     FOR zz = 1 TO 9
  73.         DrawButton Buttons(zz).x, Buttons(zz).y, RTRIM$(Buttons(zz).T), Red, Blu
  74.     NEXT
  75.  
  76. SUB MakeScreen (T AS STRING)
  77.     FOR z = 1 TO 9
  78.         MakeButton (RND * 500) + 1, z * 40, RTRIM$(LEFT$(T, (RND * 30) + 1)), Red, Blu
  79.     NEXT
  80.  
  81. SUB MakeButton (X AS INTEGER, Y AS INTEGER, txt AS STRING, Bcolor AS _UNSIGNED LONG, Fcolor AS _UNSIGNED LONG)
  82.     InitButtons X, Y, txt
  83.     DrawButton X, Y, txt, Bcolor, Fcolor
  84.  
  85. SUB DrawButton (X AS INTEGER, Y AS INTEGER, txt AS STRING, Bcolor AS _UNSIGNED LONG, Fcolor AS _UNSIGNED LONG)
  86.     LINE (X, Y)-(X + (Widht * LEN(txt)) + 10, Y + Height + 10), Bcolor, BF
  87.     COLOR Fcolor, Bcolor
  88.     _PRINTSTRING (X + 5, Y + 5), txt
  89.     LINE (X, Y)-(X + (Widht * LEN(txt)) + 10, Y + Height + 10), White, B
  90.  
  91. SUB InitButtons (X AS INTEGER, Y AS INTEGER, T AS STRING)
  92.     Buttons(z).x = X
  93.     Buttons(z).y = Y
  94.     Buttons(z).T = T
  95.     Buttons(z).H = Height
  96.     Buttons(z).w = Widht * LEN(Buttons(z).T)

soon I hope to post the different method in which the behavior of the object is not flagged by general manager but it is managed from into each object.

Thanks to read and try
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Multiple objects/entities in the program: 2 ways to manage it
« Reply #1 on: September 27, 2019, 06:34:57 pm »
Hi
here is the second way that I know to manage multiple objects/entities in the program

in this case the flow of program runs into all interested objects  that manage the input...

Code: QB64: [Select]
  1. _TITLE "multiple entities on the screen NOT overlapping 2"
  2. 'this is a little example of the 1st different concepts to manage
  3. 'multiple entities on the screen (or in the program but
  4. ' not visible like a timer or a counter)
  5. '
  6. '
  7. SCREEN _NEWIMAGE(800, 600, 32)
  8. CONST White = _RGBA32(255, 255, 255, 255)
  9. CONST Black = _RGBA32(0, 0, 0, 255)
  10. CONST Blu = _RGBA32(0, 0, 200, 255)
  11. CONST Red = _RGBA32(200, 0, 0, 255)
  12. CONST Green = _RGBA32(0, 200, 0, 255)
  13.  
  14. TYPE btn
  15.     x AS INTEGER
  16.     y AS INTEGER
  17.     H AS INTEGER
  18.     w AS INTEGER
  19.     T AS STRING * 30
  20.     N AS STRING * 1
  21.  
  22. DIM SHARED Height AS INTEGER, Widht AS INTEGER, Buttons(1 TO 10) AS btn, z AS INTEGER, K$
  23.  
  24. Height = _FONTHEIGHT
  25. Widht = _FONTWIDTH
  26.  
  27. textBase$ = "This is a try to show different" ' 30 chars
  28.  
  29. ' graphic output/interface--------------------------------
  30. LINE (1, 1)-(799, 599), Green, B
  31. MakeScreen textBase$
  32. COLOR Red, Black
  33. _PRINTSTRING (100, 520), " Press from 1 to 10 for activate the button or left click on it"
  34. ' End graphic interface-----------------------------------
  35.  
  36. 'main
  37.  
  38. 'the events of the main are managed here
  39.     K$ = "Null"
  40.     FOR zz = 1 TO 9
  41.  
  42.         ActivateButtons zz
  43.  
  44.     NEXT
  45.  
  46.     IF K$ = CHR$(27) THEN K$ = "Done"
  47.     IF _MOUSEBUTTON(2) THEN K$ = "Done"
  48. LOOP WHILE K$ <> "Done"
  49.  
  50.  
  51. SUB ActivateButtons (Index AS INTEGER)
  52.  
  53.     IF K$ = "Null" THEN
  54.         ' it controls the keyboard and mouse if it is not done
  55.         DO
  56.             K$ = INKEY$
  57.             IF _MOUSEINPUT THEN K$ = "Null": EXIT DO
  58.         LOOP UNTIL K$ <> ""
  59.     END IF
  60.  
  61.     IF UCASE$(K$) = Buttons(Index).N THEN
  62.         EventButton Index
  63.         EXIT SUB
  64.     END IF
  65.     LOCATE 1, 1: PRINT K$, Buttons(Index).N
  66.         mX = _MOUSEX
  67.         mY = _MOUSEY
  68.         IF Buttons(Index).x <= mX AND Buttons(Index).x + Buttons(Index).w >= mX THEN
  69.             IF Buttons(Index).y <= mY AND Buttons(Index).y + Buttons(Index).H >= mY THEN
  70.                 EventButton Index
  71.             END IF
  72.         END IF
  73.     END IF
  74.  
  75. SUB EventButton (Index AS INTEGER)
  76.     DrawButton Buttons(Index).x, Buttons(Index).y, RTRIM$(Buttons(Index).T), Green, Black
  77.     _DELAY .25
  78.     DrawButton Buttons(Index).x, Buttons(Index).y, RTRIM$(Buttons(Index).T), Red, Blu
  79.  
  80. SUB MakeScreen (T AS STRING)
  81.     FOR z = 1 TO 9
  82.         MakeButton (RND * 500) + 1, z * 40, RTRIM$(LEFT$(T, (RND * 30) + 1)), Red, Blu
  83.     NEXT
  84.  
  85. SUB MakeButton (X AS INTEGER, Y AS INTEGER, txt AS STRING, Bcolor AS _UNSIGNED LONG, Fcolor AS _UNSIGNED LONG)
  86.     InitButtons X, Y, txt
  87.     DrawButton X, Y, txt, Bcolor, Fcolor
  88.  
  89. SUB DrawButton (X AS INTEGER, Y AS INTEGER, txt AS STRING, Bcolor AS _UNSIGNED LONG, Fcolor AS _UNSIGNED LONG)
  90.     LINE (X, Y)-(X + (Widht * LEN(txt)) + 10, Y + Height + 10), Bcolor, BF
  91.     COLOR Fcolor, Bcolor
  92.     _PRINTSTRING (X + 5, Y + 5), txt
  93.     LINE (X, Y)-(X + (Widht * LEN(txt)) + 10, Y + Height + 10), White, B
  94.  
  95. SUB InitButtons (X AS INTEGER, Y AS INTEGER, T AS STRING)
  96.     Buttons(z).x = X
  97.     Buttons(z).y = Y
  98.     Buttons(z).T = T
  99.     Buttons(z).H = Height
  100.     Buttons(z).w = Widht * LEN(Buttons(z).T)
  101.     Buttons(z).N = LTRIM$(STR$(z))

so each object make a slice of the polling on the keyboard and mouse if it isn't already done while it verify if it must react to input.

Thanks for reading
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Multiple objects/entities in the program: 2 ways to manage it
« Reply #2 on: September 27, 2019, 10:29:23 pm »
It's an interesting problem isn't it! I have been thinking about it for going on a week now.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Multiple objects/entities in the program: 2 ways to manage it
« Reply #3 on: September 28, 2019, 03:06:21 am »
Good work, TempodiBasic. I use this method oft.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Multiple objects/entities in the program: 2 ways to manage it
« Reply #4 on: October 06, 2019, 01:16:19 pm »
A smooth and fast version for the second tecnique: a slice of polling for each entities

I got it using a layer for manage mouse button. In the fashion of QB64, the mouse's informations have been saved in a buffer stack also while you are not using _MOUSEINPUT.

waiting the imagination and energy to add _MOUSECLEAR to QB64 new statements.

Code: QB64: [Select]
  1. _TITLE "multiple entities on the screen NOT overlapping"
  2. 'this is a little example of the 1st different concepts to manage
  3. 'multiple entities on the screen (or in the program but
  4. ' not visible like a timer or a counter)
  5. '
  6.  
  7. 'IMHO when we go to think as objects or entities to use in our programs we have two option...
  8. '1.  centered managing of mouse and keyboard and other input ways  ,
  9. '      so we can think about it like a pyramid that has like apex the I/O routine to be directed to the right entity at bottom,
  10. '      or like a collection of areas to be activated by a flag (in Windows there are the messages among objects)
  11. '2. a plane or circular managing of I/O  in which the objects/entities have the own different action that is activated by the specific
  12. '    position of the mouse and by the specific comboKey
  13. '
  14. 'the substantial difference between them is that in the first the apex
  15. 'manages I/O and triggers entity's action, in the second there is a main
  16. 'loop in which each entity tests if position of mouse
  17. 'and combokey activate its action/s.
  18. '
  19. SCREEN _NEWIMAGE(800, 600, 32)
  20. CONST White = _RGBA32(255, 255, 255, 255)
  21. CONST Black = _RGBA32(0, 0, 0, 255)
  22. CONST Blu = _RGBA32(0, 0, 200, 255)
  23. CONST Red = _RGBA32(200, 0, 0, 255)
  24. CONST Green = _RGBA32(0, 200, 0, 255)
  25.  
  26. TYPE btn
  27.     x AS INTEGER
  28.     y AS INTEGER
  29.     H AS INTEGER
  30.     W AS INTEGER
  31.     T AS STRING * 30
  32.     N AS STRING * 1
  33.  
  34. DIM SHARED Height AS INTEGER, Widht AS INTEGER, Buttons(1 TO 10) AS btn, z AS INTEGER, K$, mX, mY, mB
  35.  
  36. Height = _FONTHEIGHT
  37. Widht = _FONTWIDTH
  38.  
  39. textBase$ = "This is a try to show different" ' 30 chars
  40.  
  41. ' graphic output/interface--------------------------------
  42. LINE (1, 1)-(799, 599), Green, B
  43. MakeScreen textBase$
  44. COLOR Red, Black
  45. _PRINTSTRING (100, 520), " Press from 1 to 10 for activate the button or left click on it"
  46. ' End graphic interface-----------------------------------
  47.  
  48. 'main
  49.  
  50. 'the events of the main are managed here
  51.  
  52.     K$ = "Null"
  53.     FOR zz = 1 TO 9
  54.  
  55.         ActivateButtons zz
  56.  
  57.     NEXT
  58.  
  59.     IF K$ = CHR$(27) THEN K$ = "Done"
  60.     IF _MOUSEBUTTON(2) THEN K$ = "Done"
  61. LOOP WHILE K$ <> "Done"
  62.  
  63.  
  64. SUB ActivateButtons (Index AS INTEGER)
  65.  
  66.     TakeInput
  67.     ' here it analyzes input taken
  68.  
  69.     ' keyboard input event
  70.     IF UCASE$(K$) = Buttons(Index).N THEN
  71.         EventButton Index
  72.         EXIT SUB
  73.     END IF
  74.  
  75.     ' mouse input event
  76.     IF mB > 0 THEN
  77.         IF Buttons(Index).x <= mX AND Buttons(Index).x + Buttons(Index).W >= mX THEN
  78.             IF Buttons(Index).y <= mY AND Buttons(Index).y + Buttons(Index).H >= mY THEN
  79.                 EventButton Index
  80.                 mB = 0 ' we need to read new status of mousebuttons
  81.             END IF
  82.         END IF
  83.     END IF
  84.  
  85. SUB EventButton (Index AS INTEGER)
  86.     DrawButton Buttons(Index).x, Buttons(Index).y, RTRIM$(Buttons(Index).T), Green, Black
  87.     _DELAY .25
  88.     DrawButton Buttons(Index).x, Buttons(Index).y, RTRIM$(Buttons(Index).T), Red, Blu
  89.  
  90. SUB MakeScreen (T AS STRING)
  91.     FOR z = 1 TO 9
  92.         MakeButton (RND * 500) + 1, z * 40, RTRIM$(LEFT$(T, (RND * 30) + 1)), Red, Blu
  93.     NEXT
  94.  
  95. SUB MakeButton (X AS INTEGER, Y AS INTEGER, txt AS STRING, Bcolor AS _UNSIGNED LONG, Fcolor AS _UNSIGNED LONG)
  96.     InitButtons X, Y, txt
  97.     DrawButton X, Y, txt, Bcolor, Fcolor
  98.  
  99. SUB DrawButton (X AS INTEGER, Y AS INTEGER, txt AS STRING, Bcolor AS _UNSIGNED LONG, Fcolor AS _UNSIGNED LONG)
  100.     LINE (X, Y)-(X + (Widht * LEN(txt)) + 10, Y + Height + 10), Bcolor, BF
  101.     COLOR Fcolor, Bcolor
  102.     _PRINTSTRING (X + 5, Y + 5), txt
  103.     LINE (X, Y)-(X + (Widht * LEN(txt)) + 10, Y + Height + 10), White, B
  104.  
  105. SUB InitButtons (X AS INTEGER, Y AS INTEGER, T AS STRING)
  106.     Buttons(z).x = X
  107.     Buttons(z).y = Y
  108.     Buttons(z).T = T
  109.     Buttons(z).H = Height
  110.     Buttons(z).W = Widht * LEN(Buttons(z).T)
  111.     Buttons(z).N = LTRIM$(STR$(z))
  112.  
  113. SUB __MouseClear
  114.     DO
  115.         REM do nothing!
  116.  
  117. SUB TakeInput
  118.     IF K$ = "Null" THEN
  119.         ' it controls the keyboard and mouse if it is not done
  120.         DO
  121.             K$ = INKEY$
  122.             IF _MOUSEINPUT THEN
  123.                 K$ = "Null"
  124.  
  125.                 IF _MOUSEBUTTON(1) THEN mB = mB + 1
  126.                 IF _MOUSEBUTTON(2) THEN mB = mB + 2
  127.                 __MouseClear
  128.                 mX = _MOUSEX
  129.                 mY = _MOUSEY
  130.             END IF
  131.         LOOP UNTIL K$ <> ""
  132.     END IF
  133.  

Thanks to try
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Multiple objects/entities in the program: 2 ways to manage it
« Reply #5 on: October 06, 2019, 06:19:43 pm »
Hi TempodiBasic,

Seems to work well, nice job!

I can't wait to try your simple little mouseClear, I hate using _delay .2 so code doesn't act like it's been clicked 5 times! I have a new spot where my old tricks did not work but yours might. :)

Update, it worked! Bless you!
Code: QB64: [Select]
« Last Edit: October 06, 2019, 06:26:57 pm by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Multiple objects/entities in the program: 2 ways to manage it
« Reply #6 on: October 07, 2019, 05:12:33 pm »
Thanks Bplus
you give me the right vision of the SUB

Syntax
_MouseClear [ LoopForSecond]

  • LoopForSecond range 0.2 to 1000
  • if omitted the parameter the default is  100

inner code
Code: QB64: [Select]
  1. DO: _LIMIT LoopForSecond: LOOP UNTIL _MOUSEINPUT
--- Well this is a good point.




Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Multiple objects/entities in the program: 2 ways to manage it
« Reply #7 on: October 07, 2019, 06:55:30 pm »
Hi,

I do have an update for using that little line of code. Do it only after you detect a MouseButton, otherwise it will really bog down your code. When I reported my happy results it was after a detection of a mousebutton that I applied the "clear". I got tired of placing it in so many places so I tried it right after the WHILE _MOUSEINPUT: WEND. That's when the code really bogged down, so I added an IF mb1 (mouse button detection) then the "clear" and now it works well with only the one time placement.
« Last Edit: October 07, 2019, 07:30:34 pm by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Multiple objects/entities in the program: 2 ways to manage it
« Reply #8 on: October 09, 2019, 06:34:03 am »
Hi Bplus

about
Quote
I got tired of placing it in so many places so I tried it right after the WHILE _MOUSEINPUT: WEND.

I think that
Code: QB64: [Select]
is the specular definition of
Code: QB64: [Select]
so to  be safer the new function function can have at start a control on general _MOUSEINPUT

Code: QB64: [Select]
Programming isn't difficult, only it's  consuming time and coffee