Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  FSEM  (Read 1495 times)
0 Members and 1 Guest are viewing this topic.
Offline Coinerson

Junior Member




Introducing the world's cutest zombie, Timmy


« Posted 2006-07-08 04:04:07 »

Till now my games have been either windowed or FSEM. How can i switch back and fourth. My code fails.... miserably.


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  
public void setFullScreen(DisplayMode displayMode) {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setUndecorated(true);
        frame.setIgnoreRepaint(true);
        frame.setResizable(false);

        device.setFullScreenWindow(frame);

        if (displayMode != null && device.isDisplayChangeSupported()) {
            try {
                device.setDisplayMode(displayMode);
            } catch (IllegalArgumentException ex) { }
            // fix for mac os x
           frame.setSize(displayMode.getWidth(),
                displayMode.getHeight());
        }
        // Avoid potential deadlock in 1.4.1_02
       try {
            EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    frame.createBufferStrategy(NUM_BUFFERS);
                }
            });
        }
        catch (InterruptedException ex) {
            // ignore
       }
        catch (InvocationTargetException  ex) {
            // ignore
       }
    }



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  
   public void restoreScreen() {
        //Window window = device.getFullScreenWindow();
       //if (window != null) {
           //window.dispose();
       //}
     
      final JFrame frame = (JFrame)device.getFullScreenWindow();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setUndecorated(true);
        frame.setIgnoreRepaint(true);
        frame.setResizable(false);

      DisplayMode displayMode = getCurrentDisplayMode();
        if (displayMode != null && device.isDisplayChangeSupported()) {
            try {
                device.setDisplayMode(displayMode);
            } catch (IllegalArgumentException ex) { }
            // fix for mac os x
           frame.setSize(displayMode.getWidth(),
                displayMode.getHeight());
        }
        // Avoid potential deadlock in 1.4.1_02
       try {
            EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    frame.createBufferStrategy(NUM_BUFFERS);
                }
            });
        }
        catch (InterruptedException ex) {
            // ignore
       }
        catch (InvocationTargetException  ex) {
            // ignore
       }
     
     
        device.setFullScreenWindow(null);


There are a pair of methods in my screenmanager class. Here is what is in my manager:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public void windowedMode()   {
      fullscreen = false;
      screen.restoreScreen();
   }
   
   //Set to FSEM
  public void fullscreenMode()   {
      fullscreen = true;
      DisplayMode displayMode =
            screen.findFirstCompatibleMode(POSSIBLE_MODES);
        screen.setFullScreen(displayMode);
     
   }


screen is an instance of the screenManager. I feel stupid :\
Offline CommanderKeith
« Reply #1 - Posted 2006-07-08 04:24:21 »

I use the below code inside a class that extends JFrame to switch between Full screen & window mode in the middle of the game (after pausing the game loop rendering thread) :

It works mostly in Java 1.5, but sometimes it caused a VM crash with a hs_pid output file etc.  On Mustang it fails almost always, it only works sometimes.  I'm not sure why.

public void setFullScreen(boolean fullScreen) throws IllegalArgumentException{
       if (isFullScreen() == fullScreen){
          // we're already in fullscreen, so return.
          return;
       }
         if (fullScreen){
            setResizable(false);
            device.setFullScreenWindow(this);
            if (displayMode != null && device.isDisplayChangeSupported()){
               try{
                  device.setDisplayMode(displayMode);
                  System.out.println("display mode set");
               }
               catch(IllegalArgumentException e){
                  System.out.println("display mode not supported");
                  device.setFullScreenWindow(null);
                  throw e;
               }
            }else{
               device.setFullScreenWindow(null);
               throw new IllegalArgumentException("device.isDisplayChangeSupported() == false");
            }
         }
         else{
            device.setFullScreenWindow(null);
            setResizable(true);
         }
         createBufferStrategy(Main.getPlayingController().getView().getNumBuffers());
    }

    public boolean isFullScreen(){
         if (device.getFullScreenWindow() == null){
            return false;
         }
         return true;
    }

Offline Coinerson

Junior Member




Introducing the world's cutest zombie, Timmy


« Reply #2 - Posted 2006-07-08 06:11:18 »

Hrm, havn't tried it yet but it seems like we are doing the same thing, or I was doing that then I elaborated.

What happens when I move back to windowed mode? Is it simply resetting the display mode? When I do that i get an empty window.

I am guessing you do key detection in your subclass of JPanel? I do mine in the Subclass of canvas so its more convenient to have the code in the canvas.

You seem to just be setting the fullscreen window to nothing, then creating a new buffer strategy. That doesn't appear to work over here, I get a window that doesn't have focus and is not being drawn in.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CommanderKeith
« Reply #3 - Posted 2006-07-09 04:55:37 »

I've got a window listener on the JFrame.  Maybe that's what's grabbing the focus back:

public void windowActivated(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}
    public void windowClosing(WindowEvent e){
       close();
    }
    public void windowDeactivated(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowOpened(WindowEvent e) {
       if (Main.getPlayingController().getView() != null){
          Main.getPlayingController().getView().requestFocus();
       }
    }


Offline Coinerson

Junior Member




Introducing the world's cutest zombie, Timmy


« Reply #4 - Posted 2006-07-10 04:31:48 »

That Isn't it.

Here is my subclass of JFrame

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  
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class RunnerApp extends JFrame   {
   public static final int WIDTH = 640, HEIGHT = 480;
   public static GameManager manager;
   
   public static void main(String[] args)   {
      RunnerApp frame = new RunnerApp();
      manager = new GameManager();
      manager.setPreferredSize(new Dimension(WIDTH, HEIGHT));
     
      frame.add(manager);
      frame.pack();
      frame.setResizable(false);
     
      manager.init();
      frame.setVisible(true);
      manager.start();
   }
   
   public RunnerApp()   {
      super("Game");
      addWindowListener(new WinHandler());
   }
   
   private class WinHandler extends WindowAdapter   {
      public void windowClosing(WindowEvent e)   {
         manager.stopGame();
      }
     
      public void windowActivated(WindowEvent e)   {
         manager.setPaused(false);
      }
     
      public void windowDeactivated(WindowEvent e)   {
         manager.setPaused(true);
      }
     
      public void windowOpened(WindowEvent e)   {
         requestFocus();
      }
   }
}


And my "manager" class

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  
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.sound.sampled.AudioFormat;
import javax.sound.midi.*;

import java.io.*;
import javax.sound.sampled.*;
import java.text.DecimalFormat;
import java.net.URL;


public class GameManager extends GameCanvas {
   private static final int REFRESH_RATE =
        DisplayMode.REFRESH_RATE_UNKNOWN;
    protected static final DisplayMode POSSIBLE_MODES[] = {
        new DisplayMode(800, 600, 32, REFRESH_RATE),
        new DisplayMode(800, 600, 24, REFRESH_RATE),
        new DisplayMode(800, 600, 16, REFRESH_RATE),
        new DisplayMode(640, 480, 32, REFRESH_RATE),
        new DisplayMode(640, 480, 24, REFRESH_RATE),
        new DisplayMode(640, 480, 16, REFRESH_RATE),
    };
   private ScreenManager screen;
   private boolean fullscreen;

    private int fps;          // frames per second
  private ResourceManager resourceManager;
   private KeyState keys; //keys
 
   public GameManager()   {
      addKeyListener(new KeyHandler());
   }
   
   // Driver to start the game
   public static void main(String[] args) {
        RunnerFullScreen canvas = new RunnerFullScreen();
        canvas.start();
    }
   
   // Set full-screen mode and start game
   public void start() {
        screen = new ScreenManager();
        DisplayMode displayMode =
            screen.findFirstCompatibleMode(POSSIBLE_MODES);
        screen.setFullScreen(displayMode);
      fullscreen = true;

        // Add canvas after entering FSEM and changing mode
       JFrame frame = screen.getFullScreenWindow();
        frame.add(this);  // add components
       frame.validate(); // layout components

        init();
        startGame();
    }
   
    public void init() {
        // Load images and create sprites
     resourceManager = new ResourceManager(GraphicsEnvironment
            .getLocalGraphicsEnvironment()
            .getDefaultScreenDevice()
            .getDefaultConfiguration());
    }

    public void Update(long elapsedTime) {
        if (elapsedTime > 0) { // update frame rate
           fps = (int) (1000L / elapsedTime);
        }
    }

   //Restore to windowed screen
  public void windowedMode()   {
      fullscreen = false;
      screen.restoreScreen();
      createBufferStrategy(NUM_BUFFERS);
   }
   
   //Set to FSEM
  public void fullscreenMode()   {
      fullscreen = true;
      DisplayMode displayMode =
            screen.findFirstCompatibleMode(POSSIBLE_MODES);
        screen.setFullScreen(displayMode);
     
   }

   // Override superclass to restore the screen
   public void exitGame() {
        screen.restoreScreen();
      fullscreen = false;
        super.exitGame();
    }
   
    public void draw(Graphics g) {
        if (g instanceof Graphics2D) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(
                RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        }

        // Render game here
       g.setColor(Color.GREEN);
        g.fillRect(0, 0, getWidth(), getHeight());

        // Display data
       g.setColor(Color.WHITE);
        g.setFont(new Font("Dialog", Font.PLAIN, 18));
        g.drawString("FPS: " + fps, getWidth() - 90, getHeight() - 10);
      BufferedImage image;
      for (int i = 0 ; i < 11 ; i++ )   {
         for (int x = 0; x < 18 ; x++)   {
            image = (BufferedImage)resourceManager.getAnims().get(String.valueOf(i) + String.valueOf(x));
            g.drawImage(image, (x*32)+5, (i*32)+5, null);
         }
      }
    }
   
   
   
   private class KeyHandler extends KeyAdapter   {
      public void keyPressed(KeyEvent e)   {
         int keyCode = e.getKeyCode();
      }
      public void keyReleased(KeyEvent e)   {
         int keyCode = e.getKeyCode();
         if (keyCode == KeyEvent.VK_M)   {
            if (fullscreen)   {
               windowedMode();
            }
            else   {
               fullscreenMode();
            }
         }
      }
   }
   
}


And I already posted my screen manager.
Offline CommanderKeith
« Reply #5 - Posted 2006-07-11 03:23:15 »

Quote
private class WinHandler extends WindowAdapter   {
      public void windowClosing(WindowEvent e)   {
         manager.stopGame();
      }
      
      public void windowActivated(WindowEvent e)   {
         manager.setPaused(false);
      }
      
      public void windowDeactivated(WindowEvent e)   {
         manager.setPaused(true);
      }
      
      public void windowOpened(WindowEvent e)   {
         requestFocus();
      }
   }
Well I don't know what's causing that behaviour.  But make sure its not your own code in the above... you could be pausing the game & never restarting it... that would explain an empty window.

Offline Coinerson

Junior Member




Introducing the world's cutest zombie, Timmy


« Reply #6 - Posted 2006-07-21 22:12:33 »

None of that gets called I just realized by adding lots of println lines. It calls activate, opened, deactivated, then it detects my keypress (that triggers the toggle) and no more window events get called. So it isnt a true RunnerApp.
Offline CommanderKeith
« Reply #7 - Posted 2006-07-22 03:08:59 »

I still haven't figured out how to reliably switch to full-screen mode midway through the game  Tongue.

There must be something simple we're doing wrong, maybe someone can lend us some pointers?

Offline CommanderKeith
« Reply #8 - Posted 2006-08-09 07:36:28 »

I've now got full screen mode switching (back & forth, mid-game) working well - the problem for me was the change of bit-depth. 

Now I just keep the same bit depth as it was in windowed mode and there's no problem.  I was changing to 16-bit depth and by default my setup is 32.  Maybe that will solve it for you too?

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (41 views)
2014-09-24 16:13:29

Pippogeek (32 views)
2014-09-24 16:12:22

Pippogeek (22 views)
2014-09-24 16:12:06

Grunnt (47 views)
2014-09-23 14:38:19

radar3301 (30 views)
2014-09-21 23:33:17

BurntPizza (65 views)
2014-09-21 02:42:18

BurntPizza (37 views)
2014-09-21 01:30:30

moogie (44 views)
2014-09-21 00:26:15

UprightPath (53 views)
2014-09-20 20:14:06

BurntPizza (55 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!