わかりました、これは私が持っているものです。ライフ生成のコンウェイ ゲームからのライブ状態またはデッド状態を含む 1 次元ビットマップ (またはビット配列、ビットセット、ビット文字列ですが、今はビットマップと呼びます) があります。のセル(x, y)
はビット at で表されy * map_width + x
ます。
これで、人生ゲームの「エンジン」が動作するようになりました。グラフィカルなものをレンダリングできればいいのですが。これには OpenGL が適していると思いましたが、どのように開始すればよいか、ビットマップを黒の 'n白いピクセル。
もしあなたが今、「馬鹿なopenglはダメだ...」と思っているなら、遠慮なく言ってください。私は変更を受け入れます。
編集
1 バイトあたり 8 ビットを格納するコンパクトな bitarray を使用し、マスキングを使用してそれらのバイトを取得することを忘れていました。これは私の手作りのライブラリです。
#include <stdint.h> // uint32_t
#include <stdlib.h> // malloc()
#include <string.h> // memset()
#include <limits.h> // CHAR_BIT
typedef uint32_t word_t;
enum {
WORD_SIZE = sizeof(word_t), // size of one word in bytes
BITS_PER_WORD = sizeof(word_t) * CHAR_BIT, // size of one word in bits
MAX_WORD_VALUE = UINT32_MAX // max value of one word
};
typedef struct {
word_t *words;
int nwords;
int nbytes;
} bitmap_t;
inline int WORD_OFFSET(int b) { return b / BITS_PER_WORD; }
inline int BIT_OFFSET(int b) { return b % BITS_PER_WORD; }
inline void setbit(bitmap_t bitmap, int n) { bitmap.words[WORD_OFFSET(n)] |= (1 << BIT_OFFSET(n)); }
inline void flipbit(bitmap_t bitmap, int n) { bitmap.words[WORD_OFFSET(n)] ^= (1 << BIT_OFFSET(n)); }
inline void clearbit(bitmap_t bitmap, int n) { bitmap.words[WORD_OFFSET(n)] &= ~(1 << BIT_OFFSET(n)); }
inline int getbit(bitmap_t bitmap, int n) { return (bitmap.words[WORD_OFFSET(n)] & (1 << BIT_OFFSET(n))) != 0; }
inline void clearall(bitmap_t bitmap) {
int i;
for (i = bitmap.nwords - 1; i >= 0; i--) {
bitmap.words[i] = 0;
}
}
inline void setall(bitmap_t bitmap) {
int i;
for (i = bitmap.nwords - 1; i >= 0; i--) {
bitmap.words[i] = MAX_WORD_VALUE;
}
}
bitmap_t bitmap_create(int nbits) {
bitmap_t bitmap;
bitmap.nwords = nbits / BITS_PER_WORD + 1;
bitmap.nbytes = bitmap.nwords * WORD_SIZE;
bitmap.words = malloc(bitmap.nbytes);
if (bitmap.words == NULL) { // could not allocate memory
printf("ERROR: Could not allocate (enough) memory.");
exit(1);
}
clearall(bitmap);
return bitmap;
}
void bitmap_free(bitmap_t bitmap) {
free(bitmap.words);
}