Jogamp
bc514ed0a857d7f82113521884de32f3a1db94e3
[users/sgothel/jake2.git] / src / jake2 / render / opengl / NEWTWin.java
1 /*
2  * NEWTWin.java
3  * Copyright (C) 2004
4  * 
5  */
6 package jake2.render.opengl;
7
8 import jake2.Defines;
9 import jake2.Globals;
10 import jake2.SizeChangeListener;
11 import jake2.client.VID;
12 import jake2.game.cvar_t;
13 import jake2.qcommon.Cbuf;
14 import jake2.qcommon.Cvar;
15 import jake2.render.Base;
16 import jake2.sys.NEWTKBD;
17
18 import java.util.List;
19
20 import javax.media.nativewindow.CapabilitiesChooser;
21 import javax.media.nativewindow.util.Dimension;
22 import javax.media.nativewindow.util.DimensionImmutable;
23 import javax.media.nativewindow.util.SurfaceSize;
24 import javax.media.opengl.GLCapabilities;
25 import javax.media.opengl.GLContext;
26 import javax.media.opengl.GLProfile;
27
28 import jogamp.opengl.FPSCounterImpl;
29
30 import com.jogamp.common.os.Platform;
31 import com.jogamp.newt.*;
32 import com.jogamp.newt.event.WindowAdapter;
33 import com.jogamp.newt.event.WindowEvent;
34 import com.jogamp.newt.opengl.GLWindow;
35 import com.jogamp.newt.util.MonitorModeUtil;
36 import com.jogamp.opengl.GenericGLCapabilitiesChooser;
37
38 public class NEWTWin {
39     MonitorMode oldDisplayMode = null;
40     volatile Screen screen = null;
41     volatile GLWindow window = null;
42     final FPSCounterImpl fpsCounter = new FPSCounterImpl();
43
44     public List<MonitorMode> getModeList() {
45         if( null != window ) {
46             final MonitorDevice mainMonitor = window.getMainMonitor();
47             return mainMonitor.getSupportedModes();
48         } else {        
49             return screen.getMonitorModes();
50         }
51     }
52
53     public MonitorMode findDisplayMode(DimensionImmutable dim) {
54         final List<MonitorMode> sml = MonitorModeUtil.filterByResolution(getModeList(), dim);
55         if(sml.size() == 0) {
56             return oldDisplayMode;
57         }
58         return sml.get(0);
59     }
60
61     public String getModeString(MonitorMode mm) {
62         final SurfaceSize ss = mm.getSurfaceSize();
63         final DimensionImmutable m = ss.getResolution();
64         final StringBuffer sb = new StringBuffer();
65         sb.append(m.getWidth());
66         sb.append('x');
67         sb.append(m.getHeight());
68         sb.append('x');
69         sb.append(ss.getBitsPerPixel());
70         sb.append('@');
71         sb.append(mm.getRefreshRate());
72         sb.append("Hz");
73         return sb.toString();
74     }
75
76     /**
77      * @param dim
78      * @param mode
79      * @param fullscreen
80      * @param driverName TODO
81      * @return enum Base.rserr_t
82      */
83     public int setMode(GLProfile glp, Dimension dim, int mode, boolean fullscreen, String driverName) {
84         final boolean isARM = Platform.CPUFamily.ARM == Platform.getCPUFamily();
85
86         final Dimension newDim = new Dimension();
87
88         VID.Printf(Defines.PRINT_ALL, "Initializing OpenGL display for profile "+glp+"\n");
89
90         if(null == screen) {
91             screen = NewtFactory.createScreen(NewtFactory.createDisplay(null), 0);
92             screen.addReference(); // trigger native creation
93         } else if( !screen.isNativeValid() ) {
94             screen.addReference(); // trigger native creation
95         }
96         
97         if (!VID.GetModeInfo(newDim, mode)) {
98             VID.Printf(Defines.PRINT_ALL, " invalid mode\n");
99             return Base.rserr_invalid_mode;
100         }
101
102         VID.Printf(Defines.PRINT_ALL, "...setting mode " + mode + ", " + newDim.getWidth() + " x " + newDim.getHeight() + ", fs " + fullscreen + ", driver " + driverName + "\n");
103
104         // destroy the existing window, not screen
105         shutdownImpl(false);
106         
107         if(null != window) {
108             throw new InternalError("XXX");            
109         }
110         final GLCapabilities caps = new GLCapabilities(glp);
111         CapabilitiesChooser chooser = null; // default
112         {
113             final cvar_t v = Cvar.Get("jogl_rgb565", "0", 0);
114             if( v.value != 0f ) {
115                 caps.setRedBits(5);
116                 caps.setGreenBits(6);
117                 caps.setBlueBits(5);
118                 chooser = new GenericGLCapabilitiesChooser(); // don't trust native GL-TK chooser
119             }
120         }
121         window = GLWindow.create(screen, caps);
122         window.setCapabilitiesChooser(chooser);
123         window.addWindowListener(new WindowAdapter() {
124             public void windowDestroyNotify(WindowEvent e) {
125                 if (!Globals.appletMode) {
126                     if( null != window ) { // already in shutdown ?
127                         Cbuf.ExecuteText(Defines.EXEC_APPEND, "quit");
128                     }
129                 }
130             }
131
132             public void windowResized(WindowEvent e) {
133                 propagateNewSize();
134             }
135         });
136         window.setTitle("Jake2 ("+driverName+"-newt-"+glp.getName().toLowerCase()+")");
137
138         final MonitorDevice mainMonitor = window.getMainMonitor();
139         
140         if (oldDisplayMode == null) {
141             oldDisplayMode = mainMonitor.getCurrentMode();
142         }
143
144         // We need to feed the NEWT Window to the NEWTKBD
145         NEWTKBD.Init(window);
146         
147         window.addWindowListener(NEWTKBD.listener);
148         window.addKeyListener(NEWTKBD.listener);
149         window.addMouseListener(NEWTKBD.listener);
150
151         if (fullscreen) {
152             MonitorMode mm = findDisplayMode(newDim);
153             final DimensionImmutable smDim = mm.getSurfaceSize().getResolution();
154             newDim.setWidth( smDim.getWidth() );
155             newDim.setHeight( smDim.getHeight() );
156             mainMonitor.setCurrentMode(mm);
157             window.setFullscreen(true);
158         } else {
159             window.setSize(newDim.getWidth(), newDim.getHeight());
160             if (Globals.appletMode) {
161                 // Notify the size listener about the change
162                 final SizeChangeListener listener = Globals.sizeChangeListener;
163                 if (listener != null) {
164                     listener.sizeChanged(newDim.getWidth(), newDim.getHeight());
165                 }
166             }
167         }
168         window.setVisible(true);
169
170         if (!Globals.appletMode) {
171             while ( !window.isNativeValid()|| !window.isRealized() ) {
172                 try {
173                     Thread.sleep(100);
174                 } catch (InterruptedException e) {}
175             }
176         }
177         window.requestFocus();
178         window.display(); // force GL resource validation
179         window.setAutoSwapBufferMode(false);
180
181         VID.Printf(Defines.PRINT_ALL, "...reques GLCaps "+window.getRequestedCapabilities()+'\n');
182         VID.Printf(Defines.PRINT_ALL, "...chosen GLCaps "+window.getChosenGLCapabilities()+'\n');
183         VID.Printf(Defines.PRINT_ALL, "...size "+window.getWidth()+" x "+window.getHeight()+'\n');
184         fpsCounter.setUpdateFPSFrames(isARM ? 60 : 4*60, System.err);
185
186         // propagateNewSize("init");
187         activateGLContext();
188         
189         return Base.rserr_ok;
190     }
191     
192     private void propagateNewSize() {
193         final int width = window.getWidth();
194         final int height = window.getHeight();
195         final int _width;
196         final int mask = ~0x03;
197         if ((width & 0x03) != 0) {
198             _width = ( width & mask ) + 4;
199         } else {
200             _width = width;
201         }
202         VID.Printf(Defines.PRINT_ALL, "Resize: " + width + " x " + height + ", masked " + _width + "x" + height + "\n");  
203
204         Base.setVid(_width, height);
205         // let the sound and input subsystems know about the new window
206         VID.NewWindow(_width, height);                    
207     }
208
209     protected final void activateGLContext() {
210         final GLContext ctx = window.getContext();
211         if ( null != ctx && GLContext.getCurrent() != ctx ) {                
212             ctx.makeCurrent();
213         }
214     }
215     
216     protected final void deactivateGLContext() {
217         final GLContext ctx = window.getContext();
218         if ( null != ctx && GLContext.getCurrent() == ctx) {
219             ctx.release();
220         }        
221     }
222     
223     void shutdown() {
224         shutdownImpl(true);
225     }
226     
227     private void shutdownImpl(boolean withScreen) {
228         if ( null != window ) {
229             deactivateGLContext();
230             final GLWindow _window = window;
231             window = null;
232             _window.destroy(); // same thing
233         }
234         if( withScreen && null != screen ) {
235             screen.destroy();
236             screen = null;
237         }
238     }    
239     
240 }
http://JogAmp.org git info: FAQ, tutorial and man pages.