|
| 1 | +/* |
| 2 | + * Copyright (c) 2009-2023 jMonkeyEngine |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are |
| 7 | + * met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * * Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
| 17 | + * may be used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | +package jme3test.renderer; |
| 33 | + |
| 34 | +import com.jme3.app.SimpleApplication; |
| 35 | +import com.jme3.input.KeyInput; |
| 36 | +import com.jme3.input.controls.ActionListener; |
| 37 | +import com.jme3.input.controls.KeyTrigger; |
| 38 | + |
| 39 | +/** |
| 40 | + * Test for JMonkeyEngine issue #798: OpenGLException on restart with changed |
| 41 | + * display settings. |
| 42 | + * <p> |
| 43 | + * If the issue is resolved, then pressing the "P", "T", or "Y" key shouldn't |
| 44 | + * crash the app; it should close the app display and create a new display, |
| 45 | + * mostly black with statistics displayed in the lower left. |
| 46 | + * <p> |
| 47 | + * If the issue is not resolved, the expected failure mode depends on whether |
| 48 | + * assertions are enabled. If they're enabled, the app will crash with an |
| 49 | + * OpenGLException. If assertions aren't enabled, the new window will be |
| 50 | + * entirely black, with no statistics visible. |
| 51 | + * <p> |
| 52 | + * Since the issue was specific to LWJGL v2, this test should be built with the |
| 53 | + * jme3-lwjgl library, not jme3-lwjgl3. |
| 54 | + * |
| 55 | + * @author Stephen Gold |
| 56 | + */ |
| 57 | +public class TestIssue798 extends SimpleApplication { |
| 58 | + /** |
| 59 | + * Main entry point for the TestIssue798 application. |
| 60 | + * |
| 61 | + * @param args array of command-line arguments (not null) |
| 62 | + */ |
| 63 | + public static void main(String[] args) { |
| 64 | + TestIssue798 app = new TestIssue798(); |
| 65 | + app.start(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Initialize this application. |
| 70 | + */ |
| 71 | + @Override |
| 72 | + public void simpleInitApp() { |
| 73 | + inputManager.addMapping("windowedMode", new KeyTrigger(KeyInput.KEY_F)); |
| 74 | + inputManager.addMapping("moreSamples", new KeyTrigger(KeyInput.KEY_P)); |
| 75 | + inputManager.addMapping("toggleDepth", new KeyTrigger(KeyInput.KEY_T)); |
| 76 | + inputManager.addMapping("toggleBpp", new KeyTrigger(KeyInput.KEY_Y)); |
| 77 | + |
| 78 | + ActionListener listener = new ActionListener() { |
| 79 | + @Override |
| 80 | + public void onAction(String name, boolean keyPressed, float tpf) { |
| 81 | + if (name.equals("moreSamples") && keyPressed) { |
| 82 | + moreSamples(); |
| 83 | + } else if (name.equals("toggleBpp") && keyPressed) { |
| 84 | + toggleBpp(); |
| 85 | + } else if (name.equals("toggleDepth") && keyPressed) { |
| 86 | + toggleDepth(); |
| 87 | + } else if (name.equals("windowedMode") && keyPressed) { |
| 88 | + windowedMode(); |
| 89 | + } |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + inputManager.addListener(listener, |
| 94 | + "moreSamples", "toggleBpp", "toggleDepth", "windowedMode"); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Restart the app, requesting 2 more MSAA samples per pixel. |
| 99 | + */ |
| 100 | + private void moreSamples() { |
| 101 | + int numSamples = settings.getSamples(); |
| 102 | + numSamples += 2; |
| 103 | + System.out.println("numSamples = " + numSamples); |
| 104 | + settings.setSamples(numSamples); |
| 105 | + setSettings(settings); |
| 106 | + |
| 107 | + restart(); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Restart the app, requesting a different number of bits per pixel in the |
| 112 | + * RGB buffer. |
| 113 | + */ |
| 114 | + private void toggleBpp() { |
| 115 | + int bpp = settings.getBitsPerPixel(); |
| 116 | + bpp = (bpp == 24) ? 16 : 24; |
| 117 | + System.out.println("BPP = " + bpp); |
| 118 | + settings.setBitsPerPixel(bpp); |
| 119 | + setSettings(settings); |
| 120 | + |
| 121 | + restart(); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Restart the app, requesting a different number of bits per pixel in the |
| 126 | + * depth buffer. |
| 127 | + */ |
| 128 | + private void toggleDepth() { |
| 129 | + int depthBits = settings.getDepthBits(); |
| 130 | + depthBits = (depthBits == 24) ? 16 : 24; |
| 131 | + System.out.println("depthBits = " + depthBits); |
| 132 | + settings.setDepthBits(depthBits); |
| 133 | + setSettings(settings); |
| 134 | + |
| 135 | + restart(); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * If the app is in fullscreen mode, restart it in 640x480 windowed mode. |
| 140 | + */ |
| 141 | + private void windowedMode() { |
| 142 | + boolean isFullscreen = settings.isFullscreen(); |
| 143 | + if (!isFullscreen) { |
| 144 | + System.out.println("Request ignored: already in windowed mode!"); |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + System.out.println("fullscreen = " + false); |
| 149 | + settings.setFullscreen(false); |
| 150 | + settings.setWidth(640); |
| 151 | + settings.setHeight(480); |
| 152 | + setSettings(settings); |
| 153 | + |
| 154 | + restart(); |
| 155 | + } |
| 156 | +} |
0 commit comments