migeel's demoscene corner: 4k Delphi intro



If you look at the title you probably think I am crazy. No, I am not. It is really possible to make a pretty nice 4k intro in Delphi. This tutorial will show you, how to do it.

At first, you will have to download some tools from the internet:
Now, let's start Delphi and try to compile an empty form and look at the size of executable. Huh, 360 kB... this is not funny. Now click on Project->View source. Leave here just this:

program Project1;

uses
  windows;

begin

end.
Compile it again and look at exe size. This looks much better - only 8k, but still too much for us. Now it's time to unpack new System units. Unpack them and in Delphi click on Tools->Environment options. Select Library page and enter path to your new System units to the list (they have to be the FIRST in the list). Now click OK and compile again. 5632 bytes! That's very good (keep in mind, that we'll compress it later).
We can now decide - will we use RegisterClass, CreateWindow, TranslateMessage,... API functions or we'll implement the main window as a dialog from resources. I think, that dialog is a better (and smaller) choice, but you can also try the second option.
So, let's make the dialog. Start Notepad and type this:

MAIN DIALOG 0, 0, 320, 240
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "4k Delphi intro: POSSiBLE"
BEGIN

END
Save it as dialog.rc, compile it to dialog.res (brcc32.exe dialog.rc) and copy it to your project directory. In your project, add {$R dialog.res}, include a DialogBoxParam call and viola; your first 4k intro is complete. I made a little example, so you can understand it better. It sucks, but is very easy to understand (no unnecessary code).
But hey! It has still 6656 bytes! This is not a 4k intro! Of course, I forgot: this is not important for this intro (it is small enough), but you will need it, if you make your 4k intro: download ResHacker or any other resource editor and open our intro with it. You will see it has some strange resources: DVCLAL and PACKAGEINFO. They aren't important for the application. Delete them. Now it's time for my tool: Realign. This program deletes DOS stub from your file and realigns it. Type "realign.exe project1.exe" and application size will decrease to 6319 bytes. Still too much, huh?
Unpack FakeCom to your project's directory and type:

copy /B fakecom.com+project1.exe project1.com

This command merges FakeCom.com and Project1.exe together. FakeCom is a small program, which extracts data contained at the end of file (our exe file) and runs it. This is important because we want to compress WHOLE file (EXE headers normally don't get compressed because Windows would refuse to run it).
Now pack the new com file with UPX (upx.exe --best project1.com) and look at the new size. UPX did his job very good and packed our Delphi exe to unbelievable 2835 bytes! The best thing about this is, that the intro still works :-)
As you see, there is enough place for some useful code, so stop reading this article and go code something useful!

To make your life easyer, create a batch file, which will do the hardest job for you:

makeit.bat:
@echo off
start /W reshacker.exe
realign project1.exe
copy /B fakecom.com+project1.exe project1.com
upx --best project1.com

echo Run intro now?
choice
if NOT ERRORLEVEL 2 project1.com