1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-15 13:51:40 +00:00

Bring that Whole New World to the Old Continent too

Applies the clang-format style to the 2.1 branch as done for master in
5dbf1809c6.
This commit is contained in:
Rémi Verschelde
2017-03-19 00:36:26 +01:00
parent 1d418afe86
commit f8db8a3faa
1308 changed files with 147754 additions and 174357 deletions

View File

@@ -29,63 +29,44 @@
#include "audio_driver_javascript.h"
#include <string.h>
#define MAX_NUMBER_INTERFACES 3
#define MAX_NUMBER_OUTPUT_DEVICES 6
/* Structure for passing information to callback function */
//AudioDriverJavaScript* AudioDriverJavaScript::s_ad=NULL;
const char* AudioDriverJavaScript::get_name() const {
const char *AudioDriverJavaScript::get_name() const {
return "JavaScript";
}
Error AudioDriverJavaScript::init(){
Error AudioDriverJavaScript::init() {
return OK;
}
void AudioDriverJavaScript::start(){
void AudioDriverJavaScript::start() {
}
int AudioDriverJavaScript::get_mix_rate() const {
return 44100;
}
AudioDriverSW::OutputFormat AudioDriverJavaScript::get_output_format() const{
AudioDriverSW::OutputFormat AudioDriverJavaScript::get_output_format() const {
return OUTPUT_STEREO;
}
void AudioDriverJavaScript::lock(){
void AudioDriverJavaScript::lock() {
//if (active && mutex)
// mutex->lock();
}
void AudioDriverJavaScript::unlock() {
//if (active && mutex)
// mutex->unlock();
}
void AudioDriverJavaScript::finish(){
void AudioDriverJavaScript::finish() {
}
AudioDriverJavaScript::AudioDriverJavaScript()
{
AudioDriverJavaScript::AudioDriverJavaScript() {
}

View File

@@ -29,28 +29,24 @@
#ifndef AUDIO_DRIVER_JAVASCRIPT_H
#define AUDIO_DRIVER_JAVASCRIPT_H
#include "servers/audio/audio_server_sw.h"
#include "os/mutex.h"
#include "servers/audio/audio_server_sw.h"
class AudioDriverJavaScript : public AudioDriverSW {
public:
void set_singleton();
virtual const char* get_name() const;
virtual const char *get_name() const;
virtual Error init();
virtual void start();
virtual int get_mix_rate() const ;
virtual int get_mix_rate() const;
virtual OutputFormat get_output_format() const;
virtual void lock();
virtual void unlock();
virtual void finish();
AudioDriverJavaScript();
};
#endif

View File

@@ -35,168 +35,151 @@ AudioMixer *AudioServerJavascript::get_mixer() {
return NULL;
}
void AudioServerJavascript::audio_mixer_chunk_callback(int p_frames){
void AudioServerJavascript::audio_mixer_chunk_callback(int p_frames) {
}
RID AudioServerJavascript::sample_create(SampleFormat p_format, bool p_stereo, int p_length) {
Sample *sample = memnew( Sample );
sample->format=p_format;
sample->stereo=p_stereo;
sample->length=p_length;
sample->loop_begin=0;
sample->loop_end=p_length;
sample->loop_format=SAMPLE_LOOP_NONE;
sample->mix_rate=44100;
sample->index=-1;
Sample *sample = memnew(Sample);
sample->format = p_format;
sample->stereo = p_stereo;
sample->length = p_length;
sample->loop_begin = 0;
sample->loop_end = p_length;
sample->loop_format = SAMPLE_LOOP_NONE;
sample->mix_rate = 44100;
sample->index = -1;
return sample_owner.make_rid(sample);
}
void AudioServerJavascript::sample_set_description(RID p_sample, const String& p_description){
void AudioServerJavascript::sample_set_description(RID p_sample, const String &p_description) {
}
String AudioServerJavascript::sample_get_description(RID p_sample) const{
String AudioServerJavascript::sample_get_description(RID p_sample) const {
return String();
}
AudioServerJavascript::SampleFormat AudioServerJavascript::sample_get_format(RID p_sample) const{
AudioServerJavascript::SampleFormat AudioServerJavascript::sample_get_format(RID p_sample) const {
return SAMPLE_FORMAT_PCM8;
}
bool AudioServerJavascript::sample_is_stereo(RID p_sample) const{
bool AudioServerJavascript::sample_is_stereo(RID p_sample) const {
const Sample *sample = sample_owner.get(p_sample);
ERR_FAIL_COND_V(!sample,false);
ERR_FAIL_COND_V(!sample, false);
return sample->stereo;
}
int AudioServerJavascript::sample_get_length(RID p_sample) const{
int AudioServerJavascript::sample_get_length(RID p_sample) const {
const Sample *sample = sample_owner.get(p_sample);
ERR_FAIL_COND_V(!sample,0);
ERR_FAIL_COND_V(!sample, 0);
return sample->length;
}
const void* AudioServerJavascript::sample_get_data_ptr(RID p_sample) const{
const void *AudioServerJavascript::sample_get_data_ptr(RID p_sample) const {
return NULL;
}
void AudioServerJavascript::sample_set_data(RID p_sample, const DVector<uint8_t>& p_buffer){
void AudioServerJavascript::sample_set_data(RID p_sample, const DVector<uint8_t> &p_buffer) {
Sample *sample = sample_owner.get(p_sample);
ERR_FAIL_COND(!sample);
int chans = sample->stereo?2:1;
int chans = sample->stereo ? 2 : 1;
Vector<float> buffer;
buffer.resize(sample->length*chans);
DVector<uint8_t>::Read r=p_buffer.read();
if (sample->format==SAMPLE_FORMAT_PCM8) {
const int8_t*ptr = (const int8_t*)r.ptr();
for(int i=0;i<sample->length*chans;i++) {
buffer[i]=ptr[i]/128.0;
buffer.resize(sample->length * chans);
DVector<uint8_t>::Read r = p_buffer.read();
if (sample->format == SAMPLE_FORMAT_PCM8) {
const int8_t *ptr = (const int8_t *)r.ptr();
for (int i = 0; i < sample->length * chans; i++) {
buffer[i] = ptr[i] / 128.0;
}
} else if (sample->format==SAMPLE_FORMAT_PCM16){
const int16_t*ptr = (const int16_t*)r.ptr();
for(int i=0;i<sample->length*chans;i++) {
buffer[i]=ptr[i]/32768.0;
} else if (sample->format == SAMPLE_FORMAT_PCM16) {
const int16_t *ptr = (const int16_t *)r.ptr();
for (int i = 0; i < sample->length * chans; i++) {
buffer[i] = ptr[i] / 32768.0;
}
} else {
ERR_EXPLAIN("Unsupported for now");
ERR_FAIL();
}
sample->tmp_data=buffer;
sample->tmp_data = buffer;
}
DVector<uint8_t> AudioServerJavascript::sample_get_data(RID p_sample) const{
DVector<uint8_t> AudioServerJavascript::sample_get_data(RID p_sample) const {
return DVector<uint8_t>();
}
void AudioServerJavascript::sample_set_mix_rate(RID p_sample,int p_rate){
void AudioServerJavascript::sample_set_mix_rate(RID p_sample, int p_rate) {
Sample *sample = sample_owner.get(p_sample);
ERR_FAIL_COND(!sample);
sample->mix_rate=p_rate;
sample->mix_rate = p_rate;
}
int AudioServerJavascript::sample_get_mix_rate(RID p_sample) const{
int AudioServerJavascript::sample_get_mix_rate(RID p_sample) const {
const Sample *sample = sample_owner.get(p_sample);
ERR_FAIL_COND_V(!sample,0);
ERR_FAIL_COND_V(!sample, 0);
return sample->mix_rate;
}
void AudioServerJavascript::sample_set_loop_format(RID p_sample,SampleLoopFormat p_format){
void AudioServerJavascript::sample_set_loop_format(RID p_sample, SampleLoopFormat p_format) {
Sample *sample = sample_owner.get(p_sample);
ERR_FAIL_COND(!sample);
sample->loop_format=p_format;
sample->loop_format = p_format;
}
AudioServerJavascript::SampleLoopFormat AudioServerJavascript::sample_get_loop_format(RID p_sample) const {
const Sample *sample = sample_owner.get(p_sample);
ERR_FAIL_COND_V(!sample,SAMPLE_LOOP_NONE);
ERR_FAIL_COND_V(!sample, SAMPLE_LOOP_NONE);
return sample->loop_format;
}
void AudioServerJavascript::sample_set_loop_begin(RID p_sample,int p_pos){
void AudioServerJavascript::sample_set_loop_begin(RID p_sample, int p_pos) {
Sample *sample = sample_owner.get(p_sample);
ERR_FAIL_COND(!sample);
sample->loop_begin=p_pos;
sample->loop_begin = p_pos;
}
int AudioServerJavascript::sample_get_loop_begin(RID p_sample) const{
int AudioServerJavascript::sample_get_loop_begin(RID p_sample) const {
const Sample *sample = sample_owner.get(p_sample);
ERR_FAIL_COND_V(!sample,0);
ERR_FAIL_COND_V(!sample, 0);
return sample->loop_begin;
}
void AudioServerJavascript::sample_set_loop_end(RID p_sample,int p_pos){
void AudioServerJavascript::sample_set_loop_end(RID p_sample, int p_pos) {
Sample *sample = sample_owner.get(p_sample);
ERR_FAIL_COND(!sample);
sample->loop_end=p_pos;
sample->loop_end = p_pos;
}
int AudioServerJavascript::sample_get_loop_end(RID p_sample) const{
int AudioServerJavascript::sample_get_loop_end(RID p_sample) const {
const Sample *sample = sample_owner.get(p_sample);
ERR_FAIL_COND_V(!sample,0);
ERR_FAIL_COND_V(!sample, 0);
return sample->loop_end;
}
/* VOICE API */
RID AudioServerJavascript::voice_create(){
RID AudioServerJavascript::voice_create() {
Voice *voice = memnew( Voice );
Voice *voice = memnew(Voice);
voice->index=voice_base;
voice->volume=1.0;
voice->pan=0.0;
voice->pan_depth=.0;
voice->pan_height=0.0;
voice->chorus=0;
voice->reverb_type=REVERB_SMALL;
voice->reverb=0;
voice->mix_rate=-1;
voice->positional=false;
voice->active=false;
voice->index = voice_base;
voice->volume = 1.0;
voice->pan = 0.0;
voice->pan_depth = .0;
voice->pan_height = 0.0;
voice->chorus = 0;
voice->reverb_type = REVERB_SMALL;
voice->reverb = 0;
voice->mix_rate = -1;
voice->positional = false;
voice->active = false;
/* clang-format off */
EM_ASM_( {
@@ -210,24 +193,23 @@ RID AudioServerJavascript::voice_create(){
voice_base++;
return voice_owner.make_rid( voice );
return voice_owner.make_rid(voice);
}
void AudioServerJavascript::voice_play(RID p_voice, RID p_sample){
void AudioServerJavascript::voice_play(RID p_voice, RID p_sample) {
Voice* voice=voice_owner.get(p_voice);
Voice *voice = voice_owner.get(p_voice);
ERR_FAIL_COND(!voice);
Sample *sample=sample_owner.get(p_sample);
Sample *sample = sample_owner.get(p_sample);
ERR_FAIL_COND(!sample);
// due to how webaudio works, sample cration is deferred until used
// sorry! WebAudio absolutely sucks
if (sample->index==-1) {
if (sample->index == -1) {
//create sample if not created
ERR_FAIL_COND(sample->tmp_data.size()==0);
sample->index=sample_base;
ERR_FAIL_COND(sample->tmp_data.size() == 0);
sample->index = sample_base;
/* clang-format off */
EM_ASM_({
_as_samples[$0] = _as_audioctx.createBuffer($1, $2, $3);
@@ -235,17 +217,16 @@ void AudioServerJavascript::voice_play(RID p_voice, RID p_sample){
/* clang-format on */
sample_base++;
int chans = sample->stereo?2:1;
int chans = sample->stereo ? 2 : 1;
for(int i=0;i<chans;i++) {
for (int i = 0; i < chans; i++) {
/* clang-format off */
EM_ASM_({
_as_edited_buffer = _as_samples[$0].getChannelData($1);
}, sample->index, i);
/* clang-format on */
for(int j=0;j<sample->length;j++) {
for (int j = 0; j < sample->length; j++) {
/* clang-format off */
EM_ASM_({
_as_edited_buffer[$0] = $1;
@@ -257,14 +238,13 @@ void AudioServerJavascript::voice_play(RID p_voice, RID p_sample){
sample->tmp_data.clear();
}
voice->sample_mix_rate=sample->mix_rate;
if (voice->mix_rate==-1) {
voice->mix_rate=voice->sample_mix_rate;
voice->sample_mix_rate = sample->mix_rate;
if (voice->mix_rate == -1) {
voice->mix_rate = voice->sample_mix_rate;
}
float freq_diff = Math::log(float(voice->mix_rate)/float(voice->sample_mix_rate))/Math::log(2.0);
int detune = int(freq_diff*1200.0);
float freq_diff = Math::log(float(voice->mix_rate) / float(voice->sample_mix_rate)) / Math::log(2.0);
int detune = int(freq_diff * 1200.0);
/* clang-format off */
EM_ASM_({
@@ -288,15 +268,15 @@ void AudioServerJavascript::voice_play(RID p_voice, RID p_sample){
}, voice->index, sample->index, sample->mix_rate * sample->loop_begin, sample->mix_rate * sample->loop_end, sample->loop_format != SAMPLE_LOOP_NONE, voice->pan, voice->volume * fx_volume_scale, detune);
/* clang-format on */
voice->active=true;
voice->active = true;
}
void AudioServerJavascript::voice_set_volume(RID p_voice, float p_volume){
void AudioServerJavascript::voice_set_volume(RID p_voice, float p_volume) {
Voice* voice=voice_owner.get(p_voice);
Voice *voice = voice_owner.get(p_voice);
ERR_FAIL_COND(!voice);
voice->volume=p_volume;
voice->volume = p_volume;
if (voice->active) {
/* clang-format off */
@@ -305,16 +285,15 @@ void AudioServerJavascript::voice_set_volume(RID p_voice, float p_volume){
}, voice->index, voice->volume * fx_volume_scale);
/* clang-format on */
}
}
void AudioServerJavascript::voice_set_pan(RID p_voice, float p_pan, float p_depth,float height){
void AudioServerJavascript::voice_set_pan(RID p_voice, float p_pan, float p_depth, float height) {
Voice* voice=voice_owner.get(p_voice);
Voice *voice = voice_owner.get(p_voice);
ERR_FAIL_COND(!voice);
voice->pan=p_pan;
voice->pan_depth=p_depth;
voice->pan_height=height;
voice->pan = p_pan;
voice->pan_depth = p_depth;
voice->pan_height = height;
if (voice->active) {
/* clang-format off */
@@ -324,26 +303,23 @@ void AudioServerJavascript::voice_set_pan(RID p_voice, float p_pan, float p_dept
/* clang-format on */
}
}
void AudioServerJavascript::voice_set_filter(RID p_voice, FilterType p_type, float p_cutoff, float p_resonance, float p_gain){
void AudioServerJavascript::voice_set_filter(RID p_voice, FilterType p_type, float p_cutoff, float p_resonance, float p_gain) {
}
void AudioServerJavascript::voice_set_chorus(RID p_voice, float p_chorus ){
void AudioServerJavascript::voice_set_chorus(RID p_voice, float p_chorus) {
}
void AudioServerJavascript::voice_set_reverb(RID p_voice, ReverbRoomType p_room_type, float p_reverb){
void AudioServerJavascript::voice_set_reverb(RID p_voice, ReverbRoomType p_room_type, float p_reverb) {
}
void AudioServerJavascript::voice_set_mix_rate(RID p_voice, int p_mix_rate){
void AudioServerJavascript::voice_set_mix_rate(RID p_voice, int p_mix_rate) {
Voice* voice=voice_owner.get(p_voice);
Voice *voice = voice_owner.get(p_voice);
ERR_FAIL_COND(!voice);
voice->mix_rate=p_mix_rate;
voice->mix_rate = p_mix_rate;
if (voice->active) {
float freq_diff = Math::log(float(voice->mix_rate)/float(voice->sample_mix_rate))/Math::log(2.0);
int detune = int(freq_diff*1200.0);
float freq_diff = Math::log(float(voice->mix_rate) / float(voice->sample_mix_rate)) / Math::log(2.0);
int detune = int(freq_diff * 1200.0);
/* clang-format off */
EM_ASM_({
_as_voices[$0].detune.value = $1;
@@ -351,75 +327,74 @@ void AudioServerJavascript::voice_set_mix_rate(RID p_voice, int p_mix_rate){
/* clang-format on */
}
}
void AudioServerJavascript::voice_set_positional(RID p_voice, bool p_positional){
void AudioServerJavascript::voice_set_positional(RID p_voice, bool p_positional) {
}
float AudioServerJavascript::voice_get_volume(RID p_voice) const{
float AudioServerJavascript::voice_get_volume(RID p_voice) const {
Voice* voice=voice_owner.get(p_voice);
ERR_FAIL_COND_V(!voice,0);
Voice *voice = voice_owner.get(p_voice);
ERR_FAIL_COND_V(!voice, 0);
return voice->volume;
}
float AudioServerJavascript::voice_get_pan(RID p_voice) const{
float AudioServerJavascript::voice_get_pan(RID p_voice) const {
Voice* voice=voice_owner.get(p_voice);
ERR_FAIL_COND_V(!voice,0);
Voice *voice = voice_owner.get(p_voice);
ERR_FAIL_COND_V(!voice, 0);
return voice->pan;
}
float AudioServerJavascript::voice_get_pan_depth(RID p_voice) const{
Voice* voice=voice_owner.get(p_voice);
ERR_FAIL_COND_V(!voice,0);
float AudioServerJavascript::voice_get_pan_depth(RID p_voice) const {
Voice *voice = voice_owner.get(p_voice);
ERR_FAIL_COND_V(!voice, 0);
return voice->pan_depth;
}
float AudioServerJavascript::voice_get_pan_height(RID p_voice) const{
float AudioServerJavascript::voice_get_pan_height(RID p_voice) const {
Voice* voice=voice_owner.get(p_voice);
ERR_FAIL_COND_V(!voice,0);
Voice *voice = voice_owner.get(p_voice);
ERR_FAIL_COND_V(!voice, 0);
return voice->pan_height;
}
AudioServerJavascript::FilterType AudioServerJavascript::voice_get_filter_type(RID p_voice) const{
AudioServerJavascript::FilterType AudioServerJavascript::voice_get_filter_type(RID p_voice) const {
return FILTER_NONE;
}
float AudioServerJavascript::voice_get_filter_cutoff(RID p_voice) const{
float AudioServerJavascript::voice_get_filter_cutoff(RID p_voice) const {
return 0;
}
float AudioServerJavascript::voice_get_filter_resonance(RID p_voice) const{
float AudioServerJavascript::voice_get_filter_resonance(RID p_voice) const {
return 0;
}
float AudioServerJavascript::voice_get_chorus(RID p_voice) const{
float AudioServerJavascript::voice_get_chorus(RID p_voice) const {
return 0;
}
AudioServerJavascript::ReverbRoomType AudioServerJavascript::voice_get_reverb_type(RID p_voice) const{
AudioServerJavascript::ReverbRoomType AudioServerJavascript::voice_get_reverb_type(RID p_voice) const {
return REVERB_SMALL;
}
float AudioServerJavascript::voice_get_reverb(RID p_voice) const{
float AudioServerJavascript::voice_get_reverb(RID p_voice) const {
return 0;
}
int AudioServerJavascript::voice_get_mix_rate(RID p_voice) const{
int AudioServerJavascript::voice_get_mix_rate(RID p_voice) const {
return 44100;
}
bool AudioServerJavascript::voice_is_positional(RID p_voice) const{
bool AudioServerJavascript::voice_is_positional(RID p_voice) const {
return false;
}
void AudioServerJavascript::voice_stop(RID p_voice){
void AudioServerJavascript::voice_stop(RID p_voice) {
Voice* voice=voice_owner.get(p_voice);
Voice *voice = voice_owner.get(p_voice);
ERR_FAIL_COND(!voice);
if (voice->active) {
@@ -433,14 +408,12 @@ void AudioServerJavascript::voice_stop(RID p_voice){
}, voice->index);
/* clang-format on */
voice->active=false;
voice->active = false;
}
}
bool AudioServerJavascript::voice_is_active(RID p_voice) const{
Voice* voice=voice_owner.get(p_voice);
ERR_FAIL_COND_V(!voice,false);
bool AudioServerJavascript::voice_is_active(RID p_voice) const {
Voice *voice = voice_owner.get(p_voice);
ERR_FAIL_COND_V(!voice, false);
return voice->active;
}
@@ -449,13 +422,12 @@ bool AudioServerJavascript::voice_is_active(RID p_voice) const{
RID AudioServerJavascript::audio_stream_create(AudioStream *p_stream) {
Stream *s = memnew(Stream);
s->audio_stream=p_stream;
s->event_stream=NULL;
s->active=false;
s->E=NULL;
s->volume_scale=1.0;
s->audio_stream = p_stream;
s->event_stream = NULL;
s->active = false;
s->E = NULL;
s->volume_scale = 1.0;
p_stream->set_mix_rate(webaudio_mix_rate);
return stream_owner.make_rid(s);
@@ -463,43 +435,38 @@ RID AudioServerJavascript::audio_stream_create(AudioStream *p_stream) {
RID AudioServerJavascript::event_stream_create(EventStream *p_stream) {
Stream *s = memnew(Stream);
s->audio_stream=NULL;
s->event_stream=p_stream;
s->active=false;
s->E=NULL;
s->volume_scale=1.0;
s->audio_stream = NULL;
s->event_stream = p_stream;
s->active = false;
s->E = NULL;
s->volume_scale = 1.0;
//p_stream->set_mix_rate(AudioDriverJavascript::get_singleton()->get_mix_rate());
return stream_owner.make_rid(s);
}
void AudioServerJavascript::stream_set_active(RID p_stream, bool p_active) {
Stream *s = stream_owner.get(p_stream);
ERR_FAIL_COND(!s);
if (s->active==p_active)
if (s->active == p_active)
return;
s->active=p_active;
s->active = p_active;
if (p_active)
s->E=active_audio_streams.push_back(s);
s->E = active_audio_streams.push_back(s);
else {
active_audio_streams.erase(s->E);
s->E=NULL;
s->E = NULL;
}
}
bool AudioServerJavascript::stream_is_active(RID p_stream) const {
Stream *s = stream_owner.get(p_stream);
ERR_FAIL_COND_V(!s,false);
ERR_FAIL_COND_V(!s, false);
return s->active;
}
@@ -507,25 +474,22 @@ void AudioServerJavascript::stream_set_volume_scale(RID p_stream, float p_scale)
Stream *s = stream_owner.get(p_stream);
ERR_FAIL_COND(!s);
s->volume_scale=p_scale;
s->volume_scale = p_scale;
}
float AudioServerJavascript::stream_set_volume_scale(RID p_stream) const {
Stream *s = stream_owner.get(p_stream);
ERR_FAIL_COND_V(!s,0);
ERR_FAIL_COND_V(!s, 0);
return s->volume_scale;
}
/* Audio Physics API */
void AudioServerJavascript::free(RID p_id){
void AudioServerJavascript::free(RID p_id) {
if (voice_owner.owns(p_id)) {
Voice* voice=voice_owner.get(p_id);
Voice *voice = voice_owner.get(p_id);
ERR_FAIL_COND(!voice);
if (voice->active) {
@@ -568,11 +532,10 @@ void AudioServerJavascript::free(RID p_id){
} else if (stream_owner.owns(p_id)) {
Stream *s=stream_owner.get(p_id);
Stream *s = stream_owner.get(p_id);
if (s->active) {
stream_set_active(p_id,false);
stream_set_active(p_id, false);
}
memdelete(s);
@@ -582,25 +545,22 @@ void AudioServerJavascript::free(RID p_id){
extern "C" {
void audio_server_mix_function(int p_frames) {
//print_line("MIXI! "+itos(p_frames));
static_cast<AudioServerJavascript*>(AudioServerJavascript::get_singleton())->mix_to_js(p_frames);
static_cast<AudioServerJavascript *>(AudioServerJavascript::get_singleton())->mix_to_js(p_frames);
}
}
void AudioServerJavascript::mix_to_js(int p_frames) {
//process in chunks to make sure to never process more than INTERNAL_BUFFER_SIZE
int todo=p_frames;
int offset=0;
int todo = p_frames;
int offset = 0;
while(todo) {
while (todo) {
int tomix=MIN(todo,INTERNAL_BUFFER_SIZE);
int tomix = MIN(todo, INTERNAL_BUFFER_SIZE);
driver_process_chunk(tomix);
/* clang-format off */
@@ -618,12 +578,12 @@ void AudioServerJavascript::mix_to_js(int p_frames) {
}, internal_buffer, offset, tomix);
/* clang-format on */
todo-=tomix;
offset+=tomix;
todo -= tomix;
offset += tomix;
}
}
void AudioServerJavascript::init(){
void AudioServerJavascript::init() {
/*
// clang-format off
@@ -633,16 +593,15 @@ void AudioServerJavascript::init(){
// clang-format on
*/
//int latency = GLOBAL_DEF("javascript/audio_latency",16384);
internal_buffer_channels=2;
internal_buffer = memnew_arr(float,INTERNAL_BUFFER_SIZE*internal_buffer_channels);
stream_buffer = memnew_arr(int32_t,INTERNAL_BUFFER_SIZE*4); //max 4 channels
internal_buffer_channels = 2;
internal_buffer = memnew_arr(float, INTERNAL_BUFFER_SIZE *internal_buffer_channels);
stream_buffer = memnew_arr(int32_t, INTERNAL_BUFFER_SIZE * 4); //max 4 channels
stream_volume=0.3;
stream_volume = 0.3;
int buffer_latency=16384;
int buffer_latency = 16384;
/* clang-format off */
EM_ASM_( {
@@ -657,79 +616,73 @@ void AudioServerJavascript::init(){
}
}, buffer_latency);
/* clang-format on */
}
void AudioServerJavascript::finish(){
void AudioServerJavascript::finish() {
}
void AudioServerJavascript::update(){
void AudioServerJavascript::update() {
for(List<Stream*>::Element *E=active_audio_streams.front();E;) { //stream might be removed durnig this callback
for (List<Stream *>::Element *E = active_audio_streams.front(); E;) { //stream might be removed durnig this callback
List<Stream*>::Element *N=E->next();
List<Stream *>::Element *N = E->next();
if (E->get()->audio_stream)
E->get()->audio_stream->update();
E=N;
E = N;
}
}
/* MISC config */
void AudioServerJavascript::lock(){
void AudioServerJavascript::lock() {
}
void AudioServerJavascript::unlock(){
void AudioServerJavascript::unlock() {
}
int AudioServerJavascript::get_default_channel_count() const{
int AudioServerJavascript::get_default_channel_count() const {
return 1;
}
int AudioServerJavascript::get_default_mix_rate() const{
int AudioServerJavascript::get_default_mix_rate() const {
return 44100;
}
void AudioServerJavascript::set_stream_global_volume_scale(float p_volume){
void AudioServerJavascript::set_stream_global_volume_scale(float p_volume) {
stream_volume_scale=p_volume;
stream_volume_scale = p_volume;
}
void AudioServerJavascript::set_fx_global_volume_scale(float p_volume){
void AudioServerJavascript::set_fx_global_volume_scale(float p_volume) {
fx_volume_scale=p_volume;
fx_volume_scale = p_volume;
}
void AudioServerJavascript::set_event_voice_global_volume_scale(float p_volume){
void AudioServerJavascript::set_event_voice_global_volume_scale(float p_volume) {
}
float AudioServerJavascript::get_stream_global_volume_scale() const{
float AudioServerJavascript::get_stream_global_volume_scale() const {
return 1;
}
float AudioServerJavascript::get_fx_global_volume_scale() const{
float AudioServerJavascript::get_fx_global_volume_scale() const {
return 1;
}
float AudioServerJavascript::get_event_voice_global_volume_scale() const{
float AudioServerJavascript::get_event_voice_global_volume_scale() const {
return 1;
}
uint32_t AudioServerJavascript::read_output_peak() const{
uint32_t AudioServerJavascript::read_output_peak() const {
return 0;
}
AudioServerJavascript *AudioServerJavascript::singleton=NULL;
AudioServerJavascript *AudioServerJavascript::singleton = NULL;
AudioServer *AudioServerJavascript::get_singleton() {
return singleton;
}
double AudioServerJavascript::get_mix_time() const{
double AudioServerJavascript::get_mix_time() const {
return 0;
}
@@ -738,69 +691,61 @@ double AudioServerJavascript::get_output_delay() const {
return 0;
}
void AudioServerJavascript::driver_process_chunk(int p_frames) {
int samples = p_frames * internal_buffer_channels;
int samples=p_frames*internal_buffer_channels;
for(int i=0;i<samples;i++) {
internal_buffer[i]=0;
for (int i = 0; i < samples; i++) {
internal_buffer[i] = 0;
}
for(List<Stream*>::Element *E=active_audio_streams.front();E;E=E->next()) {
for (List<Stream *>::Element *E = active_audio_streams.front(); E; E = E->next()) {
ERR_CONTINUE(!E->get()->active); // bug?
AudioStream *as=E->get()->audio_stream;
AudioStream *as = E->get()->audio_stream;
if (!as)
continue;
int channels=as->get_channel_count();
if (channels==0)
int channels = as->get_channel_count();
if (channels == 0)
continue; // does not want mix
if (!as->mix(stream_buffer,p_frames))
if (!as->mix(stream_buffer, p_frames))
continue; //nothing was mixed!!
int32_t stream_vol_scale=(stream_volume*stream_volume_scale*E->get()->volume_scale)*(1<<STREAM_SCALE_BITS);
int32_t stream_vol_scale = (stream_volume * stream_volume_scale * E->get()->volume_scale) * (1 << STREAM_SCALE_BITS);
#define STRSCALE(m_val) ((((m_val>>STREAM_SCALE_BITS)*stream_vol_scale)>>8)/8388608.0)
switch(channels) {
#define STRSCALE(m_val) ((((m_val >> STREAM_SCALE_BITS) * stream_vol_scale) >> 8) / 8388608.0)
switch (channels) {
case 1: {
for(int i=0;i<p_frames;i++) {
for (int i = 0; i < p_frames; i++) {
internal_buffer[(i<<1)+0]+=STRSCALE(stream_buffer[i]);
internal_buffer[(i<<1)+1]+=STRSCALE(stream_buffer[i]);
internal_buffer[(i << 1) + 0] += STRSCALE(stream_buffer[i]);
internal_buffer[(i << 1) + 1] += STRSCALE(stream_buffer[i]);
}
} break;
case 2: {
for(int i=0;i<p_frames*2;i++) {
for (int i = 0; i < p_frames * 2; i++) {
internal_buffer[i]+=STRSCALE(stream_buffer[i]);
internal_buffer[i] += STRSCALE(stream_buffer[i]);
}
} break;
case 4: {
for(int i=0;i<p_frames;i++) {
for (int i = 0; i < p_frames; i++) {
internal_buffer[(i<<2)+0]+=STRSCALE((stream_buffer[(i<<2)+0]+stream_buffer[(i<<2)+2])>>1);
internal_buffer[(i<<2)+1]+=STRSCALE((stream_buffer[(i<<2)+1]+stream_buffer[(i<<2)+3])>>1);
internal_buffer[(i << 2) + 0] += STRSCALE((stream_buffer[(i << 2) + 0] + stream_buffer[(i << 2) + 2]) >> 1);
internal_buffer[(i << 2) + 1] += STRSCALE((stream_buffer[(i << 2) + 1] + stream_buffer[(i << 2) + 3]) >> 1);
}
} break;
}
#undef STRSCALE
}
}
/*void AudioServerSW::driver_process(int p_frames,int32_t *p_buffer) {
@@ -820,9 +765,9 @@ void AudioServerJavascript::driver_process_chunk(int p_frames) {
AudioServerJavascript::AudioServerJavascript() {
singleton=this;
sample_base=1;
voice_base=1;
singleton = this;
sample_base = 1;
voice_base = 1;
/* clang-format off */
EM_ASM(
_as_samples = {};
@@ -841,9 +786,8 @@ AudioServerJavascript::AudioServerJavascript() {
return _as_audioctx.sampleRate;
);
/* clang-format on */
print_line("WEBAUDIO MIX RATE: "+itos(webaudio_mix_rate));
event_voice_scale=1.0;
fx_volume_scale=1.0;
stream_volume_scale=1.0;
print_line("WEBAUDIO MIX RATE: " + itos(webaudio_mix_rate));
event_voice_scale = 1.0;
fx_volume_scale = 1.0;
stream_volume_scale = 1.0;
}

View File

@@ -29,16 +29,15 @@
#ifndef AUDIO_SERVER_JAVASCRIPT_H
#define AUDIO_SERVER_JAVASCRIPT_H
#include "servers/audio_server.h"
class AudioServerJavascript : public AudioServer {
class AudioServerJavascript : public AudioServer {
OBJ_TYPE(AudioServerJavascript,AudioServer);
OBJ_TYPE(AudioServerJavascript, AudioServer);
enum {
INTERNAL_BUFFER_SIZE=4096,
STREAM_SCALE_BITS=12
INTERNAL_BUFFER_SIZE = 4096,
STREAM_SCALE_BITS = 12
};
@@ -77,7 +76,6 @@ class AudioServerJavascript : public AudioServer {
bool positional;
bool active;
};
mutable RID_Owner<Voice> voice_owner;
@@ -86,19 +84,19 @@ class AudioServerJavascript : public AudioServer {
struct Stream {
bool active;
List<Stream*>::Element *E;
List<Stream *>::Element *E;
AudioStream *audio_stream;
EventStream *event_stream;
float volume_scale;
};
List<Stream*> active_audio_streams;
List<Stream *> active_audio_streams;
//List<Stream*> event_streams;
float * internal_buffer;
float *internal_buffer;
int internal_buffer_channels;
int32_t * stream_buffer;
int32_t *stream_buffer;
mutable RID_Owner<Stream> stream_owner;
@@ -108,45 +106,41 @@ class AudioServerJavascript : public AudioServer {
float event_voice_scale;
float fx_volume_scale;
void driver_process_chunk(int p_frames);
int webaudio_mix_rate;
static AudioServerJavascript *singleton;
public:
public:
void mix_to_js(int p_frames);
/* SAMPLE API */
virtual RID sample_create(SampleFormat p_format, bool p_stereo, int p_length);
virtual void sample_set_description(RID p_sample, const String& p_description);
virtual void sample_set_description(RID p_sample, const String &p_description);
virtual String sample_get_description(RID p_sample) const;
virtual SampleFormat sample_get_format(RID p_sample) const;
virtual bool sample_is_stereo(RID p_sample) const;
virtual int sample_get_length(RID p_sample) const;
virtual const void* sample_get_data_ptr(RID p_sample) const;
virtual const void *sample_get_data_ptr(RID p_sample) const;
virtual void sample_set_data(RID p_sample, const DVector<uint8_t>& p_buffer);
virtual void sample_set_data(RID p_sample, const DVector<uint8_t> &p_buffer);
virtual DVector<uint8_t> sample_get_data(RID p_sample) const;
virtual void sample_set_mix_rate(RID p_sample,int p_rate);
virtual void sample_set_mix_rate(RID p_sample, int p_rate);
virtual int sample_get_mix_rate(RID p_sample) const;
virtual void sample_set_loop_format(RID p_sample,SampleLoopFormat p_format);
virtual void sample_set_loop_format(RID p_sample, SampleLoopFormat p_format);
virtual SampleLoopFormat sample_get_loop_format(RID p_sample) const;
virtual void sample_set_loop_begin(RID p_sample,int p_pos);
virtual void sample_set_loop_begin(RID p_sample, int p_pos);
virtual int sample_get_loop_begin(RID p_sample) const;
virtual void sample_set_loop_end(RID p_sample,int p_pos);
virtual void sample_set_loop_end(RID p_sample, int p_pos);
virtual int sample_get_loop_end(RID p_sample) const;
/* VOICE API */
virtual RID voice_create();
@@ -154,9 +148,9 @@ public:
virtual void voice_play(RID p_voice, RID p_sample);
virtual void voice_set_volume(RID p_voice, float p_volume);
virtual void voice_set_pan(RID p_voice, float p_pan, float p_depth=0,float height=0); //pan and depth go from -1 to 1
virtual void voice_set_filter(RID p_voice, FilterType p_type, float p_cutoff, float p_resonance, float p_gain=0);
virtual void voice_set_chorus(RID p_voice, float p_chorus );
virtual void voice_set_pan(RID p_voice, float p_pan, float p_depth = 0, float height = 0); //pan and depth go from -1 to 1
virtual void voice_set_filter(RID p_voice, FilterType p_type, float p_cutoff, float p_resonance, float p_gain = 0);
virtual void voice_set_chorus(RID p_voice, float p_chorus);
virtual void voice_set_reverb(RID p_voice, ReverbRoomType p_room_type, float p_reverb);
virtual void voice_set_mix_rate(RID p_voice, int p_mix_rate);
virtual void voice_set_positional(RID p_voice, bool p_positional);
@@ -219,7 +213,6 @@ public:
virtual double get_mix_time() const; //useful for video -> audio sync
virtual double get_output_delay() const;
AudioServerJavascript();
};

View File

@@ -236,7 +236,7 @@ int dom2godot_scancode(int dom_keycode) {
return KEY_F1 + (dom_keycode - DOM_VK_F1);
}
switch(dom_keycode) {
switch (dom_keycode) {
//case DOM_VK_CANCEL: return KEY_UNKNOWN;
case DOM_VK_HELP: return KEY_HELP;
case DOM_VK_BACK_SPACE: return KEY_BACKSPACE;
@@ -258,7 +258,8 @@ int dom2godot_scancode(int dom_keycode) {
return KEY_ALT;
case DOM_VK_PAUSE: return KEY_PAUSE;
case DOM_VK_CAPS_LOCK: return KEY_CAPSLOCK;
case DOM_VK_CAPS_LOCK:
return KEY_CAPSLOCK;
/*
case DOM_VK_KANA: return KEY_UNKNOWN;
@@ -270,7 +271,8 @@ int dom2godot_scancode(int dom_keycode) {
case DOM_VK_KANJI: return KEY_UNKNOWN;
*/
case DOM_VK_ESCAPE: return KEY_ESCAPE;
case DOM_VK_ESCAPE:
return KEY_ESCAPE;
/*
case DOM_VK_CONVERT: return KEY_UNKNOWN;
case DOM_VK_NONCONVERT: return KEY_UNKNOWN;
@@ -286,7 +288,8 @@ int dom2godot_scancode(int dom_keycode) {
case DOM_VK_LEFT: return KEY_LEFT;
case DOM_VK_UP: return KEY_UP;
case DOM_VK_RIGHT: return KEY_RIGHT;
case DOM_VK_DOWN: return KEY_DOWN;
case DOM_VK_DOWN:
return KEY_DOWN;
//case DOM_VK_SELECT: return KEY_UNKNOWN;
@@ -303,15 +306,18 @@ int dom2godot_scancode(int dom_keycode) {
return KEY_META;
case DOM_VK_CONTEXT_MENU: return KEY_MENU;
case DOM_VK_SLEEP: return KEY_STANDBY;
case DOM_VK_SLEEP:
return KEY_STANDBY;
// these are numpad keys according to MDN
case DOM_VK_MULTIPLY: return KEY_KP_MULTIPLY;
case DOM_VK_ADD: return KEY_KP_ADD;
case DOM_VK_SEPARATOR: return KEY_KP_PERIOD; // good enough?
case DOM_VK_SEPARATOR:
return KEY_KP_PERIOD; // good enough?
case DOM_VK_SUBTRACT: return KEY_KP_SUBTRACT;
case DOM_VK_DECIMAL: return KEY_KP_PERIOD;
case DOM_VK_DIVIDE: return KEY_KP_DIVIDE;
case DOM_VK_DIVIDE:
return KEY_KP_DIVIDE;
/*
case DOM_VK_F17: return KEY_UNKNOWN;
@@ -325,7 +331,8 @@ int dom2godot_scancode(int dom_keycode) {
*/
case DOM_VK_NUM_LOCK: return KEY_NUMLOCK;
case DOM_VK_SCROLL_LOCK: return KEY_SCROLLLOCK;
case DOM_VK_SCROLL_LOCK:
return KEY_SCROLLLOCK;
/*
case DOM_VK_WIN_OEM_FJ_JISHO: return KEY_UNKNOWN;
@@ -364,7 +371,8 @@ int dom2godot_scancode(int dom_keycode) {
case DOM_VK_OPEN_BRACKET: return KEY_BRACKETLEFT;
case DOM_VK_BACK_SLASH: return KEY_BACKSLASH;
case DOM_VK_CLOSE_BRACKET: return KEY_BRACKETRIGHT;
case DOM_VK_QUOTE: return KEY_APOSTROPHE;
case DOM_VK_QUOTE:
return KEY_APOSTROPHE;
// rest is OEM/unusual

View File

@@ -26,21 +26,21 @@
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "version.h"
#include "export.h"
#include "editor/editor_settings.h"
#include "editor/editor_import_export.h"
#include "editor/editor_node.h"
#include "io/zip_io.h"
#include "io/marshalls.h"
#include "editor/editor_settings.h"
#include "globals.h"
#include "io/marshalls.h"
#include "io/zip_io.h"
#include "os/file_access.h"
#include "os/os.h"
#include "platform/javascript/logo.h"
#include "string.h"
#include "version.h"
class EditorExportPlatformJavaScript : public EditorExportPlatform {
OBJ_TYPE( EditorExportPlatformJavaScript,EditorExportPlatform );
OBJ_TYPE(EditorExportPlatformJavaScript, EditorExportPlatform);
String custom_release_package;
String custom_debug_package;
@@ -50,7 +50,7 @@ class EditorExportPlatformJavaScript : public EditorExportPlatform {
PACK_MULTIPLE_FILES
};
void _fix_html(Vector<uint8_t>& p_html, const String& p_name, bool p_debug);
void _fix_html(Vector<uint8_t> &p_html, const String &p_name, bool p_debug);
PackMode pack_mode;
@@ -68,207 +68,194 @@ class EditorExportPlatformJavaScript : public EditorExportPlatform {
Ref<ImageTexture> logo;
protected:
bool _set(const StringName& p_name, const Variant& p_value);
bool _get(const StringName& p_name,Variant &r_ret) const;
void _get_property_list( List<PropertyInfo> *p_list) const;
bool _set(const StringName &p_name, const Variant &p_value);
bool _get(const StringName &p_name, Variant &r_ret) const;
void _get_property_list(List<PropertyInfo> *p_list) const;
public:
virtual String get_name() const { return "HTML5"; }
virtual ImageCompression get_image_compression() const { return IMAGE_COMPRESSION_BC; }
virtual Ref<Texture> get_logo() const { return logo; }
virtual bool poll_devices() { return show_run?true:false;}
virtual int get_device_count() const { return show_run?1:0; };
virtual String get_device_name(int p_device) const { return "Run in Browser"; }
virtual bool poll_devices() { return show_run ? true : false; }
virtual int get_device_count() const { return show_run ? 1 : 0; };
virtual String get_device_name(int p_device) const { return "Run in Browser"; }
virtual String get_device_info(int p_device) const { return "Run exported HTML in the system's default browser."; }
virtual Error run(int p_device,int p_flags=0);
virtual Error run(int p_device, int p_flags = 0);
virtual bool requires_password(bool p_debug) const { return false; }
virtual String get_binary_extension() const { return "html"; }
virtual Error export_project(const String& p_path,bool p_debug,int p_flags=0);
virtual Error export_project(const String &p_path, bool p_debug, int p_flags = 0);
virtual bool can_export(String *r_error=NULL) const;
virtual bool can_export(String *r_error = NULL) const;
EditorExportPlatformJavaScript();
~EditorExportPlatformJavaScript();
};
bool EditorExportPlatformJavaScript::_set(const StringName& p_name, const Variant& p_value) {
bool EditorExportPlatformJavaScript::_set(const StringName &p_name, const Variant &p_value) {
String n=p_name;
String n = p_name;
if (n=="custom_package/debug")
custom_debug_package=p_value;
else if (n=="custom_package/release")
custom_release_package=p_value;
else if (n=="browser/enable_run")
show_run=p_value;
else if (n=="options/memory_size")
max_memory=p_value;
else if (n=="html/title")
html_title=p_value;
else if (n=="html/head_include")
html_head_include=p_value;
else if (n=="html/font_family")
html_font_family=p_value;
else if (n=="html/style_include")
html_style_include=p_value;
else if (n=="html/controls_enabled")
html_controls_enabled=p_value;
if (n == "custom_package/debug")
custom_debug_package = p_value;
else if (n == "custom_package/release")
custom_release_package = p_value;
else if (n == "browser/enable_run")
show_run = p_value;
else if (n == "options/memory_size")
max_memory = p_value;
else if (n == "html/title")
html_title = p_value;
else if (n == "html/head_include")
html_head_include = p_value;
else if (n == "html/font_family")
html_font_family = p_value;
else if (n == "html/style_include")
html_style_include = p_value;
else if (n == "html/controls_enabled")
html_controls_enabled = p_value;
else
return false;
return true;
}
bool EditorExportPlatformJavaScript::_get(const StringName& p_name,Variant &r_ret) const{
bool EditorExportPlatformJavaScript::_get(const StringName &p_name, Variant &r_ret) const {
String n=p_name;
String n = p_name;
if (n=="custom_package/debug")
r_ret=custom_debug_package;
else if (n=="custom_package/release")
r_ret=custom_release_package;
else if (n=="browser/enable_run")
r_ret=show_run;
else if (n=="options/memory_size")
r_ret=max_memory;
else if (n=="html/title")
r_ret=html_title;
else if (n=="html/head_include")
r_ret=html_head_include;
else if (n=="html/font_family")
r_ret=html_font_family;
else if (n=="html/style_include")
r_ret=html_style_include;
else if (n=="html/controls_enabled")
r_ret=html_controls_enabled;
if (n == "custom_package/debug")
r_ret = custom_debug_package;
else if (n == "custom_package/release")
r_ret = custom_release_package;
else if (n == "browser/enable_run")
r_ret = show_run;
else if (n == "options/memory_size")
r_ret = max_memory;
else if (n == "html/title")
r_ret = html_title;
else if (n == "html/head_include")
r_ret = html_head_include;
else if (n == "html/font_family")
r_ret = html_font_family;
else if (n == "html/style_include")
r_ret = html_style_include;
else if (n == "html/controls_enabled")
r_ret = html_controls_enabled;
else
return false;
return true;
}
void EditorExportPlatformJavaScript::_get_property_list( List<PropertyInfo> *p_list) const{
p_list->push_back( PropertyInfo( Variant::STRING, "custom_package/debug", PROPERTY_HINT_GLOBAL_FILE,"zip"));
p_list->push_back( PropertyInfo( Variant::STRING, "custom_package/release", PROPERTY_HINT_GLOBAL_FILE,"zip"));
p_list->push_back( PropertyInfo( Variant::INT, "options/memory_size",PROPERTY_HINT_ENUM,"32mb,64mb,128mb,256mb,512mb,1024mb"));
p_list->push_back( PropertyInfo( Variant::BOOL, "browser/enable_run"));
p_list->push_back( PropertyInfo( Variant::STRING, "html/title"));
p_list->push_back( PropertyInfo( Variant::STRING, "html/head_include",PROPERTY_HINT_MULTILINE_TEXT));
p_list->push_back( PropertyInfo( Variant::STRING, "html/font_family"));
p_list->push_back( PropertyInfo( Variant::STRING, "html/style_include",PROPERTY_HINT_MULTILINE_TEXT));
p_list->push_back( PropertyInfo( Variant::BOOL, "html/controls_enabled"));
void EditorExportPlatformJavaScript::_get_property_list(List<PropertyInfo> *p_list) const {
p_list->push_back(PropertyInfo(Variant::STRING, "custom_package/debug", PROPERTY_HINT_GLOBAL_FILE, "zip"));
p_list->push_back(PropertyInfo(Variant::STRING, "custom_package/release", PROPERTY_HINT_GLOBAL_FILE, "zip"));
p_list->push_back(PropertyInfo(Variant::INT, "options/memory_size", PROPERTY_HINT_ENUM, "32mb,64mb,128mb,256mb,512mb,1024mb"));
p_list->push_back(PropertyInfo(Variant::BOOL, "browser/enable_run"));
p_list->push_back(PropertyInfo(Variant::STRING, "html/title"));
p_list->push_back(PropertyInfo(Variant::STRING, "html/head_include", PROPERTY_HINT_MULTILINE_TEXT));
p_list->push_back(PropertyInfo(Variant::STRING, "html/font_family"));
p_list->push_back(PropertyInfo(Variant::STRING, "html/style_include", PROPERTY_HINT_MULTILINE_TEXT));
p_list->push_back(PropertyInfo(Variant::BOOL, "html/controls_enabled"));
//p_list->push_back( PropertyInfo( Variant::INT, "resources/pack_mode", PROPERTY_HINT_ENUM,"Copy,Single Exec.,Pack (.pck),Bundles (Optical)"));
}
void EditorExportPlatformJavaScript::_fix_html(Vector<uint8_t>& p_html, const String& p_name, bool p_debug) {
void EditorExportPlatformJavaScript::_fix_html(Vector<uint8_t> &p_html, const String &p_name, bool p_debug) {
String str;
String strnew;
str.parse_utf8((const char*)p_html.ptr(),p_html.size());
Vector<String> lines=str.split("\n");
for(int i=0;i<lines.size();i++) {
str.parse_utf8((const char *)p_html.ptr(), p_html.size());
Vector<String> lines = str.split("\n");
for (int i = 0; i < lines.size(); i++) {
String current_line = lines[i];
current_line = current_line.replace("$GODOT_TMEM",itos((1<<(max_memory+5))*1024*1024));
current_line = current_line.replace("$GODOT_FS",p_name+"fs.js");
current_line = current_line.replace("$GODOT_MEM",p_name+".mem");
current_line = current_line.replace("$GODOT_JS",p_name+".js");
current_line = current_line.replace("$GODOT_ASM",p_name+".asm.js");
current_line = current_line.replace("$GODOT_CANVAS_WIDTH",Globals::get_singleton()->get("display/width"));
current_line = current_line.replace("$GODOT_CANVAS_HEIGHT",Globals::get_singleton()->get("display/height"));
current_line = current_line.replace("$GODOT_HEAD_TITLE",!html_title.empty()?html_title:(String) Globals::get_singleton()->get("application/name"));
current_line = current_line.replace("$GODOT_HEAD_INCLUDE",html_head_include);
current_line = current_line.replace("$GODOT_STYLE_FONT_FAMILY",html_font_family);
current_line = current_line.replace("$GODOT_STYLE_INCLUDE",html_style_include);
current_line = current_line.replace("$GODOT_CONTROLS_ENABLED",html_controls_enabled?"true":"false");
current_line = current_line.replace("$GODOT_DEBUG_ENABLED",p_debug?"true":"false");
strnew += current_line+"\n";
current_line = current_line.replace("$GODOT_TMEM", itos((1 << (max_memory + 5)) * 1024 * 1024));
current_line = current_line.replace("$GODOT_FS", p_name + "fs.js");
current_line = current_line.replace("$GODOT_MEM", p_name + ".mem");
current_line = current_line.replace("$GODOT_JS", p_name + ".js");
current_line = current_line.replace("$GODOT_ASM", p_name + ".asm.js");
current_line = current_line.replace("$GODOT_CANVAS_WIDTH", Globals::get_singleton()->get("display/width"));
current_line = current_line.replace("$GODOT_CANVAS_HEIGHT", Globals::get_singleton()->get("display/height"));
current_line = current_line.replace("$GODOT_HEAD_TITLE", !html_title.empty() ? html_title : (String)Globals::get_singleton()->get("application/name"));
current_line = current_line.replace("$GODOT_HEAD_INCLUDE", html_head_include);
current_line = current_line.replace("$GODOT_STYLE_FONT_FAMILY", html_font_family);
current_line = current_line.replace("$GODOT_STYLE_INCLUDE", html_style_include);
current_line = current_line.replace("$GODOT_CONTROLS_ENABLED", html_controls_enabled ? "true" : "false");
current_line = current_line.replace("$GODOT_DEBUG_ENABLED", p_debug ? "true" : "false");
strnew += current_line + "\n";
}
CharString cs = strnew.utf8();
p_html.resize(cs.length());
for(int i=9;i<cs.length();i++) {
p_html[i]=cs[i];
for (int i = 9; i < cs.length(); i++) {
p_html[i] = cs[i];
}
}
static void _fix_files(Vector<uint8_t>& html,uint64_t p_data_size) {
static void _fix_files(Vector<uint8_t> &html, uint64_t p_data_size) {
String str;
String strnew;
str.parse_utf8((const char*)html.ptr(),html.size());
Vector<String> lines=str.split("\n");
for(int i=0;i<lines.size();i++) {
if (lines[i].find("$DPLEN")!=-1) {
strnew+=lines[i].replace("$DPLEN",itos(p_data_size));
str.parse_utf8((const char *)html.ptr(), html.size());
Vector<String> lines = str.split("\n");
for (int i = 0; i < lines.size(); i++) {
if (lines[i].find("$DPLEN") != -1) {
strnew += lines[i].replace("$DPLEN", itos(p_data_size));
} else {
strnew+=lines[i]+"\n";
strnew += lines[i] + "\n";
}
}
CharString cs = strnew.utf8();
html.resize(cs.length());
for(int i=9;i<cs.length();i++) {
html[i]=cs[i];
for (int i = 9; i < cs.length(); i++) {
html[i] = cs[i];
}
}
struct JSExportData {
EditorProgress *ep;
FileAccess *f;
};
Error EditorExportPlatformJavaScript::export_project(const String& p_path, bool p_debug, int p_flags) {
Error EditorExportPlatformJavaScript::export_project(const String &p_path, bool p_debug, int p_flags) {
String src_template;
EditorProgress ep("export","Exporting for javascript",104);
EditorProgress ep("export", "Exporting for javascript", 104);
if (p_debug)
src_template=custom_debug_package;
src_template = custom_debug_package;
else
src_template=custom_release_package;
src_template = custom_release_package;
if (src_template=="") {
if (src_template == "") {
String err;
if (p_debug) {
src_template=find_export_template("javascript_debug.zip", &err);
src_template = find_export_template("javascript_debug.zip", &err);
} else {
src_template=find_export_template("javascript_release.zip", &err);
src_template = find_export_template("javascript_release.zip", &err);
}
if (src_template=="") {
if (src_template == "") {
EditorNode::add_io_error(err);
return ERR_FILE_NOT_FOUND;
}
}
FileAccess *src_f=NULL;
FileAccess *src_f = NULL;
zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
ep.step("Exporting to HTML5",0);
ep.step("Exporting to HTML5", 0);
ep.step("Finding Files..",1);
ep.step("Finding Files..", 1);
FileAccess *f=FileAccess::open(p_path.get_base_dir()+"/data.pck",FileAccess::WRITE);
FileAccess *f = FileAccess::open(p_path.get_base_dir() + "/data.pck", FileAccess::WRITE);
if (!f) {
EditorNode::add_io_error("Could not create file for writing:\n"+p_path.basename()+"_files.js");
EditorNode::add_io_error("Could not create file for writing:\n" + p_path.basename() + "_files.js");
return ERR_FILE_CANT_WRITE;
}
Error err = save_pack(f);
@@ -277,89 +264,82 @@ Error EditorExportPlatformJavaScript::export_project(const String& p_path, bool
if (err)
return err;
unzFile pkg = unzOpen2(src_template.utf8().get_data(), &io);
if (!pkg) {
EditorNode::add_io_error("Could not find template HTML5 to export:\n"+src_template);
EditorNode::add_io_error("Could not find template HTML5 to export:\n" + src_template);
return ERR_FILE_NOT_FOUND;
}
ERR_FAIL_COND_V(!pkg, ERR_CANT_OPEN);
int ret = unzGoToFirstFile(pkg);
while(ret==UNZ_OK) {
while (ret == UNZ_OK) {
//get filename
unz_file_info info;
char fname[16384];
ret = unzGetCurrentFileInfo(pkg,&info,fname,16384,NULL,0,NULL,0);
ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
String file=fname;
String file = fname;
Vector<uint8_t> data;
data.resize(info.uncompressed_size);
//read
unzOpenCurrentFile(pkg);
unzReadCurrentFile(pkg,data.ptr(),data.size());
unzReadCurrentFile(pkg, data.ptr(), data.size());
unzCloseCurrentFile(pkg);
//write
if (file=="godot.html") {
if (file == "godot.html") {
_fix_html(data,p_path.get_file().basename(), p_debug);
file=p_path.get_file();
_fix_html(data, p_path.get_file().basename(), p_debug);
file = p_path.get_file();
}
if (file=="godotfs.js") {
if (file == "godotfs.js") {
_fix_files(data,len);
file=p_path.get_file().basename()+"fs.js";
_fix_files(data, len);
file = p_path.get_file().basename() + "fs.js";
}
if (file=="godot.js") {
if (file == "godot.js") {
//_fix_godot(data);
file=p_path.get_file().basename()+".js";
file = p_path.get_file().basename() + ".js";
}
if (file=="godot.asm.js") {
if (file == "godot.asm.js") {
file=p_path.get_file().basename()+".asm.js";
file = p_path.get_file().basename() + ".asm.js";
}
if (file=="godot.mem") {
if (file == "godot.mem") {
//_fix_godot(data);
file=p_path.get_file().basename()+".mem";
file = p_path.get_file().basename() + ".mem";
}
String dst = p_path.get_base_dir().plus_file(file);
FileAccess *f=FileAccess::open(dst,FileAccess::WRITE);
FileAccess *f = FileAccess::open(dst, FileAccess::WRITE);
if (!f) {
EditorNode::add_io_error("Could not create file for writing:\n"+dst);
EditorNode::add_io_error("Could not create file for writing:\n" + dst);
unzClose(pkg);
return ERR_FILE_CANT_WRITE;
}
f->store_buffer(data.ptr(),data.size());
f->store_buffer(data.ptr(), data.size());
memdelete(f);
ret = unzGoToNextFile(pkg);
}
return OK;
}
Error EditorExportPlatformJavaScript::run(int p_device, int p_flags) {
String path = EditorSettings::get_singleton()->get_settings_path()+"/tmp/tmp_export.html";
Error err = export_project(path,true,p_flags);
String path = EditorSettings::get_singleton()->get_settings_path() + "/tmp/tmp_export.html";
Error err = export_project(path, true, p_flags);
if (err)
return err;
@@ -368,59 +348,50 @@ Error EditorExportPlatformJavaScript::run(int p_device, int p_flags) {
return OK;
}
EditorExportPlatformJavaScript::EditorExportPlatformJavaScript() {
show_run=false;
Image img( _javascript_logo );
logo = Ref<ImageTexture>( memnew( ImageTexture ));
show_run = false;
Image img(_javascript_logo);
logo = Ref<ImageTexture>(memnew(ImageTexture));
logo->create_from_image(img);
max_memory=3;
html_title="";
html_font_family="arial,sans-serif";
html_controls_enabled=true;
pack_mode=PACK_SINGLE_FILE;
max_memory = 3;
html_title = "";
html_font_family = "arial,sans-serif";
html_controls_enabled = true;
pack_mode = PACK_SINGLE_FILE;
}
bool EditorExportPlatformJavaScript::can_export(String *r_error) const {
bool valid=true;
bool valid = true;
String err;
if (!exists_export_template("javascript_debug.zip") || !exists_export_template("javascript_release.zip")) {
valid=false;
err+="No export templates found.\nDownload and install export templates.\n";
valid = false;
err += "No export templates found.\nDownload and install export templates.\n";
}
if (custom_debug_package!="" && !FileAccess::exists(custom_debug_package)) {
valid=false;
err+="Custom debug package not found.\n";
if (custom_debug_package != "" && !FileAccess::exists(custom_debug_package)) {
valid = false;
err += "Custom debug package not found.\n";
}
if (custom_release_package!="" && !FileAccess::exists(custom_release_package)) {
valid=false;
err+="Custom release package not found.\n";
if (custom_release_package != "" && !FileAccess::exists(custom_release_package)) {
valid = false;
err += "Custom release package not found.\n";
}
if (r_error)
*r_error=err;
*r_error = err;
return valid;
}
EditorExportPlatformJavaScript::~EditorExportPlatformJavaScript() {
}
void register_javascript_exporter() {
Ref<EditorExportPlatformJavaScript> exporter = Ref<EditorExportPlatformJavaScript>( memnew(EditorExportPlatformJavaScript) );
Ref<EditorExportPlatformJavaScript> exporter = Ref<EditorExportPlatformJavaScript>(memnew(EditorExportPlatformJavaScript));
EditorImportExport::get_singleton()->add_export_platform(exporter);
}

View File

@@ -31,16 +31,20 @@
#include "javascript_eval.h"
#include "emscripten.h"
JavaScript *JavaScript::singleton=NULL;
JavaScript *JavaScript::singleton = NULL;
JavaScript *JavaScript::get_singleton() {
return singleton;
}
Variant JavaScript::eval(const String& p_code, bool p_use_global_exec_context) {
Variant JavaScript::eval(const String &p_code, bool p_use_global_exec_context) {
union { int i; double d; char* s; } js_data[4];
union {
int i;
double d;
char *s;
} js_data[4];
/* clang-format off */
Variant::Type return_type = static_cast<Variant::Type>(EM_ASM_INT({
@@ -127,21 +131,20 @@ Variant JavaScript::eval(const String& p_code, bool p_use_global_exec_context) {
}, js_data, sizeof *js_data, p_code.utf8().get_data(), p_use_global_exec_context));
/* clang-format on */
switch(return_type) {
switch (return_type) {
case Variant::BOOL:
return !!js_data->i;
case Variant::INT:
return js_data->i;
case Variant::REAL:
return js_data->d;
case Variant::STRING:
{
String str = String::utf8(js_data->s);
/* clang-format off */
case Variant::STRING: {
String str = String::utf8(js_data->s);
/* clang-format off */
EM_ASM_({ _free($0); }, js_data->s);
/* clang-format on */
return str;
}
/* clang-format on */
return str;
}
case Variant::VECTOR2:
return Vector2(js_data[0].d, js_data[1].d);
case Variant::VECTOR3:
@@ -149,7 +152,7 @@ Variant JavaScript::eval(const String& p_code, bool p_use_global_exec_context) {
case Variant::RECT2:
return Rect2(js_data[0].d, js_data[1].d, js_data[2].d, js_data[3].d);
case Variant::COLOR:
return Color(js_data[0].d/255., js_data[1].d/255., js_data[2].d/255., js_data[3].d);
return Color(js_data[0].d / 255., js_data[1].d / 255., js_data[2].d / 255., js_data[3].d);
}
return Variant();
}
@@ -166,7 +169,6 @@ JavaScript::JavaScript() {
}
JavaScript::~JavaScript() {
}
#endif // JAVASCRIPT_EVAL_ENABLED

View File

@@ -26,92 +26,88 @@
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include <GL/glut.h>
#include "os_javascript.h"
#include "main/main.h"
#include "io/resource_loader.h"
#include "emscripten.h"
#include "io/resource_loader.h"
#include "main/main.h"
#include "os_javascript.h"
#include <GL/glut.h>
#include <string.h>
OS_JavaScript *os=NULL;
OS_JavaScript *os = NULL;
static void _gfx_init(void *ud,bool gl2,int w, int h,bool fs) {
static void _gfx_init(void *ud, bool gl2, int w, int h, bool fs) {
glutInitWindowSize(w, h);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("godot");
}
static uint32_t _mouse_button_mask=0;
static uint32_t _mouse_button_mask = 0;
static void _glut_mouse_button(int button, int state, int x, int y) {
InputEvent ev;
ev.type=InputEvent::MOUSE_BUTTON;
switch(button) {
case GLUT_LEFT_BUTTON: ev.mouse_button.button_index=BUTTON_LEFT; break;
case GLUT_MIDDLE_BUTTON: ev.mouse_button.button_index=BUTTON_MIDDLE; break;
case GLUT_RIGHT_BUTTON: ev.mouse_button.button_index=BUTTON_RIGHT; break;
case 3: ev.mouse_button.button_index=BUTTON_WHEEL_UP; break;
case 4: ev.mouse_button.button_index=BUTTON_WHEEL_DOWN; break;
ev.type = InputEvent::MOUSE_BUTTON;
switch (button) {
case GLUT_LEFT_BUTTON: ev.mouse_button.button_index = BUTTON_LEFT; break;
case GLUT_MIDDLE_BUTTON: ev.mouse_button.button_index = BUTTON_MIDDLE; break;
case GLUT_RIGHT_BUTTON: ev.mouse_button.button_index = BUTTON_RIGHT; break;
case 3: ev.mouse_button.button_index = BUTTON_WHEEL_UP; break;
case 4: ev.mouse_button.button_index = BUTTON_WHEEL_DOWN; break;
}
ev.mouse_button.pressed = state == GLUT_DOWN;
ev.mouse_button.x = x;
ev.mouse_button.y = y;
ev.mouse_button.global_x = x;
ev.mouse_button.global_y = y;
ev.mouse_button.pressed=state==GLUT_DOWN;
ev.mouse_button.x=x;
ev.mouse_button.y=y;
ev.mouse_button.global_x=x;
ev.mouse_button.global_y=y;
if (ev.mouse_button.button_index<4) {
if (ev.mouse_button.button_index < 4) {
if (ev.mouse_button.pressed) {
_mouse_button_mask |= 1 << (ev.mouse_button.button_index-1);
_mouse_button_mask |= 1 << (ev.mouse_button.button_index - 1);
} else {
_mouse_button_mask &= ~(1 << (ev.mouse_button.button_index-1));
_mouse_button_mask &= ~(1 << (ev.mouse_button.button_index - 1));
}
}
ev.mouse_button.button_mask=_mouse_button_mask;
ev.mouse_button.button_mask = _mouse_button_mask;
uint32_t m = glutGetModifiers();
ev.mouse_button.mod.alt=(m&GLUT_ACTIVE_ALT)!=0;
ev.mouse_button.mod.shift=(m&GLUT_ACTIVE_SHIFT)!=0;
ev.mouse_button.mod.control=(m&GLUT_ACTIVE_CTRL)!=0;
ev.mouse_button.mod.alt = (m & GLUT_ACTIVE_ALT) != 0;
ev.mouse_button.mod.shift = (m & GLUT_ACTIVE_SHIFT) != 0;
ev.mouse_button.mod.control = (m & GLUT_ACTIVE_CTRL) != 0;
os->push_input(ev);
if (ev.mouse_button.button_index==BUTTON_WHEEL_UP || ev.mouse_button.button_index==BUTTON_WHEEL_DOWN) {
if (ev.mouse_button.button_index == BUTTON_WHEEL_UP || ev.mouse_button.button_index == BUTTON_WHEEL_DOWN) {
// GLUT doesn't send release events for mouse wheel, so send manually
ev.mouse_button.pressed=false;
ev.mouse_button.pressed = false;
os->push_input(ev);
}
}
static int _glut_prev_x=0;
static int _glut_prev_y=0;
static int _glut_prev_x = 0;
static int _glut_prev_y = 0;
static void _glut_mouse_motion(int x, int y) {
InputEvent ev;
ev.type=InputEvent::MOUSE_MOTION;
ev.mouse_motion.button_mask=_mouse_button_mask;
ev.mouse_motion.x=x;
ev.mouse_motion.y=y;
ev.mouse_motion.global_x=x;
ev.mouse_motion.global_y=y;
ev.mouse_motion.relative_x=x-_glut_prev_x;
ev.mouse_motion.relative_y=y-_glut_prev_y;
_glut_prev_x=x;
_glut_prev_y=y;
ev.type = InputEvent::MOUSE_MOTION;
ev.mouse_motion.button_mask = _mouse_button_mask;
ev.mouse_motion.x = x;
ev.mouse_motion.y = y;
ev.mouse_motion.global_x = x;
ev.mouse_motion.global_y = y;
ev.mouse_motion.relative_x = x - _glut_prev_x;
ev.mouse_motion.relative_y = y - _glut_prev_y;
_glut_prev_x = x;
_glut_prev_y = y;
uint32_t m = glutGetModifiers();
ev.mouse_motion.mod.alt=(m&GLUT_ACTIVE_ALT)!=0;
ev.mouse_motion.mod.shift=(m&GLUT_ACTIVE_SHIFT)!=0;
ev.mouse_motion.mod.control=(m&GLUT_ACTIVE_CTRL)!=0;
ev.mouse_motion.mod.alt = (m & GLUT_ACTIVE_ALT) != 0;
ev.mouse_motion.mod.shift = (m & GLUT_ACTIVE_SHIFT) != 0;
ev.mouse_motion.mod.control = (m & GLUT_ACTIVE_CTRL) != 0;
os->push_input(ev);
}
static void _gfx_idle() {
@@ -119,49 +115,45 @@ static void _gfx_idle() {
glutPostRedisplay();
}
int start_step=0;
int start_step = 0;
static void _godot_draw(void) {
if (start_step==1) {
start_step=2;
if (start_step == 1) {
start_step = 2;
Main::start();
os->main_loop_begin();
os->main_loop_begin();
}
if (start_step==2) {
if (start_step == 2) {
os->main_loop_iterate();
}
glutSwapBuffers();
}
extern "C" {
void main_after_fs_sync(int value) {
start_step=1;
start_step = 1;
printf("FS SYNCHED!\n");
}
}
int main(int argc, char *argv[]) {
/* Initialize the window */
printf("let it go!\n");
glutInit(&argc, argv);
os = new OS_JavaScript(_gfx_init,NULL,NULL);
os = new OS_JavaScript(_gfx_init, NULL, NULL);
#if 0
char *args[]={"-test","gui","-v",NULL};
Error err = Main::setup("apk",3,args);
#else
// char *args[]={"-v",NULL};//
// Error err = Main::setup("",1,args);
Error err = Main::setup("",0,NULL);
// char *args[]={"-v",NULL};//
// Error err = Main::setup("",1,args);
Error err = Main::setup("", 0, NULL);
#endif
ResourceLoader::set_abort_on_missing_resources(false); //ease up compatibility
@@ -170,17 +162,13 @@ int main(int argc, char *argv[]) {
glutMotionFunc(_glut_mouse_motion);
glutPassiveMotionFunc(_glut_mouse_motion);
/* Set up glut callback functions */
glutIdleFunc (_gfx_idle);
// glutReshapeFunc(gears_reshape);
/* Set up glut callback functions */
glutIdleFunc(_gfx_idle);
// glutReshapeFunc(gears_reshape);
glutDisplayFunc(_godot_draw);
//glutSpecialFunc(gears_special);
//glutSpecialFunc(gears_special);
//mount persistent filesystem
//mount persistent filesystem
/* clang-format off */
EM_ASM(
FS.mkdir('/userfs');
@@ -203,12 +191,9 @@ int main(int argc, char *argv[]) {
glutMainLoop();
return 0;
}
/*
*
*09] <azakai|2__> reduz: yes, define TOTAL_MEMORY on Module. for example var Module = { TOTAL_MEMORY: 12345.. }; before the main

View File

@@ -27,25 +27,25 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "os_javascript.h"
#include "drivers/gles2/rasterizer_gles2.h"
#include "core/io/file_access_buffered_fa.h"
#include "drivers/unix/file_access_unix.h"
#include "drivers/gles2/rasterizer_gles2.h"
#include "drivers/unix/dir_access_unix.h"
#include "drivers/unix/file_access_unix.h"
#include "servers/visual/visual_server_raster.h"
#include "main/main.h"
#include "core/globals.h"
#include "stdlib.h"
#include "emscripten.h"
#include "dom_keys.h"
#include "emscripten.h"
#include "stdlib.h"
int OS_JavaScript::get_video_driver_count() const {
return 1;
}
const char * OS_JavaScript::get_video_driver_name(int p_driver) const {
const char *OS_JavaScript::get_video_driver_name(int p_driver) const {
return "GLES2";
}
@@ -60,7 +60,7 @@ int OS_JavaScript::get_audio_driver_count() const {
return 1;
}
const char * OS_JavaScript::get_audio_driver_name(int p_driver) const {
const char *OS_JavaScript::get_audio_driver_name(int p_driver) const {
return "JavaScript";
}
@@ -69,20 +69,19 @@ void OS_JavaScript::initialize_core() {
OS_Unix::initialize_core();
FileAccess::make_default<FileAccessBufferedFA<FileAccessUnix> >(FileAccess::ACCESS_RESOURCES);
}
void OS_JavaScript::set_opengl_extensions(const char* p_gl_extensions) {
void OS_JavaScript::set_opengl_extensions(const char *p_gl_extensions) {
ERR_FAIL_COND(!p_gl_extensions);
gl_extensions=p_gl_extensions;
gl_extensions = p_gl_extensions;
}
static EM_BOOL _browser_resize_callback(int event_type, const EmscriptenUiEvent *ui_event, void *user_data) {
ERR_FAIL_COND_V(event_type!=EMSCRIPTEN_EVENT_RESIZE, false);
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_RESIZE, false);
OS_JavaScript* os = static_cast<OS_JavaScript*>(user_data);
OS_JavaScript *os = static_cast<OS_JavaScript *>(user_data);
// the order in which _browser_resize_callback and
// _fullscreen_change_callback are called is browser-dependent,
@@ -102,13 +101,13 @@ static Size2 _windowed_size;
static EM_BOOL _fullscreen_change_callback(int event_type, const EmscriptenFullscreenChangeEvent *event, void *user_data) {
ERR_FAIL_COND_V(event_type!=EMSCRIPTEN_EVENT_FULLSCREENCHANGE, false);
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_FULLSCREENCHANGE, false);
OS_JavaScript* os = static_cast<OS_JavaScript*>(user_data);
OS_JavaScript *os = static_cast<OS_JavaScript *>(user_data);
String id = String::utf8(event->id);
// empty id is canvas
if (id.empty() || id=="canvas") {
if (id.empty() || id == "canvas") {
OS::VideoMode vm = os->get_video_mode();
// this event property is the only reliable information on
@@ -120,8 +119,7 @@ static EM_BOOL _fullscreen_change_callback(int event_type, const EmscriptenFulls
vm.height = event->screenHeight;
os->set_video_mode(vm);
emscripten_set_canvas_size(vm.width, vm.height);
}
else {
} else {
os->set_video_mode(vm);
if (!os->is_window_maximized()) {
os->set_window_size(_windowed_size);
@@ -144,12 +142,12 @@ static InputEvent _setup_key_event(const EmscriptenKeyboardEvent *emscripten_eve
String unicode = String::utf8(emscripten_event->key);
// check if empty or multi-character (e.g. `CapsLock`)
if (unicode.length()!=1) {
if (unicode.length() != 1) {
// might be empty as well, but better than nonsense
unicode = String::utf8(emscripten_event->charValue);
}
if (unicode.length()==1) {
ev.key.unicode=unicode[0];
if (unicode.length() == 1) {
ev.key.unicode = unicode[0];
}
return ev;
@@ -159,59 +157,58 @@ static InputEvent deferred_key_event;
static EM_BOOL _keydown_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) {
ERR_FAIL_COND_V(event_type!=EMSCRIPTEN_EVENT_KEYDOWN, false);
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_KEYDOWN, false);
InputEvent ev = _setup_key_event(key_event);
ev.key.pressed = true;
if (ev.key.unicode==0 && keycode_has_unicode(ev.key.scancode)) {
if (ev.key.unicode == 0 && keycode_has_unicode(ev.key.scancode)) {
// defer to keypress event for legacy unicode retrieval
deferred_key_event = ev;
return false; // do not suppress keypress event
}
static_cast<OS_JavaScript*>(user_data)->push_input(ev);
static_cast<OS_JavaScript *>(user_data)->push_input(ev);
return true;
}
static EM_BOOL _keypress_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) {
ERR_FAIL_COND_V(event_type!=EMSCRIPTEN_EVENT_KEYPRESS, false);
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_KEYPRESS, false);
deferred_key_event.key.unicode = key_event->charCode;
static_cast<OS_JavaScript*>(user_data)->push_input(deferred_key_event);
static_cast<OS_JavaScript *>(user_data)->push_input(deferred_key_event);
return true;
}
static EM_BOOL _keyup_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) {
ERR_FAIL_COND_V(event_type!=EMSCRIPTEN_EVENT_KEYUP, false);
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_KEYUP, false);
InputEvent ev = _setup_key_event(key_event);
ev.key.pressed = false;
static_cast<OS_JavaScript*>(user_data)->push_input(ev);
return ev.key.scancode!=KEY_UNKNOWN && ev.key.scancode!=0;
static_cast<OS_JavaScript *>(user_data)->push_input(ev);
return ev.key.scancode != KEY_UNKNOWN && ev.key.scancode != 0;
}
static EM_BOOL joy_callback_func(int p_type, const EmscriptenGamepadEvent *p_event, void *p_user) {
OS_JavaScript *os = (OS_JavaScript*) OS::get_singleton();
OS_JavaScript *os = (OS_JavaScript *)OS::get_singleton();
if (os) {
return os->joy_connection_changed(p_type, p_event);
}
return false;
}
void OS_JavaScript::initialize(const VideoMode& p_desired,int p_video_driver,int p_audio_driver) {
void OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {
print_line("Init OS");
if (gfx_init_func)
gfx_init_func(gfx_init_ud,use_gl2,p_desired.width,p_desired.height,p_desired.fullscreen);
gfx_init_func(gfx_init_ud, use_gl2, p_desired.width, p_desired.height, p_desired.fullscreen);
// nothing to do here, can't fulfil fullscreen request due to
// browser security, window size is already set from HTML
video_mode=p_desired;
video_mode.fullscreen=false;
_windowed_size=get_window_size();
video_mode = p_desired;
video_mode.fullscreen = false;
_windowed_size = get_window_size();
// find locale, emscripten only sets "C"
char locale_ptr[16];
@@ -239,18 +236,18 @@ void OS_JavaScript::initialize(const VideoMode& p_desired,int p_video_driver,int
AudioDriverManagerSW::add_driver(&audio_driver_javascript);
if (true) {
RasterizerGLES2 *rasterizer_gles22=memnew( RasterizerGLES2(false,false,false,false) );
RasterizerGLES2 *rasterizer_gles22 = memnew(RasterizerGLES2(false, false, false, false));
rasterizer_gles22->set_use_framebuffers(false); //not supported by emscripten
if (gl_extensions)
rasterizer_gles22->set_extensions(gl_extensions);
rasterizer = rasterizer_gles22;
} else {
// rasterizer = memnew( RasterizerGLES1(true, false) );
// rasterizer = memnew( RasterizerGLES1(true, false) );
}
print_line("Init VS");
visual_server = memnew( VisualServerRaster(rasterizer) );
visual_server = memnew(VisualServerRaster(rasterizer));
visual_server->init();
visual_server->cursor_set_visible(false, 0);
@@ -264,7 +261,7 @@ void OS_JavaScript::initialize(const VideoMode& p_desired,int p_video_driver,int
print_line("Init SM");
//sample_manager = memnew( SampleManagerMallocSW );
audio_server = memnew( AudioServerJavascript );
audio_server = memnew(AudioServerJavascript);
print_line("Init Mixer");
@@ -273,28 +270,33 @@ void OS_JavaScript::initialize(const VideoMode& p_desired,int p_video_driver,int
print_line("Init SoundServer");
spatial_sound_server = memnew( SpatialSoundServerSW );
spatial_sound_server = memnew(SpatialSoundServerSW);
spatial_sound_server->init();
print_line("Init SpatialSoundServer");
spatial_sound_2d_server = memnew( SpatialSound2DServerSW );
spatial_sound_2d_server = memnew(SpatialSound2DServerSW);
spatial_sound_2d_server->init();
//
print_line("Init Physicsserver");
physics_server = memnew( PhysicsServerSW );
physics_server = memnew(PhysicsServerSW);
physics_server->init();
physics_2d_server = memnew( Physics2DServerSW );
physics_2d_server = memnew(Physics2DServerSW);
physics_2d_server->init();
input = memnew( InputDefault );
input = memnew(InputDefault);
#define EM_CHECK(ev) if (result!=EMSCRIPTEN_RESULT_SUCCESS)\
#define EM_CHECK(ev) \
if (result != EMSCRIPTEN_RESULT_SUCCESS) \
ERR_PRINTS("Error while setting " #ev " callback: Code " + itos(result))
#define SET_EM_CALLBACK(ev, cb) result = emscripten_set_##ev##_callback(NULL, this, true, &cb); EM_CHECK(ev)
#define SET_EM_CALLBACK_NODATA(ev, cb) result = emscripten_set_##ev##_callback(NULL, true, &cb); EM_CHECK(ev)
#define SET_EM_CALLBACK(ev, cb) \
result = emscripten_set_##ev##_callback(NULL, this, true, &cb); \
EM_CHECK(ev)
#define SET_EM_CALLBACK_NODATA(ev, cb) \
result = emscripten_set_##ev##_callback(NULL, true, &cb); \
EM_CHECK(ev)
EMSCRIPTEN_RESULT result;
SET_EM_CALLBACK(keydown, _keydown_callback)
@@ -310,16 +312,15 @@ void OS_JavaScript::initialize(const VideoMode& p_desired,int p_video_driver,int
#undef EM_CHECK
}
void OS_JavaScript::set_main_loop( MainLoop * p_main_loop ) {
void OS_JavaScript::set_main_loop(MainLoop *p_main_loop) {
main_loop=p_main_loop;
main_loop = p_main_loop;
input->set_main_loop(p_main_loop);
}
void OS_JavaScript::delete_main_loop() {
memdelete( main_loop );
memdelete(main_loop);
}
void OS_JavaScript::finalize() {
@@ -327,7 +328,7 @@ void OS_JavaScript::finalize() {
memdelete(input);
}
void OS_JavaScript::alert(const String& p_alert,const String& p_title) {
void OS_JavaScript::alert(const String &p_alert, const String &p_title) {
/* clang-format off */
EM_ASM_({
@@ -336,7 +337,6 @@ void OS_JavaScript::alert(const String& p_alert,const String& p_title) {
/* clang-format on */
}
void OS_JavaScript::set_mouse_show(bool p_show) {
//javascript has no mouse...
@@ -363,7 +363,7 @@ int OS_JavaScript::get_mouse_button_state() const {
return last_button_mask;
}
void OS_JavaScript::set_window_title(const String& p_title) {
void OS_JavaScript::set_window_title(const String &p_title) {
/* clang-format off */
EM_ASM_({
@@ -376,7 +376,7 @@ void OS_JavaScript::set_window_title(const String& p_title) {
//void set_clipboard(const String& p_text);
//String get_clipboard() const;
void OS_JavaScript::set_video_mode(const VideoMode& p_video_mode,int p_screen) {
void OS_JavaScript::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
video_mode = p_video_mode;
}
@@ -388,11 +388,11 @@ OS::VideoMode OS_JavaScript::get_video_mode(int p_screen) const {
Size2 OS_JavaScript::get_screen_size(int p_screen) const {
ERR_FAIL_COND_V(p_screen!=0, Size2());
ERR_FAIL_COND_V(p_screen != 0, Size2());
EmscriptenFullscreenChangeEvent ev;
EMSCRIPTEN_RESULT result = emscripten_get_fullscreen_status(&ev);
ERR_FAIL_COND_V(result!=EMSCRIPTEN_RESULT_SUCCESS, Size2());
ERR_FAIL_COND_V(result != EMSCRIPTEN_RESULT_SUCCESS, Size2());
return Size2(ev.screenWidth, ev.screenHeight);
}
@@ -411,7 +411,7 @@ void OS_JavaScript::set_window_size(const Size2 p_size) {
Size2 OS_JavaScript::get_window_size() const {
int canvas[3];
emscripten_get_canvas_size(canvas, canvas+1, canvas+2);
emscripten_get_canvas_size(canvas, canvas + 1, canvas + 2);
return Size2(canvas[0], canvas[1]);
}
@@ -423,23 +423,21 @@ void OS_JavaScript::set_window_maximized(bool p_enabled) {
if (is_window_fullscreen()) {
// _browser_resize callback will set canvas size
set_window_fullscreen(false);
}
else {
} else {
/* clang-format off */
video_mode.width = EM_ASM_INT_V(return window.innerWidth);
video_mode.height = EM_ASM_INT_V(return window.innerHeight);
/* clang-format on */
emscripten_set_canvas_size(video_mode.width, video_mode.height);
}
}
else {
} else {
set_window_size(_windowed_size);
}
}
void OS_JavaScript::set_window_fullscreen(bool p_enable) {
if (p_enable==is_window_fullscreen()) {
if (p_enable == is_window_fullscreen()) {
return;
}
@@ -450,10 +448,9 @@ void OS_JavaScript::set_window_fullscreen(bool p_enable) {
/* clang-format off */
EM_ASM(Module.requestFullscreen(false, false););
/* clang-format on */
}
else {
} else {
result = emscripten_exit_fullscreen();
if (result!=EMSCRIPTEN_RESULT_SUCCESS) {
if (result != EMSCRIPTEN_RESULT_SUCCESS) {
ERR_PRINTS("Failed to exit fullscreen: Code " + itos(result));
}
}
@@ -464,7 +461,7 @@ bool OS_JavaScript::is_window_fullscreen() const {
return video_mode.fullscreen;
}
void OS_JavaScript::get_fullscreen_mode_list(List<VideoMode> *p_list,int p_screen) const {
void OS_JavaScript::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
Size2 screen = get_screen_size();
p_list->push_back(OS::VideoMode(screen.width, screen.height, true));
@@ -500,16 +497,16 @@ bool OS_JavaScript::main_loop_iterate() {
if (!main_loop)
return false;
if (time_to_save_sync>=0) {
if (time_to_save_sync >= 0) {
int64_t newtime = get_ticks_msec();
int64_t elapsed = newtime - last_sync_time;
last_sync_time=newtime;
last_sync_time = newtime;
time_to_save_sync-=elapsed;
time_to_save_sync -= elapsed;
print_line("elapsed "+itos(elapsed)+" tts "+itos(time_to_save_sync));
print_line("elapsed " + itos(elapsed) + " tts " + itos(time_to_save_sync));
if (time_to_save_sync<0) {
if (time_to_save_sync < 0) {
//time to sync, for real
/* clang-format off */
EM_ASM(
@@ -521,8 +518,6 @@ bool OS_JavaScript::main_loop_iterate() {
);
/* clang-format on */
}
}
process_joysticks();
return Main::iteration();
@@ -532,7 +527,6 @@ void OS_JavaScript::main_loop_end() {
if (main_loop)
main_loop->finish();
}
void OS_JavaScript::main_loop_focusout() {
@@ -540,186 +534,173 @@ void OS_JavaScript::main_loop_focusout() {
if (main_loop)
main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT);
//audio_driver_javascript.set_pause(true);
}
void OS_JavaScript::main_loop_focusin(){
void OS_JavaScript::main_loop_focusin() {
if (main_loop)
main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
//audio_driver_javascript.set_pause(false);
}
void OS_JavaScript::push_input(const InputEvent& p_ev) {
void OS_JavaScript::push_input(const InputEvent &p_ev) {
InputEvent ev = p_ev;
ev.ID=last_id++;
if (ev.type==InputEvent::MOUSE_MOTION) {
ev.ID = last_id++;
if (ev.type == InputEvent::MOUSE_MOTION) {
input->set_mouse_pos(Point2(ev.mouse_motion.x, ev.mouse_motion.y));
}
else if (ev.type==InputEvent::MOUSE_BUTTON) {
} else if (ev.type == InputEvent::MOUSE_BUTTON) {
last_button_mask = ev.mouse_button.button_mask;
}
input->parse_input_event(p_ev);
}
void OS_JavaScript::process_touch(int p_what,int p_pointer, const Vector<TouchPos>& p_points) {
void OS_JavaScript::process_touch(int p_what, int p_pointer, const Vector<TouchPos> &p_points) {
// print_line("ev: "+itos(p_what)+" pnt: "+itos(p_pointer)+" pointc: "+itos(p_points.size()));
// print_line("ev: "+itos(p_what)+" pnt: "+itos(p_pointer)+" pointc: "+itos(p_points.size()));
switch(p_what) {
switch (p_what) {
case 0: { //gesture begin
if (touch.size()) {
//end all if exist
InputEvent ev;
ev.type=InputEvent::MOUSE_BUTTON;
ev.ID=last_id++;
ev.mouse_button.button_index=BUTTON_LEFT;
ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
ev.mouse_button.pressed=false;
ev.mouse_button.x=touch[0].pos.x;
ev.mouse_button.y=touch[0].pos.y;
ev.mouse_button.global_x=touch[0].pos.x;
ev.mouse_button.global_y=touch[0].pos.y;
ev.type = InputEvent::MOUSE_BUTTON;
ev.ID = last_id++;
ev.mouse_button.button_index = BUTTON_LEFT;
ev.mouse_button.button_mask = BUTTON_MASK_LEFT;
ev.mouse_button.pressed = false;
ev.mouse_button.x = touch[0].pos.x;
ev.mouse_button.y = touch[0].pos.y;
ev.mouse_button.global_x = touch[0].pos.x;
ev.mouse_button.global_y = touch[0].pos.y;
input->parse_input_event(ev);
for(int i=0;i<touch.size();i++) {
for (int i = 0; i < touch.size(); i++) {
InputEvent ev;
ev.type=InputEvent::SCREEN_TOUCH;
ev.ID=last_id++;
ev.screen_touch.index=touch[i].id;
ev.screen_touch.pressed=false;
ev.screen_touch.x=touch[i].pos.x;
ev.screen_touch.y=touch[i].pos.y;
ev.type = InputEvent::SCREEN_TOUCH;
ev.ID = last_id++;
ev.screen_touch.index = touch[i].id;
ev.screen_touch.pressed = false;
ev.screen_touch.x = touch[i].pos.x;
ev.screen_touch.y = touch[i].pos.y;
input->parse_input_event(ev);
}
}
touch.resize(p_points.size());
for(int i=0;i<p_points.size();i++) {
touch[i].id=p_points[i].id;
touch[i].pos=p_points[i].pos;
for (int i = 0; i < p_points.size(); i++) {
touch[i].id = p_points[i].id;
touch[i].pos = p_points[i].pos;
}
{
//send mouse
InputEvent ev;
ev.type=InputEvent::MOUSE_BUTTON;
ev.ID=last_id++;
ev.mouse_button.button_index=BUTTON_LEFT;
ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
ev.mouse_button.pressed=true;
ev.mouse_button.x=touch[0].pos.x;
ev.mouse_button.y=touch[0].pos.y;
ev.mouse_button.global_x=touch[0].pos.x;
ev.mouse_button.global_y=touch[0].pos.y;
last_mouse=touch[0].pos;
ev.type = InputEvent::MOUSE_BUTTON;
ev.ID = last_id++;
ev.mouse_button.button_index = BUTTON_LEFT;
ev.mouse_button.button_mask = BUTTON_MASK_LEFT;
ev.mouse_button.pressed = true;
ev.mouse_button.x = touch[0].pos.x;
ev.mouse_button.y = touch[0].pos.y;
ev.mouse_button.global_x = touch[0].pos.x;
ev.mouse_button.global_y = touch[0].pos.y;
last_mouse = touch[0].pos;
input->parse_input_event(ev);
}
//send touch
for(int i=0;i<touch.size();i++) {
for (int i = 0; i < touch.size(); i++) {
InputEvent ev;
ev.type=InputEvent::SCREEN_TOUCH;
ev.ID=last_id++;
ev.screen_touch.index=touch[i].id;
ev.screen_touch.pressed=true;
ev.screen_touch.x=touch[i].pos.x;
ev.screen_touch.y=touch[i].pos.y;
ev.type = InputEvent::SCREEN_TOUCH;
ev.ID = last_id++;
ev.screen_touch.index = touch[i].id;
ev.screen_touch.pressed = true;
ev.screen_touch.x = touch[i].pos.x;
ev.screen_touch.y = touch[i].pos.y;
input->parse_input_event(ev);
}
} break;
case 1: { //motion
if (p_points.size()) {
//send mouse, should look for point 0?
InputEvent ev;
ev.type=InputEvent::MOUSE_MOTION;
ev.ID=last_id++;
ev.mouse_motion.button_mask=BUTTON_MASK_LEFT;
ev.mouse_motion.x=p_points[0].pos.x;
ev.mouse_motion.y=p_points[0].pos.y;
input->set_mouse_pos(Point2(ev.mouse_motion.x,ev.mouse_motion.y));
ev.mouse_motion.speed_x=input->get_mouse_speed().x;
ev.mouse_motion.speed_y=input->get_mouse_speed().y;
ev.mouse_motion.relative_x=p_points[0].pos.x-last_mouse.x;
ev.mouse_motion.relative_y=p_points[0].pos.y-last_mouse.y;
last_mouse=p_points[0].pos;
ev.type = InputEvent::MOUSE_MOTION;
ev.ID = last_id++;
ev.mouse_motion.button_mask = BUTTON_MASK_LEFT;
ev.mouse_motion.x = p_points[0].pos.x;
ev.mouse_motion.y = p_points[0].pos.y;
input->set_mouse_pos(Point2(ev.mouse_motion.x, ev.mouse_motion.y));
ev.mouse_motion.speed_x = input->get_mouse_speed().x;
ev.mouse_motion.speed_y = input->get_mouse_speed().y;
ev.mouse_motion.relative_x = p_points[0].pos.x - last_mouse.x;
ev.mouse_motion.relative_y = p_points[0].pos.y - last_mouse.y;
last_mouse = p_points[0].pos;
input->parse_input_event(ev);
}
ERR_FAIL_COND(touch.size()!=p_points.size());
ERR_FAIL_COND(touch.size() != p_points.size());
for(int i=0;i<touch.size();i++) {
for (int i = 0; i < touch.size(); i++) {
int idx=-1;
for(int j=0;j<p_points.size();j++) {
int idx = -1;
for (int j = 0; j < p_points.size(); j++) {
if (touch[i].id==p_points[j].id) {
idx=j;
if (touch[i].id == p_points[j].id) {
idx = j;
break;
}
}
ERR_CONTINUE(idx==-1);
ERR_CONTINUE(idx == -1);
if (touch[i].pos==p_points[idx].pos)
if (touch[i].pos == p_points[idx].pos)
continue; //no move unncesearily
InputEvent ev;
ev.type=InputEvent::SCREEN_DRAG;
ev.ID=last_id++;
ev.screen_drag.index=touch[i].id;
ev.screen_drag.x=p_points[idx].pos.x;
ev.screen_drag.y=p_points[idx].pos.y;
ev.screen_drag.relative_x=p_points[idx].pos.x - touch[i].pos.x;
ev.screen_drag.relative_y=p_points[idx].pos.y - touch[i].pos.y;
ev.type = InputEvent::SCREEN_DRAG;
ev.ID = last_id++;
ev.screen_drag.index = touch[i].id;
ev.screen_drag.x = p_points[idx].pos.x;
ev.screen_drag.y = p_points[idx].pos.y;
ev.screen_drag.relative_x = p_points[idx].pos.x - touch[i].pos.x;
ev.screen_drag.relative_y = p_points[idx].pos.y - touch[i].pos.y;
input->parse_input_event(ev);
touch[i].pos=p_points[idx].pos;
touch[i].pos = p_points[idx].pos;
}
} break;
case 2: { //release
if (touch.size()) {
//end all if exist
InputEvent ev;
ev.type=InputEvent::MOUSE_BUTTON;
ev.ID=last_id++;
ev.mouse_button.button_index=BUTTON_LEFT;
ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
ev.mouse_button.pressed=false;
ev.mouse_button.x=touch[0].pos.x;
ev.mouse_button.y=touch[0].pos.y;
ev.mouse_button.global_x=touch[0].pos.x;
ev.mouse_button.global_y=touch[0].pos.y;
ev.type = InputEvent::MOUSE_BUTTON;
ev.ID = last_id++;
ev.mouse_button.button_index = BUTTON_LEFT;
ev.mouse_button.button_mask = BUTTON_MASK_LEFT;
ev.mouse_button.pressed = false;
ev.mouse_button.x = touch[0].pos.x;
ev.mouse_button.y = touch[0].pos.y;
ev.mouse_button.global_x = touch[0].pos.x;
ev.mouse_button.global_y = touch[0].pos.y;
input->parse_input_event(ev);
for(int i=0;i<touch.size();i++) {
for (int i = 0; i < touch.size(); i++) {
InputEvent ev;
ev.type=InputEvent::SCREEN_TOUCH;
ev.ID=last_id++;
ev.screen_touch.index=touch[i].id;
ev.screen_touch.pressed=false;
ev.screen_touch.x=touch[i].pos.x;
ev.screen_touch.y=touch[i].pos.y;
ev.type = InputEvent::SCREEN_TOUCH;
ev.ID = last_id++;
ev.screen_touch.index = touch[i].id;
ev.screen_touch.pressed = false;
ev.screen_touch.x = touch[i].pos.x;
ev.screen_touch.y = touch[i].pos.y;
input->parse_input_event(ev);
}
touch.clear();
}
@@ -727,38 +708,33 @@ void OS_JavaScript::process_touch(int p_what,int p_pointer, const Vector<TouchPo
} break;
case 3: { // add tuchi
ERR_FAIL_INDEX(p_pointer, p_points.size());
ERR_FAIL_INDEX(p_pointer,p_points.size());
TouchPos tp=p_points[p_pointer];
TouchPos tp = p_points[p_pointer];
touch.push_back(tp);
InputEvent ev;
ev.type=InputEvent::SCREEN_TOUCH;
ev.ID=last_id++;
ev.screen_touch.index=tp.id;
ev.screen_touch.pressed=true;
ev.screen_touch.x=tp.pos.x;
ev.screen_touch.y=tp.pos.y;
ev.type = InputEvent::SCREEN_TOUCH;
ev.ID = last_id++;
ev.screen_touch.index = tp.id;
ev.screen_touch.pressed = true;
ev.screen_touch.x = tp.pos.x;
ev.screen_touch.y = tp.pos.y;
input->parse_input_event(ev);
} break;
case 4: {
for(int i=0;i<touch.size();i++) {
if (touch[i].id==p_pointer) {
for (int i = 0; i < touch.size(); i++) {
if (touch[i].id == p_pointer) {
InputEvent ev;
ev.type=InputEvent::SCREEN_TOUCH;
ev.ID=last_id++;
ev.screen_touch.index=touch[i].id;
ev.screen_touch.pressed=false;
ev.screen_touch.x=touch[i].pos.x;
ev.screen_touch.y=touch[i].pos.y;
ev.type = InputEvent::SCREEN_TOUCH;
ev.ID = last_id++;
ev.screen_touch.index = touch[i].id;
ev.screen_touch.pressed = false;
ev.screen_touch.x = touch[i].pos.x;
ev.screen_touch.y = touch[i].pos.y;
input->parse_input_event(ev);
touch.remove(i);
i--;
@@ -766,12 +742,10 @@ void OS_JavaScript::process_touch(int p_what,int p_pointer, const Vector<TouchPo
}
} break;
}
}
void OS_JavaScript::process_accelerometer(const Vector3& p_accelerometer) {
void OS_JavaScript::process_accelerometer(const Vector3 &p_accelerometer) {
input->set_accelerometer(p_accelerometer);
}
@@ -790,7 +764,7 @@ void OS_JavaScript::main_loop_request_quit() {
void OS_JavaScript::reload_gfx() {
if (gfx_init_func)
gfx_init_func(gfx_init_ud,use_gl2,video_mode.width,video_mode.height,video_mode.fullscreen);
gfx_init_func(gfx_init_ud, use_gl2, video_mode.width, video_mode.height, video_mode.fullscreen);
if (rasterizer)
rasterizer->reload_vram();
}
@@ -822,12 +796,12 @@ String OS_JavaScript::get_executable_path() const {
return String();
}
void OS_JavaScript::_close_notification_funcs(const String& p_file,int p_flags) {
void OS_JavaScript::_close_notification_funcs(const String &p_file, int p_flags) {
print_line("close "+p_file+" flags "+itos(p_flags));
if (p_file.begins_with("/userfs") && p_flags&FileAccess::WRITE) {
static_cast<OS_JavaScript*>(get_singleton())->last_sync_time=OS::get_singleton()->get_ticks_msec();
static_cast<OS_JavaScript*>(get_singleton())->time_to_save_sync=5000; //five seconds since last save
print_line("close " + p_file + " flags " + itos(p_flags));
if (p_file.begins_with("/userfs") && p_flags & FileAccess::WRITE) {
static_cast<OS_JavaScript *>(get_singleton())->last_sync_time = OS::get_singleton()->get_ticks_msec();
static_cast<OS_JavaScript *>(get_singleton())->time_to_save_sync = 5000; //five seconds since last save
}
}
@@ -849,8 +823,7 @@ void OS_JavaScript::process_joysticks() {
jx.min = 0;
jx.value = value;
last_id = input->joy_axis(last_id, i, j, jx);
}
else {
} else {
last_id = input->joy_button(last_id, i, j, value);
}
}
@@ -872,8 +845,7 @@ bool OS_JavaScript::joy_connection_changed(int p_type, const EmscriptenGamepadEv
if (String(p_event->mapping) == "standard")
guid = "Default HTML5 Gamepad";
input->joy_connection_changed(p_event->index, true, String(p_event->id), guid);
}
else {
} else {
input->joy_connection_changed(p_event->index, false, "");
}
return true;
@@ -887,24 +859,22 @@ String OS_JavaScript::get_joy_guid(int p_device) const {
return input->get_joy_guid_remapped(p_device);
}
OS_JavaScript::OS_JavaScript(GFXInitFunc p_gfx_init_func,void*p_gfx_init_ud, GetDataDirFunc p_get_data_dir_func) {
OS_JavaScript::OS_JavaScript(GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, GetDataDirFunc p_get_data_dir_func) {
gfx_init_func=p_gfx_init_func;
gfx_init_ud=p_gfx_init_ud;
last_button_mask=0;
main_loop=NULL;
last_id=1;
gl_extensions=NULL;
rasterizer=NULL;
window_maximized=false;
gfx_init_func = p_gfx_init_func;
gfx_init_ud = p_gfx_init_ud;
last_button_mask = 0;
main_loop = NULL;
last_id = 1;
gl_extensions = NULL;
rasterizer = NULL;
window_maximized = false;
get_data_dir_func=p_get_data_dir_func;
FileAccessUnix::close_notification_func=_close_notification_funcs;
get_data_dir_func = p_get_data_dir_func;
FileAccessUnix::close_notification_func = _close_notification_funcs;
time_to_save_sync=-1;
time_to_save_sync = -1;
}
OS_JavaScript::~OS_JavaScript() {
}

View File

@@ -29,39 +29,37 @@
#ifndef OS_JAVASCRIPT_H
#define OS_JAVASCRIPT_H
#include "os/input.h"
#include "audio_driver_javascript.h"
#include "audio_server_javascript.h"
#include "drivers/unix/os_unix.h"
#include "emscripten/html5.h"
#include "main/input_default.h"
#include "os/input.h"
#include "os/main_loop.h"
#include "servers/audio/audio_server_sw.h"
#include "servers/physics/physics_server_sw.h"
#include "servers/physics_2d/physics_2d_server_sw.h"
#include "servers/spatial_sound/spatial_sound_server_sw.h"
#include "servers/spatial_sound_2d/spatial_sound_2d_server_sw.h"
#include "servers/audio/audio_server_sw.h"
#include "servers/physics_2d/physics_2d_server_sw.h"
#include "servers/visual/rasterizer.h"
#include "audio_server_javascript.h"
#include "audio_driver_javascript.h"
#include "main/input_default.h"
#include "emscripten/html5.h"
typedef void (*GFXInitFunc)(void *ud,bool gl2,int w, int h, bool fs);
typedef void (*GFXInitFunc)(void *ud, bool gl2, int w, int h, bool fs);
typedef String (*GetDataDirFunc)();
class OS_JavaScript : public OS_Unix {
public:
struct TouchPos {
int id;
Point2 pos;
};
private:
Vector<TouchPos> touch;
Point2 last_mouse;
int last_button_mask;
unsigned int last_id;
GFXInitFunc gfx_init_func;
void*gfx_init_ud;
void *gfx_init_ud;
bool use_gl2;
@@ -77,66 +75,63 @@ private:
PhysicsServer *physics_server;
Physics2DServer *physics_2d_server;
AudioDriverJavaScript audio_driver_javascript;
const char* gl_extensions;
const char *gl_extensions;
InputDefault *input;
bool window_maximized;
VideoMode video_mode;
MainLoop * main_loop;
MainLoop *main_loop;
GetDataDirFunc get_data_dir_func;
static void _close_notification_funcs(const String& p_file,int p_flags);
static void _close_notification_funcs(const String &p_file, int p_flags);
void process_joysticks();
public:
// functions used by main to initialize/deintialize the OS
virtual int get_video_driver_count() const;
virtual const char * get_video_driver_name(int p_driver) const;
virtual const char *get_video_driver_name(int p_driver) const;
virtual VideoMode get_default_video_mode() const;
virtual int get_audio_driver_count() const;
virtual const char * get_audio_driver_name(int p_driver) const;
virtual const char *get_audio_driver_name(int p_driver) const;
virtual void initialize_core();
virtual void initialize(const VideoMode& p_desired,int p_video_driver,int p_audio_driver);
virtual void initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver);
virtual void set_main_loop( MainLoop * p_main_loop );
virtual void set_main_loop(MainLoop *p_main_loop);
virtual void delete_main_loop();
virtual void finalize();
typedef int64_t ProcessID;
//static OS* get_singleton();
virtual void print_error(const char* p_function, const char* p_file, int p_line, const char *p_code, const char* p_rationale, ErrorType p_type) {
virtual void print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) {
OS::print_error(p_function, p_file, p_line, p_code, p_rationale, p_type);
}
virtual void alert(const String& p_alert,const String& p_title="ALERT!");
virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
virtual void set_mouse_show(bool p_show);
virtual void set_mouse_grab(bool p_grab);
virtual bool is_mouse_grab_enabled() const;
virtual Point2 get_mouse_pos() const;
virtual int get_mouse_button_state() const;
virtual void set_window_title(const String& p_title);
virtual void set_window_title(const String &p_title);
//virtual void set_clipboard(const String& p_text);
//virtual String get_clipboard() const;
virtual void set_video_mode(const VideoMode& p_video_mode,int p_screen=0);
virtual VideoMode get_video_mode(int p_screen=0) const;
virtual void get_fullscreen_mode_list(List<VideoMode> *p_list,int p_screen=0) const;
virtual void set_video_mode(const VideoMode &p_video_mode, int p_screen = 0);
virtual VideoMode get_video_mode(int p_screen = 0) const;
virtual void get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen = 0) const;
virtual Size2 get_screen_size(int p_screen=0) const;
virtual Size2 get_screen_size(int p_screen = 0) const;
virtual void set_window_size(const Size2);
virtual Size2 get_window_size() const;
@@ -161,7 +156,7 @@ public:
virtual bool has_touchscreen_ui_hint() const;
void set_opengl_extensions(const char* p_gl_extensions);
void set_opengl_extensions(const char *p_gl_extensions);
void reload_gfx();
@@ -170,17 +165,16 @@ public:
String get_executable_path() const;
virtual String get_resource_dir() const;
void process_accelerometer(const Vector3& p_accelerometer);
void process_touch(int p_what,int p_pointer, const Vector<TouchPos>& p_points);
void push_input(const InputEvent& p_ev);
void process_accelerometer(const Vector3 &p_accelerometer);
void process_touch(int p_what, int p_pointer, const Vector<TouchPos> &p_points);
void push_input(const InputEvent &p_ev);
virtual bool is_joy_known(int p_device);
virtual String get_joy_guid(int p_device) const;
bool joy_connection_changed(int p_type, const EmscriptenGamepadEvent *p_event);
OS_JavaScript(GFXInitFunc p_gfx_init_func,void*p_gfx_init_ud, GetDataDirFunc p_get_data_dir_func);
OS_JavaScript(GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, GetDataDirFunc p_get_data_dir_func);
~OS_JavaScript();
};
#endif