From c49894a4931bc37b4364ba91786204c82ab9b24c Mon Sep 17 00:00:00 2001 From: idea4good Date: Fri, 9 Nov 2018 15:01:48 +0800 Subject: [PATCH] refactor assert, logout and bitmap --- README.md | 8 +++++-- core/core_include/api.h | 6 ++--- core/core_include/bitmap.h | 2 +- core/core_include/resource.h | 1 - core/core_include/wnd.h | 3 +-- core/src/adapter/api_linux.cpp | 33 +++++++++++++++++++++++++++ core/src/adapter/api_win.cpp | 28 +++++++++++++++++++++++ core/src/bitmap.cpp | 19 +++++----------- core/src/wave_ctrl.cpp | 17 +++++++------- gui/gui_include/my_resource.h | 4 ++++ gui/src/my_resource.cpp | 41 ++++++++++++++++++++++++++++++++++ 11 files changed, 131 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index b8953ee..ba6dc50 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,14 @@ 相比QT、MFC,GuiLite不预设开发者的使用场景,不在具体功能上,大包大揽;用框架的简洁,换取开发的自由;GuiLite在图形绘制上面,力图一步到位,运行效率感人。 -## 新功能:支持多种文字:Unicode +## 新功能:万国语和墙纸 +墙纸: +![unicode](doc/wallpaper.jpg) + +万国语(unicode): ![unicode](doc/unicode.jpg) -[如何制作多种文字资源?](https://github.com/idea4good/GuiLiteToolkit) +[如何制作多种文字/位图资源?](https://github.com/idea4good/GuiLiteToolkit) ## 卓越的跨平台能力 在Mac, iOS下的运行效果: diff --git a/core/core_include/api.h b/core/core_include/api.h index 90e171b..fa10986 100644 --- a/core/core_include/api.h +++ b/core/core_include/api.h @@ -7,12 +7,12 @@ #define TRUE 1 #define FALSE 0 -void do_assert(const char* file, int line); +void register_debug_function(void(*my_assert)(const char* file, int line), void(*my_log_out)(const char* log)); +void _assert(const char* file, int line); #define ASSERT(condition) \ do{ \ - if(!(condition))do_assert(__FILE__, __LINE__);\ + if(!(condition))_assert(__FILE__, __LINE__);\ }while(0) - void log_out(const char* log); #define GL_ARGB(a, r, g, b) ((((unsigned int)(a)) << 24) | (((unsigned int)(r)) << 16) | (((unsigned int)(g)) << 8) | ((unsigned int)(b))) diff --git a/core/core_include/bitmap.h b/core/core_include/bitmap.h index 3b9cee4..5f87b27 100644 --- a/core/core_include/bitmap.h +++ b/core/core_include/bitmap.h @@ -10,7 +10,7 @@ public: private: static void draw_bitmap_565(c_surface* surface, int z_order, int x, int y, int xsize, int ysize, const unsigned char* pPixel); static void get_bitmap_pos(const BITMAP_INFO *pBitmap, c_rect rect, unsigned int align_type, int &x, int &y); - static void draw_bitmap_565_inrect(c_surface* surface, int z_order, int x, int y, int width, int height, int xsize, int ysize, const unsigned char* pPixel); + static void draw_bitmap_565_in_rect(c_surface* surface, int z_order, int x, int y, int width, int height, int xsize, int ysize, const unsigned char* pPixel); c_bitmap(){} c_bitmap(const c_bitmap&); diff --git a/core/core_include/resource.h b/core/core_include/resource.h index 7690b45..8fb002d 100644 --- a/core/core_include/resource.h +++ b/core/core_include/resource.h @@ -6,7 +6,6 @@ typedef struct struct_bitmap_info { unsigned short XSize; unsigned short YSize; - unsigned short BytesPerLine; unsigned short BitsPerPixel; const unsigned char* pData; /* Pointer to picture data (indices) */ } BITMAP_INFO; diff --git a/core/core_include/wnd.h b/core/core_include/wnd.h index 07f391c..83cceb2 100644 --- a/core/core_include/wnd.h +++ b/core/core_include/wnd.h @@ -59,7 +59,6 @@ public: virtual void modify_style(unsigned int add_style = 0, unsigned int remove_style = 0); void set_str(char* str) { m_str = str; } - char* get_str_id() const { return m_str; } bool is_visible() const { return m_is_visible_now; } bool is_foreground(); @@ -141,7 +140,7 @@ protected: c_wnd* m_next_sibling; char* m_str; - const FONT_INFO* m_font_type; + const FONT_INFO* m_font_type; unsigned int m_font_color; unsigned int m_bg_color; diff --git a/core/src/adapter/api_linux.cpp b/core/src/adapter/api_linux.cpp index 5144a5d..ee3ca80 100644 --- a/core/src/adapter/api_linux.cpp +++ b/core/src/adapter/api_linux.cpp @@ -18,6 +18,39 @@ #define MAX_TIMER_CNT 10 #define TIMER_UNIT 50//ms +static void(*do_assert)(const char* file, int line); +static void(*do_log_out)(const char* log); +void register_debug_function(void(*my_assert)(const char* file, int line), void(*my_log_out)(const char* log)) +{ + do_assert = my_assert; + do_log_out = my_log_out; +} + +void _assert(const char* file, int line) +{ + if(do_assert) + { + do_assert(file, line); + } + else + { + printf("assert@ file:%s, line:%d, error no: %d\n", file, line, errno); + } +} + +void log_out(const char* log) +{ + if (do_log_out) + { + do_log_out(log); + } + else + { + printf(log); + fflush(stdout); + } +} + typedef struct _timer_manage { struct _timer_info diff --git a/core/src/adapter/api_win.cpp b/core/src/adapter/api_win.cpp index e7880ec..f4940a9 100644 --- a/core/src/adapter/api_win.cpp +++ b/core/src/adapter/api_win.cpp @@ -5,10 +5,38 @@ #include #include #include +#include #define MAX_TIMER_CNT 10 #define TIMER_UNIT 50//ms +static void(*do_assert)(const char* file, int line); +static void(*do_log_out)(const char* log); +void register_debug_function(void(*my_assert)(const char* file, int line), void(*my_log_out)(const char* log)) +{ + do_assert = my_assert; + do_log_out = my_log_out; +} + +void _assert(const char* file, int line) +{ + (do_assert) ? do_assert(file, line) : assert(false); +} + +void log_out(const char* log) +{ + if (do_log_out) + { + do_log_out(log); + } + else + { + printf(log); + fflush(stdout); + OutputDebugStringA(log); + } +} + typedef struct _timer_manage { struct _timer_info diff --git a/core/src/bitmap.cpp b/core/src/bitmap.cpp index 2aba491..798f670 100644 --- a/core/src/bitmap.cpp +++ b/core/src/bitmap.cpp @@ -23,7 +23,7 @@ void c_bitmap::draw_bitmap_in_rect(c_surface* surface, int z_order, const BITMAP } int x, y; get_bitmap_pos(pBitmap, rect, align_type, x, y); - draw_bitmap_565_inrect(surface, z_order, rect.m_left + x, rect.m_top + y, + draw_bitmap_565_in_rect(surface, z_order, rect.m_left + x, rect.m_top + y, (rect.m_right - rect.m_left + 1), (rect.m_bottom - rect.m_top + 1), pBitmap->XSize, pBitmap->YSize, (unsigned char const *)pBitmap->pData); } @@ -55,7 +55,6 @@ void c_bitmap::get_bitmap_pos(const BITMAP_INFO *pBitmap, c_rect rect, unsigned x = width - x_size; } break; - default: ASSERT(0); break; @@ -86,10 +85,7 @@ void c_bitmap::get_bitmap_pos(const BITMAP_INFO *pBitmap, c_rect rect, unsigned void c_bitmap::draw_bitmap_565(c_surface* surface, int z_order, int x, int y, int xsize, int ysize, const unsigned char* pPixel) { - const unsigned short* pData; - pData = (const unsigned short*)pPixel; - int BytesPerLine; - BytesPerLine = xsize; + const unsigned short* pData = (const unsigned short*)pPixel; for (int j = 0; j < ysize; j++) { const unsigned short * p = pData; @@ -98,16 +94,13 @@ void c_bitmap::draw_bitmap_565(c_surface* surface, int z_order, int x, int y, in unsigned int rgb = *p++; surface->draw_pixel(x + i, y + j, GL_RGB_16_to_32(rgb), z_order); } - pData += BytesPerLine; + pData += xsize; } } -void c_bitmap::draw_bitmap_565_inrect(c_surface* surface, int z_order, int x, int y, int width, int height, int xsize, int ysize, const unsigned char* pPixel) +void c_bitmap::draw_bitmap_565_in_rect(c_surface* surface, int z_order, int x, int y, int width, int height, int xsize, int ysize, const unsigned char* pPixel) { - const unsigned short* pData; - pData = (const unsigned short*)pPixel; - int BytesPerLine; - BytesPerLine = xsize; + const unsigned short* pData = (const unsigned short*)pPixel; for (int j = 0; j < ysize; j++) { if(j >= height)break; @@ -122,6 +115,6 @@ void c_bitmap::draw_bitmap_565_inrect(c_surface* surface, int z_order, int x, in unsigned int rgb = *p++; surface->draw_pixel(x + i, y + j, GL_RGB_16_to_32(rgb), z_order); } - pData += BytesPerLine; + pData += xsize; } } diff --git a/core/src/wave_ctrl.cpp b/core/src/wave_ctrl.cpp index 03daedb..b9fa21c 100644 --- a/core/src/wave_ctrl.cpp +++ b/core/src/wave_ctrl.cpp @@ -61,7 +61,6 @@ void c_wave_ctrl::set_max_min_base(short max_data, short min_data, short data_ba void c_wave_ctrl::set_wave_gain(E_WAVE_GAIN gain) { m_gain = gain; - on_paint(); } void c_wave_ctrl::set_wave_sample_rate(unsigned int rate) @@ -192,7 +191,7 @@ void c_wave_ctrl::draw_smooth_vline(int y_min, int y_max, int mid, unsigned int short r = GL_RGB_R(rgb); short g = GL_RGB_G(rgb); short b = GL_RGB_B(rgb); - int index = dy / 2 + 2; + int index = (dy >> 1) + 2; int y; draw_pixel(m_wave_cursor, mid, rgb); @@ -204,23 +203,23 @@ void c_wave_ctrl::draw_smooth_vline(int y_min, int y_max, int mid, unsigned int unsigned char cur_r,cur_g,cur_b; unsigned int cur_rgb; - for (int i = 1; i <= dy/2 + 1; ++i ) + for (int i = 1; i <= (dy >> 1) + 1; ++i ) { if ( (mid + i) <= y_max ) { y = mid + i; - cur_r = r*(index - i)/index; - cur_g = g*(index - i)/index; - cur_b = b*(index - i)/index; + cur_r = r * (index - i) / index; + cur_g = g * (index - i) / index; + cur_b = b * (index - i) / index; cur_rgb = GL_RGB(cur_r, cur_g, cur_b); draw_pixel(m_wave_cursor, y, cur_rgb); } if ( (mid - i) >= y_min ) { y = mid - i; - cur_r = r*(index - i)/index; - cur_g = g*(index - i)/index; - cur_b = b*(index - i)/index; + cur_r = r * (index - i) / index; + cur_g = g * (index - i) / index; + cur_b = b * (index - i) / index; cur_rgb = GL_RGB(cur_r, cur_g, cur_b); draw_pixel(m_wave_cursor, y, cur_rgb); } diff --git a/gui/gui_include/my_resource.h b/gui/gui_include/my_resource.h index 483a148..3f75d09 100644 --- a/gui/gui_include/my_resource.h +++ b/gui/gui_include/my_resource.h @@ -32,6 +32,10 @@ enum BITMAP_TYPE BITMAP_DOWN_BT_NORMAL, BITMAP_DOWN_BT_FOCUS, + BITMAP_CUSTOM1, + BITMAP_CUSTOM2, + BITMAP_CUSTOM3, + BITMAP_MAX }; diff --git a/gui/src/my_resource.cpp b/gui/src/my_resource.cpp index e63f304..43601dc 100644 --- a/gui/src/my_resource.cpp +++ b/gui/src/my_resource.cpp @@ -1,3 +1,4 @@ +#include "core_include/api.h" #include "core_include/rect.h" #include "core_include/resource.h" #include "../gui_include/my_resource.h" @@ -9,44 +10,84 @@ static unsigned int s_color_map[COLOR_MAX]; int c_my_resource::add_font(FONT_TYPE index, const FONT_INFO* font) { + if (index >= FONT_MAX) + { + ASSERT(FALSE); + return -1; + } s_font_map[index] = font; return 0; } const FONT_INFO* c_my_resource::get_font(FONT_TYPE index) { + if (index >= FONT_MAX) + { + ASSERT(FALSE); + return NULL; + } return s_font_map[index]; } int c_my_resource::add_bitmap(BITMAP_TYPE index, const BITMAP_INFO* bmp) { + if (index >= BITMAP_MAX) + { + ASSERT(FALSE); + return -1; + } s_bmp_map[index] = bmp; return 0; } const BITMAP_INFO* c_my_resource::get_bmp(BITMAP_TYPE index) { + if (index >= BITMAP_MAX) + { + ASSERT(FALSE); + return NULL; + } return s_bmp_map[index]; } int c_my_resource::add_shape(SHAPE_TYPE index, const COLOR_RECT* shape) { + if (index >= SHAPE_MAX) + { + ASSERT(FALSE); + return -1; + } s_shape_map[index] = shape; return 0; } const COLOR_RECT* c_my_resource::get_shape(SHAPE_TYPE index) { + if (index >= SHAPE_MAX) + { + ASSERT(FALSE); + return NULL; + } return s_shape_map[index]; } int c_my_resource::add_color(COLOR_TYPE index, const unsigned int color) { + if (index >= COLOR_MAX) + { + ASSERT(FALSE); + return -1; + } s_color_map[index] = color; return 0; } const unsigned int c_my_resource::get_color(COLOR_TYPE index) { + if (index >= COLOR_MAX) + { + ASSERT(FALSE); + return NULL; + } return s_color_map[index]; } \ No newline at end of file