1 /**
2  * Display text using a font from a .zip happened to the executable
3 
4  * Copyright (C) 2007  Sylvain Beucler
5 
6  * This file is part of GNU FreeDink
7 
8  * GNU FreeDink is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 3 of the
11  * License, or (at your option) any later version.
12 
13  * GNU FreeDink is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17 
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see
20  * <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <unistd.h>
24 #include "SDL.h"
25 #include "SDL_ttf.h"
26 #include "SDL_rwops_libzip.h"
27 
main(int argc,char * argv[])28 int main (int argc, char *argv[])
29 {
30   SDL_RWops* rwops;
31   SDL_Surface *screen;
32   TTF_Font *font;
33 
34   /* Init */
35   SDL_Init(SDL_INIT_VIDEO);
36   TTF_Init();
37   screen = SDL_SetVideoMode(640, 480, 0, SDL_SWSURFACE);
38 
39 #ifndef _WIN32
40   /* Find myself */
41   {
42     char buf[1024];
43 
44     printf("argv[0] = %s\n", argv[0]);
45 
46     /* Try to see where /proc/self/exe points. It doesn't exist with
47        upx-linux/elf386 (executed from RAM) */
48     int n = readlink("/proc/self/exe", buf, 1024-1);
49     if (n < 0)
50       perror("readlink(/proc/self/exe)");
51     else
52       {
53 	buf[n] = '\0';
54 	printf("buf = %s\n", buf);
55       }
56 
57     /* Try /proc/self/maps just in case */
58     FILE *f = fopen ("/proc/self/maps", "r");
59     if (f == NULL)
60       perror("fopen(/proc/self/maps)");
61     else
62       {
63 	/* The first entry should contain the executable name. */
64 	char *result = fgets (buf, 1024, f);
65 	if (result == NULL) {
66 	  perror("fgets(/proc/self/maps)");
67 	}
68 	printf("maps = %s\n", buf);
69 	fclose (f);
70       }
71   }
72 #endif
73 
74   /* Load embedded font */
75   {
76     char myself[1024]; /* bad! */
77     strcpy(myself, argv[0]);
78     rwops = SDL_RWFromZIP(myself, "LiberationSans-Regular.ttf");
79     /* rwops = SDL_RWFromZZIP("embedded_font/LiberationSans-Regular.ttf", "rb"); */
80   }
81 
82   if (!rwops)
83     {
84       perror("SDL_RWFromZZIP");
85       exit(1);
86     }
87 
88   /* Display test text */
89   {
90     SDL_Surface *tmp;
91     SDL_Color grey = {127, 127, 127};
92     SDL_Rect dst = {280, 220, -1, -1};
93     SDL_Event ev;
94     font = TTF_OpenFontRW(rwops, 1, 16);
95     if (font == NULL)
96       {
97 	fprintf(stderr, "TTF_OpenFontRW: %s\n", TTF_GetError());
98 	exit(1);
99       }
100     TTF_SetFontStyle(font, TTF_STYLE_BOLD);
101     tmp = TTF_RenderText_Solid(font, "Hello, world!", grey);
102     if (tmp == NULL)
103       fprintf(stderr, "TTF_RenderText_Solid: %s\n", TTF_GetError());
104     else
105       SDL_BlitSurface(tmp, NULL, screen, &dst);
106     SDL_Flip(screen);
107     while (SDL_WaitEvent(&ev))
108       {
109 	if (ev.type == SDL_KEYDOWN)
110 	  break;
111       }
112   }
113 
114   /* Clean-up */
115   TTF_CloseFont(font);
116   SDL_QuitSubSystem(SDL_INIT_VIDEO);
117 
118   return 0;
119 }
120