Skip to content

Commit

Permalink
Remove logo splash screen
Browse files Browse the repository at this point in the history
  • Loading branch information
BenCheung0422 authored and ChristofferHolmesland committed Jul 8, 2023
1 parent b094de0 commit 6c4734a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 99 deletions.
2 changes: 2 additions & 0 deletions src/client/java/minicraft/core/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import minicraft.core.io.Settings;
import minicraft.core.io.Sound;
import minicraft.entity.mob.Player;
import minicraft.gfx.Screen;
import minicraft.level.Level;
import minicraft.level.tile.Tiles;
import minicraft.network.Analytics;
Expand Down Expand Up @@ -110,6 +111,7 @@ public static void main(String[] args) {
Updater.updateFullscreen();
}

Initializer.launchWindow();
// Actually start the game.
Initializer.run();

Expand Down
102 changes: 6 additions & 96 deletions src/client/java/minicraft/core/Initializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,16 @@

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.LinearGradientPaint;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Objects;

public class Initializer extends Game {
private Initializer() {}
Expand All @@ -33,7 +27,6 @@ private Initializer() {}
* Reference to actual frame, also it may be null.
*/
static JFrame frame;
static LogoSplashCanvas logoSplash = new LogoSplashCanvas();
static int fra, tik; // These store the number of frames and ticks in the previous second; used for fps, at least.

public static JFrame getFrame() { return frame; }
Expand Down Expand Up @@ -120,12 +113,10 @@ static void createAndDisplayFrame() {
Renderer.canvas.setMinimumSize(new java.awt.Dimension(1, 1));
Renderer.canvas.setPreferredSize(Renderer.getWindowSize());
Renderer.canvas.setBackground(Color.BLACK);
logoSplash.setMinimumSize(new java.awt.Dimension(1, 1));
logoSplash.setPreferredSize(Renderer.getWindowSize());
JFrame frame = Initializer.frame = new JFrame(NAME);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout()); // Sets the layout of the window
frame.add(logoSplash, BorderLayout.CENTER);
frame.add(Renderer.canvas, BorderLayout.CENTER); // Adds the game (which is a canvas) to the center of the screen.
frame.pack(); // Squishes everything into the preferredSize.

try {
Expand Down Expand Up @@ -157,94 +148,13 @@ public void windowClosing(WindowEvent e) {
quit();
}
});

frame.setVisible(true);
logoSplash.setDisplay(true);
logoSplash.renderer.start();
}

private static class LogoSplashCanvas extends JPanel {
private Image logo;

{
try {
logo = ImageIO.read(Objects.requireNonNull(Initializer.class.getResourceAsStream("/assets/textures/gui/title.png")));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private int transparency = 255;
private boolean display = false;
private boolean inAnimation = false;
private boolean interruptWhenAnimated = false;

public Thread renderer = new Thread(() -> {
do {
repaint();
if (interruptWhenAnimated && !inAnimation) break;
} while (!Initializer.logoSplash.renderer.isInterrupted());
}, "Logo Splash Screen Renderer");

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
final int w = g.getClipBounds().width;
final int h = g.getClipBounds().height;

// Drawing colorful background.
Graphics2D g2d = (Graphics2D) g;
int n = 6;
float[] fractions = new float[n];
Color[] colors = new Color[n];
for (int x = 0; x < n; x++) {
double sin = Math.sin(Math.PI * (255-transparency)/255.0);
double cos = Math.cos(Math.PI * x/n);
double hue = Math.abs((sin*210 + cos*45) % 360) / 360;
double s = 1 - Math.pow(Math.min(Math.sin(Math.cos(Math.abs(sin - cos))), 1), 4);
fractions[x] = (float) Math.sin((double) x/n * Math.PI/2) * x/n;
colors[x] = Color.getHSBColor((float) hue, (float) s, 1);
}
g2d.setPaint(new LinearGradientPaint(0, 0, w, 0, fractions, colors));
g2d.fillRect(0, 0, w, h);

// Drawing the centered logo.
if (transparency < 255) g.drawImage(logo, w/2 - logo.getWidth(frame)*2, h/2 - logo.getHeight(frame)*2, logo.getWidth(frame)*4, logo.getHeight(frame)*4, frame);

// Fading effect.
g.setColor(new Color(0, 0, 0, Math.max(Math.min(255 - (int) (Math.cos(transparency/255.0 * Math.PI) * 255), 255), 0)));
g.fillRect(0, 0, w, h);

if (inAnimation) {
if (display) {
if (transparency > 0) transparency -= 5;
else inAnimation = false;
} else {
if (transparency < 255) transparency += 5;
else inAnimation = false;
}
}
}

public void setDisplay(boolean display) {
this.display = display;
inAnimation = true;
}
}

/** Remove the logo splash screen and start canvas rendering. */
static void startCanvasRendering() {
logoSplash.setDisplay(false);
logoSplash.interruptWhenAnimated = true;
try {
logoSplash.renderer.join();
} catch (InterruptedException ignored) {}
logoSplash.renderer.interrupt();
frame.remove(logoSplash);
frame.add(Renderer.canvas, BorderLayout.CENTER); // Adds the game (which is a canvas) to the center of the screen.
frame.pack();
frame.revalidate();
logoSplash = null; // Discard the canvas.
/** Launching the main window. */
static void launchWindow() {
frame.setVisible(true);
frame.requestFocus();
Renderer.canvas.requestFocus();
}

/**
Expand Down
3 changes: 0 additions & 3 deletions src/client/java/minicraft/core/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,7 @@ public static void initScreen() {
screen.pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
hudSheet = new LinkedSprite(SpriteType.Gui, "hud");

Initializer.startCanvasRendering();
canvas.createBufferStrategy(3);

canvas.requestFocus();
}


Expand Down

0 comments on commit 6c4734a

Please sign in to comment.