2010-01-31 44 views
5

在此之後:Best approach for oldschool 2D zelda-like game的Android OpenGL ES的平鋪發動機,平滑滾動

我有一個簡單的2D區塊發電機工作,即時通訊根據讀取int地圖[100] [100]填充以1或0和繪製瓦片他們的瓷磚ID,0是水,1草。

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

在我的平局循環,我只是畫足夠的瓷磚,以適應:

即時通訊使用一些基本的數字鍵盤控制處理器,採用了camIncr(32.0f),我根據運動設置的攝像機位置我的屏幕,並使用cameraOffsetX和cameraOffsetY(它的相機位置/瓷磚尺寸)跟蹤左上角的瓷磚

使用GLU.gluOrtho2D進行投影。

這裏是我的自定義渲染內戰平循環:

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); 

我tiledBackground繪製函數:

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(); 

} 

的waterTile和grassTile .draw功能繪製一個32x32的紋理瓷磚,可能會發布的代碼如果相關。

一切都很好,我可以使用數字鍵盤箭頭移動,並且我的地圖'隨着我移動',因爲我只繪製我能看到的東西,它的速度很快(請參閱android OpenGL ES simple Tile generator performance problem,其中Aleks指示我一個簡單的'剔除'想法)

我想我的引擎「平滑滾動」了。我試過調整camIncr變量,GLU.gluOrtho2D等,沒有任何工作。

任何想法? :)

+0

我可能只是使用32x32像素瓷磚添加即時通訊。 – Dullahx 2010-01-31 17:30:52

+0

我發現這一點:http://gpwiki.org/index.php/DirectX:DirectDraw:Tutorials:VB:DX7:Smooth_Scrolling_Tiles卻無法實現我的代碼邏輯,任何幫助將是巨大的。 – Dullahx 2010-01-31 21:36:33

回答

2

我終於找到了。

我右鍵進入循環前添加一個方法的glTranslatef:

gl.glPushMatrix(); 

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

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

首先,我是失敗嘗試翻譯使用蠻力cameraPosX/TILE_HEIGHT師現場,沒有工作。

我們有翻譯的偏移由瓷磚超出屏幕,而不是總cameraPosX 偏移,所以我們使用國防部(%)運營商,而不是分裂。

對不起,我的英語不好^^