[JAVA] A note on the libGDX Utils class

There are several utility classes in libGDX. Is it really convenient? .. .. I would like to summarize it because it is a great deal, including the survey.

Target version

1.9.5

Target package

com.balogic.gdx.utils

For the time being, I will summarize the Utils system.

NumberUtils

It performs Floating-point related conversions that comply with IEEE 754. It's close to an alias for the static methods of Double and Float. I tried to touch it, but it seems difficult to use. It is used inside the com.badlogic.gdx.graphics.Color class.

I think it's best to look at the float and double javadoc for details.

https://docs.oracle.com/javase/jp/8/docs/api/java/lang/Float.html https://docs.oracle.com/javase/jp/6/api/java/lang/Double.html#doubleToLongBits(double)

TimeUtils

TimeUtils is a utility class for basic time manipulation. If you want to implement a common time operation instantly, it seems better to implement it after seeing the method of this class.



    // System.currentTimeMillis()Same as
    System.out.println("millisecond: " + TimeUtils.millis()); // millisecond: 1484574498965
            
    //Convert milliseconds to nanoseconds
    System.out.println("Convert milliseconds to nanoseconds: " + TimeUtils.millisToNanos(TimeUtils.millis())); // Convert milliseconds to nanoseconds: 1484574498965000000

    //Convert nanoseconds to milliseconds
    System.out.println("Convert nanoseconds to milliseconds: " + TimeUtils.nanosToMillis(TimeUtils.millisToNanos(TimeUtils.millis()))); // Convert nanoseconds to milliseconds: 1484574498965

    //Elapsed time from the argument(millisecond)
    System.out.println("2011/12/5 11:Elapsed time from 11 (milliseconds)" + TimeUtils.timeSinceMillis(TimeUtils.nanosToMillis(LocalDateTime.of(2011, 12, 5, 11, 11).getNano()))); // 2011/12/5 11:Elapsed time from 11 (milliseconds)1484574498976

    //Elapsed time from the argument(Nanoseconds)
    System.out.println("2011/12/5 11:Elapsed time from 11 (nanoseconds)" + TimeUtils.timeSinceNanos(LocalDateTime.of(2011, 12, 5, 11, 11).getNano())); // 2011/12/5 11:Elapsed time from 11 (nanoseconds)872694705378791

PropertiesUtils

PropertiesUtils is a Util that loads the information of Reader instance in key = value format and converts it to Map. You can easily load it with information in the properties file.

hoge.properties


hoge=fuga
piyo=piyo
age=28
    ObjectMap<String, String> map = new ObjectMap<>();
    PropertiesUtils.load(map, new FileReader(Gdx.files.internal("hoge.properties").file()));
    map.forEach(System.out::println);

Output result


piyo=piyo
hoge=fuga
age=28

ScreenUtils

ScreenUtils is a utility class for image processing that mainly uses the frame buffer.

What is Farmbuffer?

A framebuffer (frame buffer, or sometimes framestore) is a portion of RAM containing a bitmap that is used to refresh a video display from a memory buffer containing a complete frame of data.

https://en.wikipedia.org/wiki/Framebuffer

So, if you interpret it properly, I think you should think about it as the memory area of the frame drawing information!


    //byte the default framebuffer[]Return with. The length of the array is screen width* height * 4。
    byte[] bytes = ScreenUtils.getFrameBufferPixels(true);


    //Reads the specified position from the framebuffer and returns a Pixmap
    int x = 0;
    int y = 0;
    int width = 0;
    int height = 0;
    Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(x, y, width, height);

    //Generate Texture from framebuffer
    TextureRegion region = ScreenUtils.getFrameBufferTexture();

This class only works on the libGDX engine. Since the information being started is simply acquired, a nullpo will occur if there is no acquisition source.

BufferUtils

BufferUtils is a utility class for processing that uses the buffer area, and its main methods are copy and transform. Overloaded copy and transform methods are provided for various buffer data types. There is also a factory method. (BufferUtils.newByteBuffer etc.)


    //Factory method
    ByteBuffer src = BufferUtils.newByteBuffer(1024);
    ByteBuffer dest = BufferUtils.newByteBuffer(1024);
            
    //copy
    BufferUtils.copy(src, dest, 1024);

I've tried it, but it only works on the libGDX engine. For example, if you simply try to execute it from the main method, an Error will occur around BufferUtils.copy. Looking at the stack trace java.lang.UnsatisfiedLinkError: com.badlogic.gdx.utils.BufferUtils.copyJni(Ljava/nio/Buffer;ILjava/nio/Buffer;II)V ... apparently ...

ThreadUtils

This class currently has only yield methods as of 1.9.5 and this implementation I think that there is no usage scene because it only calls the yield of java.lang.Thread. Rather, it is difficult to judge the usage scene.

According to javadoc in java.lang.Thread https://docs.oracle.com/javase/jp/8/docs/api/java/lang/Thread.html

public static void yield() A hint to the scheduler that the current thread is ready to yield the current processor usage. The scheduler can ignore this hint. Concessions are heuristic attempts to improve relative progress between threads that would otherwise overuse CPU. When using it, you should also combine detailed profiles and benchmarks to make sure that you are actually getting the desired effect.

There are rare opportunities to use this method. This can be useful for debugging and testing purposes (it can be useful for reproducing bugs due to race conditions). This can also be useful when designing concurrency control components, such as those in the java.util.concurrent.locks package.

The yield method seems to make a native call and notify the scheduler. 

StreamUtils

StreamUtils is not a Stream API, but a utility class for manipulating the Stream interface for file I / O.




    Path tmpFile = Files.createTempFile(Paths.get("").toAbsolutePath(), "tmp", "file");
    tmpFile.toFile().deleteOnExit();
    StreamUtils.copyStream(Gdx.files.internal("hoge.properties").read(), new FileOutputStream(tmpFile.getFileName().toString()));
    InputStream is = Gdx.files.internal("hoge.properties").read();

    //Convert to String
    String streamToString = StreamUtils.copyStreamToString(is);
    System.out.println("streamToString" + streamToString);

    //close process
    StreamUtils.closeQuietly(is);

closeQuietly is also in IOUtils of Apache Commons. If it is Java SE 7 or later, it will not be very active because there are try-with-resources, but before that, it is a nice guy to reduce the description of redundant close processing.

Summary

It seems that TimeUtils, PropertiesUtils, and StreamUtils can be used immediately. Other than that, the layers used are limited, or it seems that it is not used in client implementation.

This time I limited my research to XXXUtils, but com.balogic.gdx.utils There are various APIs such as com.balogic.gdx.utils.async, collection API, Json, XML operation, text formatting API, etc., so I think I'll summarize them if I feel like it in another article. ..

Recommended Posts

A note on the libGDX Utils class
A review note for the class java.util.Optional
A review note for the class java.util.Objects
A rudimentary note on the Fibonacci sequence
A note about the scope
A note about the seed function of Ruby on Rails
Note on the path of request.getRequestDispatcher
Put a badge on the icon
A murmur about the utility class
A review note for the package java.time.temporal
A note on the differences between interfaces and abstract classes in Java
Come out with a suffix on the method
A note when the heroku command becomes unavailable
Come out with a suffix on the method 2
A note about the Rails and Vue process
A quick note on using jshell with the official Docker image of the JDK
Getting a little stuck on the last day of the month using Java's Calendar class
What is the difference between a class and a struct? ?? ??
A note for Initializing Fields in the Java tutorial
[Ruby / Rails] Set a unique (unique) value in the class
A review note of the Spring Framework Resource interface
About the StringBuilder class
[Swift 5] Select a date with the IDate Picker on iOS14
I tried JAX-RS and made a note of the procedure
Try launching a webAP server on the micro using Helidon
[Note] About the Fizz_Buzz problem (How Ruby on Rails works)
Use the high-performance SQL development tool "A5: SQL Mk-2" on Ubuntu
I want to give a class name to the select attribute