Author Topic: Question about _MAPTRIANGLE, hardware images, and image handles... :'(  (Read 3474 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline jakeh0916

  • Newbie
  • Posts: 9
    • View Profile
Hey everyone, I'm trying to figure out _maptriangle and hardware images here. My intention is to use _maptriangle to map some triangles onto a mode 33 image. Then use _putimage to place that image on the screen. The reason I want to do this instead of just using _maptriangle to directly put triangles on the screen is that I want to preserve the screen resolution (which should be 500x500 here) in fullscreen mode. _FULLSCREEN _SQUAREPIXELS has no effect when using _MAPTRIANGLE in 3d mode, as far as I can tell... and I know of no other way to properly scale a small screen to fullscreen without smoothing it and stretching it.

Anyway, the problem with the current method is that it eats up memory like crazy (starts at around 60MB, steadily increases until crash), and I think it's pretty obvious what the issue is: it's copying that image (background&) to a new mode 33 image every single frame. I'm not sure how to deal with this though, since I don't believe you can _FREEIMAGE a mode 33 image (since its handle is negative) and I don't know how to do this projecting in any other way. (I could be wrong about _FREEIMAGE, though, as I did try it once and it didn't give me an error trying to free the mode 33 image. But, even with "freeing" the image every frame before copying a new one, the memory still rose until the program crashed.)

If you have any ideas or see what I'm messing up here... let me know!

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(500, 500, 32)
  2.  
  3. ' Load a mode 33 texture for _maptriangle to use
  4. DIM texture&
  5. texture& = _LOADIMAGE("tex", 32)
  6. DIM SHARED texture33&
  7. texture33& = _COPYIMAGE(texture&, 33)
  8. _FREEIMAGE(texture&)
  9.  
  10. ' Load an image to use as the background; say, a sunny sky, etc.
  11. ' This should be the same size as the screen
  12. DIM SHARED background&
  13. background& = _LOADIMAGE("bg", 32)
  14. DIM SHARED background33&
  15.  
  16.    _LIMIT 60
  17.      
  18.    _DEST 0: CLS
  19.    ' Make a mode 33 copy of background to use in the following method...
  20.    background33& = _COPYIMAGE(background&, 33)
  21.    CALL RenderTrianglesToBackground
  22.    
  23.    ' Paste background33 image with rendered triangles to screen
  24.    _PUTIMAGE(0,0), background33&
  25.  
  26. SUB RenderTrianglesToBackground
  27.    FOR i = 0 TO 99
  28.       ' Map a triangle to some 3d space from some texture space... -> onto background33&
  29.       _MAPTRIANGLE _ANTICLOCKWISE (t0u, t0v)-(t1u, t1v)-(t2u, t2v), texture33& TO(v0x, v0y, v0z)-(v1x, v1y, v1z)-(v2x, v2y, v2z), background33&
  30.    NEXT
  31.  
« Last Edit: February 12, 2021, 01:09:46 am by jakeh0916 »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Question about _MAPTRIANGLE, hardware images, and image handles... :'(
« Reply #1 on: February 12, 2021, 12:47:26 am »
You need a _DISPLAY before the end of your loop.

Hardware images won’t render, or leave memory, until _DISPLAY is called.  Toss one in there after the Putimage and let me know how it performs. 
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline jakeh0916

  • Newbie
  • Posts: 9
    • View Profile
Re: Question about _MAPTRIANGLE, hardware images, and image handles... :'(
« Reply #2 on: February 12, 2021, 01:04:36 am »
Oops, that's just a typo. I wrote that sample code for demonstration... I've got a display in there in my actual program, and the displaying works great. The PUTIMAGE scales properly with the screen and keeps its resolution, it's just that the copying of a bunch of software -> hardware images seems to leak memory somewhere.

Offline jakeh0916

  • Newbie
  • Posts: 9
    • View Profile
Re: Question about _MAPTRIANGLE, hardware images, and image handles... :'(
« Reply #3 on: February 12, 2021, 01:15:28 am »
Here, I've found this from the wiki as well.

Code: QB64: [Select]
  1. CONST MenuHeight = 200
  2.  
  3.  
  4. SCREEN _NEWIMAGE(640, 480, 32)
  5. 'SLEEP 1
  6.     _LIMIT 60
  7.     DisplayMenu
  8.     k = _KEYHIT
  9.     IF k <> 0 THEN PRINT k,
  10. LOOP UNTIL k = 32 OR k = 27
  11.  
  12.  
  13. SUB DisplayMenu
  14.     STATIC init, MS_HW AS LONG
  15.     IF NOT init THEN
  16.         init = -1
  17.         MS = _NEWIMAGE(640, MenuHeight, 32) 'MenuScreen image
  18.         D = _DEST: _DEST MS
  19.         CLS , &HFFAAAAAA 'background color gray
  20.         _PRINTSTRING (20, 2), "Menu Test" 'image text
  21.         MS_HW = _COPYIMAGE(MS, 33) 'create the MenuScreen_HardWare image
  22.         _FREEIMAGE MS
  23.         _DEST D
  24.     END IF
  25.     _PUTIMAGE (0, 0)-(640, MenuHeight), MS_HW
  26.     _DISPLAY
  27.  

When I run this at a high frame rate, it seems to also leak some memory. As the program sits, the memory used increases steadily...
Not sure what's causing this. Anyone know of a way to avoid this but get a hardware image wiped, written & drawn to the screen each frame?

Marked as best answer by jakeh0916 on February 11, 2021, 11:37:22 pm

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Question about _MAPTRIANGLE, hardware images, and image handles... :'(
« Reply #4 on: February 12, 2021, 01:42:42 am »
What OS?  Version of QB64?

In Win 10, with QB64x64, I run the test code and memory usage stays steady at 57.4MB.  I'm not seeing a leak from this.  Is it OS specific?
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline jakeh0916

  • Newbie
  • Posts: 9
    • View Profile
Re: Question about _MAPTRIANGLE, hardware images, and image handles... :'(
« Reply #5 on: February 12, 2021, 03:08:28 am »
I'm running QB64-1.2 on Windows 10, because I had a problem with _MOUSEMOVEMENTX/Y in 1.4.

Could definitely be some legacy bug. Ran the program again (the one from the wiki) and had memory increase from 40.1 MB to 52.2 MB over about ten minutes, and it was a pretty gradual increase throughout. I'd say this is kind of insignificant, but it looks like the same issue is getting magnified on my other program and causing a lot more memory to leak--or something, idk. I'm going to try it in QB 1.4 and see how that handles...

Offline jakeh0916

  • Newbie
  • Posts: 9
    • View Profile
Re: Question about _MAPTRIANGLE, hardware images, and image handles... :'(
« Reply #6 on: February 12, 2021, 04:36:48 am »
Incredibly, it does seem to be that me running an old version of QB64 was the problem. Who would've guessed.
Now if only I can get _MOUSEMOVEMENT working on 1.4!

Offline jakeh0916

  • Newbie
  • Posts: 9
    • View Profile
Re: Question about _MAPTRIANGLE, hardware images, and image handles... :'(
« Reply #7 on: February 12, 2021, 04:58:19 am »
Oh.. encountered the issue again, actually. I'm going to make a new post with a better example/title for this because I think I've made this one a little convoluted!

FellippeHeitor

  • Guest
Re: Question about _MAPTRIANGLE, hardware images, and image handles... :'(
« Reply #8 on: February 12, 2021, 06:33:45 am »
_MOUSEMOVEMENT has been fixed in upcoming v1.5. You can already start using it though: https://www.qb64.org/portal/development-build/