Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/LWJGLX/lwjgl3-awt
Browse files Browse the repository at this point in the history
  • Loading branch information
SWinxy committed Jan 17, 2022
2 parents b92e55c + 5d82867 commit 8a223f6
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 13 deletions.
27 changes: 27 additions & 0 deletions src/org/lwjgl/opengl/awt/AWTGLCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import org.lwjgl.system.Platform;

import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.concurrent.Callable;

/**
Expand Down Expand Up @@ -33,6 +36,16 @@ private static PlatformGLCanvas createPlatformCanvas() {
protected final GLData data;
protected final GLData effective = new GLData();
protected boolean initCalled;
private int framebufferWidth, framebufferHeight;
private final ComponentListener listener = new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
java.awt.geom.AffineTransform t = AWTGLCanvas.this.getGraphicsConfiguration().getDefaultTransform();
float sx = (float) t.getScaleX(), sy = (float) t.getScaleY();
AWTGLCanvas.this.framebufferWidth = (int) (getWidth() * sx);
AWTGLCanvas.this.framebufferHeight = (int) (getHeight() * sy);
}
};

@Override
public void removeNotify() {
Expand All @@ -43,11 +56,17 @@ public void removeNotify() {
disposeCanvas();
}

@Override
public synchronized void addComponentListener(ComponentListener l) {
super.addComponentListener(l);
}

public void disposeCanvas() {
this.platformCanvas.dispose();
}
protected AWTGLCanvas(GLData data) {
this.data = data;
this.addComponentListener(listener);
}

protected AWTGLCanvas() {
Expand Down Expand Up @@ -120,6 +139,14 @@ public void render() {
*/
public abstract void paintGL();

public int getFramebufferWidth() {
return framebufferWidth;
}

public int getFramebufferHeight() {
return framebufferHeight;
}

public final void swapBuffers() {
platformCanvas.swapBuffers();
}
Expand Down
7 changes: 2 additions & 5 deletions test/org/lwjgl/opengl/awt/AWTTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public static void main(String[] args) {
frame.setLayout(new BorderLayout());
frame.setPreferredSize(new Dimension(600, 600));
GLData data = new GLData();
data.samples = 4;
data.swapInterval = 0;
AWTGLCanvas canvas;
frame.add(canvas = new AWTGLCanvas(data) {
private static final long serialVersionUID = 1L;
Expand All @@ -27,8 +25,8 @@ public void initGL() {
glClearColor(0.3f, 0.4f, 0.5f, 1);
}
public void paintGL() {
int w = getWidth();
int h = getHeight();
int w = getFramebufferWidth();
int h = getFramebufferHeight();
float aspect = (float) w / h;
double now = System.currentTimeMillis() * 0.001;
float width = (float) Math.abs(Math.sin(now * 0.3));
Expand All @@ -47,7 +45,6 @@ public void paintGL() {
frame.pack();
frame.setVisible(true);
frame.transferFocus();

Runnable renderLoop = new Runnable() {
public void run() {
if (!canvas.isValid())
Expand Down
4 changes: 2 additions & 2 deletions test/org/lwjgl/opengl/awt/AWTThreadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public void initGL() {
glClearColor(0.3f, 0.4f, 0.5f, 1);
}
public void paintGL() {
int w = getWidth();
int h = getHeight();
int w = getFramebufferWidth();
int h = getFramebufferHeight();
float aspect = (float) w / h;
double now = System.currentTimeMillis() * 0.001;
float width = (float) Math.abs(Math.sin(now * 0.3));
Expand Down
4 changes: 2 additions & 2 deletions test/org/lwjgl/opengl/awt/CompareScreenshotTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ public void initGL() {
}

public void paintGL() {
int w = getWidth();
int h = getHeight();
int w = getFramebufferWidth();
int h = getFramebufferHeight();
float aspect = (float) w / h;
double now = 100;
float width = (float) Math.abs(Math.sin(now * 0.3));
Expand Down
4 changes: 2 additions & 2 deletions test/org/lwjgl/opengl/awt/Core32Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public void initGL() {
aspectUniform = glGetUniformLocation(prog, "aspect");
}
public void paintGL() {
int w = getWidth();
int h = getHeight();
int w = getFramebufferWidth();
int h = getFramebufferHeight();
float aspect = (float) w / h;
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, w, h);
Expand Down
4 changes: 2 additions & 2 deletions test/org/lwjgl/opengl/awt/DrawOnDemandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public void initGL() {

@Override
public void paintGL() {
int w = getWidth();
int h = getHeight();
int w = getFramebufferWidth();
int h = getFramebufferHeight();
if (w == 0 || h == 0) {
return;
}
Expand Down

0 comments on commit 8a223f6

Please sign in to comment.