libgdx Transparent Overlay - java

I'm working on writing a JWindow type UI with OpenGL using libgdx. For some reason my app still has a black background even after enabling GL20.GL_BLEND.
Here is my code:
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FitViewport;
public class MyGdxGame extends ApplicationAdapter {
Stage stage;
SpriteBatch batch;
ShapeRenderer shapeRenderer;
#Override
public void create() {
stage = new Stage(new FitViewport(2560, 1440));
batch = new SpriteBatch();
shapeRenderer = new ShapeRenderer();
shapeRenderer.setAutoShapeType(true);
}
#Override
public void render() {
Gdx.gl.glEnable(GL20.GL_BLEND);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glClearColor(0, 0, 0, .25f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
shapeRenderer.setColor(Color.RED);
shapeRenderer.rect(100, 25, 200, 450);
shapeRenderer.set(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(Color.GREEN);
shapeRenderer.rect(303, 24, 10, 451);
shapeRenderer.end();
batch.end();
Gdx.gl.glDisable(GL20.GL_BLEND);
}
#Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
#Override
public void dispose() {
stage.dispose();
batch.dispose();
shapeRenderer.dispose();
}
}
I've found a thread of someone asking the same thing but in C (How to make an OpenGL rendering context with transparent background?). I even tried adding a LwglAWTCanvas to a JWindow but I'm still unable to make the background transparent.
I'd like to have a clear background so I can draw rects/shapes over the users screen.
Thanks!

I was able to make this work on Windows using JNA.
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.graphics.Color;
import com.mygdx.game.CharlatanoOverlay;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;
import static com.sun.jna.platform.win32.WinUser.*;
public class DesktopLauncher {
public static final WinDef.DWORD DWM_BB_ENABLE = new WinDef.DWORD(0x00000001);
public static final WinDef.DWORD DWM_BB_BLURREGION = new WinDef.DWORD(0x00000002);
public static final WinDef.DWORD DWM_BB_TRANSITIONONMAXIMIZED = new WinDef.DWORD(0x00000004);
public static final WinDef.HWND HWND_TOPPOS = new WinDef.HWND(new Pointer(-1));
private static final int SWP_NOSIZE = 0x0001;
private static final int SWP_NOMOVE = 0x0002;
private static final int WS_EX_TOOLWINDOW = 0x00000080;
private static final int WS_EX_APPWINDOW = 0x00040000;
public static void main(String[] arg) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
System.setProperty("org.lwjgl.opengl.Window.undecorated", "true");
cfg.width = LwjglApplicationConfiguration.getDesktopDisplayMode().width-1;
cfg.height = LwjglApplicationConfiguration.getDesktopDisplayMode().height-1;
cfg.resizable = false;
cfg.fullscreen = false;
cfg.initialBackgroundColor = new Color(0, 0, 0, 0);
new LwjglApplication(new CharlatanoOverlay(), cfg);
WinDef.HWND hwnd;
while ((hwnd = User32.INSTANCE.FindWindow(null, "CharlatanoOverlay")) == null) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(hwnd);
System.out.println(transparentWindow(hwnd));
}
public static boolean transparentWindow(WinDef.HWND hwnd) {
DWM_BLURBEHIND bb = new DWM_BLURBEHIND();
bb.dwFlags = DWM_BB_ENABLE;
bb.fEnable = true;
bb.hRgnBlur = null;
DWM.DwmEnableBlurBehindWindow(hwnd, bb);
int wl = User32.INSTANCE.GetWindowLong(hwnd, WinUser.GWL_EXSTYLE);
wl = wl | WinUser.WS_EX_LAYERED | WinUser.WS_EX_TRANSPARENT;
User32.INSTANCE.SetWindowLong(hwnd, WinUser.GWL_EXSTYLE, wl);
wl &= ~(WS_VISIBLE);
wl |= WS_EX_TOOLWINDOW; // flags don't work - windows remains in taskbar
wl &= ~(WS_EX_APPWINDOW);
User32.INSTANCE.ShowWindow(hwnd, SW_HIDE); // hide the window
User32.INSTANCE.SetWindowLong(hwnd, GWL_STYLE, wl); // set the style
User32.INSTANCE.ShowWindow(hwnd, SW_SHOW); // show the window for the new style to come into effect
User32.INSTANCE.SetWindowLong(hwnd, WinUser.GWL_EXSTYLE, wl);
return User32.INSTANCE.SetWindowPos(hwnd, HWND_TOPPOS, 0, 0, 2560, 1440, SWP_NOMOVE | SWP_NOSIZE);
}
}
DWM:
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinNT;
public class DWM {
static {
Native.register("Dwmapi");
}
public static native WinNT.HRESULT DwmEnableBlurBehindWindow(WinDef.HWND hWnd, DWM_BLURBEHIND pBlurBehind);
}
DWM_BLURBEHIND:
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.WinDef;
import java.util.Arrays;
import java.util.List;
public class DWM_BLURBEHIND extends Structure {
public WinDef.DWORD dwFlags;
public boolean fEnable;
public WinDef.HRGN hRgnBlur;
public boolean fTransitionOnMaximized;
#Override
protected List<String> getFieldOrder() {
return Arrays.asList("dwFlags", "fEnable", "hRgnBlur", "fTransitionOnMaximized");
}
}

Related

Libgdx button onclick not working

I have created a button and I want to change its appearance on hover and on click. I get no errors, but it isn't working. It doesn't change the image when it is clicked or when it is hovered. The only image that is displayed is the one from playButtonStyle.up.
Here is my code:
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.starships.MainClass;
import com.sun.prism.paint.Color;
import helpers.Info;
public class MainMenuScreen implements Screen {
MainClass game;
Stage stage;
private Texture background;
private AssetManager assets;
private TextureAtlas atlas;
private Skin skin;
private Table table;
private Button playButton;
public MainMenuScreen(MainClass mainClass) {
game = mainClass;
Gdx.input.setInputProcessor(stage);
stage = new Stage();
background = new Texture(Gdx.files.internal("Background.png"));
assets = new AssetManager();
assets.load("Buttons/PlayButtonAtlas.atlas", TextureAtlas.class);
assets.finishLoading();
atlas = assets.get("Buttons/PlayButtonAtlas.atlas");
skin = new Skin(atlas);
table = new Table(skin);
table.setBounds(0, 0, Info.WIDTH, Info.HEIGHT);
Button.ButtonStyle playButtonStyle = new Button.ButtonStyle();
playButtonStyle.up = skin.getDrawable("PlayButton");
playButtonStyle.over = skin.getDrawable("PlayButtonHover");
playButtonStyle.down = skin.getDrawable("PlayButtonPressed");
playButton = new Button(playButtonStyle);
table.add(playButton);
stage.addActor(table);
}
#Override
public void show() {
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0.7f, 0.8f, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
stage.dispose();
}
}
Set InputProcessor after initialisation of Stage like this :
public MainMenuScreen(MainClass mainClass) {
game = mainClass;
stage = new Stage();
Gdx.input.setInputProcessor(stage); // This call should be after initialisation of stage.
background = new Texture(Gdx.files.internal("Background.png"));
...
...
}

Java libGdx: FreeTypeFontGenerator is not working (Black screen)

I added in a custom font Aller_light.ttf and when I try to run the program with the font drawn with no errors the screen is black, so I'm not sure if its rendering somewhere else off screen or its not rendering at all.
If you could give your 2 cents that would be of great value to me.
EDIT
Now it's saying i have a nullPointerExeption
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.mygdx.finis.states.MenuState.draw(MenuState.java:43)
at com.mygdx.finis.states.GameStateManager.draw(GameStateManager.java:31)
at com.mygdx.finis.Main.render(Main.java:36)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
This is what should be happening:
MenuState Class
package com.mygdx.finis.states;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.mygdx.finis.Assets;
import com.mygdx.finis.Main;
public class MenuState extends GameState{
private SpriteBatch sb;
private BitmapFont font;
private int currentItem;
private String[] menuItems;
protected MenuState(GameStateManager gsm) {
super(gsm);
}
public void init() {
sb = new SpriteBatch();
FreeTypeFontGenerator gen = new FreeTypeFontGenerator(
Gdx.files.internal(null));
font = gen.generateFont(20);
menuItems = new String[]{
"Play"
};
}
public void update(float dt) {
handleInput();
}
public void draw() {
sb.setProjectionMatrix(Main.cam.combined);//*** This is where the null pointer starts *********
sb.begin();
font.draw(sb, "Quack", Main.WIDTH / 2, Main.HEIGHT / 2);//*** This does not work *******************
sb.end();
}
public void handleInput() {
}
public void dispose() {
}
}
Main Class
package com.mygdx.finis;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.mygdx.finis.states.GameStateManager;
public class Main implements ApplicationListener{
public static int WIDTH;
public static int HEIGHT;
public static OrthographicCamera cam;
private GameStateManager gsm;
public void create(){
WIDTH = Gdx.graphics.getWidth();
HEIGHT = Gdx.graphics.getHeight();
cam = new OrthographicCamera(WIDTH, HEIGHT);
cam.translate(WIDTH / 2, HEIGHT / 2);
cam.update();
gsm = new GameStateManager();
}
public void render(){
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
gsm.update(Gdx.graphics.getDeltaTime());
gsm.draw();//****** THIS IS WHERE THE ISSUE ENDS ******
}
public void resize(int width, int height){}
public void pause(){}
public void resume(){}
public void dispose(){}
}
It seems most likely that your SpriteBatch sb is null. Are you absolutely sure init() is being called?
The only other alternative is that Main.cam is null.
It has to be one of those.
Incidentally, this looks odd to me...
Gdx.files.internal(null));
Why are you doing that?

Libgdx Desktop Launcher blank

Just starting Libgdx and java game development. Got a little bit of code to try and write a sprite sheet moving animation person, just started it so not finished by when i try an run it the single sprite drawing is not running, nothing is the desktop appliction is just black.
Desktop Launcher:
package com.mkgame.game1.desktop;
import com.MKgames.OptionScreen;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.mkgame.game1.Game1;
public class DesktopLauncher extends Game1{
public static void main (String[] arg) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "Game1";
cfg.width = 960;
cfg.height = 540;
new LwjglApplication(new Game1 (), cfg);
}
}
Main java class 'Game1':
package com.mkgame.game1;
import com.MKgames.FarmerAsset;
import com.MKgames.OptionScreen;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Game1 extends ApplicationAdapter {
public OptionScreen game_screen;
public void create() {
FarmerAsset.load();
}
}
Game screen:
package com.MKgames;
import sun.java2d.loops.DrawGlyphListAA.General;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class OptionScreen implements Screen{
GameMain game;
OrthographicCamera camera;
SpriteBatch batch;
int farmerX;
public OptionScreen(GameMain game){
this.game = game;
camera = new OrthographicCamera();
camera.setToOrtho(true, 1920, 1080);
batch = new SpriteBatch();
farmerX = 960-85;
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0.95F, 0.95F, 0.95F, 0.95F);
camera.update();
generalUpdate();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(FarmerAsset.farmer1, farmerX, 200);
batch.end();
}
public void generalUpdate() {
if(Gdx.input.isKeyPressed(Keys.A)||Gdx.input.isKeyPressed(Keys.LEFT)){
farmerX -= 5;
}
else if(Gdx.input.isKeyPressed(Keys.D)||Gdx.input.isKeyPressed(Keys.RIGHT)){
farmerX += 5;
}
}
#Override
public void show() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
#Override
public void resize(int width, int height) {
}
#Override
public void hide() {
}
}
Change
public class Game1 extends ApplicationAdapter {
to
public class Game1 extends Game {
Create an instance of OptionScreen in Game:
public OptionScreen game_screen;
public void create() {
FarmerAsset.load();
game_screen = new OptionScreen(this);
setScreen(game_screen);
}
And it should be good to go.

Stage don't show up When call

I'm trying to do a menu for the option of the game i'm creating.
For this screen I use a tablelayout but I can't get it work.
All I get is a black screen.
See yourself :
package com.me.mygdxgame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.CheckBox;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
public class BlueToothOptionScreen implements Screen{
final Stage stage;
Skin skin;
boolean hote = false;
Table table;
public BlueToothOptionScreen() {
this.stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getWidth(), true);
FileHandle skinFile1 = Gdx.files.internal("uiskin.json");
skin = new Skin(skinFile1);
}
#Override
public void render(float delta) {
stage.act(delta);
Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
stage.draw();
Table.drawDebug(stage);
}
#Override
public void resize(int width, int height) {
stage.setViewport(width, height, true);
}
#Override
public void show() {
Gdx.input.setInputProcessor( stage );
table = new Table(skin);
stage.addActor(table);
table.setFillParent(true);
table.defaults().spaceBottom(30);
table.add("Options").colspan(3);
final CheckBox hoteCheckBox = new CheckBox( "", skin );
hoteCheckBox.setChecked(true);
hoteCheckBox.addListener(new ChangeListener() {
#Override
public void changed(
ChangeEvent event,
Actor actor )
{
boolean enabled = hoteCheckBox.isChecked();
hote = enabled;
}
} );
table.row();
table.add("Hote");
table.add(hoteCheckBox);
}
}
I will be glad, if you can help me.

importing an object with extension md2 in libgdx

i'm new in java game programing i want to import an md2 object i used this tuto http://code.google.com/p/libgdx-users/wiki/MD2_Keyframe_Animation
but the probleme is that i cant instanciate an instance from class KeyframedModelViewer this is my code
package com.ELISA.ELISAgame.Screens;
import com.ELISA.ELISAgame.ELISA;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.ModelLoader;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
/*import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;*/
import com.badlogic.gdx.graphics.g3d.loader.ObjLoader;
import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
public class Game implements ApplicationListener, Screen {
ELISA game;
PerspectiveCamera cam;
CameraInputController camController;
ModelBatch modelBatch;
ModelLoader loader;
AssetManager assets;
Model model;
Material material;
ModelInstance instance;
public Game(ELISA game) {
this.game = game;
}
public void create() {
}
#Override
public void render(float delta) {
camController.update();
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
modelBatch.begin(cam);
modelBatch.render(instance);
modelBatch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
//new JoglApplication(new KeyframedModelViewer("data/antigene.md2", "data/antigene.png"), "KeframedModel Viewer", 800, 480, false);
modelBatch = new ModelBatch();
cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
cam.position.set(0f, 6f, 11.5f);
cam.lookAt(0, 0, 0);
cam.near = 0.8f;
cam.far = 300f;
cam.update();
loader = new ObjLoader();
//model = loader.loadModel(Gdx.files.internal("data/labo.obj"));
instance = new ModelInstance(model);
Material material = new Material("material", new TextureAttribute(texture, 0, "s_tex"));
model.setMaterial(material);
camController = new CameraInputController(cam);
Gdx.input.setInputProcessor(camController);
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
modelBatch.dispose();
model.dispose();
}
}
That wiki, "libgdx-users" is highly outdated. MD2 is not supported right now, this is how 3d animations are handled now:
https://github.com/libgdx/libgdx/wiki/3D-animations-and-skinning
"When using fbx-conv https://github.com/libgdx/fbx-conv to convert your model from FBX to G3DB/G3DJ, animations are automatically converted along with it. Just like FBX, G3DB/G3DJ files can contain multiple animations in a single file along with the other model data. Animations applied to nodes which are not present in the source FBX file, will not be converted. So make sure to select all the nodes and animations when exporting to FBX."

Categories

Resources