好的,這就是我所擁有的。我有一個1d位圖(或bitarray,bitset,bitstring,但我現在稱之爲位圖),其中包含康威生活遊戲中的活狀態或死狀態。位於(x, y)
處的單元由位於y * map_width + x
的位表示。渲染位圖/ bitarray到2D平面的最佳方式(使用OpenGL)
現在我有我的遊戲「引擎」工作,如果我現在可以渲染一些圖形的東西,這將是很好的。我認爲OpenGL會是一個不錯的選擇,但我不知道該如何開始,並且如果有任何特定的函數或着色器(我知道關於着色器沒有什麼),它可以高效地將位圖渲染成黑色的2d平面'n白色像素。
如果你現在認爲「不,你傻瓜opengl是不好的... ...」,隨時可以說,我願意改變。
EDIT
我忘了說,我使用的緊湊bitarray存儲每字節8位,並使用掩模來檢索這些字節。這是我手工製作的圖書館東西:
#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);
}
由於我存儲8位在一個字節中是否需要在將其提供給OpenGL之前將其轉換爲每字節一位或者它是否具有可以理解屏蔽存儲的模式? – orlp 2011-03-05 20:23:56
@nightcracker看到這個問題我問我什麼時候做我的GoL http://stackoverflow.com/questions/327642/opengl-and-monochrome-texture – 2011-03-05 20:27:53
你的問題幾乎是我的問題:)謝謝。雖然我有點擔心,因爲這會將內存使用量增加8倍。感謝上帝,我將應用程序模塊化,但我可以輕鬆更改系統。 – orlp 2011-03-05 20:32:20