A JavaFX based Game Authoring System
http://www.mirkosertic.de/doku.php/javastuff/javafxgameauthoring
——————————————————————————————————————————————————
A JavaFX based Game Authoring System
A few weeks ago I decided to enhance my knowledge in game coding, so I bought two very interesting books: “Real-Time Rendering, Third Edition by Thomas Akenine-Möller, Eric Haines and Nety Hoffmann” and “Game Coding Complete, Fourth Edition by Mike McShaffry and David “Rez” Graham”. After some insightful moments, hundreds of pages read and sleepless nights, I wanted to create my own game. So this project was born.
Primary Goals
- Create a simple platform game with the Java Duke
- Enhance my knowledge in JavaFX
- Have fun doing some cool stuff
Secondary Goals
- Create a game engine and a user friendly game authoring tool with JavaFX
- Make the game engine runnable on more than one device type(Desktop and Web)
- Check what would be the best way to create complex UIs with JavaFX
- Create a rich domain model for computer games
- Apply domain-driven design to game engine architecture
Well, I have to say that I achieved all of these goals. Here is a screenshot of the JavaFX based authoring tool:
And the running game in Firefox deployed as a plain HTML website:
and the game also running in Internet Explorer:
Nice! Of course the game engine has some very cool features, like a flexible rule engine, physics support and sound!
The Game Authoring System
The authoring system is a JavaFX application. I used Java CDI(Context and Dependency Injection) for the UI(yes, CDI can also be used on UI side!). CDI has also a very cool feature, the CDI Events. Using CDI events, complex UI logic can be completely decoupled, and it greatly fits together with the Model-View-Presenter Pattern. Using this style of architecture, it is quite easy to split a complex user interface into separate modules, like the property browser, and project structure view and the game scene preview. When a user selects an object in the project structure, an event is fired using CDI. This event is observed by the property browser, the game scene preview and other components, so they can react by highlighting an object, showing its properties or even updating its game engine state. I really learned to love JavaFX and CDI!
The Core Game Engine
The game engine is the heart of the system. Basically it is structured the following way:
There are different game systems and game views. A game system for
instance is the sound system or the physics system. A game view is the
presentation of the game state for a user. The game systems and the game
views are tied together in the game loop. For every frame rendered, the
game loop is executed, every game system gets a chance to do something
and finally the presentation is rendered by a game view.
The different game systems are decoupled by an event dispatching system.
Every object or game system can create or consume events. So pressing a
key by a user creates a keypressed event, which is consumed by the duke
object, knowing that it must jump now(This behavior is defined by a
rule engine). So another event is created to inform the physics engine
that a force must be applied to a given object. Now the physics system
is called during the main loop, and it can run the physics simulation
for a given amount of time. After that, updateposition events are fired
by the physics game system informing the game domain model to update an
object. Now the game view are called to render the game state to a
device. All this happens during one step of the main game loop. What I
have learned is that computer games are also a great appliance for event
driven architecture!
The game engine also defines a set of infrastructure services. These
services are implemented by the renderers as described in the following
chapters. Basically the core engine defines an abstract sound system API
and gives the renderer an interface to the current game state as a kind
of camera. The renderer can now implement the sound playing using the
device dependent APIs and also use the camera to retrieve visible
objects and render them to screen using any kind of graphics API.
The JavaFX Renderer/Game View
The JavaFX renderer is a game view implementation. It renders the
current game state with a JavaFX Canvas element. It also couples the
main game loop with the JavaFX pulse event. A pulse is throttled at 60
frames per second maximum, and this frequency fits well together with
the main game loop. Using the pulse event we can avoid different threads
for the game loop and the rendering system, and the maximum of 60
frames per second is more than enough for most games.
The JavaFX Renderer uses plain JaxaFX key events and passes them to the
core event dispatching system. It also uses the Java Sound API
to play music or other sound effects. The JavaFX renderer is basically
used by the game authoring system as a preview for the game. The user
interface for the web game view is a little bit more complicated as you
will see in the following chapter.
The Web Renderer/Game View
The web renderer is a different beast. The game engine is coded in Java,
and there are also some very cool physics engines like JBox2D available
which can be embedded as a game system. I really want to avoid Applets
or other browser plugins, so do we really need to recode everything in
plain JavaScript to make it runnable as a website? Short answer: no.
There are different options available. For instance the bck2brwsr(Back
to Browser) project, which implements a complete Java Virtual Machine in
JavaScript.
Another option is a Java-to-JavaScript compiler. And luckily, there is a
very common compiler available, called GWT(Google Web Toolkit). GWT
also supports HTML5 features like the Canvas element or even WebGL. For
my project, I decided to go the GWT way and code a renderer using the
HTML5 canvas technology. The amazing result was that the code of the GWT
renderer is almost the same as the JavaFX Canvas renderer! Cool,
different framework, almost the same API.
Really nice! There is also a WebGL renderer available. It is used if
the Browser supports WebGL. Modern Browsers do this for a long time, but
Internet Explorer still does not. So the Web Renderer will by default
use WebGL and fallback to Canvas if the Browser does not support WebGL.
WebGL is based on OpenGL ES, so we can do a lot of cool stuff on the GPU
with the programmable shader technology, but for my small game i will
just draw some sprites:-)
Reacting on user input can be tricky on the web. We can of course use
plain old keypressed events and react on them the same way as seen in
the JavaFX renderer. This is also implemented this way as GWT code. But
what shall we do on touch devices?
Well, we can use APIs like PhoneGap/Apache Cordova to react on touch
events, or we can even use the device accelerometer as a kind of user
feedback. This is something I will implement in a future version of the
renderer. For now, I will just react on plain old key events.
Conclusion
Cross platform games are doable with JavaFX and Java-to-JavaScript cross
compilation using GWT. Domain-driven Design, CDI and domain events
greatly fit together with game engine architecture, and compiling
Java-to-JavaScript saves a lot of time and enables us to deploy games on
different platforms. I really love JavaFX and game coding!
Links:
The source code is available for free on GitHub: github.com/mirkosertic/GameComposer
The Duke example game can be played here(WebGL with HTML5 Canvas fallback): www.mirkosertic.de/games/dukeplatform
The Duke example game can be played here(Only HTML5 Canvas): www.mirkosertic.de/games/dukeplatform_canvas
A JavaFX based Game Authoring System的更多相关文章
- 生成项目目录结构(based on windows system)
描述: 作为程序员,在工作中,我们经常会有需求,需要罗列出项目的结构图:如果手工来整理的话,太过浪费时间,其实我们可以借助tree命令来快速生成目录结构. 本文主要介绍一下,基于windows系统,如 ...
- 《A computer-aided healthcare system for cataract classification and grading based on fundus image analysis》学习笔记
Abstract This paper presents a fundus image analysis based computer aided system for automatic class ...
- Top 10 Open Source Bug Tracking System系统
Bugzilla http://www.bugzilla.org/ Mantis php http://www.mantisbt.org/ Trac Python also provides wiki ...
- Hypervisor, computer system, and virtual processor scheduling method
A hypervisor calculates the total number of processor cycles (the number of processor cycles of one ...
- PatentTips - Method for guest operating system integrity validation
BACKGROUND The embodiments relate to guest operating system integrity validation, and more particula ...
- PatentTips - Scheduling compute kernel workgroups to heterogeneous processors based on historical processor execution times and utilizations
BACKGROUND OF THE INVENTION 1. Field of the Invention The present invention relates generally to h ...
- Data Flow Diagram with Examples - Customer Service System
Data Flow Diagram with Examples - Customer Service System Data Flow Diagram (DFD) provides a visual ...
- iOS 10.0 更新点(开发者视角)
html, body {overflow-x: initial !important;}html { font-size: 14px; } body { margin: 0px; padding: 0 ...
- iOS 10 SceneKit 新特性 – SceneKit 制作 3D 场景框架
来源:scauos(@大朕东) 链接:http://www.jianshu.com/p/b30785bb6c97 开头语: 今天的主题是探索iOS10 SceneKit的新功能,你可以观看今年WWDC ...
随机推荐
- 非常非常非常好!path-sum-iii
https://leetcode.com/problems/path-sum-iii/ 最终我还是没做出好的解法.还是看的别人的解法. 即使看了别人的解法,开始还实现错了. 还有很长的路要走. pac ...
- 【笨嘴拙舌WINDOWS】GDI(1)
GDI:Graphics Device Interface 图形设备接口. 操作系统从命令行界面到图形界面的过度是施乐公司实验室对计算机普及作出的不可估量的贡献,苹果公司乔布斯与微软公司比尔盖茨对其的 ...
- css新增UI样式
1.圆角 border-radius <style> .box{width:200px;height:300px;border:1px solid #000;border-radius:1 ...
- InputStream重用技巧(利用ByteArrayOutputStream)
有时候我们需要对同一个InputStream对象使用多次.比如,客户端从服务器获取数据 ,利用HttpURLConnection的getInputStream()方法获得Stream对象,这时既要把数 ...
- MySQL集群的可行方案
如果单MySQL的优化始终还是顶不住压力时,这个时候我们就必须考虑MySQL的高可用架构(很多同学也爱说成是MySQL集群)了,目前可行的方案有: 一.MySQL Cluster优势:可用性非常高,性 ...
- C的输入输出函数的基本用法
printf输出函数: printf()函数是格式化输出函数, 一般用于向标准输出设备按规定格式输出信息. printf()函数的调用格式为: printf("<格式化字符串>& ...
- S2sh整合MAven项目所需坐标大全
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> & ...
- SQL利用Case When Then多条件判断
CASE WHEN 条件1 THEN 结果1 WHEN 条件2 THEN 结果2 WHEN 条件3 THEN 结果3 WHEN 条件4 THEN 结果4 ....... ...
- TextView及其子类
1.TextView控件(TextView是EditView.Button等类的父类) <1>android:id 给当前控件定义了一个唯一标识符 <2>android:l ...
- http报头正文开头会有一个整数的问题
HTTP/.0 200 OK (省略一些东西...) Content-type:text/html 正文内容 这是我用Arduino访问某个php得到的结果 如果我在php里头输出1000个字符或更少 ...