Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I need to know how to get drawn image length and width.

I have this code so far where I draw Radar in left top corner with hard-coded coordinates:

int _SRC = R.drawable._item; 

...

float vertices[] = {
            0f,  0f,  0.0f,
            0f,  2.0f,0.0f,
            2.0f,0f,  0.0f,         
            2.0f,2.0f,0.0f
    };
...

addRadarToView(0,240); 

...

public void addRadarToView(float x, float y) {      
    this.x = x;
    this.y = y;     
}


 ...

private void updateScreenData(float screenWidth, float screenHeight){
    // takes screen Height and Width
    this.mScreenWidth = (screenWidth > 0) ? screenWidth : 1f;
    this.mScreenHeight = screenHeight;
    wRatio = 10f/mScreenWidth;
    hRatio = mScreenHeight/10f;     
}


 @Override
public void loadTextures(GL10 gl) {
    Log.e(LOG_TAG, "loadTextures");  

    InputStream is;

    is = mContext.getResources().openRawResource(_SRC);

    Bitmap bitmap = BitmapFactory.decodeStream(is);

    try {
        is.close();
        is = null;
    } catch (IOException e) {
    }

    gl.glGenTextures(TEXTURE_COUNT, textures, 0);

    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

    bitmap.recycle(); 


}

 protected void drawRadar(GL10 gl){
    gl.glPushMatrix();
    try {           
        float transx =  (wRatio * (x)); 
        float transy =  (mScreenHeight*wRatio) - (wRatio * (y)) - 1/hRatio; 
        // Log.e(LOG_TAG, "transx: " + transx + "; transy: " + transy + "; sprite.x: "+  sprite.x + "; sprite.y: " + sprite.y);  

        gl.glTranslatef(transx, transy, 0.0f);

        //###########  draw ##############

        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, floatBufferArray[mFrame]);

        //update();

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        //###############################

    } catch (NullPointerException e) {
    }
    gl.glPopMatrix();   
}

I used addRadarToView(0,240); to set x=0 and y= 240;

So as I see x represents left border of vertices. When 240 is y that represents bottom border. Strange.

[EDIT]

In canvas i used something like:

 int srcX = currentFrame * width;

Rect  src = new Rect(srcX, 0, srcX+width, height);
Rect  dst = new Rect(x, y, x+width, y+height);
canvas.drawBitmap(bitmap, src, dst, null);

Where width and height are:

bitmap.getWidth();
bitmap.getHeight();

Please, help me to sort it out.

Thanks,

share|improve this question
    
Did you call updateScreenData() with the correct resolution? –  Mario Jul 25 '13 at 9:54
    
yep, from Parent class, its working code, just want to replace hard-coded part: addRadarToView(0,240); –  Maxim Shoustin Jul 25 '13 at 10:53
    
Not really sure what you're asking then? Do you want to calculate the actual coordinates where the radar is drawn to? –  Mario Jul 25 '13 at 10:54
    
when I draw vertice on the screen, I tell where to draw it (x,y). But position should be based on Bitmap size. otherwise if image is big, it can be out of the screen –  Maxim Shoustin Jul 25 '13 at 10:57
    
Still not really sure what you're looking for. Are you unhappy with the origin of the radar screen (i.e. the point you pass to draw it)? –  Mario Jul 25 '13 at 11:06
add comment

1 Answer

up vote 1 down vote accepted

This is one of the great things about openGL over canvas. OpenGL and android will handle the scaling of the image for you. Depending on how you build your renderer class will determine exactly how you specify your vertices location, but generally if you follow a how to guide you will always be working in a plane from -1 to +1 for 2D games. This means that if you declare a bitmap within this plane you can simply use translatef() to move the image where it needs to be.

After looking at your code I believe the problems you are having is because you are attempting to use pixels to determine an amount to translate your image by. It is important to use raw measurements from a 1.0 dpi device to get your base pixel location, otherwise it will not scale properly.

This is how I did it here and here

To summarize I believe your confusion is coming from the fact that openGL does not use pixels to determine location but instead uses a vertices that are translated during run.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.