📄 graphic.c
字号:
/*
* $Id: graphic.c,v 1.2 2003/08/29 03:13:17 casic Exp $
*
* graphic.c : draw graphics on LCD
*
* Copyright (C) 2003 ynding ( haoanian@263.net )
*
* 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
*
*/
#include "color.h"
#include "graphic.h"
/*
* draw a dot in the LCD
* x,y : coordinate of the pixel to be lightened
* color : the color of the dot
*/
void draw_dot(int x, int y, COLOR color)
{
unsigned char* fb_ptr;
COLOR pure_color = 0x0000;
/* if the dot is out of the LCD, return */
if ( x<0 || x>=320|| y<0 || y>=240) {
return;
}
/* calculate the address according the dot */
fb_ptr = (unsigned char*)0xc0000000 + (x+y*320)*12/8;
/* setup the image from the pixel to the frame buffer */
if ( x & 0x1 ) {
pure_color = ( color & 0x000f ) << 4;
*fb_ptr &= 0x0f;
*fb_ptr |= pure_color;
pure_color = ( color & 0x0ff0 ) >> 4;
*(fb_ptr+1) = 0xff & pure_color;
} else {
pure_color = color & 0x00ff;
*fb_ptr = 0xff & pure_color;
pure_color = ( color & 0x0f00 ) >> 8;
*(fb_ptr+1) &= 0xf0;
*(fb_ptr+1) |= pure_color;
}
return;
}
/* draw a vertical line */
void draw_vline(int x, int y1, int y2, COLOR color)
{
int tmp;
int i = 0;
if ( y1>y2 ) {
tmp = y1;
y1 = y2;
y2 = tmp;
}
tmp = y2 - y1;
for ( i=0;i<=tmp;++i ) {
draw_dot(x,y1+i,color);
}
return;
}
/* draw a horizontal line */
void draw_hline(int x1, int x2, int y, COLOR color)
{
int tmp;
int i = 0;
if ( x1>x2 ) {
tmp = x1;
x1 = x2;
x2 = tmp;
}
tmp = x2 - x1;
for ( i=0;i<=tmp;++i ) {
draw_dot(x1+i,y,color);
}
return;
}
/* draw a vertical dashed */
void draw_vdashed(int x, int y1, int y2, COLOR color)
{
int tmp;
int i = 0;
if ( y1>y2 ) {
tmp = y1;
y1 = y2;
y2 = tmp;
}
tmp = y2 - y1;
for ( i=0;i<=tmp;i+=5 ) {
draw_dot(x, y1+i, color);
}
return;
}
/* draw a horizontal dashed */
void draw_hdashed(int x1, int x2, int y, COLOR color)
{
int tmp;
int i = 0;
if ( x1>x2 ) {
tmp = x1;
x1 = x2;
x2 = tmp;
}
tmp = x2 - x1;
for ( i=0;i<=tmp;i+=5 ) {
draw_dot(x1+i, y, color);
}
return;
}
/* draw vertical scales */
void draw_vscale(int x, int y1, int y2, COLOR color)
{
int tmp;
if ( y1>y2 ) {
tmp = y1;
y1 = y2;
y2 = tmp;
}
tmp = y2 - y1;
draw_vdashed(x, y1, y2, color);
draw_vdashed(x-1, y1, y2, color);
draw_vdashed(x+1, y1, y2, color);
return;
}
/* draw horizontal scales */
void draw_hscale(int x1, int x2, int y, COLOR color)
{
int tmp;
if ( x1>x2 ) {
tmp = x1;
x1 = x2;
x2 = tmp;
}
tmp = x2 - x1;
draw_hdashed(x1, x2, y, color);
draw_hdashed(x1, x2, y-1, color);
draw_hdashed(x1, x2, y+1, color);
return;
}
/* draw a big dot -- 4*4 pixels */
void draw_bigdot(int x, int y, COLOR color)
{
draw_vline(x, y, y+4, color);
draw_vline(x+1, y, y+4, color);
draw_vline(x+2, y, y+4, color);
draw_vline(x+3, y, y+4, color);
return;
}
/*
* (start_x,start_y) : coordinate of the top-left-corner of the rectangle
* (end_x,end_y) : coordinate of the bottom-right-corner of the rectangle
*/
void draw_rectangle(int start_x, int start_y, int end_x, int end_y, COLOR color)
{
draw_vline(start_x, start_y, end_y, color);
draw_vline(end_x, start_y, end_y, color);
draw_hline(start_x, end_x, start_y, color);
draw_hline(start_x, end_x, end_y, color);
return;
}
/*
* (start_x,start_y) : coordinate of the top-left-corner of the rectangle
* (end_x,end_y) : coordinate of the bottom-right-corner of the rectangle
* this rectangle is filled with pixels which color is the same as the frame
*/
void draw_full_rectangle(int start_x, int start_y, int end_x, int end_y, COLOR color)
{
int i = 0;
int tmp = 0;
tmp = end_x - start_x;
for ( i=0;i<tmp;++i ) {
draw_vline(start_x+i, start_y, end_y, color);
}
return;
}
/* (x,y) -- coordinate of the center of the circle */
void draw_circle(int x, int y, int r, COLOR color)
{
int x1 = 0;
int y1 = r;
int p = 3 - 2 * r;
while(x1 <= y1 ) {
draw_dot(x + x1, y + y1, color);
draw_dot(x - x1, y + y1, color);
draw_dot(x + x1, y - y1, color);
draw_dot(x - x1, y - y1, color);
draw_dot(x + y1, y + x1, color);
draw_dot(x - y1, y + x1, color);
draw_dot(x + y1, y - x1, color);
draw_dot(x - y1, y - x1, color);
if(p < 0)
p+= 4 * x1++ +6;
else
p+= 4 * (x1++ - y1--) + 10;
}
return;
}
/* printf 4 colors' scales, and 7 characteristic colors */
void display_color_scales(void)
{
static COLOR red[] = {
red0, red1, red2, red3, red4, red5, red6, red7,
red8, red9, reda, redb, redc, redd, rede, redf
};
static COLOR green[] = {
green0, green1, green2, green3, green4, green5, green6, green7,
green8, green9, greena, greenb, greenc, greend, greene, greenf
};
static COLOR blue[] = {
blue0, blue1, blue2, blue3, blue4, blue5, blue6, blue7,
blue8, blue9, bluea, blueb, bluec, blued, bluee, bluef
};
static COLOR gray[] = {
gray0, gray1, gray2, gray3, gray4, gray5, gray6, gray7,
gray8, gray9, graya, grayb, grayc, grayd, graye, grayf
};
int i = 0;
for ( i=0;i<16;++i ) {
/* red scale */
draw_full_rectangle(20*i,0,20*(i+1),20,red[i]);
/* green scale */
draw_full_rectangle(20*i,20,20*(i+1),40,green[i]);
/* blue scale */
draw_full_rectangle(20*i,40,20*(i+1),60,blue[i]);
/* gray scale */
draw_full_rectangle(20*i,60,20*(i+1),80,gray[i]);
}
/* characteristic colors:red,orange,yellow,green,cyan,blue,purple */
draw_full_rectangle(90,100,110,120,RED);
draw_full_rectangle(110,100,130,120,ORANGE);
draw_full_rectangle(130,100,150,120,YELLOW);
draw_full_rectangle(150,100,170,120,GREEN);
draw_full_rectangle(170,100,190,120,CYAN);
draw_full_rectangle(190,100,210,120,BLUE);
draw_full_rectangle(210,100,230,120,PURPLE);
return;
}
void display_circles(void)
{
draw_circle(50,120,20,RED);
draw_circle(50,120,30,GREEN);
draw_circle(270,120,20,BLUE);
draw_circle(270,120,30,BLACK);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -