1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/android/src/org/lxdream/LxdreamView.java Sat Feb 25 21:30:49 2012 +1000
1.7 + * Lxdream GL view. Derived from android
1.9 + * Copyright (c) 2011 Nathan Keynes.
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.22 +package org.lxdream;
1.24 + * Copyright (C) 2008,2009 The Android Open Source Project
1.26 + * Licensed under the Apache License, Version 2.0 (the "License");
1.27 + * you may not use this file except in compliance with the License.
1.28 + * You may obtain a copy of the License at
1.30 + * http://www.apache.org/licenses/LICENSE-2.0
1.32 + * Unless required by applicable law or agreed to in writing, software
1.33 + * distributed under the License is distributed on an "AS IS" BASIS,
1.34 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1.35 + * See the License for the specific language governing permissions and
1.36 + * limitations under the License.
1.40 +import android.content.Context;
1.41 +import android.graphics.PixelFormat;
1.42 +import android.opengl.GLSurfaceView;
1.43 +import android.util.AttributeSet;
1.44 +import android.util.Log;
1.45 +import android.view.KeyEvent;
1.46 +import android.view.MotionEvent;
1.48 +import javax.microedition.khronos.egl.EGL10;
1.49 +import javax.microedition.khronos.egl.EGLConfig;
1.50 +import javax.microedition.khronos.egl.EGLContext;
1.51 +import javax.microedition.khronos.egl.EGLDisplay;
1.52 +import javax.microedition.khronos.opengles.GL10;
1.55 + * A simple GLSurfaceView sub-class that demonstrate how to perform
1.56 + * OpenGL ES 2.0 rendering into a GL Surface. Note the following important
1.59 + * - The class must use a custom context factory to enable 2.0 rendering.
1.60 + * See ContextFactory class definition below.
1.62 + * - The class must use a custom EGLConfigChooser to be able to select
1.63 + * an EGLConfig that supports 2.0. This is done by providing a config
1.64 + * specification to eglChooseConfig() that has the attribute
1.65 + * EGL10.ELG_RENDERABLE_TYPE containing the EGL_OPENGL_ES2_BIT flag
1.66 + * set. See ConfigChooser class definition below.
1.68 + * - The class must select the surface's format, then choose an EGLConfig
1.69 + * that matches it exactly (with regards to red/green/blue/alpha channels
1.70 + * bit depths). Failure to do so would result in an EGL_BAD_MATCH error.
1.72 +class LxdreamView extends GLSurfaceView {
1.73 + private static String TAG = "LxdreamView";
1.74 + private static final boolean DEBUG = false;
1.76 + public LxdreamView(Context context) {
1.78 + init(false, 0, 0);
1.81 + public LxdreamView(Context context, boolean translucent, int depth, int stencil) {
1.83 + init(translucent, depth, stencil);
1.86 + private void init(boolean translucent, int depth, int stencil) {
1.88 + /* By default, GLSurfaceView() creates a RGB_565 opaque surface.
1.89 + * If we want a translucent one, we should change the surface's
1.90 + * format here, using PixelFormat.TRANSLUCENT for GL Surfaces
1.91 + * is interpreted as any 32-bit surface with alpha by SurfaceFlinger.
1.93 + if (translucent) {
1.94 + this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
1.97 + /* Setup the context factory for 2.0 rendering.
1.98 + * See ContextFactory class definition below
1.100 + setEGLContextFactory(new ContextFactory());
1.102 + /* We need to choose an EGLConfig that matches the format of
1.103 + * our surface exactly. This is going to be done in our
1.104 + * custom config chooser. See ConfigChooser class definition
1.107 + setEGLConfigChooser( translucent ?
1.108 + new ConfigChooser(8, 8, 8, 8, depth, stencil) :
1.109 + new ConfigChooser(5, 6, 5, 0, depth, stencil) );
1.111 + /* Set the renderer responsible for frame rendering */
1.112 + setRenderer(new Renderer());
1.115 + private static class ContextFactory implements GLSurfaceView.EGLContextFactory {
1.116 + private static int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
1.117 + public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig) {
1.118 + Log.w(TAG, "creating OpenGL ES 2.0 context");
1.119 + checkEglError("Before eglCreateContext", egl);
1.120 + int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
1.121 + EGLContext context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list);
1.122 + checkEglError("After eglCreateContext", egl);
1.126 + public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context) {
1.127 + egl.eglDestroyContext(display, context);
1.131 + private static void checkEglError(String prompt, EGL10 egl) {
1.133 + while ((error = egl.eglGetError()) != EGL10.EGL_SUCCESS) {
1.134 + Log.e(TAG, String.format("%s: EGL error: 0x%x", prompt, error));
1.139 + private static class Renderer implements GLSurfaceView.Renderer {
1.140 + public void onDrawFrame(GL10 gl) {
1.144 + public void onSurfaceChanged(GL10 gl, int width, int height) {
1.145 + Dreamcast.init();
1.146 + Dreamcast.setViewSize(width,height);
1.149 + public void onSurfaceCreated(GL10 gl, EGLConfig config) {