Search in sources :

Example 1 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.

the class LwjglApplication method mainLoop.

void mainLoop() {
    SnapshotArray<LifecycleListener> lifecycleListeners = this.lifecycleListeners;
    try {
        graphics.setupDisplay();
    } catch (LWJGLException e) {
        throw new GdxRuntimeException(e);
    }
    listener.create();
    graphics.resize = true;
    int lastWidth = graphics.getWidth();
    int lastHeight = graphics.getHeight();
    graphics.lastTime = System.nanoTime();
    boolean wasActive = true;
    while (running) {
        Display.processMessages();
        if (Display.isCloseRequested())
            exit();
        boolean isActive = Display.isActive();
        if (wasActive && !isActive) {
            // if it's just recently minimized from active state
            wasActive = false;
            synchronized (lifecycleListeners) {
                LifecycleListener[] listeners = lifecycleListeners.begin();
                for (int i = 0, n = lifecycleListeners.size; i < n; ++i) listeners[i].pause();
                lifecycleListeners.end();
            }
            listener.pause();
        }
        if (!wasActive && isActive) {
            // if it's just recently focused from minimized state
            wasActive = true;
            synchronized (lifecycleListeners) {
                LifecycleListener[] listeners = lifecycleListeners.begin();
                for (int i = 0, n = lifecycleListeners.size; i < n; ++i) listeners[i].resume();
                lifecycleListeners.end();
            }
            listener.resume();
        }
        boolean shouldRender = false;
        if (graphics.canvas != null) {
            int width = graphics.canvas.getWidth();
            int height = graphics.canvas.getHeight();
            if (lastWidth != width || lastHeight != height) {
                lastWidth = width;
                lastHeight = height;
                Gdx.gl.glViewport(0, 0, lastWidth, lastHeight);
                listener.resize(lastWidth, lastHeight);
                shouldRender = true;
            }
        } else {
            graphics.config.x = Display.getX();
            graphics.config.y = Display.getY();
            if (graphics.resize || Display.wasResized() || (int) (Display.getWidth() * Display.getPixelScaleFactor()) != graphics.config.width || (int) (Display.getHeight() * Display.getPixelScaleFactor()) != graphics.config.height) {
                graphics.resize = false;
                graphics.config.width = (int) (Display.getWidth() * Display.getPixelScaleFactor());
                graphics.config.height = (int) (Display.getHeight() * Display.getPixelScaleFactor());
                Gdx.gl.glViewport(0, 0, graphics.config.width, graphics.config.height);
                if (listener != null)
                    listener.resize(graphics.config.width, graphics.config.height);
                graphics.requestRendering();
            }
        }
        if (executeRunnables())
            shouldRender = true;
        // If one of the runnables set running to false, for example after an exit().
        if (!running)
            break;
        input.update();
        shouldRender |= graphics.shouldRender();
        input.processEvents();
        if (audio != null)
            audio.update();
        if (!isActive && graphics.config.backgroundFPS == -1)
            shouldRender = false;
        int frameRate = isActive ? graphics.config.foregroundFPS : graphics.config.backgroundFPS;
        if (shouldRender) {
            graphics.updateTime();
            graphics.frameId++;
            listener.render();
            Display.update(false);
        } else {
            // Sleeps to avoid wasting CPU in an empty loop.
            if (frameRate == -1)
                frameRate = 10;
            if (frameRate == 0)
                frameRate = graphics.config.backgroundFPS;
            if (frameRate == 0)
                frameRate = 30;
        }
        if (frameRate > 0)
            Display.sync(frameRate);
    }
    synchronized (lifecycleListeners) {
        LifecycleListener[] listeners = lifecycleListeners.begin();
        for (int i = 0, n = lifecycleListeners.size; i < n; ++i) {
            listeners[i].pause();
            listeners[i].dispose();
        }
        lifecycleListeners.end();
    }
    listener.pause();
    listener.dispose();
    Display.destroy();
    if (audio != null)
        audio.dispose();
    if (graphics.config.forceExit)
        System.exit(-1);
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) LifecycleListener(com.badlogic.gdx.LifecycleListener) LWJGLException(org.lwjgl.LWJGLException)

Example 2 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.

the class OggInputStream method readPCM.

/** Decode the OGG file as shown in the jogg/jorbis examples */
private void readPCM() {
    boolean wrote = false;
    while (true) {
        // we repeat if the bitstream is chained
        if (endOfBitStream) {
            if (!getPageAndPacket()) {
                break;
            }
            endOfBitStream = false;
        }
        if (!inited) {
            inited = true;
            return;
        }
        float[][][] _pcm = new float[1][][];
        int[] _index = new int[oggInfo.channels];
        // The rest is just a straight decode loop until end of stream
        while (!endOfBitStream) {
            while (!endOfBitStream) {
                int result = syncState.pageout(page);
                if (result == 0) {
                    // need more data
                    break;
                }
                if (result == -1) {
                    // missing or corrupt data at this page position
                    // throw new GdxRuntimeException("Corrupt or missing data in bitstream.");
                    Gdx.app.log("gdx-audio", "Error reading OGG: Corrupt or missing data in bitstream.");
                } else {
                    // can safely ignore errors at
                    streamState.pagein(page);
                    // this point
                    while (true) {
                        result = streamState.packetout(packet);
                        // need more data
                        if (result == 0)
                            break;
                        if (result == -1) {
                        // missing or corrupt data at this page position
                        // no reason to complain; already complained above
                        } else {
                            // we have a packet. Decode it
                            int samples;
                            if (vorbisBlock.synthesis(packet) == 0) {
                                // test for success!
                                dspState.synthesis_blockin(vorbisBlock);
                            }
                            while ((samples = dspState.synthesis_pcmout(_pcm, _index)) > 0) {
                                float[][] pcm = _pcm[0];
                                // boolean clipflag = false;
                                int bout = (samples < convsize ? samples : convsize);
                                // interleave
                                for (int i = 0; i < oggInfo.channels; i++) {
                                    int ptr = i * 2;
                                    // int ptr=i;
                                    int mono = _index[i];
                                    for (int j = 0; j < bout; j++) {
                                        int val = (int) (pcm[i][mono + j] * 32767.);
                                        // might as well guard against clipping
                                        if (val > 32767) {
                                            val = 32767;
                                        }
                                        if (val < -32768) {
                                            val = -32768;
                                        }
                                        if (val < 0)
                                            val = val | 0x8000;
                                        if (bigEndian) {
                                            convbuffer[ptr] = (byte) (val >>> 8);
                                            convbuffer[ptr + 1] = (byte) (val);
                                        } else {
                                            convbuffer[ptr] = (byte) (val);
                                            convbuffer[ptr + 1] = (byte) (val >>> 8);
                                        }
                                        ptr += 2 * (oggInfo.channels);
                                    }
                                }
                                int bytesToWrite = 2 * oggInfo.channels * bout;
                                if (bytesToWrite > pcmBuffer.remaining()) {
                                    throw new GdxRuntimeException("Ogg block too big to be buffered: " + bytesToWrite + " :: " + pcmBuffer.remaining());
                                } else {
                                    pcmBuffer.put(convbuffer, 0, bytesToWrite);
                                }
                                wrote = true;
                                // tell libvorbis how
                                dspState.synthesis_read(bout);
                            // many samples we
                            // actually consumed
                            }
                        }
                    }
                    if (page.eos() != 0) {
                        endOfBitStream = true;
                    }
                    if ((!endOfBitStream) && (wrote)) {
                        return;
                    }
                }
            }
            if (!endOfBitStream) {
                bytes = 0;
                int index = syncState.buffer(BUFFER_SIZE);
                if (index >= 0) {
                    buffer = syncState.data;
                    try {
                        bytes = input.read(buffer, index, BUFFER_SIZE);
                    } catch (Exception e) {
                        throw new GdxRuntimeException("Error during Vorbis decoding.", e);
                    }
                } else {
                    bytes = 0;
                }
                syncState.wrote(bytes);
                if (bytes == 0) {
                    endOfBitStream = true;
                }
            }
        }
        // clean up this logical bitstream; before exit we see if we're
        // followed by another [chained]
        streamState.clear();
        // ogg_page and ogg_packet structs always point to storage in
        // libvorbis. They're never freed or manipulated directly
        vorbisBlock.clear();
        dspState.clear();
        // must be called last
        oggInfo.clear();
    }
    // OK, clean up the framer
    syncState.clear();
    endOfStream = true;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) IOException(java.io.IOException) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException)

Example 3 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.

the class Lwjgl3Application method createGlfwWindow.

static long createGlfwWindow(Lwjgl3ApplicationConfiguration config, long sharedContextWindow) {
    GLFW.glfwDefaultWindowHints();
    GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE);
    GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, config.windowResizable ? GLFW.GLFW_TRUE : GLFW.GLFW_FALSE);
    GLFW.glfwWindowHint(GLFW.GLFW_MAXIMIZED, config.windowMaximized ? GLFW.GLFW_TRUE : GLFW.GLFW_FALSE);
    if (sharedContextWindow == 0) {
        GLFW.glfwWindowHint(GLFW.GLFW_RED_BITS, config.r);
        GLFW.glfwWindowHint(GLFW.GLFW_GREEN_BITS, config.g);
        GLFW.glfwWindowHint(GLFW.GLFW_BLUE_BITS, config.b);
        GLFW.glfwWindowHint(GLFW.GLFW_ALPHA_BITS, config.a);
        GLFW.glfwWindowHint(GLFW.GLFW_STENCIL_BITS, config.stencil);
        GLFW.glfwWindowHint(GLFW.GLFW_DEPTH_BITS, config.depth);
        GLFW.glfwWindowHint(GLFW.GLFW_SAMPLES, config.samples);
    }
    if (config.useGL30) {
        GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, config.gles30ContextMajorVersion);
        GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, config.gles30ContextMinorVersion);
        if (SharedLibraryLoader.isMac) {
            // hints mandatory on OS X for GL 3.2+ context creation, but fail on Windows if the
            // WGL_ARB_create_context extension is not available
            // see: http://www.glfw.org/docs/latest/compat.html
            GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, GLFW.GLFW_TRUE);
            GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE);
        }
    }
    if (config.debug) {
        GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_DEBUG_CONTEXT, GLFW.GLFW_TRUE);
    }
    long windowHandle = 0;
    if (config.fullscreenMode != null) {
        // glfwWindowHint(GLFW.GLFW_REFRESH_RATE, config.fullscreenMode.refreshRate);
        windowHandle = GLFW.glfwCreateWindow(config.fullscreenMode.width, config.fullscreenMode.height, config.title, config.fullscreenMode.getMonitor(), sharedContextWindow);
    } else {
        GLFW.glfwWindowHint(GLFW.GLFW_DECORATED, config.windowDecorated ? GLFW.GLFW_TRUE : GLFW.GLFW_FALSE);
        windowHandle = GLFW.glfwCreateWindow(config.windowWidth, config.windowHeight, config.title, 0, sharedContextWindow);
    }
    if (windowHandle == 0) {
        throw new GdxRuntimeException("Couldn't create window");
    }
    Lwjgl3Window.setSizeLimits(windowHandle, config.windowMinWidth, config.windowMinHeight, config.windowMaxWidth, config.windowMaxHeight);
    if (config.fullscreenMode == null && !config.windowMaximized) {
        if (config.windowX == -1 && config.windowY == -1) {
            int windowWidth = Math.max(config.windowWidth, config.windowMinWidth);
            int windowHeight = Math.max(config.windowHeight, config.windowMinHeight);
            if (config.windowMaxWidth > -1)
                windowWidth = Math.min(windowWidth, config.windowMaxWidth);
            if (config.windowMaxHeight > -1)
                windowHeight = Math.min(windowHeight, config.windowMaxHeight);
            GLFWVidMode vidMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
            GLFW.glfwSetWindowPos(windowHandle, vidMode.width() / 2 - windowWidth / 2, vidMode.height() / 2 - windowHeight / 2);
        } else {
            GLFW.glfwSetWindowPos(windowHandle, config.windowX, config.windowY);
        }
    }
    if (config.windowIconPaths != null) {
        Lwjgl3Window.setIcon(windowHandle, config.windowIconPaths, config.windowIconFileType);
    }
    GLFW.glfwMakeContextCurrent(windowHandle);
    GLFW.glfwSwapInterval(config.vSyncEnabled ? 1 : 0);
    GL.createCapabilities();
    initiateGL();
    if (!glVersion.isVersionEqualToOrHigher(2, 0))
        throw new GdxRuntimeException("OpenGL 2.0 or higher with the FBO extension is required. OpenGL version: " + GL11.glGetString(GL11.GL_VERSION) + "\n" + glVersion.getDebugVersionString());
    if (!supportsFBO()) {
        throw new GdxRuntimeException("OpenGL 2.0 or higher with the FBO extension is required. OpenGL version: " + GL11.glGetString(GL11.GL_VERSION) + ", FBO extension: false\n" + glVersion.getDebugVersionString());
    }
    if (config.debug) {
        glDebugCallback = GLUtil.setupDebugMessageCallback(config.debugStream);
        setGLDebugMessageControl(GLDebugMessageSeverity.NOTIFICATION, false);
    }
    return windowHandle;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) GLFWVidMode(org.lwjgl.glfw.GLFWVidMode)

Example 4 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.

the class HeadlessApplication method initialize.

private void initialize() {
    mainLoopThread = new Thread("HeadlessApplication") {

        @Override
        public void run() {
            try {
                HeadlessApplication.this.mainLoop();
            } catch (Throwable t) {
                if (t instanceof RuntimeException)
                    throw (RuntimeException) t;
                else
                    throw new GdxRuntimeException(t);
            }
        }
    };
    mainLoopThread.start();
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException)

Example 5 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.

the class LwjglGraphics method getDisplayModes.

@Override
public DisplayMode[] getDisplayModes() {
    try {
        org.lwjgl.opengl.DisplayMode[] availableDisplayModes = Display.getAvailableDisplayModes();
        DisplayMode[] modes = new DisplayMode[availableDisplayModes.length];
        int idx = 0;
        for (org.lwjgl.opengl.DisplayMode mode : availableDisplayModes) {
            if (mode.isFullscreenCapable()) {
                modes[idx++] = new LwjglDisplayMode(mode.getWidth(), mode.getHeight(), mode.getFrequency(), mode.getBitsPerPixel(), mode);
            }
        }
        return modes;
    } catch (LWJGLException e) {
        throw new GdxRuntimeException("Couldn't fetch available display modes", e);
    }
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) LWJGLException(org.lwjgl.LWJGLException)

Aggregations

GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)202 IOException (java.io.IOException)40 FileHandle (com.badlogic.gdx.files.FileHandle)14 Array (com.badlogic.gdx.utils.Array)13 Texture (com.badlogic.gdx.graphics.Texture)12 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)11 AtlasRegion (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)9 InputStream (java.io.InputStream)9 Pixmap (com.badlogic.gdx.graphics.Pixmap)8 Sprite (com.badlogic.gdx.graphics.g2d.Sprite)8 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)7 AtlasSprite (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite)7 BufferedInputStream (java.io.BufferedInputStream)7 File (java.io.File)7 OutputStream (java.io.OutputStream)7 VertexAttribute (com.badlogic.gdx.graphics.VertexAttribute)6 LifecycleListener (com.badlogic.gdx.LifecycleListener)5 ByteBuffer (java.nio.ByteBuffer)5 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)4 NinePatch (com.badlogic.gdx.graphics.g2d.NinePatch)4