Slick Forums

Discuss the Slick 2D Library
It is currently Sat May 18, 2013 9:53 pm

All times are UTC




Post new topic Reply to topic  [ 42 posts ]  Go to page Previous  1, 2, 3
Author Message
 Post subject:
PostPosted: Sun Feb 07, 2010 4:30 pm 
Offline
Regular

Joined: Wed Dec 09, 2009 6:56 pm
Posts: 132
At the top of my init() method, I execute the following code:

Code:
    try {
      for(DisplayMode displayMode : Display.getAvailableDisplayModes()) {
        if (displayMode.getBitsPerPixel() == 32
            && (displayMode.getWidth() > maxWidth
                || displayMode.getHeight() > maxHeight)) {
          maxWidth = displayMode.getWidth();
          maxHeight = displayMode.getHeight();
          nativeDisplayMode = displayMode;
        }
      }
    } catch(Throwable t) {
      throw new SlickException("Error finding native monitor resolution.", t);
    }


This tries to find the maximum resolution available on your monitor. It captures that display mode into the public variable nativeDisplayMode.

The exception that you see originates from this line when you try to try to switch to full-screen mode:

Code:
targetDisplayMode = fullscreen
            ? main.nativeDisplayMode : originalDisplayMode;

        if (targetDisplayMode == null) {
          throw new SlickException("Target display mode not set.");
        }


This suggests that nativeDisplayMode was never set. Either it didn't really loop because Display.getAvailableDisplayModes() returned an empty set or displayMode.getBitsPerPixel() == 32 is never satisfied.

Any chance that you can check which of the cases is failing for you?

Thanks for your help Kappa.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 07, 2010 6:55 pm 
Offline
Game Developer
User avatar

Joined: Sun Nov 12, 2006 8:40 pm
Posts: 574
zeroone wrote:
This suggests that nativeDisplayMode was never set. Either it didn't really loop because Display.getAvailableDisplayModes() returned an empty set or displayMode.getBitsPerPixel() == 32 is never satisfied.

Any chance that you can check which of the cases is failing for you?


ah, that must be your problem then. Linux has no 32 bit color mode, it only has 24bit color mode as a max.

best way is to just get the current color mode and use that (everybody just uses 24 or 32 nowdays anyway)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 07, 2010 7:48 pm 
Offline
Game Developer
User avatar

Joined: Sun Nov 12, 2006 8:40 pm
Posts: 574
you could also try the following AppletGameContainer and see if it works well enough for you

Code:
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.ByteBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Cursor;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;
import org.newdawn.slick.Game;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.openal.SoundStore;
import org.newdawn.slick.opengl.CursorLoader;
import org.newdawn.slick.opengl.ImageData;
import org.newdawn.slick.opengl.InternalTextureLoader;
import org.newdawn.slick.util.Log;

/**
* A game container that displays the game as an applet. Note however that the
* actual game container implementation is an internal class which can be
* obtained with the getContainer() method - this is due to the Applet being a
* class wrap than an interface.
*
* @author kevin
*/
public class AppletGameContainer extends Applet {
   /** The GL Canvas used for this container */
   protected ContainerPanel canvas;
   /** The actual container implementation */
   protected Container container;
   /** The parent of the display */
   protected Canvas displayParent;
   /** The thread that is looping for the game */
   protected Thread gameThread;
   /** Alpha background supported */
   protected boolean alphaSupport = true;
   
   /**
    * @see java.applet.Applet#destroy()
    */
   public void destroy() {
      if (displayParent != null) {
         remove(displayParent);
      }
      super.destroy();
     
      Log.info("Clear up");
   }

   /**
    * Clean up the LWJGL resources
    */
   private void destroyLWJGL() {
      container.stopApplet();
     
      try {
         gameThread.join();
      } catch (InterruptedException e) {
         Log.error(e);
      }
   }

   /**
    * @see java.applet.Applet#start()
    */
   public void start() {
     
   }
   
   /**
    * Start a thread to run LWJGL in
    */
   public void startLWJGL() {
      if (gameThread != null) {
         return;
      }
     
      gameThread = new Thread() {
         public void run() {
            try {
               canvas.start();
            }
            catch (Exception e) {
               e.printStackTrace();
               if (Display.isCreated()) {
                  Display.destroy();
               }
               displayParent.setVisible(false);//removeAll();
               add(new ConsolePanel(e));
               validate();
            }
         }
      };
     
      gameThread.start();
   }

   /**
    * @see java.applet.Applet#stop()
    */
   public void stop() {
   }

   /**
    * @see java.applet.Applet#init()
    */
   public void init() {
      removeAll();
      setLayout(new BorderLayout());
      setIgnoreRepaint(true);

      try {
         Game game = (Game) Class.forName(getParameter("game")).newInstance();
         
         container = new Container(game);
         canvas = new ContainerPanel(container);
         displayParent = new Canvas() {
            public final void addNotify() {
               super.addNotify();
               startLWJGL();
            }
            public final void removeNotify() {
               destroyLWJGL();
               super.removeNotify();
            }

         };

         displayParent.setSize(getWidth(), getHeight());
         add(displayParent);
         displayParent.setFocusable(true);
         displayParent.requestFocus();
         displayParent.setIgnoreRepaint(true);
         setVisible(true);
      } catch (Exception e) {
         Log.error(e);
         throw new RuntimeException("Unable to create game container");
      }
   }

   /**
    * Get the GameContainer providing this applet
    *
    * @return The game container providing this applet
    */
   public GameContainer getContainer() {
      return container;
   }

   /**
    * Create a new panel to display the GL context
    *
    * @author kevin
    */
   public class ContainerPanel {
      /** The container being displayed on this canvas */
      private Container container;

      /**
       * Create a new panel
       *
       * @param container The container we're running
       */
      public ContainerPanel(Container container) {
         this.container = container;
      }

      /**
       * Create the LWJGL display
       *
       * @throws Exception Failure to create display
       */
      private void createDisplay() throws Exception {
         try {
            // create display with alpha
            Display.create(new PixelFormat(8,8,0));
            alphaSupport = true;
         } catch (Exception e) {
            // if we couldn't get alpha, let us know
            alphaSupport = false;
             Display.destroy();
             // create display without alpha
            Display.create();
         }
      }
     
      /**
       * Start the game container
       *
       * @throws Exception Failure to create display
       */
      public void start() throws Exception {
         Display.setParent(displayParent);
         Display.setVSyncEnabled(true);
         
         try {
            createDisplay();
         } catch (LWJGLException e) {
            e.printStackTrace();
            // failed to create Display, apply workaround (sleep for 1 second) and try again
            Thread.sleep(1000);
            createDisplay();
         }
         
         initGL();
         displayParent.requestFocus();
         container.runloop();
      }

      /**
       * Initialise GL state
       */
      protected void initGL() {
         try {
            InternalTextureLoader.get().clear();
            SoundStore.get().clear();

            container.initApplet();
         } catch (Exception e) {
            Log.error(e);
            container.stopApplet();
         }
      }
   }

   /**
    * A game container to provide the applet context
    *
    * @author kevin
    */
   public class Container extends GameContainer {
      /**
       * Create a new container wrapped round the game
       *
       * @param game The game to be held in this container
       */
      public Container(Game game) {
         super(game);

         width = AppletGameContainer.this.getWidth();
         height = AppletGameContainer.this.getHeight();
      }

      /**
       * Initiliase based on Applet init
       *
       * @throws SlickException Indicates a failure to inialise the basic framework
       */
      public void initApplet() throws SlickException {
         initSystem();
         enterOrtho();

         try {
            getInput().initControllers();
         } catch (SlickException e) {
            Log.info("Controllers not available");
         } catch (Throwable e) {
            Log.info("Controllers not available");
         }

         game.init(this);
         getDelta();
      }

      /**
       * Check if the applet is currently running
       *
       * @return True if the applet is running
       */
      public boolean isRunning() {
         return running;
      }

      /**
       * Stop the applet play back
       */
      public void stopApplet() {
         running = false;
      }

      /**
       * @see org.newdawn.slick.GameContainer#getScreenHeight()
       */
      public int getScreenHeight() {
         return 0;
      }

      /**
       * @see org.newdawn.slick.GameContainer#getScreenWidth()
       */
      public int getScreenWidth() {
         return 0;
      }
     
      /**
       * Check if the display created supported alpha in the back buffer
       *
       * @return True if the back buffer supported alpha
       */
      public boolean supportsAlphaInBackBuffer() {
         return alphaSupport;
      }
     
      /**
       * @see org.newdawn.slick.GameContainer#hasFocus()
       */
      public boolean hasFocus() {
         return true;
      }
     
      /**
       * Returns the Applet Object
       * @return Applet Object
       */
      public Applet getApplet() {
         return AppletGameContainer.this;
      }
     
      /**
       * @see org.newdawn.slick.GameContainer#setIcon(java.lang.String)
       */
      public void setIcon(String ref) throws SlickException {
         // unsupported in an applet
      }

      /**
       * @see org.newdawn.slick.GameContainer#setMouseGrabbed(boolean)
       */
      public void setMouseGrabbed(boolean grabbed) {
         Mouse.setGrabbed(grabbed);
      }

     /**
      * @see org.newdawn.slick.GameContainer#isMouseGrabbed()
      */
      public boolean isMouseGrabbed() {
         return Mouse.isGrabbed();
     }
     
      /**
       * @see org.newdawn.slick.GameContainer#setMouseCursor(java.lang.String,
       *      int, int)
       */
      public void setMouseCursor(String ref, int hotSpotX, int hotSpotY) throws SlickException {
         try {
            Cursor cursor = CursorLoader.get().getCursor(ref, hotSpotX, hotSpotY);
            Mouse.setNativeCursor(cursor);
         } catch (Exception e) {
            Log.error("Failed to load and apply cursor.", e);
         }
      }

      /**
       * Get the closest greater power of 2 to the fold number
       *
       * @param fold The target number
       * @return The power of 2
       */
      private int get2Fold(int fold) {
          int ret = 2;
          while (ret < fold) {
              ret *= 2;
          }
          return ret;
      }
     
      /**
       * {@inheritDoc}
       */
      public void setMouseCursor(Image image, int hotSpotX, int hotSpotY) throws SlickException {
          try {
             Image temp = new Image(get2Fold(image.getWidth()), get2Fold(image.getHeight()));
             Graphics g = temp.getGraphics();
             
             ByteBuffer buffer = BufferUtils.createByteBuffer(temp.getWidth() * temp.getHeight() * 4);
             g.drawImage(image.getFlippedCopy(false, true), 0, 0);
             g.flush();
             g.getArea(0,0,temp.getWidth(),temp.getHeight(),buffer);
             
             Cursor cursor = CursorLoader.get().getCursor(buffer, hotSpotX, hotSpotY,temp.getWidth(),temp.getHeight());
             Mouse.setNativeCursor(cursor);
          } catch (Exception e) {
             Log.error("Failed to load and apply cursor.", e);
          }
       }
     
      /**
       * @see org.newdawn.slick.GameContainer#setIcons(java.lang.String[])
       */
      public void setIcons(String[] refs) throws SlickException {
         // unsupported in an applet
      }

      /**
       * @see org.newdawn.slick.GameContainer#setMouseCursor(org.newdawn.slick.opengl.ImageData, int, int)
       */
      public void setMouseCursor(ImageData data, int hotSpotX, int hotSpotY) throws SlickException {
         try {
            Cursor cursor = CursorLoader.get().getCursor(data, hotSpotX, hotSpotY);
            Mouse.setNativeCursor(cursor);
         } catch (Exception e) {
            Log.error("Failed to load and apply cursor.", e);
         }
      }

      /**
       * @see org.newdawn.slick.GameContainer#setMouseCursor(org.lwjgl.input.Cursor, int, int)
       */
      public void setMouseCursor(Cursor cursor, int hotSpotX, int hotSpotY) throws SlickException {
         try {
            Mouse.setNativeCursor(cursor);
         } catch (Exception e) {
            Log.error("Failed to load and apply cursor.", e);
         }
      }

      /**
       * @see org.newdawn.slick.GameContainer#setDefaultMouseCursor()
       */
      public void setDefaultMouseCursor() {
      }

      public boolean isFullscreen() {
         return Display.isFullscreen();
      }

      public void setFullscreen(boolean fullscreen) throws SlickException {
         if (fullscreen == isFullscreen()) {
            return;
         }

         try {
            if (fullscreen) {
               // get current screen resolution
               int screenWidth = Display.getDisplayMode().getWidth();
               int screenHeight = Display.getDisplayMode().getHeight();

               // scale game to match new resolution
               GL11.glViewport(0, 0, screenWidth, screenHeight);

               enterOrtho();

               // fix input to match new resolution
               this.getInput().setScale((float) width / screenWidth,
                     (float) height / screenHeight);

               width = screenWidth;
               height = screenHeight;
               Display.setFullscreen(true);
            } else {
               // restore input
               this.getInput().setOffset(0, 0);
               this.getInput().setScale(1, 1);
               width = AppletGameContainer.this.getWidth();
               height = AppletGameContainer.this.getHeight();
               GL11.glViewport(0, 0, width, height);

               enterOrtho();

               Display.setFullscreen(false);
            }
         } catch (LWJGLException e) {
            Log.error(e);
         }

      }

      /**
       * The running game loop
       *
       * @throws Exception Indicates a failure within the game's loop rather than the framework
       */
      public void runloop() throws Exception {
         while (running) {
            int delta = getDelta();

            updateAndRender(delta);

            updateFPS();
            Display.update();
         }

         Display.destroy();
      }
   }
   
   /**
    * A basic console to display an error message if the applet crashes.
    * This will prevent the applet from just freezing in the browser
    * and give the end user an a nice gui where the error message can easily
    * be viewed and copied.
    */
   public class ConsolePanel extends Panel {
      /** The area display the console output */
      TextArea textArea = new TextArea();
     
      /**
       * Create a new panel to display the console output
       *
       * @param e The exception causing the console to be displayed
       */
      public ConsolePanel(Exception e) {
         setLayout(new BorderLayout());
         setBackground(Color.black);
         setForeground(Color.white);
         
         Font consoleFont = new Font("Arial", Font.BOLD, 14);
         
         Label slickLabel = new Label("SLICK CONSOLE", Label.CENTER);
         slickLabel.setFont(consoleFont);
         add(slickLabel, BorderLayout.PAGE_START);
         
         StringWriter sw = new StringWriter();
         e.printStackTrace(new PrintWriter(sw));
         
         textArea.setText(sw.toString());
         textArea.setEditable(false);
         add(textArea, BorderLayout.CENTER);
         
         // add a border on both sides of the console
         add(new Panel(), BorderLayout.LINE_START);
         add(new Panel(), BorderLayout.LINE_END);
         
         Panel bottomPanel = new Panel();
         bottomPanel.setLayout(new GridLayout(0, 1));
         Label infoLabel1 = new Label("An error occured while running the applet.", Label.CENTER);
         Label infoLabel2 = new Label("Plese contact support to resolve this issue.", Label.CENTER);
         infoLabel1.setFont(consoleFont);
         infoLabel2.setFont(consoleFont);
         bottomPanel.add(infoLabel1);
         bottomPanel.add(infoLabel2);
         add(bottomPanel, BorderLayout.PAGE_END);
      }
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 08, 2010 2:18 pm 
Offline
Regular

Joined: Wed Dec 09, 2009 6:56 pm
Posts: 132
Thanks for the info about Linux. Is that code the standard applet container that comes with Slick?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 08, 2010 8:06 pm 
Offline
Game Developer
User avatar

Joined: Sun Nov 12, 2006 8:40 pm
Posts: 574
zeroone wrote:
Thanks for the info about Linux. Is that code the standard applet container that comes with Slick?


it is but with a slight tweak to allow proper fullscreen without maintaining aspect ratio, try it and see if it works well enough for you.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 08, 2010 8:20 pm 
Offline
Regular

Joined: Wed Dec 09, 2009 6:56 pm
Posts: 132
Quote:
it is but with a slight tweak to allow proper fullscreen without maintaining aspect ratio, try it and see if it works well enough for you.


I'll test it out this weekend, but I want to maintain the correct aspect ratio. I found that the scaling appeared the nicest and the animation the fluidest when the monitor was in the native resolution. So, I try to max out the rez in my version.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 13, 2010 5:53 pm 
Offline
Regular

Joined: Wed Dec 09, 2009 6:56 pm
Posts: 132
I compressed the music down to about 1/3 of the original size. Hopefully, Java 6 build 10 or higher is no longer required.

Also, I altered the way it detects the native monitor resolution. Full-screen mode should now work under Linux. If you test this out Kappa, let if know if the aspect ratio is maintained and if there appears to be any kind of clipping issues on a wide screen monitor. Thanks again for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 13, 2010 8:21 pm 
Offline
Game Developer
User avatar

Joined: Sun Nov 12, 2006 8:40 pm
Posts: 574
full screen works great now on linux.

I do have an issue with controls not working after returning to window mode from full screen, but I think this might be a lwjgl issue, which hopefully should be fixing in the forthcoming lwjlg 2.3

great work, think game might be read to be posted in the showcase section on JGO :)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 13, 2010 9:05 pm 
Offline
Regular

Joined: Wed Dec 09, 2009 6:56 pm
Posts: 132
Quote:
I do have an issue with controls not working after returning to window mode from full screen, but I think this might be a lwjgl issue, which hopefully should be fixing in the forthcoming lwjlg 2.3


I wonder if you have to click on it with the mouse. Applets tend to lose focus sometimes. That effect does not appear to happen under Vista.

Quote:
great work, think game might be read to be posted in the showcase section on JGO


Uh... what?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 13, 2010 9:22 pm 
Offline
Game Developer
User avatar

Joined: Sun Nov 12, 2006 8:40 pm
Posts: 574
zeroone wrote:
Uh... what?


sorry, JGO = http://www.javagaming.org/


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 13, 2010 9:26 pm 
Offline
Regular

Joined: Wed Dec 09, 2009 6:56 pm
Posts: 132
Thanks for the tip Kappa. I posted it.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 13, 2010 9:27 pm 
Offline
Game Developer
User avatar

Joined: Sun Nov 12, 2006 8:40 pm
Posts: 574
zeroone wrote:
Thanks for the tip Kappa. I posted it.


nice, but don't forget to add a description and maybe some screenshots :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 42 posts ]  Go to page Previous  1, 2, 3

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group