Pascal: The best of both worlds

We regularly get emails saying “Why aren’t you using a normal language like C++?“. That’s often followed by “If you want to move to C++ I can help you”, as if they are offering us salvation from Pascal. The reasons people give for C++ being a better choice than Pascal are usually cross platform support, performance and being widely used. This is something I feel quite strongly about so it seems like a good article to start off the blog (and from now on when we get those emails I can just send people a link to this article :P).

This is not a “why Pascal is better than C” article, but instead I will explain why Pascal is an appropriate language for the KaM Remake project.

Pascal is an old language published in 1970. It was made popular in the 1990s by a commercial product called Delphi (which is still being developed and sold today) and more recently by Free Pascal and Lazarus which offer a free and open source alternative to Delphi.

Note: When I refer to Pascal in this article I mean Object Pascal (Delphi and Lazarus), not plain Pascal which doesn’t have objects/classes.

Requirements

What are the language requirements of a project like the KaM Remake? We don’t require blisteringly fast performance and manual memory management that you might need on something like a modern first person shooter rendering engine, but we do need fairly good performance because we are dealing with a real-time multiplayer game on large maps (256×256 = 65,536 tiles) and possibly thousands of units existing at once, all of which require pathfinding and other CPU intensive operations. We also want it to run on fairly low end PCs. This rules out a lot of high level languages for performance reasons.

High level features

To me Pascal seems like a good compromise between higher level language features and performance. It compiles straight to machine code and doesn’t have the features that often slow down very high level languages such as virtual machines/interpreted code, fully automated garbage collection, implicit typing, etc. I’m not saying any of these are bad features, but they do have a performance cost. However, Pascal does support some high level features such as inheritance, abstract classes, overloading, properties, sets, easy to use strings and arrays, etc. which make it easier to use than low level languages like C++.

Object oriented
The object oriented features of Pascal have allowed us keep the project maintainable and easy to add new features as it continues to grow in size. Through my University course I’ve had to work with Java a lot, and I’ve been impressed (and sometimes frustrated) by the multitude of object oriented features it supports. However, in my opinion Object Pascal supports most of the really useful object oriented features that Java has whilst still having  efficiency that can compete with a low level language like C++.

Sets, arrays and strings
I think sets are one of the really interesting features of Pascal. There are number of places in our code where it would be painful to use anything else. Sets are not a low level concept (consider their importance in mathematics) and yet they can be compiled into very efficient machine code using bitmasks and bitwise operations.

Strings are built into the language and can be used much like you would expect in a high level language.

Arrays are also a pleasure to use. If you don’t need to change the size you can use a static array (which allows the compiler to perform more optimisation), and if you do need to change the size you can use a dynamic array. Multidimensional arrays are straight forward. You can also index static arrays by enumerators which is very useful to make lookup tables and avoid using meaningless numbers (for example Land[X,Y].WalkConnect[wcRoad] is much more meaningful than Land[X,Y].WalkConnect[2]).

Performance

Pascal has good performance. I’ve never tested it myself but from what I’ve read both Delphi and FPC are comparable with C++, and certainly well ahead of higher level languages like C# and Java.

I like the fact that you can still use low level features like pointers and embedded assembly code, but you rarely ever want to or need to. For example you can treat arrays and strings as blocks of memory like you would in C/C++, which is useful for loading, saving or transmitting them. This also allows gives you more control when optimising your code.

Cross platform support

The Free Pascal Compiler (FPC) compiles for Windows, Linux, Mac, even Raspberry Pi and Android. Lazarus can be used as a graphical front end to Free Pascal, like Delphi but free. We have made our code compile on Linux without much difficulty, but we still need to sort out some libraries and system calls to get everything working smoothly (you’d have this problem in other compiled languages such as C++ too, porting always takes a bit of effort because libraries and system calls vary between operating systems). So far we haven’t put effort into making and maintaining a Linux build because all our Linux users say it runs perfectly under Wine (Windows Emulator). There’s been very little interest in a Mac port, so we haven’t experimented there either. We’ve ported the server to Linux, that was very easy as it doesn’t use many libraries (just networking really). Somebody even compiled the Linux version of the server to Raspberry Pi (without any code changes required) which I think is pretty cool (that’s a whole different CPU architecture after all).

Popularity

Pascal is certainly an uncommon language, although mostly thanks to Free Pascal and Lazarus there’s still an active community. There’s plenty of documentation and example code available on the internet. Pascal is an easy to learn language and the structures are similar to other object oriented languages. Translating code between Pascal and C# or Java is not hard, we’ve done this for certain algorithms that we borrowed which were written in another language. Being a good programmer is not about knowing the syntax of a language, but about knowing concepts and being able to apply them regardless of the language.

Using a more popular language does not necessarily mean that we would have more developers willing to assist us. When people have contacted us saying they are interested in helping us with development but do not want to learn Pascal, we have suggested they make tools/utilities that are needed or write DLLs in their language of choice, yet no one has taken us up on these offers. We’ve also had various Pascal developers join us for a short time, but they all lost motivation and disappeared quite quickly. So it seems like it’s not the language that’s the limiting factor, just a lack of motivated developers.

It’s worth noting that some widely used software such as Skype and InnoSetup were written in Pascal, both of which are successful and well known.

Libraries

We haven’t had any problems finding libraries to use, in fact there are some really excellent libraries written for Pascal. For example our crash reporting library madExcept is astonishingly good, it has almost zero overhead yet provides a complete stack trace (including line numbers!) as well as lots of other useful information, and uploads this along with any other files we choose to include such as a replay of the game to our server as a ZIP file. This has made fixing crashes extremely easy for us and greatly sped up our community beta testing process. PascalScript is another excellent library that we use for dynamic mission scripts. It compiles and runs Pascal code written by map authors to allow missions to be dynamic and complex in ways which were never possible before. It was very easy for us to use and seems to run very efficiently. We also had no problems finding up to date wrappers for well known libraries such as OpenGL, OpenAL and zlib.

Conclusion

I’m not saying Pascal is the perfect language for everything, if efficiency doesn’t matter so much then there are higher level languages that have much better cross platform support and higher level abstractions/features. I also don’t have anything against low level languages like C++, sometimes you need the extra control and speed of a lower level language. If I was writing a high performance computation algorithm which required extreme optimisation and manual memory management, C++ would be a better choice. But for a project like ours Pascal seems like a good compromise. It has the efficiency of low level languages yet some of the features of high level languages which make development faster and easier to maintain. In my opinion it really does give us the best of both worlds.

This entry was posted in Pascal. Bookmark the permalink.

20 Responses to Pascal: The best of both worlds

  1. Tim Fennis says:

    I don’t like Pascal, but I can’t argue with your decision to use it. You and krom have done an amazing job rebuilding Knights and Merchants. I think that your probably wouldn’t have gotten this far if you had used a different language. Working in a language you are comfortable with is much better than using a language that’s arguably better.

    In my personal opinion making a game in C++ is ridiculous anyways. Anyone that argues that C++ is the best language for video games based on performance has never really made a video game. Using object oriented programming and magic objects that automatically allocate memory is terrible for performance and should be avoided in those cases. If you really want to build a high performance 3D rendering engine you combine C and Assembly, if you don’t you pick a language that you like.

    Good job guys <3
    Thank you so much

    • Lewin says:

      Thanks for your kind comments 🙂
      I agree that we probably wouldn’t have gotten this far in another language.

      “Using object oriented programming and magic objects that automatically allocate memory is terrible for performance and should be avoided in those cases”
      Objects in Pascal are quite efficient and we certainly wouldn’t be able to maintain the project at its current size without object oriented programming. I think that the object oriented features provided by Pascal are a good compromise between abstraction and performance. I believe that fully automated garbage collection (like Java has) is a waste of performance considering that it’s not so hard to free objects when you’ve finished with them, in fact I’m considering writing an article on that (we had to implement a system to keep track of references for unit and house objects).

      We’re proud of the codebase because it’s almost all well structured and easy to maintain, Krom has done a great job refactoring poorly structured bits when the need arises. The reason we’re still developing it today is that it’s still enjoyable and feasible to work on and add new features to. The code doesn’t feel old and stale like some of my earlier projects do.

  2. Ben Hymers says:

    “Using a more popular language does not necessarily means that we would have more developers willing to assist us.”

    You’d have more developers able to assist you though. When I first heard about the project, the first thing I did (after installing and playing the game of course!) was browse the source to see if it would be simple enough to improve the AI, but I stopped as soon as I saw it was written in Pascal. That’s just too big a barrier to entry, sorry! I understand how Pascal is an appropriate language for the project, but there are plenty of other languages that are appropriate too, and I think some of them (yes, C++) would be better choices.

    • Krom says:

      I believe the biggest problem with AI is not the language itself. It may seem so for a start, but when I tried writing the thing in diagrams and on paper, making a plan I quickly realized that Pascal is little/none involved. The actual big thing is design, architecture. There are many C++ specialists and amateurs around, but how many of them have a clue about AI and how many are good at it? I would wild-guess that there are less than 1%. Some know buzz-words like “neural networks” and “fuzzy logic”, but do not really know how to use them …

      In the end, programming is not about typing the code, but about mindset and problem-solving. After being able to design AI “on paper”, translating it to Pascal/C#/Java code is rather trivial 😉

  3. Beast says:

    You seem to be misguided about C++. C++ can be used in a low-level way, yes, but it doesn’t need to be in any way. The language itself has plenty of high-level features. The problems with C++ is just that the STL (the standard library of C++) is a pain in the ass. However, there is the Qt library which can be used as replacement of the STL which makes using C++ a lot more comfortable. I would recommend anybody who is learning C++ to try to use Qt for some time, to understand how C++ can look like when done properly.

  4. If you work in the industry as game developer you will learn its not about the language you use, but about the way you’ll use it. Thats why I have to disagree with saying any other language could make a significant difference.

    Even the best (not yet existing) feature-rich, object-oriented and performant language will be shit if you don’t know how to use it.

    So staying with Pascal – even tho you won’t be able to find additional developers easily – was possibly the better decision for the project.

    However, I’ve worked on a lot of projects using popular languages like Java and PHP. We didn’t find any appropriate developers aswell, because basically most of these ‘enthusiats’ were not able to adapt to any other language than those that they learned first.

  5. David says:

    C# has about the same performace as C++, many banchmarks prooves this. It runs on a virtual machine, but it’s compiled to machine code at the execution of the program. I think C# could have been used to develop KaM Remake too. The only thing I miss from it is that there is no inline assembly, it can’t emit packed SSE instructions for vector operations, and it doesn’t have a perfect pointer and fixed size array support.

    In my opinion C++ makes the developers life much harder. Game developers doesn’t use much of the features of C++, rather only use which is also supported by C (except basic OOP) to prevent the code becoming too complicated. I hate C++, so I agree with you entirely for not using it.

    I used Pascal, but I don’t know it much. I don’t like its syntax and miss some high level features, e.g. lamda functions. It’s not a bad language though. I don’t know any language that I think it’s perfect, so I’m trying to create one, but I didn’t expect it to take so much time, it’s a really hard task. Also what I like in a language is not surely good for other people.

    • steveE says:

      well no C# is fast enough, but its not quite as fast as C++ (or object pascal for that matter), although if raw speed is the issue you would be rolling your own compiler and / or embedding assembler in the source at bottlenecks

      the biggest problem with languages like c# and java in my opinion is they force you into an object orientated straight jacket. They believe in the silver bullet of OO, but really it’s a sledgehammer to crack a nut that often needs a whole bunch of useless boiler plate code where a simple function would be a simpler aproach.

      With C++ or OP you have a choice. The bits that work best with an OO approach you use OO. The bits that work well with a meta approach you use meta programming (templates in C++, generics and meta classes in OP). The bits that work best with a functional approach, you just use use straight functions.

      Not to mention you can inline assembler, manually manage memory etc..

      That said c# has excellent support through XNA. Unity supports it pretty much out the box, but you can get OP and C++ plugins for unity.

      C++ would be my choice, just because there are so many gaming libraries, frameworks and example projects for it.

      OP would possibly be a better game making language than c++, as it does everything C++ does at almost the same performance (except multiple inheritance which is generally a bad thing anyway) but it has more safety features like finally blocks, strongly typed pointers, built in range and overflow checking and a much much better class construction model than all the C family of languages (large OP projects don’t need a ton class factories and associated boilerplate because OP constructors are basically static functions that return an instance of self, you can declare multiple constructors and you can even make them dynamic)
      BUT
      OP doesn’t have the wealth of examples, engines, frameworks etc.. you get with C++, C#, java etc..

  6. Belzurix says:

    I’m quite sure you chose the right language for this project because your speed and quality of the game is amazing . As I heard, the greatest enemy of game developers is ( or at least seems to be ) the horrible concurrency model named shared state. They complained about this more than once. I’d like to ask that have you had any problems connected with the management of threads?

    • Lewin says:

      We actually use very little concurrency in the KaM Remake. We have a separate thread for scanning maps so the lists can be populated quickly (and without making the interface lag) but apart from that the KaM Remake is single threaded. We have discussed splitting the game logic into a separate thread from the rendering and user interface, but it would be an enormous amount of work for little gain. Performance isn’t really a problem for the project and we’re still finding significant optimisations to make (this upcoming release might actually perform better than the last one even though we’ve added a lot of new features).

  7. Encaitar says:

    Really lovely to read this article, as a Java programmer it’s always nice to read a article about technical stuff behind the game. You did well to write this article, as it explains why you choose Pascal quite good.

    “Being a good programmer is not about knowing the syntax of a language, but about knowing concepts and being able to apply them regardless of the language.”

    Couldn’t agree more. Logic, concepts and algorithms are what’s important, syntax is just a matter of taste (and after a time something you could get used to).

  8. steveE says:

    oh another thing

    c++ isnt a low level language

    assembler is a 1st gen language
    languages like c and turbo pascal are 2nd gen languages
    languages like C++ and object pascal are 3rd gen languages, in that they are OO, but you can still use them in a non OO way and they compile to straight exes
    languages like C# and java are 4th gen languages

    • Jan van der Zanden says:

      C must be seen as a low level language, because it lacks
      • strict formal language checks (THEN) which makes it badly readable and thus checkable/reviewable,
      • type checking
      • and pointer dereference type checking.
      Which devaluates it to an assembly like language. Perhaps unless you use it like Pascal…..
      I estimate, on the basis of my experience, that $10 billion could be saved worldwide if C would be replaced with Pascal in all application programming. This saving is realised by avoiding illegal programming, found by the compiler instead of days spending debugging and finding (of even not finding) the errors of badly used (dereferenced) pointers, intermixing of characters and integers, misspelled expressions leading to wrong logic and so on and so on.
      C should be left to OS’s and to the basics of embedded systems; all applications should be programmed with (Object) Pascal!

  9. jslarochelle says:

    Yes the Free Pascal language is a great language. It has lots of nice features such as Set, Enumeration, and a fairly simple Object model. One feature that stands out is procedural types that gives FP a fairly lightweight method of defining callbacks and something similar to Java 8 lamba expression.
    However, speed is not really the big advantage over Java because actually Java programs are very fast. Java is an hybrid language that gets interpreted at startup but eventually compiled to native code (JIT – just in time). In fact Java executes at speed very close to C++ and is one of the fastest language around. There are benchmarks available on the Web and I think the results will surprise you. The only time Java is slow is when the Garbage Collector kicks in. However, the Java GC is very flexible and can be tunes to give very good performances in most applications.
    So if you go looking for a language for a project don’t reject Java for performance reasons. Use some other criteria. One reason I can think of to reject Java is the need to install the Runtime to use Java programs. I can think of a lot of situation where it is nice to run a small utility or other program without having to install a Runtime.

  10. Zex says:

    One of the best features of Pascal are:

    – Quality IDE for visual creation of GUI applications. Only Visual Studio and QT Creator can be compared to Delphi. But when it comes to FireMonkey controls, I’m not aware of any other tool that offers that level of flexibility.

    – Easy deployment and small size of apps. Pascal compiles directly to EXE. Your users won’t need to check which JRE or NET framework they have installed. They won’t have to wait for the external library to download and install. Or to make your apps internet independent, you wont have to bundle JRE or NET with your app, making it huge in size. Object Pascal will pack all the libraries you need inside the executable, and this executable won’t need internet connection for installation, while being under 4 MB in size.

    – Component model for creating new components from scratch, or by extending old components, which can then be added to component palette and used whenever necessary. This was the invention of Anders Hejslberg in Delphi, then when Microsoft hired him, he implemented the same system in C#. Not many other languages support custom drag’n’drop components in such an easy way.

    – Wide range of features, from high-level object hierarchy down to low-level speed optimizations, including assembly language. You can write end-user GUI programs, but at the same time you can write device drivers. You could in fact write entire operating system in Pascal, since it contains all the low-level features needed. In C# or Java you can’t do that. They are good for high-level things, but they don’t stretch down to the metal.

    – Properties. As opposed to Java getter/setter functions, Object Pascal has real properties, where you don’t need to define a getter or setter if the property accesses a local variable. You can select whether to use a variable, getter/setter or a combination, while the property looks the same to the outer code. You don’t have to call functions, you just access the property and leave the implementation details to the object designer.

    – Performance. Among high-level languages Object Pascal is second only to C/C++ when it comes to speed. And since it offers built-in assembler, you can use assembly language directly inside Pascal functions, so if you really need speed you can beat any other language by using asm, MMX, SSE and other CPU extensions.

    – String handling. Pascal uses super-fast string handling, lazy write, write-on-copy and other advanced techniques that guarantee string manipulation is as fast as possible. Unlike C which needs to find the string terminator to find out its length, Pascal stores the string length for maximum speed. And it’s really easy to use and compare strings, unlike Java confusing comparisons that involve “==” and “===”.

    – No marshaling. Pascal is native to the platform, it requires no marshaling, so if you’re having a lot of system calls or DLL usage, Pascal will run like hairy goat, while C# will waste a lot of time on boxing and unboxing data from the native format to its own format.

    And there are probably other differences, but I can’t remember them right now.

  11. Michael says:

    Hi, just came across this post and would just like to offer one or two comments on why Pascal is an excellent programming language. Firstly, since it is strongly-typed many coding errors will be caught by the compiler instead of by the end user. It is also not as cryptic as the C derivative laguages which means that in 2 or 3 years time when you come back to modify some code you have a better chance of understanding what you did. If I could change anything in Pascal I would have statement-specific ends, eg, endif, endwhile etc so it is immediately obvious what an ‘end’ belongs to. Over the years I have read numerous articles written by developers who started a project in some language and (often reluctantly) decided to change to Delphi and found that their development speed improved considerably. In many cases it was because Delphi takes care of much of the lower-level stuff that you have to be concerned with in other languages. Years ago, I enjoyed getting into low-level stuff; these days I just want to get the job done as quickly as possible. And Delphi (and Free Pascal) enable that.

Leave a Reply

Your email address will not be published.

*