Slick Forums

Discuss the Slick 2D Library
It is currently Sat Feb 23, 2019 12:32 am

All times are UTC




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Effed up "draw"ings..
PostPosted: Thu Jul 28, 2011 4:27 am 
Offline

Joined: Sun Jul 24, 2011 3:52 pm
Posts: 19
Hi there, I have the following code:

Code:
public class Test extends BasicGame {
    Image towerSlot;
   
    @Override
    public void init(GameContainer gc) throws SlickException {
       this.towerSlot = Resources.getImage(1, 1);
       
    }

    @Override
    public void update(GameContainer gc, int i) throws SlickException {
    }

    public void render(GameContainer gc, Graphics grphcs) throws SlickException {
       
        this.towerSlot.draw(100, 100);
        grphcs.setColor(Color.red);
        grphcs.drawRect(200, 100, 100, 100);
        grphcs.setColor(Color.green);
        grphcs.draw(new Rectangle(350,250,100,100));
    }
}


which is pretty basic. However, the code produces the following screenshot: Image[/img]

As you can see, the code produces three different shapes as it's told, but doesn't draw any of them correctly. The TowerSlot image is a png, and the other two are rectangles (the colour is blue for one that was supposed to be red because the screenshot software I used messed up). Below is an image showing what the images should have looked like:
Image

The code does not result in error at all. Here is a log of the code running:



Quote:
I/InputReader( 242): dispatchTouch::touch event's action is 0
I/InputDispatcher( 242): Delivering touch to current input target: action: 0, c
hannel '408f0ca8 com.sec.android.app.twlauncher/com.sec.android.app.twlauncher.L
auncher (server)'
I/InputReader( 242): dispatchTouch::touch event's action is 1
I/InputDispatcher( 242): Delivering touch to current input target: action: 1, c
hannel '408f0ca8 com.sec.android.app.twlauncher/com.sec.android.app.twlauncher.L
auncher (server)'
D/SamsungUtils( 599): acquireDVFSlock arg0 : 0 arg1 : 1000
I/power ( 242): acquire_dvfs_lock level=0
--------- beginning of /dev/log/system
D/PowerManagerService( 242): acquireDVFSlock : level : 0, timeMS : 1000
I/power ( 242): acquire_dvfs_lock level=0, duration=1000, val=1000
I/ActivityManager( 242): Starting: Intent { act=android.intent.action.MAIN cat=
[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.mangelok/.GameActivity
bnds=[492,258][621,368] } from pid 599
I/Launcher( 599): onPause()
I/ActivityManager( 242): Start proc org.mangelok for activity org.mangelok/.Gam
eActivity: pid=4268 uid=10088 gids={1015}
I/Launcher( 599): onWindowFocusChanged(false)
E/jdwp ( 4268): Failed sending reply to debugger: Broken pipe
D/dalvikvm( 4268): Debugger has detached; object registry had 1 entries
D/Launcher( 599): It's image wallpaper. suggestDesiredDimensions(-1,-1)
W/System.err( 4268): GdxNativesLoader: Couldn't unpack and load native 'libgdx.s
o'
D/dalvikvm( 4268): Trying to load lib /data/data/org.mangelok/lib/libgdx.so 0x40
5132b0
D/dalvikvm( 4268): Added shared lib /data/data/org.mangelok/lib/libgdx.so 0x4051
32b0
D/dalvikvm( 4268): No JNI_OnLoad found in /data/data/org.mangelok/lib/libgdx.so
0x405132b0, skipping init
E/SLICK ( 4268): Slick-AE 0
I/GLThread( 4268): noticed surfaceView surface lost tid=10
D/SensorManager( 4268): ====>>>>>Num Sensor: 1
D/SensorManager( 4268): ====>>>>>Num Sensor: 2
D/SensorManager( 4268): ====>>>>>Num Sensor: 3
D/SensorManager( 4268): ====>>>>>Num Sensor: 4
D/SensorManager( 4268): ====>>>>>Num Sensor: 5
D/SensorManager( 4268): ====>>>>>Num Sensor: 6
D/SensorManager( 4268): ====>>>>>Num Sensor: 7
D/SensorManager( 4268): ====>>>>>Num Sensor: 0
E/SensorManager( 4268): registerListener :: handle = 0 name= SMB380 3-axis Acce
lerometer delay= 20000 Listener= com.badlogic.gdx.backends.android.AndroidInput@
40594400
E/SensorManager( 4268): =======>>>Sensor Thread RUNNING <<<========
D/Sensors ( 242): Smb380Sensor::~enable(0, 1)
D/Sensors ( 242): Smb380Sensor::~setDelay(0, 60000000)
D/Sensors ( 242): Smb380Sensor::~setDelay(0, 20000000)
E/SensorManager( 4268): reg :: handle = 0
I/GLThread( 4268): onResume tid=10
I/GLThread( 4268): noticed surfaceView surface acquired tid=10
W/EglHelper( 4268): start() tid=10
D/libEGL ( 4268): loaded /system/lib/egl/libEGL_POWERVR_SGX540_120.so
D/libEGL ( 4268): loaded /system/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
D/libEGL ( 4268): loaded /system/lib/egl/libGLESv2_POWERVR_SGX540_120.so
I/ActivityManager( 242): Displayed org.mangelok/.GameActivity: +177ms
W/EglHelper( 4268): createContext com.google.android.gles_jni.EGLContextImpl@405
9fb78 tid=10
I/GLThread( 4268): noticing that we want render notification tid=10
W/GLThread( 4268): egl createSurface
W/EglHelper( 4268): createSurface() tid=10
W/GLThreadManager( 4268): checkGLESVersion mGLESVersion = 131072 mMultipleGLESCo
ntextsAllowed = true
W/GLThread( 4268): onSurfaceCreated
D/AndroidGraphics( 4268): OGL renderer: PowerVR SGX 540
D/AndroidGraphics( 4268): OGL vendor: Imagination Technologies
D/AndroidGraphics( 4268): OGL version: OpenGL ES-CM 1.1
D/AndroidGraphics( 4268): OGL extensions: GL_OES_byte_coordinates GL_OES_fixed_p
oint GL_OES_single_precision GL_OES_matrix_get GL_OES_read_format GL_OES_compres
sed_paletted_texture GL_OES_point_sprite GL_OES_point_size_array GL_OES_matrix_p
alette GL_OES_draw_texture GL_OES_query_matrix GL_OES_texture_env_crossbar GL_OE
S_texture_mirrored_repeat GL_OES_texture_cube_map GL_OES_blend_subtract GL_OES_b
lend_func_separate GL_OES_blend_equation_separate GL_OES_stencil_wrap GL_OES_ext
ended_matrix_palette GL_OES_framebuffer_object GL_OES_rgb8_rgba8 GL_OES_depth24
GL_OES_stencil8 GL_OES_compressed_ETC1_RGB8_texture GL_OES_mapbuffer GL_OES_EGL_
image GL_EXT_multi_draw_arrays GL_OES_required_internalformat GL_IMG_read_format
GL_IMG_texture_compression_pvrtc GL_IMG_texture_format_BGRA8888 GL_EXT_texture_
format_BGRA8888 GL_IMG_texture_stream GL_OES_egl_sync GL_IMG_vertex_array_object

D/AndroidGraphics( 4268): framebuffer: (5, 6, 5, 0)
D/AndroidGraphics( 4268): depthbuffer: (24)
D/AndroidGraphics( 4268): stencilbuffer: (8)
E/SLICK ( 4268): Starting display 640x480 533x320
E/SLICK ( 4268): Requesting: defaultfont.png
D/dalvikvm( 4268): GC_EXTERNAL_ALLOC freed 114K, 44% free 3314K/5831K, external
2520K/2768K, paused 22ms
E/SLICK ( 4268): Requesting: defaultfont.fnt
E/SLICK ( 4268): Fail: Unsupported: glGenLists
E/SLICK ( 4268): Creating application: org.mangelok.GameActivity@4051e308
E/SLICK ( 4268): Requesting: ui/towerslot.png
W/GLThread( 4268): onSurfaceChanged(533, 320)
I/GLThread( 4268): sending render notification tid=10


I'd really like to be able to draw a rectangle. The TowerSlot image error is weird because I have many other .pngs that load fine, but I'd be happy if any one can fix any of my issues.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2011 9:11 am 
Offline
Slick Zombie

Joined: Wed Apr 02, 2008 1:32 pm
Posts: 1340
Location: Italy
I have this problem too, seems that draw a rectangle results in a triangle.. maybe it is missing a last point ?

_________________
Blog | Last game Drone Swarm


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2011 4:04 pm 
Offline
Oldbie
User avatar

Joined: Thu Jan 13, 2011 4:42 pm
Posts: 349
I must say that the rectangle drawing has me baffled. I suppose I'll look at how slick puts graphics on libgdx. However, the white png problem looks familiar. Libgdx (which slick-AE uses) cannot handle 24-bit pngs, so you might be able to get the image to appear if you can force it to be saved in 32 bits.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2011 10:29 pm 
Offline

Joined: Sun Jul 24, 2011 3:52 pm
Posts: 19
Thanks!! That fixed the png issue. I opened up GIMP, added an alpha channel to the image, and it worked! Thanks so much!

One issue down, one to go. I'll take a look at the renderer or try to find a workaround somehow as I'm not all that good at low-levelish stuff (e.g. OpenGL).

EDIT: For now I'm giving up on this and working on other elements and glitches. I'm assuming that it's some issue with one of the renderers. Is Slick-AE still being worked on?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 01, 2011 12:36 am 
Offline
Oldbie
User avatar

Joined: Thu Jan 13, 2011 4:42 pm
Posts: 349
I don't think Kevglass is working on slick-ae right now, but a few of us have found a few ways of making things work better. What comes to mind is that you can upgrade to libgdx 9 if you edit the right methods in one of the Slick-AE input classes. You can also make tiled maps work with images outside of their own directory by adding something to a tiled map class.

I have tested your code, and though the draw(Rectangle) type call has the same problem, the drawRect method works fine. This might be the version of android I'm running, but i suspect it's the fact that I use libgdx 9.

See http://slick.javaunlimited.net/viewtopic.php?t=3496 for everything I know about android so far (including how to use libgdx 9).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 01, 2011 1:45 am 
Offline
Oldbie
User avatar

Joined: Thu Jan 13, 2011 4:42 pm
Posts: 349
Update: I thin that the problem is somewhere in the QuadBasedLineStripRenderer. If I take a desktop application and call

Renderer.setLineStripRenderer(Renderer.QUAD_BASED_LINE_STRIP_RENDERER);

just before I start() the game, I get the same problem on desktop or something similar. Android appears to use the QuadBasedLineStripRenderer. Also, the drawRect calls draws four lines individually instead of drawing all lines in the same swing (as the ShapeRenderer does)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 02, 2011 5:40 pm 
Offline

Joined: Sun Jul 24, 2011 3:52 pm
Posts: 19
When the width of the lines which make up the rectangle is one (which is default), Slick-AE's renderer will use the GDXRenderer, otherwise, yes, it does use the QuadBasedLineStripRenderer which makes me believe that the problem is in the GDXRenderer.

Specifically, it's probably the Vertex3f function. The ShapeRenderer calls the Vertex2f method, which then calls the Vertex3f function where z is set to zero. somewhere in there there's probably a problem, but I've been unable to find it.

I'll see if I can follow your guide to make libgdx 9 work.

Also, I'm currently using the AppGameContainer to make it run on my desktop as I can't seem to make GDX work. What slick-ae/slick jars are you using to make the GDX container work on your desktop?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 02, 2011 7:12 pm 
Offline

Joined: Sun Jul 24, 2011 3:52 pm
Posts: 19
I upgraded to libgdx 9.01. My game still has the same issue(I am using drawRect), and the test displays the same result as it did before (except the png of my TowerSlot actually works now :P thanks again!)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2011 2:24 pm 
Offline
Oldbie
User avatar

Joined: Thu Jan 13, 2011 4:42 pm
Posts: 349
I think I've found another step in the right direction. After playing around and guestimating a bit, I was able to re-create your problem with the red square by scaling the display. It looks like scaling caused it to cut out some of the pixels to make the screen smaller. making the line wider (with graphics.setLineWidth(float width);) will keep it from completely vanishing. For some reason, setting the line width to 2 also solved the green square glitch.
I'll probably ask on the main forum about the quad based renderer.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2011 4:25 pm 
Offline

Joined: Sun Jul 24, 2011 3:52 pm
Posts: 19
I suspected it was scaling. I wonder if there's certain resolutions that would scale better. I'm using 640x480 because it will actually stretch to fit at that resolution (some resolutions give a sort of widescreen on normal effect)

The line width solution is pretty good but now some of the sides (normally the top and right sides) have a line width of 2 and the rest still look like a line width of one.

Thanks again!


Top
 Profile  
 
PostPosted: Fri Jan 20, 2012 2:58 am 
Offline
Oldbie
User avatar

Joined: Thu Jan 13, 2011 4:42 pm
Posts: 349
Allright! it took some time, but I finally figured out what the problem was. Slick-AE has to use a quad based line renderer to work right, but when the line width was one, it would use the default renderer. I changed it to not use the default for one, and the problem was solved. Ill post a coded solution soon.

The working quad based renderer:
Code:

package org.newdawn.slick.opengl.renderer;

/**
 * A line strip renderer that uses quads to generate lines
 *
 * @author kevin
 *
 * @author Mr. Kenkron:  I have changed this version of the Renderer
 * to not use the DefaultLineStripRenderer at all.  This is done to
 * prevent bugs in slick-AE
 */
public class QuadBasedLineStripRenderer implements LineStripRenderer {
   /** The renderer used to interact with GL */
   private SGL GL = Renderer.get();
   
   /** Maximum number of points allowed in a single strip */
   public static int MAX_POINTS = 10000;
   /** True if antialiasing is currently enabled */
   private boolean antialias;
   /** The width of the lines to draw */
   private float width = 1;
   /** The points to draw */
   private float[] points;
   /** The colours to draw */
   private float[] colours;
   /** The number of points to draw */
   private int pts;
   /** The number of colour points recorded */
   private int cpt;
   
   /** Indicates need to render half colour */
   private boolean renderHalf;
   
   /** True if we shoudl render end caps */
   private boolean lineCaps = false;
   
   /**
    * Create a new strip renderer
    */
   public QuadBasedLineStripRenderer() {
      points = new float[MAX_POINTS * 2];
      colours = new float[MAX_POINTS * 4];
   }
   
   /**
    * Indicate if we should render end caps
    *
    * @param caps True if we should render end caps
    */
   public void setLineCaps(boolean caps) {
      this.lineCaps = caps;
   }
   
   /**
    * @see org.newdawn.slick.opengl.renderer.LineStripRenderer#start()
    */
   public void start() {
      
      pts = 0;
      cpt = 0;
      GL.flush();
      
      float[] col = GL.getCurrentColor();
      color(col[0],col[1],col[2],col[3]);
   }
   
   /**
    * @see org.newdawn.slick.opengl.renderer.LineStripRenderer#end()
    */
   public void end() {
      
      renderLines(points, pts);
   }
   
   /**
    * @see org.newdawn.slick.opengl.renderer.LineStripRenderer#vertex(float, float)
    */
   public void vertex(float x, float y) {
      
      points[(pts*2)] = x;
      points[(pts*2)+1] = y;
      pts++;
      
      int index = pts-1;
      color(colours[(index*4)], colours[(index*4)+1], colours[(index*4)+2], colours[(index*4)+3]);
   }
   
   /**
    * @see org.newdawn.slick.opengl.renderer.LineStripRenderer#setWidth(float)
    */
   public void setWidth(float width) {
      this.width = width;
   }
   
   /**
    * @see org.newdawn.slick.opengl.renderer.LineStripRenderer#setAntiAlias(boolean)
    */
   public void setAntiAlias(boolean antialias) {
      if (antialias) {
         GL.glEnable(SGL.GL_LINE_SMOOTH);
      } else {
         GL.glDisable(SGL.GL_LINE_SMOOTH);
      }
      this.antialias = antialias;
   }

   /**
    * Render the lines applying antialiasing if required
    *
    * @param points The points to be rendered as lines
    * @param count The number of points to render
    */
   public void renderLines(float[] points, int count) {
      if (antialias) {
         GL.glEnable(SGL.GL_POLYGON_SMOOTH);
         renderLinesImpl(points,count,width+1f);
      }

      GL.glDisable(SGL.GL_POLYGON_SMOOTH);
      renderLinesImpl(points,count,width);
      
      if (antialias) {
         GL.glEnable(SGL.GL_POLYGON_SMOOTH);
      }
   }
   
   /**
    * Render the lines given
    *
    * @param points The points building up the lines
    * @param count The number of points to render
    * @param w The width to render at
    */
   public void renderLinesImpl(float[] points, int count, float w) {
      float width = w / 2;
      
      float lastx1 = 0;
      float lasty1 = 0;
      float lastx2 = 0;
      float lasty2 = 0;

      GL.glBegin(SGL.GL_QUADS);
      for (int i=0;i<count+1;i++) {
         int current = i;
         int next = i+1;
         int prev = i-1;
         if (prev < 0) {
            prev += count;
         }
         if (next >= count) {
            next -= count;
         }
         if (current >= count) {
            current -= count;
         }

         float x1 = points[(current*2)];
         float y1 = points[(current*2)+1];
         float x2 = points[(next*2)];
         float y2 = points[(next*2)+1];
         
         // draw the next segment
         float dx = x2 - x1;
         float dy = y2 - y1;

         if ((dx == 0) && (dy == 0)) {
            continue;
         }
         
         float d2 = (dx*dx)+(dy*dy);
         float d = (float) Math.sqrt(d2);
         dx *= width;
         dy *= width;
         dx /= d;
         dy /= d;
         
         float tx = dy;
         float ty = -dx;

         if (i != 0) {
            bindColor(prev);
            GL.glVertex3f(lastx1,lasty1,0);
            GL.glVertex3f(lastx2,lasty2,0);
            bindColor(current);
            GL.glVertex3f(x1+tx,y1+ty,0);
            GL.glVertex3f(x1-tx,y1-ty,0);
         }
         
         lastx1 = x2-tx;
         lasty1 = y2-ty;
         lastx2 = x2+tx;
         lasty2 = y2+ty;
         
         if (i < count-1) {
            bindColor(current);
            GL.glVertex3f(x1+tx,y1+ty,0);
            GL.glVertex3f(x1-tx,y1-ty,0);
            bindColor(next);
            GL.glVertex3f(x2-tx,y2-ty,0);
            GL.glVertex3f(x2+tx,y2+ty,0);
         }
      }
      
      GL.glEnd();
      
      float step = width <= 12.5f ?  5 : 180 / (float)Math.ceil(width / 2.5);
      
      // start cap
      if (lineCaps) {
         float dx = points[2] - points[0];
         float dy = points[3] - points[1];
         float fang = (float) Math.toDegrees(Math.atan2(dy,dx)) + 90;
         
         if ((dx != 0) || (dy != 0)) {
            GL.glBegin(SGL.GL_TRIANGLE_FAN);
            bindColor(0);
            GL.glVertex2f(points[0], points[1]);
            for (int i=0;i<180+step;i+=step) {
               float ang = (float) Math.toRadians(fang+i);
               GL.glVertex2f(points[0]+((float) (Math.cos(ang) * width)),
                          points[1]+((float) (Math.sin(ang) * width)));
            }
            GL.glEnd();
         }
      }
      
      // end cap
      if (lineCaps) {
         float dx = points[(count*2)-2] - points[(count*2)-4];
         float dy = points[(count*2)-1] - points[(count*2)-3];
         float fang = (float) Math.toDegrees(Math.atan2(dy,dx)) - 90;
         
         if ((dx != 0) || (dy != 0)) {
            GL.glBegin(SGL.GL_TRIANGLE_FAN);
            bindColor(count-1);
            GL.glVertex2f(points[(count*2)-2], points[(count*2)-1]);
            for (int i=0;i<180+step;i+=step) {
               float ang = (float) Math.toRadians(fang+i);
               GL.glVertex2f(points[(count*2)-2]+((float) (Math.cos(ang) * width)),
                          points[(count*2)-1]+((float) (Math.sin(ang) * width)));
            }
            GL.glEnd();
         }
      }
   }

   /**
    * Bind the colour at a given index in the array
    *
    * @param index The index of the colour to bind
    */
   private void bindColor(int index) {
      if (index < cpt) {
         if (renderHalf) {
            GL.glColor4f(colours[(index*4)]*0.5f, colours[(index*4)+1]*0.5f,
                       colours[(index*4)+2]*0.5f, colours[(index*4)+3]*0.5f);
         } else {
            GL.glColor4f(colours[(index*4)], colours[(index*4)+1],
                      colours[(index*4)+2], colours[(index*4)+3]);
         }
      }
   }
   
   /**
    * @see org.newdawn.slick.opengl.renderer.LineStripRenderer#color(float, float, float, float)
    */
   public void color(float r, float g, float b, float a) {
      
      colours[(pts*4)] = r;
      colours[(pts*4)+1] = g;
      colours[(pts*4)+2] = b;
      colours[(pts*4)+3] = a;
      cpt++;
   }

   public boolean applyGLLineFixes() {
      return true;
   }
}


_________________
"Artificial intelligence will never be a match for human stupidity" - "Jamos Kennedynos"


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

All times are UTC


Who is online

Users browsing this forum: No registered users 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:  
cron
Powered by phpBB® Forum Software © phpBB Group