📄 lcd_terminal_window.c
字号:
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "simple_graphics.h"
#include "fonts.h"
#include "alt_video_display.h"
#include "lcd_terminal_window.h"
// This is our global LCD display
extern alt_video_display* global_display;
unsigned char* rendered_letters[94];
int terminal_vid_print_string_alpha( alt_video_display * display, char string[] )
{
static int current_y = 30;
static int current_x = 30;
static char last_char[80];
static int last_char_index = 0;
char character;
char* copy_src;
char* copy_dest;
int copy_size;
char* dest_ptr;
struct abc_font_struct* font = couriernewbold_24;
int string_length, i, frame;
string_length = strlen(string);
// Work out our new positon.
for( i = 0; i < string_length; i++ )
{
if( string[i] == '\n' )
{
if( current_y < ( 480 - ( 2 * ( font['A'].bounds_height ))))
{
current_x = 30;
current_y += font['A'].bounds_height;
}
else
{
copy_size = (480 - font['A'].bounds_height) * (800 * display->bytes_per_pixel);
for( frame = 0; frame < display->num_frame_buffers; frame++ )
{
copy_src = (( display->buffer_ptrs[display->buffer_being_written]->buffer ) +
( font['A'].bounds_height * ( display->width * display->bytes_per_pixel )));
copy_dest = ( display->buffer_ptrs[display->buffer_being_written]->buffer );
memcpy( copy_dest, copy_src, copy_size );
vid_draw_box ( 0, 480 - font['A'].bounds_height,
799, 479, BLACK_24, DO_FILL, display);
alt_video_display_register_written_buffer( display );
}
current_x = 30;
}
}
else if( string[i] == '\b' ) // Backspace
{
// go back the width of the last character
last_char_index = ( last_char_index == 0 ) ? 79 : ( last_char_index - 1 );
current_x -= font[last_char[last_char_index] - 33].bounds_width;
}
else
{
if( string[i] == ' ' )
{
character = '-'; // Here we just add the length of the '-' character
}
else
{
character = string[i];
for( frame = 0; frame < display->num_frame_buffers; frame++ )
{
dest_ptr = display->buffer_ptrs[display->buffer_being_written]->buffer + (current_x * 4) + (current_y * display->width * 4 );
VidCopyImageToBuffer( dest_ptr,
rendered_letters[character-33],
display->width,
font[character-33].bounds_width,
font[character-33].bounds_height );
// vid_print_char_alpha (current_x, current_y, WHITE_24, character, BLACK_24, font, display);
alt_video_display_register_written_buffer( display );
}
}
last_char[last_char_index] = character;
last_char_index = ( last_char_index + 1 ) % 80;
if(( current_x + font[character - 33].bounds_width ) < ( 800 - 30 ))
{
current_x += font[character - 33].bounds_width;
}
else
{
if( current_y < ( 480 - ( 2 * ( font['A'].bounds_height ))))
{
current_x = 30;
current_y += font['A'].bounds_height;
}
else
{
copy_size = (480 - font['A'].bounds_height) * (800 * display->bytes_per_pixel);
for( frame = 0; frame < display->num_frame_buffers; frame++ )
{
copy_src = (( display->buffer_ptrs[display->buffer_being_written]->buffer ) +
( font['A'].bounds_height * ( display->width * display->bytes_per_pixel )));
copy_dest = ( display->buffer_ptrs[display->buffer_being_written]->buffer );
memcpy( copy_dest, copy_src, copy_size );
vid_draw_box ( 0, 480 - font['A'].bounds_height,
799, 479, BLACK_24, DO_FILL, display);
alt_video_display_register_written_buffer( display );
}
current_x = 30;
}
}
}
}
return( 0 );
}
int printf_lcd( const char * format, ... )
{
va_list ap;
va_start( ap ,format );
char string[100];
vsprintf( string, format, ap );
terminal_vid_print_string_alpha( global_display, string );
return( 0 );
}
// Prints to both the LCD and stdout
int printf_lcd_stdout( const char * format, ... )
{
va_list ap;
va_start( ap ,format );
char string[100];
vsprintf( string, format, ap );
terminal_vid_print_string_alpha( global_display, string );
vprintf( format, ap );
return( 0 );
}
/*****************************************************************************
* Function: PrerenderFont
*
* Purpose: Prerenders a font so that it can be displayed more quickly.
*
* Returns: 0
****************************************************************************/
int PrerenderFont( struct abc_font_struct font[],
int fg_color,
int bg_color )
{
int i;
// We're going to use the video library functions to render this, and
// since those functions expect an alt_video_display type to which to write,
// we create a small temporary one here and fill it out just enough to draw
// to it.
alt_video_display* temp_buffer;
temp_buffer = (alt_video_display*) malloc(sizeof (alt_video_display));
temp_buffer->color_depth = 32;
temp_buffer->num_frame_buffers = 1;
temp_buffer->bytes_per_pixel = ( 4 );
temp_buffer->buffer_being_written = 0;
temp_buffer->buffer_ptrs[0] = (alt_video_frame*) malloc( sizeof( alt_video_frame ));
for( i = 0; i < 94; i++ )
{
temp_buffer->width = font[i].bounds_width ;
temp_buffer->height = font[i].bounds_height;
temp_buffer->bytes_per_frame = (( temp_buffer->width * temp_buffer->height ) * 4 );
temp_buffer->buffer_ptrs[0]->buffer = (void*) malloc( temp_buffer->bytes_per_frame );
vid_print_char_alpha ( 0, 0, fg_color, i + 33, bg_color, font, temp_buffer );
rendered_letters[i] = temp_buffer->buffer_ptrs[0]->buffer;
}
free( temp_buffer->buffer_ptrs[0] );
free( temp_buffer );
return( 0 );
}
/*****************************************************************************
* Function: FreeRenderedFont
*
* Purpose: Frees the memory allocated for a pre-rendered font.
*
* Returns: 0
****************************************************************************/
int FreeRenderedFont( )
{
int i;
for( i = 0; i < 94; i++ )
{
free( rendered_letters[i] );
}
return( 0 );
}
/*****************************************************************************
* Function: VidCopyImageToBuffer
*
* Purpose: Copies an image to a video buffer of a different width. This
* function differs from AsCopyVidBlock() in that the source and
* destination do not have to be contained in the same buffer,
* and can be differend widths. This function is useful for copying
* images stored in RAM to the active frame buffer to be displayed.
*
* Returns: 0
****************************************************************************/
int VidCopyImageToBuffer( char* dest, char* src,
int dest_width,
int src_width, int src_height )
{
int y;
//Copy one line at a time from top to bottom
for ( y = 0; y < src_height; y++ )
{
memcpy( dest, src, ( src_width * 4 ));
src += ( src_width * 4 );
dest += ( dest_width * 4 );
}
return( 0 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -