Author Topic: Image color swap, Part 2  (Read 1960 times)

0 Members and 1 Guest are viewing this topic.

Offline Craz1000

  • Forum Regular
  • Posts: 111
  • I'm OK
    • View Profile
    • Craz1000.net
Image color swap, Part 2
« on: April 08, 2020, 10:37:46 pm »
The original post was locked. However I have an update on progress.

I originally asked for a way to swap colors on a sprite to keep me from photoshoping the same sprite numerous times for color effects. Petr gave me this function... which works flawlessly.

Code: QB64: [Select]
  1. FUNCTION swapcolor (handle, oldcolor~&, newcolor~&)
  2.     DIM m AS _MEM, c AS _UNSIGNED LONG
  3.     swapcolor = _COPYIMAGE(handle, 32)
  4.     m = _MEMIMAGE(swapcolor)
  5.     DO UNTIL a& = m.SIZE - 4
  6.         a& = a& + 4
  7.         c~& = _MEMGET(m, m.OFFSET + a&, _UNSIGNED LONG)
  8.         IF c~& = oldcolor~& THEN _MEMPUT m, m.OFFSET + a&, newcolor~&
  9.     LOOP
  10.     _MEMFREE m

However I also happen to use this same function to make the sprites "flash" random colors when they take damage. The issue is when this is ran a few thousand times the game hard crashes without warning and without the "On Error" command catching it. When i run task manager the memory usage goes up and up until it reaches a point and the game window closes without warning. To be sure i disabled the use of this function and memory usage stays down and the game ran for two hours without a problem. _MEM is very new to me and I dont quite understand it yet.

So my questions are...
1)why is this happening? If it is using _MEMFREE to release m then in theory memory usage shouldn't climb like the way it is.

2)how could I fix this? and if not is there maybe a way to do this without using _MEM since i suspect that is the culprit.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Image color swap, Part 2
« Reply #1 on: April 08, 2020, 10:54:48 pm »
swapcolor is creating a new image over and over with _COPYIMAGE, so you need to _FREEIMAGE the memory when you no longer need it.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Image color swap, Part 2
« Reply #2 on: April 09, 2020, 12:01:17 am »
Cool, I was just asking Qwerkey about this! :)

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Image color swap, Part 2
« Reply #3 on: April 09, 2020, 11:35:55 am »
Hi. Try this repaired version, it run if your IDE use basic setting (background color is 0,0,170)

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(1024, 768, 32)
  2.  
  3.  
  4. old~& = _RGB32(0, 0, 170) 'basic blue background in IDE is used as test layer
  5.  
  6.     new~& = _RGB32(RND * 70, RND * 128, RND * 55)
  7.     i = swapcolor(i, old~&, new~&)
  8.     old~& = new~&
  9.     _PUTIMAGE , i
  10.     _LIMIT 30
  11.  
  12. FUNCTION swapcolor (handle, oldcolor~&, newcolor~&)
  13.     DIM m AS _MEM, c AS _UNSIGNED LONG
  14.     swapcolor = _COPYIMAGE(handle, 32) '                           create new handle
  15.   _FREEIMAGE handle'                                                        delete old handle from memory
  16.     m = _MEMIMAGE(swapcolor)
  17.     DO UNTIL a& = m.SIZE - 4
  18.         a& = a& + 4
  19.         c~& = _MEMGET(m, m.OFFSET + a&, _UNSIGNED LONG)
  20.         IF c~& = oldcolor~& THEN _MEMPUT m, m.OFFSET + a&, newcolor~&
  21.     LOOP
  22.     _MEMFREE m
  23.  
  24.  

Offline Craz1000

  • Forum Regular
  • Posts: 111
  • I'm OK
    • View Profile
    • Craz1000.net
Re: Image color swap, Part 2
« Reply #4 on: April 09, 2020, 01:23:26 pm »
That fixed it. Thanks!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Image color swap, Part 2
« Reply #5 on: April 09, 2020, 02:58:08 pm »
You know if you just keep track of what images are one offs and free them, you can keep the original image which makes sense to me. Unless maybe you continue to change different colors in the new images created?

I think this might be very useful in future so I tested and created demo and added it to my Tool Box.
https://www.qb64.org/forum/index.php?topic=1511.msg116787#msg116787

This definitely answers my question put to Qwerkey in Pi-In-The-Sky thread.