Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML with JavaScript, Box2D...  (Read 8700 times)

0 Members and 1 Guest are viewing this topic.

imekon

  • Newbie
  • *
  • Posts: 5
    • View Profile
SFML with JavaScript, Box2D...
« on: November 04, 2015, 01:40:32 pm »
I've been poking around GameMaker Studio, Construct 2 and Clickteam Fusion 2.5 and started wondering how hard it would be to make a 2d game engine.

My reason for doing this was all of the above engines are fine, but they get in the way a bit, as I'd rather write code than visually program. GameMaker is unique in that it has a script language. Fusion has JavaScript for plugins.

I looked briefly at SDL but chose to use SFML instead (classes not C functions). I bolted a Lua interpreter on at first, but after reading "why everyone should learn javascript" (http://blog.monitor.us/2014/08/why-everyone-should-learn-javascript/), I thought, why not?

I got hold of duktape, a JavaScript engine and swapped out Lua. Now Lua is fine, but JavaScript is better (IMHO) as it's more C/C++ like.

I added Box2d physics and created my first couple of demos: a simple block game (http://imgur.com/a/z7IrB) and a physics demo.

So where am I going with this? Am I trying to make a game? I'm interested in Match3 type games and procedural generation... so that's why I'm doing this.

Oh yeah, and writing code like this is fun.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: SFML with JavaScript, Box2D...
« Reply #1 on: November 04, 2015, 02:03:26 pm »
Can you elaborate a bit on how your "bridge" to JavaScript looks like and what you can do solely in JavaScript?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

imekon

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: SFML with JavaScript, Box2D...
« Reply #2 on: November 04, 2015, 02:16:27 pm »
I used duktape (duktape.org) to provide the embedded JavaScript. I originally used Lua but switched to duktape (JavaScript) as it's more C/C++ like. In fact, duktape is very similar to Lua in the C code.

The JavaScript behind each 'game' has at least two functions - initialise and run that get called by the engine.

I keep time consuming operations in C++ but let the JavaScript do the higher level stuff, like creation etc.

To create a box (32 x 32) with physics, you'd do something like

Code: [Select]
function initialise()
{
    box = GameObject.textureObject(1);    // create a sprite with texture index 1
    box.setOrigin(16, 16);                           // important for Box2D physics
    box.setPosition(100, 100);                    // set the position
    Physics.createDynamicBox(box, { density: 1, friction: 0.6 });
}

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: SFML with JavaScript, Box2D...
« Reply #3 on: November 04, 2015, 04:17:40 pm »
This is quite nice, and I haven't heard of Duktape yet. Would be nice to the code, if this is by any chance open.

imekon

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: SFML with JavaScript, Box2D...
« Reply #4 on: November 04, 2015, 05:46:51 pm »
It's not open source yet, but could be - if I can work out how to bring together SFML, Box2D, Duktape and the rest into one Git project.

SFGUI - maybe I should replace my crude console implementation with that for logging...

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: SFML with JavaScript, Box2D...
« Reply #5 on: November 04, 2015, 07:02:51 pm »
It's not open source yet, but could be - if I can work out how to bring together SFML, Box2D, Duktape and the rest into one Git project
One option would be git submodule :
 https://git-scm.com/book/en/v2/Git-Tools-Submodules
 https://git-scm.com/docs/git-submodule

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: SFML with JavaScript, Box2D...
« Reply #6 on: November 04, 2015, 11:58:51 pm »
Quote
Now Lua is fine, but JavaScript is better (IMHO) as it's more C/C++ like.
Squirrel and AngelScript (and maybe a few others) are meant to fill the role of "more C++ like Lua like scripting", not JavaScript.
Back to C++ gamedev with SFML in May 2023

GraphicsWhale

  • Full Member
  • ***
  • Posts: 131
    • View Profile
Re: SFML with JavaScript, Box2D...
« Reply #7 on: November 05, 2015, 09:32:40 pm »
I've been poking around GameMaker Studio, Construct 2 and Clickteam Fusion 2.5 and started wondering how hard it would be to make a 2d game engine.

You've completely underestimated the complexity of those engines.

Squirrel and AngelScript (and maybe a few others) are meant to fill the role of "more C++ like Lua like scripting", not JavaScript.

I wouldn't make a game relying mostly on Squirrel or Angelscript, though. Neither have a large community or has a proven JIT compiler available for it.

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: SFML with JavaScript, Box2D...
« Reply #8 on: November 10, 2015, 08:31:59 am »
And starting with ECMAScript2015 the language is indeed getting better. :P

imekon

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: SFML with JavaScript, Box2D...
« Reply #9 on: November 10, 2015, 02:17:14 pm »
And starting with ECMAScript2015 the language is indeed getting better. :P

Duktape is V5 with some bits of V6...  :-[

eugustus

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: SFML with JavaScript, Box2D...
« Reply #10 on: December 29, 2016, 01:34:01 am »
Hello.

Progress? Why not implement berkelium?

I found with links but i don't try it yet..

https://github.com/Yours3lf/gl_browser_gui
https://github.com/ThomasColliers/glexp-gui


Sorry for bad english