Author Topic: Using _MEM to clear layer without having to reset CLEARCOLOR!  (Read 2566 times)

0 Members and 1 Guest are viewing this topic.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Using _MEM to clear layer without having to reset CLEARCOLOR!
« on: October 24, 2019, 01:04:06 pm »
I need a faster way to clear a layer, while retaining the current alpha settings, AKA _CLEARCOLOR. As CLS resets alpha values.
So I was thinking of trying this, and just wanted to have it looked over once to make sure that if it doesn't work its not because of my use of the code. Before I spend a lot of time testing this and trying to implement it I would appreciate it if someone(Steve perhaps) could double check it.

Code: QB64: [Select]
  1. DIM ML1 as _MEM, ML2 as _MEM
  2. Layer1&=_NEWIMAGE(1472,1472,32) 'main sprite layer
  3. Layer2&=_NEWIMAGE(1472,1472,32) 'quick clear layer
  4. _CLEARCOLOR _RGB32(0,0,0), Layer2&
  5.  
  6. ML1 = _MEMIMAGE(Layer1&)
  7. ML2 = _MEMIMAGE(Layer2&)
  8.  
  9. 'then to clear layer1 I was thinking that this might work.
  10. _MEMPUT ML1, 0, _MEMGET(ML2, 0, STRING*8667136)
  11.  

My current method is:

Code: QB64: [Select]
  1. _DEST Layer1
  2.  
  3. 'Draw sprites to layer1...
  4.  
  5. _CLEARCOLOR _RGB32(0,0,0), Layer1
  6.  

which is horribly slow when your running it every cycle.
But I'm not positive I can use the _MEMPUT method I thought of, and just want a little reassurance before I go and spend a good bit of time on the matter.

Or have it not work and think it can't be done when it might just be something with the way I wrote it and not the actual method.
« Last Edit: October 24, 2019, 05:46:30 pm by Cobalt »
Granted after becoming radioactive I only have a half-life!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Need my use of _MEM double checked Please.
« Reply #1 on: October 24, 2019, 01:42:24 pm »
Hi Cobalt. Steve helped me with this problem in my editor. MEMFILL is fast way:

Code: QB64: [Select]
  1. SUB Steve_SCLS (x1 AS LONG, y1 AS LONG, x2 AS LONG, y2 AS LONG) 'for Hardware images. Create "window" in Software layer for view Hardware Layer in background. This is SteveMcNeill's upgrade for my previous but 80x slower version.
  2.    
  3.     a = _DEST
  4.     STATIC m AS _MEM
  5.     DIM x AS LONG, y AS LONG
  6.     DIM w AS LONG, o AS _OFFSET
  7.     m = _MEMIMAGE(a)
  8.     IF y1 > y2 THEN SWAP y1, y2
  9.     IF x1 > x2 THEN SWAP x1, x2
  10.  
  11.     'memory out of range prevent
  12.     IF x1 < 0 THEN x1 = 0
  13.     IF y1 < 0 THEN y1 = 0
  14.     w = _WIDTH(a): h = _HEIGHT(a)
  15.     IF x2 > w - 1 THEN x2 = w - 1
  16.     IF y2 > h - 1 THEN y2 = h - 1
  17.     '---------------------------
  18.    REM LINE (W(3).X1, W(3).Y1)-(W(3).X2, W(3).Y2), _RGB32(183, 194, 200), BF 'create border between software and hardware area
  19.  
  20.     y = y1 * w * 4 + x1 * 4
  21.     y2 = y2 * w * 4
  22.     xstop = x2 * 4 - x1 * 4
  23.     w = w * 4
  24.     DO UNTIL y > y2
  25.         o = m.OFFSET + y
  26.         _MEMFILL m, o, xstop, 0 AS _UNSIGNED _BYTE
  27.         y = y + w
  28.     LOOP
  29.     '_MEMFREE m 'No need to memfree as we keep the memblock available to point to other images in the future
  30.  

CLS do Alpha channel as 255 (Full color). This code do ALPHA as 0, so then is this layer transparent. (Original use for this is between hardware and software screens)

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Need my use of _MEM double checked Please.
« Reply #2 on: October 24, 2019, 02:26:16 pm »
Thanks, Petr. But even MEM will slow down if your running it over 2 million times(1472x1472 image=2166784pixels) at 60+ times a second(to look smoother when moving\scrolling).

The way I worked out seem to work, and is blazing fast compared to my old method. though being able to use it as a general way to erase any layer retaining a transparency color would be even better.

Granted after becoming radioactive I only have a half-life!