Author Topic: Multiple images  (Read 3572 times)

0 Members and 1 Guest are viewing this topic.

Offline Leo

  • Newbie
  • Posts: 4
    • View Profile
Multiple images
« on: October 27, 2020, 08:08:26 pm »
I have been hobby programming in various basics for 48 years, and I like the power QB64 offers.
I have examined the graphics commands but cannot find details of what I want.

Q. How do I load images into different areas on the display screen, so they are all fully visible simultaneously?

I will be grateful for any advice, or pointers to demo programs that show how to do this.

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Multiple images
« Reply #1 on: October 27, 2020, 11:22:47 pm »
_LOADIMAGE and _PUTIMAGE

The syntax of _PUTIMAGE still seems strange to me, but it sure works. With it you can not only place the image anywhere on the screen but also resize and flip horizontally or vertically.  After loading an image the _WIDTH and _SIZE of it are available - you don't have to know it in advance.  Also, _LOADIMAGE supports a bunch of common file formats.

You could also use _LOADIMAGE along with the old QBASIC GET and PUT.  _PUTIMAGE doesn't have the AND, OR, and XOR capability of PUT, exactly.  Much of that can be accomplished with color transparencies (_RGBA), but it ain't simple.

Start with SCREEN _NEWIMAGE(1024,768,32), or whatever size you want  to have something to paste onto!

You could study my chess program.  All the pieces are images, like Chuck Norris, Mel Brooks, and SpongeBob.  There's
even a key to display all funny images - "Z" - like a simple picture viewer.
« Last Edit: October 27, 2020, 11:46:38 pm by Richard Frost »
It works better if you plug it in.

Offline Leo

  • Newbie
  • Posts: 4
    • View Profile
Re: Multiple images
« Reply #2 on: October 28, 2020, 12:04:55 am »
Thanks for your reply.
Where can I find your chess program?

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Multiple images
« Reply #3 on: October 28, 2020, 12:13:22 am »
Chess program is at:
https://www.qb64.org/forum/index.php?topic=2437.msg116514#msg116514

There are two versions.  The second, bigger zip includes sound, and is the one I'm constantly updating.
The "view all funny pictures" is in SUB ShowFunny. 


« Last Edit: October 28, 2020, 12:14:47 am by Richard Frost »
It works better if you plug it in.

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Multiple images
« Reply #4 on: October 28, 2020, 12:19:21 am »
And here's a simple picture viewer I did when I was still learning QB64. 
It has bugs, like cannot arrow to all pictures, but it does have "a" for
automatic, to view full images.

Code: QB64: [Select]
  1. DEFINT A-Z
  2. DIM f$(5000), i&(20, 20)
  3. xm = 1280: ym = 800
  4. SCREEN _NEWIMAGE(1280, 800, 32)
  5.  
  6. q$ = CHR$(34)
  7. d$ = "temp.dir"
  8. q = 8
  9.  
  10. p$ = "\pix\aurora\" '                 change this to your directory of pictures
  11. 'p$ = "\pix\mycars\"
  12.  
  13. c$ = "dir " + p$ + " > \qb64\" + d$
  14.  
  15. GOSUB Init
  16.     begin:
  17.     replot = 0
  18.     CLS
  19.     FOR tr = 1 TO maxr
  20.         FOR tc = 1 TO maxc
  21.             GOSUB mload
  22.         NEXT tc
  23.     NEXT tr
  24.     DO: _LIMIT 10
  25.         GOSUB kscan
  26.     LOOP UNTIL replot OR (i$ = Enter$) OR (fs = 1)
  27.     IF replot THEN GOTO begin
  28.     fs = 1
  29.     DO: _LIMIT 10
  30.         tc = cc: tr = rr: GOSUB mload
  31.         zc = cc: zr = rr
  32.         DO: _LIMIT 10
  33.             IF auto THEN
  34.                 IF autot! = 0 THEN autot! = TIMER + 1
  35.                 IF TIMER > autot! THEN
  36.                     cc = cc + 1
  37.                     autot! = TIMER + 5
  38.                 END IF
  39.             END IF
  40.             GOSUB kscan
  41.         LOOP UNTIL (i$ = Enter$) OR (zc <> cc) OR (zr <> rr)
  42.     LOOP UNTIL i$ = Enter$
  43.     fs = 0
  44.  
  45. mload:
  46. n = mn + (tr - 1) * q + tc
  47. f$ = p$ + f$(n)
  48. x = (tc - 1) * xq
  49. y = (tr - 1) * yq
  50. t = 0
  51. tryagain:
  52. GOSUB kscan
  53. t = t + 1
  54. IF i&(tc, tr) = 0 THEN i&(tc, tr) = _LOADIMAGE(f$)
  55. IF (i&(tc, tr) >= -1) AND (t < 3) THEN i&(tc, tr) = 0: GOTO tryagain
  56. IF (i&(tc, tr) < -1) THEN
  57.     IF fs THEN
  58.         z1 = _WIDTH(i&(tc, tr))
  59.         z2 = _HEIGHT(i&(tc, tr))
  60.         x1 = (_WIDTH - z1) \ 2
  61.         y1 = (_HEIGHT - z2) \ 2
  62.         CLS
  63.         IF z1 < _WIDTH THEN
  64.             _PUTIMAGE (x1, y1)-(x1 + z1, y1 + z2), i&(tc, tr), 0
  65.         ELSE
  66.             _PUTIMAGE , i&(tc, tr), 0
  67.         END IF
  68.     ELSE
  69.         _PUTIMAGE (x0 + x, y0 + y)-(x0 + x + xq, y0 + y + yq), i&(tc, tr), 0
  70.     END IF
  71.     IF fs THEN
  72.         CLS
  73.     ELSE
  74.         LINE (x0 + x, y0 + y)-(x0 + x + xq, y0 + y + yq), black&, BF
  75.     END IF
  76.  
  77. n = mn + (tr - 1) * q + tc
  78. IF n > nf THEN n = 1
  79.  
  80. IF fs = 0 THEN
  81.     IF (tc = cc) AND (tr = rr) THEN tc& = white& ELSE tc& = black&
  82.     FOR z = 0 TO 4
  83.         LINE (x0 + x + z, y0 + y + z)-(x0 + x + xq - z, y0 + y + yq - z), tc&, B
  84.     NEXT z
  85.  
  86. kscan:
  87. i$ = INKEY$
  88. IF i$ = "a" THEN auto = auto XOR 1: fs = 1: RETURN
  89. IF fs = 0 THEN
  90.     IF i$ = "+" THEN
  91.         q = q - 1
  92.         IF q < 2 THEN q = 2
  93.         GOSUB init2
  94.     END IF
  95.     IF i$ = "-" THEN
  96.         q = q + 1
  97.         IF q > 20 THEN q = 20
  98.         GOSUB init2
  99.     END IF
  100. IF i$ = "d" THEN
  101.     _FREEIMAGE i&(tc, tr)
  102.     i&(tc, tr) = 0
  103.     'c$ = "del " + CHR$(34) + f$ + CHR$(34)
  104.     SHELL _HIDE c$
  105. IF i$ = Esc$ THEN SYSTEM
  106. wr = rr: wc = cc
  107. IF LEN(i$) = 2 THEN
  108.     kk = ASC(RIGHT$(i$, 1))
  109.     cd = (kk = 75) - (kk = 77)
  110.     rd = (kk = 72) - (kk = 80)
  111.     cc = cc + cd '                                       left right
  112.     rr = rr + rd '                                       up down
  113. tp = (mn + (rr - 1) * maxr + cc)
  114. IF (tp > nf) OR (tp < 1) THEN SOUND 2222, 1: cc = wc: rr = wr
  115. IF cc < 1 THEN
  116.     cc = q
  117.     rr = rr - 1
  118. IF cc > q THEN
  119.     cc = 1
  120.     rr = rr + 1
  121. IF rr < 1 THEN
  122.     rr = 1
  123.     mn = mn - maxr
  124.     IF mn < 0 THEN mn = 0: RETURN
  125.     FOR tc = 1 TO maxc
  126.         IF i&(tc, maxr) < -1 THEN _FREEIMAGE i&(tc, maxr)
  127.     NEXT tc
  128.     FOR tr = maxr TO 2 STEP -1
  129.         FOR tc = 1 TO maxc
  130.             i&(tc, tr) = (i&(tc, tr - 1))
  131.         NEXT tc
  132.     NEXT tr
  133.     FOR tc = 1 TO maxc
  134.         i&(tc, 1) = 0
  135.     NEXT tc
  136.     replot = 1
  137.     RETURN
  138. IF rr > maxr THEN
  139.     rr = maxr
  140.     mn = mn + maxc
  141.     IF ((mn + (maxr - 1) * maxc + 1) > nf) THEN mn = mn - maxc: RETURN ' wonka
  142.     FOR tc = 1 TO maxc
  143.         IF i&(tc, 1) < -1 THEN _FREEIMAGE i&(tc, 1)
  144.     NEXT tc
  145.     FOR tr = 1 TO maxr - 1
  146.         FOR tc = 1 TO maxc
  147.             i&(tc, tr) = (i&(tc, tr + 1))
  148.         NEXT tc
  149.     NEXT tr
  150.     FOR tc = 1 TO maxc
  151.         i&(tc, maxr) = 0
  152.     NEXT tc
  153.     replot = 1
  154.     RETURN
  155. IF (rr = wr) AND (cc = wc) THEN RETURN
  156. IF fs = 0 THEN
  157.     tr = wr: tc = wc: GOSUB mload
  158.     tr = rr: tc = cc: GOSUB mload
  159.  
  160. Init:
  161. Enter$ = CHR$(13)
  162. Esc$ = CHR$(27)
  163.  
  164. black& = _RGB32(0, 0, 0)
  165. white& = _RGB32(255, 255, 255)
  166. red& = _RGB32(255, 0, 0)
  167. bg& = rgba(0, 0, 0, 0)
  168.  
  169.     LINE INPUT #1, t$: t$ = LCASE$(t$)
  170.     IF (LEN(t$) > 4) AND (INSTR("jpg gif png bmp", RIGHT$(t$, 3))) THEN
  171.         nf = nf + 1
  172.         f$(nf) = RIGHT$(t$, LEN(t$) - 39)
  173.     END IF
  174.  
  175. init2:
  176. maxc = q
  177. maxr = q * xm / ym
  178. xq = (xm - 1) \ maxc
  179. yq = (ym - 1) \ maxr
  180. x0 = (xm / q - INT(xm / q)) * q / 2
  181. y0 = 16
  182. rr = 1: cc = 1
  183. replot = 1
  184.  
  185.  
It works better if you plug it in.

Offline Leo

  • Newbie
  • Posts: 4
    • View Profile
Re: Multiple images
« Reply #5 on: October 28, 2020, 02:01:41 am »
thanks.
your programs do not run on my pc, but you have given me good fodder to chew on.

[being there: peter, spike, harry. they may have fallen in the well. we all do. :) ]

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Multiple images
« Reply #6 on: October 28, 2020, 02:59:02 am »
At the top left of the webpage, right-click on the QB64 image and save it to your qb64 folder.  By default, it should be "QB64-1.4-Release.png".  With it, you can run the simple little demo below:

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(640, 480, 32)
  2. image = _LOADIMAGE("QB64-1.4-Release.png", 32)
  3.  
  4. _PUTIMAGE , image
  5. FOR i = 0 TO 600 STEP 200
  6.     _PUTIMAGE (i, 0)-STEP(200, 200), image

For multiple images, just load them into multiple image handles and then _PUTIMAGE them at the coordinates you want them to appear on the screen. 
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Leo

  • Newbie
  • Posts: 4
    • View Profile
Re: Multiple images
« Reply #7 on: October 31, 2020, 02:17:21 am »
This was most useful, and educated me, and I wrote several progs using multiple images.
I then wrote a program with a single well sized and placed image (150kB) and used a mouse to overlay single characters at different print positions.
I was ecstatic. A dream come true. Then I hit the wall.
4 clicks and a freeze, and my cpu fan went crazy.
The ground started shaking. Was it an earthquake?
No, it was the QB64 reality helicopter landing on my dream.
I had to use task manager to escape.

You have solved my problem, but introduced me to a beast, now I will have to open another thread about freezing to avoid going off topic.

[fortunately there are no bugs in QB64, only 'undocumented features', or 'glitches'  :) ]

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Multiple images
« Reply #8 on: October 31, 2020, 02:34:46 am »
This was most useful, and educated me, and I wrote several progs using multiple images.
I then wrote a program with a single well sized and placed image (150kB) and used a mouse to overlay single characters at different print positions.
I was ecstatic. A dream come true. Then I hit the wall.
4 clicks and a freeze, and my cpu fan went crazy.
The ground started shaking. Was it an earthquake?
No, it was the QB64 reality helicopter landing on my dream.
I had to use task manager to escape.

You have solved my problem, but introduced me to a beast, now I will have to open another thread about freezing to avoid going off topic.

[fortunately there are no bugs in QB64, only 'undocumented features', or 'glitches'  :) ]

Post some code, we can find the "undocumented features" and make your dreams come true faster ;-))

Man I hate calling on the task manager but .ogg files do worse things (in Windows 10 File Explorer).

4 clicks and a freeze, I sure would like to know what you fed the beast.
« Last Edit: October 31, 2020, 02:54:42 am by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Multiple images
« Reply #9 on: October 31, 2020, 09:57:44 am »
4 clicks and a freeze, and my cpu fan went crazy.
The ground started shaking. Was it an earthquake?
No, it was the QB64 reality helicopter landing on my dream.

This sounds like an endless loop.  Share the code and we’ll see if we can find it for you.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!