Animated graphics with Scala

704 views
Skip to first unread message

Boris Hollas

unread,
Apr 2, 2013, 3:36:09 AM4/2/13
to scala...@googlegroups.com
Hello,

I want to draw animated graphics based on timer or user input events. However, since the painComponent and repaint methods have no parameters except a Graphics object, there is no straightforward way to do so in Swing. All the solutions I have seen for Java use a member variable to pass information to the paint-method. However, using a global mutable storage to exchange information between functions is an ugly way to structure code, especially in functional programming.

SDL in contrast executes all graphics commands right away, the same way a println is executed. Therefore, there's no need to pass information via a global variable to a paint-method. Do similar graphic libraries exist for Scala?

-Boris

Andrzej Giniewicz

unread,
Apr 2, 2013, 6:27:13 AM4/2/13
to Boris Hollas, scala-user
Hi,

On Tue, Apr 2, 2013 at 9:36 AM, Boris Hollas
<hol...@informatik.htw-dresden.de> wrote:
> I want to draw animated graphics based on timer or user input events.
...
> SDL executes all graphics commands right away
...
> Do similar graphic libraries exist for Scala?

I don't think anything Scala specific exists. But - keep in mind you
can use any Java library with Scala and people are successfully doing
it this way, for example you could use SDLJava (although it isn't
updated for quite some time).

I'm not sure this will be what you want, but I'd go with libgdx
anyway. I know that http://libgdx.badlogicgames.com/ works with Scala,
it is not only for games, you can make any type of interactive
graphical 2d/3d app in it - as an extra profit, your code will be more
portable - including Android, and who knows - maybe iOS soon? They
already offer official support for libgdx (Java) code on iOS (trough
IKVM+Mono.Touch currently, but they have Google SoC projects for
RoboVM and Avian), it most likely will mean that as soon as Scala will
work on IKVM/Avian/RoboVM on iOS you will be able to port your app
there in a snap, including access to iOS specific API. Also it's easy
to start up with it because there is g8 template for SBT project set
up for scala/libgdx/Win/Lin/Mac/Android development -
https://github.com/ajhager/libgdx-sbt-project.g8 - you can even check
some games made with Scala for PC & Android - like
https://github.com/tlazaro/scala-metagun - a PORT of game made by
MineCraft author in single weekend for Ludum Dare
http://www.mojang.com/compo/metagun/ (he used Java+LibGDX combo, but
Scala+LibGDX works just as well). It's best to see a complete running
example and decide if this is what you need - like
https://github.com/tlazaro/scala-metagun/blob/master/common/src/main/scala/com/mojang/metagun/screen/TitleScreen.scala
- you update object in tick method and render it in render method, you
still need to keep some information, but it isn't "graphics objects",
only stuff like positions of objects in world, etc (the spriteBatch
call isn't obligatory here, it's just for optimization to not send a
lot of small requests to GFX card, because it's slower. It allows you
to prepare a batch of any size and send it at once, but it is you who
decides if and when to use it - you can also make multiple batches and
stuff like that). Library takes care for the rest. I don't know if you
will get anything closer to what you want, at least I'm not aware
about anything closer.

Also, it all depends on what is your final goal. You aim for more
game-like experience where your application will be single, usually
full-screen window without decoration and OS styled components? Or
maybe you want to do something more like graphics editor, where you
not only have single window but mix graphics and standard OS elements
like status bar, RMB menu with cut/copy/paste, top menu, toolbar, etc?
If you are more into game-like experience or want to be cross platform
to Android, I'd really recommend libgdx. If you aim for more
editor-like experience, I'd go with learning OpenGL and use it
directly or for example inside Swing trough JOGL.

Hope it helps,
Andrzej.

Paul Keeble

unread,
Apr 2, 2013, 6:28:31 AM4/2/13
to scala-user
You can get access to Java's exclusive mode with active rendering and take direct control of the delivery of frames as you see fit. You can also setup the buffering strategy and make decisions about vsync and levels of outstanding frames while using a Graphics object. I use this with high reliable results from scala although some of the code 


I have a few areas where the code isn't pure around the tracking of frames and timing but that is decoration around the main functional work load which ends up being pure world -> world transformations followed by a render step. The Java code examples they have translate quite quickly into Scala and then you can start cutting it up into a reasonable API and start controlling your strategy for when frames should be dispatched.

PK

Paul Keeble
Director
P Keeble Consulting ltd
Mobile: 07931488924




-Boris

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Boris Hollas

unread,
Apr 2, 2013, 10:52:08 AM4/2/13
to scala...@googlegroups.com, Boris Hollas
Am Dienstag, 2. April 2013 12:27:13 UTC+2 schrieb Andrzej Giniewicz:
I'm not sure this will be what you want, but I'd go with libgdx
anyway. I know that http://libgdx.badlogicgames.com/ works with Scala,

That looks interesting and more powerful than SDL.
 
some games made with Scala for PC & Android - like
https://github.com/tlazaro/scala-metagun - a PORT of game made by

I've tried it, but sbt first dowloaded lots of stuff for Scala 2.9 and 2.8 (I have 2.10) and then stopped with
[error] Android SDK not found.

I have no Android SDK on my system, which is a linux PC.

Andrzej Giniewicz

unread,
Apr 2, 2013, 11:32:13 AM4/2/13
to Boris Hollas, scala-user
This happened during first run of sbt or later? If during starting,
try something like:

sbt "project desktop" run

in one line when starting sbt - there is chance it will work then. If
not, you can install Android SDK, who knows when it will be useful :)
http://developer.android.com/sdk/index.html

Anyway, this SBT project contains 3 subprojects - desktop, android and
common. Both android and desktop depend on common. In their source
code you only have stuff like definition of window size (things that
aren't cross-platform). Whole game is inside common. Default project
in this case is android, so it checked for it. If you set project to
desktop, it shouldn't download&check for android stuff.

Boris Hollas

unread,
Apr 2, 2013, 11:54:31 AM4/2/13
to scala...@googlegroups.com, Boris Hollas
I did this:

~/scala-metagun$ sbt "project desktop" run
[info] Loading project definition from /home/boris/scala-metagun/project
[error] Android SDK not found. You might need to set ANDROID_SDK_HOME or ANDROID_SDK_ROOT or ANDROID_HOME

Andrzej Giniewicz

unread,
Apr 2, 2013, 12:25:10 PM4/2/13
to Boris Hollas, scala-user
It is because of how build script was written by person porting the
game. I haven't noticed because I have android SDK installed. Edit
file

project/plugins.sbt

and remove or comment out line

addSbtPlugin("org.scala-sbt" %% "sbt-android-plugin" % "0.6.2-SNAPSHOT")

also comment out in project/Build.scala:

import AndroidKeys._

whole object "object AndroidGeneral" (bottom of same file) and whole
android project settings: "lazy val android = ...", also at end. Then
you will have a desktop only project and:

sbt "project dektop" run

should work (just tested it with ADK disabled). I think that with new
versions of Android plugin it works little better, cannot guarantee
though as I haven't tested it without SDK in place. Of course, if you
install Android SDK it will also work without modification. It just
was configured to build for Android as primary platform.

Just keep in mind, that this game was made by single person in single
weekend, so it might not be polished, but it works and is small enough
to serve as good starting point.

Boris Hollas

unread,
Apr 2, 2013, 1:36:09 PM4/2/13
to scala...@googlegroups.com, Boris Hollas
Thanks, now it works!

I also read about JavaFX. How does this compare to SDL or libGDX?

Andrzej Giniewicz

unread,
Apr 2, 2013, 1:53:16 PM4/2/13
to Boris Hollas, scala-user
I don't know much about JavaFX, but as I understand it is for usual
graphics, like Swing replacement (according to JavaFX faq). With all
that windows and stuff like UI elements that can be differently
styles, also to look like OS thing (I think so, haven't tried it).

On the other hand, SDL and libGDX are multimedia libraries - providing
not only graphics but also sound (and in case of libGDX also physics
emulation, math libraries with common 3D operations on
vectors/matrices, abstractions for stuff like Sprite, and much more).
libGDX is one stop shop for everything you might need for games or
multimedia applications - you could write in it for example 3D
shooter, top-down RPG, card collector game, also movie player with
OSD, interactive screen-saver, stuff like that - but not something
that is windowed application - it requires embedding SDL/libGDX in
another GUI toolkit with such capabilities (I think Swings can do
that, don't know about JavaFX - anyway I never tried it so it's mostly
based on expectations - I only started to use JVM because of ability
to do stuff on Android, so I never looked into windowed applications).
This is not to say that you cannot use SDL/libGDX in windowed
environment. You can, but then your content fills the window in whole
and there are no other OS elements inside.

Bjorn Regnell

unread,
Apr 2, 2013, 2:22:51 PM4/2/13
to scala...@googlegroups.com
You might want to check out Kojo for making games using Scala scripts and the Kojo turtle graphics API. There is concise and high-level animation support in the latest web-version of Kojo; check out menu Samples -> Multiple turtles -> Sprite Animation
http://www.kogics.net/webkojo

/Bjorn

Brian Schlining

unread,
Apr 8, 2013, 2:49:52 PM4/8/13
to Andrzej Giniewicz, Boris Hollas, scala-user
>> I also read about JavaFX. How does this compare to SDL or libGDX?
>>

I don't know much about JavaFX, but as I understand it is for usual
graphics, like Swing replacement (according to JavaFX faq). With all
that windows and stuff like UI elements that can be differently
styles, also to look like OS thing (I think so, haven't tried it).

On the other hand, SDL and libGDX are multimedia libraries - providing
not only graphics but also sound (and in case of libGDX also physics
emulation, math libraries with common 3D operations on
vectors/matrices, abstractions for stuff like Sprite, and much more).


JavaFX is meant to be quite a bit more than a Swing replacement. The next big release of JavaFX will be JavaFX 8 and will be bundled with Java 8. JavaFX is built on a decent scene graph design (something completely missing from Swing) and JavaFX 8 EA has a 3D API. See http://fxexperience.com/2013/02/javafx-3d-early-access-available/ and http://www.youtube.com/watch?v=ayQHlt2SoAk. There' is also a decent sound API in it (See http://carlfx.wordpress.com/2012/08/26/javafx-2-gametutorial-part-5/)

Cheers 

--
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Brian Schlining
bschl...@gmail.com
Reply all
Reply to author
Forward
0 new messages