/*
    Robot Interface
    (C) 2006 Jason Hunt
    nulluser@gmail.com
    
    File: display.cpp
*/

#include <windows.h>
#include <stdio.h>

#include "display.h"
#include "input.h"

unsigned int display_x;
unsigned int display_y;



const unsigned int fb_size_x = 320;             // Initial screen size
const unsigned int fb_size_y = 200;


typedef struct fb_entry
{
 unsigned char b,g,r,p;

    
};


fb_entry frame_buffer[fb_size_x*fb_size_y];
BITMAPINFO image_buffer_info;          // Bitmap info for camera

HDC memdc = NULL;	           	           // Memory device context
HBITMAP h_mem_bitmap = NULL;                // Offscreen bitmap



       
void display_start( void )
{
    
    // Set the new Bitmap information
    image_buffer_info.bmiHeader.biSize = sizeof(image_buffer_info.bmiHeader);
    image_buffer_info.bmiHeader.biWidth = fb_size_x;
    image_buffer_info.bmiHeader.biHeight = fb_size_y; 
    image_buffer_info.bmiHeader.biPlanes = 1;
    image_buffer_info.bmiHeader.biBitCount = 32;
    image_buffer_info.bmiHeader.biCompression = BI_RGB;
    image_buffer_info.bmiHeader.biSizeImage = fb_size_x * fb_size_x  * 4;
    image_buffer_info.bmiHeader.biClrUsed = 0;
    image_buffer_info.bmiHeader.biClrImportant = 0;    
    
}       
       

/* Copy offscreen buffer to display */
void do_paint(HWND hwnd)
{
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(hwnd, &ps);
    BitBlt(hdc, 0, 0, display_x, display_y, memdc, 0, 0, SRCCOPY);
    EndPaint(hwnd, &ps);  
}
/* End of do_paint */



/* Update display */
void update_display(HWND hwnd)
{


 fb_entry c;
 c.r=0;
 c.g=0;
 c.b=0;
 
 
 
    for (int y = 0; y < fb_size_y; y++)
    {
        for (int x = 0; x < fb_size_x; x++)
        {
            frame_buffer[x+y*fb_size_x] = c;
 
        }
        c.b += 1;
    } 
 
 
 
    RECT r;

    GetClientRect(hwnd, &r);

    Rectangle(memdc, r.left, r.top, r.right, r.bottom);


        StretchDIBits(memdc, 
                      0, 0, display_x, display_y, 
                      0, 0, fb_size_x, fb_size_y,               
                      frame_buffer, &image_buffer_info, 
                      DIB_RGB_COLORS, SRCCOPY);         



  /*  SelectObject(memdc, (HBRUSH) GetStockObject(WHITE_BRUSH));
    SelectObject(memdc, (HBRUSH) GetStockObject(WHITE_PEN));    
   

    // Clear old offscreen buffer
    Rectangle(memdc, r.left, r.top, r.right, r.bottom);

    char b[400];    

//    sprintf(b, "Serial Rate %3.0f ", robot.get_update_speed());
//    TextOut(memdc, 1, 1, b, strlen(b));

//    sprintf(b, "Camera Rate %3d ",  camera.get_update_rate());
//    TextOut(memdc, 1, 20, b, strlen(b));

//    sprintf(b, "Motion time %4.2f ", motion_time);
//    TextOut(memdc, 1, 40, b, strlen(b));

    unsigned int text_height = font_y_size * text_row;

    SelectObject(memdc, (HBRUSH) GetStockObject(BLACK_PEN));    


        
    LeaveCriticalSection(&text_lock);   */     
}
/* End of update display */



/* Create off screen buffer */
void setup_display( HWND hwnd )
{
    // Get main DC
    HDC hdc = GetDC(hwnd);

  	// Create Memory device contextand bitmap
    memdc = CreateCompatibleDC(hdc);	

    // Create off screen buffer
  	h_mem_bitmap = CreateCompatibleBitmap(hdc, display_x, display_y);

  	// Select the off screen buffer into the DC
   	SelectObject(memdc, h_mem_bitmap);		

    ReleaseDC(hwnd, hdc);       // Main DC is no longer needed
}
/* End of setup_display */



/* Resize the display and update buffers */
void resize_display(HWND hwnd, LPARAM l)
{
    display_x = LOWORD(l);
    display_y = HIWORD(l);

    DeleteObject(h_mem_bitmap);		//	 Delete old screen buffer
    DeleteDC(memdc);		      	// Delete old memory DC

    setup_display(hwnd);

    InvalidateRect(hwnd, NULL, true);   
}
/* End of resize_display */









