diff --git a/CMakeLists.txt b/CMakeLists.txt index aa0e06f..ea0b9f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,24 +18,29 @@ foreach (SIM_SUBDIR ${SIM_SUBDIRS}) include_directories(${SIM_SUBDIR}) endforeach () -file(GLOB LVGL_SUBDIRS "lvgl/demos/*" "lvgl/*" "lvgl/src/*") -foreach (LVGL_SUBDIR ${LVGL_SUBDIRS}) - include_directories(${LVGL_SUBDIR}) -endforeach () +set(LVGL_ROOT_DIR lvgl) +include(lvgl/env_support/cmake/custom.cmake) include_directories(easyx/include) link_directories(easyx/lib64) -file(GLOB_RECURSE SOURCES "demo/*/*.*" "sim/*/*.*" "lvgl/src/*/*.*" "lvgl/demos/*/*.*") -#file(GLOB_RECURSE SOURCES "demo/tft/*.*" "sim/*/*.*" "lvgl/src/*/*.*") +file(GLOB_RECURSE SOURCES "demo/*/*.*" "sim/*/*.*") +message(${SOURCES}) +add_subdirectory(SDL3 EXCLUDE_FROM_ALL) +link_libraries(SDL3::SDL3) link_libraries(libeasyx.a libgdi32.a libole32.a) add_executable(HW_Lib main.c ${SOURCES}) -add_subdirectory(SDL3 EXCLUDE_FROM_ALL) -link_libraries(SDL3::SDL3) + #导入库 add_subdirectory(lib) target_link_libraries(HW_Lib HW_LIB_List HW_LIB_Task HW_LIB_Printf HW_LIB_Utils HW_LIB_Iic - HW_LIB_Spi HW_LIB_Key HW_LIB_Oled HW_LIB_Font HW_LIB_Tft + HW_LIB_Spi HW_LIB_Key HW_LIB_Oled HW_LIB_Font HW_LIB_Tft lvgl::lvgl +) + +add_custom_command(TARGET HW_Lib POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + $ + $ ) diff --git a/demo/lvgl/test.c b/demo/lvgl/test.c index 301caa7..dfe8a81 100644 --- a/demo/lvgl/test.c +++ b/demo/lvgl/test.c @@ -2,7 +2,6 @@ #include "lv_port_disp.h" #include "lv_port_indev.h" #include "lvgl.h" -#include "lv_demo_widgets.h" #include #include "t_lvgl.h" diff --git a/demo/oled/test.c b/demo/oled/test.c index 0a4c23b..a82953d 100644 --- a/demo/oled/test.c +++ b/demo/oled/test.c @@ -114,12 +114,10 @@ OLED_T oled = { void Test_OLED(void *pVoid) { SIM_OLED_INIT(128, 64, CYAN, 0x0, 5, 0); - SIM_OLED_START(); - OLED_Init(&oled); OLED_CLS(&oled); - OLED_ShowCHString(&oled, 1, 24, "姓名学号:2233"); + OLED_ShowCHString(&oled, 1, 24, "星海科技机械师"); OLED_DrawRect(&oled, 0, 0, 127, 63); diff --git a/demo/tft/test.c b/demo/tft/test.c index 9660eeb..d079aa5 100644 --- a/demo/tft/test.c +++ b/demo/tft/test.c @@ -9,7 +9,7 @@ TFT_T demo_tft; uint16_t xs = 0, ys = 0, xe = 0, ye = 0; bool fill; - +static SIM_Display_t tft_display; //屏幕指令 #define WGRAM_CMD 0x5C #define SETXCMD 0x2A @@ -79,7 +79,7 @@ uint8_t tft_senddata(uint8_t *data, size_t len) { if (len == 2) { color_u.u8[1] = *data; color_u.u8[0] = *data++; - SIM_Color_DrawPiexl(RGB565_to_RGB888(color_u.u16, true), xs, ys); + SIM_Color_DrawPiexl(&tft_display, (SIM_Color_t) RGB565_to_RGB888(color_u.u16, true), xs, ys); // LOGT("Piexl","color:%x,x:%d,y:%d,len:%d",color_u.u16,xs,ys,len); // SIM_Color_ImgFromBuffer(color,xs, ys, uint16_t width, 1) } else { @@ -88,10 +88,10 @@ uint8_t tft_senddata(uint8_t *data, size_t len) { data++; color_u.u8[0] = *data; data++; - color[i] = RGB565_to_RGB888(color_u.u16, true); + color[i] = RGB565_to_RGB888(color_u.u16, true) | 0xFF000000; } -// SIM_Color_ImgFromBuffer(color, xs, ys, len / 2 - 1, 1); - SIM_Color_DrawHLineBuffer(color, xs, ys, len / 2); + SIM_Color_ImgFromBuffer(&tft_display, color, xs, ys, len / 2 - 1, 1); +// SIM_Color_DrawHLineBuffer(color, xs, ys, len / 2); // LOGT("Img","x:%d,y:%d,len:%d",xs,ys,len); } @@ -116,8 +116,8 @@ void Test_tft(void *) { demo_tft.dir = HORIZONTAL;//必填 //模拟初始化 - SIM_Display_INIT(demo_tft.width, demo_tft.height, 0xFFFF, 0x0000, 2, 0); - SIM_Display_START(); + SIM_Display_Init("TFT", demo_tft.width, demo_tft.height, 0xFFFF, 0x0000, 2, &tft_display); + TFT_Init(&demo_tft);//初始化 TFT_Color_t t; diff --git a/lib/oled/inc/oled.h b/lib/oled/inc/oled.h index 006f4dc..8d752f0 100644 --- a/lib/oled/inc/oled.h +++ b/lib/oled/inc/oled.h @@ -10,7 +10,7 @@ extern "C" { #define REFRESH_CALL_ENABLE 1 //使用DMA或者整体刷新函数 #define HZK_FONT //使用HZK 12/16 字体 tools下可自由生成 -//#define UTF8_TO_UNICODE //启用UTF8转换显示 +#define UTF8_TO_UNICODE //启用UTF8转换显示 //#define LVGL_FONT //启用LVGL字体 /** diff --git a/main.c b/main.c index 8a4bc06..1705473 100644 --- a/main.c +++ b/main.c @@ -12,26 +12,163 @@ #include "t_tft.h" #include "tool.h" #include "sim_test.h" +#include "sim_sdl.h" #include #include +#include +#include -int main() { + +#define POINTS_COUNT 4 + +static SDL_Point points[POINTS_COUNT] = { + {320, 200}, + {300, 240}, + {340, 240}, + {320, 200} +}; + +static SDL_Rect bigrect = {0, 0, 540, 380}; + +static SIM_SDL3_t sdl1; +static SIM_SDL3_t sdl2; +static SIM_SDL3_t sdl3; + +static SIM_SDL3_t sdls[3]; + +int main(int argc, char *argv[]) { srand((unsigned) time(NULL)); + + +// SDL_Log("Hello, SDL3!"); +// int count = SDL_GetNumRenderDrivers(); +// for (int i = 0; i < count; ++i) { +// const char* name = SDL_GetRenderDriver(i); +// SDL_Log("Render driver[%d]: %s", i, name); +// } +// if (SDL_Init(SDL_INIT_VIDEO) < 0) { +// SDL_Log("SDL_Init failed: %s", SDL_GetError()); +// return -1; +// } +// SDL_Window* window = SDL_CreateWindow("Hello, SDL3!", 800, 600, SDL_WINDOW_RESIZABLE); +// if (!window) { +// SDL_Log("Could not create a window: %s", SDL_GetError()); +// return -1; +// } +// +// SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); +// +// SDL_Renderer* renderer = SDL_CreateRenderer(window, nullptr); +// if (!renderer) { +// SDL_Log("Create renderer failed: %s", SDL_GetError()); +// return -1; +// } +// +// SDL_SetRenderDrawColor(renderer, 16, 0, 16, 255); +// SDL_RenderClear(renderer); +// SDL_Delay(2000); +// SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); +// for (int i = 0; i < 100; ++i) { +// SDL_RenderPoint(renderer,i,i); +// } +// if(!SDL_RenderPoints(renderer,points,POINTS_COUNT)){ +// SDL_Log("Draw Renderer failed: %s", SDL_GetError()); +// return -1; +// } +// SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); +// SDL_Rect rect1 = { 1, 1, 500, 500 }; +// SDL_RenderFillRect(renderer, &rect1); +// +// SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE); +// +// SDL_RenderLines(renderer, points, POINTS_COUNT); +// +// SDL_Rect rect = { 200, 300, 100, 100 }; +// SDL_RenderRect(renderer, &rect); +// +// SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255); +// SDL_RenderFillRect(renderer, &rect); +// +// SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); +// SDL_RenderFillRect(renderer, &bigrect); +// +// +// SDL_RenderPresent(renderer); +// SDL_Delay(5000); +// SDL_Event event; +// bool keep_going = true; +// +// while (keep_going) { +// while (SDL_PollEvent(&event)) { +// switch (event.type) { +// case SDL_EVENT_QUIT: { +// keep_going = false; +// break; +// } +// case SDL_EVENT_KEY_DOWN: { +// keep_going = keep_going && (event.key.key != SDLK_ESCAPE); +// break; +// } +// } +//// SDL_Log("Event: %d", event.type); +// } +// SDL_RenderClear(renderer); +// SDL_RenderPresent(renderer); +// } +// +// +// SDL_DestroyRenderer(renderer); +// SDL_DestroyWindow(window); +// SDL_Quit(); + + + +// SIM_SDLInit("TEST1",480,320,2,&sdls[0]); +// SIM_SDLInit("TEST2",480,320,1,&sdls[1]); +// SIM_SDLInit("TEST3",320,320,1,&sdls[2]); +// SIM_Color_t color; +// color.full=0xffff0000; +// for (int i = 0; i < 3; ++i) { +// for (int j = 0; j < 100; ++j) { +// SIM_SDL_Color_DrawPiexl(&sdls[i],j,j,color); +// } +// } +// SDL_Event event; +// bool keep_going = true; +// while (keep_going) { +// while (SDL_PollEvent(&event)) { +// switch (event.type) { +// case SDL_EVENT_QUIT: { +// keep_going = false; +// break; +// } +// case SDL_EVENT_KEY_DOWN: { +// keep_going = keep_going && (event.key.key != SDLK_ESCAPE); +// break; +// } +// } +//// SDL_Log("Event: %d", event.type); +// } +// } +// SIM_SDL_Stop(&sdls[0]); +// SIM_SDL_Stop(&sdls[1]); +// SIM_SDL_Stop(&sdls[2]); + // int i = 1; // POUT((++i) + (++i)); // // char str[] = "123.456"; // float result = Str2Float(str); // printf("Result: %.3f\n", result); - Test_Run("SPI", Test_spi); - Test_Run("IIC", Test_iic); - Test_Run("ArgPase", Test_argpase); - Test_Run("List", Test_List); - Test_RunTime("Key", Test_Key); - Test_RunTime("Queue", Test_Queue); - Test_RunTime("Task", Test_task); - Test_RunTime("OLED", Test_OLED); - Test_Run("LVGL", Test_lvgl); +// Test_Run("SPI", Test_spi); +// Test_Run("IIC", Test_iic); +// Test_Run("ArgPase", Test_argpase); +// Test_Run("List", Test_List); +// Test_RunTime("Key", Test_Key); +// Test_RunTime("Queue", Test_Queue); +// Test_RunTime("Task", Test_task); +// Test_Run("OLED", Test_OLED); +// Test_Run("LVGL", Test_lvgl); Test_RunTime("TFT", Test_tft); return 0; } diff --git a/sim/Test/sim_test.h b/sim/Test/sim_test.h index 8b56ac0..98a74c9 100644 --- a/sim/Test/sim_test.h +++ b/sim/Test/sim_test.h @@ -4,16 +4,24 @@ #ifndef HW_LIB_SIM_TEST_H #define HW_LIB_SIM_TEST_H +#ifdef __cplusplus +extern "C" { +#endif /** - * @brief 测试函数执行时间 - * @param name: [输入] 测试名称 - * @param pFunction: [输入] 指向待测试函数的指针 - * @return void - * @example Test("FunctionName", functionName); +* @brief 测试函数执行时间 +* @param name: [输入] 测试名称 +* @param pFunction: [输入] 指向待测试函数的指针 +* @return void +* @example Test("FunctionName", functionName); **/ void Test_RunTime(char *name, void (*pFunction)(void *)); void Test_Run(char *name, void (*pFunction)(void *)); + +#ifdef __cplusplus +} +#endif + #endif //HW_LIB_SIM_TEST_H diff --git a/sim/display/sim_display.cpp b/sim/display/sim_display.cpp index e1f7ad3..162281d 100644 --- a/sim/display/sim_display.cpp +++ b/sim/display/sim_display.cpp @@ -2,11 +2,6 @@ #include "graphics.h" #include - -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 diff --git a/sim/display/sim_display.h b/sim/display/sim_display.h index 46f7415..2875d2e 100644 --- a/sim/display/sim_display.h +++ b/sim/display/sim_display.h @@ -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); diff --git a/sim/lvgl/lv_port_disp.cpp b/sim/lvgl/lv_port_disp.cpp index 4815507..75b9382 100644 --- a/sim/lvgl/lv_port_disp.cpp +++ b/sim/lvgl/lv_port_disp.cpp @@ -44,7 +44,7 @@ static void disp_flush(lv_disp_drv_t* disp_drv, const lv_area_t* area, lv_color_ /********************** * STATIC VARIABLES **********************/ - +static SIM_Display_t lvgl_display; /********************** * MACROS **********************/ @@ -142,8 +142,7 @@ void lv_port_disp_init(void) static void disp_init(void) { /*You code here*/ - SIM_Display_INIT(MY_DISP_HOR_RES,MY_DISP_VER_RES, 0xffffff, BLACK, 1, false); - SIM_Display_START(); + SIM_Display_Init("LVGL", MY_DISP_HOR_RES, MY_DISP_VER_RES, 0xffffff, BLACK, 1, &lvgl_display); } volatile bool disp_flush_enabled = true; @@ -186,7 +185,7 @@ static void disp_flush(lv_disp_drv_t* disp_drv, const lv_area_t* area, lv_color_ color = (uint32_t*)malloc(sizeof(uint32_t) * l); colortorgb24(color_p, color, l); - SIM_Color_FillFromBuffer(color, area->x1, area->y1, area->x2, area->y2); + SIM_Color_FillFromBuffer(&lvgl_display, color, area->x1, area->y1, area->x2, area->y2); free(color); } diff --git a/sim/oled/oled.cpp b/sim/oled/oled.cpp index e50d228..f79e3c4 100644 --- a/sim/oled/oled.cpp +++ b/sim/oled/oled.cpp @@ -2,21 +2,16 @@ #include "graphics.h" #include +static SIM_Display_t oled_display; void SIM_OLED_INIT(int width, int height, uint32_t pixcolor, uint32_t backcolor, int scale, uint8_t b) { - SIM_Display_INIT(width,height,pixcolor,backcolor,scale,b); + SIM_Display_Init("OLED", width, height, pixcolor, backcolor, scale, &oled_display); } -void SIM_OLED_START() { - SIM_Display_START(); -} void SIM_OLED_STOP() { - SIM_Display_STOP(); + SIM_Display_STOP(&oled_display); } void SIM_OLED_DrawFromBuffer(uint8_t *buf, uint8_t width, uint8_t height) { - SIM_OneColor_DrawFromBuffer(buf,width,height); + SIM_OneColor_DrawFromBuffer(&oled_display, buf, width, height); } - - - diff --git a/sim/oled/sim_oled.h b/sim/oled/sim_oled.h index 3b189b5..a5241ae 100644 --- a/sim/oled/sim_oled.h +++ b/sim/oled/sim_oled.h @@ -19,13 +19,6 @@ extern "C" { **/ void SIM_OLED_INIT(int width, int height, uint32_t pixcolor, uint32_t backcolor, int scale, uint8_t border); -/** - * @brief 开始模拟 OLED 显示 - * @return void - * @example SIM_OLED_START(); - **/ -void SIM_OLED_START(); - /** * @brief 停止模拟 OLED 显示 * @return void diff --git a/sim/sdl/sim_sdl.cpp b/sim/sdl/sim_sdl.cpp new file mode 100644 index 0000000..384ceaf --- /dev/null +++ b/sim/sdl/sim_sdl.cpp @@ -0,0 +1,38 @@ +#include "sim_sdl.h" + + +bool SIM_SDLInit(char *name, int width, int height, int scale, SIM_SDL3_t *sdl3) { + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + SDL_Log("SDL_Init failed: %s", SDL_GetError()); + return false; + } + sdl3->scale = scale; + sdl3->window = SDL_CreateWindow(name, width * scale, height * scale, SDL_WINDOW_RESIZABLE); + if (!sdl3->window) { + SDL_Log("Could not create a window: %s", SDL_GetError()); + return false; + } + SDL_SetWindowPosition(sdl3->window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); + sdl3->renderer = SDL_CreateRenderer(sdl3->window, nullptr); + if (!sdl3->renderer) { + SDL_Log("Create renderer failed: %s", SDL_GetError()); + return false; + } + return true; +} + +void SIM_SDL_Color_DrawPiexl(SIM_SDL3_t *sdl3, uint16_t x, uint16_t y, SIM_Color_t color) { + SDL_SetRenderDrawColor(sdl3->renderer, color.ch.red, color.ch.green, color.ch.blue, color.ch.alpha); + for (int y_ = y * sdl3->scale; y_ < (y * sdl3->scale) + sdl3->scale; ++y_) { + for (int x_ = x * sdl3->scale; x_ < (x * sdl3->scale) + sdl3->scale; ++x_) { + SDL_RenderPoint(sdl3->renderer, x_, y_); + } + } + SDL_RenderPresent(sdl3->renderer); +} + +void SIM_SDL_Stop(SIM_SDL3_t *sdl3) { + SDL_DestroyRenderer(sdl3->renderer); + SDL_DestroyWindow(sdl3->window); + SDL_Quit(); +} diff --git a/sim/sdl/sim_sdl.h b/sim/sdl/sim_sdl.h new file mode 100644 index 0000000..2af98ee --- /dev/null +++ b/sim/sdl/sim_sdl.h @@ -0,0 +1,36 @@ +#ifndef HW_LIB_SIM_SDL_H +#define HW_LIB_SIM_SDL_H + +#include "SDL3/SDL.h" + +#ifdef __cplusplus +extern "C" { +#endif +typedef struct { + SDL_Window *window; + SDL_Renderer *renderer; + SDL_Event *event; + int scale; +} SIM_SDL3_t; + +typedef union { + struct { + uint8_t blue; + uint8_t green; + uint8_t red; + uint8_t alpha; + } ch; + uint32_t full; +} SIM_Color_t; + + +bool SIM_SDLInit(char *name, int width, int height, int scale, SIM_SDL3_t *sdl3); + +void SIM_SDL_Color_DrawPiexl(SIM_SDL3_t *sdl3, uint16_t x, uint16_t y, SIM_Color_t color); + +void SIM_SDL_Stop(SIM_SDL3_t *sdl3); + +#ifdef __cplusplus +} +#endif +#endif //HW_LIB_SIM_SDL_H