因此,我正在尝试学习使用 LWJGL,并创建了这个基本应用程序,它将应用程序置于全屏状态,然后绘制一些填满屏幕的图形。继承人的代码:
import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
public class TextureExample {
/** The texture that will hold the image details */
private Texture texture, texture2;
private Texture charcter;
int positionX = 705, positionY = 790;
int screenWidth, screenHeight;
int onScreenCount = 0;
String runningMode = "START_SCREEN";
String playerFacing = "RIGHT";
int jumpHeight = 0, playerHasJumped = 0;
/**
* Start the example
*/
public void start() {
initGL();
init("res/openingLogo.png");
while (true) {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
render();
if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
positionX -= 1;
playerFacingSwap("LEFT");
if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
positionX -= 2;
}
}
if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
positionX += 1;
playerFacingSwap("RIGHT");
if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
positionX += 2;
}
}
if (Keyboard.isKeyDown(Keyboard.KEY_W)
|| Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
if (jumpHeight < 100) {
jumpHeight += 5;
}
}
if (playerHasJumped <= jumpHeight) {
positionY -= 1;
playerHasJumped += 1;
} else if (playerHasJumped > jumpHeight) {
jumpHeight = 0;
positionY += 1;
playerHasJumped -= 1;
}
onScreenCount += 1;
if (onScreenCount == 240) {
init("res/openingScreen.png");
} else if (onScreenCount == 480) {
runningMode = "MAIN_MENU";
init("res/background.png");
}
Display.update();
Display.sync(60);
if (Display.isCloseRequested()) {
Display.destroy();
System.exit(0);
}
}
}
/**
* Initialise the GL display
*
* @param width
* The width of the display
* @param height
* The height of the display
*/
private void initGL() {
try {
Display.setDisplayMode(new DisplayMode(100, 100));
setDisplayMode(1440, 900, true);
screenWidth = Display.getWidth();
screenHeight = Display.getHeight();
System.out.println("Display Size Is: "
+ Integer.toString(screenWidth) + " x "
+ Integer.toString(screenHeight));
Display.create();
Display.setVSyncEnabled(true);
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// enable alpha blending
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0, 0, screenWidth, screenHeight);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, screenWidth, screenHeight, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
/**
* Initialise resources
*/
public void init(String resource) {
try {
// load texture from PNG file
texture = TextureLoader.getTexture("PNG",
ResourceLoader.getResourceAsStream(resource));
texture2 = TextureLoader.getTexture("PNG",
ResourceLoader.getResourceAsStream("res/foreground.png"));
charcter = TextureLoader.getTexture("PNG",
ResourceLoader.getResourceAsStream("res/tempCharcter.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void playerFacingSwap(String currentMovement) {
if (currentMovement == "RIGHT" && playerFacing == "LEFT") {
try {
charcter = TextureLoader.getTexture("PNG", ResourceLoader
.getResourceAsStream("res/tempCharcter.png"));
} catch (IOException e) {
e.printStackTrace();
}
playerFacing = "RIGHT";
} else if (currentMovement == "LEFT" && playerFacing == "RIGHT") {
try {
charcter = TextureLoader.getTexture("PNG", ResourceLoader
.getResourceAsStream("res/tempCharcterLeft.png"));
} catch (IOException e) {
e.printStackTrace();
}
playerFacing = "LEFT";
}
}
public void render() {
if (runningMode == "START_SCREEN") {
drawImage(texture, 0, 0, 2048, 1050);
} else if (runningMode == "MAIN_MENU") {
drawImage(texture, 0, 0, 2048, 990);
drawImage(texture2, 0, 0, 2048, 990);
drawImage(charcter, positionX, positionY, 60, 120);
}
}
public void drawImage(Texture Image, int x, int y, int width, int height) {
Color.white.bind();
Image.bind(); // or GL11.glBind(texture.getTextureID());
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(x, y);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(x + width, y);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(x + width, y + height);
GL11.glTexCoord2f(0, 1);
GL11.glVertex2f(x, y + height);
GL11.glEnd();
}
/**
* Main Class
*/
public static void main(String[] argv) {
TextureExample textureExample = new TextureExample();
textureExample.start();
}
/**
* Set the display mode to be used
*
* @param width
* The width of the display required
* @param height
* The height of the display required
* @param fullscreen
* True if we want fullscreen mode
*/
public void setDisplayMode(int width, int height, boolean fullscreen) {
// return if requested DisplayMode is already set
if ((Display.getDisplayMode().getWidth() == width)
&& (Display.getDisplayMode().getHeight() == height)
&& (Display.isFullscreen() == fullscreen)) {
return;
}
try {
DisplayMode targetDisplayMode = null;
if (fullscreen) {
DisplayMode[] modes = Display.getAvailableDisplayModes();
int freq = 0;
for (int i = 0; i < modes.length; i++) {
DisplayMode current = modes[i];
if ((current.getWidth() == width)
&& (current.getHeight() == height)) {
if ((targetDisplayMode == null)
|| (current.getFrequency() >= freq)) {
if ((targetDisplayMode == null)
|| (current.getBitsPerPixel() > targetDisplayMode
.getBitsPerPixel())) {
targetDisplayMode = current;
freq = targetDisplayMode.getFrequency();
}
}
// if we've found a match for bpp and frequence against
// the
// original display mode then it's probably best to go
// for this one
// since it's most likely compatible with the monitor
if ((current.getBitsPerPixel() == Display
.getDesktopDisplayMode().getBitsPerPixel())
&& (current.getFrequency() == Display
.getDesktopDisplayMode().getFrequency())) {
targetDisplayMode = current;
break;
}
}
}
} else {
targetDisplayMode = new DisplayMode(width, height);
}
if (targetDisplayMode == null) {
System.out.println("Failed to find value mode: " + width + "x"
+ height + " fs=" + fullscreen);
return;
}
Display.setDisplayMode(targetDisplayMode);
Display.setFullscreen(fullscreen);
} catch (LWJGLException e) {
System.out.println("Unable to setup mode " + width + "x" + height
+ " fullscreen=" + fullscreen + e);
}
}
}
屏幕是在应用程序启动时测量的,它全部设置为全屏,它返回 1440x900 像素的测量值,这是我的显示器尺寸。但是,如果我尝试绘制我的图形(图像也是 1440x900 像素),它不会填满整个屏幕,要让任何图像填满整个屏幕,我必须将它们绘制为 2048x1050,我看不到任何相关性编号到我的显示器,所以我想知道是否有人知道我为什么必须这样做以及是否有修复。
编辑经过大量研究,我发现 Slick 将纹理缩放到最接近 2 的幂是很常见的,但我仍然不明白为什么这意味着我必须将图形绘制到这个大小,当然我应该仍然以屏幕大小绘制它们。