resource manager support loading res from file system

This commit is contained in:
xianjimli 2018-04-27 18:12:12 +08:00
parent cd321ab9fb
commit f0980f748e
38 changed files with 616 additions and 160 deletions

View File

@ -10,23 +10,25 @@ GTEST_ROOT = os.path.join(TK_ROOT, '3rd/gtest/googletest')
BIN_DIR=os.path.join(TK_ROOT, 'bin')
LIB_DIR=os.path.join(TK_ROOT, 'lib')
LCD='SDL'
LCD='NANOVG'
if OS_NAME == 'Windows':
LCD='NANOVG'
else:
LCD='SDL'
LCD='NANOVG'
VGCANVAS='PICASSO'
VGCANVAS='AGG'
VGCANVAS='AGG'
VGCANVAS='PICASSO'
COMMON_CCFLAGS=' -DTK_ROOT=\\\"'+TK_ROOT+'\\\" -DLUA_COMPAT_MODULE -DHAS_STD_MALLOC -DSTBTT_STATIC -DSTB_IMAGE_STATIC -DIMAGE_RGBA'
COMMON_CCFLAGS=' -DTK_ROOT=\\\"'+TK_ROOT+'\\\" -DHAS_STD_MALLOC -DSDL2'
COMMON_CCFLAGS=COMMON_CCFLAGS+' -DLUA_COMPAT_MODULE -DSTBTT_STATIC -DSTB_IMAGE_STATIC -DWITH_STB_IMAGE -DWITH_STB_FONT'
if LCD == 'NANOVG':
VGCANVAS='NANOVG'
COMMON_CCFLAGS = COMMON_CCFLAGS + ' -DWITH_NANOVG -DWITH_GL3'
else:
COMMON_CCFLAGS = COMMON_CCFLAGS + ' -DWITH_STB_IMAGE -DWITH_STB_FONT -DSDL2'
if VGCANVAS == 'AGG':
COMMON_CCFLAGS = COMMON_CCFLAGS + ' -DWITH_AGG'
elif VGCANVAS == 'PICASSO':
COMMON_CCFLAGS = COMMON_CCFLAGS + ' -DWITH_PICASSO'
COMMON_CCFLAGS = COMMON_CCFLAGS + ' -DWITH_PICASSO -DIMAGE_RGBA'
os.environ['LCD'] = LCD
@ -46,17 +48,17 @@ OS_LIBS=['SDL2', 'glad', 'stdc++', 'pthread', 'm']
if OS_NAME == 'Darwin':
OS_LINKFLAGS='-framework OpenGL'
COMMON_CCFLAGS = COMMON_CCFLAGS + ' -DLUA_USE_POSIX -D__APPLE__'
COMMON_CCFLAGS = COMMON_CCFLAGS + ' -DLUA_USE_POSIX -D__APPLE__ -DWITH_FS_RES'
if VGCANVAS == 'PICASSO':
OS_LIBS = ['freetype'] + OS_LIBS
COMMON_CCFLAGS = COMMON_CCFLAGS + ' -DENABLE_FREE_TYPE2=1 -DFONT_FILE_NAME=\\\"'+TK_ROOT+'/demos/res/raw/fonts/font.ttf\\\"'
COMMON_CCFLAGS = COMMON_CCFLAGS + ' -DENABLE_FREE_TYPE2=1 -DFONT_FILE_NAME=\\\"'+TK_ROOT+'/demos/res/raw/fonts/default_ttf.ttf\\\"'
elif OS_NAME == 'Linux':
OS_LIBS = ['GL'] + OS_LIBS + ['dl']
COMMON_CCFLAGS = COMMON_CCFLAGS + ' -DLUA_USE_POSIX -DLINUX'
if VGCANVAS == 'PICASSO':
OS_LIBS = ['freetype'] + OS_LIBS
COMMON_CCFLAGS = COMMON_CCFLAGS + ' -DENABLE_FREE_TYPE2=1 -DFONT_FILE_NAME=\\\"'+TK_ROOT+'/demos/res/raw/fonts/font.ttf\\\"'
COMMON_CCFLAGS = COMMON_CCFLAGS + ' -DENABLE_FREE_TYPE2=1 -DFONT_FILE_NAME=\\\"'+TK_ROOT+'/demos/res/raw/fonts/default_ttf.ttf\\\"'
elif OS_NAME == 'Windows':
OS_LIBS=['SDL2', 'glad']

View File

@ -180,4 +180,4 @@ const unsigned char image_green_btn_o[] = {
0x26,0x83,0x5a,0xff,0x26,0x83,0x5a,0xff,0x26,0x83,0x5a,0xff,0x26,0x83,0x5a,0xff,0x26,0x83,0x5a,0xff,
0x26,0x83,0x5a,0xff,0x26,0x83,0x5a,0xff,0x26,0x83,0x5a,0xff,0x26,0x83,0x5a,0xff,0x26,0x83,0x5a,0xff,
0x3a,0x8d,0x68,0xff,0x62,0xa2,0x85,0xd7,0x85,0xb7,0xa0,0x97,0xd4,0xe5,0xdd,0x37,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,};/*3640*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x7f,0x00,0x00,};/*3640*/

View File

@ -157,4 +157,4 @@ const unsigned char image_slider_drag_o[] = {
0x7e,0x7e,0x7e,0xc0,0x7e,0x7e,0x7e,0xff,0x7e,0x7e,0x7e,0xff,0x7e,0x7e,0x7e,0xff,0x7e,0x7e,0x7e,0xff,
0x7e,0x7e,0x7e,0xd0,0x7d,0x7d,0x7d,0xa0,0x7d,0x7d,0x7d,0x50,0x7e,0x7e,0x7e,0x00,0x7e,0x7e,0x7e,0x00,
0x7e,0x7e,0x7e,0x00,0x7e,0x7e,0x7e,0x00,0x7e,0x7e,0x7e,0x00,0x7d,0x7d,0x7d,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,};/*3176*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x7f,0x00,0x00,};/*3176*/

View File

@ -123,4 +123,4 @@ const unsigned char theme_default[] = {
0x02,0x00,0x00,0x00,0x0d,0x00,0x00,0x00,0x69,0x6e,0x66,0x6f,0x00,0x0b,0x00,0x00,0x00,0x64,0x69,0x61,
0x6c,0x6f,0x67,0x5f,0x74,0x69,0x74,0x6c,0x65,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0xed,0xed,
0xed,0xff,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0xed,0xed,0xed,0xff,0x0a,0x00,
0x00,0x00,0xbe,0xbe,0xbe,0xff,0x00,0x00,0x00,0x00,0x5b,0x77,0xff,0x7f,};/*2494*/
0x00,0x00,0xbe,0xbe,0xbe,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,};/*2494*/

Binary file not shown.

BIN
demos/res/raw/ui/bottom.bin Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
demos/res/raw/ui/fade.bin Normal file

Binary file not shown.

BIN
demos/res/raw/ui/top.bin Normal file

Binary file not shown.

BIN
demos/res/raw/ui/window.bin Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,5 +1,6 @@
#include "tk.h"
#include "base/resource_manager.h"
#ifndef WITH_FS_RES
#include "res/inc/fonts/ap.data"
#include "res/inc/fonts/default.data"
#include "res/inc/fonts/default_ttf.data"
@ -41,51 +42,57 @@
#include "res/inc/ui/window.data"
#include "res/inc/ui/window1.data"
#include "res/inc/ui/window2.data"
#endif
ret_t resource_init(void) {
resource_manager_init(30);
resource_manager_t* rm = resource_manager();
resource_manager_add(font_ap);
resource_manager_add(font_default);
resource_manager_add(font_default_ttf);
resource_manager_add(image_bricks);
resource_manager_add(image_checked);
resource_manager_add(image_dialog_title);
resource_manager_add(image_earth);
resource_manager_add(image_green_btn_n);
resource_manager_add(image_green_btn_o);
resource_manager_add(image_green_btn_p);
resource_manager_add(image_info);
resource_manager_add(image_left_off);
resource_manager_add(image_left_on);
resource_manager_add(image_middle_off);
resource_manager_add(image_middle_on);
resource_manager_add(image_muted);
resource_manager_add(image_radio_checked);
resource_manager_add(image_radio_unchecked);
resource_manager_add(image_red_btn_n);
resource_manager_add(image_red_btn_o);
resource_manager_add(image_red_btn_p);
resource_manager_add(image_right_off);
resource_manager_add(image_right_on);
resource_manager_add(image_slider_bg);
resource_manager_add(image_slider_drag);
resource_manager_add(image_slider_drag_o);
resource_manager_add(image_slider_drag_p);
resource_manager_add(image_slider_fg);
resource_manager_add(image_slider_v_bg);
resource_manager_add(image_slider_v_fg);
resource_manager_add(image_unchecked);
resource_manager_add(image_unmuted);
resource_manager_add(theme_default);
resource_manager_add(ui_bottom);
resource_manager_add(ui_dialog1);
resource_manager_add(ui_dialog2);
resource_manager_add(ui_fade);
resource_manager_add(ui_top);
resource_manager_add(ui_window);
resource_manager_add(ui_window1);
resource_manager_add(ui_window2);
#ifdef WITH_FS_RES
resource_manager_load(rm, RESOURCE_TYPE_THEME, "default");
resource_manager_load(rm, RESOURCE_TYPE_FONT, "default_ttf");
#else
resource_manager_add(rm, font_ap);
resource_manager_add(rm, font_default);
resource_manager_add(rm, font_default_ttf);
resource_manager_add(rm, image_bricks);
resource_manager_add(rm, image_checked);
resource_manager_add(rm, image_dialog_title);
resource_manager_add(rm, image_earth);
resource_manager_add(rm, image_green_btn_n);
resource_manager_add(rm, image_green_btn_o);
resource_manager_add(rm, image_green_btn_p);
resource_manager_add(rm, image_info);
resource_manager_add(rm, image_left_off);
resource_manager_add(rm, image_left_on);
resource_manager_add(rm, image_middle_off);
resource_manager_add(rm, image_middle_on);
resource_manager_add(rm, image_muted);
resource_manager_add(rm, image_radio_checked);
resource_manager_add(rm, image_radio_unchecked);
resource_manager_add(rm, image_red_btn_n);
resource_manager_add(rm, image_red_btn_o);
resource_manager_add(rm, image_red_btn_p);
resource_manager_add(rm, image_right_off);
resource_manager_add(rm, image_right_on);
resource_manager_add(rm, image_slider_bg);
resource_manager_add(rm, image_slider_drag);
resource_manager_add(rm, image_slider_drag_o);
resource_manager_add(rm, image_slider_drag_p);
resource_manager_add(rm, image_slider_fg);
resource_manager_add(rm, image_slider_v_bg);
resource_manager_add(rm, image_slider_v_fg);
resource_manager_add(rm, image_unchecked);
resource_manager_add(rm, image_unmuted);
resource_manager_add(rm, theme_default);
resource_manager_add(rm, ui_bottom);
resource_manager_add(rm, ui_dialog1);
resource_manager_add(rm, ui_dialog2);
resource_manager_add(rm, ui_fade);
resource_manager_add(rm, ui_top);
resource_manager_add(rm, ui_window);
resource_manager_add(rm, ui_window1);
resource_manager_add(rm, ui_window2);
#endif
tk_init_resources();
return RET_OK;

138
src/base/fs.c Normal file
View File

@ -0,0 +1,138 @@
/**
* File: fs.h
* Author: Li XianJing <xianjimli@hotmail.com>
* Brief: simple fs api
*
* Copyright (c) 2018 - 2018 Li XianJing <xianjimli@hotmail.com>
*
* 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
* License file for more details.
*
*/
/**
* History:
* ================================================================
* 2018-04-27 Li XianJing <xianjimli@hotmail.com> created
*
*/
#include "base/fs.h"
#include "base/mem.h"
#if defined(HAS_POSIX_FS) || defined(__APPLE__) || defined(LINUX) || defined(WIN32)
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
int32_t fs_file_size(const char* name) {
struct stat st = {0};
return_value_if_fail(name != NULL, -1);
if(stat(name, &st) == 0 && (st.st_mode & S_IFMT) == S_IFREG) {
return st.st_size;
} else {
return -1;
}
}
int32_t fs_read_file_part(const char* name, void* buff, uint32_t size, uint32_t offset) {
int32_t ret = 0;
FILE* fp = NULL;
return_value_if_fail(name != NULL && buff != NULL && size > 0, -1);
fp = fopen(name, "rb");
if(fp != NULL) {
ret = fseek(fp, offset, SEEK_SET);
if(ret == 0) {
ret = fread(buff, 1, size, fp);
}
fclose(fp);
}
return ret;
}
void* fs_read_file(const char* name, uint32_t* size) {
uint8_t* buff = NULL;
int32_t len = fs_file_size(name);
return_value_if_fail(name != NULL && size != NULL && len > 0, NULL);
buff = (uint8_t*)MEM_ALLOC(len+1);
return_value_if_fail(buff != NULL, NULL);
if(fs_read_file_part(name, buff, len, 0) == len) {
*size = len;
buff[len] = '\0';
return buff;
} else {
*size = 0;
MEM_FREE(buff);
return NULL;
}
}
ret_t fs_write_file(const char* name, const void* buff, uint32_t size) {
FILE* fp = NULL;
return_value_if_fail(name != NULL && buff != NULL, RET_BAD_PARAMS);
fp = fopen(name, "wb+");
return_value_if_fail(fp != NULL, RET_FAIL);
fwrite(buff, size, 1, fp);
fclose(fp);
return RET_OK;
}
ret_t fs_unlink(const char* name) {
return_value_if_fail(name != NULL, RET_BAD_PARAMS);
unlink(name);
return RET_OK;
}
#else
int32_t fs_file_size(const char* name) {
(void)name;
return FALSE;
}
int32_t fs_read_file_part(const char* name, void* buff, uint32_t size, uint32_t offset) {
(void)name;
(void)buff;
(void)size;
(void)offset;
return RET_FAIL;
}
void* fs_read_file(const char* name, uint32_t* size) {
(void)name;
(void)size;
return NULL;
}
ret_t fs_write_file(const char* name, const void* buff, uint32_t size) {
(void)name;
(void)buff;
(void)size;
return RET_FAIL;
}
ret_t fs_unlink(const char* name) {
(void)name;
return RET_FAIL;
}
#endif

37
src/base/fs.h Normal file
View File

@ -0,0 +1,37 @@
/**
* File: fs.h
* Author: Li XianJing <xianjimli@hotmail.com>
* Brief: simple fs api
*
* Copyright (c) 2018 - 2018 Li XianJing <xianjimli@hotmail.com>
*
* 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
* License file for more details.
*
*/
/**
* History:
* ================================================================
* 2018-04-27 Li XianJing <xianjimli@hotmail.com> created
*
*/
#ifndef TK_FS_H
#define TK_FS_H
#include "base/types_def.h"
BEGIN_C_DECLS
ret_t fs_unlink(const char* name);
int32_t fs_file_size(const char* name);
void* fs_read_file(const char* name, uint32_t* size);
int32_t fs_read_file_part(const char* name, void* buff, uint32_t size, uint32_t offset);
ret_t fs_write_file(const char* name, const void* buff, uint32_t size);
END_C_DECLS
#endif /*TK_FS_H*/

View File

@ -23,13 +23,15 @@
#include "base/image_manager.h"
#include "base/resource_manager.h"
static image_manager_t* s_imm = NULL;
image_manager_t* image_manager() {
static image_manager_t* imm = NULL;
if (imm == NULL) {
imm = image_manager_create();
}
return s_imm;
}
return imm;
ret_t image_manager_set(image_manager_t* imm) {
s_imm = imm;
return RET_OK;
}
image_manager_t* image_manager_create() {
@ -50,7 +52,7 @@ ret_t image_manager_load(image_manager_t* imm, const char* name, bitmap_t* image
const resource_info_t* res = NULL;
return_value_if_fail(imm != NULL && name != NULL && image != NULL, RET_BAD_PARAMS);
res = resource_manager_ref(RESOURCE_TYPE_IMAGE, name);
res = resource_manager_ref(resource_manager(), RESOURCE_TYPE_IMAGE, name);
return_value_if_fail(res != NULL, RET_NOT_FOUND);
if (res->subtype == RESOURCE_TYPE_IMAGE_RAW) {
@ -61,9 +63,13 @@ ret_t image_manager_load(image_manager_t* imm, const char* name, bitmap_t* image
image->format = header->format;
image->name = res->name;
image->data = header->data;
return RET_OK;
} else if (imm->loader != NULL) {
return image_loader_load(imm->loader, res->data, res->size, image);
ret_t ret = image_loader_load(imm->loader, res->data, res->size, image);
resource_manager_unref(resource_manager(), res);
return ret;
} else {
return RET_NOT_FOUND;
}

View File

@ -40,6 +40,7 @@ typedef struct _image_manager_t {
} image_manager_t;
image_manager_t* image_manager(void);
ret_t image_manager_set(image_manager_t* imm);
image_manager_t* image_manager_create(void);
image_manager_t* image_manager_init(image_manager_t* im, image_loader_t* loader);

View File

@ -1,5 +1,5 @@
/**
* File: resource_manager.c
* File: resource_manager.h
* Author: Li XianJing <xianjimli@hotmail.com>
* Brief: resource manager
*
@ -19,53 +19,210 @@
*
*/
#include "base/array.h"
#include "base/mem.h"
#include "base/system_info.h"
#include "base/resource_manager.h"
static array_t s_resources;
static resource_manager_t* s_resource_manager = NULL;
ret_t resource_manager_init(uint32_t init_res_nr) {
return array_init(&(s_resources), init_res_nr) ? RET_OK : RET_FAIL;
#ifdef WITH_FS_RES
#include "base/fs.h"
#ifndef RES_ROOT
#define RES_ROOT TK_ROOT"/demos/res/raw"
#endif
static resource_info_t* load_resource(uint16_t type, uint16_t subtype, uint32_t size, const char* path, const char* name) {
resource_info_t* info = MEM_ALLOC(sizeof(resource_info_t) + size);
return_value_if_fail(info != NULL, NULL);
memset(info, 0x00, sizeof(resource_info_t));
info->size = size;
info->type = type;
info->subtype = subtype;
info->is_in_rom = FALSE;
strncpy(info->name, name, NAME_LEN);
ENSURE(fs_read_file_part(path, info->data, size, 0) == size);
return info;
}
const resource_info_t* resource_manager_ref(resource_type_t type, const char* name) {
resource_info_t* resource_manager_load(resource_manager_t* rm, resource_type_t type, const char* name) {
int32_t size = 0;
char path[MAX_PATH+1];
resource_info_t* info = NULL;
system_info_t* sysinfo = system_info();
float_t dpr = sysinfo->device_pixel_ratio;
switch(type) {
case RESOURCE_TYPE_FONT: {
snprintf(path, MAX_PATH, "%s/fonts/%s.ttf", RES_ROOT, name);
size = fs_file_size(path);
if(size > 0) {
info = load_resource(type, RESOURCE_TYPE_FONT_TTF, size, path, name);
break;
}
snprintf(path, MAX_PATH, "%s/fonts/%s.bin", RES_ROOT, name);
size = fs_file_size(path);
if(size > 0) {
info = load_resource(type, RESOURCE_TYPE_FONT_BMP, size, path, name);
break;
}
break;
}
case RESOURCE_TYPE_THEME: {
snprintf(path, MAX_PATH, "%s/theme/%s.bin", RES_ROOT, name);
size = fs_file_size(path);
if(size > 0) {
info = load_resource(type, RESOURCE_TYPE_THEME, size, path, name);
break;
}
break;
}
case RESOURCE_TYPE_IMAGE: {
const char* ratio = "x1";
if(dpr >= 3) {
ratio = "x3";
} else if(dpr >= 2) {
ratio = "x2";
}
ratio = "x1";
snprintf(path, MAX_PATH, "%s/images/%s/%s.png", RES_ROOT, ratio, name);
size = fs_file_size(path);
if(size > 0) {
info = load_resource(type, RESOURCE_TYPE_IMAGE_PNG, size, path, name);
break;
}
break;
}
case RESOURCE_TYPE_UI: {
snprintf(path, MAX_PATH, "%s/ui/%s.bin", RES_ROOT, name);
size = fs_file_size(path);
if(size > 0) {
info = load_resource(type, RESOURCE_TYPE_UI_BIN, size, path, name);
break;
}
break;
}
case RESOURCE_TYPE_XML: {
snprintf(path, MAX_PATH, "%s/xml/%s.xml", RES_ROOT, name);
size = fs_file_size(path);
if(size > 0) {
info = load_resource(type, RESOURCE_TYPE_XML, size, path, name);
break;
}
break;
}
case RESOURCE_TYPE_DATA: {
snprintf(path, MAX_PATH, "%s/data/%s.bin", RES_ROOT, name);
size = fs_file_size(path);
if(size > 0) {
info = load_resource(type, RESOURCE_TYPE_DATA, size, path, name);
break;
}
break;
}
default:break;
}
if(info != NULL) {
resource_manager_add(rm, info);
}
return info;
}
#else
resource_info_t* resource_manager_load(resource_manager_t* rm, resource_type_t type, const char* name) {
(void)type;
(void)name;
return NULL;
}
#endif/*WITH_FS_RES*/
resource_manager_t* resource_manager(void) {
return s_resource_manager;
}
ret_t resource_manager_set(resource_manager_t* rm) {
s_resource_manager = rm;
return RET_OK;
}
resource_manager_t* resource_manager_create(uint32_t init_res_nr) {
resource_manager_t* rm = MEM_ZALLOC(resource_manager_t);
return resource_manager_init(rm, init_res_nr);
}
resource_manager_t* resource_manager_init(resource_manager_t* rm, uint32_t init_res_nr) {
return_value_if_fail(rm != NULL, NULL);
array_init(&(rm->resources), init_res_nr);
return rm;
}
ret_t resource_manager_add(resource_manager_t* rm, const void* info) {
return_value_if_fail(rm != NULL && info != NULL, RET_BAD_PARAMS);
return array_push(&(rm->resources), (void*)info) ? RET_OK : RET_FAIL;
}
const resource_info_t* resource_manager_ref(resource_manager_t* rm, resource_type_t type, const char* name) {
uint32_t i = 0;
const resource_info_t* iter = NULL;
const resource_info_t** all = (const resource_info_t**)s_resources.elms;
return_value_if_fail(name != NULL, NULL);
const resource_info_t** all = NULL;
return_value_if_fail(rm != NULL && name != NULL, NULL);
for (i = 0; i < s_resources.size; i++) {
all = (const resource_info_t**)(rm->resources.elms);
for (i = 0; i < rm->resources.size; i++) {
iter = all[i];
if (type == iter->type && strcmp(name, iter->name) == 0) {
return iter;
}
}
return NULL;
return resource_manager_load(rm, type, name);
}
ret_t resource_manager_unref(const resource_info_t* info) {
return_value_if_fail(info != NULL, RET_BAD_PARAMS);
ret_t resource_manager_unref(resource_manager_t* rm, const resource_info_t* info) {
return_value_if_fail(rm != NULL && info != NULL, RET_BAD_PARAMS);
return RET_OK;
}
ret_t resource_manager_add(const void* data) {
const resource_info_t* info = (const resource_info_t*)data;
return_value_if_fail(info != NULL, RET_BAD_PARAMS);
ret_t resource_manager_deinit(resource_manager_t* rm) {
uint32_t i = 0;
resource_info_t* iter = NULL;
resource_info_t** all = NULL;
return_value_if_fail(rm != NULL, RET_BAD_PARAMS);
return array_push(&s_resources, (void*)info) ? RET_OK : RET_FAIL;
}
all = (resource_info_t**)(rm->resources.elms);
ret_t resource_manager_deinit() {
array_deinit(&(s_resources));
return RET_OK;
}
const resource_info_t** resource_manager_get_all(uint32_t* size) {
if (size != NULL) {
*size = s_resources.size;
for (i = 0; i < rm->resources.size; i++) {
iter = all[i];
if(!iter->is_in_rom) {
MEM_FREE(iter);
}
}
return (const resource_info_t**)s_resources.elms;
array_deinit(&(rm->resources));
return RET_OK;
}
ret_t resource_manager_destroy(resource_manager_t* rm) {
return_value_if_fail(rm != NULL, RET_BAD_PARAMS);
resource_manager_deinit(rm);
MEM_FREE(rm);
return RET_OK;
}

View File

@ -22,7 +22,7 @@
#ifndef TK_RESOURCE_MANAGER_H
#define TK_RESOURCE_MANAGER_H
#include "base/types_def.h"
#include "base/array.h"
BEGIN_C_DECLS
@ -79,7 +79,8 @@ typedef enum _resource_ui_type_t {
*/
typedef struct _resource_info_t {
uint16_t type;
uint16_t subtype;
uint8_t subtype;
uint8_t is_in_rom;
uint32_t size;
char name[NAME_LEN + 1];
uint8_t data[4];
@ -87,61 +88,102 @@ typedef struct _resource_info_t {
/**
* @class resource_manager_t
* @fake
*
*/
typedef struct _resource_manager_t {
array_t resources;
} resource_manager_t;
/**
* @method resource_manager
*
* @constructor
*
* @return {resource_manager_t*} resource manager对象
*/
resource_manager_t* resource_manager(void);
/**
* @method resource_manager_set
*
* @param {resource_manager_t*} rm resource manager对象
*
* @return {ret_t} RET_OK表示成功
*/
ret_t resource_manager_set(resource_manager_t* rm);
/**
* @method resource_manager_create
*
* @constructor
* @param {uint32_t} init_res_nr
*
* @return {resource_manager_t*} resource manager对象
*/
resource_manager_t* resource_manager_create(uint32_t init_res_nr);
resource_info_t* resource_manager_load(resource_manager_t* rm, resource_type_t type, const char* name);
/**
* @method resource_manager_init
*
* @static
* @constructor
* @param {resource_manager_t*} rm resource manager对象
* @param {uint32_t} init_res_nr
*
* @return {ret_t} RET_OK表示成功
* @return {resource_manager_t*} resource manager对象
*/
ret_t resource_manager_init(uint32_t init_res_nr);
resource_manager_t* resource_manager_init(resource_manager_t* rm, uint32_t init_res_nr);
/**
* @method resource_manager_add
*
* @static
* @param {resource_manager_t*} rm resource manager对象
* @param {resource_info_t} info
*
* @return {ret_t} RET_OK表示成功
*/
ret_t resource_manager_add(const void* info);
ret_t resource_manager_add(resource_manager_t* rm, const void* info);
/**
* @method resource_manager_ref
*
* @static
* @param {resource_manager_t*} rm resource manager对象
* @param {resource_type_t} type
* @param {char*} name
*
* @return {ret_t}
*/
const resource_info_t* resource_manager_ref(resource_type_t type, const char* name);
const resource_info_t* resource_manager_ref(resource_manager_t* rm, resource_type_t type, const char* name);
/**
* @method resource_manager_unref
*
* @static
* @param {resource_manager_t*} rm resource manager对象
* @param {resource_info_t} info
*
* @return {ret_t} RET_OK表示成功
*/
ret_t resource_manager_unref(const resource_info_t* info);
ret_t resource_manager_unref(resource_manager_t* rm, const resource_info_t* info);
/**
* @method resource_manager_deinit
* @param {resource_manager_t*} rm resource manager对象
*
* @static
*
* @return {ret_t} RET_OK表示成功
*/
ret_t resource_manager_deinit(void);
ret_t resource_manager_deinit(resource_manager_t* rm);
const resource_info_t** resource_manager_get_all(uint32_t* size);
/**
* @method resource_manager_destroy
* @param {resource_manager_t*} rm resource manager对象
* resource manager对象
*
* @return {ret_t} RET_OK表示成功
*/
ret_t resource_manager_destroy(resource_manager_t* rm);
END_C_DECLS

View File

@ -202,4 +202,8 @@ typedef float float_t;
#define STR_DEFAULT_FONT "default"
#endif /*WITH_NANOVG*/
#ifndef MAX_PATH
#define MAX_PATH 255
#endif/*MAX_PATH*/
#endif /*TYPES_DEF_H*/

View File

@ -159,7 +159,7 @@ lcd_t* lcd_sdl2_init(SDL_Renderer* render) {
lcd.lcd_mem = (lcd_mem_t*)lcd_mem_create(w, h, FALSE);
lcd.texture =
SDL_CreateTexture(render, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, w, h);
lcd.type = lcd.lcd_mem.type;
base->type = lcd.lcd_mem->base.type;
//SDL_CreateTexture(render, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, w, h);
// SDL_CreateTexture(render, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STREAMING, w, h);

View File

@ -29,7 +29,6 @@
#include "base/image_manager.h"
#include "base/resource_manager.h"
#ifndef WITH_NANOVG
#ifdef WITH_STB_FONT
#include "font/font_stb.h"
#endif /*WITH_STB_FONT*/
@ -37,13 +36,6 @@
#ifdef WITH_STB_IMAGE
#include "image_loader/image_loader_stb.h"
#endif /*WITH_STB_IMAGE*/
#define WITH_BITMAP_FONT 1
#else
#undef WITH_NANOVG
#undef WITH_STB_FONT
#undef WITH_BITMAP_FONT
#undef WITH_STB_IMAGE
#endif /*WITH_NANOVG*/
static ret_t tk_add_font(const resource_info_t* res) {
if (res->subtype == RESOURCE_TYPE_FONT_BMP) {
@ -62,8 +54,8 @@ static ret_t tk_add_font(const resource_info_t* res) {
ret_t tk_init_resources() {
uint32_t i = 0;
uint32_t nr = 0;
const resource_info_t** all = resource_manager_get_all(&nr);
uint32_t nr = resource_manager()->resources.size;
const resource_info_t** all = (const resource_info_t**)(resource_manager()->resources.elms);
for (i = 0; i < nr; i++) {
const resource_info_t* iter = all[i];
@ -84,6 +76,9 @@ ret_t tk_init(wh_t w, wh_t h, uint32_t* heap, uint32_t size) {
return_value_if_fail(platform_prepare() == RET_OK, RET_FAIL);
return_value_if_fail(mem_init(heap, size) == RET_OK, RET_FAIL);
return_value_if_fail(main_loop_init(w, h) != NULL, RET_FAIL);
image_manager_set(image_manager_create());
resource_manager_set(resource_manager_create(30));
#ifdef WITH_STB_IMAGE
image_manager_init(image_manager(), image_loader_stb());
@ -95,6 +90,7 @@ ret_t tk_init(wh_t w, wh_t h, uint32_t* heap, uint32_t size) {
ret_t tk_run() {
main_loop_run(main_loop());
main_loop_destroy(main_loop());
resource_manager_destroy(resource_manager());
return RET_OK;
}

View File

@ -170,11 +170,11 @@ ui_builder_t* ui_builder_default() {
widget_t* window_open(const char* name) {
ui_loader_t* loader = default_ui_loader();
ui_builder_t* builder = ui_builder_default();
const resource_info_t* ui = resource_manager_ref(RESOURCE_TYPE_UI, name);
const resource_info_t* ui = resource_manager_ref(resource_manager(), RESOURCE_TYPE_UI, name);
return_value_if_fail(ui != NULL, NULL);
ui_loader_load(loader, ui->data, ui->size, builder);
resource_manager_unref(ui);
resource_manager_unref(resource_manager(), ui);
return builder->root;
}

View File

@ -296,7 +296,7 @@ static ret_t vgcanvas_nanovg_set_font(vgcanvas_t* vgcanvas, const char* name) {
font_id = nvgFindFont(vg, name);
if (font_id < 0) {
const resource_info_t* r = resource_manager_ref(RESOURCE_TYPE_FONT, name);
const resource_info_t* r = resource_manager_ref(resource_manager(), RESOURCE_TYPE_FONT, name);
if (r == NULL || r->subtype != RESOURCE_TYPE_FONT_TTF) {
name = STR_DEFAULT_FONT;
font_id = nvgFindFont(vg, name);
@ -305,7 +305,7 @@ static ret_t vgcanvas_nanovg_set_font(vgcanvas_t* vgcanvas, const char* name) {
return RET_OK;
}
r = resource_manager_ref(RESOURCE_TYPE_FONT, name);
r = resource_manager_ref(resource_manager(), RESOURCE_TYPE_FONT, name);
}
if (r != NULL && r->subtype == RESOURCE_TYPE_FONT_TTF) {

19
tests/fs_test.cc Normal file
View File

@ -0,0 +1,19 @@
#include "base/fs.h"
#include "base/mem.h"
#include "gtest/gtest.h"
TEST(Fs, basic) {
char buff[128];
uint32_t size = 0;
const char* str = "hello world";
const char* filename = "test.bin";
fs_write_file(filename, str, strlen(str));
char* ret = (char*)fs_read_file(filename, &size);
ASSERT_EQ(fs_read_file_part(filename, buff, sizeof(buff), 0), strlen(str));
ASSERT_EQ(strcmp(ret, str), 0);
ASSERT_EQ(size, strlen(str));
fs_unlink(filename);
MEM_FREE(ret);
}

View File

@ -55,11 +55,12 @@ static ret_t add_image_res(const char* filename, const char* name) {
return_value_if_fail(ret == RET_OK, RET_FAIL);
strcpy(r->name, name);
r->is_in_rom = TRUE;
r->type = RESOURCE_TYPE_IMAGE;
r->subtype = RESOURCE_TYPE_IMAGE_RAW;
r->size = image_gen_buff(&image, r->data, sizeof(buff) - sizeof(resource_info_t));
return resource_manager_add(buff);
return resource_manager_add(resource_manager(), buff);
}
TEST(ImageLoaderStb, gen) {

View File

@ -33,21 +33,37 @@
#include "gtest/gtest.h"
#include "demos/resource.h"
#include "base/font_manager.h"
#include "base/image_manager.h"
#include "base/resource_manager.h"
#ifdef WITH_STB_FONT
#include "font/font_stb.h"
#endif /*WITH_STB_FONT*/
#ifdef WITH_STB_IMAGE
#include "image_loader/image_loader_stb.h"
#endif /*WITH_STB_IMAGE*/
static uint32_t s_heap_mem[4 * 1024 * 1024];
GTEST_API_ int main(int argc, char** argv) {
printf("Running main() from gtest_main.cc\n");
testing::InitGoogleTest(&argc, argv);
mem_init(s_heap_mem, sizeof(s_heap_mem));
image_manager_set(image_manager_create());
resource_manager_set(resource_manager_create(10));
#ifdef WITH_STB_IMAGE
image_manager_init(image_manager(), image_loader_stb());
#endif /*WITH_STB_IMAGE*/
resource_init();
tk_init_resources();
RUN_ALL_TESTS();
font_manager_destroy(font_manager());
resource_manager_deinit();
resource_manager_destroy(resource_manager());
return 0;
}

View File

@ -3,20 +3,24 @@
TEST(ResourceManager, basic) {
const resource_info_t* null_res = NULL;
resource_info_t img1 = {RESOURCE_TYPE_IMAGE, RESOURCE_TYPE_IMAGE_BMP, 100, "img1"};
resource_info_t img2 = {RESOURCE_TYPE_IMAGE, RESOURCE_TYPE_IMAGE_PNG, 101, "img2"};
resource_info_t ui1 = {RESOURCE_TYPE_UI, RESOURCE_TYPE_UI_BIN, 102, "ui1"};
resource_info_t ui2 = {RESOURCE_TYPE_UI, RESOURCE_TYPE_UI_XML, 103, "ui2"};
resource_manager_t* rm = resource_manager_create(10);
ASSERT_EQ(resource_manager_add(&img1), RET_OK);
ASSERT_EQ(resource_manager_add(&img2), RET_OK);
ASSERT_EQ(resource_manager_add(&ui1), RET_OK);
ASSERT_EQ(resource_manager_add(&ui2), RET_OK);
resource_info_t img1 = {RESOURCE_TYPE_IMAGE, RESOURCE_TYPE_IMAGE_BMP, TRUE, 100, "img1"};
resource_info_t img2 = {RESOURCE_TYPE_IMAGE, RESOURCE_TYPE_IMAGE_PNG, TRUE, 101, "img2"};
resource_info_t ui1 = {RESOURCE_TYPE_UI, RESOURCE_TYPE_UI_BIN, TRUE, 102, "ui1"};
resource_info_t ui2 = {RESOURCE_TYPE_UI, RESOURCE_TYPE_UI_XML, TRUE, 103, "ui2"};
ASSERT_EQ(resource_manager_ref(RESOURCE_TYPE_IMAGE, "img1"), &img1);
ASSERT_EQ(resource_manager_ref(RESOURCE_TYPE_IMAGE, "img2"), &img2);
ASSERT_EQ(resource_manager_ref(RESOURCE_TYPE_IMAGE, "img3"), null_res);
ASSERT_EQ(resource_manager_add(rm, &img1), RET_OK);
ASSERT_EQ(resource_manager_add(rm, &img2), RET_OK);
ASSERT_EQ(resource_manager_add(rm, &ui1), RET_OK);
ASSERT_EQ(resource_manager_add(rm, &ui2), RET_OK);
ASSERT_EQ(resource_manager_ref(RESOURCE_TYPE_UI, "ui1"), &ui1);
ASSERT_EQ(resource_manager_ref(RESOURCE_TYPE_UI, "ui2"), &ui2);
ASSERT_EQ(resource_manager_ref(rm, RESOURCE_TYPE_IMAGE, "img1"), &img1);
ASSERT_EQ(resource_manager_ref(rm, RESOURCE_TYPE_IMAGE, "img2"), &img2);
ASSERT_EQ(resource_manager_ref(rm, RESOURCE_TYPE_IMAGE, "img3"), null_res);
ASSERT_EQ(resource_manager_ref(rm, RESOURCE_TYPE_UI, "ui1"), &ui1);
ASSERT_EQ(resource_manager_ref(rm, RESOURCE_TYPE_UI, "ui2"), &ui2);
resource_manager_destroy(rm);
}

View File

@ -26,24 +26,17 @@
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "base/fs.h"
#include "base/mem.h"
#include "base/enums.h"
#include "base/resource_manager.h"
char* read_file(const char* file_name, uint32_t* length) {
struct stat st = {0};
if (stat(file_name, &st)) {
return NULL;
} else {
char* buffer = (char*)MEM_ALLOC(st.st_size + 1);
FILE* fp = fopen(file_name, "rb");
fread(buffer, 1, st.st_size, fp);
fclose(fp);
buffer[st.st_size] = '\0';
*length = st.st_size;
return fs_read_file(file_name, length);
}
return buffer;
}
ret_t write_file(const char* file_name, const void* buff, uint32_t length) {
return fs_write_file(file_name, buff, length);
}
int unique(wchar_t* str, int size) {
@ -130,6 +123,7 @@ ret_t output_res_c_source(const char* filename, uint16_t type, uint16_t subtype,
memset(res, 0x00, sizeof(resource_info_t));
res->size = size;
res->type = type;
res->is_in_rom = TRUE;
res->subtype = subtype;
memcpy(res->data, buff, size);
filename_to_name(filename, res->name, sizeof(res->name));

View File

@ -28,6 +28,7 @@ BEGIN_C_DECLS
int unique(wchar_t* str, int size);
char* read_file(const char* file_name, uint32_t* length);
ret_t write_file(const char* file_name, const void* buff, uint32_t length);
ret_t filename_to_name(const char* filename, char* str, uint32_t size);
ret_t output_c_source(const char* filename, const char* prefix, const char* name, uint8_t* buff,
uint32_t size);

View File

@ -23,15 +23,17 @@
#include "xml_theme_gen.h"
int main(int argc, char* argv[]) {
bool_t output_bin = argc > 3;
static uint32_t s_heap_mem[1024 * 1024];
mem_init(s_heap_mem, sizeof(s_heap_mem));
if (argc != 3) {
printf("Usage: %s input output\n", argv[0]);
if (argc < 3) {
printf("Usage: %s input output [bin]\n", argv[0]);
return 0;
}
xml_gen(argv[1], argv[2]);
xml_gen(argv[1], argv[2], output_bin);
return 0;
}

View File

@ -218,7 +218,7 @@ uint32_t xml_gen_buff(const char* xml, uint8_t* output, uint32_t max_size) {
return size;
}
bool xml_gen(const char* input_file, const char* output_file) {
bool xml_gen(const char* input_file, const char* output_file, bool_t output_bin) {
xml_builder_t b;
uint8_t buff[1024 * 1024];
return_value_if_fail(input_file != NULL && output_file != NULL, false);
@ -231,7 +231,12 @@ bool xml_gen(const char* input_file, const char* output_file) {
return_value_if_fail(end != NULL, false);
uint32_t size = end - buff;
output_res_c_source(output_file, RESOURCE_TYPE_THEME, 0, buff, size);
if(output_bin) {
write_file(output_file, buff, size);
} else {
output_res_c_source(output_file, RESOURCE_TYPE_THEME, 0, buff, size);
}
xml_parser_destroy(parser);

View File

@ -24,7 +24,7 @@
#ifndef XML_THEME_GEN_H
#define XML_THEME_GEN_H
bool xml_gen(const char* input_file, const char* output_file);
bool xml_gen(const char* input_file, const char* output_file, bool_t output_bin);
uint32_t xml_gen_buff(const char* xml, uint8_t* output, uint32_t max_size);
#endif /*XML_THEME_GEN_H*/

View File

@ -30,6 +30,7 @@ int main(int argc, char** argv) {
wbuffer_t wbuffer;
char* content = NULL;
uint8_t data[10 * 1024];
bool_t output_bin = argc > 3;
const char* in_filename = NULL;
const char* out_filename = NULL;
ui_loader_t* loader = xml_ui_loader();
@ -37,8 +38,8 @@ int main(int argc, char** argv) {
static uint32_t s_heap_mem[1024 * 1024];
mem_init(s_heap_mem, sizeof(s_heap_mem));
if (argc != 3) {
printf("Usage: in_filename out_filename\n");
if (argc < 3) {
printf("Usage: %s in_filename out_filename [bin]\n", argv[0]);
return 0;
}
@ -51,8 +52,12 @@ int main(int argc, char** argv) {
ui_loader_load(loader, (const uint8_t*)content, size, builder);
output_res_c_source(out_filename, RESOURCE_TYPE_UI, RESOURCE_TYPE_UI_BIN, wbuffer.data,
if(output_bin) {
write_file(out_filename, wbuffer.data, wbuffer.cursor);
} else {
output_res_c_source(out_filename, RESOURCE_TYPE_UI, RESOURCE_TYPE_UI_BIN, wbuffer.data,
wbuffer.cursor);
}
MEM_FREE(content);

View File

@ -40,8 +40,13 @@ def prepare():
os.makedirs(joinPath(OUTPUT_DIR, 'ui'));
def themegen(raw, inc):
print(joinPath(BIN_DIR, 'themegen') + ' ' + joinPath(INPUT_DIR, raw) + ' ' + joinPath(OUTPUT_DIR, inc))
os.system(joinPath(BIN_DIR, 'themegen') + ' ' + joinPath(INPUT_DIR, raw) + ' ' + joinPath(OUTPUT_DIR, inc))
def themegen_bin(raw, bin):
print(joinPath(BIN_DIR, 'themegen') + ' ' + joinPath(INPUT_DIR, raw) + ' ' + joinPath(INPUT_DIR, bin) + ' bin')
os.system(joinPath(BIN_DIR, 'themegen') + ' ' + joinPath(INPUT_DIR, raw) + ' ' + joinPath(INPUT_DIR, bin) + ' bin')
def resgen(raw, inc):
os.system(joinPath(BIN_DIR, 'resgen') + ' ' + joinPath(INPUT_DIR, raw) + ' ' + joinPath(OUTPUT_DIR, inc))
@ -55,11 +60,15 @@ def imagegen(raw, inc):
def xml_to_ui(raw, inc):
os.system(joinPath(BIN_DIR, 'xml_to_ui') + ' ' + raw + ' ' + inc)
def xml_to_ui_bin(raw, bin):
os.system(joinPath(BIN_DIR, 'xml_to_ui') + ' ' + raw + ' ' + bin + ' bin')
def gen_all():
themegen('theme/theme.xml', 'theme/default.data');
resgen('fonts/font.ttf', 'fonts/default_ttf.data');
resgen('fonts/action_protocol.ttf', 'fonts/ap.data');
fontgen('fonts/font.ttf', 'fonts/text.txt', 'fonts/default.data', 20);
themegen_bin('theme/theme.xml', 'theme/default.bin');
resgen('fonts/default_ttf.ttf', 'fonts/default_ttf.data');
resgen('fonts/ap.ttf', 'fonts/ap.data');
fontgen('fonts/default_ttf.ttf', 'fonts/text.txt', 'fonts/default.data', 20);
for f in glob.glob(joinPath(INPUT_DIR, 'images/'+DPI+'/*.*')):
inc=copy.copy(f);
@ -73,9 +82,12 @@ def gen_all():
for f in glob.glob(joinPath(INPUT_DIR, 'ui/*.xml')):
inc=copy.copy(f);
raw=copy.copy(f);
bin=copy.copy(f);
inc=inc.replace('.xml', '.data')
inc=inc.replace(INPUT_DIR, OUTPUT_DIR)
xml_to_ui(raw, inc)
bin=bin.replace('.xml', '.bin')
xml_to_ui_bin(raw, bin)
def writeResult(str):
fd = os.open(RESOURCE_C, os.O_RDWR|os.O_CREAT|os.O_TRUNC)
@ -86,6 +98,7 @@ def gen_res_c():
result = '#include "tk.h"\n'
result += '#include "base/resource_manager.h"\n'
result += '#ifndef WITH_FS_RES\n'
files=glob.glob(joinPath(OUTPUT_DIR, '**/*.data'))
for f in files:
incf = copy.copy(f);
@ -93,12 +106,17 @@ def gen_res_c():
incf=incf.replace('\\', '/');
incf=incf.replace('./', '');
result += '#include "'+incf+'"\n'
result += '#endif\n'
result += '\n';
result += 'ret_t resource_init(void) {\n'
result += ' resource_manager_init(30);\n\n'
result += ' resource_manager_t* rm = resource_manager();\n\n'
result += ''
result += '#ifdef WITH_FS_RES\n'
result += ' resource_manager_load(rm, RESOURCE_TYPE_THEME, "default");\n'
result += ' resource_manager_load(rm, RESOURCE_TYPE_FONT, "default_ttf");\n'
result += '#else\n'
for f in files:
incf = copy.copy(f);
basename = incf.replace(OUTPUT_DIR, '.');
@ -108,7 +126,8 @@ def gen_res_c():
basename = basename.replace('fonts', 'font');
basename = basename.replace('images', 'image');
basename = basename.replace('.data', '');
result += ' resource_manager_add('+basename+');\n'
result += ' resource_manager_add(rm, '+basename+');\n'
result += '#endif\n'
result += '\n'
result += ' tk_init_resources();\n'