Recommended Posts

The first language I learnt was Blitz Basic, which is all very well for writing simple 2d games but, for a mathematically-orientated application such as the first program I wrote, it's next to useless; because of all the unneccessary operations listed in its compiler, it takes anywhere up to an hour to render a desktop-resolution image. I thought I'd overcome the problem by rewriting the program in assembler but, being something of a novice at programming, wasn't really prepared for the sudden change in level required. I am certain that assembler is ideal for my purposes, but issues such as the fact that almost all the numbers I have to deal with are 2 words long and that I have no idea how to even read from a file mean that I am somewhat at a loss.
For those of you who know Blitz, the program in its current form is as follows:

To test it, type in the following inputs:
2
0
0
2
0
0
0
-2.25
1.25
1
0
0
0
-0.6875
0
1
0
0
0
0.875
1.25
1
0
1024
768
0
1500
The computer should enter full-screen graphics mode, generate a coloured image of the Mandelbrot set in about 10 seconds (depending on your computer's specifications) and then save it as fractal.bmp in the same directory as the compiled program. This is more or less what I want it to do, although it would be nice if it could render the image in the background or, better still, write straight to a bitmap file pixel by pixel. Also, I would prefer it to read the inputs from a csv file rather than needing an input dialog; this way, you would be able to run the program in a directory with the appropriate csv file, and generate a bitmap image file without anything actually appearing on the screen.
The main problem, though, is this: the program, as it currently stands, runs ridiculously slowly. That's why I wanted to rewrite it in some form of assembler, of course, but I'm at a bit of a loss as to where to begin, never having written in any sort of low-level language before.
Any tips or suggestions would be very much appreciated. Thanks,
Robin :)
[Edited by - Robin S on September 23, 2006 6:45:06 PM]

0

Share this post

Link to post

Share on other sites

Jumping from Blitz Basic to Assembler is quite a leap. You might want to try a higher level language instead, like C or C++, Pascal or Java. Mandlebrot sets tend to take a while to render, but an hour seems quite excessive (unless your PC is 10 years old).

Share this post

Link to post

Share on other sites

1 hour was the length of time needed for more complicated fractals (which required tens of thousands of iterations per pixel); the Mandelbrot set took about 10 seconds to render, but this is still quite excessive compared with some applets I have seen which can generate it more or less instantly.

I would like to learn C++, but don't have a compiler for it, and I understand manuals for it tend to be huge, which is a bit daunting. Thanks for the advice, though - I'll see what I can do.

They have 4 different compilers for download, C++, C#, VB, and J#. They are all free, easy to install, and the Visual Studio IDE is one of the best IDE's around. If you're more of an open source kinda guy u can pick up DJGPP (a GNU C/C++ compiler) at:

http://www.delorie.com/

Just follow the DJGPP link to the zip picker link.

There's also a ton of other C++ compilers out there for free, if those two aren't to your liking. As far as learning C++, well GameDev is a good place to start.

Share this post

Link to post

Share on other sites

I'd avoid trying to do this in straight-up assembly. In fact, its much more trouble than its worth to write a windows application entirely in ASM. I would recomend C or C++, the Visual C++ Express Edition already mentioned is an excelent compiler and is only second to Intel's own compiler in respect to its optimization technology. After you get a working version in straight C/C++, you can optimize the inner workings with inline assembly if you still find it to be to slow.

Truth be told, however, its unlikely you will be able to beat the compiler at optimizing your code since you're an ASM rookie. Compilers really are that good these days, as they will beat 90% of programmers 90% of the time. For someone not skilled in assembly, attempting to optimize code by converting it to assembly yourself will likely result in two things 1) much learned about assembly and 2) slower program execution. The 10% of programmers that DO beat the assembler, most often do so by exploiting knowlege of the algorithm that the compiler does not have, such as exploiting parallelism to compute multiple results at once. An example of this would be to convert a parallelizable algorithm to make use of the SSE instruction set. A smart programmer is better able to exploit parallelism than most compilers.

To make a long story short:

1) C/C++, get this working and evaluate the speed.2) Optimize the algorithm, then evaluate the speed again.3) Only then, if you're still unhappy with the speed, should you consider ASM.

Share this post

Link to post

Share on other sites

Ok, so I've started writing up the program in C. It hasn't taken long, but I've got stuck on the subject of output. Before I develop the program further, I want to be able to do in C what I did in Blitz: that is, output to a bitmap file. Here is the program so far (compiled using GCC):

The pixel's properties are determined by the values of r, s and v: r is the x co-ordinate, s is the inverse of the y co-ordinate i.e. it is 0 at the top of the image, and v determines the colour according to the following code from the Blitz program:

Share this post

Link to post

Share on other sites

The inputs are nondescriptive because some of them (particularly the arrays) are used for multiple purposes in order to save on memory. It's not so much how to use if/else statements that I'm looking for - I want to know how to write to a bitmap file. In the Blitz version of the program I did this by choosing a colour, then drawing that pixel on the screen, then finally when all pixels were drawn the program saved the buffer on which the image had been drawn. However, I'd prefer to be able to write to an image file without having to display the image on the screen.

Even more ideal would be to write straight to a PNG, but I'll stick with simple things first.

To give a rough idea of what the program does:

//take inputs;//do some maths;//create and set up 24-bit bitmap file, dimensions k by l, ready to write (not yet done);for(r=0;r<k;r++){ for(s=0;s<l;s++) { //do some more maths; //create pixel at r,l-s-1 coloured using v as in above post (not yet done); //do some more maths; } //do some more maths;}

Share this post

Link to post

Share on other sites

I probably will rewrite it in C++ once I have a working version in C. I'll need to so that things like the GUI are easier to write. For now, though, I just want a version that works faster than the one in Basic. Thanks for the advice, though. I'll keep what you said in mind.

Meanwhile, could anyone possibly help me towards getting the C version finished?

0

Share this post

Link to post

Share on other sites

Guest Anonymous Poster

Guest Anonymous Poster

just compile your C program as C++ (it's mostly backward compatible) and use the BMP library to spit it out. There's even an example mandelbrot generator on the site that does EXACTLY what you want to do: