下面这个问题:http://stackoverflow.com/questions/2125354/best-approach-for-oldschool-2d-zelda-like-game
感谢为previous答复,并与 HTTP的一大启示:/ /insanitydesign.com/wp/projects/nehe-android-ports/ ,我开始构建一个简单的瓷砖发生器我简单的2D塞尔达般的游戏项目。
我现在可以生成具有相同纹理的瓷砖地图中,使用2(..)叠瓦迭代绘制的水平和垂直瓷砖,并得到了一些基本DPAD键输入听众滚动在x和y轴。
但我现在遇到了我的第一个性能问题,只是一个纹理和一个模型。
当试图建立一个10×10的地图,滚动细腻。
在50×50与尝试,在事态进一步恶化,并配有100×100,它的方式是不可接受的。
就只有这么一个办法告诉OpenGL渲染我地图集的看得见的一部分,而忽略隐藏的瓷砖?我是一个全新的了这一点。
使用im
GLU.gluLookAt(GL,cameraPosX,cameraPosY,10.0f,cameraPosX,cameraPosY,0.0,0.0,1.0F,0.0);
设置相机和观点的2D风格的感觉。
任何帮助吗? :)
为(INT J = 0; J< 10; J ++){ 对(INT I = 0; I&小于10;我++){ gl.glPushMatrix(); // Sauvegarde LA矩阵的计算河畔乐栈 //根据设置纹理滤波器绑定纹理 gl.glBindTexture(GL10.GL_TEXTURE_2D,纹理[过滤器]); //设置旋转脸 gl.glFrontFace(GL10.GL_CW); //启用纹理状态 gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); //启用顶点状态 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); //指向我们的顶点缓冲 gl.glVertexPointer(3,GL10.GL_FLOAT,0,vertexBuffer); //指向我们的纹理的buff gl.glTexCoordPointer(2,GL10.GL_FLOAT,0,textureBuffer); //绘制顶点的三角形带 gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,0,vertices.length / 3); //临行前禁用客户端状态 gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glTranslatef(1.0F,0.0,0.0); //在AVANCE D'UNE瓷砖 } //对VA commencer一个dessiner拉2E LIGNE gl.glPopMatrix(); // Rappelle LA矩阵的计算河畔乐栈 gl.glTranslatef(0.0,-1.0F,0.0); }
解决方案
您可以轻松让你的循环仅绘制可见的咏叹调。下面是一些例子,它需要如何做。我不知道Android的API,以便线程我的例子中为元code。
INT COLS = SCREEN_WIDTH / TILE_SIZE + 1; //多少列可以在屏幕上显示诠释行= SCREEN_HEIGHT / TILE_SIZE + 1; //山楂许多行可以在屏幕上显示INT firstVisibleCol = cameraPosX / TILE_SIZE; //第一列,我们需要绘制INT firstVisibleRow = cameraPosY / TILE_SIZE; //第一行,我们需要绘制//现在循环将成为对于(INT J = firstVisibleRow; J<行; J ++){ 的for(int i = firstVisibleCol; I< COLS;我++){ ... }}
following this question : http://stackoverflow.com/questions/2125354/best-approach-for-oldschool-2d-zelda-like-game
Thank to previous replies, and with a major inspiration from http://insanitydesign.com/wp/projects/nehe-android-ports/ , i started to build a simple Tile Generator for my simple 2D zelda-like game project.
I can now generate a map with the same textured tile, using 2 for(..) imbricated iterations to draw horizontal and vertical tiles, and got some basic DPAD key input listeners to scroll over the x and y axis.
but now im running into my first performance problems, just with one texture and one model.
When trying to build a 10x10 map, scrolling is fine and smooth.
When trying with 50x50, things get worse, and with a 100x100, its way unacceptable.
Is there a way only to tell OpenGL to render the 'visible' part of my mapset and ignore the hidden tiles? im a totally new to this.
im using
GLU.gluLookAt(gl, cameraPosX, cameraPosY, 10.0f,cameraPosX, cameraPosY, 0.0f, 0.0f, 1.0f, 0.0f);
to set the camera and point of view for a 2D-style feeling.
Any help ? :)
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 10; i++) {
gl.glPushMatrix(); // Sauvegarde la matrice sur le stack
//Bind the texture according to the set texture filter
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[filter]);
//Set the face rotation
gl.glFrontFace(GL10.GL_CW);
//Enable texture state
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
//Enable vertex state
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
//Point to our vertex buffer
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
//point to our texture buff
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
//Draw the vertices as triangle strip
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
//Disable the client state before leaving
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTranslatef(1.0f, 0.0f, 0.0f); // on avance d'une tile
}
// on va commencer a dessiner la 2e ligne
gl.glPopMatrix(); // Rappelle la matrice sur le stack
gl.glTranslatef(0.0f, -1.0f, 0.0f);
}
解决方案
You can easily make your loop to draw only the visible aria. Here is some example how it needs to be done. I don't know the android API so thread my example as metacode.
int cols = SCREEN_WIDTH / TILE_SIZE + 1; // how many columns can fit on the screen
int rows = SCREEN_HEIGHT / TILE_SIZE + 1; // haw many rows can fit on the screen
int firstVisibleCol = cameraPosX / TILE_SIZE; // first column we need to draw
int firstVisibleRow = cameraPosY / TILE_SIZE; // first row we need to draw
// and now the loop becomes
for (int j = firstVisibleRow; j < rows; j++) {
for (int i = firstVisibleCol ; i < cols; i++) {
...
}
}