iOS and Objective-C

For the past few month I have been learning and coding using Xcode in Objective-C and C++.

Here are the result:

https://itunes.apple.com/us/app/jie-qi/id854201316?ls=1&mt=8

It is a food suggestions app based on 24 chinese solar terms.

Apart from objective-c, I have also been learning and working on c++, using cocos2d-x making a lemonade stand game. So far I have touched plist configuration, cocos2d-ui, Game Center Leaderboard, Achievements, In-App purchases (using soomla), animation, sound, many areas I never tried on.

Right now its in the finishing stages.

For those who wants to learn cocos2d-x, I have posted all source up to date.

https://github.com/dkstar88/lemonade-jack

Using FireMonkey’s filter effect without visible controls

In my current application I have some images I need to add a glow effect on the image and convert it to an Windows icon.

The conventional way would be add a TImage with a TGlowEffect child control, and using MakeScreenshot to capture the result image. But I found this is not possible at least TGlowEffect do not work (or any other effects) when the control is not visible.

The problem lead me to dig into FMX.Effects, FMX.Filter and FMX.Filter.Custom. This is what I found.

  1. Filters code are in FMX.Filter.Custom, TGlowFilter, TInnerGlowFilter, etc.
  2. They are registered to TFilterManager
  3. TGlowEffect component and other effect components uses TFilterManager to work
  4. Filter’s parameter can be set using ValueAsFloat, and ValueAsBitmap etc as long as you know the key string, e.g. ValuesAsFloat['BlurAmount'] := FSoftness

 

Here is my code using GlowFilter manually.  Call GlowFilter.ApplyWithoutCopyToOutput will render a new image with the effect only, and I manually render the original image on top of it to make the actually resulted image.

      with TFilterManager.FilterByName('GlowFilter') do
      begin
        ValuesAsFloat['BlurAmount'] := 0.2;
        ValuesAsColor['Color'] := $FFFFFFFF;
        ValuesAsBitmap['Input'] := icon;
        ApplyWithoutCopyToOutput;
        icon2.SetSize(icon.Width, icon.Height);
        TFilterManager.FilterContext.CopyToBitmap(icon2, Rect(0, 0, icon2.Width, icon2.Height));
        icon2.Canvas.BeginScene();
        try
          icon2.Canvas.DrawBitmap(icon, RectF(0, 0, icon.Width, icon.Height),
         RectF((icon2.Width-icon.Width)/2, (icon2.Height-icon.Height)/2,
         (icon2.Width-icon.Width)/2+icon.Width,
         (icon2.Height-icon.Height)/2+icon.Height), 1);
        finally
          icon2.Canvas.EndScene;
        end;
        fTrayicon.SetImage('normal', icon2);

Delphi XE5 threading problem on Android

When I tried to make a a simple game for Android using Delphi XE5, I encountered an problem using TBitmap in threads.  When ever I use TBitmap in thread it just causes the App to quit back to the Launcher screeen.  Delphi doesn’t catch any exceptions.

In order to find out what the problem was, and to isolate the problem. I created a test project with 3 threads.

  1. loads a jpeg from file
  2. only creates 100×100 bitmap
  3. on top of (2), calls FillRect

This is what test project UI look like. Each button calls each thread.

2013101501

 

This is the result from Logcat. I pressed buttons from bottom up (since I knew which one fails).  The result is very interesting.

AndroidThreadFail

Thread 3: Thread executed from start to finish, but paint failed with “Cannot create render buffers from TContextAndroid” exception.

Thread 2: No exceptions

Thread 1: It caused the app to quit, it obviously failed. But looking at the log. I can see the thread actually ran from start to finish. There were no exceptions in the middle. Two exceptions were raised after the thread operation.

“10-15 11:51:25.283: I/info(1159): FMX: Project5: [Context Exception]: Cannot create OpenGL context for ‘CreateDummyContext’.
10-15 11:51:25.283: I/info(1159): FMX: Project5: [Context Exception]: Cannot create OpenGL context for ‘eglMakeCurrent’.”

 

 

For anyone wants to investigate this problem further, or just to try it out, here is the source code. threadtest.7z

(I first post this problem on stackoverflow.com)

Disk Cache Manager for String, TBitmap, TStrings, TStream data types

TDiskCache is a easy use local cache manager, stores data in a specified storage folder, all stored files are
stored in a flat dir structure, no sub folders, filenames are hashed using md5.

In the root folder, a hidden file “cache.ini” will be created to store cache time, and other data.

Full Source: https://gist.github.com/dkstar88/6129742

It requires HashUtil.pas, which can be also found at Gist.

Short hash function names

If you ever tried to use MD5 or SHA hash function came with Indy library, especially when you want to make a cross platform application using FireMonkey. You are pretty much locked to Indy, there may be many other better options, I only found Indy to be cross platform so far.

In Indy, to get a MD5 from a string, you need to do:

with TIdHashMessageDigest5.Create do
begin
Result := HashStringAsHex(S);
Free;
end;

Not too complicated, but it become obvious to create a wrapper function if you have lots of places needs md5 hashes.

So it becomes

function MD5(S: String): String;
begin
with TIdHashMessageDigest5.Create do
begin
Result := HashStringAsHex(S);
Free;
end;
end;

lMd5 = MD5('string to hash');

Full source can be found at gist.

generic Version structure utilizing operator override

A generic Version structure utilizing operator override feature in Delphi versions.

Currently it supports comparison, and not so useful addition and substraction.

Now you can do
TVersion('1.0.2.3') > TVersion('1.0.2.2')

I also put AppVersion, which returns the version of your running application for both Windows and MacOS. So checking where if the current running app is the latest version.

// get LatestVersion info from the net
if (LatestVersion > AppVersion) then
begin
// Pull latest version from the net
end;

 

ImageLoader for FireMonkey TImage component

A ImageLoader for FireMonkey TImage component. I written this when I found I need to load a few images from the Internet. Obviously delphi isn’s browser, if I just call download using Indy it will just block my app’s UI.

ImageLoader is very simple class with a loading queue, and a timer to trigger the work on the queue. You just need to call the LoadImage from TImageLoader, you don’t even need to create TImageLoader there is a default “DefaultImageLoader” already created on unit initialization. A simple cache machanism is also there to prevent waste of bandwidth.

DefaultImageLoader.LoadImage(
Image1,
'http://www.google.com/images/errors/robot.png'
);

What’s next

Right now, ImageLoader only loads the image for TImage, it doesn’t fire any event when the image is ready, so post processing is not available. That will be added in the future. Ajax waiting animation will be added as option.

Full Source:

WebLabel

Features of TWebLabel

TWebLabel is recently added graphix component pack for FireMonkey. I was looking for a a URL label component and found  TUrlLabel from monkeystyler.com.
But it doesn’t have a dedicated field to store URL, it treat text as url link. Thankfully the source code is there.

I made many changes to the orignal code.

TWebLabel-Properties

  • Added TFontTextSettings – TTextSettings with Font and FontColor exposed as published properties
  • Added FontNormal, FontHover, FontDown property to change label appearance on different mouse state
  • Added a dedicated attribute to store url
  • Changed parent object to TText rather than TLabel

WebLabel

Source Code:

 
Download: https://github.com/dkstar88/graphix/archive/master.zip