/*
    ORPP Client

    (C) 2005 Jason Hunt

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

    Contact: huntjas2@msu.edu

    Compile with -lwsock32

    FILE: display.cpp
*/


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

#pragma hdrstop

#include "display.h"
#include "log.h"
 

// inital display size
unsigned int display_x  = 640;
unsigned int display_y  = 480;

unsigned int image_x  = 0;
unsigned int image_y  = 0;


bool display_text = true; // true if you want to see debugging iinformation

char text_area[text_y_size][text_x_size];  // the data for textr display

unsigned int text_y = 0;                            // current y_pos in text buffer


// stores image from camrea
color_type camera_buffer[max_image_x * max_image_y];

BITMAPINFO buffer_info;


/* setup off screen buffer */
void setup_buffer( void )
{
	buffer_info.bmiHeader.biSize = sizeof(buffer_info.bmiHeader);
	buffer_info.bmiHeader.biWidth = image_x;
	buffer_info.bmiHeader.biHeight = image_y;
	buffer_info.bmiHeader.biPlanes = 1;
	buffer_info.bmiHeader.biBitCount = 32;
	buffer_info.bmiHeader.biCompression = BI_RGB;
	buffer_info.bmiHeader.biSizeImage = image_x * image_y * 4;
	buffer_info.bmiHeader.biClrUsed = 0;
	buffer_info.bmiHeader.biClrImportant = 0;
}
/* end of setup_buffer */


/* reset the text area */
void clear_text_area( void )
{
    memset(text_area, ' ', text_y_size * text_x_size);
}
/* end fo clear text area */


/* adds a line to the text buffer */
void add_line(char *line)
{
    // 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 ++;

    // erase the line
    memset(text_area[text_y-1], ' ', text_x_size);

    // copy the string
    unsigned int line_len = strlen(line);

    if (line_len > text_x_size)
        line_len = text_x_size;

    memcpy(text_area[text_y-1], line, line_len);

    // force paint
    if (main_win != NULL)
        InvalidateRect (main_win, NULL, FALSE);
        
    if (logging_enabled)
        addlog(line); // add to log file
}
/* end of add line */



/* show the text buffer on the screen */
void display(void)
{
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(main_win,&ps);

    // copy camera buffer
    StretchDIBits(hdc, 0, 0, display_x, display_y, 
                       0, 0, image_x,   image_y,               
                       camera_buffer,
                       &buffer_info, 
                       DIB_RGB_COLORS, SRCCOPY);

    if (display_text)
    {
        SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));

        // draw text lines
        for (unsigned int r = 0; r < text_y_size; r++)
            TextOut(hdc, 0, r * font_y_size + display_y, text_area[r], text_x_size);
    }

    DeleteDC(hdc);

    EndPaint(main_win, &ps);
}
/* end of display_text_area */


/* called when we resize */
void resize_window(unsigned int x_size, unsigned int y_size)
{
    display_x  = x_size;

    if (display_text)
        display_y  = y_size - text_area_height;
    else
        display_y  = y_size;
}
/* end of resize */

