Hi this is my first post. I've been using LWJGL for a while and I'm working on a game that happens to have a full screen button. When it is clicked the game goes into full screen but the textures don't bind it's just a white screen.
Here is the code for the full screen transition code which I suspect is where the problem is:
package lifeLine.game;
import static org.lwjgl.opengl.GL11.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
public class FullscreenManager {
boolean fsState;
GameLoop gameLoop;
public FullscreenManager(GameLoop gl, boolean defaultState) {
gameLoop = gl;
fsState = defaultState;
Display.destroy();
try {
Display.setFullscreen(fsState);
Display.setDisplayMode(new DisplayMode(gameLoop.WIDTH, gameLoop.HEIGHT));
Display.setTitle("Life Line: 1.0");
Display.create();
} catch (LWJGLException e){
e.printStackTrace();
}
initGL();
}
public void toggleFullscreen() {
Display.destroy();
try {
Display.setDisplayMode(new DisplayMode(gameLoop.WIDTH,gameLoop.HEIGHT));
Display.setFullscreen(!fsState);
Display.create();
Display.setVSyncEnabled(true);
fsState = !fsState;
} catch (LWJGLException e){
Logger.getLogger(GameLoop.class.getName()).log(Level.SEVERE, null, e);
}
initGL();
}
public void setFullscreen(boolean state) {
try {
Display.destroy();
if (!state) {
Display.setDisplayMode(new DisplayMode(gameLoop.WIDTH, gameLoop.HEIGHT));
}
fsState = state;
Display.setFullscreen(state);
Display.create();
Display.setVSyncEnabled(true);
} catch (LWJGLException e){
Logger.getLogger(GameLoop.class.getName()).log(Level.SEVERE, null, e);
}
initGL();
}
private void initGL() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, gameLoop.WIDTH, 0, gameLoop.HEIGHT, -1, 1);
glMatrixMode(GL_MODELVIEW);
glClearColor(0, 0, 0, 1);
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
}
This causes a blank white after one of the fullscreen functions.
Please help!
Thanks in advance.
Related
I am trying to display a png as a texture in Eclipse using the LWJGL library. I made sure to bind the texture and to set the coordinates BEFORE drawing the vertexes, but the image still isn't displaying. What could be the problem?
package javagame;
import static org.lwjgl.opengl.GL11.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
#SuppressWarnings("unused")
public class ImageLWJGL {
public static Texture p;
public static void main(String[] args) {
createDisplay();
createGL();
render();
cleanUp();
}
private static void createDisplay(){
try {
Display.setDisplayMode(new DisplayMode(800,600));
Display.create();
Display.setVSyncEnabled(true);
} catch (LWJGLException e) {
e.printStackTrace();
}
}
public static void createGL(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Display.getWidth(), 0, Display.getHeight(), -1, 1);
glMatrixMode(GL_MODELVIEW);
glClearColor(0,0,0,1);
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
}
private static void render(){
while(!Display.isCloseRequested()){
try {
p = TextureLoader.getTexture("PNG",
new FileInputStream(new File("res/wood.png")));
} catch (IOException e) {
e.printStackTrace();
}
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3f(0.25f,0.75f,0.5f);
p.bind();
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(0,0); // (origin, origin)
glTexCoord2f(0,1);
glVertex2f(0,p.getHeight()); // (origin, y-axis)-height
glTexCoord2f(1,1);
glVertex2f(p.getWidth(),p.getHeight()); // (x-axis, y-axis)
glTexCoord2f(1,0);
glVertex2f(p.getWidth(),0); // (x-axis, origin)-width
glEnd();
Display.update();
}
}
private static void cleanUp(){
Display.destroy();
}
}
Your problem is your Ortho Projection Matrix.
glOrtho(0, Display.getWidth(), 0, Display.getHeight(), -1, 1);
It goes from 0 to Display Width and 0 to Display Height, but you render your Quad from 0 to 1. So your Quad will be rendered, but to Small, that you can see it.
To solve the Problem Change the glOrtho to:
glOrtho(0, 1, 0, 1, -1, 1);
I am new to java programming and I am trying to draw a textured square but it only shows up in white.
if i add glEnable(texture2d) it distorts the color. I can get the texture to bind when not in "state format", but like i said, the glEnable(texture2d) distorts the color. Is there something im missing? Thanks!
Here is the main class
package main;
static org.lwjgl.opengl.GL11.*;
import org.newdawn.slick.openal.*;
import org.newdawn.slick.opengl.Texture;
import static helper.Artist.*;
import helper.Artist.*;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;
public class GameWindow {
public static enum states{
MAIN, GAME; }
public states state = states.MAIN;
public void render(){
switch(state){
case MAIN:
glColor3f(1.0f, 0, 0);
glRectf(0, 0, 800, 600);
break;
case GAME:
glColor3f(1,1,1);
DrawQuad(grass,5,5,64,64);
}
}
public void checkInput(){
switch(state){
case MAIN:
if(Keyboard.isKeyDown(Keyboard.KEY_RETURN)){
state = states.GAME;
}
break;
case GAME:
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
state = states.MAIN;
}
}
}
public GameWindow(){
try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.create();
} catch (LWJGLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Display.setTitle("Hello");
//initialization
grass = LoadTexture("dirt64");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 800, 600, 0, 1, -1 );
glMatrixMode(GL_MODELVIEW);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
while(!Display.isCloseRequested()){
//render code
checkInput();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
render();
Display.update();
Display.sync(60);
}
Display.destroy();
}
public static void main(String[] args){
new GameWindow();
}}
The second class
package helper;
import static org.lwjgl.opengl.GL11.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.newdawn.slick.openal.*;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;
public class Artist {
public static Texture grass;
public void load(){
try {
grass = TextureLoader.getTexture("PNG", new FileInputStream("res/dirt64.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Texture LoadTexture(String key){
try{
return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/" + key + ".png")));
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void Draw(float x, float y, float width, float height){
glBegin(GL_QUADS);
glVertex2f(x,y);
glVertex2f(x + width, y);
glVertex2f(x + width, y + height);
glVertex2f(x, y + height);
glEnd();
}
public static void DrawQuad(Texture texture, float x, float y, float width, float height){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
texture.bind();
glBegin(GL_QUADS);
glVertex2f(x, y);
glVertex2f(x + width, y);
glVertex2f(x + width, y + height);
glVertex2f(x, y + height);
glEnd();
}
}
I have been going about on this for the past 3 hours.
I'm trying to make a 2D Strategy game in Java using the lwjg libraries, and for starters, I've got stuck at loading a texture for an object (Farmer).
Its giving me this:
Exception in thread "main" java.lang.NullPointerException
at _2nd_Branch.Farmer.render(Farmer.java:42)
at _1st_Branch.CoreGame.render(CoreGame.java:30)
at _1st_Branch.Game.GameLoop(Game.java:33)
at _1st_Branch.Game.main(Game.java:13)
Here is the code I've been working on (As a way to organize things. Got this from someone on youtube explaining some bits about lwjgl):
Game.java:
package _1st_Branch;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
public class Game {
private static CoreGame coreGame;
public static void main(String [] args)
{
CD();
createGame();
GameLoop();
CleanUp();
}
private static void CD()
{
Window.create(800, 600);
}
private static void createGame()
{
coreGame = new CoreGame();
}
public static void GameLoop()
{
while(!Window.isCloseRequested())
{
Window.Clear();
coreGame.input();
coreGame.logic();
coreGame.render();
Display.update();
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
{
Display.destroy();
System.exit(0);
}
}
}
private static void CleanUp()
{
coreGame.dispose();
Window.Destroy();
}
}
CoreGame.java:
package _1st_Branch;
import _2nd_Branch.Farmer;
import _2nd_Branch.Player;
public class CoreGame {
public final static int TILE_SIZE = 64;
private static Player player;
private static Farmer farmer;
public CoreGame()
{
farmer = new Farmer();
}
public void input()
{
}
public void logic()
{
}
public void render()
{
farmer.render();
}
public void dispose()
{
}
}
Window.java:
package _1st_Branch;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
public class Window {
public static void create(int width, int height)
{
try
{
Display.setDisplayMode(new DisplayMode(width, height));
Display.setTitle("Artyas RTS");
Display.create();
initGL();
initInput();
}
catch (LWJGLException e)
{
e.printStackTrace();
}
}
public static void initGL()
{
glClearColor(0.5f, 0.5f, 1, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Display.getWidth(), 0, Display.getHeight(), -1, 1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLoadIdentity();
}
private static void initInput()
{
try
{
Keyboard.create();
}
catch (LWJGLException e)
{
e.printStackTrace();
}
}
public static void Clear()
{
glClear(GL_COLOR_BUFFER_BIT);
}
public static void Destroy()
{
Keyboard.destroy();
Display.destroy();
System.exit(0);
}
public static void update()
{
Display.update();
Display.sync(60);
}
public static boolean isCloseRequested()
{
return Display.isCloseRequested();
}
}
Farmer.java:
//CIVILIAN UNIT
package _2nd_Branch;
import java.io.IOException;
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 Farmer {
public Farmer() {};
private Texture texture;
public void init() {
try {
// load texture from PNG file
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("Farmer.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void render()
{
Color.white.bind();
texture.bind();
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f(400,500);
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(450,500);
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(450, 550);
GL11.glTexCoord2f(0,1);
GL11.glVertex2f(400, 550);
GL11.glEnd();
}
}
I am literally stuck here. If someone could indicate what needs to be done, I will be eternally grateful! I really need to get going on making this project, to learn more about Java.
The texture.bind() function from the Farmer class gives me the error.
Being stuck here, I don't know what it says about me as a programmer.
You never call Farmer.init() which loads the texture. You only call the constructor in CoreGame. Either you should call init() from inside the Farmer constructor or call init after calling the constructor in CoreGame. I recommend calling init() from CoreGame, and continue to keep texture loading away from the constructor simply because if you initialize and have to call the constructor to Farmer before you have created your display and initialized OpenGL (Window.initGL()) you don't want to load the texture. Loading a texture with slick before the display is created and opengl is initialized can cause a NullpointerException in TextureLoader.
I have a problem loading my image. This is what I get: http://imgur.com/BZaubNz. The black spaces shouldn't be there. I don't know if there's something wrong with my image or my code. I've tried other images and some work just fine with this code, some don't.
import java.io.IOException;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
import org.newdawn.slick.Color;
import org.lwjgl.opengl.GL11;
public void init() {
try {
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("sprites/playButton.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void render() {
texture.bind();
Color.white.bind();
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f(400-getWidth()/2,250);
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(400+getWidth()/2,250);
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(400+getWidth()/2,250+getHeight());
GL11.glTexCoord2f(0,1);
GL11.glVertex2f(400-getWidth()/2,250+getHeight());
GL11.glEnd();
}
This is the main render function.
public void start() {
try {
Display.setDisplayMode(new DisplayMode(800,600));
Display.setInitialBackground(255, 255, 255);
Display.create();
Display.setVSyncEnabled(true);
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
// init OpenGL here
MainMenu mainMenu = new MainMenu();
mainMenu.init();
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glClearColor(255.0f, 255.0f, 255.0f, 0.0f);
GL11.glClearDepth(1);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0,800,600);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 600, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
while (!Display.isCloseRequested()) {
// render OpenGL here
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
mainMenu.render();
Display.update();
Display.sync(100);
}
im trying to create a applet for a little game i created. when i run the game normally (no applet) it works just fine but when im trying to run it as an applet it gives me the following error.
im using lwjgl 2.8.4 opengl
java.lang.RuntimeException: No OpenGL context found in the current thread.
java.lang.RuntimeException: Unable to create display
at com.base.engine.GameApplet.init(GameApplet.java:202)
at sun.applet.AppletPanel.run(AppletPanel.java:434)
at java.lang.Thread.run(Thread.java:722)
here is the code for the GameApplet.java
package com.base.engine;
import com.base.game.Game;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
public class GameApplet extends Applet {
Canvas display_parent;
/** Thread which runs the main game loop */
Thread gameThread;
/** is the game loop running */
private static boolean running = false;
private static Game game;
public void startLWJGL() {
//gameThread = new Thread() {
// #Override
//public void run() {
running = true;
try {
initDisplay();
Display.setParent(display_parent);
System.out.println("hij loopt");
//Display.create();
System.out.println("hij loopt");
initGL();
initGame();
gameLoop();
cleanUp();
} catch (LWJGLException e) {
return;
}
//gameLoop();
//}
//};
//gameThread.start();
}
private static void initGame()
{
game = new Game();
}
private static void getInput()
{
System.out.print("before input");
game.getInput();
System.out.print("after input");
}
private static void update()
{
game.update();
}
private static void render()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
game.render();
Display.update();
Display.sync(60);
}
private static void gameLoop()
{
System.out.print("before while");
while(!Display.isCloseRequested())
{
while(running) {
System.out.print("before running shit");
Display.sync(60);
Display.update();
Display.destroy();
//getInput();
update();
render();
System.out.println("running gameLoop..");
}
}
System.out.print("after while");
}
private static void initGL()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Display.getWidth(), 0, Display.getHeight(), -1, 1);
glMatrixMode(GL_MODELVIEW);
glDisable(GL_DEPTH_TEST);
glClearColor(0,0,0,0);
}
private static void cleanUp()
{
Display.destroy();
Keyboard.destroy();
}
private static void initDisplay()
{
try
{
Display.setDisplayMode(new DisplayMode(800,600));
Display.create();
Keyboard.create();
System.out.println("Keyboard created");
Display.setVSyncEnabled(true);
} catch (LWJGLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static ArrayList<GameObject> sphereCollide(float x, float y, float radius)
{
return game.sphereCollide(x, y, radius);
}
/**
* Tell game loop to stop running, after which the LWJGL Display will
* be destoryed. The main thread will wait for the Display.destroy().
*/
private void stopLWJGL() {
running = false;
}
#Override
public void start() {
//startLWJGL();
}
#Override
public void stop() {
}
/**
* Applet Destroy method will remove the canvas,
* before canvas is destroyed it will notify stopLWJGL()
* to stop the main game loop and to destroy the Display
*/
#Override
public void destroy() {
remove(display_parent);
super.destroy();
}
#Override
public void init() {
setLayout(new BorderLayout());
try {
display_parent = new Canvas() {
#Override
public final void addNotify() {
super.addNotify();
startLWJGL();
}
#Override
public final void removeNotify() {
stopLWJGL();
super.removeNotify();
}
};
display_parent.setSize(800,600);
add(display_parent);
System.out.println("gaat ie lekker");
display_parent.setFocusable(true);
display_parent.requestFocus();
display_parent.setIgnoreRepaint(true);
setVisible(true);
} catch (Exception e) {
System.err.println(e);
throw new RuntimeException("Unable to create display");
}
}
}
can someone tell me what im doing wrong?