功能编程语言是否适合图形编程? [英] Are functional programming languages suitable for graphics programming?

查看:105
本文介绍了功能编程语言是否适合图形编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是非常好奇的是,从我自己的经验来看,所有的图形编程似乎都是C或C ++相关的。像Direct10X。功能编程语言是否提供某种图形库来开发视频游戏?

Just very curious about this, from my own experience , all the graphic programming seems to C or C++ related. Like the Direct10X. Does functional programming language provide some sort of graphic library to develop video game?

推荐答案

编程就像在任何其他语言。

You can use functional languages to do graphics/game programming just as in any other language.

这只是一个简单的游戏,但我写了 Ironclad :Clojure中的Steam Legions 作为游戏开发的函数式编程练习。

It's only a simple game, but I wrote Ironclad: Steam Legions in Clojure as an exercise in functional programming for game development.

这里是我学习的一些教训/关于使用Clojure进行游戏编程的一般观察:

Here are some lessons I learnt / general observations on using Clojure for game programming:


  • 您必须注意性能,因为游戏可能非常苛刻,功能性语言一些开销。 Clojure对于大多数游戏来说肯定是足够好,但是你需要知道这些技巧来保持你的代码优化。例如,功能语言可以得到一点GC重,产生大量的临时对象。您需要学习避免这种情况的技巧(例如,使用reduce避免创建新序列对象或利用原始的算术)

  • You need to be careful about performance as games can be very demanding and functional languages do impose some overheads. Clojure is certainly "good enough" for most games, but you need to know the tricks to keep your code optimised. For example, functional languages can get a bit GC-heavy producing a lot of temporary objects. You need to learn the tricks to avoid this (for example, using reduce in a way that avoids creating new sequence objects, or leveraging primitive artithmetic)

Mutability 在游戏中很有用。例如,如果你正在做任何物理或平滑的动画,你经常有很多对象与不断变化的位置。你可以用功能/不可变的数据结构来模拟它,但如果你关心性能,这不是一个好主意。因此,值得了解如何在你的函数式语言中获取可变数据,即使它不是惯用的(例如在Clojure中你可能想使用Java数组)。

Mutability is useful in games. For example, if you are doing anything with physics or smooth animation you often have a lot of objects with constantly changing locations. You can simulate this with functional/immutable data structures but if you care about performance it's not a good idea. Hence it's worth finding out how to get mutable data in your functional language even if it isn't idiomatic (e.g. in Clojure you will probably want to make use of Java arrays)

不变的持久性数据结构实际上在游戏中也非常有用。在Ironclad中,整个游戏状态存储在一个不可变的数据结构中。

Immutable persistent data structures actually turn out to be very useful in games as well. In Ironclad, the entire game state was stored in a single immutable data structure. This allowed for some cool tricks like efficiently snapshotting the game state / instant undos / running backwards in time.

Clojure是游戏脚本的真棒

Clojure is awesome for game scripting. The dynamic nature coupled with runtime compilation and the ability to define arbitrary DSLs with macros is a massive win. In fact, even if I was writing a game in an OOP language like Java I would seriously consider using Clojure (or another Lisp) for scripting.

Clojure 非常适合互动开发。我经常发现自己在一个窗口中运行游戏,而在REPL旁边运行代码。改变游戏数据结构并立即看到效果很有趣! 这个真棒视频还让您了解Clojure风格的开发可能带来的好处。

Clojure is awesome for interactive development. I often found myself running the game in one window while hacking the running code in a REPL alongside. It's fun to alter game data structures and immediately see the effects! This awesome video also gives you a taste of what's possible with Clojure-style development.

在Clojure中,至少你会经常想使用图形库Java ,例如2D的摇摆或3D的LWJGL。在某些情况下,这些包装器已经存在,但我发现它很容易直接从Clojure使用它们。毕竟,Java interop和(。methodName object arg1 arg2)一样简单

In Clojure at least you will often want to use the Java libraries for graphics, e.g. Swing for 2D or LWJGL for 3D. In some cases wrappers for these already exist, however I found it easy enough to use them directly from Clojure. After all, Java interop is as simple as (.methodName object arg1 arg2)

总之,我认为函数式语言是游戏开发的完美选择,除非是性能密集型游戏,你仍然可能更好地使用C / C ++,以便更直接地控制硬件。

In conclusion, I think functional languages are perfectly good choices for game development, with the exception of very performance-intensive games where you are still likely to be better with C/C++ in order to have more direct control over the hardware.

这篇关于功能编程语言是否适合图形编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆