Skip to content

Commit

Permalink
Implemented dynamic main menu backgrounds.
Browse files Browse the repository at this point in the history
- This can be switched off in the configuration file.
- Credits: https://osu.ppy.sh/forum/t/98954

Other changes:
- Removed OsuFile setters/getters from Game state, replaced with a 'getOsuFile()' method in MusicController (a more logical location).

Signed-off-by: Jeffrey Han <[email protected]>
  • Loading branch information
itdelatrisu committed Jul 2, 2014
1 parent bcd0c23 commit 5612336
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 24 deletions.
3 changes: 1 addition & 2 deletions src/itdelatrisu/opsu/GameScore.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package itdelatrisu.opsu;

import itdelatrisu.opsu.states.Game;
import itdelatrisu.opsu.states.Options;

import java.util.HashMap;
Expand Down Expand Up @@ -449,7 +448,7 @@ public void drawGameElements(Graphics g, int mapLength, boolean breakPeriod, boo
float circleDiameter = getScoreSymbolImage('0').getHeight() * 0.75f;
g.drawOval(circleX, circleY, circleDiameter, circleDiameter);

int firstObjectTime = Game.getOsuFile().objects[0].time;
int firstObjectTime = MusicController.getOsuFile().objects[0].time;
int trackPosition = MusicController.getPosition();
if (trackPosition > firstObjectTime) {
g.fillArc(circleX, circleY, circleDiameter, circleDiameter,
Expand Down
5 changes: 5 additions & 0 deletions src/itdelatrisu/opsu/MusicController.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ public static boolean trackExists() {
return (player != null);
}

/**
* Returns the OsuFile associated with the current track.
*/
public static OsuFile getOsuFile() { return lastOsu; }

/**
* Returns the name of the current track.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/itdelatrisu/opsu/Opsu.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public boolean closeRequested() {
if (id == STATE_GAME || id == STATE_GAMEPAUSEMENU || id == STATE_GAMERANKING) {
// start playing track at preview position
MusicController.pause();
MusicController.playAt(Game.getOsuFile().previewTime, true);
MusicController.playAt(MusicController.getOsuFile().previewTime, true);
this.enterState(Opsu.STATE_SONGMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
return false;
}
Expand Down
9 changes: 3 additions & 6 deletions src/itdelatrisu/opsu/states/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,9 @@ else if ((hitObject.type & OsuHitObject.TYPE_SLIDER) > 0)
@Override
public void enter(GameContainer container, StateBasedGame game)
throws SlickException {
if (restart == RESTART_NEW)
osu = MusicController.getOsuFile();

if (osu == null || osu.objects == null)
throw new RuntimeException("Running game with no OsuFile loaded.");

Expand Down Expand Up @@ -810,12 +813,6 @@ private void setMapModifiers() {
public static void setRestart(byte restart) { Game.restart = restart; }
public static byte getRestart() { return Game.restart; }

/**
* Sets or returns the associated OsuFile.
*/
public static void setOsuFile(OsuFile osu) { Game.osu = osu; }
public static OsuFile getOsuFile() { return osu; }

/**
* Returns the associated GameScore object.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/itdelatrisu/opsu/states/GamePauseMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void keyPressed(int key, char c) {
// 'esc' will normally unpause, but will return to song menu if health is zero
if (Game.getRestart() == Game.RESTART_LOSE) {
MusicController.stop();
MusicController.playAt(Game.getOsuFile().previewTime, true);
MusicController.playAt(MusicController.getOsuFile().previewTime, true);
SoundController.playSound(SoundController.SOUND_MENUBACK);
game.enterState(Opsu.STATE_SONGMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
} else
Expand Down Expand Up @@ -174,7 +174,7 @@ else if (retryButton.contains(x, y)) {
unPause(Game.RESTART_MANUAL);
} else if (backButton.contains(x, y)) {
MusicController.pause(); // lose state
MusicController.playAt(Game.getOsuFile().previewTime, true);
MusicController.playAt(MusicController.getOsuFile().previewTime, true);
SoundController.playSound(SoundController.SOUND_MENUBACK);
game.enterState(Opsu.STATE_SONGMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
}
Expand Down
8 changes: 4 additions & 4 deletions src/itdelatrisu/opsu/states/GameRanking.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void render(GameContainer container, StateBasedGame game, Graphics g)
int width = container.getWidth();
int height = container.getHeight();

OsuFile osu = Game.getOsuFile();
OsuFile osu = MusicController.getOsuFile();

// background
if (!osu.drawBG(width, height, 0.7f))
Expand Down Expand Up @@ -147,7 +147,7 @@ public void keyPressed(int key, char c) {
switch (key) {
case Input.KEY_ESCAPE:
MusicController.pause();
MusicController.playAt(Game.getOsuFile().previewTime, true);
MusicController.playAt(MusicController.getOsuFile().previewTime, true);
SoundController.playSound(SoundController.SOUND_MENUBACK);
game.enterState(Opsu.STATE_SONGMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
break;
Expand All @@ -164,7 +164,7 @@ public void mousePressed(int button, int x, int y) {
return;

if (retryButton.contains(x, y)) {
OsuFile osu = Game.getOsuFile();
OsuFile osu = MusicController.getOsuFile();
Display.setTitle(String.format("%s - %s", game.getTitle(), osu.toString()));
Game.setRestart(Game.RESTART_MANUAL);
SoundController.playSound(SoundController.SOUND_MENUHIT);
Expand All @@ -174,7 +174,7 @@ public void mousePressed(int button, int x, int y) {
game.enterState(Opsu.STATE_MAINMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
} else if (Utils.getBackButton().contains(x, y)) {
MusicController.pause();
MusicController.playAt(Game.getOsuFile().previewTime, true);
MusicController.playAt(MusicController.getOsuFile().previewTime, true);
SoundController.playSound(SoundController.SOUND_MENUBACK);
game.enterState(Opsu.STATE_SONGMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
}
Expand Down
33 changes: 27 additions & 6 deletions src/itdelatrisu/opsu/states/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import itdelatrisu.opsu.GUIMenuButton;
import itdelatrisu.opsu.MusicController;
import itdelatrisu.opsu.Opsu;
import itdelatrisu.opsu.OsuFile;
import itdelatrisu.opsu.OsuGroupNode;
import itdelatrisu.opsu.SoundController;
import itdelatrisu.opsu.Utils;
Expand Down Expand Up @@ -94,6 +95,11 @@ public class MainMenu extends BasicGameState {
*/
private Image backgroundImage;

/**
* Alpha level for fade-in effect for dynamic backgrounds.
*/
private float dynamicBackgroundAlpha = 0f;

// game-related variables
private StateBasedGame game;
private int state;
Expand Down Expand Up @@ -151,14 +157,18 @@ public void init(GameContainer container, StateBasedGame game)
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g)
throws SlickException {
if (backgroundImage != null)
int width = container.getWidth();
int height = container.getHeight();

// draw background
OsuFile osu = MusicController.getOsuFile();
if (Options.isDynamicBackgroundEnabled() &&
osu != null && osu.drawBG(width, height, dynamicBackgroundAlpha))
;
else if (backgroundImage != null)
backgroundImage.draw();
else
g.setBackground(Utils.COLOR_BLUE_BACKGROUND);
g.setFont(Utils.FONT_MEDIUM);

int width = container.getWidth();
int height = container.getHeight();

// draw buttons
if (logoTimer > 0) {
Expand All @@ -182,6 +192,7 @@ public void render(GameContainer container, StateBasedGame game, Graphics g)
148f * MusicController.getPosition() / MusicController.getTrackLength(), 5, 4);

// draw text
g.setFont(Utils.FONT_MEDIUM);
int lineHeight = Utils.FONT_MEDIUM.getLineHeight();
g.drawString(String.format("Loaded %d songs and %d beatmaps.",
Opsu.groups.size(), Opsu.groups.getMapCount()), 25, 25);
Expand All @@ -207,10 +218,18 @@ else if (MusicController.trackExists()) {
Utils.drawFPS();
Utils.drawCursor();
}

@Override
public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException {
// dynamic backgrounds
if (Options.isDynamicBackgroundEnabled() && dynamicBackgroundAlpha < 0.9f) {
dynamicBackgroundAlpha += delta / 1000f;
if (dynamicBackgroundAlpha > 0.9f)
dynamicBackgroundAlpha = 0.9f;
}

// buttons
if (logoClicked) {
if (logoTimer == 0) { // shifting to left
if (logo.getX() > container.getWidth() / 3.3f)
Expand Down Expand Up @@ -275,10 +294,12 @@ else if (!MusicController.isConverting())
OsuGroupNode node = menu.setFocus(Opsu.groups.getRandomNode(), -1, true);
if (node != null)
previous.add(node.index);
dynamicBackgroundAlpha = 0f;
} else if (musicPrevious.contains(x, y)) {
if (!previous.isEmpty()) {
SongMenu menu = (SongMenu) game.getState(Opsu.STATE_SONGMENU);
menu.setFocus(Opsu.groups.getBaseNode(previous.pop()), -1, true);
dynamicBackgroundAlpha = 0f;
} else
MusicController.setPosition(0);
}
Expand Down
24 changes: 22 additions & 2 deletions src/itdelatrisu/opsu/states/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ public class Options extends BasicGameState {
*/
private static boolean newCursor = true;

/**
* Whether or not dynamic backgrounds are enabled.
*/
private static boolean dynamicBackground = true;

/**
* Game option coordinate modifiers (for drawing).
*/
Expand Down Expand Up @@ -612,6 +617,12 @@ private static void toggleMod(int mod) {
*/
public static boolean isNewCursorEnabled() { return newCursor; }

/**
* Returns whether or not the main menu background should be the current track image.
* @return true if enabled
*/
public static boolean isDynamicBackgroundEnabled() { return dynamicBackground; }

/**
* Returns the current beatmap directory.
* If invalid, this will attempt to search for the directory,
Expand Down Expand Up @@ -718,14 +729,19 @@ public static void parseOptions() {
case "ComboBurst":
showComboBursts = Boolean.parseBoolean(value);
break;

// custom entries
case "Port":
i = Integer.parseInt(value);
if (i > 0 && i <= 65535)
port = i;
break;
case "NewCursor": // custom
case "NewCursor":
newCursor = Boolean.parseBoolean(value);
break;
case "DynamicBackground":
dynamicBackground = Boolean.parseBoolean(value);
break;
}
}
} catch (IOException e) {
Expand Down Expand Up @@ -777,9 +793,13 @@ public static void saveOptions() {
writer.newLine();
writer.write(String.format("ComboBurst = %b", showComboBursts));
writer.newLine();

// custom entries
writer.write(String.format("Port = %d", port));
writer.newLine();
writer.write(String.format("NewCursor = %b", newCursor)); // custom
writer.write(String.format("NewCursor = %b", newCursor));
writer.newLine();
writer.write(String.format("DynamicBackground = %b", dynamicBackground));
writer.newLine();
writer.close();
} catch (IOException e) {
Expand Down
1 change: 0 additions & 1 deletion src/itdelatrisu/opsu/states/SongMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,6 @@ private void startGame(OsuFile osu) {
Display.setTitle(String.format("%s - %s", game.getTitle(), osu.toString()));
OsuParser.parseHitObjects(osu);
SoundController.setSampleSet(osu.sampleSet);
Game.setOsuFile(osu);
Game.setRestart(Game.RESTART_NEW);
game.enterState(Opsu.STATE_GAME, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
}
Expand Down

0 comments on commit 5612336

Please sign in to comment.