| 1 | package com.mojang.blaze3d.platform;
|
| 2 |
|
| 3 | import com.google.gson.Gson;
|
| 4 | import com.google.gson.JsonElement;
|
| 5 | import com.google.gson.JsonParser;
|
| 6 | import com.mojang.logging.LogUtils;
|
| 7 | import com.mojang.serialization.Codec;
|
| 8 | import com.mojang.serialization.JsonOps;
|
| 9 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 10 | import java.util.Locale;
|
| 11 | import java.util.Objects;
|
| 12 | import java.util.Optional;
|
| 13 | import net.fabricmc.api.EnvType;
|
| 14 | import net.fabricmc.api.Environment;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 | import org.lwjgl.sdl.SDLPixels;
|
| 17 | import org.lwjgl.sdl.SDL_DisplayMode;
|
| 18 | import org.lwjgl.sdl.SDL_PixelFormatDetails;
|
| 19 | import org.slf4j.Logger;
|
| 20 |
|
| 21 | @Environment(EnvType.CLIENT)
|
| 22 | public final class VideoMode {
|
| 23 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 24 | public static final Codec<VideoMode> CODEC = RecordCodecBuilder.create(
|
| 25 | instance -> instance.group(
|
| 26 | Codec.INT.fieldOf("width").forGetter(VideoMode::getWidth),
|
| 27 | Codec.INT.fieldOf("height").forGetter(VideoMode::getHeight),
|
| 28 | Codec.INT.fieldOf("red_bits").forGetter(VideoMode::getRedBits),
|
| 29 | Codec.INT.fieldOf("green_bits").forGetter(VideoMode::getGreenBits),
|
| 30 | Codec.INT.fieldOf("blue_bits").forGetter(VideoMode::getBlueBits),
|
| 31 | Codec.FLOAT.fieldOf("refresh_rate").forGetter(VideoMode::getRefreshRate)
|
| 32 | )
|
| 33 | .apply(instance, VideoMode::new)
|
| 34 | );
|
| 35 | private static final Gson GSON = new Gson();
|
| 36 | private final int width;
|
| 37 | private final int height;
|
| 38 | private final int redBits;
|
| 39 | private final int greenBits;
|
| 40 | private final int blueBits;
|
| 41 | private final float refreshRate;
|
| 42 |
|
| 43 | public VideoMode(final int width, final int height, final int redBits, final int greenBits, final int blueBits, final int refreshRate) {
|
| 44 | this(width, height, redBits, greenBits, blueBits, (float)refreshRate);
|
| 45 | }
|
| 46 |
|
| 47 | private VideoMode(final int width, final int height, final int redBits, final int greenBits, final int blueBits, final float refreshRate) {
|
| 48 | this.width = width;
|
| 49 | this.height = height;
|
| 50 | this.redBits = redBits;
|
| 51 | this.greenBits = greenBits;
|
| 52 | this.blueBits = blueBits;
|
| 53 | this.refreshRate = refreshRate;
|
| 54 | }
|
| 55 |
|
| 56 | public VideoMode(final SDL_DisplayMode mode) {
|
| 57 | this.width = mode.w();
|
| 58 | this.height = mode.h();
|
| 59 | this.refreshRate = mode.refresh_rate();
|
| 60 | SDL_PixelFormatDetails details = SDLPixels.SDL_GetPixelFormatDetails(mode.format());
|
| 61 | this.redBits = details != null ? details.Rbits() : 8;
|
| 62 | this.greenBits = details != null ? details.Gbits() : 8;
|
| 63 | this.blueBits = details != null ? details.Bbits() : 8;
|
| 64 | }
|
| 65 |
|
| 66 | public static Optional<VideoMode> read(final @Nullable String s) {
|
| 67 | if (s == null) {
|
| 68 | return Optional.empty();
|
| 69 | }
|
| 70 |
|
| 71 | try {
|
| 72 | JsonElement json = JsonParser.parseString(s);
|
| 73 | return CODEC.parse(JsonOps.INSTANCE, json).resultOrPartial(errorx -> LOGGER.warn("Failed to parse video mode '{}': {}", s, errorx));
|
| 74 | } catch (RuntimeException error) {
|
| 75 | LOGGER.warn("Failed to parse video mode '{}'", s, error);
|
| 76 | return Optional.empty();
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | public int getWidth() {
|
| 81 | return this.width;
|
| 82 | }
|
| 83 |
|
| 84 | public int getHeight() {
|
| 85 | return this.height;
|
| 86 | }
|
| 87 |
|
| 88 | public int getRedBits() {
|
| 89 | return this.redBits;
|
| 90 | }
|
| 91 |
|
| 92 | public int getGreenBits() {
|
| 93 | return this.greenBits;
|
| 94 | }
|
| 95 |
|
| 96 | public int getBlueBits() {
|
| 97 | return this.blueBits;
|
| 98 | }
|
| 99 |
|
| 100 | public float getRefreshRate() {
|
| 101 | return this.refreshRate;
|
| 102 | }
|
| 103 |
|
| 104 | public String refreshRateLabel() {
|
| 105 | float rate = this.getRefreshRate();
|
| 106 | return rate == Math.rint(rate) ? Integer.toString((int)rate) : String.format(Locale.ROOT, "%.2f", rate);
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | public boolean equals(final Object o) {
|
| 111 | if (this == o) {
|
| 112 | return true;
|
| 113 | } else if (o != null && this.getClass() == o.getClass()) {
|
| 114 | VideoMode videoMode = (VideoMode)o;
|
| 115 | return this.width == videoMode.width
|
| 116 | && this.height == videoMode.height
|
| 117 | && this.redBits == videoMode.redBits
|
| 118 | && this.greenBits == videoMode.greenBits
|
| 119 | && this.blueBits == videoMode.blueBits
|
| 120 | && Float.compare(this.refreshRate, videoMode.refreshRate) == 0;
|
| 121 | } else {
|
| 122 | return false;
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | public int hashCode() {
|
| 128 | return Objects.hash(this.width, this.height, this.redBits, this.greenBits, this.blueBits, this.refreshRate);
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | public String toString() {
|
| 133 | return String.format(Locale.ROOT, "%sx%s@%s (%sbit)", this.width, this.height, this.refreshRateLabel(), this.redBits + this.greenBits + this.blueBits);
|
| 134 | }
|
| 135 |
|
| 136 | public String write() {
|
| 137 | return CODEC.encodeStart(JsonOps.INSTANCE, this).result().map(GSON::toJson).orElseThrow();
|
| 138 | }
|
| 139 | }
|
| 140 |
|