Skip to content

Commit

Permalink
Update api to use SDL_bool instead of an int return code
Browse files Browse the repository at this point in the history
also update against the api changes in SDL_ttf.
  • Loading branch information
sezero committed Aug 28, 2024
1 parent 92cd9fc commit 0077d43
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 104 deletions.
4 changes: 2 additions & 2 deletions cmake/test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ int main(int argc, char *argv[])
SDL_Log("SDL_Init: could not initialize SDL: %s", SDL_GetError());
return 1;
}
if (TTF_Init() == -1) {
SDL_Log("TTF_Init: %s", TTF_GetError());
if (!TTF_Init()) {
SDL_Log("TTF_Init: %s", SDL_GetError());
}
version = RTF_Version();
SDL_Log("SDL_rtf linked version: %u.%u.%u",
Expand Down
84 changes: 46 additions & 38 deletions examples/showrtf.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static const char *FontList[8];
/* Note, this is only one way of looking up fonts */
static int FontFamilyToIndex(RTF_FontFamily family)
{
switch(family) {
switch (family) {
case RTF_FontDefault:
return 0;
case RTF_FontRoman:
Expand Down Expand Up @@ -61,16 +61,16 @@ static Uint32 UTF8_to_UNICODE(const char *utf8, int *advance)
Uint32 ch;

ch = ((const unsigned char *)utf8)[i];
if ( ch >= 0xF0 ) {
if (ch >= 0xF0) {
ch = (Uint16)((utf8[i]&0x07) << 18);
ch |= (Uint16)(utf8[++i]&0x3F) << 12;
ch |= (Uint16)(utf8[++i]&0x3F) << 6;
ch |= (Uint16)(utf8[++i]&0x3F);
} else if ( ch >= 0xE0 ) {
} else if (ch >= 0xE0) {
ch = (Uint16)(utf8[i]&0x3F) << 12;
ch |= (Uint16)(utf8[++i]&0x3F) << 6;
ch |= (Uint16)(utf8[++i]&0x3F);
} else if ( ch >= 0xC0 ) {
} else if (ch >= 0xC0) {
ch = (Uint16)(utf8[i]&0x3F) << 6;
ch |= (Uint16)(utf8[++i]&0x3F);
}
Expand All @@ -87,18 +87,22 @@ static void * SDLCALL CreateFont(const char *name, RTF_FontFamily family, int ch
(void)charset;

index = FontFamilyToIndex(family);
if (!FontList[index])
if (!FontList[index]) {
index = 0;
}

font = TTF_OpenFont(FontList[index], size);
if (font) {
int TTF_style = TTF_STYLE_NORMAL;
if ( style & RTF_FontBold )
if (style & RTF_FontBold) {
TTF_style |= TTF_STYLE_BOLD;
if ( style & RTF_FontItalic )
}
if (style & RTF_FontItalic) {
TTF_style |= TTF_STYLE_ITALIC;
if ( style & RTF_FontUnderline )
}
if (style & RTF_FontUnderline) {
TTF_style |= TTF_STYLE_UNDERLINE;
}
TTF_SetFontStyle(font, TTF_style);
}

Expand All @@ -121,7 +125,7 @@ static int SDLCALL GetCharacterOffsets(void *_font, const char *text, int *byteO
int pixels = 0;
int advance;
Uint16 ch;
while ( *text && i < maxOffsets ) {
while (*text && i < maxOffsets) {
byteOffsets[i] = bytes;
pixelOffsets[i] = pixels;
++i;
Expand All @@ -132,7 +136,7 @@ static int SDLCALL GetCharacterOffsets(void *_font, const char *text, int *byteO
TTF_GlyphMetrics(font, ch, NULL, NULL, NULL, NULL, &advance);
pixels += advance;
}
if ( i < maxOffsets ) {
if (i < maxOffsets) {
byteOffsets[i] = bytes;
pixelOffsets[i] = pixels;
}
Expand All @@ -159,8 +163,8 @@ static void SDLCALL FreeFont(void *_font)

static void LoadRTF(RTF_Context *ctx, const char *file)
{
if ( RTF_Load(ctx, file) < 0 ) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", file, RTF_GetError());
if (!RTF_Load(ctx, file)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", file, SDL_GetError());
return;
}
}
Expand Down Expand Up @@ -190,43 +194,43 @@ int main(int argc, char *argv[])
const Uint8 *keystate;

/* Parse command line arguments */
for ( i = 1; i < argc; ++i ) {
if ( SDL_strcmp(argv[i], "-fdefault") == 0 ) {
for (i = 1; i < argc; ++i) {
if (SDL_strcmp(argv[i], "-fdefault") == 0) {
FontList[FontFamilyToIndex(RTF_FontDefault)] = argv[++i];
} else if ( SDL_strcmp(argv[i], "-froman") == 0 ) {
} else if (SDL_strcmp(argv[i], "-froman") == 0) {
FontList[FontFamilyToIndex(RTF_FontRoman)] = argv[++i];
} else if ( SDL_strcmp(argv[i], "-fswiss") == 0 ) {
} else if (SDL_strcmp(argv[i], "-fswiss") == 0) {
FontList[FontFamilyToIndex(RTF_FontSwiss)] = argv[++i];
} else if ( SDL_strcmp(argv[i], "-fmodern") == 0 ) {
} else if (SDL_strcmp(argv[i], "-fmodern") == 0) {
FontList[FontFamilyToIndex(RTF_FontModern)] = argv[++i];
} else if ( SDL_strcmp(argv[i], "-fscript") == 0 ) {
} else if (SDL_strcmp(argv[i], "-fscript") == 0) {
FontList[FontFamilyToIndex(RTF_FontScript)] = argv[++i];
} else if ( SDL_strcmp(argv[i], "-fdecor") == 0 ) {
} else if (SDL_strcmp(argv[i], "-fdecor") == 0) {
FontList[FontFamilyToIndex(RTF_FontDecor)] = argv[++i];
} else if ( SDL_strcmp(argv[i], "-ftech") == 0 ) {
} else if (SDL_strcmp(argv[i], "-ftech") == 0) {
FontList[FontFamilyToIndex(RTF_FontTech)] = argv[++i];
} else {
break;
}
}
start = i;
stop = (argc-1);
if ( !FontList[0] || (start > stop) ) {
stop = (argc - 1);
if (!FontList[0] || (start > stop)) {
PrintUsage(argv[0]);
return(1);
return 1;
}

/* Initialize the TTF library */
if ( TTF_Init() < 0 ) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize TTF: %s\n",SDL_GetError());
if (!TTF_Init()) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize TTF: %s\n", SDL_GetError());
SDL_Quit();
return(3);
return 3;
}

if (!SDL_CreateWindowAndRenderer("showrtf demo", SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateWindowAndRenderer() failed: %s\n", SDL_GetError());
cleanup();
return(4);
return 4;
}

/* Create and load the RTF document */
Expand All @@ -237,10 +241,10 @@ int main(int argc, char *argv[])
fontEngine.RenderText = RenderText;
fontEngine.FreeFont = FreeFont;
ctx = RTF_CreateContext(renderer, &fontEngine);
if ( ctx == NULL ) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create RTF context: %s\n", RTF_GetError());
if (!ctx) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create RTF context: %s\n", SDL_GetError());
cleanup();
return(5);
return 5;
}
LoadRTF(ctx, argv[i]);
SDL_SetWindowTitle(window, RTF_GetTitle(ctx));
Expand All @@ -267,15 +271,15 @@ int main(int argc, char *argv[])
done = 1;
break;
case SDLK_LEFT:
if ( i > start ) {
if (i > start) {
--i;
LoadRTF(ctx, argv[i]);
offset = 0;
height = RTF_GetHeight(ctx, w);
}
break;
case SDLK_RIGHT:
if ( i < stop ) {
if (i < stop) {
++i;
LoadRTF(ctx, argv[i]);
offset = 0;
Expand All @@ -290,14 +294,16 @@ int main(int argc, char *argv[])
break;
case SDLK_PAGEUP:
offset -= h;
if ( offset < 0 )
if (offset < 0) {
offset = 0;
}
break;
case SDLK_PAGEDOWN:
case SDLK_SPACE:
offset += h;
if ( offset > (height - h) )
if (offset > (height - h)) {
offset = (height - h);
}
break;
default:
break;
Expand All @@ -308,15 +314,17 @@ int main(int argc, char *argv[])
}
}
keystate = SDL_GetKeyboardState(NULL);
if ( keystate[SDL_SCANCODE_UP] ) {
if (keystate[SDL_SCANCODE_UP]) {
offset -= 1;
if ( offset < 0 )
if (offset < 0) {
offset = 0;
}
}
if ( keystate[SDL_SCANCODE_DOWN] ) {
if (keystate[SDL_SCANCODE_DOWN]) {
offset += 1;
if ( offset > (height - h) )
if (offset > (height - h)) {
offset = (height - h);
}
}

SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
Expand Down
19 changes: 9 additions & 10 deletions include/SDL3_rtf/SDL_rtf.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,31 +147,34 @@ extern SDL_DECLSPEC RTF_Context * SDLCALL RTF_CreateContext(SDL_Renderer *render
*
* \param ctx the RTF context to update.
* \param file the file path to load RTF data from.
* \returns 0 on success, -1 on failure.
* \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
* for more information.
*
* \since This function is available since SDL_rtf 3.0.0.
*/
extern SDL_DECLSPEC int SDLCALL RTF_Load(RTF_Context *ctx, const char *file);
extern SDL_DECLSPEC SDL_bool SDLCALL RTF_Load(RTF_Context *ctx, const char *file);

/**
* Set the text of an RTF context, with data loaded from an SDL_IOStream.
*
* This can be called multiple times to change the text displayed.
*
* If `closeio` is non-zero, this function will close `src`, whether this
* If `closeio` is SDL_TRUE, this function will close `src`, whether this
* function succeeded or not.
*
* On failure, call RTF_GetError() to get a human-readable text message
* corresponding to the error.
*
* \param ctx the RTF context to update.
* \param src the SDL_IOStream to load RTF data from.
* \param closeio non-zero to close/free `src`, zero to leave open.
* \returns 0 on success, -1 on failure.
* \param closeio SDL_TRUE to close `src` when the font is closed, SDL_FALSE
* to leave it open.
* \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
* for more information.
*
* \since This function is available since SDL_rtf 3.0.0.
*/
extern SDL_DECLSPEC int SDLCALL RTF_Load_IO(RTF_Context *ctx, SDL_IOStream *src, int closeio);
extern SDL_DECLSPEC SDL_bool SDLCALL RTF_Load_IO(RTF_Context *ctx, SDL_IOStream *src, SDL_bool closeio);

/**
* Get the title of an RTF document.
Expand Down Expand Up @@ -261,10 +264,6 @@ extern SDL_DECLSPEC void SDLCALL RTF_Render(RTF_Context *ctx, SDL_Rect *rect, in
*/
extern SDL_DECLSPEC void SDLCALL RTF_FreeContext(RTF_Context *ctx);

/* We'll use SDL for reporting errors */
#define RTF_SetError SDL_SetError
#define RTF_GetError SDL_GetError

/* Ends C function definitions when using C++ */
#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit 0077d43

Please sign in to comment.