Skip to content

Commit

Permalink
(#20) Added examples
Browse files Browse the repository at this point in the history
- polygon2d - Creating Polygon2D objects
- text2d - Creating Text2D objects
- model2d - Creating Model2D objects
- modelreadwrite - Reading & Writing Model2D objects
- logging - FastJ logging
- fastjengineconfig - FastJEngine Configuration
  • Loading branch information
lucasstarsz committed May 25, 2021
1 parent b21a591 commit e612bef
Showing 6 changed files with 402 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package io.github.lucasstarsz.fastj.example.fastjengineconfig;

import io.github.lucasstarsz.fastj.engine.FastJEngine;
import io.github.lucasstarsz.fastj.engine.HWAccel;
import io.github.lucasstarsz.fastj.math.Point;
import io.github.lucasstarsz.fastj.math.Pointf;
import io.github.lucasstarsz.fastj.graphics.Display;
import io.github.lucasstarsz.fastj.graphics.DrawUtil;
import io.github.lucasstarsz.fastj.graphics.game.Polygon2D;

import io.github.lucasstarsz.fastj.systems.control.SimpleManager;

public class Main extends SimpleManager {
@Override
public void init(Display display) {
/* As a small aside, this is a small inclusion in order to show how configuring FastJ
* affects rendering.
* The code is not the primary focus of the example -- this is just to give a visual. */
Pointf[] squareMesh = DrawUtil.createBox(50f, 50f, 100f);
Polygon2D square = new Polygon2D(squareMesh);
drawableManager.addGameObject(square);
}

@Override
public void update(Display display) {
// Empty -- this example does not make use of this method.
}

public static void main(String[] args) {
FastJEngine.init("Hello, Configuration!", new Main());


/* FastJEngine Configuration */

/* When using the other examples or FastJ in general, you may have noticed that the game
* engine defaults to a 1280*720 window. This is part of FastJEngine's default
* configuration.
*
* With that said, let's get started with the first two types of configuration:
* 1. configureViewerResolution
* 2. configureInternalResolution
*
* Note 1: Both of these configurations require use of a Point. A Point is similar to that
* of a Pointf, but it only allows integer values.
* Note 2: Configurations should always be done after initializing the game engine.
*/


/* "FastJEngine#configureViewerResolution" configures the viewer resolution -- the size of
* the window that the player plays in.
* By default, this is configured to 720p -- 1280*720. Of course, you can change this to
* any set of values you would like, as long as both values are at least 1.
*
* For this example, I've set it to 640*480. Feel free to mess around with the numbers! */
FastJEngine.configureViewerResolution(new Point(640, 480));

/* "FastJEngine#configureInternalResolution" configures the internal resolution -- the size
* of the actual game canvas (where the game gets rendered).
* By default, this is also configured to 720p -- 1280*720. Like the viewer resolution,
* this can be set to any values that are at least 1.
*
* For this example, I've set this to 640*480. Feel free to mess around with the numbers! */
FastJEngine.configureInternalResolution(new Point(640, 480));


/* Now, we'll move onto configureHardwareAcceleration.
* By making use of java2d, FastJ supports a few hardware-accelerated graphics APIs:
* - OpenGL
* - Direct3D
*
* With that in mind, "FastJEngine#configureHardwareAcceleration" allows you to configure
* the type of hardware acceleration your game uses. This is set using the "HWAccel" enum.
*
* For this example, we're going to set the hardware acceleration to OpenGL. */
FastJEngine.configureHardwareAcceleration(HWAccel.OpenGL);


FastJEngine.run();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.github.lucasstarsz.fastj.example.logging;

import io.github.lucasstarsz.fastj.engine.FastJEngine;

public class Main {
public static void main(String[] args) {
/* FastJ Logging */

/* FastJ's logging solution is not very robust just yet. It currently makes use of the
* standard out/err streams (System.out and System.err). While not performant, it is currently sufficient for the engine's usage. */

/* First up, "FastJEngine#log". This method prints out in this style:
* "INFO: <your message here, without the braces>" */

FastJEngine.log("Hello! This is an informational logging statement.");

/* Next, "FastJEngine#warning". This method prints out in this style:
* "WARNING: <your message here, without the braces>" */

FastJEngine.warning("Be careful now! This is a warning.");

/* Lastly, "FastJEngine#error". This method is special, because it not only prints out the
* error -- it crashes the engine with an exception as well.
* This method prints in this style:
* "ERROR: <your message here, without the braces>"
* "Exception <the rest of the stack trace would be here>" */

/* Under normal circumstances, this method also requires that you add an exception -- the
* exception that caused a need to call "FastJEngine#error".
* For the sake of demonstration, we will create an exception ourselves. */
IllegalStateException exampleException = new IllegalStateException("This is an example exception.");
FastJEngine.error("Oh dear, looks like something went wrong. This is an error message.", exampleException);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.github.lucasstarsz.fastj.example.model2d;

import io.github.lucasstarsz.fastj.engine.FastJEngine;
import io.github.lucasstarsz.fastj.math.Pointf;
import io.github.lucasstarsz.fastj.graphics.Display;
import io.github.lucasstarsz.fastj.graphics.DrawUtil;
import io.github.lucasstarsz.fastj.graphics.game.Model2D;
import io.github.lucasstarsz.fastj.graphics.game.Polygon2D;

import io.github.lucasstarsz.fastj.systems.control.SimpleManager;

public class Main extends SimpleManager {
@Override
public void init(Display display) {
/* Model2D */

/* WARNING: If you're not familiar with FastJ, you'll need to read through the
* "polygon2d" example first.
*
* In order to create a Model2D, you'll need an array of Polygon2D objects.
* A Model2D is essentially a collection of Polygon2D objects, and it treats those objects
* as one game object. */

// We'll create two squares -- just like the ones from the "hellopolygon2d" example.
Pointf[] smallSquareMesh1 = DrawUtil.createBox(0f, 0f, 50f);
Polygon2D smallSquare1 = new Polygon2D(smallSquareMesh1);

// To visualize the two squares as different, we'll change the second one up a bit.
Pointf[] smallSquareMesh2 = DrawUtil.createBox(50f, 50f, 25f);
Polygon2D smallSquare2 = new Polygon2D(smallSquareMesh2);

// In order to create our Model2D, we need those squares in an array.
Polygon2D[] smallSquaresArray = {smallSquare1, smallSquare2};

// Now, we can create our Model2D object.
Model2D smallSquares = new Model2D(smallSquaresArray);

/* Super simple! Now, this alone does not cause the model to render to the screen. In order
* for it to be rendered, you need to add it as a game object to the drawable manager. */
drawableManager.addGameObject(smallSquares);
}

@Override
public void update(Display display) {
// Empty -- this example does not make use of this method.
}

public static void main(String[] args) {
FastJEngine.init("Hello, Model2D!", new Main());
FastJEngine.run();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package io.github.lucasstarsz.fastj.example.modelreadwrite;

import io.github.lucasstarsz.fastj.engine.FastJEngine;
import io.github.lucasstarsz.fastj.math.Pointf;
import io.github.lucasstarsz.fastj.graphics.DrawUtil;
import io.github.lucasstarsz.fastj.graphics.game.Model2D;
import io.github.lucasstarsz.fastj.graphics.game.Polygon2D;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
public static void main(String[] args) {
/* Model Reading/Writing */

/* WARNING: If you're not familiar with FastJ, you'll need to read through the
* "model2d" example first.
*
* Creating models from pure code is convenient, but quite cumbersome to read through. Let's
* learn how to read and write them to a file format instead. */

// We'll start with the model code from the "model2d" example.
Pointf[] smallSquareMesh1 = DrawUtil.createBox(0f, 0f, 50f);
Polygon2D smallSquare1 = new Polygon2D(smallSquareMesh1);

Pointf[] smallSquareMesh2 = DrawUtil.createBox(50f, 50f, 25f);
Polygon2D smallSquare2 = new Polygon2D(smallSquareMesh2);

Polygon2D[] smallSquaresArray = {smallSquare1, smallSquare2};
Model2D smallSquares = new Model2D(smallSquaresArray);


/* Now that that's out of the way, let's write this Model2D to a file.
* DrawUtil contains the function for writing Model2D objects to a file. With this, we'll
* write the file, and then print out the contents.
*
* Model2D objects can be written to the PSDF (Polygon Structural Data Format) format,
* which is a format written specifically for use with the FastJ engine.
* At a later point in time, the engine will support writing Model2D objects to more
* standard formats, but until then we'll be using the PSDF format.
*/


/* This is the path which we will be writing to. It resolves to
* "src/example/resources/modelreadwrite/smallSquares.psdf". */
String smallSquaresFilePath = "src/example/resources/modelreadwrite/smallSquares.psdf";

// And now, we can call the model-writing method.
DrawUtil.writeToPSDF(smallSquaresFilePath, smallSquares);


/* As a small aside, this gets the file and prints out its contents.
* The code is not the primary focus of the example -- this is just to give a visual. */
try {
String smallSquaresFileContents = Files.readString(Path.of(smallSquaresFilePath));
FastJEngine.log("Contents of smallSquares.psdf: \n" + smallSquaresFileContents);
} catch (IOException exception) {
exception.printStackTrace();
}


/* Next up, we'll read the file back.
* Since we already have the string denoting the location of the file, we can reuse it. */
Polygon2D[] smallSquaresArrayFromFile = DrawUtil.load2DModel(smallSquaresFilePath);

// We can use this to create a new Model2D.
Model2D smallSquaresFromFile = new Model2D(smallSquaresArrayFromFile);

/* Here, we check for equality between the original and the read file.
* As expected, the two contain the exact same contents. */
FastJEngine.log("smallSquaresfromFile == smallSquares? " + smallSquaresFromFile.equals(smallSquares));


/* Another small aside: this removes the file we created earlier in the program.
* This code is not the primary focus of the example -- it's just to clean up the work we did. */
try {
boolean wasDeleted = Files.deleteIfExists(Path.of(smallSquaresFilePath));
if (!wasDeleted) {
FastJEngine.warning("The file at \"" + smallSquaresFilePath + "\" was not deleted.");
}
} catch (IOException exception) {
exception.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package io.github.lucasstarsz.fastj.example.polygon2d;

import io.github.lucasstarsz.fastj.engine.FastJEngine;
import io.github.lucasstarsz.fastj.math.Pointf;
import io.github.lucasstarsz.fastj.graphics.Display;
import io.github.lucasstarsz.fastj.graphics.DrawUtil;
import io.github.lucasstarsz.fastj.graphics.game.Polygon2D;

import io.github.lucasstarsz.fastj.systems.control.SimpleManager;

import java.awt.Color;

public class Main extends SimpleManager {

@Override
public void init(Display display) {
/* Polygon2D */

/* In order to create a Polygon2D, you first need to create a mesh.
* A mesh is essentially an array of "Pointf"s (vectors) that form a shape when drawn in
* order. This mesh is the base upon which the Polygon2D can be constructed, and defines
* what sort of shape gets drawn to the screen. */

// You can create a mesh by hand...
Pointf[] smallSquareMeshByHand = {
new Pointf(0f, 0f),
new Pointf(0f, 50f),
new Pointf(50f, 50f),
new Pointf(50f, 0f)
};

// ...or by using one of the many "DrawUtil.create" methods.
Pointf[] smallSquareMeshFromDrawUtil = DrawUtil.createBox(0f, 0f, 50f);

/* The two meshes above result in the exact same thing: a square at the point (0, 0) with a
* size of 50. */


/* Now, we can create our Polygon2D. We'll use smallSquareMeshByHand for right now, but you
* should try switching from it to smallSquareMeshFromDrawUtil to see what happens! */
Polygon2D smallSquare = new Polygon2D(smallSquareMeshByHand);

/* Super simple! Now, this alone does not cause the square to render to the screen. In
* order for it to be rendered, you need to add it as a game object to the drawable
* manager. */
drawableManager.addGameObject(smallSquare);

/* If you comment out the line above, you'll see that the small square does not get
* rendered. */


/* You can set the following properties of a Polygon2D:
* - Mesh (Pointf[])
* - Color
* - PaintFilled (render the outline or fill)
* - Rotation
* - Scale
* - Translation
*
* To show this off, we'll create a larger square with the following property values:
* - Square mesh at (625, 25) with a size of 47.9
* - Blue color
* - Outline
* - Rotation of 30 degrees
* - Scaled to 50% (0.5)
* - Translated by (20, 10) */
Pointf[] largeSquareMesh = DrawUtil.createBox(625f, 25.5f, 47.9f);
Polygon2D largeSquare = new Polygon2D(largeSquareMesh)
.setColor(Color.blue)
.setFilled(false);

/* As you can see from the code above, many of the methods Polygon2D contains allow for
* method chaining.
* From the code below, you can see that other methods do not. */

largeSquare.rotate(30f);
largeSquare.scale(new Pointf(0.5f, 0.5f));
largeSquare.translate(new Pointf(20f, 10f));

drawableManager.addGameObject(largeSquare);
}

@Override
public void update(Display display) {
// Empty -- this example does not make use of this method.
}

public static void main(String[] args) {
FastJEngine.init("Hello, Polygon2D!", new Main());
FastJEngine.run();
}
}
Loading

0 comments on commit e612bef

Please sign in to comment.