Skip to content

Commit

Permalink
feat(rm): add resource manager
Browse files Browse the repository at this point in the history
  • Loading branch information
mcgivrer committed Mar 3, 2023
1 parent 0b9ac91 commit 28de2cd
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public static void load(String path) {
if (!path.contains(".")) {
return;
}
switch (path.substring(path.lastIndexOf(".") + 1, path.length() - (path.lastIndexOf(".") + 1)).toUpperCase()) {
String ext = path.substring(path.lastIndexOf(".") + 1, path.length()).toUpperCase();
switch (ext) {
case "PNG", "JPG", "GIF" -> {
BufferedImage img = null;
if (!cache.containsKey(path)) {
Expand Down
20 changes: 3 additions & 17 deletions app/src/main/java/fr/snapgames/demo/gdemoapp/scenes/DemoScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fr.snapgames.demo.core.entity.*;
import fr.snapgames.demo.core.gfx.RandomColor;
import fr.snapgames.demo.core.gfx.Renderer;
import fr.snapgames.demo.core.io.ResourceManager;
import fr.snapgames.demo.core.math.Vector2D;
import fr.snapgames.demo.core.physic.Material;
import fr.snapgames.demo.core.scene.AbstractScene;
Expand Down Expand Up @@ -41,25 +42,10 @@ public DemoScene(Game g, String name) {
}


/**
* Load a {@link BufferedImage} from a file path.
*
* @param pathToImage path to the image file.
* @return the corresponding {@link BufferedImage} instance.
*/
private BufferedImage loadImage(String pathToImage) {
try {
return ImageIO.read(this.getClass().getResourceAsStream(pathToImage));
} catch (NullPointerException | IOException e) {
logger.severe("Unable to read image resource from " + pathToImage);
}
return null;
}

@Override
public void prepare(Game g) {
imageBackground = loadImage("/images/backgrounds/forest.jpg");
imagePlayer = loadImage("/images/sprites01.png");
imageBackground = ResourceManager.getImage("/images/backgrounds/forest.jpg");
imagePlayer = ResourceManager.getImage("/images/sprites01.png");
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion docs/chapter-10-adding_scene_and_scenemanager.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The following diagram propose an implementation layering on a `Scene` interface,
provide
internal mechanism, and a `SceneManager`.

![Implementation class diagram](https://www.plantuml.com/plantuml/png/TP11RkCm34NtEeLcby5CBk15aQ85kkgcEG4Z_Te295aWgQHeqRlNhemuJT0DORx_QDhSYH9hJT7yqG49jaGAsGp1moReC7ff3QboE5I561T9n3vHWWbPmQ_aOFVWOt1Omhr3nZDbwi4sf1AHa5NEbWpZgKdBblLD3NyO-o_ae85YUCUcx-PrVxlZ6FoK54OXli6EGNf3GovLhs4jVbN_MqGg0j_viHxYQdccqzdXxWNPWy5h7gICWiqPzD27IMQQK7imdyRGGPJ9gMt7kL_Qzl-Nrvc1QT8ZqjW4l2colnepypVPxVRknwdDPVETpbhNJ-KgMkOwcQly4hhAZAncX-nhYby0)
![Implementation class diagram](http://www.plantuml.com/plantuml/png/TP11JWCn34NtEKNO5K4lC0jK118hDd02T_APiPHa8jkX2CJTwJIZjch59hBV_rxaRTH8hAReRcn4WY-HEBkH0to2ZLFGjf2PyBOQrFMHLFqB9IpZUu-CLyeqG1ndWtnGZuP_dB_ZpCOK-HkhePllRhlFYSC-AO92SeLx0TaLvLogNS9Qmgbwxn6fA48NfhgDfrOGsXqxqhqX1mZabBEMIR4wWW7sH0b2UzG6z-jqd7RjLuiRHrireTvy4_6uhAt3gKGaP5iCBRQUaQPMm9SXXuipTsxJhzSF1zFTCEkUfmCwxrmSB66dywl_X7qLGJQtGGurnJy0)

## Scene interface

Expand Down
52 changes: 52 additions & 0 deletions docs/chapter-11-add_a_resource_manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,55 @@ public class ResourceManager {
}
}
```
## Using the ResourceManager

In your own `Scene` implementation, you can now preload some resources in the `prepare()`
method:

```java
class MyScene extends AbstractScene{

//...
@Override
public void prepare(Game g) {
// load resources int cache
ResourceManager.getImage("/images/backgrounds/forest.jpg");
ResourceManager.getImage("/images/sprites01.png");
}
//...
}
```

And during the Scene creation using the `create()` method, you can get the preloaded resources :

```java
class MyScene extends AbstractScene {
//...
@Override
public void create(Game g) {
//...
// Create the main player entity.
var playerFrame1 = ResoureManager.getImage("/images/sprites01.png");
var playerFrame1 = imagePlayer.getSubimage(0, 0, 32, 32);
var player = (GameObject) new GameObject("player")
.setImage(playerFrame1);
//...
}
//...
}
```

So each time you re-activate this scene, resources are already in cache, so no wait to display the scene.

## Conclusion

We add hee avery useful service to load and cache some resources. Then, those resources can be used or reused by mulitple scene,
reducing the loading time.

Like in the 10 previous episodes, you can access the code from the GitHub repository you already know
now: https://github.com/SnapGames/game101 on
tag [create-resource-manager](https://github.com/SnapGames/game101/releases/tag/create-resource-manager).

That’s all falk!

McG.

0 comments on commit 28de2cd

Please sign in to comment.