【发布时间】:2011-03-05 20:13:22
【问题描述】:
好的,这就是我所拥有的。我有一个 1d 位图(或位数组、位集、位串,但我现在称它为位图)包含来自康威生命生成游戏的活状态或死状态。 (x, y) 处的单元格由y * map_width + x 处的位表示。
现在我的生活游戏“引擎”开始工作了,如果我现在可以渲染一些图形内容就好了。我认为 OpenGL 将是一个不错的选择,但我不知道我应该如何开始以及是否有任何特定的函数或着色器(我对着色器一无所知)可以有效地将位图渲染到具有黑色和白色像素的 2d 平面。
如果你现在认为“不,你这个白痴 opengl 不好用......”,尽管说出来,我愿意改变。
编辑
我忘了说我使用了一个紧凑的位数组,每个字节存储 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);
}
【问题讨论】:
标签: c opengl conways-game-of-life