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!
In the SVN version, DWScript now has experimental support for helpers. This is a generalization of Delphi’s class helpers, which can be applied to (almost) any type. You can also have multiple helpers per type.
Here is a summary of the changes of recent happenings in the SVN.
Note that this list doesn’t include the very latest changes and features, which will get an article of their own.
If you recognize the title of this article by Robert Lee, then chances are you’ve been around Delphi for a while! 🙂
Alas the optimalcode.com website and Robert Lee disappeared years ago without a trace, but the “Delphi Optimization Guidelines” (dating back from 2002-3003) has been safeguarded and preserved. Recently someone pointed to me that the mirror I had in my Links section had disappeared too…
Amongst the samples included with Delphi, you can find a “Meteors” clone. With SmartMobileStudio approaching the next beta milestone, I gave it a run at compiling old Delphi code, VCL, TCanvas-based, that was never intended to run Web-side… but now does! Click here or the image below to test it!
Oxygene syntax (aka Delphi Prism) for enumerations was recently added to DWScript, with both enum and flags contextual keywords.
Enumerations scoping was already in, but optional, using these keywords make scoping explicitly required.