Wednesday, 31 August 2011

Little things make a big difference

Just wanted to share this post from Bob Keeney, one of the 'gurus' of RealStudio development:

BKeeney Briefs » Favorite New Feature in 2011 Release 3


If you use graphics a lot in your projects, you’ve probably found Real Studio to be a painful to use at times.  If you changed the graphic file, you would have to reload the project for it to take effect.  If you found that to be a pain too, then 2011 Release 3 contains much goodness for you.


I discovered this today while I was adjusting my window layout.  I decided that my BevelButton icon needed to be 48×48 rather than 32×32.  I had the Window editor still open when I copied the graphic with the same name as the old one into the same location.  A few seconds later the BevelButton updated with the new icon with no actions taken by me.  Yay!


Of course, it’s possible this has been in Real Studio for a couple of releases until now but I’ll give the credit for this release.  Sometimes it’s the little things that make you happy.  :)

Tuesday, 30 August 2011

Multithreading in RealStudio - avoiding a common pitfall

I've been working on a new project (more details later), which requires a background thread to perform some processing quite frequently. Everything was going fine and all testing was going great, until I started to increase the amount of data in the application.
Suddenly, things seemed to slow down dramatically. I thought this was  due to too much processing in the paint method of a custom control I had created, so I stripped it down as much as possible, removing a lot of the functionality which I would have to replace later. But, none of this seemed to fix the issue.
After a few days, I realised that there was one area, I had not touched: the background thread.

To cut a long (and quite painful) story short, I had treated the thread as I would have in Delphi, Objective-C or any other language I have used in the past. These other languages allow for true multithreading. RealStudio, does not. Rather than having the OS handle the threading, RealStudio uses it's own thread scheduler.

Turns out the fix was this was very simple. In the loop, within the threads execution, I place the following line:

App.YieldToNextThread

before the end of the loop. This allowed the application to perform other actions required in the main thread.

Wednesday, 24 August 2011

Some old, but useful code

Digging through some archives, I found some useful code, so I thought I would share these.

These snippets are from my Delphi programming days, so the code is in Pascal, but it can easily be converted to whichever language is being used.

Hide an application from the taskbar - Delphi

This is a really simple technique that can hide your application from the taskbar.In your main forms On Create event, type the following code:

SetWindowLong(Application.Handle, GWL_EXSTYLE, GetWindowLong(Application.Handle, GWL_EXSTYLE ) or WS_EX_TOOLWINDOW and not WS_EX_APPWINDOW);

That's all there is to it.

Making a program run at Windows startup - Delphi

On your programs Settings or Preferences form, place a TCheckBox with the caption Run at Windows Startup.

In the forms uses clause, include the unit Registry.

In the TCheckBox On Click event, include the following code:

procedure TForm1.TCheckBox1Click(Sender: TObject); 

     var Registry: TRegistry; 

 begin

      Registry := TRegistry.Create; 

      Registry.RootKey := HKEY_LOCAL_MACHINE;

      Registry.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run', false);

      If TCheckBox1.Checked then 

          Registry.WriteString(Application.Title, Application.ExeName) 

      else Registry.DeleteValue(Application.Title); 

      Registry.CloseKey; 

      Registry.free; 

end; 

To ensure that the TCheckBox displays the current status of the function when the form is displayed, we need to insert the following code into the forms On Show event: 

procedure TForm1.FormShow(Sender: TObject); 

    var Registry : TRegistry; 

begin 

    Registry := TRegistry.Create;

    Registry.RootKey := HKEY_LOCAL_MACHINE;

    Registry.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run', false); 

    CheckBox1.Checked := Registry.ValueExists(Application.Title); 

    Registry.CloseKey; 

    Registry.free; 

end;

This code simply checks to see if the run command for the appllication has been inserted into the registry. If it has, the the TCheckBox is checked, otherwise it is not.

Tuesday, 21 June 2011

Tip: Opening a file with a non-default application - Part II

Looking back at my last post, I just realised that the Mac version might not work properly. The shell path of a folder item in RealBasic is an 'escaped string'. This means that a file path such as /Users/markoxley/Desktop/Untitled Folder would end up as /Users/markoxley/Desktop/Untitled\ Folder (note the back slash before the space in 'Untitled Folder').

To fix this, we can implement a very simple function:

 

Sub UnEscape(value As String) As String

 Dim s As String

  Dim index as integer=0

  while index<Len(value)

    index=index+1

    if mid(value,index,1)="\" then

      if index<>Len(value) then 

        index=index+1

        s=s+mid(value,index,1)

      end if

    else

      s=s+mid(value,index,1)

    end if

  wend 

  Return s

End Sub

We would then replace the original subroutine with this:

 

Sub OpenFileWith(DocumentPath As FolderItem, ApplicationPath As FolderItem)

  Dim ShellString As String

  Dim sh As Shell=New Shell

  #if TargetMacOS

    ShellString="open -a '" + UnEscape(ApplicationPath.ShellPath) + "' '" + UnEscape(DocumentPath.ShellPath) + "'"

  #elseif TargetWin32

    ShellString=ApplicationPath.AbsolutePath + " " + DocumentPath.AbsolutePath

  #endif

  

  sh.Execute(ShellString)

End Sub

Sunday, 5 June 2011

Tip: Opening a file with a non-default application

I have worked on a couple of projects which have required me to designate an application to open a file and in some circumstances, this is not the default application. Therefore, I came up with a small routine in RealBasic to do this (only Mac and Windows I'm afraid)

 

Sub OpenFileWith(DocumentPath As FolderItem, ApplicationPath As FolderItem)

  Dim ShellString As String

  Dim sh As Shell=New Shell

  #if TargetMacOS

    ShellString="open -a '" + ApplicationPath.ShellPath + "' '" + DocumentPath.ShellPath + "'"

  #elseif TargetWin32

    ShellString=ApplicationPath.AbsolutePath + " " + DocumentPath.AbsolutePath

  #endif

  

  sh.Execute(ShellString)

End Sub

 

On a Mac, it also appears that you can open the application by just using its name, so this can also be addressed by the overriding the subroutine with the following:

 

Sub OpenFileWith(DocumentPath As FolderItem, ApplicationName As String)

  #if TargetMacOS

    Dim ShellString As String

    Dim sh As Shell=New Shell

    ShellString="open -a '" + ApplicationName + "' '" + DocumentPath.ShellPath + "'"

    sh.Execute(ShellString)

  #endif

End Sub

 

Hope this helps anyone else who needs to do something like this.

Friday, 28 January 2011

New direction

Well, it has finally happened. I have succumbed to the lure of the mobile market. I spent the last week teaching myself to program games for Android devices.

It turned out to be easier than I was expecting. Apart from getting my head around the Activity lifecycle, I thought it was quite straight forward.

This does not mean that I will be abandoning desktop platforms, I am hoping to produce desktop and mobile versions of future projects. I will also be looking at applications that will use both desktop and mobile devices in tandem, but more of that at another time.

As I am talking about mobile devices (actually, I'm typing this on my tablet), I have begun to use Evernote to store code snippets. This service, available at http://www.evernote.com, is a marvellous way to synchronise data between devices. I whole heartedly recommend checking it out.

Published with Blogger-droid v1.6.6

Tuesday, 18 January 2011

Back to work

Christmas is finally over and after a slightly extended break from coding, I am back at it.
I am revisiting an old project that I abandoned last year, Senet Collection. This time, I am using my framework, which is allowing me to concentrate more on the game, rather than the mechanics, although I have found a bug in the framework that causes labels to shift when a dialog is displayed over the top. Not sure what is causing this, but until I can find the problem, I have created a work around solution.
"What is Senet?" I hear you cry.
It is thought to be the forerunner of backgammon, as played by the ancient Egyptians. Unfortunately, the actual rules of the game have been lost in the mists of time, however, several people have attempted to work them out. In light of this, I have decided to create my computerised version with four sets of rules, as created by Gustave Jéquier, Timothy Kendall, R. C. Bell and Professor John Tait. There are other variations, but I will be sticking to the four mentioned.
Here are a couple of screen shots to wet your appetite.