기술/beginning games2011. 6. 2. 11:22
download download
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(); 
}



public class Pool<T> {   // Garbage Collector를 사용함
public interface PoolObjectFactory<T>{
public T createObject();
}
private final List<T> freeObjects; 
private final PoolObjectFactory<T> factory; 
private final int maxSize;
public Pool(PoolObjectFactory<T> factory, int maxSize) {
this.factory = factory;
this.maxSize = maxSize;
this.freeObjects = new ArrayList<T>(maxSize); 
}
public T newObject() {
T object = null;
if (freeObjects.size() == 0)
object = factory.createObject(); 
else
object = freeObjects.remove(freeObjects.size() - 1); 
return object;
}
public void free(T object) {
if (freeObjects.size() < maxSize)
freeObjects.add(object); 
}
}
 
download download download
Posted by yachtie_leo
기술/beginning games2011. 6. 2. 09:57
download download
public interface Graphics {
public static enum PixmapFormat {
ARGB8888, ARGB4444, 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 int getHeight(); 
public PixmapFormat getFormat();
public void dispose(); 

 


import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Pixmap;

public class AndroidGraphics implements Graphics {
AssetManager assets;
Bitmap frameBuffer;
Canvas canvas;
Paint paint;
Rect srcRect = new Rect();
Rect dstRect = new Rect();
public AndroidGraphics(AssetManager assets, Bitmap frameBuffer){
this.assets = assets;
this.frameBuffer = frameBuffer;
this.canvas = new Canvas(frameBuffer);
this.paint = new Paint();
}

@Override
public Pixmap newPixmap(String fileName, PixmapFormat format) {
Config config = null;
if(format == PixmapFormat.RGB565)
config = Config.RGB_565;
else if(format==PixmapFormat.ARGB4444)
config = config.ARGB_4444;
else
config = config.ARGB_8888;
Options options = new Options();
options.inPreferredConfig = config;
InputStream in = null;
Bitmap bitmap = null;
try{
in = assets.open(fileName);
bitmap = BitmapFactory.decodeStream(in);
if(bitmap == null)
throw new RuntimeException("couldn't load bitmap from asset '" + fileName + "'");
} catch (IOException e){
throw new RuntimeException("couldn't load bitmap from asset '" + fileName + "'");
} finally {
if(in != null){
try{
in.close();
} catch (IOException e){
}
}
}
if(bitmap.getConfig()==Config.RGB_565)
format = PixmapFormat.RGB565;
else if(bitmap.getConfig()==Config.ARGB_4444)
format = PixmapFormat.ARGB4444;
else
format = PixmapFormat.ARGB8888;
return new AndroidPixmap(bitmap, format);
}

@Override
public void clear(int color) {
canvas.drawRGB((color & 0xff0000) >> 16, (color & 0xff00) >> 8, (color & 0xff));
}

@Override
public void drawPixel(int x, int y, int color) {
paint.setColor(color);
canvas.drawPoint(x, y, paint);
}

@Override
public void drawLine(int x, int y, int x2, int y2, int color) {
paint.setColor(color);
canvas.drawLine(x, y, x2, y2, paint);
}

@Override
public void drawRect(int x, int y, int width, int height, int color) {
paint.setColor(color);
paint.setStyle(Style.FILL);
canvas.drawRect(x, y, x + width - 1, y + width - 1, paint);
}

@Override
public void drawPixmap(Pixmap pixmap, int x, int y, int srcX, int srcY,
int srcWidth, int srcHeight) {
srcRect.left = srcX;
srcRect.top = srcY;
srcRect.right = srcX + srcWidth - 1;
srcRect.bottom = srcY + srcHeight - 1;
dstRect.left = x;
dstRect.top = y;
dstRect.right = x + srcWidth - 1;
dstRect.bottom = y + srcHeight - 1;
canvas.drawBitmap(((AndroidPixmap) pixmap).bitmap, srcRect, dstRect, null);
}

@Override
public void drawPixmap(Pixmap pixmap, int x, int y) {
canvas.drawBitmap(((AndroidPixmap)pixmap).bitmap, x, y, null);
}

@Override
public int getWidth() {
return frameBuffer.getWidth();
}

@Override
public int getHeight() {
return frameBuffer.getHeight();
}
}
download download download
Posted by yachtie_leo