Double Buffering problems
Posted: Fri Feb 24, 2017 4:37 am
I have been running into some really weird problems when i tried implementing Double Buffering in my graphics driver in my operating system. When i don't use the double buffering my functions work fine. When i do however, if I use malloc to allocate space nothing happens and the screen is black but when i don't it's really weird and it draws my first yellow 200x200 rectangle, but it wont draw anything else and there is an exception (VirtualBox won't tell me much else).
Here is my graphics code:
Here is my graphics code:
Code: Select all
namespace graphics{
vesa_mode_info_t *modeInfo;
uint32_t vram = 0;
unsigned char* dblBuffer;
unsigned char* screen;
bool initialized = false;
vesa_mode_info_t * initGfx(){
if(!initialized){
modeInfo = EnterGraphicsMode();
vram = modeInfo->physbase;
//if(vram == 0)
// panic("ERR_GFX_MODE_FAILED", "Error whilst setting graphics mode.",false);
screen = (unsigned char*)vram;
//dblBuffer = (unsigned char*)malloc(modeInfo->width*modeInfo->height*modeInfo->bpp/8);
UpdateScreen();
initialized = true;
}
return modeInfo;
}
void putpixel(int x,int y, uint8_t r, uint8_t g, uint8_t b) {
putpixel(x,y,(r << 16) + (g << 8) + b);
}
/* Using Double Buffer
void putpixel(int x,int y, uint32_t colour) {
unsigned where = y * modeInfo->pitch + (x * (modeInfo->bpp/8));
dblBuffer[where] = colour & 255; // BLUE
dblBuffer[where + 1] = (colour >> 8) & 255; // GREEN
dblBuffer[where + 2] = (colour >> 16) & 255; // RED
}
void fillrect(int x, int y, int w, int h, uint32_t colour) {
int bpp = modeInfo->bpp;
int pitch = modeInfo->pitch;
uint32_t where = 0;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
where = (y+i) * pitch + ((x+j) * (bpp/8));
dblBuffer[where] = colour & 255; // BLUE
dblBuffer[where + 1] = (colour >> 8) & 255; // GREEN
dblBuffer[where + 2] = (colour >> 16) & 255; // RED
}
}
}*/
/* Without Double Buffer*/
void putpixel(int x,int y, uint32_t colour) {
unsigned where = y * modeInfo->pitch + (x * (modeInfo->bpp/8));
screen[where] = colour & 255; // BLUE
screen[where + 1] = (colour >> 8) & 255; // GREEN
screen[where + 2] = (colour >> 16) & 255; // RED
}
void fillrect(int x, int y, int w, int h, uint32_t colour) {
int bpp = modeInfo->bpp;
int pitch = modeInfo->pitch;
uint32_t where = 0;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
where = (y+i) * pitch + ((x+j) * (bpp/8));
screen[where] = colour & 255; // BLUE
screen[where + 1] = (colour >> 8) & 255; // GREEN
screen[where + 2] = (colour >> 16) & 255; // RED
}
}
}
void fillrect(int x, int y, int w, int h, unsigned char r, unsigned char g, unsigned char b){
fillrect(x,y,w,h,(r << 16) + (g << 8) + b);
}
// Copy back buffer to video memory
void UpdateScreen(){
memcpy(screen,dblBuffer,modeInfo->width*modeInfo->height*(modeInfo->bpp/8));
}
}