| 09-27-2009, 02:09 PM | #1 |
Hi! I'm currently working on a small scale and fade snippet, and I've gotten the "scale" part done, bsaically... Now, it's just the "fade" part left, and I thought it would be basically the same, but no no, it's giving me quite a hard time XD So, atm it's using a constant timer of 0.03125 interval, using Timer32 by Jesus4Lyf from TheHelper.net (Please don't mind this, I could've used a local timer aswell). Oh, well here's my code atm: JASS:library ScaleandFade uses T32 struct SmoothScale private unit u private real scale private real endScale private real increment private method periodic takes nothing returns boolean set this.scale = this.scale + this.increment if this.scale > this.endScale then call this.destroy() return true endif call SetUnitScale(this.u,this.scale,1.,1.) debug call BJDebugMsg( R2S( this.scale) ) return false endmethod implement T32 static method create takes unit u, real startScale, real endScale, real time returns thistype local thistype this=thistype.allocate() set this.u = u set this.scale = startScale set this.endScale = endScale set this.increment = (endScale - startScale) / time * T32_PERIOD call this.startPeriodic() return this endmethod endstruct struct SmoothFade private real alpha private real increment private real endAlpha private unit u private method periodic takes nothing returns boolean set this.alpha = this.alpha + this.increment if this.alpha > this.endAlpha then call this.destroy() return true endif call SetUnitVertexColor( this.u, 255, 255, 255, R2I( this.alpha ) ) //call BJDebugMsg( R2S( this.alpha ) ) call BJDebugMsg( R2S( this.increment ) ) call BJDebugMsg( "Hellu!" ) return false endmethod implement T32 static method create takes unit u, real startAlpha, real endAlpha, real time returns thistype local thistype this=thistype.allocate() set this.u = u set this.alpha = startAlpha set this.endAlpha = endAlpha set this.increment = ( endAlpha - startAlpha ) * ( time * T32_PERIOD ) call this.startPeriodic() call BJDebugMsg( R2S(this.alpha)) call BJDebugMsg( R2S(this.increment)) return this endmethod endstruct endlibrary And my ongoing problem with this is that it keeps "breaking my thread", meaning that it just stops, and won't keep going. And right now, only the ".create" method is executed, as seen by the debug msg'es... Any ideas to why the ".periodic" method won't run ? :S (If you wanna see T32, here it is:) JASS://~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //~~ Timer32 ~~ By Jesus4Lyf ~~ Version 1.05 ~~ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // What is Timer32? // - Timer32 implements a fully optimised timer loop for a struct. // - Instances can be added to the loop, which will call .periodic every // PERIOD until it returns true. // // =Pros= // - Efficient. // // =Cons= // - Only allows one period. // - The called method must be named ".periodic". // // Methods: // - struct.startPeriodic() // // - private method periodic takes nothing returns boolean // // This must be defined in structs that implement Periodic Module. // It will be executed by the module every PERIOD until it returns true. // Put "implement T32" BELOW this method. // // Details: // - Uses one timer. // // - While .periodic() returns false the timer will continue to call it each period // Once .periodic() returns true the instance will be excluded from iteration. // // - When there are no active instances, the timer for that struct type will // pause itself. // // How to import: // - Create a trigger named T32. // - Convert it to custom text and replace the whole trigger text with this. // // Thanks: // - Infinitegde for finding a bug in the debug message that actually altered // system operation (when in debug mode). // //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ library T32 initializer OnInit globals public constant real PERIOD=0.03125 public constant integer FPS=R2I(1/PERIOD) //============================================================================== private trigger Trig=CreateTrigger() endglobals module T32 private thistype next private thistype prev private static method PeriodicLoop takes nothing returns boolean local thistype this=thistype(0).next loop exitwhen this==0 if this.periodic() then // This is some real magic. set this.prev.next=this.next set this.next.prev=this.prev // This will even work for the starting element. debug set this.prev=0 endif set this=this.next endloop return false endmethod method startPeriodic takes nothing returns nothing debug if this.prev!=0 or thistype(0).next==this then debug call BJDebugMsg("T32 ERROR: Struct #"+I2S(this)+" had startPeriodic called while already running!") debug endif set thistype(0).next.prev=this set this.next=thistype(0).next set thistype(0).next=this set this.prev=thistype(0) endmethod private static method onInit takes nothing returns nothing call TriggerAddCondition(Trig,Condition(function thistype.PeriodicLoop)) endmethod endmodule private function OnInit takes nothing returns nothing call TriggerRegisterTimerEvent(Trig,PERIOD,true) endfunction endlibrary |
| 09-27-2009, 02:44 PM | #2 |
You should have some struct for storing units current numbers, so you can have multiple fades going on, without fucking things up. Using some unit indexing system for getting indexes for your struct array is probably a good idea. |
| 09-27-2009, 02:54 PM | #3 | |
Quote:
Ok... Which one would you suggest I use ? :o (And, I'm a "Not-Noob-but-definately-not-Pro" in using vJASS, so if you could maybe at least guide me to the right path of implementing it, that'd be nice !!! ;)) But anyways, atm it's the "not-working" part I'm trying to fix :P And, I just discovered another thing... My "scale" struct doesn't work with negative values, like this works: Trigger: But this doesn't: Trigger: And, if you want to know... This is the trigger I'm using for testing the "fade" struct: Trigger: |
| 09-28-2009, 07:32 PM | #4 |
Ok... I've extended this snippet/system quite a bit now, though I might remove some stuff XD Well, anyways there are some "Why"s I want to get answers to:
Ok, I've now fixed all "problems" with it, and it's only that one question left now ! :D (It was a few maths issues XD) But, I really want the user to be able to have callback on it, and also a boolean so the user can specify if he wants te callback on each interval, or only on the last one :D Well, here's the updated code : JASS:library ScaleAndFade uses KT globals private constant real PERIOD = 0.03125 endglobals struct SmoothScale private unit u private real scale private real endScale private real increment //private code userFunc private static method periodic takes nothing returns boolean local SmoothScale this = KT_GetData() set this.scale = this.scale + this.increment if ( this.scale > this.endScale and this.increment > 0 ) or ( this.scale < this.endScale and this.increment < 0 ) then call this.destroy() //call this.userFunc.execute() return true endif call SetUnitScale( this.u, this.scale, 1., 1. ) debug call BJDebugMsg( R2S( this.scale ) ) debug call BJDebugMsg( R2S( this.increment ) ) return false endmethod static method create takes unit Scaler, real startScale, real endScale, real Time, real Period, code userFunc returns thistype local thistype this = thistype.allocate() set this.u = Scaler set this.scale = startScale set this.endScale = endScale //set this.userFunc = userFunc if Period <= 0 then set this.increment = ( ( ( endScale - startScale ) * PERIOD ) / Time ) call KT_Add( function thistype.periodic, this, PERIOD ) else set this.increment = ( ( ( endScale - startScale ) * Period ) / Time ) call KT_Add( function thistype.periodic, this, Period ) endif return this endmethod endstruct function ScaleSimple takes unit Scaler, real startScale, real endScale, real Time returns SmoothScale return SmoothScale.create( Scaler, startScale, endScale, Time, 0., null ) endfunction function ScaleSimplePeriod takes unit Scaler, real startScale, real endScale, real Time, real Period, code userFunc returns SmoothScale return SmoothScale.create( Scaler, startScale, endScale, Time, Period, null ) endfunction function ScaleEx takes unit Scaler, real startScale, real endScale, real Time, real Period, code userFunc returns SmoothScale return SmoothScale.create( Scaler, startScale, endScale, Time, 0., userFunc ) endfunction function ScaleExPeriod takes unit Scaler, real startScale, real endScale, real Time, real Period, code userFunc returns SmoothScale return SmoothScale.create( Scaler, startScale, endScale, Time, Period, userFunc ) endfunction struct SmoothFade private real rincrement private real gincrement private real bincrement private real alpha private real increment private real endAlpha private unit u private static method periodic takes nothing returns boolean local SmoothFade this = KT_GetData() set this.alpha = this.alpha + this.increment if ( this.alpha > this.endAlpha and this.increment > 0 ) or ( this.alpha < this.endAlpha and this.increment < 0 ) then call this.destroy() return true endif if this.rincrement == 0 and this.gincrement == 0 and this.bincrement == 0 then call SetUnitVertexColor( this.u, 255, 255, 255, R2I( this.alpha ) ) else call SetUnitVertexColor( this.u, R2I( this.rincrement ), R2I( this.gincrement ), R2I( this.bincrement ), R2I( this.alpha ) ) endif debug call BJDebugMsg( R2S( this.alpha ) ) return false endmethod static method create takes unit Fader, boolean StaticRGB, real startAlpha, real endAlpha, real Time, real Period, real startRed, real startGreen, real startBlue, real endRed, real endGreen, real endBlue returns thistype local thistype this = thistype.allocate() set this.u = Fader set this.alpha = startAlpha set this.endAlpha = endAlpha if Period <= 0 then set this.increment = ( ( ( endAlpha - startAlpha ) * PERIOD ) / Time ) if StaticRGB != true then set this.rincrement = ( ( ( endRed - startRed ) * PERIOD ) / Time ) set this.gincrement = ( ( ( endGreen - startGreen ) * PERIOD ) / Time ) set this.bincrement = ( ( ( endBlue - startBlue ) * PERIOD ) / Time ) endif call KT_Add( function thistype.periodic, this, PERIOD ) else set this.increment = ( ( ( endAlpha - startAlpha ) * Period ) / Time ) if StaticRGB != true then set this.rincrement = ( ( ( endRed - startRed ) * Period ) / Time ) set this.gincrement = ( ( ( endGreen - startGreen ) * Period ) / Time ) set this.bincrement = ( ( ( endBlue - startBlue ) * Period ) / Time ) endif call KT_Add( function thistype.periodic, this, Period ) endif return this endmethod endstruct function FadeSimple takes unit Fader, real startAlpha, real endAlpha, real Time returns SmoothFade return SmoothFade.create( Fader, true, startAlpha, endAlpha, Time, 0., 255., 255., 255., 255., 255., 255. ) endfunction function FadeSimplePeriod takes unit Fader, real startAlpha, real endAlpha, real Time, real Period returns SmoothFade return SmoothFade.create( Fader, true, startAlpha, endAlpha, Time, Period, 255., 255., 255., 255., 255., 255. ) endfunction function FadeEx takes unit Fader, real startAlpha, real endAlpha, real Time, real startRed, real startGreen, real startBlue, real endRed, real endGreen, real endBlue returns SmoothFade return SmoothFade.create( Fader, false, startAlpha, endAlpha, Time, 0., startRed, startGreen, startBlue, endRed, endGreen, endBlue ) endfunction function FadeExStatic takes unit Fader, real startAlpha, real endAlpha, real Time, real startRed, real startGreen, real startBlue, real endRed, real endGreen, real endBlue returns SmoothFade return SmoothFade.create( Fader, true, startAlpha, endAlpha, Time, 0., startRed, startGreen, startBlue, endRed, endGreen, endBlue ) endfunction function FadeExPeriod takes unit Fader, real startAlpha, real endAlpha, real Time, real Period, real startRed, real startGreen, real startBlue, real endRed, real endGreen, real endBlue returns SmoothFade return SmoothFade.create( Fader, true, startAlpha, endAlpha, Time, Period, startRed, startGreen, startBlue, endRed, endGreen, endBlue ) endfunction function FadeExPeriodStatic takes unit Fader, real startAlpha, real endAlpha, real Time, real Period, real startRed, real startGreen, real startBlue, real endRed, real endGreen, real endBlue returns SmoothFade return SmoothFade.create( Fader, false, startAlpha, endAlpha, Time, Period, startRed, startGreen, startBlue, endRed, endGreen, endBlue ) endfunction endlibrary Sorry for all the code, but I want you to have all the information you'll need ;) |
