一个游戏的基础框架大致分为这几个部分:
1 Window management
2 Input
3 File I/O
4 Graphics
5 Audio
6 Game framework
?
之后书中给出了具体每一部分接口的定义,如下:
Input接口
?
public interface Input { public static class KeyEvent{ public static final int KEY_DOWN=0; public static final int KEY_UP=1; public int type; public int keyCode; public char keyChar; } public static class TouchEvent{ public static final int TOUCH_DOWN=0; public static final int TOUCH_UP=1; public static final int TOUCH_DRAGGED=2; public int type; public int x,y; public int pointer; } public boolean isKeyPressed(int keyCode); public boolean isTouchDown(int pointer); public int getTouchX(int pointer); public int getTouchY(int pointer); public float getAccelX(); public float getAccelY(); public float getAccelZ(); public List<KeyEvent> getKeyEvents(); public List<TouchEvent> getTouchEvents();}
?
?
FileIO接口
?
public interface FileIO { public InputStream readAsset(String fileName) throws IOException; public InputStream readFile(String fileName) throws IOException; public OutputStream writeFile(String fileName) throws IOException;}
?
?
Audio接口 其中还包含了Music和Sound的接口定义
?
public interface Audio { public Music newMusic(String filename); public Sound newSound(String filename);}?
?
public interface Music { public void play(); public void stop(); public void pause(); public void setLooping(boolean looping); public void setVolume(float volume); public boolean isPlaying(); public boolean isStopped(); public boolean isLooping(); public void dispose();}
?
?
?
public interface Sound { public void play(float volume); public void dispose();}
?
?
Graphics接口的定义 包括Pixmap
?
public interface Graphics { public static enum PixmapFormat { ARGB8888, ARGB84444, RGB565 } public Pixmap newPixmap(String fileName, PixmapFormat format); public void clear(int color); public void drawPixel(int x, int y, int color); public void drawLine(int x, int y, int x2, int y2, int color); public void drawRect(int x, int y, int width, int height, int color); public void drawPixmap(Pixmap pixmap, int x, int y, int srcX, int srcY, int srcWidth, int srcHeight); public void drawPixmap(Pixmap pixmap,int x,int y); public int getWidth(); public int getHeight();}
?
public interface Pixmap { public int getWidth(); public PixmapFormat getFormat(); public void dispose();}
?
最后是Game接口的定义 以及抽象类Screen
public interface Game { public Input getInput(); public FileIO getFileIO(); public Graphics getGraphics(); public Audio getAudio(); public void setScreen(Screen screen); public Screen getCurrentScreen(); public Screen getStartScreen();}
?
public abstract class Screen { protected final Game game; public Screen(Game game){ this.game=game; } //更新状态 public abstract void update(float deltaTime); //刷新画面 public abstract void present(float deltaTime); public abstract void pause(); public abstract void resume(); public abstract void dispose();}
?
以上就基本实现了最简单的框架 觉得还是比较清晰的 所以照搬记录一下
?
最后Chapter3还提到了Frame Rate–Independent Movement 也就是固定帧数的问题?
例如若每次循环移动1个像素点 在60FPS的情况下 1秒就移动了60个像素 而30FPS则移动了30个像素 这样移动的速度相当于提升了2倍 作者给出的解决方案是根据deltaTime 计算应该移动的实际距离 也就是从x+=1 改成x+=30*deltaTime 30是希望的每秒移动像素值
?————————————题外话——————————
再想到之前看到的一个介绍 游戏基本是一个循环 : ?更新状态——重新绘制——休眠——更新状态
而其中的休眠时间并不是一个固定值 而是动态计算的 休眠长度=理想值-处理用去的时间 其实也跟上面的思路类似
?