408 lines
9.3 KiB
C
Raw Normal View History

2021-08-10 15:02:33 +08:00
//<2F><>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD>ƿ<EFBFBD>
#include "stdlib.h"
#include "string.h"
2021-08-10 15:02:33 +08:00
#include "spi.h"
2021-08-04 10:46:24 +08:00
#include "lcd.h"
#include "GameEngine.h"
#include "GE_Draw.h"
/**************************************** <20>Դ<EFBFBD> ****************************************/
2021-08-04 10:46:24 +08:00
uint8_t GE_Draw_VRam[153600];
2021-08-04 10:46:24 +08:00
/*****************************************************************************************/
_ge_draw_pic_set ge_draw_pic_set;
_ge_draw_mono_set ge_draw_mono_set;
2021-08-04 10:46:24 +08:00
/**
* @brief <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD> GE_Draw
*/
void GE_Draw_Init(void)
{
//<2F><><EFBFBD><EFBFBD>ͼƬ<CDBC><C6AC><EFBFBD><EFBFBD>
ge_draw_pic_set.is_reverse = FALSE;
ge_draw_pic_set.pos_mode = UP_LEFT;
2021-08-10 15:02:33 +08:00
//<2F><><EFBFBD>Ƶ<EFBFBD>ɫͼ<C9AB><CDBC><EFBFBD><EFBFBD>
ge_draw_mono_set.draw_mode = MONO_OR;
ge_draw_mono_set.pos_mode = UP_LEFT;
ge_draw_mono_set.mono_color = BLACK;
ge_draw_mono_set.back_color = WHITE;
2021-08-04 10:46:24 +08:00
GE_Draw_ClrAll(WHITE);
2021-08-04 10:46:24 +08:00
}
/**
* @brief ˢ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ
2021-08-04 10:46:24 +08:00
*/
void GE_Draw_Disp(void)
2021-08-04 10:46:24 +08:00
{
LCD_SetWin(0, 0, LCD_WIDTH, LCD_HEIGHT);
LCD_SendCmdDataBytes(LCD_CMD_RAMWR, GE_Draw_VRam, 153600);
2021-08-04 10:46:24 +08:00
}
/**
* @brief <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param color: <EFBFBD><EFBFBD>ɫ
*/
void GE_Draw_ClrAll(uint16_t color)
{
GE_Draw_Fill(0, 0, LCD_WIDTH, LCD_HEIGHT, color);
}
/**
* @brief <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶȽ<EFBFBD><EFBFBD><EFBFBD>
* @param x: 0~319
* @param y: 0~239
* @param width: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD>
* @param height: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD>
* @param color: <EFBFBD><EFBFBD>ɫ
*/
void GE_Draw_Fill(int16_t x, int16_t y, uint16_t width, uint16_t height, uint16_t color)
2021-08-04 10:46:24 +08:00
{
uint32_t point_num = width * height;
int16_t x0 = x, y0 = y;
2021-08-04 10:46:24 +08:00
for (uint32_t i = 0; i < point_num; i++)
{
GE_Draw_Point(x++, y, color);
2021-08-04 10:46:24 +08:00
if (x == x0 + width)
{
y++;
x = x0;
}
}
2021-08-04 10:46:24 +08:00
}
/**
* @brief <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD>ĵ<EFBFBD>
* @param x: 0~319
* @param y: 0~239
* @param color: <EFBFBD><EFBFBD>ɫ
*/
void GE_Draw_Point(int16_t x, int16_t y, uint16_t color)
2021-08-04 10:46:24 +08:00
{
if (x < 0 || x > LCD_WIDTH - 1 || y < 0 || y > LCD_HEIGHT - 1)
return;
GE_Draw_VRam[(x + y * 320) * 2] = color >> 8;
GE_Draw_VRam[(x + y * 320) * 2 + 1] = color;
}
/**
* @brief <EFBFBD><EFBFBD>ȡָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ذ<EFBFBD>ɫ
* @param x: 0~319
* @param y: 0~239
*/
uint16_t GE_Draw_GetPoint(int16_t x, int16_t y)
{
if (x < 0 || x > LCD_WIDTH - 1 || y < 0 || y > LCD_HEIGHT - 1)
return WHITE;
return ((uint16_t)GE_Draw_VRam[(x + y * 320) * 2] << 8) | GE_Draw_VRam[(x + y * 320) * 2 + 1];
2021-08-04 10:46:24 +08:00
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>߶<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param x1: 0~319
* @param y1: 0~239
* @param x2: 0~319
* @param y3: 0~239
* @param color: <EFBFBD><EFBFBD>ɫ
*/
2021-10-06 00:43:18 +08:00
void GE_Draw_Line(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color)
2021-08-04 10:46:24 +08:00
{
//ʹ<><CAB9> Bresenham <20>
int16_t dx = abs(x1 - x0);
int8_t sx = x0 < x1 ? 1 : -1;
int16_t dy = -abs(y1 - y0);
int8_t sy = y0 < y1 ? 1 : -1;
int16_t err = dx + dy, e2;
while (1)
{
GE_Draw_Point(x0, y0, color);
if (x0 == x1 && y0 == y1)
break;
e2 = 2 * err;
if (e2 >= dy)
{
err += dy;
x0 += sx;
}
if (e2 <= dx)
{
err += dx;
y0 += sy;
}
}
}
/**
* @brief <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><EFBFBD><EFBFBD>ƾ<EFBFBD><EFBFBD>α߿<EFBFBD>
* @param x: 0~319
* @param y: 0~239
* @param width: <EFBFBD><EFBFBD><EFBFBD>εĿ<EFBFBD>
* @param height: <EFBFBD><EFBFBD><EFBFBD>εĸ<EFBFBD>
* @param color: <EFBFBD><EFBFBD>ɫ
*/
void GE_Draw_Rectangle(int16_t x, int16_t y, uint16_t width, uint16_t height, uint16_t color)
2021-08-04 10:46:24 +08:00
{
uint16_t i;
for (i = 0; i < width; i++)
{
GE_Draw_Point(x + i, y, color);
GE_Draw_Point(x + i, y + height - 1, color);
}
for (i = 0; i < height; i++)
{
GE_Draw_Point(x, y + i, color);
GE_Draw_Point(x + width - 1, y + i, color);
}
}
/**
* @brief <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Բ
* @param xm: 0~319
* @param ym: 0~239
* @param r: Բ<EFBFBD>İ
* @param color: <EFBFBD><EFBFBD>ɫ
*/
void GE_Draw_Circle(int16_t xm, int16_t ym, uint16_t r, uint16_t color)
2021-08-04 10:46:24 +08:00
{
int16_t x = -r, y = 0, err = 2 - 2 * r, rm = r;
do
{
GE_Draw_Point(xm - x, ym + y, color);
GE_Draw_Point(xm - y, ym - x, color);
GE_Draw_Point(xm + x, ym - y, color);
GE_Draw_Point(xm + y, ym + x, color);
rm = err;
if (rm > x)
err += ++x * 2 + 1;
if (rm <= y)
err += ++y * 2 + 1;
} while (x < 0);
}
2021-10-06 00:43:18 +08:00
/**
* @brief <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Բ
* @param xm: 0~319
* @param ym: 0~239
* @param r: Բ<EFBFBD>İ
* @param color: <EFBFBD><EFBFBD>ɫ
*/
void GE_Draw_FillCircle(int16_t xm, int16_t ym, uint16_t r, uint16_t color)
{
int16_t x = -r, y = 0, err = 2 - 2 * r, rm = r;
do
{
GE_Draw_Line(xm - x, ym + y, xm + x, ym + y, color);
GE_Draw_Line(xm - y, ym - x, xm + y, ym - x, color);
GE_Draw_Line(xm + x, ym - y, xm - x, ym - y, color);
GE_Draw_Line(xm + y, ym + x, xm - y, ym + x, color);
rm = err;
if (rm > x)
err += ++x * 2 + 1;
if (rm <= y)
err += ++y * 2 + 1;
} while (x < 0);
}
2021-08-04 10:46:24 +08:00
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ƾ<EFBFBD><EFBFBD><EFBFBD>ͼƬ<EFBFBD><EFBFBD>ͼƬΪ const unsigned char <EFBFBD><EFBFBD><EFBFBD>4096 ɫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD>ͷ<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD>ȷ<EFBFBD><EFBFBD>ͼƬ<EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2021-08-04 10:46:24 +08:00
* @param x: 0~319
* @param y: 0~239
* @param is_reverse: TRUE Ϊ<EFBFBD><EFBFBD>ɫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫģʽ<EFBFBD><EFBFBD>ˢ<EFBFBD>½<EFBFBD><EFBFBD><EFBFBD>
* @param pos_mode: UP_LEFT Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͻǿ<EFBFBD>ʼ<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>MID Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ
* @param pic: ָ<EFBFBD><EFBFBD>ͼƬ
* @param width: ͼƬ<EFBFBD>Ŀ<EFBFBD>
* @param height: ͼƬ<EFBFBD>ĸ<EFBFBD>
*/
void GE_Draw_Pic(
int16_t x,
int16_t y,
2021-08-04 10:46:24 +08:00
uint8_t is_reverse,
uint8_t pos_mode,
const unsigned char *pic,
uint16_t width,
uint16_t height)
{
if (pos_mode == MID)
2021-08-04 10:46:24 +08:00
{
x = x - width / 2;
y = y - height / 2;
}
if (x == 0 && y == 0 && width == LCD_WIDTH && height == LCD_HEIGHT)
memcpy(GE_Draw_VRam, pic, 153600);
2021-08-04 10:46:24 +08:00
uint32_t point_num = width * height;
int16_t x0 = x, y0 = y;
uint16_t color;
2021-08-04 10:46:24 +08:00
if (is_reverse == TRUE) //<2F><>ɫģʽ
{
for (uint32_t i = 0; i < point_num; i++)
2021-08-04 10:46:24 +08:00
{
color = 0xffff - (((uint16_t)pic[2 * i] << 8) | pic[2 * i + 1]);
GE_Draw_Point(x++, y, color);
if (x == x0 + width)
{
y++;
x = x0;
}
2021-08-04 10:46:24 +08:00
}
}
else //<2F>Ƿ<EFBFBD>ɫģʽ
{
for (uint32_t i = 0; i < point_num; i++)
{
color = ((uint16_t)pic[2 * i] << 8) | pic[2 * i + 1];
GE_Draw_Point(x++, y, color);
if (x == x0 + width)
{
y++;
x = x0;
}
}
2021-08-04 10:46:24 +08:00
}
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ƾ<EFBFBD><EFBFBD><EFBFBD>ͼƬ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param x: 0~319
* @param y: 0~239
* @param pic: ָ<EFBFBD><EFBFBD>ͼƬ
* @param width: ͼƬ<EFBFBD>Ŀ<EFBFBD>
* @param height: ͼƬ<EFBFBD>ĸ<EFBFBD>
*/
void GE_Draw_Pic_WithSet(int16_t x, int16_t y, const unsigned char *pic, uint16_t width, uint16_t height)
2021-08-04 10:46:24 +08:00
{
GE_Draw_Pic(
x,
y,
ge_draw_pic_set.is_reverse,
ge_draw_pic_set.pos_mode,
pic,
width,
height);
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD>ɫλͼ<EFBFBD><EFBFBD>ͼƬΪ const unsigned char <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD>ͷ
* <EFBFBD><EFBFBD>ȷ<EFBFBD><EFBFBD>ͼƬ<EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param x: 0~319
* @param y: 0~239
* @param draw_mode: <EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD>ʽ<EFBFBD><EFBFBD>MONO_OR MONO_AND MONO_XOR MONO_COVER
* @param pos_mode: UP_LEFT Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͻǿ<EFBFBD>ʼ<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>MID Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ
* @param mono_color: <EFBFBD><EFBFBD>ɫ
* @param back_color: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD> MONO_COVER ʱ<EFBFBD><EFBFBD>Ч
* @param pic: ָ<EFBFBD><EFBFBD>ͼƬ
* @param width: ͼƬ<EFBFBD>Ŀ<EFBFBD>
* @param height: ͼƬ<EFBFBD>ĸ<EFBFBD>
*/
void GE_Draw_Mono(
int16_t x,
int16_t y,
uint8_t draw_mode,
uint8_t pos_mode,
uint16_t mono_color,
uint16_t back_color,
const unsigned char *pic,
uint16_t width,
uint16_t height)
{
if (pos_mode == MID)
{
x = x - width / 2;
y = y - height / 2;
}
uint8_t temp8;
int16_t x0 = x, y0 = y;
//ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (uint16_t i = 0; y - y0 < height; i++)
{
temp8 = pic[i];
for (uint16_t j = 0; j < 8; j++)
{
if (temp8 & 0x80)
{
if (
(draw_mode == MONO_OR) ||
(draw_mode == MONO_AND && GE_Draw_GetPoint(x, y) == mono_color) ||
(draw_mode == MONO_XOR && GE_Draw_GetPoint(x, y) != mono_color) ||
(draw_mode == MONO_COVER))
{
GE_Draw_Point(x, y, mono_color);
}
}
else
{
if (
(draw_mode == MONO_AND && GE_Draw_GetPoint(x, y) != mono_color) ||
(draw_mode == MONO_XOR && GE_Draw_GetPoint(x, y) == mono_color) ||
(draw_mode == MONO_COVER))
{
GE_Draw_Point(x, y, back_color);
}
}
temp8 <<= 1;
x++;
if (x - x0 == width)
{
x = x0;
y++;
break;
}
}
}
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD>ɫλͼ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>á<EFBFBD>ͼƬΪ const unsigned char <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD>ͷ
* <EFBFBD><EFBFBD>ȷ<EFBFBD><EFBFBD>ͼƬ<EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param x: 0~319
* @param y: 0~239
* @param pic: ָ<EFBFBD><EFBFBD>ͼƬ
* @param width: ͼƬ<EFBFBD>Ŀ<EFBFBD>
* @param height: ͼƬ<EFBFBD>ĸ<EFBFBD>
*/
void GE_Draw_Mono_WithSet(
int16_t x,
int16_t y,
const unsigned char *pic,
uint16_t width,
uint16_t height)
{
GE_Draw_Mono(
x,
y,
ge_draw_mono_set.draw_mode,
ge_draw_mono_set.pos_mode,
ge_draw_mono_set.mono_color,
ge_draw_mono_set.back_color,
pic,
width,
height);
}