aboutsummaryrefslogtreecommitdiffstats
path: root/src/jake2/render/opengl/NEWTWin.java
blob: ff7b94931e0337ff04e836b3ee431eb12ab2131f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * NEWTWin.java
 * Copyright (C) 2004
 * 
 */
package jake2.render.opengl;

import jake2.Defines;
import jake2.Globals;
import jake2.SizeChangeListener;
import jake2.client.VID;
import jake2.game.cvar_t;
import jake2.qcommon.Cbuf;
import jake2.qcommon.Cvar;
import jake2.render.Base;
import jake2.sys.NEWTKBD;

import java.util.List;

import javax.media.nativewindow.CapabilitiesChooser;
import javax.media.nativewindow.WindowClosingProtocol.WindowClosingMode;
import javax.media.nativewindow.util.Dimension;
import javax.media.nativewindow.util.DimensionImmutable;
import javax.media.nativewindow.util.SurfaceSize;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLProfile;

import jogamp.opengl.FPSCounterImpl;

import com.jogamp.common.os.Platform;
import com.jogamp.newt.*;
import com.jogamp.newt.event.WindowAdapter;
import com.jogamp.newt.event.WindowEvent;
import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.newt.util.MonitorModeUtil;
import com.jogamp.opengl.GenericGLCapabilitiesChooser;

public class NEWTWin {
    MonitorMode oldDisplayMode = null;
    volatile Screen screen = null;
    volatile GLWindow window = null;
    volatile boolean shouldQuit = false;
    final FPSCounterImpl fpsCounter = new FPSCounterImpl();

    public List<MonitorMode> getModeList() {
        if( null != window ) {
            final MonitorDevice mainMonitor = window.getMainMonitor();
            return mainMonitor.getSupportedModes();
        } else {        
            return screen.getMonitorModes();
        }
    }

    public MonitorMode findDisplayMode(DimensionImmutable dim) {
        final List<MonitorMode> sml = MonitorModeUtil.filterByResolution(getModeList(), dim);
        if(sml.size() == 0) {
            return oldDisplayMode;
        }
        return sml.get(0);
    }

    public String getModeString(MonitorMode mm) {
        final SurfaceSize ss = mm.getSurfaceSize();
        final DimensionImmutable m = ss.getResolution();
        final StringBuffer sb = new StringBuffer();
        sb.append(m.getWidth());
        sb.append('x');
        sb.append(m.getHeight());
        sb.append('x');
        sb.append(ss.getBitsPerPixel());
        sb.append('@');
        sb.append(mm.getRefreshRate());
        sb.append("Hz");
        return sb.toString();
    }

    /**
     * @param dim
     * @param mode
     * @param fullscreen
     * @param driverName TODO
     * @return enum Base.rserr_t
     */
    public int setMode(GLProfile glp, Dimension dim, int mode, boolean fullscreen, String driverName) {
        final boolean isARM = Platform.CPUFamily.ARM == Platform.getCPUFamily();

        final Dimension newDim = new Dimension();

        VID.Printf(Defines.PRINT_ALL, "Initializing OpenGL display for profile "+glp+"\n");

        if(null == screen) {
            screen = NewtFactory.createScreen(NewtFactory.createDisplay(null), 0);
            screen.addReference(); // trigger native creation
        } else if( !screen.isNativeValid() ) {
            screen.addReference(); // trigger native creation
        }
        
        if (!VID.GetModeInfo(newDim, mode)) {
            VID.Printf(Defines.PRINT_ALL, " invalid mode\n");
            return Base.rserr_invalid_mode;
        }

        VID.Printf(Defines.PRINT_ALL, "...setting mode " + mode + ", " + newDim.getWidth() + " x " + newDim.getHeight() + ", fs " + fullscreen + ", driver " + driverName + "\n");

        // destroy the existing window, not screen
        shutdownImpl(false);
        
        if(null != window) {
            throw new InternalError("XXX");            
        }
        final GLCapabilities caps = new GLCapabilities(glp);
        CapabilitiesChooser chooser = null; // default
        {
            final cvar_t v = Cvar.Get("jogl_rgb565", "0", 0);
            if( v.value != 0f ) {
                caps.setRedBits(5);
                caps.setGreenBits(6);
                caps.setBlueBits(5);
                chooser = new GenericGLCapabilitiesChooser(); // don't trust native GL-TK chooser
            }
        }
        window = GLWindow.create(screen, caps);
        window.setDefaultCloseOperation(WindowClosingMode.DO_NOTHING_ON_CLOSE); // we do handle QUIT on our own, no GLWindow.display() called.
        window.setCapabilitiesChooser(chooser);
        window.addWindowListener(new WindowAdapter() {
            public void windowDestroyNotify(WindowEvent e) {
                shouldQuit = !Globals.appletMode && null != window; // not applet and not already in shutdown ?
            }

            public void windowResized(WindowEvent e) {
                propagateNewSize();
            }
        });
        window.setTitle("Jake2 ("+driverName+"-newt-"+glp.getName().toLowerCase()+")");

        final MonitorDevice mainMonitor = window.getMainMonitor();
        
        if (oldDisplayMode == null) {
            oldDisplayMode = mainMonitor.getCurrentMode();
        }

        // We need to feed the NEWT Window to the NEWTKBD
        NEWTKBD.Init(window);
        
        window.addWindowListener(NEWTKBD.listener);
        window.addKeyListener(NEWTKBD.listener);
        window.addMouseListener(NEWTKBD.listener);

        if (fullscreen) {
            MonitorMode mm = findDisplayMode(newDim);
            final DimensionImmutable smDim = mm.getSurfaceSize().getResolution();
            newDim.setWidth( smDim.getWidth() );
            newDim.setHeight( smDim.getHeight() );
            mainMonitor.setCurrentMode(mm);
            window.setFullscreen(true);
        } else {
            window.setSize(newDim.getWidth(), newDim.getHeight());
            if (Globals.appletMode) {
                // Notify the size listener about the change
                final SizeChangeListener listener = Globals.sizeChangeListener;
                if (listener != null) {
                    listener.sizeChanged(newDim.getWidth(), newDim.getHeight());
                }
            }
        }
        window.setVisible(true);

        if (!Globals.appletMode) {
            while ( !window.isNativeValid()|| !window.isRealized() ) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {}
            }
        }
        window.requestFocus();
        window.display(); // force GL resource validation
        window.setAutoSwapBufferMode(false);

        VID.Printf(Defines.PRINT_ALL, "...reques GLCaps "+window.getRequestedCapabilities()+'\n');
        VID.Printf(Defines.PRINT_ALL, "...chosen GLCaps "+window.getChosenGLCapabilities()+'\n');
        VID.Printf(Defines.PRINT_ALL, "...size "+window.getWidth()+" x "+window.getHeight()+'\n');
        fpsCounter.setUpdateFPSFrames(isARM ? 60 : 4*60, System.err);

        // propagateNewSize("init");
        activateGLContext();
        
        return Base.rserr_ok;
    }
    
    private void propagateNewSize() {
        final int width = window.getWidth();
        final int height = window.getHeight();
        final int _width;
        final int mask = ~0x03;
        if ((width & 0x03) != 0) {
            _width = ( width & mask ) + 4;
        } else {
            _width = width;
        }
        VID.Printf(Defines.PRINT_ALL, "Resize: " + width + " x " + height + ", masked " + _width + "x" + height + "\n");  

        Base.setVid(_width, height);
        // let the sound and input subsystems know about the new window
        VID.NewWindow(_width, height);                    
    }

    protected final void activateGLContext() {
        final GLContext ctx = window.getContext();
        if ( null != ctx && GLContext.getCurrent() != ctx ) {                
            ctx.makeCurrent();
        }
    }
    
    protected final void deactivateGLContext() {
        final GLContext ctx = window.getContext();
        if ( null != ctx && GLContext.getCurrent() == ctx) {
            ctx.release();
        }        
    }
    
    /** Performs {@link GLWindow#swapBuffers()}, ticks the fps counter and performs <code>QUIT</code> if requested. */
    public final void endFrame() {
        window.swapBuffers();
        fpsCounter.tickFPS();
        if( shouldQuit ) {
            deactivateGLContext();
            Cbuf.ExecuteText(Defines.EXEC_APPEND, "quit");
        }
    }
    /** Performs <code>QUIT</code> if requested. */
    public final void checkQuit() {
        if( shouldQuit ) {
            deactivateGLContext();
            Cbuf.ExecuteText(Defines.EXEC_APPEND, "quit");
        }
    }
    
    void shutdown() {
        shutdownImpl(true);
    }
    
    private void shutdownImpl(boolean withScreen) {
        if ( null != window ) {
            deactivateGLContext();
            final GLWindow _window = window;
            window = null;
            _window.destroy(); // same thing
        }
        if( withScreen && null != screen ) {
            screen.destroy();
            screen = null;
        }
    }    
    
}