value System::GetDisplay (int id) { if (!init) { id_bounds = val_id ("bounds"); id_currentMode = val_id ("currentMode"); id_dpi = val_id ("dpi"); id_height = val_id ("height"); id_name = val_id ("name"); id_pixelFormat = val_id ("pixelFormat"); id_refreshRate = val_id ("refreshRate"); id_supportedModes = val_id ("supportedModes"); id_width = val_id ("width"); init = true; } int numDisplays = GetNumDisplays (); if (id < 0 || id >= numDisplays) { return alloc_null (); } value display = alloc_empty_object (); alloc_field (display, id_name, alloc_string (SDL_GetDisplayName (id))); SDL_Rect bounds = { 0, 0, 0, 0 }; SDL_GetDisplayBounds (id, &bounds); alloc_field (display, id_bounds, Rectangle (bounds.x, bounds.y, bounds.w, bounds.h).Value ()); float dpi = 72.0; #ifndef EMSCRIPTEN SDL_GetDisplayDPI (id, &dpi, NULL, NULL); #endif alloc_field (display, id_dpi, alloc_float (dpi)); SDL_DisplayMode displayMode = { SDL_PIXELFORMAT_UNKNOWN, 0, 0, 0, 0 }; DisplayMode mode; SDL_GetDesktopDisplayMode (id, &displayMode); mode.height = displayMode.h; switch (displayMode.format) { case SDL_PIXELFORMAT_ARGB8888: mode.pixelFormat = ARGB32; break; case SDL_PIXELFORMAT_BGRA8888: case SDL_PIXELFORMAT_BGRX8888: mode.pixelFormat = BGRA32; break; default: mode.pixelFormat = RGBA32; } mode.refreshRate = displayMode.refresh_rate; mode.width = displayMode.w; alloc_field (display, id_currentMode, mode.Value ()); int numDisplayModes = SDL_GetNumDisplayModes (id); value supportedModes = alloc_array (numDisplayModes); for (int i = 0; i < numDisplayModes; i++) { SDL_GetDisplayMode (id, i, &displayMode); mode.height = displayMode.h; switch (displayMode.format) { case SDL_PIXELFORMAT_ARGB8888: mode.pixelFormat = ARGB32; break; case SDL_PIXELFORMAT_BGRA8888: case SDL_PIXELFORMAT_BGRX8888: mode.pixelFormat = BGRA32; break; default: mode.pixelFormat = RGBA32; } mode.refreshRate = displayMode.refresh_rate; mode.width = displayMode.w; val_array_set_i (supportedModes, i, mode.Value ()); } alloc_field (display, id_supportedModes, supportedModes); return display; }
value HypRate_setPositive_text( value s ) { setPositive_text( val_string( s ) ); return alloc_null( ); }
value HypRate_setNegative_text( value s ) { setCancel_text( val_string( s ) ); return alloc_null( ); }
/** sprintf : fmt:string -> params:(any | array) -> string <doc> Format a string. If only one parameter is needed then it can be directly passed, either the parameters need to be stored in an array. The following formats are accepted (with corresponding types) : <ul> <li>[%s] : string</li> <li>[%d] [%x] [%X] : int</li> <li>[%c] : int in the 0..255 range</li> <li>[%b] : bool</li> <li>[%f] : float</li> </ul> </doc> **/ static value neko_sprintf( value fmt, value params ) { const char *last, *cur, *end; int count = 0; buffer b; val_check(fmt,string); b = alloc_buffer(0); last = val_string(fmt); cur = last; end = cur + val_strlen(fmt); while( cur != end ) { if( *cur == '%' ) { int width = 0, prec = 0, flags = 0; buffer_append_sub(b,last,cur - last); cur++; while( *cur >= '0' && *cur <= '9' ) { width = width * 10 + (*cur - '0'); cur++; } if( *cur == '.' ) { cur++; while( *cur >= '0' && *cur <= '9' ) { prec = prec * 10 + (*cur - '0'); cur++; } } if( *cur == '%' ) { buffer_append_sub(b,"%",1); cur++; } else { value param; if( count == 0 && !val_is_array(params) ) { // first ? param = params; count++; } else if( !val_is_array(params) || val_array_size(params) <= count ) return alloc_null(); else param = val_array_i(params,count++); switch( *cur ) { case 'c': { int c; char cc; val_check(param,int); c = val_int(param); if( c < 0 || c > 255 ) return alloc_null(); cc = (char)c; buffer_append_sub(b,&cc,1); } break; case 'x': flags |= HEX_SMALL; case 'X': flags |= HEX; case 'd': { char tmp[10]; int sign = 0; int size = 0; int tsize; int n; val_check(param,int); n = val_int(param); if( !(flags & HEX) && n < 0 ) { sign++; prec--; n = -n; } else if( n == 0 ) tmp[9-size++] = '0'; if( flags & HEX ) { unsigned int nn = (unsigned int)n; while( nn > 0 ) { int k = nn&15; if( k < 10 ) tmp[9-size++] = k + '0'; else tmp[9-size++] = (k - 10) + ((flags & HEX_SMALL)?'a':'A'); nn = nn >> 4; } } else { while( n > 0 ) { tmp[9-size++] = (n % 10) + '0'; n = n / 10; } } tsize = (size > prec)?size:prec + sign; while( width > tsize ) { width--; buffer_append_sub(b," ",1); } if( sign ) buffer_append_sub(b,"-",1); while( prec > size ) { prec--; buffer_append_sub(b,"0",1); } buffer_append_sub(b,tmp+10-size,size); } break; case 'f': { val_check(param,float); val_buffer(b,param); } break; case 's': { int size; int tsize; val_check(param,string); size = val_strlen(param); tsize = (size > prec)?size:prec; while( width > tsize ) { width--; buffer_append_sub(b," ",1); } while( prec > size ) { prec--; buffer_append_sub(b," ",1); } buffer_append_sub(b,val_string(param),size); } break; case 'b': { val_check(param,bool); buffer_append_sub(b,val_bool(param)?"true":"false",val_bool(param)?4:5); } break; default: return alloc_null(); break; } }
/** sys_read_dir : string -> string list <doc>Return the content of a directory</doc> **/ static value sys_read_dir( value p) { val_check(p,string); value result = alloc_array(0); #if defined(HX_WINRT) && defined(__cplusplus_winrt) auto folder = (Windows::Storage::StorageFolder::GetFolderFromPathAsync( ref new Platform::String(val_wstring(p)) ))->GetResults(); auto results = folder->GetFilesAsync(Windows::Storage::Search::CommonFileQuery::DefaultQuery)->GetResults(); for(int i=0;i<results->Size;i++) val_array_push(result,alloc_wstring(results->GetAt(i)->Path->Data())); #elif defined(NEKO_WINDOWS) && !defined(KORE_CONSOLE) const wchar_t *path = val_wstring(p); size_t len = wcslen(path); if (len>MAX_PATH) return alloc_null(); WIN32_FIND_DATAW d; HANDLE handle; #if defined(HX_WINRT) && !defined(_XBOX_ONE) std::wstring tempWStr(path); std::string searchPath(tempWStr.begin(), tempWStr.end()); #else wchar_t searchPath[ MAX_PATH + 4 ]; memcpy(searchPath,path, len*sizeof(wchar_t)); #endif if( len && path[len-1] != '/' && path[len-1] != '\\' ) searchPath[len++] = '/'; searchPath[len++] = '*'; searchPath[len++] = '.'; searchPath[len++] = '*'; searchPath[len] = '\0'; gc_enter_blocking(); #if defined(HX_WINRT) && !defined(_XBOX_ONE) handle = FindFirstFileEx(searchPath.c_str(), FindExInfoStandard, &d, FindExSearchNameMatch, NULL, 0); #else handle = FindFirstFileW(searchPath,&d); #endif if( handle == INVALID_HANDLE_VALUE ) { gc_exit_blocking(); return alloc_null(); } while( true ) { // skip magic dirs if( d.cFileName[0] != '.' || (d.cFileName[1] != 0 && (d.cFileName[1] != '.' || d.cFileName[2] != 0)) ) { gc_exit_blocking(); val_array_push(result,alloc_wstring(d.cFileName)); gc_enter_blocking(); } if( !FindNextFileW(handle,&d) ) break; } FindClose(handle); #elif !defined(EPPC) && !defined(KORE_CONSOLE) DIR *d; struct dirent *e; const char *name = val_string(p); gc_enter_blocking(); d = opendir(name); if( d == NULL ) { gc_exit_blocking(); val_throw(alloc_string("Invalid directory")); } while( true ) { e = readdir(d); if( e == NULL ) break; // skip magic dirs if( e->d_name[0] == '.' && (e->d_name[1] == 0 || (e->d_name[1] == '.' && e->d_name[2] == 0)) ) continue; gc_exit_blocking(); val_array_push(result, alloc_string(e->d_name) ); gc_enter_blocking(); } closedir(d); #endif gc_exit_blocking(); return result; }
value admob_ad_show_interstitial() { admob::showInterstitial(); return alloc_null(); }
value achievements_setSteps(value id, value numSteps) { googleapi::achievements::setSteps(val_string(id), val_int(numSteps)); return alloc_null(); }
value lime_renderer_flip (value renderer) { ((Renderer*)(intptr_t)val_float (renderer))->Flip (); return alloc_null (); }
value lime_renderer_unlock (value renderer) { ((Renderer*)(intptr_t)val_float (renderer))->Unlock (); return alloc_null (); }
value lime_mouse_set_cursor (value cursor) { Mouse::SetCursor ((MouseCursor)val_int (cursor)); return alloc_null (); }
value lime_mouse_show () { Mouse::Show (); return alloc_null (); }
value lime_mouse_set_lock (value lock) { Mouse::SetLock (val_bool (lock)); return alloc_null (); }
value lime_mouse_hide () { Mouse::Hide (); return alloc_null (); }
value JObjectToHaxe (JNIEnv *inEnv, JNIType inType, jobject inObject) { if (inObject == 0) { return alloc_null (); } if (inType.isUnknownType ()) { jclass cls = inEnv->GetObjectClass (inObject); if (cls) { for (int i = 0; i < jniELEMENTS; i++) { if (JNIType::elementClass[i] == 0) continue; if (inEnv->IsSameObject (cls, JNIType::elementClass[i])) { inType = JNIType ((JNIElement)i, 0); break; } } if (inType.isUnknownType ()) { for (int i = 0; i < jniELEMENTS; i++) { if (JNIType::elementArrayClass[i] == 0) continue; if (inEnv->IsSameObject (cls, JNIType::elementArrayClass[i])) { inType = JNIType ((JNIElement)i, 1); break; } } } if (inType.isUnknownType ()) { if (inEnv->CallBooleanMethod (cls, isArrayClass)) { inType = JNIType (jniUnknown, 1); } } } if (inType.isUnknownType ()) { inType = JNIType (jniObject, 0); } } if (inType.arrayDepth > 1 || (inType.arrayDepth == 1 && inType.element < jniPODStart)) { int len = inEnv->GetArrayLength ((jarray)inObject); value result = alloc_array (len); JNIType child = inType.elemType (); for (int i = 0; i < len; i++) { val_array_set_i (result, i, JObjectToHaxe (inEnv, child, inEnv->GetObjectArrayElement ((jobjectArray)inObject, i))); } return result; } else if (inType.arrayDepth == 1) { int len = inEnv->GetArrayLength ((jarray)inObject); value result = alloc_array (len); switch (inType.element) { ARRAY_SET (Boolean, jboolean, alloc_bool) //ARRAY_SET (Byte, jbyte, alloc_int) ARRAY_SET (Char, jchar, alloc_int) ARRAY_SET (Short, jshort, alloc_int) ARRAY_SET (Int, jint, alloc_int) ARRAY_SET (Long, jlong, alloc_int) ARRAY_SET (Float, jfloat, alloc_float) ARRAY_SET (Double, jdouble, alloc_float) case jniByte: { if (len > 0) { jboolean copy; jbyte *data = inEnv->GetByteArrayElements ((jbyteArray)inObject, ©); for (int i = 0; i < len; i++) { val_array_set_i (result, i, alloc_int (data[i])); } inEnv->ReleaseByteArrayElements ((jbyteArray)inObject, data, JNI_ABORT); } } break; } return result; } else {
value admob_ad_refresh() { admob::refreshAd(); return alloc_null(); }
MethodWrapper() : clbkVisitMethod(alloc_null()) { }
value admob_ad_init_interstitial(value id, value testMode) { admob::initInterstitial(val_string(id), val_bool(testMode)); return alloc_null(); }
value nme_font_register_font(value inFontName, value inBytes) { AutoGCRoot *bytes = new AutoGCRoot(inBytes); sgRegisteredFonts[std::string(val_string(inFontName))] = bytes; return alloc_null(); }
// Games Achievements value achievements_unlock(value id) { googleapi::achievements::unlock(val_string(id)); return alloc_null(); }
static value googleapi_init(value val_clientId) { googleapi::clientId = val_get_string(val_clientId); return alloc_null(); }
value achievements_reveal(value id) { googleapi::achievements::reveal(val_string(id)); return alloc_null(); }
static value googleapi_authenticate(value scopes, value handler) { googleapi::authenticate(new AutoGCRoot(handler), val_get_string(scopes)); return alloc_null(); }
/** sys_stat : string -> { gid => int, uid => int, atime => 'int32, mtime => 'int32, ctime => 'int32, dev => int, ino => int, nlink => int, rdev => int, mode => int, size => int } <doc>Run the [stat] command on the given file or directory.</doc> **/ static value sys_stat( value path ) { #if defined(EPPC) || defined(KORE_CONSOLE) return alloc_null(); #else value o; val_check(path,string); #if defined(NEKO_WINDOWS) && !defined(KORE_WINDOWSAPP) && !defined(KORE_XBOX_ONE) const wchar_t* _path = val_wstring(path); gc_enter_blocking(); WIN32_FILE_ATTRIBUTE_DATA data; if( !GetFileAttributesExW(_path,GetFileExInfoStandard,&data) ) { gc_exit_blocking(); return alloc_null(); } gc_exit_blocking(); wchar_t fullPath[MAX_PATH+1]; GetFullPathNameW(_path,MAX_PATH+1,fullPath,NULL); int dev = PathGetDriveNumberW(fullPath); #define EPOCH_DIFF (134774*24*60*60.0) ULARGE_INTEGER ui; o = alloc_empty_object( ); alloc_field(o,val_id("gid"),alloc_int(0)); alloc_field(o,val_id("uid"),alloc_int(0)); ui.LowPart = data.ftLastAccessTime.dwLowDateTime; ui.HighPart = data.ftLastAccessTime.dwHighDateTime; alloc_field(o,val_id("atime"),alloc_int32((int)(((double)ui.QuadPart) / 10000000.0 - EPOCH_DIFF))); ui.LowPart = data.ftLastWriteTime.dwLowDateTime; ui.HighPart = data.ftLastWriteTime.dwHighDateTime; alloc_field(o,val_id("mtime"),alloc_int32((int)(((double)ui.QuadPart) / 10000000.0 - EPOCH_DIFF))); ui.LowPart = data.ftCreationTime.dwLowDateTime; ui.HighPart = data.ftCreationTime.dwHighDateTime; alloc_field(o,val_id("ctime"),alloc_int32((int)(((double)ui.QuadPart) / 10000000.0 - EPOCH_DIFF))); alloc_field(o,val_id("dev"),alloc_int(dev)); alloc_field(o,val_id("ino"),alloc_int(0)); int mode = 0; if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) mode |= _S_IFDIR; if ((data.dwFileAttributes & FILE_ATTRIBUTE_OFFLINE) == 0) mode |= _S_IFREG; mode |= _S_IREAD; if ((data.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0) mode |= _S_IWRITE; if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) mode |= _S_IEXEC; alloc_field(o,val_id("mode"),alloc_int(mode)); alloc_field(o,val_id("nlink"),alloc_int(1)); alloc_field(o,val_id("rdev"),alloc_int(dev)); alloc_field(o,val_id("size"),alloc_int32(data.nFileSizeLow)); #else gc_enter_blocking(); struct stat s; if( stat(val_string(path),&s) != 0 ) { gc_exit_blocking(); return alloc_null(); } gc_exit_blocking(); o = alloc_empty_object( ); STATF(gid); STATF(uid); STATF32(atime); STATF32(mtime); STATF32(ctime); STATF(dev); STATF(ino); STATF(mode); STATF(nlink); STATF(rdev); STATF(size); #endif return o; #endif }
value admob_ad_init(value id, value x, value y, value size, value testMode) { admob::initAd(val_string(id), val_int(x), val_int(y), val_int(size), val_bool(testMode)); return alloc_null(); }
value HypRate_setDialog_msg( value s ) { setDialog_msg( val_string( s ) ); return alloc_null( ); }
value admob_ad_show() { admob::showAd(); return alloc_null(); }
value HypRate_setNeutral_text( value s ) { setNeutral_text( val_string( s ) ); return alloc_null( ); }
value admob_ad_hide() { admob::hideAd(); return alloc_null(); }
value HypRate_start( value minL , value minD , value untilL , value untilD ){ start( val_int( minL ) , val_int( minD ) , val_int( untilL ) , val_int( untilD )); return alloc_null( ); }
value AudioBuffer::Value (value audioBuffer) { if (!init) { id_bitsPerSample = val_id ("bitsPerSample"); id_channels = val_id ("channels"); id_data = val_id ("data"); id_sampleRate = val_id ("sampleRate"); init = true; } alloc_field (audioBuffer, id_bitsPerSample, alloc_int (bitsPerSample)); alloc_field (audioBuffer, id_channels, alloc_int (channels)); alloc_field (audioBuffer, id_data, data ? data->Value (val_field (audioBuffer, id_data)) : alloc_null ()); alloc_field (audioBuffer, id_sampleRate, alloc_int (sampleRate)); return audioBuffer; }