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


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

#include "display.h"
#include "window.h"
#include "main.h"
#include "types.h"
#include "image.h"
#include "robot.h"

unsigned int display_x  =  640;                     // Sample ares size in pixels
unsigned int display_y  =  200;

static char text_area[text_y_size][text_x_size];  // Data for text display
        
        
HDC memdc = NULL;	           	           // Memory device context
HBITMAP h_mem_bitmap = NULL;                // Offscreen bitmap
        

bool display_fps = true;

/* Display the text area */
void display_text( HDC hdc )
{
    unsigned int y_ofs = image_y_size;

    if (y_ofs < MIN_TEXT_Y)
        y_ofs = MIN_TEXT_Y;

    // Clear the old text area
    Rectangle(hdc, 0, y_ofs, display_x-3, y_ofs+ text_area_height+2);
        
    // Draw text lines
    for (unsigned int r = 0; r < text_y_size; r++)
        TextOut(hdc, 2, r * font_y_size + y_ofs + 1, text_area[r], strlen(text_area[r]));

  //  for (unsigned int r = 0; r < text_y_size; r++)
//        TextOut(hdc, 2, display_y - r * font_y_size + 1, text_area[r], strlen(text_area[r]));

//        TextOut(hdc, 2, display_y-text_area_height, text_area[0], strlen(text_area[0]));

//    if (main_win != NULL)
  //      InvalidateRect (main_win, NULL, FALSE);
}
/* End if display_text */


/* Called when windows is resized to modify the display area size */
void display_resize( void )
{
    unsigned int image_x = image_x_size;
    unsigned int image_y = image_y_size;
           
    if (image_x < 320) image_x = 320;
    if (image_y < 200) image_y = 200;
    
    {
        WINDOWPLACEMENT p;                      // Holds window data

        p.length = sizeof(WINDOWPLACEMENT);     // Setup struct
        GetWindowPlacement(main_win, &p);       // Get window information
        
        display_x = image_x_size + extra_display_width; //+ 12;
        display_y = image_y_size + text_area_height + 34;
        
        p.rcNormalPosition.right = p.rcNormalPosition.left + display_x;
        p.rcNormalPosition.bottom= p.rcNormalPosition.top + display_y;
            
        SetWindowPlacement(main_win, &p);
    }
}
/* End of display_resize */



void draw_sensor( HDC hdc, unsigned int x, unsigned int y, 
                    unsigned int width, unsigned int heigth, double data )
{
    // Scale sonar into display
    unsigned int size = heigth - (unsigned int)(heigth  * data);

    if (size > heigth - 1) size = heigth  -1;

    HBRUSH color1 = CreateSolidBrush(RGB(150, 150, 150));
    HBRUSH color2 = CreateSolidBrush(RGB(0, 0, 255));

    RECT r;

    r.left = x;
    r.right = x + width;

    r.top = 0;
    r.bottom = size;

    FillRect(hdc, &r,color1);

    r.top = size;
    r.bottom = heigth;

    FillRect(hdc, &r,color2);
    
    DeleteObject(color1);
    DeleteObject(color2);    
}
      
      



/* Show the text buffer on the screen */
void update_display(HWND hwnd)
{
    SelectObject(memdc, (HBRUSH) GetStockObject(WHITE_BRUSH));
    SelectObject(memdc, (HBRUSH) GetStockObject(WHITE_PEN));    
   
    RECT r;

    GetClientRect(hwnd, &r);

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

   // PAINTSTRUCT ps;
    
   // HDC hdc = BeginPaint(main_win, &ps);
    
    
/*    if (image_buffer) 


        StretchDIBits(hdc, 
                      0, 0, display_x-sonar_bar_size, display_y, 
                      0, 0, image_x_size, image_y_size,               
                      image_buffer, &image_buffer_info, 
                      DIB_RGB_COLORS, SRCCOPY);             */


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

    if (image_buffer) 
        SetDIBitsToDevice(memdc, 0, 0, 
                          image_x_size, image_y_size,
                          0,0,0,image_y_size, 
                          image_buffer, 
                          &image_buffer_info, DIB_RGB_COLORS);


for (int i = 0; i < 8; i++)

    draw_sensor(memdc, image_x_size + 10 + i * 40, 0, 35, 100, robot_data.analog[i]/1024.0 );

//    draw_sensor(memdc, image_x_size + 50, 0, 35 , 100,  robot_data.analog[0]/1024.0 );

  //  draw_sensor(memdc, image_x_size + 90, 0, 35 , 100,  robot_data.analog[0]/1024.0 );


      
      
    if (display_fps)
    {
        // Display text information
        char b[400];
        sprintf(b, "FPS: %3d", fps);
        TextOut(memdc, 2, 2, b, strlen(b));
    }

    display_text(memdc);

  //  ReleaseDC(main_win, hdc);

//EndPaint(main_win, &ps);
    //ValidateRect(main_win, NULL);
}
/* End of display_text_area */


/* Reset the text area */
void clear_text_area( void )
{
//    memset(text_area, 0x00, text_y_size * text_x_size);
}
/* End fo clear text area */


/* Adds a line to the text buffer */
void add_line(char *line)
{    
    static unsigned int text_y = 0; 
    
    static bool text_clear = false;
    
    if (!text_clear)
    {
        memset(text_area, 0x00, text_y_size * text_x_size);
        text_clear = true;
    }    

    // Scrool the display up
    if (text_y == text_y_size)
    {
        for (int i = 1; i < text_y_size; i++)
            memcpy(text_area[i-1], text_area[i], text_x_size);
    }
    else
        text_y ++;
    // Copy the string
    unsigned int line_len = strlen(line);

    strncpy(text_area[text_y - 1], line, text_x_size);

    InvalidateRect (main_win, NULL, FALSE);
//    display_text();
}
/* End of add line */


/* Make a color */
color_type get_color(unsigned char r, unsigned char g, unsigned char b)
{
    color_type t;
    
    t.r = r; t.b = b; t.g = g;
    
    return(t);
}
/* End of get_color */



/* 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 */


/* Create off screen buffer */
void display_setup( 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 display_buffer_resize(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

    display_setup(hwnd);

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













