Slick Forums

Discuss the Slick 2D Library
It is currently Fri May 24, 2013 8:39 pm

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Sun Jun 17, 2012 12:24 am 
Offline

Joined: Sat May 05, 2012 1:18 am
Posts: 20
I'm having an issue where if I try to call drawLine from a class extended from TimerTask, GL will produce an exception.
Here's my drawing code. MainGame.graphics is updated periodically in that class.
Code:
import java.util.TimerTask;

import org.newdawn.slick.Graphics;

public class DrawBox extends TimerTask{
   
   Graphics g = MainGame.graphics;
   float start = 0;
   float startY = 0;
   float end = 1;
   float endY = 1;
   long time = 0;
   
   
   public void run() {
      System.out.println(g);
      g = MainGame.graphics;
      g.drawLine(start, startY, end-((time/50)*(time/50)), startY);//top line
      g.drawLine(start, endY, end-((time/50) * (time/50)), endY);//bottom line
      g.drawLine(start, startY, start, endY-((time/50)*(time/50)));//left
      g.drawLine(end-((time/50)*(time/50)), startY, end, endY-((time/50)*(time/50)));//right
      
      
      if(time == 1000){
         this.cancel();
         return;
      }
      time += 50;
   }
   
}


Error:
Code:

Exception in thread "Timer-1" java.lang.NullPointerException
   at org.lwjgl.opengl.GL11.glDisable(GL11.java:1009)
   at org.newdawn.slick.opengl.renderer.ImmediateModeOGLRenderer.glDisable(ImmediateModeOGLRenderer.java:147)
   at org.newdawn.slick.opengl.TextureImpl.bindNone(TextureImpl.java:120)
   at org.newdawn.slick.Graphics.fillRect(Graphics.java:888)
   at org.newdawn.slick.Graphics.drawLine(Graphics.java:473)
   at DrawBox.run(DrawBox.java:18)
   at java.util.TimerThread.mainLoop(Unknown Source)
   at java.util.TimerThread.run(Unknown Source)


Top
 Profile  
 
PostPosted: Sun Jun 17, 2012 1:07 am 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1469
This isn't a bug. You get this when you try to access OpenGL from another context (i.e. another thread), or before it has been initialized. You should be rendering from the same "GL thread", i.e. only within Slick's render() method.

You shouldn't be using TimerTask anyways for timing purposes. Instead, use Slick's delta value passed to the update function. Simple example:

Code:
public void update(int delta) {
    time += delta;
    if (time > 1000) { //every 1 second
        time = 0;
        doAction();
    }
}


Top
 Profile  
 
PostPosted: Sun Jun 17, 2012 2:41 pm 
Offline

Joined: Sat May 05, 2012 1:18 am
Posts: 20
Thanks. Only problem is:

I want to make a method that'll draw a box from small to large. I also want it to be able to handle multiple drawings at a time, but as you said, I can't draw from another thread. Just an example, but if you see Super Paper Mario's flipping action, you'll see a box is drawn over the character with a cursor, kind of like when you drag your mouse across the desktop.

EDIT: all this for a fancy pause menu :P
Code:
public void drawBox(org.newdawn.slick.Color colour, int start, int startY, int end, int endY){
//stuff
}


Top
 Profile  
 
PostPosted: Sun Jun 17, 2012 3:35 pm 
Offline

Joined: Sat May 05, 2012 1:18 am
Posts: 20
I was looking at another thread and there was one where someone had a Player class and it had a render method, exactly like in BasicGame. The BasicGame's render method called the player's render method with the same parameters as BasicGame's.

Code:
public class B extends BasicGame{
//whatever
public void render(GameContainer gc, Graphics g) throws SlickException{
new Player().render(gc, g);
}
}
public class Player{
public void render(GameContainer gc, Graphics g) throws SlickException{
//whatever
}
}


Thought this may be useful.


Top
 Profile  
 
PostPosted: Sun Jun 17, 2012 3:58 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1469
Why would you create a new instance of Player every time you render it? :?

I'm not sure what you are trying to do. You don't need two threads to draw an animation.


Top
 Profile  
 
PostPosted: Mon Jun 18, 2012 12:16 am 
Offline

Joined: Sat May 05, 2012 1:18 am
Posts: 20
davedes wrote:
Why would you create a new instance of Player every time you render it? :?

I'm not sure what you are trying to do. You don't need two threads to draw an animation.


That was an example....

Basically, I want to draw a box from small to full size, gradually.


Top
 Profile  
 
PostPosted: Mon Jun 18, 2012 1:53 am 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1469
The idea of a game loop is that the rendering and update code is being called every frame -- i.e. 60+ times per second. To move an object from point A to point B, or to resize an object, or fade in/out an object, you simply need to change the value over a period of time, and draw it using the new values. Simple example:

Code:
public float size = 10;

public void render(GameContainer c, Graphics g) throws SlickException {
    g.drawRect(50, 50, size, size);
    size += 0.5f;
}


A better example would take into account timing using the delta value; ideally using easing for a stylish effect.
http://www.java-gaming.org/topics/makin ... #msg234810


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group