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 ...
随机推荐
- 一、导入、导出远程Oracle数据库
一.导入.导出远程Oracle数据库 其语法实示例如下: imp/exp [username[/password[@service]]] 其中service是服务实例名,关于如何创建服务实 ...
- linux软件的安装,更新与卸载
Linux常见的安装为tar,zip,gz,rpm,deb,bin等.我们可以简单的分为三类. 第一:打包或压缩文件tar,zip,gz等,一般解压后即可,或者解压后运行sh文件: 第二:对应的有管理 ...
- 推荐 10 款最好的 Python IDE
简述 Python 非常易学,强大的编程语言.Python 包括高效高级的数据结构,提供简单且高效的面向对象编程. Python 的学习过程少不了 IDE 或者代码编辑器,或者集成的开发编辑器(IDE ...
- HTML元素margin、padding的默认值
HTML元素margin.padding的默认值 element margin(单位像素) padding html 0 0 body 8 0 div 0 0 h1 21 0 h2 19 0 19 0 ...
- 一个小面试题sql
一. 问答题 1简要说明分页是如何实现的. A:sqlserver: Select top(pagesize) * from student where id not in( ...
- WMware 10 Ubuntu 12.04 进入Unity模式
/********************************************************************* * WMware 10 Ubuntu 12.04 进入Un ...
- 08day2
引爆炸弹 贪心 [问题描述] 有 n 个炸弹,有些炸弹牵了一根单向引线(也就是说引线只有在这一端能被炸弹点燃),只要引爆了这个炸弹,用引线连接的下一个炸弹也会爆炸.每个炸弹还有个得分,当这个炸弹被引爆 ...
- Search in Rotated Sorted Array II
Question: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? W ...
- php 系统命令执行函数
(转载)作者:海底苍鹰地址:http://blog.51yip.com/php/1064.html 1,exec函数 <?php $test = "ls /tmp/test" ...
- 【转】Linux设备驱动之Ioctl控制
原文网址:http://www.cnblogs.com/geneil/archive/2011/12/04/2275372.html 大部分驱动除了需要具备读写设备的能力之外,还需要具备对硬件控制的能 ...