5 #include <SDL_opengl.h>
17 unsigned int allocated
;
19 unsigned int last_drawn
;
22 TextQueue gtext_queue
;
23 TextQueue gtext_queue_sorted
;
24 void FlushTextQueue();
25 void DrawTextQueueItem(TextQueueItem
*item
);
29 void FlushTextQueue(TextQueue
*queue
) {
31 for (i
= 0; i
< queue
->allocated
; i
++) {
32 free (queue
->items
[i
].text
);
33 queue
->items
[i
].text
= 0;
36 queue
->last_drawn
= 0;
39 void SwapTextQueueItems(TextQueue
*queue
, int aid
, int bid
){
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
){
48 while (wanted_size
> queue
->allocated
){
50 newsize
= queue
->allocated
* 2 + 1;
51 item
= (TextQueueItem
*) realloc(queue
->items
, sizeof(TextQueueItem
)*newsize
);
53 fprintf(stderr
, "Can't allocate enough memory for text queue\n");
56 memset (&item
[queue
->allocated
], 0, sizeof(TextQueueItem
) * (newsize
- queue
->allocated
));
57 queue
->allocated
= newsize
;
64 ftglDestroyFont(ftfont
);
69 int RegenerateFonts() {
71 ftfont
= ftglCreatePixmapFont(PATH_GRAPHICS
"DejaVuSansMono.ttf");
72 ftglSetFontFaceSize(ftfont
, G2SY(0.5), G2SY(1.0));
77 int DrawTextsOnLayer(int z
){
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
)
82 DrawTextQueueItem (>ext_queue_sorted
.items
[i
]);
84 gtext_queue_sorted
.last_drawn
= i
- 1;
88 int DrawRestOfTexts(){
90 for (i
= gtext_queue_sorted
.last_drawn
+ 1; i
< gtext_queue_sorted
.next_id
; i
++) {
91 DrawTextQueueItem (>ext_queue_sorted
.items
[i
]);
93 gtext_queue_sorted
.last_drawn
= i
- 1;
94 FlushTextQueue(>ext_queue
);
95 FlushTextQueue(>ext_queue_sorted
);
101 memset (>ext_queue
, 0, sizeof(TextQueue
));
102 memset (>ext_queue_sorted
, 0, sizeof(TextQueue
));
107 FlushTextQueue(>ext_queue
);
108 FlushTextQueue(>ext_queue_sorted
);
112 void QueueDrawTextColorize(char *text
, double x
, double y
, int z
, SDL_Color color
){
116 printf("NULL text*\n");
119 GrowTextQueueIfNeeded(>ext_queue
, gtext_queue
.next_id
+1);
120 buf
= calloc (strlen (text
) + 1, sizeof(char));
123 item
= >ext_queue
.items
[gtext_queue
.next_id
];
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
){
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
);
173 TextQueueCpy (>ext_queue_sorted
, >ext_queue
);
174 ZSortTextsQueue (>ext_queue_sorted
);