Skip to content

Commit

Permalink
feat(AddCapture): AddScreenshot function
Browse files Browse the repository at this point in the history
- update Game to be a KeyListener
- add Render save buffer function
- update InputHandler

fix #10
  • Loading branch information
mcgivrer committed Aug 1, 2021
1 parent 10b165a commit b8912f2
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 7 deletions.
34 changes: 30 additions & 4 deletions src/main/java/fr/snapgames/fromclasstogame/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -23,7 +24,7 @@
* @author Frédéric Delorme<[email protected]>
* @since 0.0.1
*/
public class Game {
public class Game implements KeyListener {

private static final Logger logger = LoggerFactory.getLogger(Game.class);

Expand Down Expand Up @@ -53,8 +54,6 @@ public class Game {
* the mandatory default constructor
*/
public Game() {
renderer = new Render(320, 200);
window = new Window("", 320, 200);
}

/**
Expand All @@ -81,6 +80,7 @@ public void initialize(String[] argv) throws UnknownArgumentException {
renderer = new Render(this.width, this.height);
window = new Window(this.title, (int) (this.width * this.scale), (int) (this.height * this.scale));
inputHandler = new InputHandler(window);
inputHandler.addKeyListener(this);
}

public void loadDefaultValues() {
Expand Down Expand Up @@ -204,7 +204,7 @@ private void loop() {
*/
private void input() {
if (inputHandler.getKey(KeyEvent.VK_ESCAPE)) {
this.exit = true;

}
}

Expand Down Expand Up @@ -280,4 +280,30 @@ public static void main(String[] argc) {
logger.error("Unable to run the game", e);
}
}

@Override
public void keyTyped(KeyEvent e) {
// nothing to do now
}

@Override
public void keyPressed(KeyEvent e) {
// nothing to do now

}

@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_F11:
renderer.saveScreenshot();
break;
case KeyEvent.VK_ESCAPE:
this.exit = true;
break;
default:
break;
}

}
}
4 changes: 4 additions & 0 deletions src/main/java/fr/snapgames/fromclasstogame/InputHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public boolean getKey(int code) {
return keys[code];
}

public void addKeyListener(KeyListener kl) {
window.getFrame().addKeyListener(kl);
}

}
31 changes: 31 additions & 0 deletions src/main/java/fr/snapgames/fromclasstogame/Render.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

public class Render {

private BufferedImage buffer;
static int screenShotIndex = 0;

private List<GameObject> objects = new ArrayList<>();

Expand Down Expand Up @@ -64,4 +72,27 @@ public void clear() {
public BufferedImage getBuffer() {
return this.buffer;
}

/**
* Save a screenshot of the current buffer.
*/
public void saveScreenshot() {
final String path = this.getClass().getResource("/").getPath().substring(1);
Path targetDir = Paths.get(path + "/screenshots");
int i = screenShotIndex++;
String filename = String.format("%sscreenshots/%s-%d.png",path,java.lang.System.nanoTime(),i);

try {
if (!Files.exists(targetDir)) {
Files.createDirectory(targetDir);
}
File out = new File(filename);
ImageIO.write(getBuffer(), "PNG", out);

System.out.println(String.format("Write screenshot to %s", filename));
} catch (IOException e) {
System.err.println(String.format("Unable to write screenshot to %s:%s", filename, e.getMessage()));
}
}

}
10 changes: 7 additions & 3 deletions src/main/java/fr/snapgames/fromclasstogame/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
import java.awt.Point;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

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

public class Window {
Expand Down Expand Up @@ -41,14 +47,11 @@ public void draw(long realFPS, BufferedImage img) {
if (debugFont == null) {
debugFont = g.getFont().deriveFont(Font.CENTER_BASELINE, 8);
}
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(img, 0, 30, frame.getWidth(), frame.getHeight(), 0, 0, img.getWidth(), img.getHeight(), null);

g.setFont(debugFont);
g.setColor(Color.ORANGE);
g.drawString(String.format("FPS:%03d", realFPS), 10, frame.getHeight() - 30);

g.dispose();
}

Expand All @@ -59,4 +62,5 @@ public JFrame getFrame() {
public void close() {
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}

}
5 changes: 5 additions & 0 deletions src/test/java/features/GameDefSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public class GameDefSteps {
@Given("the Game is instantiated")
public void givenTheGameIsInstantiated() {
game = new Game();
try {
game.initialize(null);
} catch (UnknownArgumentException e) {
fail("Unable to initialize the game");
}
game.testMode = true;
}

Expand Down

0 comments on commit b8912f2

Please sign in to comment.