Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
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  
  Java Sound API, Java Full Screen Exclusive Mode API & Linux  (Read 3062 times)
0 Members and 1 Guest are viewing this topic.
Offline Virusakos

Senior Newbie





« Posted 2010-06-28 10:39:38 »

Dear all,

I suppose that Linux needs a 'specific' configuration in order for Sound & Full Screen Exclusive Mode APIs to work.
Specifically, when using Sound API I get a MidiUnavailableException because Java is trying to play sound through OSS and not through ALSA (I think).
Also, Full Screen Exclusive Mode API, reports that it can enter full screen but cannot change the display mode, which (again I think) there must be something 'wrong' with the configuration of X.

So, does anybody know how to make Sound API & Full Screen Exclusive Mode API work in Linux?

Regards,
Charalampos
Offline delt0r

JGO Coder


Medals: 22


Computers can do that?


« Reply #1 - Posted 2010-06-30 11:59:06 »

The short answer is poorly. I use lwjgl to get around both problems and to be honest the fullscreen mode may "cheat" by just creating a fullscreen undecorated window. Openal is the way i handle sound now for reasons of "just works" on many platforms i have tried..while java sound, even when it worked, had a pretty massive delay.

However depending on your video drivers, if you don't have the mode lines in the xorg.conf then these resolutions will not be available to you. But my experience is that full screen(pure java way) just doesn't work under linux.

I have no special talents. I am only passionately curious.--Albert Einstein
Offline Virusakos

Senior Newbie





« Reply #2 - Posted 2010-07-05 20:58:56 »

Thanks for your reply.

Fullscreen 'cheat' is 'hard' to implement. If you design a game to work in, say, 640x480 mode and the current mode is 1024x768 you have to increase the size of the sprites or provide sprites for that mode. I think I will pass for now. I am too new to games development to go that route.

I have started to look at OpenGL. I was wondering though, can OpenGL handle fullscreen even if xorg.conf doesn't contain the necessary mode lines?

Regards,
Charalampos
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline bobjob

JGO Knight


Medals: 10
Projects: 6


David Aaron Muhar


« Reply #3 - Posted 2010-07-06 06:22:55 »

Fullscreen 'cheat' is 'hard' to implement. If you design a game to work in, say, 640x480 mode and the current mode is 1024x768 you have to increase the size of the sprites or provide sprites for that mode.
a way to cheat the resolution is to render to an image (via Image.getGraphics()). Instead of the default graphics object in the paint method.

then finally scale it on the fly at the end of the paint(Graphics g) method using, g.drawImage(offScreenImage, 0,0,this.getWidth(),this.getHeight(), 0,0, offScreenImage.getWidth(null), offscreenImage.getHeight(null));

My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Offline Virusakos

Senior Newbie





« Reply #4 - Posted 2010-07-06 08:50:37 »

That didn't sound as hard as I was expecting  Tongue
Thanks for the tip  Smiley
Offline bobjob

JGO Knight


Medals: 10
Projects: 6


David Aaron Muhar


« Reply #5 - Posted 2010-07-06 08:56:58 »

Im not sure how experienced you are.

it might be a good idea to look up in google "animation in java applets".
There should be a section of using an offscreen buffer. not only will it give smooth graphics, on the majority oj java versions, but also help you with the issue of scaling on the fly, as you already have an offscreen buffer to scale.

My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Offline Virusakos

Senior Newbie





« Reply #6 - Posted 2010-07-09 22:25:15 »

Well...
I tried the scaling on the fly but I didn't like the results.

Here is the code I tried
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  
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.MemoryImageSource;
import java.awt.image.VolatileImage;

public class FakeFullscreen {
   static boolean isRunning;

   public static void main(String[] args) {
      final Toolkit toolkit = Toolkit.getDefaultToolkit();

      final int[] pixels = new int[16 * 16];
      final Image image = toolkit.createImage(new MemoryImageSource(16, 16, pixels, 0, 16));
      final Cursor invisibleCursor = toolkit.createCustomCursor(image, new Point(0, 0), "invisibleCursor");

      final Dimension screenSize = toolkit.getScreenSize();
      final Dimension gameSize = new Dimension(640, 480);
      final Rectangle gameArea = new Rectangle(0, 80, 640, 400);

      final Canvas canvas = new Canvas();
      canvas.setIgnoreRepaint(true);
      canvas.setSize(screenSize);

      final Frame frame = new Frame();
      frame.setAlwaysOnTop(true);
      frame.setCursor(invisibleCursor);
      frame.setIgnoreRepaint(true);
      frame.setLocation(0, 0);
      frame.setResizable(false);
      frame.setSize(screenSize);
      frame.setUndecorated(true);

      frame.addKeyListener(new KeyAdapter() {
         @Override
         public void keyPressed(KeyEvent event) {
            if (event.getKeyCode() == KeyEvent.VK_ESCAPE) {
               isRunning = false;
            }
         }
      });

      frame.add(canvas);
      frame.pack();
      frame.setVisible(true);

      canvas.createBufferStrategy(2);
      final BufferStrategy buffer = canvas.getBufferStrategy();

      final VolatileImage offScreen = canvas.createVolatileImage(gameSize.width, gameSize.height);
      Graphics2D offScreenGraphics = null;

      Graphics2D graphics = null;
      isRunning = true;

      int fps = 0;
      int frames = 0;
      long totalTime = 0;
      long curTime = System.nanoTime() / 1000000L;
      long lastTime = curTime;

      final int TICKS_PER_SECOND = 25;
      final int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
      final int MAX_FRAMESKIP = 5;

      long nextGameTick = System.nanoTime() / 1000000L;
      int loops;
      float interpolation;

      int ballWidth = 8;
      int ballHeight = 8;
      int ballX = gameArea.width / 2 - ballWidth / 2;
      int ballY = gameArea.y + (gameArea.height / 2 - ballHeight / 2);
      int ballDx = 10;
      int ballDy = 10;

      while (isRunning) {
         loops = 0;
         while ((System.nanoTime() / 1000000L) > nextGameTick && loops < MAX_FRAMESKIP) {
            ballX += ballDx;
            ballY += ballDy;

            if (ballX <= gameArea.x) {
               ballX = gameArea.x;
               ballDx = -ballDx;
            } else if (ballX >= gameArea.width - ballWidth) {
               ballX = gameArea.width - ballWidth;
               ballDx = -ballDx;
            }

            if (ballY <= gameArea.y) {
               ballY = gameArea.y;
               ballDy = -ballDy;
            } else if (ballY >= gameArea.y + gameArea.height - ballHeight) {
               ballY = gameArea.y + gameArea.height - ballHeight;
               ballDy = -ballDy;
            }

            nextGameTick += SKIP_TICKS;
            loops++;
         }

         lastTime = curTime;
         curTime = System.nanoTime() / 1000000L;
         totalTime += curTime - lastTime;
         if (totalTime > 1000) {
            totalTime -= 1000;
            fps = frames;
            frames = 0;
         }
         ++frames;

         interpolation = (float) ((System.nanoTime() / 1000000L) + SKIP_TICKS - nextGameTick) / (float) (SKIP_TICKS);

         try {
            offScreenGraphics = offScreen.createGraphics();
            offScreenGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            offScreenGraphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            offScreenGraphics.setColor(Color.BLACK);
            offScreenGraphics.fillRect(0, 0, screenSize.width, screenSize.height);
            offScreenGraphics.setColor(Color.WHITE);
            offScreenGraphics.drawString(String.format("FPS: %s", fps), 20, 20);

            int renderX = (int) (ballX + (ballDx * interpolation));
            int renderY = (int) (ballY + (ballDy * interpolation));

            offScreenGraphics.drawLine(gameArea.x, gameArea.y - ballHeight, gameArea.width, gameArea.y - ballHeight);
            offScreenGraphics.fillRect(renderX, renderY, ballWidth, ballHeight);

            graphics = (Graphics2D) buffer.getDrawGraphics();
            if (!offScreen.contentsLost()) {
               graphics.drawImage(offScreen, 0, 0, screenSize.width, screenSize.height, 0, 0, gameSize.width, gameSize.height, null);
            }

            if (!buffer.contentsLost()) {
               buffer.show();
            }

            Thread.yield();
         } finally {
            if (offScreenGraphics != null) {
               offScreenGraphics.dispose();
            }

            if (graphics != null) {
               graphics.dispose();
            }
         }
      }

      frame.setVisible(false);
      System.exit(0);
   }
}


I get about 200FPS on my laptop which runs on Windows XP but I get jerky move.
I haven't tested it yet on my desktop which has Linux, but I don't expect too much of a difference.

If I replace

1  
final Dimension screenSize = toolkit.getScreenSize();


with

1  
final Dimension screenSize = new Dimension(640, 480);


i.e. so as to have a normal frame and not a 'fake' fullscreen, I get ~700FPS and much better movement.
Either there is something wrong in my code or I didn't fully understand how to use scaling on the fly.

Perhaps giving out instructions on how to change xorg.conf so Java can use the Full Screen Exclusive Mode API might be a better idea than faking fullscreen mode?

Regards,
Charalampos
Offline Virusakos

Senior Newbie





« Reply #7 - Posted 2010-07-11 00:29:09 »

In Linux (Linux Mint 8 - XFCE), the above test case gives even worse results.
~50FPS in fake fullscreen, jerky movement and the task bar is still on top of the frame.
 Cry
Offline bobjob

JGO Knight


Medals: 10
Projects: 6


David Aaron Muhar


« Reply #8 - Posted 2010-07-11 00:51:50 »

try this:
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  
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import javax.swing.JFrame;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.MemoryImageSource;
import java.awt.image.VolatileImage;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class FakeFullscreen {
   static boolean isRunning;

   public static void main(String[] args) {
      final Toolkit toolkit = Toolkit.getDefaultToolkit();

      final int[] pixels = new int[16 * 16];
      final Image image = toolkit.createImage(new MemoryImageSource(16, 16, pixels, 0, 16));
      final Cursor invisibleCursor = toolkit.createCustomCursor(image, new Point(0, 0), "invisibleCursor");

      final Dimension screenSize = toolkit.getScreenSize();
      final Dimension gameSize = new Dimension(640, 480);
      final Rectangle gameArea = new Rectangle(0, 80, 640, 400);

      final Canvas canvas = new Canvas();
      canvas.setIgnoreRepaint(true);
      canvas.setSize(screenSize);

      final JFrame frame = new JFrame();
      frame.setAlwaysOnTop(true);
      frame.setCursor(invisibleCursor);
      frame.setIgnoreRepaint(true);
      frame.setLocation(0, 0);
      frame.setResizable(false);
      frame.setSize(screenSize);
      frame.setUndecorated(true);

      frame.addKeyListener(new KeyAdapter() {
         @Override
         public void keyPressed(KeyEvent event) {
            if (event.getKeyCode() == KeyEvent.VK_ESCAPE) {
               isRunning = false;
            }
         }
      });
     
     
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.addWindowListener(new WindowAdapter() {
         @Override
            public void windowClosing(WindowEvent e) {
                isRunning = false;
            }
        });
     
     

      frame.add(canvas);
      frame.pack();
      frame.setVisible(true);
      frame.toFront();

      canvas.createBufferStrategy(2);
      final BufferStrategy buffer = canvas.getBufferStrategy();

      final VolatileImage offScreen = canvas.createVolatileImage(gameSize.width, gameSize.height);
      Graphics2D offScreenGraphics = null;

      Graphics2D graphics = null;
      isRunning = true;

      int fps = 0;
      int frames = 0;
      long totalTime = 0;
      long curTime = System.nanoTime() / 1000000L;
      long lastTime = curTime;

      final int TICKS_PER_SECOND = 25;
      final int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
      final int MAX_FRAMESKIP = 5;

      long nextGameTick = System.nanoTime() / 1000000L;
      int loops;
      float interpolation;

      int ballWidth = 8;
      int ballHeight = 8;
      int ballX = gameArea.width / 2 - ballWidth / 2;
      int ballY = gameArea.y + (gameArea.height / 2 - ballHeight / 2);
      int ballDx = 10;
      int ballDy = 10;

      while (isRunning) {
         loops = 0;
         while ((System.nanoTime() / 1000000L) > nextGameTick && loops < MAX_FRAMESKIP) {
            ballX += ballDx;
            ballY += ballDy;

            if (ballX <= gameArea.x) {
               ballX = gameArea.x;
               ballDx = -ballDx;
            } else if (ballX >= gameArea.width - ballWidth) {
               ballX = gameArea.width - ballWidth;
               ballDx = -ballDx;
            }

            if (ballY <= gameArea.y) {
               ballY = gameArea.y;
               ballDy = -ballDy;
            } else if (ballY >= gameArea.y + gameArea.height - ballHeight) {
               ballY = gameArea.y + gameArea.height - ballHeight;
               ballDy = -ballDy;
            }

            nextGameTick += SKIP_TICKS;
            loops++;
         }

         lastTime = curTime;
         curTime = System.nanoTime() / 1000000L;
         totalTime += curTime - lastTime;
         if (totalTime > 1000) {
            totalTime -= 1000;
            fps = frames;
            frames = 0;
         }
         ++frames;

         interpolation = (float) ((System.nanoTime() / 1000000L) + SKIP_TICKS - nextGameTick) / (float) (SKIP_TICKS);

         try {
            offScreenGraphics = offScreen.createGraphics();
            //offScreenGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
           //offScreenGraphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
           offScreenGraphics.setColor(Color.BLACK);
            offScreenGraphics.fillRect(0, 0, screenSize.width, screenSize.height);
            offScreenGraphics.setColor(Color.WHITE);
            offScreenGraphics.drawString(String.format("FPS: %s", fps), 20, 20);

            int renderX = (int) (ballX + (ballDx * interpolation));
            int renderY = (int) (ballY + (ballDy * interpolation));

            offScreenGraphics.drawLine(gameArea.x, gameArea.y - ballHeight, gameArea.width, gameArea.y - ballHeight);
            offScreenGraphics.fillRect(renderX, renderY, ballWidth, ballHeight);

            graphics = (Graphics2D) buffer.getDrawGraphics();
            if (!offScreen.contentsLost()) {
               graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
               graphics.drawImage(offScreen, 0, 0, screenSize.width, screenSize.height, 0, 0, gameSize.width, gameSize.height, null);
               graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
            }

            if (!buffer.contentsLost()) {
               buffer.show();
            }

            Thread.yield();
         } finally {
            if (offScreenGraphics != null) {
               offScreenGraphics.dispose();
            }

            if (graphics != null) {
               graphics.dispose();
            }
         }
      }

      frame.setVisible(false);
      System.exit(0);
   }
}



and if that is still to slow, remove all the rendering hints

in regards to your taksbar problem check out this link:
http://stackoverflow.com/questions/309023/howto-bring-a-java-window-to-the-front

My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Offline Virusakos

Senior Newbie





« Reply #9 - Posted 2010-07-11 01:16:22 »

Thanks for the above code bobjob.
In order to get decent FPS I have to turn off rendering hints (~10 FPS with rendering hints and ~80 FPS without) but I still get jerky move.
 Undecided
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline bobjob

JGO Knight


Medals: 10
Projects: 6


David Aaron Muhar


« Reply #10 - Posted 2010-07-11 02:18:13 »

ok try this version:
class CustomTimer and FPSCounter should probably be in there own files

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  
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import javax.swing.JFrame;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.MemoryImageSource;
import java.awt.image.VolatileImage;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class FakeFullscreen {
   
   //SHOULD BE SET TO TRUE FOR NORMAL GAME LOGIC
  final static boolean FRAME_SYNC = false;
   final static boolean HIGH_QUALITY = true;
   
   static boolean isRunning;
   static CustomTimer timer;
   static FPSCounter fpsCounter;
   
   public static void main(String[] args) {
      timer = new CustomTimer();
      fpsCounter = new FPSCounter(timer);
      final Toolkit toolkit = Toolkit.getDefaultToolkit();

      final int[] pixels = new int[16 * 16];
      final Image image = toolkit.createImage(new MemoryImageSource(16, 16, pixels, 0, 16));
      final Cursor invisibleCursor = toolkit.createCustomCursor(image, new Point(0, 0), "invisibleCursor");

      final Dimension screenSize = toolkit.getScreenSize();
      final Dimension gameSize = new Dimension(640, 480);
      final Rectangle gameArea = new Rectangle(0, 80, 640, 400);

      final Canvas canvas = new Canvas();
      canvas.setIgnoreRepaint(true);
      canvas.setSize(screenSize);

      final JFrame frame = new JFrame();
      frame.setAlwaysOnTop(true);
      frame.setCursor(invisibleCursor);
      frame.setIgnoreRepaint(true);
      frame.setLocation(0, 0);
      frame.setResizable(false);
      frame.setSize(screenSize);
      frame.setUndecorated(true);

      frame.addKeyListener(new KeyAdapter() {
         @Override
         public void keyPressed(KeyEvent event) {
            if (event.getKeyCode() == KeyEvent.VK_ESCAPE) {
               isRunning = false;
            }
         }
      });
     
     
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.addWindowListener(new WindowAdapter() {
         @Override
            public void windowClosing(WindowEvent e) {
                isRunning = false;
            }
        });

      frame.add(canvas);
      frame.pack();
      frame.setVisible(true);
      frame.toFront();

      canvas.createBufferStrategy(2);
      final BufferStrategy buffer = canvas.getBufferStrategy();

      final VolatileImage offScreen = canvas.createVolatileImage(gameSize.width, gameSize.height);
      Graphics2D offScreenGraphics = null;

      Graphics2D graphics = null;
      isRunning = true;

      int ballWidth = 8;
      int ballHeight = 8;
      int ballX = gameArea.width / 2 - ballWidth / 2;
      int ballY = gameArea.y + (gameArea.height / 2 - ballHeight / 2);
      int ballDx = 1;
      int ballDy = 1;

      while (isRunning) {
         if (!FRAME_SYNC || timer.update(60)) {
            fpsCounter.update();
            ballX += ballDx;
            ballY += ballDy;

            if (ballX <= gameArea.x) {
               ballX = gameArea.x;
               ballDx = -ballDx;
            } else if (ballX >= gameArea.width - ballWidth) {
               ballX = gameArea.width - ballWidth;
               ballDx = -ballDx;
            }

            if (ballY <= gameArea.y) {
               ballY = gameArea.y;
               ballDy = -ballDy;
            } else if (ballY >= gameArea.y + gameArea.height - ballHeight) {
               ballY = gameArea.y + gameArea.height - ballHeight;
               ballDy = -ballDy;
            }
            try {
               offScreenGraphics = offScreen.createGraphics();
               offScreenGraphics.setColor(Color.BLACK);
               offScreenGraphics.fillRect(0, 0, screenSize.width, screenSize.height);
               offScreenGraphics.setColor(Color.WHITE);
               offScreenGraphics.drawString(String.format("FPS: %s", fpsCounter.getFPS()), 20, 20);

               int renderX = (int) (ballX + ballDx);
               int renderY = (int) (ballY + ballDy);

               offScreenGraphics.drawLine(gameArea.x, gameArea.y - ballHeight, gameArea.width, gameArea.y - ballHeight);
               offScreenGraphics.fillRect(renderX, renderY, ballWidth, ballHeight);

               graphics = (Graphics2D) buffer.getDrawGraphics();
               if (!offScreen.contentsLost()) {
                  if (HIGH_QUALITY) {
                     graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                  }
                  graphics.drawImage(offScreen, 0, 0, screenSize.width, screenSize.height, 0, 0, gameSize.width, gameSize.height, null);
               }

               if (!buffer.contentsLost()) {
                  buffer.show();
               }
            } finally {
               if (offScreenGraphics != null) {
                  offScreenGraphics.dispose();
               }

               if (graphics != null) {
                  graphics.dispose();
               }
            }
         }
         Thread.yield();
      }

      frame.setVisible(false);
      System.exit(0);
   }
}

class CustomTimer {
   private long timeThen;
   boolean newVersion = true;
   public CustomTimer() {
      if (System.getProperty("java.version").startsWith("1.4")) newVersion = false;

      if (newVersion) {
         timeThen = System.nanoTime();
      }
     
      else {
         System.out.println("Old Version Detected: Running Old Java Timer Version");
         timeThen = System.currentTimeMillis();
      }
   }
   public boolean update(int fps) {
      if (newVersion) {
         long gapTo = 1000000000L / fps + timeThen;
         long timeNow = System.nanoTime();
         if (gapTo > timeNow) return false;
         timeThen = timeNow;
      } else {
         long gapTo = 1000 / fps + timeThen;
         long timeNow = System.currentTimeMillis();  
         if (gapTo > timeNow) return false;
         timeThen = timeNow;
      }
      return true;
   }

   public long getTime() {
      if (newVersion) {
         return System.nanoTime();
      } else {
         return System.currentTimeMillis();
      }
   }
   public long getTimerResolution() {
      if (newVersion) {
         return 1000000000L;
      } else {
         return 1000L;
      }
   }
}
class FPSCounter {
   private CustomTimer timer;
   private float FPS = 0, fc = 0;

    private long
                 currentTime,
                 elapsedTime,
                 lastTime;
   
    public FPSCounter(CustomTimer timer) {
       this.timer = timer;
        currentTime     = 0;
        elapsedTime = lastTime = timer.getTime();
    }
    public boolean update() {
      currentTime = timer.getTime();

         fc++;
         long updateFrequency = timer.getTimerResolution()>>1;
         if((elapsedTime = currentTime - lastTime) >= updateFrequency){
           FPS         = (fc/elapsedTime)*updateFrequency*2;
           fc         = 0;
           lastTime    = currentTime;
           elapsedTime = 0;
           return true;
         }
         return false;
    }
    public float getFPS() {
       return FPS;
    }
}


try turning boolean SYNC on/off
and try turning the boolean HIGH_QUALITY on/off

My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Offline nsigma
« Reply #11 - Posted 2010-07-11 10:56:24 »

I think you'll find the taskbar problem on Linux Mint is a known issue with Compiz. Try turning off desktop effects and it should work fine.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Virusakos

Senior Newbie





« Reply #12 - Posted 2010-07-12 11:37:12 »

@nsigma
I am pretty sure there is an option somewhere but it's definitely not Compiz's option, as Compiz is not installed on my system.
It does seem to be an issue with XFCE. Fluxbox does not have that issue. I don't know about Gnome or KDE as they are not installed on my system.

@bobjob
Your last code runs much much better. Thank you.
Though I still believe full exclusive mode is the way to go. If not through pure Java, then through native code.

I gave Slick2D a try and the full screen works without any other hassle.
I think, for now, I will give up on Java2D and focus on creating games using a ready made game engine and later on after I gain some experience I will come back to do my own engine.

Regards,
Charalampos
Offline nsigma
« Reply #13 - Posted 2010-07-13 10:26:54 »

@nsigma
I am pretty sure there is an option somewhere but it's definitely not Compiz's option, as Compiz is not installed on my system.
It does seem to be an issue with XFCE. Fluxbox does not have that issue. I don't know about Gnome or KDE as they are not installed on my system.

That'll teach me to reply on my phone where I can't read the code boxes! Smiley I also missed the fact you'd written XFCE after Mint, sorry.

Take 2 : Try using the FSEM methods to set the fullscreen window rather than just trying to fake it. ie.

1  
2  
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
gd.setFullScreenWindow(frame);


Even when simulated, this should add the required hints to make the window appear above panels / toolbars.  See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6364134 and related.  The hack of just making a window the size of the screen stopped working for me about 2 years ago.

Unfortunately, as I hinted at in my previous post, the window hints still don't fix the issue when running with Compiz.

Hope that helps this time,

Neil

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Virusakos

Senior Newbie





« Reply #14 - Posted 2010-07-13 10:41:14 »

Thanks nsigma.
I will give it a shot and see what I can come up with  Smiley

Regards,
Charalampos
Offline Virusakos

Senior Newbie





« Reply #15 - Posted 2010-07-13 20:22:35 »

After upgrading my system from Linux Mint 8 to Linux Mint 9, Full Screen Exclusive Mode API seems to be working fine (apart from the taskbar issue in XFCE at least).

 Huh


Anyway, seeing how Java game related APIs are dependent on system configuration, I think I will stick with Slick 2D for now and come back in the future.

Thanks everyone for your time and help provided.

Regards,
Charalampos
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.

xsi3rr4x (28 views)
2014-04-15 18:08:23

BurntPizza (25 views)
2014-04-15 03:46:01

UprightPath (40 views)
2014-04-14 17:39:50

UprightPath (22 views)
2014-04-14 17:35:47

Porlus (38 views)
2014-04-14 15:48:38

tom_mai78101 (62 views)
2014-04-10 04:04:31

BurntPizza (121 views)
2014-04-08 23:06:04

tom_mai78101 (221 views)
2014-04-05 13:34:39

trollwarrior1 (188 views)
2014-04-04 12:06:45

CJLetsGame (195 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!