/** * 跳转到指定的Activity * * @param from */ public static void action(Activity from, Class<?> clazz) { if (LSystem.getActivity() instanceof LGameAndroid2DActivity) { ((LGameAndroid2DActivity) LSystem.getActivity()).setDestroy(false); } android.content.Intent intent = new android.content.Intent(from, clazz); from.startActivity(intent); }
/** * 跳转到指定的Activity,并将其设定为最初的Activity * * @param from * @param clazz */ public static void go(Activity from, Class<?> clazz) { if (LSystem.getActivity() instanceof LGameAndroid2DActivity) { ((LGameAndroid2DActivity) LSystem.getActivity()).setDestroy(false); } android.content.Intent intent = new android.content.Intent(from, clazz); intent.setFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP); from.startActivity(intent); }
public LGameAndroid2DView(LGameAndroid2DActivity activity, boolean isLandscape, LMode mode) { super(activity.getApplicationContext()); try { LSystem.setupHandler(activity, this, isLandscape, mode); this.handler = LSystem.getSystemHandler(); this.handler.initScreen(); this.activity = handler.getLGameActivity(); this.setFPS(LSystem.DEFAULT_MAX_FPS); this.createScreen(); } catch (Exception e) { } }
/** 创建游戏窗体载体 */ private void createScreen() { this.canvasImage = new LImage(width = handler.getWidth(), height = handler.getHeight(), false); this.gl = canvasImage.getLGraphics(); this.currentScreen = canvasImage.getBitmap(); if (LSystem.isLowerVer()) { this.surfaceHolder = getHolder(); this.surfaceHolder.addCallback(this); } else { this.surfaceHolder = getHolder(); int mode = 0; try { mode = 1; surfaceHolder.setType(android.view.SurfaceHolder.SURFACE_TYPE_HARDWARE); } catch (Exception e) { try { mode = 2; surfaceHolder.setType(android.view.SurfaceHolder.SURFACE_TYPE_GPU); } catch (Exception e2) { surfaceHolder.setType(android.view.SurfaceHolder.SURFACE_TYPE_NORMAL); } } switch (mode) { case 1: Log.i("Android2DView", "Hardware surface"); break; case 2: Log.i("Android2DView", "GPU surface"); break; default: Log.i("Android2DView", "No hardware acceleration available"); } this.surfaceHolder.addCallback(this); this.surfaceHolder.setFormat(PixelFormat.RGB_565); } this.setClickable(false); this.setFocusable(true); this.setFocusableInTouchMode(true); this.setKeepScreenOn(true); this.setLongClickable(false); this.destroyDrawingCache(); this.setDrawingCacheBackgroundColor(0); this.setDrawingCacheEnabled(false); if (LSystem.isHTC()) { this.setWillNotCacheDrawing(false); this.setWillNotDraw(false); } else { this.setWillNotCacheDrawing(true); this.setWillNotDraw(true); } this.requestFocus(); this.requestFocusFromTouch(); }
public static File getCacheFile(String fileName) { final String file = LSystem.getCacheFileName(); fileName = StringUtils.replaceIgnoreCase(fileName, "\\", "/"); if (file != null) { if (fileName.startsWith("/") || fileName.startsWith("\\")) { fileName = fileName.substring(1, fileName.length()); } if (file.endsWith("/") || file.endsWith("\\")) { return new File(LSystem.getCacheFileName() + fileName); } else { return new File(LSystem.getCacheFileName() + LSystem.FS + fileName); } } else { return new File(fileName); } }
/** * 以指定范围内的指定概率执行gc * * @param size * @param rand */ public static final void gc(final int size, final long rand) { if (rand > size) { throw new RuntimeException(("GC random probability " + rand + " > " + size).intern()); } if (LSystem.random.nextInt(size) <= rand) { LSystem.gc(); } }
/** * 加载Properties文件 * * @param file * @return */ public static final Properties loadPropertiesFromFile(final File file) { if (file == null) { return null; } Properties properties = new Properties(); try { InputStream in = LSystem.read(file); loadProperties(properties, in, file.getName()); in.close(); } catch (IOException e) { e.printStackTrace(); } return properties; }
public void destroyView() { try { synchronized (this) { handler.getAssetsSound().stopSoundAll(); handler.getPlaySound().stopSoundAll(); handler.destroy(); ActionControl.getInstance().stopAll(); LSystem.destroy(); releaseResources(); notifyAll(); } } catch (Exception e) { } }
/** * 更新游戏画布,成比例调整显示位置(画面变更为指定大小) * * @param img * @param w * @param h */ public void updateFull(Bitmap bit, int w, int h) { if (!isRunning) { return; } if (bit == null) { return; } canvas = surfaceHolder.lockCanvas(null); if (canvas != null) { int nw = bit.getWidth(); int nh = bit.getHeight(); if (nw == w && nh == h) { synchronized (surfaceHolder) { canvas.drawBitmap(bit, width / 2 - w / 2, height / 2 - h / 2, null); if (emulatorButtons != null) { emulatorButtons.draw(canvas); } } surfaceHolder.unlockCanvasAndPost(canvas); return; } if (LSystem.isOverrunOS21()) { float scaleWidth = ((float) w) / nw; float scaleHeight = ((float) h) / nh; tmp_matrix.reset(); tmp_matrix.postScale(scaleWidth, scaleHeight); tmp_matrix.postTranslate(width / 2 - w / 2, height / 2 - h / 2); synchronized (surfaceHolder) { canvas.drawBitmap(bit, tmp_matrix, null); if (emulatorButtons != null) { emulatorButtons.draw(canvas); } } surfaceHolder.unlockCanvasAndPost(canvas); } else { int x = width / 2 - w / 2; int y = height / 2 - h / 2; Rect srcR = new Rect(0, 0, w, h); Rect dstR = new Rect(x, y, x + w, y + h); synchronized (surfaceHolder) { canvas.drawBitmap(bit, srcR, dstR, null); if (emulatorButtons != null) { emulatorButtons.draw(canvas); } } surfaceHolder.unlockCanvasAndPost(canvas); } } }
/** * 加载资源文件 * * @param resName * @return */ public static final ArrayByte getResource(String resName) { if (resName == null) { return null; } String innerName = resName; String keyName = innerName.replaceAll(" ", "").toLowerCase(); synchronized (lock) { if (lazyResources.size() > LSystem.DEFAULT_MAX_CACHE_SIZE) { lazyResources.clear(); LSystem.gc(); } byte[] data = (byte[]) lazyResources.get(keyName); if (data != null) { return new ArrayByte(data); } } InputStream in = null; // 外部文件标志 boolean filePath = innerName.startsWith("$"); if (filePath || isExists(resName)) { try { innerName = innerName.substring(1, innerName.length()); in = new BufferedInputStream(new FileInputStream(new File(innerName))); } catch (FileNotFoundException ex) { } } else { in = new BufferedInputStream(classLoader.getResourceAsStream(innerName)); } ArrayByte byteArray = new ArrayByte(); try { byteArray.write(in); in.close(); byteArray.reset(); lazyResources.put(keyName, byteArray.getData()); } catch (IOException ex) { byteArray = null; } if (byteArray == null) { throw new RuntimeException(resName + " file not found !"); } return byteArray; }
public LGameWeb(String url, WebProcess webProcess) { this((LGameAndroid2DActivity) LSystem.getActivity(), webProcess, url); }
/** 清空框架临时资源 */ public static void destroy() { GraphicsUtils.destroy(); // ZipResource.destroy(); Resources.destroy(); LSystem.gc(); }
public Sprites() { this.visible = true; this.width = LSystem.getSystemHandler().getWidth(); this.height = LSystem.getSystemHandler().getHeight(); this.sprites = new ISprite[capacity]; }
public LGameWeb(String url) { this((LGameAndroid2DActivity) LSystem.getActivity(), null, url); }
private final class CanvasThread extends Thread { final LTimerContext timerContext = new LTimerContext(); final SystemTimer timer = LSystem.getSystemTimer(); private long lastTimeMicros, currTimeMicros, goalTimeMicros, elapsedTimeMicros, remainderMicros, elapsedTime, frameCount, frames; public CanvasThread() { isRunning = true; setName("CanvasThread"); } synchronized void updateCapture(final Bitmap bit) { if (!handler.next()) { return; } handler.calls(); handler.runTimer(timerContext); if (LSystem.AUTO_REPAINT) { canvas = surfaceHolder.lockCanvas(null); if (canvas == null) { return; } synchronized (surfaceHolder) { gl.update(bit); repaintMode = handler.getRepaintMode(); switch (repaintMode) { case Screen.SCREEN_BITMAP_REPAINT: gl.drawBitmap(handler.getBackground(), 0, 0); break; case Screen.SCREEN_CANVAS_REPAINT: gl.drawClear(); break; case Screen.SCREEN_NOT_REPAINT: break; default: gl.drawBitmap( handler.getBackground(), repaintMode / 2 - LSystem.random.nextInt(repaintMode), repaintMode / 2 - LSystem.random.nextInt(repaintMode)); break; } handler.draw(gl); if (isFPS) { tickFrames(); gl.setFont(fpsFont); gl.setColor(LColor.white); gl.drawString("FPS:" + curFPS, 5, 20); } if (isMemory) { Runtime runtime = Runtime.getRuntime(); long totalMemory = runtime.totalMemory(); long currentMemory = totalMemory - runtime.freeMemory(); String memory = ((float) ((currentMemory * 10) >> 20) / 10) + " of " + ((float) ((totalMemory * 10) >> 20) / 10) + " MB"; gl.setFont(fpsFont); gl.setColor(LColor.white); gl.drawString("MEMORY:" + memory, 5, 45); } if (emulatorButtons != null) { emulatorButtons.draw(gl); } } surfaceHolder.unlockCanvasAndPost(canvas); } } /** 游戏窗体主循环 */ public void run() { boolean isScale = handler.isScale(); Thread currentThread = Thread.currentThread(); int restoreCount = 0; try { do { if (LSystem.isPaused || !isFocusable()) { pause(500); lastTimeMicros = timer.getTimeMicros(); elapsedTime = 0; remainderMicros = 0; continue; } if (!handler.next()) { continue; } handler.calls(); goalTimeMicros = lastTimeMicros + 1000000L / maxFrames; currTimeMicros = timer.sleepTimeMicros(goalTimeMicros); elapsedTimeMicros = currTimeMicros - lastTimeMicros + remainderMicros; elapsedTime = Math.max(0, (int) (elapsedTimeMicros / 1000)); remainderMicros = elapsedTimeMicros - elapsedTime * 1000; lastTimeMicros = currTimeMicros; timerContext.millisSleepTime = remainderMicros; timerContext.timeSinceLastUpdate = elapsedTime; handler.runTimer(timerContext); if (LSystem.AUTO_REPAINT) { canvas = surfaceHolder.lockCanvas(null); if (canvas == null) { continue; } synchronized (surfaceHolder) { if (isScale) { canvas.setDrawFilter(zoomFilter); canvas.scale(LSystem.scaleWidth, LSystem.scaleHeight); restoreCount = canvas.save(); } gl.update(canvas); repaintMode = handler.getRepaintMode(); switch (repaintMode) { case Screen.SCREEN_BITMAP_REPAINT: if (handler.getX() == 0 && handler.getY() == 0) { gl.drawBitmap(handler.getBackground(), 0, 0); } else { gl.drawClear(); gl.drawBitmap(handler.getBackground(), handler.getX(), handler.getY()); } break; case Screen.SCREEN_CANVAS_REPAINT: gl.drawClear(); break; case Screen.SCREEN_NOT_REPAINT: break; default: if (handler.getX() == 0 && handler.getY() == 0) { gl.drawBitmap( handler.getBackground(), repaintMode / 2 - LSystem.random.nextInt(repaintMode), repaintMode / 2 - LSystem.random.nextInt(repaintMode)); } else { gl.drawClear(); gl.drawBitmap( handler.getBackground(), handler.getX() + repaintMode / 2 - LSystem.random.nextInt(repaintMode), handler.getY() + repaintMode / 2 - LSystem.random.nextInt(repaintMode)); } break; } handler.draw(gl); if (isFPS) { tickFrames(); gl.setFont(fpsFont); gl.setColor(LColor.white); gl.drawString("FPS:" + curFPS, 5, 20); } if (isMemory) { Runtime runtime = Runtime.getRuntime(); long totalMemory = runtime.totalMemory(); long currentMemory = totalMemory - runtime.freeMemory(); String memory = ((float) ((currentMemory * 10) >> 20) / 10) + " of " + ((float) ((totalMemory * 10) >> 20) / 10) + " MB"; gl.setFont(fpsFont); gl.setColor(LColor.white); gl.drawString("MEMORY:" + memory, 5, 45); } if (emulatorButtons != null) { emulatorButtons.draw(gl); } if (isScale) { canvas.restoreToCount(restoreCount); } } surfaceHolder.unlockCanvasAndPost(canvas); } } while (isRunning && mainLoop == currentThread); } catch (Exception ex) { Log.d("Android2DView", "LGame 2D View Error :", ex); } finally { destroyView(); } } private final void pause(long sleep) { try { Thread.sleep(sleep); } catch (InterruptedException ex) { } } private void tickFrames() { long time = System.currentTimeMillis(); if (time - frameCount > 1000L) { curFPS = Math.min(maxFrames, frames); frames = 0; frameCount = time; } frames++; } }
/** * 加载Properties文件 * * @param file */ public static final void loadPropertiesFileToSystem(final File file) { Properties properties = System.getProperties(); InputStream in = LSystem.read(file); loadProperties(properties, in, file.getName()); }
/** * 浏览器访问 * * @param url */ public static void openBrowser(final String url) { android.content.Intent i = new android.content.Intent(android.content.Intent.ACTION_VIEW); i.setData(android.net.Uri.parse(url)); LSystem.getActivity().startActivity(i); }
public static void runOnUiThread(final Runnable runnable) { LSystem.getActivity().runOnUiThread(runnable); }
/** * 返回当前屏幕状态 * * @return */ public static String getScreenOrientation() { return LSystem.getScreenOrientation(getActivity()); }