我应该使用 OpenGL 来制作带动画的国际象棋吗?
目前我正在尝试使用 SurfaceView 来制作带有动画的国际象棋游戏。我在模拟器中仅获得大约 8 FPS。我画了一个棋盘和 32 个棋子并旋转所有东西(看看它有多平滑),我正在使用抗锯齿。在 Droid 上我得到了大约 20FPS,所以不是很流畅。是否可以在不使用 OpenGL 的情况下实现动画非常稀缺且简单的游戏?
这就是我每一帧所做的事情:
// scale and rotate
matrix.setScale(scale, scale);
rotation += 3;
matrix.postRotate(rotation, 152, 152);
canvas = surfaceHolder.lockCanvas();
canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.FILTER_BITMAP_FLAG));
canvas.setMatrix(matrix);
canvas.drawARGB(255, 255, 255, 255); // fill the canvas with white
for (int i = 0; i < sprites.size(); i++) {
sprites.get(i).draw(canvas); // draws chessboard and chess pieces
}
At the moment I am experimenting with SurfaceView for my chess game with animations. I am getting only about 8 FPS in the emulator. I draw a chess board and 32 chess pieces and rotate everything (to see how smooth it is), I am using antialiasing. On the Droid I'm getting about 20FPS, so it's not very smooth. Is it possible to implement a game with very scarce and simple animations without having to use OpenGL?
This is what I do every frame:
// scale and rotate
matrix.setScale(scale, scale);
rotation += 3;
matrix.postRotate(rotation, 152, 152);
canvas = surfaceHolder.lockCanvas();
canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.FILTER_BITMAP_FLAG));
canvas.setMatrix(matrix);
canvas.drawARGB(255, 255, 255, 255); // fill the canvas with white
for (int i = 0; i < sprites.size(); i++) {
sprites.get(i).draw(canvas); // draws chessboard and chess pieces
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我决定在创建 SurfaceView 时预缩放所有位图。现在我根本不需要使用变换矩阵。我在模拟器上获得了超过 30 FPS 的速度,在实际设备 (Droid) 上则完全流畅。我还删除了
canvas.drawARGB(255, 255, 255, 255);
,这将 FPS 提高了大约 5。I decided to prescale all the bitmaps when the SurfaceView is created. Now I don't need to use the transformation matrix at all. I'm getting over 30 FPS on emulator and on actual device (Droid) it's completely smooth. I also removed
canvas.drawARGB(255, 255, 255, 255);
which increased the FPS by about 5.我们可以看到更多的代码吗?您的棋子图标有多大?您可以考虑降低它们的质量以减少绘制时间。您还可以限制要加载的图标数量(为每种颜色加载每种类型的一个),然后使用矩阵存储为每个方块加载的图标类型。
Could we see more of the code? What size are your chess piece icons? You might consider reducing their quality in order to reduce draw time. You could also limit the number of icons that you're loading (load one of each type for each color), and then have a matrix store what type of icon to load for each square.