Slick Forums

Discuss the Slick 2D Library
It is currently Wed May 22, 2013 1:09 am

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Sat Aug 11, 2012 10:38 pm 
Offline

Joined: Sat Aug 11, 2012 10:13 pm
Posts: 35
I am trying to enter my splashScreen for my game with a transition then a transition out after 3 seconds then transition into the menu. I have tried the Color method and it did work a bit when I set the alpha value to a hard coded value, but I can't get my timing to increase it to work. Here is my code:
Class I am working on
Code:
Image splashScreen;
   
   private float alpha = 0.0f;

   public SplashScreen(int state) {}

   public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
      splashScreen = new Image("/res/splashscreen.png");
   }
   
   public void render(GameContainer gc, StateBasedGame sbg, Graphics g) {
      Color color = new Color(1f, 1f, 1f, alpha);
      splashScreen.draw(0, 0, color);
   }
   
   public void update(GameContainer gc, StateBasedGame sbg, int delta) {
      
      if(alpha < 1) {
         alpha += alpha + 0.000001f;
      }
   }


Code snippet of first class
Code:
public void initStatesList(GameContainer gc) throws SlickException {
      this.getState(splashScreen).init(gc, this);
      this.getState(stateMenu).init(gc, this);
      this.getState(stateGame).init(gc, this);
      this.enterState(splashScreen);
   }


Any help is appreciated. If you would mind also telling me how I can have an internation TrueTypeFonts file instead of declaring it everytime would also help. And some other things I plan on wanting for the splash screen is going to be having it go for 3 seconds then going to the next state.
Thanks,
Imposter


Top
 Profile  
 
PostPosted: Thu Aug 16, 2012 4:31 pm 
Offline
Oldbie
User avatar

Joined: Thu Jan 13, 2011 4:42 pm
Posts: 349
The first thing I should mention is that you haven't stated your symptoms. I may be able to help, but I could offer more help if you told me whether the splash screen was never appearing, alwayse there, or fading at the wrong time.

the problem likely lies within the following lines:

Code:
if(alpha < 1) {
      alpha += alpha + 0.000001f;
}


this code should double the alpha and add a very small number to it each update.
this means that at 60fps, the image will have an alpha of around 0.04 (barely invisible) after the first
15 frames, and by 20 frames, be fully opaque, making it appear to pop into existence in 1/12 of a second
(instantly).

to try to offer a solution, the following, untested code should approximately do what you want:

Code:

private int alphaTimerMs=0;

private float alpha=0f;

public static final int FADE_IN_TIME_MS=500;//fade in for 1/2 second
public static final int LEAVE_TIME_MS=3000;//end the state after 3 seconds

public SplashScreen(int state) {}

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
      splashScreen = new Image("/res/splashscreen.png");
}
   
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) {
      Color color = new Color(1f, 1f, 1f, alpha);
      splashScreen.draw(0, 0, color);

      //I usually use:
      //splashScreen.setAlpha(alpha);
      //splashScreen.draw();
      //but your method should do the exact same thing
}

public void update(GameContainer gc, StateBasedGame sbg, int delta) {
      alphaTimerMs+=delta;

      //usually, alpha will need to be one
      alpha=1;

      //but if FADE_IN_TIME_MS milliseconds have not passed,
      //alpha should be set to a fraction
      if (alphaTimerMs<FADE_IN_TIME_MS){
            //this next line will compute the alpha float.  In order to divide as a float
            //one of the parts will need to be cast to a float.  I chose alphaTimerMs
            alpha=((float)alphaTimerMs)/FADE_IN_TIME_MS;
      }

      //finally, if LEAVE_TIME_MS milliseconds have passed,
      //go to the next state
      if (alphaTimerMs<FADE_IN_TIME_MS){
            sbg.enterState(sbg.STATE_MAIN_MENU);
      }
}

_________________
"Artificial intelligence will never be a match for human stupidity" - "Jamos Kennedynos"


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

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