summaryrefslogtreecommitdiffstats
path: root/src/demos/readbuffer/Main.java
blob: c6ecefa15fd8ede1070526b7107b3e1112844ab3 (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
package demos.readbuffer;

import java.lang.reflect.*;
import javax.media.opengl.*;
import javax.media.nativewindow.*;
import com.jogamp.newt.*;
import com.jogamp.newt.opengl.*;

public class Main implements WindowListener, MouseListener, SurfaceUpdatedListener {

    public boolean quit = false;
    public GLWindow window = null;

    public void surfaceUpdated(Object updater, NativeWindow window, long when) {
        if(null!=window) {
            this.window.display();
        }
    }

    public void windowResized(WindowEvent e) { }
    public void windowMoved(WindowEvent e) { }
    public void windowGainedFocus(WindowEvent e) { }
    public void windowLostFocus(WindowEvent e) { }
    public void windowDestroyNotify(WindowEvent e) {
        quit = true;
    }

    public void mouseClicked(MouseEvent e) {
        System.out.println("mouseevent: "+e);
        switch(e.getClickCount()) {
            case 1:
                if(null!=window) {
                    window.setFullscreen(!window.isFullscreen());
                }
                break;
            default: 
                quit=true;
                break;
        }
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
    public void mousePressed(MouseEvent e) {
    }
    public void mouseReleased(MouseEvent e) {
    }
    public void mouseMoved(MouseEvent e) {
    }
    public void mouseDragged(MouseEvent e) {
    }
    public void mouseWheelMoved(MouseEvent e) {
    }

    public GLWindow createOffscreen(GLCapabilities caps, int w, int h, boolean pbuffer) {
        GLCapabilities capsOffscreen = (GLCapabilities) caps.clone();
        capsOffscreen.setOnscreen(false);
        capsOffscreen.setPBuffer(pbuffer);
        capsOffscreen.setDoubleBuffered(false);
        GLWindow windowOffscreen = GLWindow.create(capsOffscreen);
        windowOffscreen.enablePerfLog(true);
        windowOffscreen.setSize(w, h);
        windowOffscreen.setVisible(true);
        return windowOffscreen;
    }

    private void run(String glProfileStr, int typeNewt, boolean fullscreen, int typeTest, GLEventListener demo, boolean pbuffer) {
        GLProfile glp = GLProfile.get(glProfileStr);
        int width = 800;
        int height = 480;
        System.out.println("readbuffer.Main.run() Test: "+typeTest);
        try {
            GLCapabilities caps = new GLCapabilities(glp);

            // Full init pbuffer window ..
            GLWindow windowOffscreen = createOffscreen(caps, width, height, pbuffer);
            // setField(demo, "glDebug", new Boolean(true));
            // setField(demo, "glTrace", new Boolean(true));
            if(!setField(demo, "window", windowOffscreen)) {
                setField(demo, "glWindow", windowOffscreen);
            }
            windowOffscreen.addGLEventListener(demo);

            if ( TEST_SURFACE2FILE < typeTest ) {
                System.out.println("readbuffer.Main.run() Using a target onscreen window with read drawable attachment");
                // Setup init onscreen window ..
                Window nWindow = null;
                if(0!=(typeNewt&USE_AWT)) {
                    Display nDisplay = NewtFactory.createDisplay(NativeWindowFactory.TYPE_AWT, null); // local display
                    Screen nScreen  = NewtFactory.createScreen(NativeWindowFactory.TYPE_AWT, nDisplay, 0); // screen 0
                    nWindow = NewtFactory.createWindow(NativeWindowFactory.TYPE_AWT, nScreen, caps);
                    window = GLWindow.create(nWindow);
                } else {
                    window = GLWindow.create(caps);
                }

                window.addWindowListener(this);
                window.addMouseListener(this);

                window.setSize(width, height);
                window.setFullscreen(fullscreen);
                window.setVisible(true);
            }

            GLDrawable readDrawable = windowOffscreen.getContext().getGLDrawable() ;

            if ( TEST_SURFACE2FILE == typeTest ) {
                Surface2File s2f = new Surface2File();
                windowOffscreen.addSurfaceUpdatedListener(s2f);
            } else if ( TEST_READBUFFER2FILE == typeTest ) {
                ReadBuffer2File readDemo = new ReadBuffer2File( readDrawable ) ;
                window.addGLEventListener(readDemo);
            } else if ( TEST_READBUFFER2SCREEN == typeTest ) {
                ReadBuffer2Screen readDemo = new ReadBuffer2Screen( readDrawable ) ;
                window.addGLEventListener(readDemo);
            }

            if(null!=window) {
                windowOffscreen.addSurfaceUpdatedListener(this);
            }

            System.out.println("+++++++++++++++++++++++++++");
            System.out.println(windowOffscreen);
            System.out.println("+++++++++++++++++++++++++++");

            while ( !quit && windowOffscreen.getDuration() < 31000) {
                // System.out.println("...............................");
                windowOffscreen.display();
            }

            // Shut things down cooperatively
            window.destroy();
            windowOffscreen.destroy();
            window.getFactory().shutdown();
            System.out.println("readbuffer.Main shut down cleanly.");
        } catch (GLException e) {
            e.printStackTrace();
        }
    }

    public static int USE_NEWT      = 0;
    public static int USE_AWT       = 1 << 0;

    public static int TEST_SURFACE2FILE      = 0;
    public static int TEST_READBUFFER2FILE   = 1;
    public static int TEST_READBUFFER2SCREEN = 2;

    public static void main(String[] args) {
        String glProfileStr = null;
        boolean fullscreen = false;
        boolean pbuffer = true;
        int typeNewt = USE_NEWT ;
        int typeTest = TEST_SURFACE2FILE;
        int i=0;
        while(i<args.length-1) {
            if(args[i].equals("-awt")) {
                typeNewt |= USE_AWT; 
            } else if(args[i].equals("-fs")) {
                fullscreen = true;
            } else if(args[i].equals("-nopbuffer")) {
                pbuffer = false;
            } else if(args[i].equals("-test")) {
                i++;
                typeTest = str2int(args[i], typeTest);
            } else if(args[i].startsWith("-GL")) {
                glProfileStr = args[i].substring(1);
            }
            i++;
        }
        String demoClassName = args[i];
        Object demoObject = null;

        try {
            Class demoClazz = Class.forName(demoClassName);
            demoObject = demoClazz.newInstance();
        } catch (Throwable t) {
            t.printStackTrace();
            throw new RuntimeException("Error while instantiating demo: "+demoClassName);
        }
        if( !(demoObject instanceof GLEventListener) ) {
            throw new RuntimeException("Not a GLEventListener: "+demoClassName);
        }
        GLEventListener demo = (GLEventListener) demoObject;

        new Main().run(glProfileStr, typeNewt, fullscreen, typeTest, demo, pbuffer);
        System.exit(0);
    }

    public static int str2int(String str, int def) {
        try {
            return Integer.parseInt(str);
        } catch (Exception ex) { ex.printStackTrace(); }
        return def;
    }

    public static boolean setField(Object instance, String fieldName, Object value) {
        try {
            Field f = instance.getClass().getField(fieldName);
            if(value instanceof Boolean || f.getType().isInstance(value)) {
                f.set(instance, value);
                return true;
            } else {
                System.out.println(instance.getClass()+" '"+fieldName+"' field not assignable with "+value.getClass()+", it's a: "+f.getType());
            }
        } catch (NoSuchFieldException nsfe) {
            System.out.println(instance.getClass()+" has no '"+fieldName+"' field");
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return false;
    }

}