在 Android 上进行实时图形编程的最佳语言是什么? [英] What's the best language for real-time graphics programming on Android?

查看:27
本文介绍了在 Android 上进行实时图形编程的最佳语言是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些谷歌搜索让我相信 C++ 是实时 2D 图形编程的最佳语言,但由于 Android 是基于 Java 的,这仍然是最佳选择吗?或者我们必须使用 NDK 来减慢它的速度之类的事实?我的程序也有很多科学计算,我知道 C++ 是最好的/最快的......

Some googling has led me to believe that C++ is the best language for real-time 2D graphics programming, but since the Android is Java-based, is that still the best option? Or us the fact that I have to use NDK going to slow it down or something? My program also has a lot of scientific computing and I know C++ is best/fastest for that...

我以前从未对 Android 做过任何事情,所以我现在真的很无助.如果我只是以错误的方式处理它,请给我其他建议......我遇到的其他一些词汇是 OpenGL(我有经验,但更多的是 3D,对吧?)和 Canvas(不要很明白这个)?如果我能使用类似 GPU 的功能,那就太好了.

I've never done anything with the Android before so I'm really helpless right now. If I'm just going about it the wrong way, please give me other suggestions... Some other vocab I came across is OpenGL (which I have experience with, but that's more for 3D, right?) and Canvas (don't quite get this)? If I could get access to GPU-like capabilities that would be great.

推荐答案

Android 应用程序是用 Java 编写的,是的 - 但是 Android NDK 允许您使用 C 或 C++ 编写程序的性能关键部分.来自 Android NDK 网站

Android applications are written Java, yes - however the Android NDK allows you to write performance-critical sections of your program in C or C++. From the Android NDK website,

Android NDK 是一个配套工具可让您构建的 Android SDK性能关键部分本机代码中的应用程序.它提供允许您的头文件和库建立活动,处理用户输入、使用硬件传感器、访问应用程序资源等,当用 C 或 C++ 编程.

The Android NDK is a companion tool to the Android SDK that lets you build performance-critical portions of your apps in native code. It provides headers and libraries that allow you to build activities, handle user input, use hardware sensors, access application resources, and more, when programming in C or C++.

也就是说,适当地使用 NDK 很可能不会减慢您的程序速度.

That said, using the NDK appropriately will most likely not slow your program down.

OpenGL 适用于 3D 和 2D 图形 - 如果您只对 2D 感兴趣,您会想要查看使用正交投影 - 请参阅 glOrtho 了解更多信息.Android Canvas,另一方面,是 Java 方法用于在屏幕上绘制光栅图形.它可以让您渲染 2D 图形,但速度较慢(并且 Android 垃圾收集器会频繁中断).

OpenGL works for 3D and 2D graphics - if you're only interested in 2D you will want to look at using an Orthographic Projection - see glOrtho for more information. The Android Canvas, on the other hand, is the Java method for drawing raster graphics to the screen. It will let you render 2D graphics, but at a slower rate (and with frequent interruptions from the Android Garbage Collector).

请记住,如果您想使用 C++,在撰写本文时,还没有可用的 STL 实现.然而,有提供大部分功能的非官方端口.STLPort 是我尝试过并取得了一些成功的一种.将代码移至 C/C++ 的最大原因是来自 Android Java 垃圾收集器的中断 - 如果您对代码不太小心,它会频繁地中断您的程序以清理您留下的对象.在实践中,这会极大地限制游戏或模拟帧率.

Keep in mind that if you want to use C++, as of writing, there is no STL implementation available. There are, however unofficial ports that provide most of the functionality. STLPort is one that I have tried with some success. The biggest reason to move code to C/C++ is because of interruptions from the Android Java Garbage Collector - if you're not overly careful with your code, it will interrupt your program frequently to clean up objects you've left lying around. In practice this can limit game or simulation framerates drastically.

综上所述,我强烈建议您研究一下出现的少数开源安卓游戏引擎之一.我尝试过的最好的是 libGDX.它会处理所有杂乱的 NDK 细节,并让您完全用 Java 编写游戏/模拟代码.它自动以本机代码运行游戏引擎中性能较高的部分,以通过轻松的 Java 编码获得尽可能快的性能.最重要的是,您只需编写一次应用程序代码,即可自动在 Windows、Linux、OSX 和 Android 上运行 - 这使得测试您的应用程序比使用 Android 模拟器容易得多.

All that said, I would strongly recommend you look into one of the few open source android game engines that are cropping up. The best one I've tried is libGDX. It takes care of all the messy NDK details and lets you code your game / simulation purely in Java. It automatically runs the performance-heavy parts of the game engine in native code to get the fastest possible performance with the ease of coding in Java. Best of all, you can write your application code once and have it automatically run on Windows, Linux, OSX and Android - which makes testing your applications much, much easier than using the Android Emulator.

如果你真的想自己研究 NDK,或者需要对 OpenGL 的工作有很好的控制,我建议你下载 Android SDK 和 NDK,设置 eclipse,然后从 NDK 示例开始.那里有一个 OpenGL 演示,向您展示如何设置所有内容.另一个好的起点是 SpinningCube 谷歌项目.

If you really want to look into the NDK yourself, or need to have really fine control on what OpenGL is doing, I would recommend you download the Android SDK and NDK, get eclipse set up, and then start with the NDK samples. There is an OpenGL demo there that shows you how to get everything set up. Another good starting point would be the SpinningCube google project.

我不太确定您所说的类似 GPU 的功能"是什么意思,但是使用 libGDX,您可以在 OpenGL ES 2.0 下编译顶点和片段着色器 - 您可以使用它来运行令人尴尬的并行代码,使用设备的 GPU.

I'm not really sure if what you mean by 'GPU-like capabilities', but With libGDX, you can compile vertex and fragment shaders under OpenGL ES 2.0 - you could use this to run embarrassingly parallel code using the device's GPU.

这篇关于在 Android 上进行实时图形编程的最佳语言是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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