Implemented Unicode text rendering... I think?

I'm in a bit of a rush so I can't check yet (also it's deactivated
for now). But I think I'm finished? Will check later.
This commit is contained in:
Julie Marchant 2019-06-02 11:28:26 -04:00
parent e17247aa99
commit 24f44e509f
12 changed files with 178 additions and 9 deletions

View File

@ -1,10 +1,30 @@
data/*
data/credits.txt
mac/*
License: GNU GPLv3 or later <https://gnu.org/licenses/gpl.html>
------------------------------------------------------------------------
data/DroidSansFallbackFull.ttf
Source: https://packages.debian.org/sid/fonts-droid-fallback
Copyright © 2006, 2007, 2008, 2009, 2010 Google Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
------------------------------------------------------------------------
gfx/alienDevice.png
gfx/prlogo.png
gfx/sflogo.png

Binary file not shown.

View File

@ -36,7 +36,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
typedef struct Message_ {
int face;
char message[255];
char message[STRMAX];
} Message;

View File

@ -57,6 +57,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define PATH_MAX 4096
#endif
#define STRMAX 2000
#define FULLSCREEN SDL_WINDOW_FULLSCREEN_DESKTOP
#define DEFAULT_SCREEN_WIDTH MAX(SCREEN_WIDTH, 640)

View File

@ -134,7 +134,7 @@ void engine_showError(int errorId, const char *name)
exit(1);
}
char string[255];
char string[STRMAX];
switch(errorId)
{

View File

@ -27,7 +27,7 @@ typedef struct Event_ {
int time;
int face;
char message[255];
char message[STRMAX];
int entity;
int flag;

147
src/gfx.c
View File

@ -17,12 +17,19 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Fonts not ready yet
#define NOFONT
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "SDL.h"
#include "SDL_image.h"
#ifndef NOFONT
#include "SDL_ttf.h"
#include "utf8proc.h"
#endif
#include "defs.h"
#include "structs.h"
@ -43,6 +50,10 @@ SDL_Surface *gfx_shopSprites[SHOP_S_MAX];
TextObject gfx_textSprites[TS_MAX];
SDL_Surface *gfx_messageBox;
#ifndef NOFONT
TTF_Font *gfx_unicodeFont;
#endif
void gfx_init()
{
screen_bufferHead = malloc(sizeof(*screen_bufferHead));
@ -72,6 +83,15 @@ void gfx_init()
gfx_messageBox = NULL;
screen = NULL;
#ifndef NOFONT
gfx_unicodeFont = TTF_OpenFont("data/DroidSansFallbackFull.ttf", 14);
if (gfx_unicodeFont == NULL)
{
printf("ERROR: TTF_OpenFont: %s\n", TTF_GetError());
exit(1);
}
#endif
}
SDL_Surface *gfx_setTransparent(SDL_Surface *sprite)
@ -197,6 +217,125 @@ int gfx_renderString(const char *in, int x, int y, int fontColor, int wrap, SDL_
return gfx_renderStringBase(in, x, y, fontColor, wrap, dest);
}
#ifdef NOFONT
int gfx_renderUnicode(const char *in, int x, int y, int fontColor, int wrap, SDL_Surface *dest)
{
return gfx_renderString(in, x, y, fontColor, wrap, dest);
}
#else
int gfx_renderUnicodeBase(const char *in, int x, int y, int fontColor, int wrap, SDL_Surface *dest)
{
SDL_Surface *textSurf;
SDL_Color color;
int w, h;
utf8proc_int32_t charList[STRMAX];
utf8proc_int32_t buf;
int nCharList;
int breakPoints[STRMAX];
int nBreakPoints;
char newStr[STRMAX];
char testStr[STRMAX];
int state;
int errorcode;
int i, j;
int offset;
color.r = fontColor & 0xff0000;
color.g = fontColor & 0x00ff00;
color.b = fontColor & 0x0000ff;
if (gfx_unicodeFont != NULL)
{
if (TTF_SizeUTF8(gfx_unicodeFont, in, &w, &h) < 0)
{
engine_error(TTF_GetError());
}
if (w > dest->w)
{
nCharList = 0;
i = 0;
while (i < STRMAX)
{
i += utf8proc_iterate(&in[i], -1, &buf);
if (buf < 0)
{
printf("WARNING: Unicode string \"%s\" contains invalid characters!", in);
break;
}
else
{
charList[nCharList] = buf;
nCharList++;
if (buf == '\0')
{
break;
}
}
}
state = 0;
nBreakPoints = 0;
for (i = 0; i < nCharList - 1; i++)
{
if (utf8proc_grapheme_break_stateful(charList[i], charList[i + 1], &state))
{
breakPoints[nBreakPoints] = i;
nBreakPoints++;
}
}
newStr = strcpy(in);
while (w > dest->w)
{
for (i = nBreakPoints - 1; i >= 0; i--)
{
testStr = "";
for (j = 0; j < nCharList - 1; j++)
{
utf8proc_encode_char(charList[j], &testStr[j + offset]);
if (j == breakPoints[i])
{
break;
}
}
if (TTF_SizeUTF8(gfx_unicodeFont, testStr, &w, &h) < 0)
{
engine_error(TTF_GetError());
}
if (w <= dest->w)
{
offset = 0;
for (j = 0; j < nCharList - 1; j++)
{
utf8proc_encode_char(charList[j], &newStr[j + offset]);
if (j == breakPoints[i])
{
offset++;
newStr[j + offset] = '\n';
}
}
break;
}
}
if (TTF_SizeUTF8(gfx_unicodeFont, newStr, &w, &h) < 0)
{
engine_error(TTF_GetError());
}
}
}
textSurf = TTF_RenderUTF8_Solid(gfx_unicodeFont, in, color);
}
}
int gfx_renderUnicode(const char *in, int x, int y, int fontColor, int wrap, SDL_Surface *dest)
{
gfx_renderString(const char *in, int x, int y, int fontColor, int wrap, SDL_Surface *dest);
}
#endif
/*
* Set the pixel at (x, y) to the given value
* NOTE: The surface must be locked before calling this!
@ -491,6 +630,14 @@ void gfx_free()
SDL_FreeSurface(gfx_messageBox);
gfx_messageBox = NULL;
}
#ifndef NOFONT
if (gfx_unicodeFont != NULL)
{
TTF_CloseFont(gfx_unicodeFont);
gfx_unicodeFont = NULL;
}
#endif
}
void gfx_scaleBackground()

View File

@ -794,7 +794,7 @@ Missions 11 and 23 to be exact!
static int mission_revealObjectives()
{
int allDone = 1;
char string[255] = "";
char string[STRMAX] = "";
for (int i = 0 ; i < 3 ; i++)
{

View File

@ -47,7 +47,7 @@ int save_initSlots()
{
char fileName[PATH_MAX];
int system;
char stationedName[255];
char stationedName[STRMAX];
int imagePos = 0;
Game tempGame;
struct stat fileInfo;

View File

@ -39,7 +39,7 @@ typedef struct ShopItem_ {
int x, y;
int price;
char name[50];
char description[255];
char description[STRMAX];
int image;
} ShopItem;

View File

@ -81,7 +81,7 @@ typedef struct TextObject_ {
int life;
float x, y;
int fontColor;
char text[255];
char text[STRMAX];
} TextObject;

View File

@ -594,7 +594,7 @@ void title_showCredits()
int yPos = 0;
int yPos2 = screen->h;
char text[255];
char text[STRMAX];
int i;
TextObject *credit;