| 維度 | 位圖 | 調色板 |
|---|---|---|
| API | Graphics.drawImage() / GC.Bitblt() | 自研像素級繪製(drawRGB/drawPixels) |
| 優點 | 簡單、內建優化、支援 Game API | 像素級控制、內存減半、多層疊加快、規避 J2ME 內存 Bug |
| 缺點 | 無法像素操作(Alpha/灰度/換色)、16-bit 像素佔用大、多層疊加慢 | 前期開發重(8 向翻轉自寫)、老機型 CPU 慢於系統 API |
// 螢幕幀緩衝(RGB32)
class RGBImage {
int width, height;
int[] pixels; // length = w*h
}
// 調色板圖集(128 色,索引用 signed byte)
class PaletteImages {
int[] palette = new int[128]; // ARGB 調色表
int imageCount;
int[] imageSize; // [w0,h0, w1,h1...]
int[] imageOffset; // 每圖在 data 起始索引
byte[] indices; // 連續索引流:圖0所有像素, 圖1所有像素...
}
為何 128 色? Java byte 範圍 -128~127,避免無符號轉換開銷;實戰證明 128 色足夠表現力。
JAR 壓縮無憂慮:PNG 與 JAR 同用 LZ77,原始索引流入 JAR 後體積相當。
drawRGB 提交RGBImage 像素陣列Graphics.drawRGB(rgbData, 0, width, 0, 0, width, height, true) 上屏實測 Nokia 7610:8 層背景 + 10 角色,無額外優化,**100-120ms/幀** 可接受。
Thread.sleep,改用忙等待時間片// 典型 sleep 寫法 ~10 fps (Symbian 7.0)
Thread.sleep(frameDelay);
// 高精度忙等待 ~13 fps
long next = System.currentTimeMillis();
while (running) {
long now = System.currentTimeMillis();
if (now >= next) {
gameUpdateAndRender();
long cost = System.currentTimeMillis() - now;
next = (cost >= frameDelay) ? now + cost + 1 : now + frameDelay;
}
}
原理:利用 JVM 內部高精度時鐘避免 Thread.sleep 最小調度粒度損耗。
typedef unsigned char U8;
typedef unsigned short U16;
typedef unsigned long U32;
struct Size { int w, h; };
struct RGBImage { Size sz; U32* pixels; }; // 32-bit framebuffer
struct PaletteImages {
U32* palette; // 256 色無符號問題
int imageCount;
Size* sizes;
int* offsets;
U8* indices; // 8-bit 索引
};
C 端自然支援 256 色調色板,索引用 U8 無符號運算更簡潔。
drawImage。