Author Topic: a little demo about VIEW WINDOW and PMAP: beginners info  (Read 1167 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
a little demo about VIEW WINDOW and PMAP: beginners info
« on: June 21, 2020, 06:28:36 am »
Hi guys
seeing that it is in the air to talk about managing of physical screen of pc and how to change its dimensions and cohordinate's system and moreover how to fragment it into more little areas that are indipendent among them for dimensions, cohordinate's system
I post here my little contibution
a demonstration lesson about VIEW WINDOW and PMAP using also _PRINTSTRING and _MOUSEcohordinates as Bplus has stressed that are out of this system and must be adapted to interact properly with it.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3.  
  4. ' 0-799<--->    0-599 ^
  5. '                     |
  6. '                     v
  7.  
  8. _TITLE "Window View and PMAP: some info"
  9.  
  10. COLOR _RGB32(0, 10, 255)
  11.  
  12. LINE (-2, -2)-(802, 602), _RGB32(255, 0, 0), B
  13. _PRINTSTRING (10, 10), "Remember you can make graphic out of screen!"
  14. WINDOW (-10, -10)-(810, 610)
  15. LINE (-2, -2)-(802, 602), _RGB32(255, 0, 0), B
  16. _PRINTSTRING (10, 10), "Remember you can make graphic out of screen!"
  17. _PRINTSTRING (10, 10), "You cannot set an area of screen not visible (out of screen visible) by VIEW!"
  18. _PRINTSTRING (10, 40), " you'll get an error messagebox, please choose to continue"
  19. VIEW (-2, -2)-(802, 602), _RGB32(200, 20, 20), _RGB32(0, 0, 255)
  20.  
  21. VIEW (1, 1)-(798, 598), _RGB32(127, 127, 127), _RGB32(200, 10, 250)
  22. _PRINTSTRING (10, 10), "You can choose the whole screen with coordinates or only VIEW with no parameters"
  23. WINDOW SCREEN(1, -10)-(850, 400) ' used SCREEN to maintain the y = 0 at top and y = max at bottom
  24. LINE (-2, -2)-(802, 602), _RGB32(200, 20, 20), B
  25. _PRINTSTRING (10, 10), "Each graphic instruction follow the dimensions of WINDOW instruction"
  26. _PRINTSTRING (10, 30), " the exceptions are _PRINTSTRING and _MOUSEpositions. "
  27. _PRINTSTRING (10, 60), "In fact this text and line are in different positions"
  28. LINE (10, 30)-(140, 30), _RGB32(255, 120, 10)
  29. _PRINTSTRING (10, 90), "Move mouse in the next demo"
  30. WINDOW (1, -10)-(850, 400)
  31. a% = 10: b% = 40: c% = b% + 20
  32.         WHILE _MOUSEINPUT: WEND
  33.         mx% = _MOUSEX
  34.         my% = _MOUSEY
  35.         LMB% = _MOUSEBUTTON(1)
  36.         RMB% = _MOUSEBUTTON(2)
  37.         CLS
  38.         _PRINTSTRING (10, 20), "X = " + STR$(mx%) + "  Y= " + STR$(my%)
  39.         _PRINTSTRING (160, 20), " Move mouse to TopLeft and BottomRight corner of screen and see its value"
  40.  
  41.         _PRINTSTRING (a%, b%), "press Right Mouse Button to move this text on the screen to mouse's position"
  42.         _PRINTSTRING (a%, c%), "press Left Mouse Button to quit this demonstration"
  43.         _PRINTSTRING (a%, c% + 40), " Lines and text aren't on the same position!"
  44.         LINE (a%, b%)-(a% + 100, b%), _RGB32(10, 200, 10)
  45.         LINE (a%, c%)-(a% + 100, c%), _RGB32(10, 200, 10)
  46.         IF RMB% THEN
  47.             a% = mx%
  48.             b% = my%
  49.             c% = my% + 20
  50.         END IF
  51.         _DISPLAY
  52.     END IF
  53. COLOR _RGB32(255, 0, 0)
  54. _PRINTSTRING (20, 10), "In sum: VIEW selects an area of the screen visible. WINDOW defines custom dimensions of that area"
  55. _PRINTSTRING (20, 30), "WINDOW inverts Y-axis, SCREEN parameter restores the original direction of Y-axis"
  56. VIEW (100, 100)-(500, 200), _RGB32(127, 127, 127)
  57. WINDOW SCREEN(1, 1)-(100, 100)
  58. LINE (50, 50)-(150, 50), _RGB32(127, 1, 127)
  59.  
  60. VIEW (100, 300)-(500, 400), _RGB32(127, 127, 127)
  61. WINDOW SCREEN(1, 1)-(200, 100)
  62. LINE (50, 50)-(150, 50), _RGB32(127, 1, 127)
  63.  
  64. VIEW (100, 500)-(500, 598), _RGB32(127, 127, 127)
  65. WINDOW SCREEN(-100, 100)-(100, 1) ' the WINDOW statement has an inner sorting of dimensions
  66. LINE (50, 50)-(150, 50), _RGB32(127, 1, 127)
  67. _PRINTSTRING (10, 10), " Now draw by mouse in the upper rectangle by left click, quit by Right click"
  68. VIEW (100, 100)-(500, 200), , _RGB32(127, 127, 0)
  69. WINDOW SCREEN(1, 1)-(300, 300)
  70. LINE (50, 50)-(150, 50), _RGB32(127, 1, 127)
  71.  
  72. VIEW (100, 300)-(500, 400), , _RGB32(127, 127, 0)
  73. WINDOW SCREEN(1, 1)-(200, 100)
  74. LINE (50, 50)-(150, 50), _RGB32(127, 1, 127)
  75.  
  76.     mx% = _MOUSEX
  77.     my% = _MOUSEY
  78.     LMB% = _MOUSEBUTTON(1)
  79.     RMB% = _MOUSEBUTTON(2)
  80.     IF LMB% THEN
  81.         IF mx% > 99 AND mx% < 501 THEN
  82.             IF my% > 99 AND my% < 201 THEN
  83.                 ' it draws in the first viewport
  84.                 VIEW (100, 100)-(500, 200), , _RGB32(127, 127, 0)
  85.                 WINDOW SCREEN(1, 1)-(300, 300)
  86.                 LINE (50, 50)-(150, 50), _RGB32(127, 1, 127)
  87.  
  88.  
  89.                 LINE -STEP(INT(PMAP(mx%, 2)), INT(PMAP(my%, 3))), _RGB32(200, 100, 200, 255)
  90.                 _DISPLAY
  91.                 VIEW
  92.                 COLOR _RGB32(200, 200, 0)
  93.                 _PRINTSTRING (100, 250), STR$(mx%) + " -> " + STR$(INT(PMAP(mx%, 2))) + "  " + STR$(my%) + " --> " + STR$(INT(PMAP(my%, 3)))
  94.  
  95.                 ' it draws in the second viewport
  96.                 VIEW (100, 300)-(500, 400), , _RGB32(127, 127, 0)
  97.                 WINDOW SCREEN(1, 1)-(200, 100)
  98.                 LINE (50, 50)-(150, 50), _RGB32(127, 1, 127)
  99.  
  100.  
  101.                 LINE -STEP(INT(PMAP(mx%, 2)), INT(PMAP(my%, 3))), _RGB32(255, 255, 255, 255)
  102.                 _DISPLAY
  103.                 VIEW
  104.                 COLOR _RGB32(255)
  105.                 _PRINTSTRING (100, 450), STR$(mx%) + " -> " + STR$(INT(PMAP(mx%, 2))) + "  " + STR$(my%) + " --> " + STR$(INT(PMAP(my%, 3)))
  106.                 _DISPLAY
  107.             END IF
  108.         END IF
  109.     END IF
  110.  
  111. LOOP UNTIL RMB%
  112. _PRINTSTRING (10, 10), "Thanks to watching"
Thanks to read
Programming isn't difficult, only it's  consuming time and coffee

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: a little demo about VIEW WINDOW and PMAP: beginners info
« Reply #1 on: June 21, 2020, 09:00:41 am »
Nice presentation. Some of the sleep statements could be a little longer for us slow readers. ;)

I've found WINDOW to be a good way to predetermine how my graphics interacts with the data generated. I use it quite a lot, particularly for quickie applications. There is a tradeoff, I can define my space in R3 like I'm used to, but then I have to be aware to alter how any trig functions behave. It can be a bit of a pain working between vector and polar coordinates. This was how I handled the space flight program I've been endlessly working on. Generate a sub-image with custom coordinates using VIEW & WINDOW then _PUTIMAGE to the mainscreen.

One painful lesson I learned from that approach, if your "model space" is much larger than your "display space" and contains a lot of "off-screen" details, you're well served to use logical operators that avoid the processing time of drawing things that will not actually be displayed. Otherwise, things can slow to a crawl if the graphics are complex, such as liberal use of Steve's fcirc, in my case.

I'm going to have to get familiar with PMAP, it suddenly occurred to me that it might hold the key to doing some mouse selection bugs I've been dealing with.


Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: a little demo about VIEW WINDOW and PMAP: beginners info
« Reply #2 on: June 22, 2020, 07:18:05 pm »
Thanks OldMoses
I'll adjust a bit those SLEEPs

I have little experience with WINDOW and PMAP  while a bit more with VIEW but I find their use very useful in some kind of graphic work.

I found an example of Mandelbrot fractal in Qbasic with use of WINDOW VIEW and PMAP. It was ok but I have thought to make it a bit more adaptable so I convert the program into a SUB and I play to draw Mandelbrot Fractal in 3 different viewport just to show how can be useful such instructions.
Thanks to read and to try following code.

Code: QB64: [Select]
  1. ' a little demonstration about WINDOW, VIEW and PMAP
  2. SCREEN _NEWIMAGE(800, 600, 8)
  3. _TITLE "a little demonstration about WINDOW, VIEW and PMAP"
  4.  
  5. 'This example uses the VIEW and WINDOW statements to define a graphics
  6. 'viewport and window. The PMAP function is used to convert viewport
  7. 'coordinates to window coordinates.  The program generates a fractal
  8. 'that shows a subset of the complex numbers called the "Mandelbrot Set."
  9.  
  10. DEFINT A-Z 'Default variable type is integer.
  11.  
  12. 'Set maximum number of iterations per point.
  13. CONST MAXLOOP = 30, MAXSIZE = 1000000
  14. CONST FALSE = 0, TRUE = NOT FALSE 'Boolean constants.
  15.  
  16. ' set maximum viewport parameters (whole screen)
  17. CONST VLm = 0, VUm = 0, VRm = 799, VBm = 599
  18. ' set  first viewport  parameters
  19. CONST VL1 = 50, VU1 = 50, VR1 = 200, VB1 = 500
  20.  
  21. ' set  second viewport  parameters
  22. CONST VL2 = 300, VU2 = 300, VR2 = 700, VB2 = 500
  23.  
  24. 'Set window paramters.
  25. CONST WLeft = -1000, WRight = 250, WTop = 625, WBottom = -625
  26.  
  27. ' declaration global variables
  28. DIM SHARED ColorRange AS INTEGER
  29.  
  30. ColorRange = 15
  31.  
  32. 'Define viewport and corresponding window
  33. ' the whole window
  34. VIEW (VLm, VUm)-(VRm, VBm), 0, ColorRange
  35. WINDOW (WLeft, WTop)-(WRight, WBottom)
  36. DrawMandelbrot VLm, VRm, VBm, VUm
  37.  
  38. ' a vertical little window
  39. VIEW (VL1, VU1)-(VR1, VB1), 0, ColorRange
  40. WINDOW (WLeft, WTop)-(WRight, WBottom)
  41. DrawMandelbrot VL1, VR1, VB1, VU1
  42.  
  43. ' an horizhontal window
  44. VIEW (VL2, VU2)-(VR2, VB2), 0, ColorRange
  45. WINDOW (WLeft, WTop)-(WRight, WBottom)
  46. DrawMandelbrot VL2, VR2, VB2, VU2
  47.  
  48. LOCATE 1, 10: PRINT "Press any key to quit.";
  49.  
  50. SUB DrawMandelbrot (Left AS INTEGER, Right AS INTEGER, Bottom AS INTEGER, Up AS INTEGER)
  51.  
  52.     DIM Xlength AS INTEGER, Ylength AS INTEGER, Y AS INTEGER, X AS INTEGER
  53.     DIM ColorWidth AS INTEGER, LogicY AS INTEGER, LogicX AS INTEGER
  54.     DIM OldColor AS INTEGER, PColor AS INTEGER, RealNum&, ImagNum&
  55.     DIM MandelX&, MandelY&
  56.  
  57.     Xlength = Right - Left
  58.     Ylength = Bottom - Up
  59.     ColorWidth = MAXLOOP \ ColorRange
  60.  
  61.     'Loop through each pixel in viewport and calculate whether or not
  62.     'it is in the Mandelbrot Set.
  63.     FOR Y = 0 TO Ylength 'Loop through every line in the viewport.
  64.         LogicY = PMAP(Y, 3) 'Get the pixel's logical y coordinate.
  65.         PSET (WLeft, LogicY) 'Plot leftmost pixel in the line.
  66.         OldColor = 0 'Start with background color.
  67.  
  68.         FOR X = 0 TO Xlength 'Loop through every pixel in the line.
  69.             LogicX = PMAP(X, 2) 'Get the pixel's logical x coordinate.
  70.             MandelX& = LogicX
  71.             MandelY& = LogicY
  72.  
  73.             'Do the calculations to see if this point is in the Mandelbrot Set.
  74.             FOR I = 1 TO MAXLOOP
  75.                 RealNum& = MandelX& * MandelX&
  76.                 ImagNum& = MandelY& * MandelY&
  77.                 IF (RealNum& + ImagNum&) >= MAXSIZE THEN EXIT FOR
  78.                 MandelY& = (MandelX& * MandelY&) \ 250 + LogicY
  79.                 MandelX& = (RealNum& - ImagNum&) \ 500 + LogicX
  80.             NEXT I
  81.  
  82.             'Assign a color to the point.
  83.             PColor = I \ ColorWidth
  84.  
  85.             'If color has changed, draw a line from the last point
  86.             'referenced to the new point, using the old color.
  87.             IF PColor <> OldColor THEN
  88.                 LINE -(LogicX, LogicY), (ColorRange - OldColor)
  89.                 OldColor = PColor
  90.             END IF
  91.  
  92.             IF INKEY$ <> "" THEN END
  93.         NEXT X
  94.  
  95.         'Draw the last line segment to the right edge of the viewport.
  96.         LINE -(LogicX, LogicY), (ColorRange - OldColor)
  97.  
  98.     NEXT Y
  99.  
Programming isn't difficult, only it's  consuming time and coffee