Fields default values, anonymous records
Recent additions to DWScript… as well as for next Smart MS version 😉
Recent additions to DWScript… as well as for next Smart MS version 😉
Here is a small class to facilitate working with off-screen dynamic images in Smart MS: w3BufferedImage.zip (1kb)
It’ll be in the next Smart update, but you can already use it, it was introduced as part of WarTrail, as a way to optimize graphic elements that are complex and don’t change over several frames (text, tiled background, etc.).
You can use it to bundle graphic layer element, for instance in WarTrail the top & bottom areas (with scores & buttons) are in two distinct buffered images, and when you bring up the “menu” for a tower upgrade, that’s another buffered image.
To use it, you simply create it, and specify its size, f.i. a 200×50 buffer is created with
buffer := TBufferedImage.Create(200, 50);
then you need to setup its OnRedraw event, which is a procedure that passes the buffered image itself. This is here that you’ll have to (re)draw whatever your buffered image is meant to contain
buffer.OnRedraw := procedure (Sender : TBufferedImage) begin Sender.Canvas... your code here ... end;
Inplace of the anonymous methods, you can also use a standalone procedure or a regular method (and it might be preferable if what you draw isn’t trivial), as they’re all compatible, the compiler takes care of everything that would differentiate a “procedure” from a “procedure of object” or a “reference to procedure” in Delphi.
When you need to draw the buffered image, you can use one of its Draw() or DrawScaled() methods, basically telling it to redraw itself on another canvas. The available methods are currently:
// top-left anchor procedure Draw(dest : TW3Canvas; x, y : Float); overload; procedure Draw(dest : TW3Canvas; x, y : Float; const sourceRect : TRect); overload; // center anchor procedure DrawScaled(dest : TW3Canvas; x, y, scale : Float);
The first two are top-left anchored, ie. the x & y are the top-left of where the image will be drawn, and you can optionally specify a sourceRect, so that only part of your buffered image will be drawn (note that for tiled images, the w3SpriteSheet class is preferable, it offers more features like rotations and automatic sourceRect computations)
For DrawScaled, the x & y are the center of where the image will be drawn.
When you need the image invalidated, you just call it’s Invalidate method, then the next Draw call will first invoke OnRedraw. You can also use the Prepare method to have OnRedraw be invoked at a time of your choosing instead.
Last thing, if you need to draw with alpha-transparency (f.i. to fade-in or out), just use the GlobalAlpha property of the target canvas, f.i.:
myCanvas.GlobalAlpha:=0.5; // 50% opacity bufferedImage.Draw(myCanvas, 50, 50); // drawn at top, left = 50,50 myCanvas.GlobalAlpha:=1; // restore 100% opacity
That’s about it! Now you can efficiently make use of custom dynamic elements, such as multi-layered text with shadow, that would otherwise be too complex to redraw from scratch at each frame!
With the SmartMS release a deal has also been concluded between Creative IT and Optimale Systemer AS, to safeguard their investments and ensure future evolutions, the JavaScript CodeGen portion of DWScript will no longer be developed as open-source, but will be exclusive to Optimale Systemer AS. Sales permitting, this should allow to dedicate more time to improving JavaScript code generation, optimizations and debugging support 🙂
Support for partial classes has been added to DWScript: they can be declared as “class partial (TAncestor)” as well as with the Oxygene syntax “partial class (TAncestor)” syntax.
When “record with methods” were introduced, an important feature was overlooked: mutability.
This article discusses the problem, and introduces a possible syntax extension to solve it. Ideas & comments welcome!