Skip to main content

News

Topic: Proper way of using Sphere? (Read 6710 times) previous topic - next topic

0 Members and 1 Guest are viewing this topic.
Proper way of using Sphere?
Any particular way Sphere is intended to be used? I seems pretty open/custom to me right now - run from the start script, drop in your code and just go. So can you really just plop whatever into your render/update loops, throw the map engine underneath and do whatever?

Or is there a more specific way of doing things the engine is suited for? Blit images and text over images and on and on? Use the map engine if you want, but it's totally not necessary and everything can just be fueled by the JavaScript?

Any particular way recommended to structure data? Just have global variables and objects for game data?

  • Rhuan
  • [*][*][*][*]
Re: Proper way of using Sphere?
Reply #1
miniSphere 5.0 is about to be released (due date the 31st) in which the Sphere v2 api will finally be stable additionally I'm hoping that the modules I've been working on to go with it https://github.com/rhuanjl/SphereLibs will provide a good base for a game (though no one other than me has tested them yet).

Previously with the Spherev1 api there were two basic means of working:
1. Use the mapEngine, have anything else you wanted to happen regularly go in the update script and render script and store key data as global objects.
2. Don't use the mapengine (as it's not suited to your project) set up your own loop with an update stage and a render stage etc.

With Sphere v2
1. The engine now provides the loop https://github.com/fatcerberus/minisphere/blob/master/docs/sphere2-core-api.txt#L153-L184
 - it has an update stage for logic a render stage and a dispatch stage (for handling one-offs)
2, I'm hoping that my Map Engine script can replace the v1 mapengine, it's intended to be more flexible and give more control - also for more complex maps it should run faster as there are fast less calls between JS and C
3. Generally with v2 globals are discouraged, run all your code as modules with their own seperate scopes, hand anything you need to share around as parameters.


  • DaVince
  • [*][*][*][*][*]
  • Administrator
  • Used Sphere for, like, half my life
Re: Proper way of using Sphere?
Reply #2
Even with old Sphere, the engine was designed to be flexible and work in a "however you want" fashion, more or less. Newer versions of miniSphere add a LOT of convenience for the things everyone kept making their own solutions before in old Sphere.

But really, the basic idea of "lay out things however you like" is still applicable. You're free to define any scripts and load any code in any fashion you like. You don't even need to necessarily use the built in event loop if you don't want to.

Re: Proper way of using Sphere?
Reply #3
Generally with v2 globals are discouraged, run all your code as modules with their own seperate scopes, hand anything you need to share around as parameters.

Yeah, I remember modules throwing me off a while ago when I read about them when they were implemented.

I've been looking at this thread to try and learn more about them, but I'm still not 100% confident I know how to use them properly.

  • DaVince
  • [*][*][*][*][*]
  • Administrator
  • Used Sphere for, like, half my life
Re: Proper way of using Sphere?
Reply #4
I've started learning miniSphere more recently, but modules are surprisingly simple:

- Create a new mjs file, put some code in there.
- Prepend the export keyword to whatever functions or classes or variables you want to use in other script files.
- Do an import in those other script files.

Example:

main.mjs
Code: [Select]
import { SomeFunction, SomeClass } from '$/myscript'; //Refers to those functions in myscript.mjs

SomeFunction();
var f = new SomeClass();

myscript.mjs
Code: [Select]
export function SomeFunction() {
  //Regular function code in here
}

export class SomeClass() {
  constructor() {
      //Do class stuff in here or whatever
  }
}

Maybe you could come chat in the Discord chat, it's been very useful to me to get the hang of how things work as Fat Cerberus and Rhuan hang out there all the time. :)
  • Last Edit: October 29, 2017, 04:45:58 pm by DaVince

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Proper way of using Sphere?
Reply #5
Note the post you linked talks about CommonJS modules which are quite a bit different structurally from mJS but the same basic idea is present, to split up your program into self-contained units that don't interfere with each other.
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

Re: Proper way of using Sphere?
Reply #6
Really? That's all there is to them? Huh, thought they'd be more complicated than that.

And yeah, I've peeked into the Discord. Looks like you guys talk about stuff related to developing the engine that I can barely make sense of.

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Proper way of using Sphere?
Reply #7
It used to be more complicated with CommonJS but now that it's built into the language, modules are very simple - you export from one file and import to another.  If you don't export it you can't access it from outside.
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

  • DaVince
  • [*][*][*][*][*]
  • Administrator
  • Used Sphere for, like, half my life
Re: Proper way of using Sphere?
Reply #8
Really? That's all there is to them? Huh, thought they'd be more complicated than that.

And yeah, I've peeked into the Discord. Looks like you guys talk about stuff related to developing the engine that I can barely make sense of.
That's just in the #engine-dev channel, which you could mute. #game-dev is there for game development questions and talk, and #spherical for any Sphere or community related talk at all.

Also, now that I'm actively working on a game, that's at least one person who's not talking about the complex inner mechanics all the time! But yeah, it'd be nice to have more people working on projects there, especially since it makes asking and answering questions so easy. It's very motivating.
  • Last Edit: October 29, 2017, 04:53:26 pm by DaVince

  • Rhuan
  • [*][*][*][*]
Re: Proper way of using Sphere?
Reply #9
And yeah, I've peeked into the Discord. Looks like you guys talk about stuff related to developing the engine that I can barely make sense of.
Sorry that's probably my fault :(

As for examples of modules, can I push my Map Engine and related files - all modular and all Sphere v2 code - have a read of the code: https://github.com/rhuanjl/SphereLibs

Re: Proper way of using Sphere?
Reply #10
Didn't want to make another thread to ask this, but does Sphere support looping audio by reading metadata? I've always wondered how do deal with the problem of looping music.
  • Last Edit: November 28, 2017, 08:20:22 pm by Datomas

  • Rhuan
  • [*][*][*][*]
Re: Proper way of using Sphere?
Reply #11
Didn't want to make another thread to ask this, but does Sphere support looping audio by reading metadata? I've always wondered how do deal with the problem of looping music.
As far as I know there is no built in feature for reading metadata.

BUT you can achieve the same effect as long as you use a seekable sound format (such as Ogg).

Sphere v2 soune objects have a writeable position property. Or if using the v1 api there's the setPosition Method and the getPosition method.

You would need to seperately store values for loop points somewhere then have an update script/function that checks the position of the current sound and if it has reached the loop point sets the position to wherever its meant to loop from.

This would be quite a simple system to make in Javascript - let me know if you'd like more detailed guidance.

Re: Proper way of using Sphere?
Reply #12
Looping music, in particular mp3, has it's issues because of how the format is designed.  The fundamental problem occurs because of this:



You can read further into that if you want here, and while there are ways around it for mp3, it's flaky.  Some engines like Unity can get around it by accounting for the delay and padding programmatically/doing processing on the music file (this is more like what Rhuan was saying with changing the position).

To get looping working for music, the best way is to render it into .ogg format and then test how it loops in a good audio editor (eg: Audacity [it's free]).  If the music should be looping well, but there's a seam or click at the looping point, then you have to go down to the sample level of the audio file and make sure it starts and ends at the 0 volume point.  With monotracks you can do that by cutting off a few point samples, but with stereo tracks the easiest way is to fade out the music over the last few sample points and fade in the first few.


In the above, it can't cut be off correctly in stereo, so instead fade it out over the last 6 samples.  The 6 samples here will only last 1/10 of a millisecond, so it won't be noticeable to the listener and should get rid of the click sound.

From there, in sphere, you should be able to play it with repeat as true and it should loop just like in audacity.


If however, you're talking about more precise looping like, pay this part and then start looping this part, then you'll either have to do it in code by playing the first part and then fading in the loop when it should start (as two separate files would probably be easiest here), or maybe using a tracker format like .mod (more limited in terms of what it can do, but does have more complicated looping options).  I'm not sure if .mod is supported in miniSphere though.
  • Last Edit: December 01, 2017, 02:18:08 pm by Beaker

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Proper way of using Sphere?
Reply #13
Yes, miniSphere supports .mod (and .it, .xm etc.) through allegro.  It doesn't support mp3 however, or MIDI.
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub