Начинающий
- Статус
- Оффлайн
- Регистрация
- 25 Дек 2025
- Сообщения
- 28
- Реакции
- 0
Код:
package Neleryse.nls.util.render.core;
import Neleryse.nls.util.render.gl.GlBackend;
import org.lwjgl.stb.STBImage;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class TextureLoader {
/**
* PNG/JPG → GL текстура (RGBA8, linear filter)
* @param backend твой GlBackend
* @param filePath путь к файлу (assets/icon.png)
* @return GL texture ID или 0 при ошибке
*/
public static int loadTexture(GlBackend backend, String filePath) {
try (var stack = MemoryStack.stackPush()) {
Path fullPath = Paths.get(filePath);
// Быстрая проверка файла
if (!Files.exists(fullPath)) {
System.err.println("File not found: " + filePath);
return 0;
}
byte[] bytes = Files.readAllBytes(fullPath);
if (bytes.length == 0) {
System.err.println("Empty file: " + filePath);
return 0;
}
ByteBuffer data = MemoryUtil.memAlloc(bytes.length);
try {
data.put(bytes).flip();
IntBuffer w = stack.mallocInt(1);
IntBuffer h = stack.mallocInt(1);
IntBuffer ch = stack.mallocInt(1);
ByteBuffer image = STBImage.stbi_load_from_memory(data, w, h, ch, 4);
if (image == null) {
System.err.println("STB Error: " + STBImage.stbi_failure_reason());
return 0;
}
int width = w.get(0);
int height = h.get(0);
if (width <= 0 || height <= 0) {
System.err.println("Invalid dimensions: " + width + "x" + height);
STBImage.stbi_image_free(image);
return 0;
}
System.out.printf("Loaded %s: %dx%d%n", filePath, width, height);
int texId = backend.createMsdfTexture(width, height, image);
STBImage.stbi_image_free(image);
return texId;
} finally {
MemoryUtil.memFree(data);
}
} catch (IOException e) {
System.err.println("IO error: " + filePath + " → " + e.getMessage());
return 0;
}
}
}
Вот код загрузки(гпт) когда я включаю рендеро дико лагает как можно оптимизировать