博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaFX game on Raspberry PI
阅读量:6493 次
发布时间:2019-06-24

本文共 4596 字,大约阅读时间需要 15 分钟。

http://www.vihrearobotti.com/other/javafx_raspberrypi

——————————————————————————————————————————————

Intro

There seems to be a lot of cool things happening on Java SE embedded world.
 
Java SE for ARM has now been there for while, but it has been lacking support for properly accelerated 2D graphics -- you could just dream running your AWT/Swing game in decent frame rates on embedded ARM platform. (**)
 
But that has just been solved by release of   which includes JavaFX with OpenGL ES support on it. I have been reading that JavaFX is being recommended over AWT for creating UIs, and it seems like AWT is not going to be included in future releases of Java SE embedded.
 
So, sounds like there's a chance for running Java based 2D game in Raspberry PI with decent (30-40) FPS, why not try it? 
 
** [There were few lower level Java OpenGL wrapper libraries (like JOGL) already available for ARM accelerated graphics.]

The plan

I thought transforming my trusty old  Android game would be nice way of trying this out, because it's already Java, we just need to get rid of Android Graphics APIs and start using JavaFX instead.
 
Game will be developed+compiled in PC and then deployed to Raspberry PI for FPS analysis :)

Buildin' it 

I've put the full sources of JavaFX port available   -- or you can just grab the prebuild JAR one from end of blog.
 
If you're planning to play with sources in Eclipse, make sure you need to add jfxrt.jar in classpath (Windows->Preferences->Available JREs->Edit). This game uses features of JavaFX 2.2, so check that you have right version. I used JDK 1.7.0_10 to compile and test it on PC.
 
To build the executable JAR file, I used
javafxpackager which is included in JDK, so just run this command in Eclipse project folder after compiling project to produce the JAR file.
 
  
 
javafxpackager -createjar -appclass org.noxo.Game -srcdir bin -outdir out -outfile asteroidhd.jar

Gameloop & JavaFX canvas

With 2d sprite based games I personally prefer more maintaining sprites/bitmaps and drawing them via graphics context, instead of using scene based graphics.
 
Starting from JavaFX 2.2 there has been support for this via Canvas class, so you can do the stuff almost in good-old-AWT-way, by getting reference to graphics context and then do the drawing using methods (drawImage, drawString..) provided.
 
One difference in JavaFX (vs AWT) is that drawing operations by graphics context should be done from JavaFX application thread.
 
One way of implementing game-update-render loop, could for example look like this:
class Game extends javafx.application.Application{    Canvas canvas = new Canvas(1280,720);    AnimationTimer animTimer;        public void start(Stage stage) {                Group gameNode = new Group(canvas);stage.setScene(new Scene(gameNode));stage.show();        animTimer = new AnimationTimer() {        @Override            public void handle(long arg0) {                gameLoop();            }        };        // start running gameloop in javafx application thread        animTimer.start();    }            public void gameLoop() {        updateGame();        renderGame();    }    public void updateGame() {                // update your game logics here    }    public void renderGame() {        GraphicsContext gc = canvas.getGraphicsContext2D();        // draw your stuff using gc..    }    public static void main(String arg[]) {        launch(arg);    }}

Installing to Raspberry PI

  • Have Raspberry PI with hard float Linux distribution installed on it
    • I had the 2012-12-16-wheezy-raspbian.zip installed from raspberrypi.org 
  • Configure GPU memory to 128MB 
    • Edit boot/config.txt => gpu_mem_256=128
    • Configuring this depends on board and Linux distro version
  • Change resolution to 720P (1280x720)
    • Edit boot/config.txt => hdmi_mode=4
  • Install Java 8 Early Access
    • Download from
    • Uncompress file anywhere (tar -xvf jdk-8-ea-b36e-linux-arm-hflt-29_nov_2012.gz)
  • Run the game
    • Push game .JAR file to Raspberry PI
    • Change in Java 8 folder (jdk-1.8.0/bin)
    • ./java -Djavafx.platform=eglfb -jar /JAR_FILE_LOCATION/asteroidhd.jar
I did include average FPS counter in game. Raspberry PI seems to be able to run this game with stable
 
~35 FPS
after bit of a jitter at startup (didn't yet try to do any kind of optimization). I guess the lag at startup is due to cost of creating and initializing textures into GPU, but didn't yet try to profile it.
 
I really like the new cool features available via JavaFX, it seems to also support H264 and video streaming nowadays, which has been lacking in Java for a long time. I've also been reading support in JavaFX - that's really good news!
 
 
 
 

转载地址:http://yskyo.baihongyu.com/

你可能感兴趣的文章
统计c盘的PE文件的个数 (遍历所有文件)
查看>>
大白话Vue源码系列目录
查看>>
EffectKeyMap系列1(Ubuntu)
查看>>
iOS手势
查看>>
Webpack源码基础-Tapable从使用Hook到源码解析
查看>>
【转载】NBU异机恢复oracle
查看>>
[BZOJ 3668&UOJ #2][Noi2014]起床困难综合症(贪心)
查看>>
魅族mx5详细打开usb调试模式的步骤
查看>>
php里关于文件下载的方法(两种)
查看>>
数据绑定(数据源控件 -- ObjectDataSource)
查看>>
微信点单
查看>>
selenium操作页面元素总结
查看>>
vim 命令
查看>>
Ubuntu 16.04 LTS安装sogou输入法详解
查看>>
计算几何专题
查看>>
GNU/Linux 正则表达式与三剑侠(grep,sed,awk)(精)
查看>>
36、自定义控件详解(一)-- 自定义属性
查看>>
bootstrap弹窗居中处理
查看>>
DOM学习笔记二
查看>>
[Array]189. Rotate Array
查看>>