SDL3 Form SIM_Display

This commit is contained in:
JiXieShi
2024-09-21 20:43:31 +08:00
parent fbc054d999
commit 6c9a999c71
14 changed files with 448 additions and 62 deletions

View File

@@ -2,11 +2,6 @@
#include "graphics.h"
#include <conio.h>
static uint32_t pixelColor, backgroundColor;
static int scaleFactor, w, h;
uint8_t border;
#define GET_BIT(x, bit) ((x & (1 << bit)) >> bit)
uint32_t RGB565_to_RGB888(uint16_t rgb565, bool isBGR) {
@@ -25,6 +20,10 @@ uint32_t RGB565_to_RGB888(uint16_t rgb565, bool isBGR) {
}
}
#ifndef USER_SDL3
static uint32_t pixelColor, backgroundColor;
static int scaleFactor, w, h;
uint8_t border;
void SIM_Display_INIT(int width, int height, uint32_t pixcolor, uint32_t backcolor, int scale, uint8_t b)
{
@@ -143,3 +142,90 @@ void SIM_Color_FillFromBuffer(uint32_t* buf, uint16_t xs, uint16_t ys, uint16_t
}
EndBatchDraw();
}
#else
bool SIM_Display_Init(char *name, int width, int height, uint32_t pixcolor, uint32_t backcolor, int scale,
SIM_Display_t *display) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
SDL_Log("SDL_Init failed: %s", SDL_GetError());
return false;
}
display->scale = scale;
display->pixelcolor.full = pixcolor;
display->backcolor.full = backcolor;
display->window = SDL_CreateWindow(name, width * scale, height * scale, SDL_WINDOW_RESIZABLE);
if (!display->window) {
SDL_Log("Could not create a window: %s", SDL_GetError());
return false;
}
SDL_SetWindowPosition(display->window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
display->renderer = SDL_CreateRenderer(display->window, nullptr);
if (!display->renderer) {
SDL_Log("Create renderer failed: %s", SDL_GetError());
return false;
}
SDL_SetRenderDrawColor(display->renderer, 0, 0, 0, 0);
SDL_RenderClear(display->renderer);
return true;
}
void SIM_Color_DrawPiexl(SIM_Display_t *display, SIM_Color_t color, uint16_t x, uint16_t y) {
SDL_SetRenderDrawColor(display->renderer, color.ch.red, color.ch.green, color.ch.blue, color.ch.alpha);
for (int y_ = y * display->scale; y_ < (y * display->scale) + display->scale; ++y_) {
for (int x_ = x * display->scale; x_ < (x * display->scale) + display->scale; ++x_) {
SDL_RenderPoint(display->renderer, x_, y_);
}
}
}
void SIM_OneColor_DrawFromBuffer(SIM_Display_t *display, uint8_t *buf, uint16_t width, uint16_t height) {
SIM_Color_t color;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
uint8_t byteData = buf[y * width + x];
for (int i = 0; i < 8; i++) {
uint8_t bit = GET_BIT(byteData, i);
if (bit)color = display->pixelcolor;
else color = display->backcolor;
SIM_Color_DrawPiexl(display, color, x, y * 8 + i);
}
}
}
SDL_RenderPresent(display->renderer);
}
void SIM_Color_ImgFromBuffer(SIM_Display_t *display, uint32_t *buf, uint16_t x, uint16_t y, uint16_t width,
uint16_t height) {
SIM_Color_t color;
for (int y_i = 0; y_i < height; y_i++) {
for (int x_i = 0; x_i < width; x_i++) {
color.full = *buf;
SIM_Color_DrawPiexl(display, color, x + x_i, y + y_i);
buf++;
}
}
SDL_RenderPresent(display->renderer);
}
void
SIM_Color_FillFromBuffer(SIM_Display_t *display, uint32_t *buf, uint16_t xs, uint16_t ys, uint16_t xe, uint16_t ye) {
SIM_Color_t color;
for (int y_i = ys; y_i < ye; y_i++) {
for (int x_i = xs; x_i < xe; x_i++) {
color.full = *buf;
SIM_Color_DrawPiexl(display, color, x_i, y_i);
buf++;
}
}
SDL_RenderPresent(display->renderer);
}
void SIM_Display_STOP(SIM_Display_t *display) {
SDL_DestroyRenderer(display->renderer);
SDL_DestroyWindow(display->window);
SDL_Quit();
}
#endif

View File

@@ -9,6 +9,10 @@ extern "C" {
#endif
#include "stdint.h"
//是否启用SDL3画图
#define USER_SDL3
/**
* @brief 定义颜色常量值,用于模拟显示
@@ -30,7 +34,7 @@ extern "C" {
#define YELLOW 0x55FFFF // 黄色
#define WHITE 0xFFFFFF // 白色
#ifndef USER_SDL3
/**
* @brief 初始化模拟 显示
* @param width: [输入] 显示宽度
@@ -111,6 +115,94 @@ void SIM_Color_ImgFromBuffer(uint32_t* buf, uint16_t x, uint16_t y, uint16_t wid
* @example SIM_Color_FillFromBuffer(buf, 50, 30, 150, 100);
**/
void SIM_Color_FillFromBuffer(uint32_t* buf, uint16_t xs, uint16_t ys, uint16_t xe, uint16_t ye);
#else
#include "SDL3/SDL.h"
typedef union {
struct {
uint8_t blue;
uint8_t green;
uint8_t red;
uint8_t alpha;
} ch;
uint32_t full;
} SIM_Color_t;
typedef struct {
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Event *event;
int scale;
SIM_Color_t pixelcolor;
SIM_Color_t backcolor;
} SIM_Display_t;
/**
* @brief 初始化模拟 显示
* @param name: [输入] 窗口名称
* @param width: [输入] 显示宽度
* @param height: [输入] 显示高度
* @param pixcolor: [输入] 像素颜色
* @param backcolor: [输入] 背景颜色
* @param scale: [输入] 显示缩放比例
* @param display: [输入] 输入窗口实例
* @return bool: true[成功] false[失败]
* @example SIM_Display_Init(128, 64, 0xFFFF00, 0x000000, 10, 1);
**/
bool SIM_Display_Init(char *name, int width, int height, uint32_t pixcolor, uint32_t backcolor, int scale,
SIM_Display_t *display);
/**
* @brief 在指定的坐标位置绘制一个像素点
* @param display: [输入] 输入窗口实例
* @param color 颜色
* @param x x坐标
* @param y y坐标
*/
void SIM_Color_DrawPiexl(SIM_Display_t *display, SIM_Color_t color, uint16_t x, uint16_t y);
void SIM_OneColor_DrawFromBuffer(SIM_Display_t *display, uint8_t *buf, uint16_t width, uint16_t height);
/**
* @brief 从缓冲区中填充图像
* @param display: [输入] 输入窗口实例
* @param buf: [输入] 缓冲区指针
* @param x: [输入] 图像起始横坐标
* @param y: [输入] 图像起始纵坐标
* @param width: [输入] 图像宽度
* @param height: [输入] 图像高度
* @return void
* @example SIM_Color_ImgFromBuffer(buf, 100, 50, 320, 240);
**/
void
SIM_Color_ImgFromBuffer(SIM_Display_t *display, uint32_t *buf, uint16_t x, uint16_t y, uint16_t width, uint16_t height);
/**
* @brief 从缓冲区中填充颜色
* @param display: [输入] 输入窗口实例
* @param buf: [输入] 缓冲区指针
* @param xs: [输入] 填充区域起始横坐标
* @param ys: [输入] 填充区域起始纵坐标
* @param xe: [输入] 填充区域结束横坐标
* @param ye: [输入] 填充区域结束纵坐标
* @return void
* @example SIM_Color_FillFromBuffer(buf, 50, 30, 150, 100);
**/
void
SIM_Color_FillFromBuffer(SIM_Display_t *display, uint32_t *buf, uint16_t xs, uint16_t ys, uint16_t xe, uint16_t ye);
/**
* @brief 停止模拟 显示
* @return void
* @example SIM_OLED_STOP();
**/
void SIM_Display_STOP(SIM_Display_t *display);
#endif
uint32_t RGB565_to_RGB888(uint16_t rgb565, bool isBGR);