renderer: pass errors via SDL_SetError (#1919)
(cherry picked from commit 90cce5edee579575025c388d995eed637c4ad0bf)
This commit is contained in:
+2
-2
@@ -92,7 +92,7 @@ static int f_font_load(lua_State *L) {
|
|||||||
RenFont** font = lua_newuserdata(L, sizeof(RenFont*));
|
RenFont** font = lua_newuserdata(L, sizeof(RenFont*));
|
||||||
*font = ren_font_load(&window_renderer, filename, size, antialiasing, hinting, style);
|
*font = ren_font_load(&window_renderer, filename, size, antialiasing, hinting, style);
|
||||||
if (!*font)
|
if (!*font)
|
||||||
return luaL_error(L, "failed to load font");
|
return luaL_error(L, "failed to load font: %s", SDL_GetError());
|
||||||
luaL_setmetatable(L, API_TYPE_FONT);
|
luaL_setmetatable(L, API_TYPE_FONT);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -138,7 +138,7 @@ static int f_font_copy(lua_State *L) {
|
|||||||
RenFont** font = lua_newuserdata(L, sizeof(RenFont*));
|
RenFont** font = lua_newuserdata(L, sizeof(RenFont*));
|
||||||
*font = ren_font_copy(&window_renderer, fonts[i], size, antialiasing, hinting, style);
|
*font = ren_font_copy(&window_renderer, fonts[i], size, antialiasing, hinting, style);
|
||||||
if (!*font)
|
if (!*font)
|
||||||
return luaL_error(L, "failed to copy font");
|
return luaL_error(L, "failed to copy font: %s", SDL_GetError());
|
||||||
luaL_setmetatable(L, API_TYPE_FONT);
|
luaL_setmetatable(L, API_TYPE_FONT);
|
||||||
if (table)
|
if (table)
|
||||||
lua_rawseti(L, -2, i+1);
|
lua_rawseti(L, -2, i+1);
|
||||||
|
|||||||
+5
-2
@@ -125,7 +125,7 @@ int main(int argc, char **argv) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) {
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) {
|
||||||
fprintf(stderr, "Error initializing sdl: %s", SDL_GetError());
|
fprintf(stderr, "Error initializing SDL: %s\n", SDL_GetError());
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
SDL_EnableScreenSaver();
|
SDL_EnableScreenSaver();
|
||||||
@@ -171,7 +171,10 @@ int main(int argc, char **argv) {
|
|||||||
fprintf(stderr, "Error creating lite-xl window: %s", SDL_GetError());
|
fprintf(stderr, "Error creating lite-xl window: %s", SDL_GetError());
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
ren_init(window);
|
if (ren_init(window) != 0) {
|
||||||
|
fprintf(stderr, "Error initializing renderer: %s\n", SDL_GetError());
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
lua_State *L;
|
lua_State *L;
|
||||||
init_lua:
|
init_lua:
|
||||||
|
|||||||
+31
-12
@@ -32,6 +32,16 @@ static void* _check_alloc(void *ptr, const char *const file, size_t ln) {
|
|||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getting freetype error messages (https://freetype.org/freetype2/docs/reference/ft2-error_enumerations.html)
|
||||||
|
static const char *const get_ft_error(FT_Error err) {
|
||||||
|
#undef FTERRORS_H_
|
||||||
|
#define FT_ERROR_START_LIST switch (FT_ERROR_BASE(err)) {
|
||||||
|
#define FT_ERRORDEF(e, v, s) case v: return s;
|
||||||
|
#define FT_ERROR_END_LIST }
|
||||||
|
#include FT_ERRORS_H
|
||||||
|
return "unknown error";
|
||||||
|
}
|
||||||
|
|
||||||
/************************* Fonts *************************/
|
/************************* Fonts *************************/
|
||||||
|
|
||||||
// approximate number of glyphs per atlas surface
|
// approximate number of glyphs per atlas surface
|
||||||
@@ -442,11 +452,14 @@ static int font_set_face_metrics(RenFont *font, FT_Face face) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
RenFont* ren_font_load(RenWindow *window_renderer, const char* path, float size, ERenFontAntialiasing antialiasing, ERenFontHinting hinting, unsigned char style) {
|
RenFont* ren_font_load(RenWindow *window_renderer, const char* path, float size, ERenFontAntialiasing antialiasing, ERenFontHinting hinting, unsigned char style) {
|
||||||
|
FT_Error err = FT_Err_Ok;
|
||||||
SDL_RWops *file = NULL; RenFont *font = NULL;
|
SDL_RWops *file = NULL; RenFont *font = NULL;
|
||||||
FT_Face face = NULL; FT_Stream stream = NULL;
|
FT_Face face = NULL; FT_Stream stream = NULL;
|
||||||
|
|
||||||
|
SDL_ClearError();
|
||||||
|
|
||||||
file = SDL_RWFromFile(path, "rb");
|
file = SDL_RWFromFile(path, "rb");
|
||||||
if (!file) return NULL;
|
if (!file) return NULL; // error set by SDL_RWFromFile
|
||||||
|
|
||||||
int len = strlen(path);
|
int len = strlen(path);
|
||||||
font = check_alloc(calloc(1, sizeof(RenFont) + len + 1));
|
font = check_alloc(calloc(1, sizeof(RenFont) + len + 1));
|
||||||
@@ -468,15 +481,16 @@ RenFont* ren_font_load(RenWindow *window_renderer, const char* path, float size,
|
|||||||
stream->pos = 0;
|
stream->pos = 0;
|
||||||
stream->size = (unsigned long) SDL_RWsize(file);
|
stream->size = (unsigned long) SDL_RWsize(file);
|
||||||
|
|
||||||
if (FT_Open_Face(library, &(FT_Open_Args) { .flags = FT_OPEN_STREAM, .stream = stream }, 0, &face) != 0)
|
if ((err = FT_Open_Face(library, &(FT_Open_Args) { .flags = FT_OPEN_STREAM, .stream = stream }, 0, &face)) != 0)
|
||||||
goto failure;
|
goto failure;
|
||||||
if (font_set_face_metrics(font, face) != 0)
|
if ((err = font_set_face_metrics(font, face)) != 0)
|
||||||
goto failure;
|
goto failure;
|
||||||
return font;
|
return font;
|
||||||
|
|
||||||
stream_failure:
|
stream_failure:
|
||||||
if (file) SDL_RWclose(file);
|
if (file) SDL_RWclose(file);
|
||||||
failure:
|
failure:
|
||||||
|
if (err != FT_Err_Ok) SDL_SetError("%s", get_ft_error(err));
|
||||||
if (face) FT_Done_Face(face);
|
if (face) FT_Done_Face(face);
|
||||||
if (font) free(font);
|
if (font) free(font);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -487,7 +501,7 @@ RenFont* ren_font_copy(RenWindow *window_renderer, RenFont* font, float size, ER
|
|||||||
hinting = hinting == -1 ? font->hinting : hinting;
|
hinting = hinting == -1 ? font->hinting : hinting;
|
||||||
style = style == -1 ? font->style : style;
|
style = style == -1 ? font->style : style;
|
||||||
|
|
||||||
return ren_font_load(window_renderer, font->path, size, antialiasing, hinting, style);
|
return ren_font_load(window_renderer, font->path, size, antialiasing, hinting, style); // SDL_SetError() will be called appropriately
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* ren_font_get_path(RenFont *font) {
|
const char* ren_font_get_path(RenFont *font) {
|
||||||
@@ -715,19 +729,24 @@ void ren_free_window_resources(RenWindow *window_renderer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO remove global and return RenWindow*
|
// TODO remove global and return RenWindow*
|
||||||
void ren_init(SDL_Window *win) {
|
int ren_init(SDL_Window *win) {
|
||||||
|
FT_Error err;
|
||||||
|
SDL_ClearError();
|
||||||
assert(win);
|
assert(win);
|
||||||
int error = FT_Init_FreeType( &library );
|
|
||||||
if ( error ) {
|
draw_rect_surface = SDL_CreateRGBSurface(0, 1, 1, 32,
|
||||||
fprintf(stderr, "internal font error when starting the application\n");
|
0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
|
||||||
return;
|
if (!draw_rect_surface)
|
||||||
}
|
return -1; // error set by SDL_CreateRGBSurface
|
||||||
|
|
||||||
|
if ((err = FT_Init_FreeType(&library)) != 0)
|
||||||
|
return SDL_SetError("%s", get_ft_error(err));
|
||||||
|
|
||||||
window_renderer.window = win;
|
window_renderer.window = win;
|
||||||
renwin_init_surface(&window_renderer);
|
renwin_init_surface(&window_renderer);
|
||||||
renwin_init_command_buf(&window_renderer);
|
renwin_init_command_buf(&window_renderer);
|
||||||
renwin_clip_to_surface(&window_renderer);
|
renwin_clip_to_surface(&window_renderer);
|
||||||
draw_rect_surface = SDL_CreateRGBSurface(0, 1, 1, 32,
|
return 0;
|
||||||
0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -42,7 +42,7 @@ double ren_draw_text(RenSurface *rs, RenFont **font, const char *text, size_t le
|
|||||||
|
|
||||||
void ren_draw_rect(RenSurface *rs, RenRect rect, RenColor color);
|
void ren_draw_rect(RenSurface *rs, RenRect rect, RenColor color);
|
||||||
|
|
||||||
void ren_init(SDL_Window *win);
|
int ren_init(SDL_Window *win);
|
||||||
void ren_resize_window(RenWindow *window_renderer);
|
void ren_resize_window(RenWindow *window_renderer);
|
||||||
void ren_update_rects(RenWindow *window_renderer, RenRect *rects, int count);
|
void ren_update_rects(RenWindow *window_renderer, RenRect *rects, int count);
|
||||||
void ren_set_clip_rect(RenWindow *window_renderer, RenRect rect);
|
void ren_set_clip_rect(RenWindow *window_renderer, RenRect rect);
|
||||||
|
|||||||
Reference in New Issue
Block a user