WID comments
[Lilanci.git] / font.c
blob8634d5eae6ce7bc6f486cdd053b9a93d171d0599
1 #include "font.h"
2 #include "path.h"
3 #include "gr.h"
4 #include <FTGL/ftgl.h>
5 #include <SDL_opengl.h>
7 typedef struct{
8 char *text;
9 double x;
10 double y;
11 int z; //z
12 SDL_Color c;
13 }TextQueueItem;
15 typedef struct{
16 TextQueueItem *items;
17 unsigned int allocated;
18 unsigned int next_id;
19 unsigned int last_drawn;
20 }TextQueue;
22 TextQueue gtext_queue;
23 TextQueue gtext_queue_sorted;
24 void FlushTextQueue();
25 void DrawTextQueueItem(TextQueueItem *item);
27 FTGLfont *ftfont=0;
29 void FlushTextQueue(TextQueue *queue) {
30 int i;
31 for (i = 0; i < queue->allocated; i++) {
32 free (queue->items[i].text);
33 queue->items[i].text = 0;
35 queue->next_id = 0;
36 queue->last_drawn = 0;
39 void SwapTextQueueItems(TextQueue *queue, int aid, int bid){
40 TextQueueItem c;
41 memcpy(&c, &queue->items[aid], sizeof(TextQueueItem));
42 memcpy(&queue->items[aid], &queue->items[bid], sizeof(TextQueueItem));
43 memcpy(&queue->items[bid], &c, sizeof(TextQueueItem));
46 void GrowTextQueueIfNeeded(TextQueue *queue, int wanted_size){
47 TextQueueItem *item;
48 while (wanted_size > queue->allocated){
49 size_t newsize=0;
50 newsize = queue->allocated * 2 + 1;
51 item = (TextQueueItem *) realloc(queue->items, sizeof(TextQueueItem)*newsize);
52 if(item == 0){
53 fprintf(stderr, "Can't allocate enough memory for text queue\n");
54 return;
56 memset (&item[queue->allocated], 0, sizeof(TextQueueItem) * (newsize - queue->allocated));
57 queue->allocated = newsize;
58 queue->items = item;
62 void DeleteFonts() {
63 if (ftfont!=0) {
64 ftglDestroyFont(ftfont);
65 ftfont = 0;
69 int RegenerateFonts() {
70 DeleteFonts();
71 ftfont = ftglCreatePixmapFont(PATH_GRAPHICS "DejaVuSansMono.ttf");
72 ftglSetFontFaceSize(ftfont, G2SY(0.5), G2SY(1.0));
74 return 0;
77 int DrawTextsOnLayer(int z){
78 int i;
79 for (i = gtext_queue_sorted.last_drawn + 1; i < gtext_queue_sorted.next_id; i++) {
80 if (gtext_queue_sorted.items[i].z > z)
81 break;
82 DrawTextQueueItem (&gtext_queue_sorted.items[i]);
84 gtext_queue_sorted.last_drawn = i - 1;
85 return 0;
88 int DrawRestOfTexts(){
89 int i;
90 for (i = gtext_queue_sorted.last_drawn + 1; i < gtext_queue_sorted.next_id; i++) {
91 DrawTextQueueItem (&gtext_queue_sorted.items[i]);
93 gtext_queue_sorted.last_drawn = i - 1;
94 FlushTextQueue(&gtext_queue);
95 FlushTextQueue(&gtext_queue_sorted);
96 return 0;
99 int FontInit() {
100 ftfont = 0;
101 memset (&gtext_queue, 0, sizeof(TextQueue));
102 memset (&gtext_queue_sorted, 0, sizeof(TextQueue));
103 return 0;
105 int FontKill() {
106 DeleteFonts();
107 FlushTextQueue(&gtext_queue);
108 FlushTextQueue(&gtext_queue_sorted);
109 return 0;
112 void QueueDrawTextColorize(char *text, double x, double y, int z, SDL_Color color){
113 TextQueueItem *item;
114 char *buf;
115 if(!text){
116 printf("NULL text*\n");
117 return;
119 GrowTextQueueIfNeeded(&gtext_queue, gtext_queue.next_id+1);
120 buf = calloc (strlen (text) + 1, sizeof(char));
121 strcpy (buf, text);
123 item = &gtext_queue.items[gtext_queue.next_id];
124 item->text = buf;
125 item->x = x;
126 item->y = y;
127 item->z = z;
128 item->c = color;
130 gtext_queue.next_id ++;
133 void QueueDrawText(char *text, double x, double y, int z){
134 const SDL_Color white = {255, 255, 255, 255};
135 QueueDrawTextColorize(text, x, y, z, white);
138 void TextQueueCpy (TextQueue *dest, TextQueue *source){
139 int i;
140 char *buf;
141 FlushTextQueue (dest);
142 GrowTextQueueIfNeeded (dest, source->next_id);
143 memcpy (dest->items, source->items, sizeof (TextQueueItem) * source->next_id);
144 dest->last_drawn = -1;
145 dest->next_id = source->next_id;
146 for (i = 0; i < dest->next_id; i ++)
147 if (dest->items[i].text != 0) {
148 buf = calloc (strlen (dest->items[i].text) + 1, sizeof(char));
149 strcpy (buf, dest->items[i].text);
150 dest->items[i].text = buf;
153 static int ZCmpTQI(const void *p1, const void *p2)
155 return ((const TextQueueItem *)p1)->z - ((const TextQueueItem *)p2)->z;
158 void ZSortTextsQueue(TextQueue *queue){
159 qsort ((void *)queue->items, queue->next_id, sizeof(TextQueueItem), ZCmpTQI);
162 void DrawTextQueueItem(TextQueueItem *item){
163 glColor4ub(item->c.r, item->c.g, item->c.b, item->c.unused);
164 glRasterPos2f(G2SX(item->x), G2SY(item->y));
165 ftglRenderFont(ftfont, item->text, FTGL_RENDER_ALL);
167 void DrawText(char *text, double x, double y){
168 glRasterPos2f(G2SX(x), G2SY(y));
169 ftglRenderFont(ftfont, "H", FTGL_RENDER_ALL);
172 void PreDrawTexts(){
173 TextQueueCpy (&gtext_queue_sorted, &gtext_queue);
174 ZSortTextsQueue (&gtext_queue_sorted);