views:

1689

answers:

2

Hello,

Following this : http://stackoverflow.com/questions/2125354/best-approach-for-oldschool-2d-zelda-like-game

I got a simple 2D tiles generator working, im reading an int map[100][100] filled with either 1's or 0's and draw tiles according to their tile id, 0 is water, 1 grass.

Im using some basic Numpad control handler, using a camIncr (32.0f), i set the camera position according to the movement :

 case KeyEvent.KEYCODE_DPAD_RIGHT:
 cameraPosX = (float)(cameraPosX + camIncr);
 break;

In my draw loop, im just drawing enough tiles to fit on my screen, and track the top left tile using cameraOffsetX and cameraOffsetY (its the camera position / tile size )

Im using a GLU.gluOrtho2D for my projection.

Here is the draw loop inside my custom renderer :

 gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

  gl.glMatrixMode( GL10.GL_PROJECTION );
  gl.glLoadIdentity( );
  GLU.gluOrtho2D(gl, 0, scrWidth, scrHeight, 0);
  repere.draw(gl, 100.0f); // this is just a helper, draw 2 lines at the origin

  //Call the drawing methods
  gl.glMatrixMode(GL10.GL_MODELVIEW);
  gl.glLoadIdentity();

  tiledBackground.draw(gl, filtering);

my tiledBackground draw function :

  int cols = (569 / 32) + 2;      // how many columns can fit on the screen 
  int rows = (320 / 32) + 1;     // haw many rows can fit on the screen 

  int cameraPosX = (int) Open2DRenderer.getCameraPosX();
  int cameraPosY = (int) Open2DRenderer.getCameraPosY();

  tileOffsetX = (int) (cameraPosX / 32);
  tileOffsetY = (int) (cameraPosY / -32);

  gl.glPushMatrix();

  for (int y = 0; y < rows; y++) {
   for (int x = 0; x < cols; x++) {

    try {
     tile = map[y + tileOffsetY][x + tileOffsetX];
    } catch (Exception e) {
     e.printStackTrace(); //when out of array
     tile = 0;
    }

    gl.glPushMatrix();

    if (tile==0){
     waterTile.draw(gl, filter);
     }

    if (tile==4) {
     grassTile.draw(gl, filter);
     }

    gl.glTranslatef(32.0f, 0.0f, 0.0f);

   }//


   gl.glPopMatrix();
   gl.glTranslatef(0.0f, 32.0f, 0.0f);

  }

  gl.glPopMatrix();

 }

the waterTile and grassTile .draw function draw a 32x32 textured tile, might post the code if relevant.

Everything is fine, i can move using numpad arrows, and my map 'moves' with me, since im only drawing what i can see, its fast (see http://stackoverflow.com/questions/2129125/android-opengl-es-simple-tile-generator-performance-problem where Aleks pointed me to a simple 'culling' idea)

I would like my engine to 'smooth scroll' now. I've tried tweaking the camIncr variable, the GLU.gluOrtho2D etc, nothing worked.

Any ideas ? :)

+1  A: 

I finally found out.

i added a glTranslatef method right before entering the loop :

gl.glPushMatrix();

    gl.glTranslatef(-cameraPosX%32, -cameraPosY%32, 0);

    for (int y = 0; y < rows; y++) {
        ...

First, i was unsuccessfully trying to translate the scene using a brute cameraPosX / TILE_HEIGHT division, didn't work.

We have to translate the offset by which the tile extends beyond the screen, not the total cameraPosX offset, so we're using the Mod (%) operator instead of division.

Sorry for my bad english ^^

Dullahx
A: 

Do you have the full source for this example?

Nilrac