Folder and files
The first step you have to do is create your Blitzmax project and set up the necessary files. We are going to start by creating a new folder anywhere you like on your computer. Let's name the folder Space Invader Emulator. Inside this folder, we are going to put the source files (i.e.: with the BMX extension) and a subfolder to store necessary data files. These data files should go in a folder named data. I have prepared a ZIP containing everything necessary for this tutorial right here: data.zip It contains the following:
COPYRIGHT WARNING ABOUT INVADERS.ROM:
This file, even if very old, is still copyrighted to its rightful owner. It is provided here for convenience only and used under the 'fair use' principle in that it's:
- A non-lucrative project
- It does not impede or prevent its owner of any monetary profit
- Educational purpose
If for any reason you have any objection of having this ROM file
available here, I will take it out immediately.
Aside from the ROM file which contains the actual code that was in a memory chip on the real machine, we have 12 WAVfiles. These sounds are actual sample of the real machine. Since we are not going to emulate the sound board, we are going to use these WAV to provide sound effects in our emulator. Now that files have been taken care of, let's create the main project file, called Space Invader.bmx. You can create the file directly in Windows or start up MaxIDE and create a new empty file. It will be named 'untitled1.bmx' by default. Now save the file and MaxIDE will ask for a proper name and let you select the destination folder at the same time. The file, as said earlier, should go directly into your Space Invader Emulator folder.
The barebone code
Ready for some typing? Good! We are going to create the basic barebone structure need for this project. Enter the code as follows:
SuperStrict
Init()
MainLoop()
End
Function Init()
DebugLog("Emulator starting...")
SetGraphicsDriver(GLMax2DDriver())
Graphics 800,600
End Function
Function MainLoop()
While Not AppTerminate() And Not KeyHit(KEY_ESCAPE)
Update()
Wend
End Function
Function Update()
Cls()
SetColor(255, 255, 255)
DrawText("Space Invader emulator started. Press ESCAPE to quit.", 0, 0)
Flip(0)
End Function
Although this is mostly straight-forward Blitzmax code, let's explain some of the things in detail in case there are Blitz-newbies here:
SuperStrict
This sets Blitzmax in the most strict coding standard: The greatest benefit is that is prevents you from accidentally create a new variable or confuse size, and it is a fairly good practice for 'heavier' language such as C++.
SetGraphicsDriver(GLMax2DDriver())
This call ensure that OpenGL will be used on all platform. By default, on Windows, Blitzmax tries to use DirectX but alas it doesn't seem to work that well on Windows Vista. Furthermore, using OpenGL makes your code compatible with Mac OS, Linux and Windows. In our case, we are not going to be using much of the graphical API anyway, so the choice of graphic driver is not so much of an issue.
While Not AppTerminate() And Not KeyHit(KEY_ESCAPE)
Update()
Wend
This will be our program main loop, calling Update() every frame. You can see by looking at the condition that two things can make the loop stops: AppTerminate(), which happens if the user closes the window or quit the application, and KeyHit(KEY_ESCAPE) which detects if the user presses on the escape key.
Then, the Update() function clears the screen, draw some text, and ends by calling Flip() to make it visible on the screen.
Flip(0)
Some people might ask 'Why Flip(0) instead of simply Flip()?' Flip(0) makes the flip 'as soon as possible', which is what we will be needing eventually since we are going to handle the frame rate by ourselves. While Flip() does wait for vertical retrace, it does not work very well on OpenGL windowed application.
