Original Post
I'm using SDL w/ OpenGL in C with Netbeans 6.0.1. I'm trying to fathom completely about the framebuffer such as color depth and palette size. I want to use a single 8-bit color byte for the 640 x 480 resolution as output. The 8-bit color depth is 2^8 = 256 colors, which means that it needs 3 bits for both red and green and 2 remaining bits for blue. These colors can fit in 1-byte. Here is a code snippet on how I setup the attributes in SDL/OpenGL Here are some code snippets on how I setup my framebuffer The problem is that the OpenGL function, glDrawPixels, doesn't work. Is there anyone that can help me understand how this works.
// setup OpenGL attributes for SDL
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 3);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 3);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 2);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
// setup the video mode for OpenGL
screen = SDL_SetVideoMode(SCR_WIDTH, SCR_HEIGHT, 8, SDL_OPENGL);
unsigned char *frameBuffer = 0;
int scr_w = 0;
int scr_h = 0;
void fbClear(const unsigned char _r,
const unsigned char _g,
const unsigned char _b)
{
int x, y;
assert(frameBuffer);
unsigned char ucColor = 0;
ucColor = _r << 5;
ucColor = _g | ucColor;
ucColor = (_b >> 6) | ucColor;
for(y = 0; y < scr_h; y++)
{
for(x = 0; x < scr_w; x++)
frameBuffer[x + y * scr_w] = ucColor;
}
}
void fbInit(const int _w, const int _h)
{
scr_w = _w;
scr_h = _h;
/*
Since the 8-bit color depth is 1 byte, it needs 640 x 480 x 1 = 307200
bytes(300 KiB) to accommodate
plus 256 x 3 = 768 additional bytes store in the
palette map (assuming 24-bits) but I don't know if I'm
supposed to allocate this part
as well.
*/
frameBuffer = (unsigned char *)malloc(scr_w * scr_h *
sizeof(unsigned char));
assert(frameBuffer);
memset(frameBuffer, 0x00, scr_w * scr_h * sizeof(unsigned char));
}
void fbFree(){ free(frameBuffer); }
void setPixel(const int x, const int y, const unsigned char r,
const unsigned char g, const unsigned char b)
{
unsigned char ucColor = 0;
assert(frameBuffer);
if((x < 0 && x > scr_w) && (y < 0 && y > scr_h)) return;
ucColor = r << 5;
ucColor = g | ucColor;
ucColor = (b >> 6) | ucColor;
frameBuffer[x + y * scr_w] = ucColor;
}
void render()
{
fbClear(255, 255, 255);
glDrawPixels(SCR_WIDTH, SCR_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, frameBuffer);
SDL_GL_SwapBuffers();
}