Fork me on GitHub

Using Resources

Decrease your memory footprint by using resource wrapper

Resources

The JRebirth Framework provides an useful way to deal with your local resources, we currently support :

These resources can consume a lot of memory if you don’t dispose them (especially big images and fonts) when you stop using them. JRebirth provides a mechanism to store them weakly and to rebuild them if necessary in order to use the less memory as required. To do this JRebirth stores a ResourceParam lightweight object that contains all information to create the heavyweight resource. This one is linked by a ResourceItem to facilitate its usage.

UML Diagram Overview:

Resource loading

Heavyweight resource object can be retrieved by calling the get() method of ResourceItem. If the resource was already loaded it is retrieved from cache otherwise it’s loaded another time and stored weakly. When system goes low memory, all resrouces not strictly retained by a Component or a Scene’s node will elligible for garbage collection to release some bits.

ResourceItem

There are 3 ways to declare resources, each one is available for all kind of resources with special feature for parameters.

The first way is to hold static field declaration instantiated with custom ‘factory’ Resources (not named, with an overloaded create method to avoid later cast). This static fields can be hold in any class you want but we recommend to store them into an Interface used ‘as’ an enum. It’s better to have an interface per resource type to avoid big file that blends all resources. Another interesting trick, is to add a static import on related static Resource.create method to shorten the resource declaration (Ctrl+Shift+M with Eclipse).

Static import declaration:

19
import static org.jrebirth.af.core.resource.Resources.create;

Example of Web color declaration:

35
36
/** The web color. */
ColorItem TEST_COLOR_WEB_1 = create(new WebColor("0088D3"));

The other way to declare is a little bit complex, it implies to create an enum that implements a ResourceItem interface. But it requires to add use a custom syntax to use default method.

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public enum EnumColors implements ColorEnum {
  
    // @formatter:off
  
    /** Color for slide title, white. */
    SLIDE_TITLE {{ web("FFFFFF", 1.0); }},
  
    /** Color for blue shape, xxx. */
    SHAPE_BLUE{{ web("3495CE", 1.0); }},
  
    /** Color for drop shadow, black. */
    DROP_SHADOW{{ web("000000", 0.8); }},
  
    /** Color for inner shadow, white. */
    INNER_SHADOW{{ web("FFFFFE", 0.3); }};
  
}

Severals default methods exist, at least one per ResourceParams to create.

We can load dynamicalli resource by creating “anonymous” ResourceItem, the resource will be kept within cache if memory is not constrained otherwise it will be deleted. Another call with the same path will hit the cache and the image will be only stored once, but created as many times as deleted and retrieved again.

524
final Image image = Resources.create(new RelImage(item.getImage())).get();

In this sample item.getImage() return the relative srtring path of the image.

Samples

For example to manage web color, (basic hexadecimal string #00CC00), you have to use this declaration:

35
36
/** The web color. */
ColorItem TEST_COLOR_WEB_1 = create(new WebColor("0088D3"));

For example to manage the Turtles font, you have to use this declaration:

38
39
/** The real font. */
FontItem TEST_REAL_FONT_1 = create(new RealFont(TestFontNames.Turtles, 10.0));

Hereafter the enumeration that stores font names.

10
11
12
13
14
15
16
17
public enum TestFontNames implements FontName {
  
    /** Turtles. */
    Turtles,
  
    /** Report 1942. */
    Report_1942,
}