-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphics.c
executable file
·107 lines (88 loc) · 3.56 KB
/
graphics.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "glknew.h"
#include <arpa/inet.h>
#include <stdlib.h>
void glk_window_fill_rect(winid_t win, glui32 color, glsi32 left, glsi32 top, glui32 width, glui32 height) {
printf(">>>glk_window_fill_rect win=%p, color=0x%06x, left=%d, top=%d, width=%d, height=%d\n",
win, color, left, top, width, height);
}
glui32 glk_image_get_info(glui32 image, glui32 *width, glui32 *height) {
giblorb_err_t err;
giblorb_result_t res;
err = giblorb_load_resource(giblorb_get_resource_map(), giblorb_method_Memory, &res, giblorb_ID_Pict, image);
if (err) {
printf("DEBUG: Couldn't get pict id %d from blorb, err = %d\n", image, err);
return FALSE;
}
if (res.chunktype == 0x504e4720) {
/* FIXME: Is that constant portable? Would 'PNG ' be more portable? */
/* Quietly assumes that the PNG is valid. */
/* 0 string \x89PNG PNG image data,
>4 belong !0x0d0a1a0a CORRUPTED,
>4 belong 0x0d0a1a0a
>>16 belong x %ld x
>>20 belong x %ld,
*/
*width = ntohl(*(glui32*)((char*)res.data.ptr+16));
*height = ntohl(*(glui32*)((char*)res.data.ptr+20));
printf("DEBUG: found image size of %d x %d\n", *width, *height);
return TRUE;
} else {
printf("Don't know how to handle chunktype=0x%x in image_get_info\n", res.chunktype);
exit(30);
}
}
glui32 glk_image_draw(winid_t win, glui32 image, glsi32 val1, glsi32 val2) {
/* FIXME: Let the system tell us what dir to use? */
char filename[] = "/tmp/glknew-image-XXXXXX";
int fd = mkstemp(filename);
int ret;
giblorb_err_t err;
giblorb_result_t res;
err = giblorb_load_resource(giblorb_get_resource_map(), giblorb_method_Memory, &res, giblorb_ID_Pict, image);
if (err) {
printf("DEBUG: Couldn't get pict id %d from blorb, err = %d\n", image, err);
return FALSE;
}
ret = write(fd, res.data.ptr, res.length);
if (ret != res.length) {
printf("Failed to write to temp file (%s) for glk_image_draw, wrote %d bytes of %d, errno=%d\n",
filename, ret, res.length, errno);
exit(31);
}
if (win->wintype == wintype_Graphics) {
printf(">>>image_draw win=%p, filename=%s, x=%d, y=%d\n",
win, filename, val1, val2);
return TRUE;
} else {
printf("DEBUG: Attempt to image_draw to something other then a Graphics window\n");
return FALSE;
}
}
/* FIXME: Merge core code with glk_image_draw, which is like this but not scaled. */
glui32 glk_image_draw_scaled(winid_t win, glui32 image, glsi32 val1, glsi32 val2, glui32 width, glui32 height) {
/* FIXME: Let the system tell us what dir to use? */
char filename[] = "/tmp/glknew-image-XXXXXX";
int fd = mkstemp(filename);
int ret;
giblorb_err_t err;
giblorb_result_t res;
err = giblorb_load_resource(giblorb_get_resource_map(), giblorb_method_Memory, &res, giblorb_ID_Pict, image);
if (err) {
printf("DEBUG: Couldn't get pict id %d from blorb, err = %d\n", image, err);
return FALSE;
}
ret = write(fd, res.data.ptr, res.length);
if (ret != res.length) {
printf("Failed to write to temp file (%s) for glk_image_draw_scaled, wrote %d bytes of %d, errno=%d\n",
filename, ret, res.length, errno);
exit(31);
}
if (win->wintype == wintype_Graphics) {
printf(">>>image_draw_scaled win=%p, filename=%s, x=%d, y=%d, width=%d, height=%d\n",
win, filename, val1, val2, width, height);
return TRUE;
} else {
printf("DEBUG: Attempt to image_draw_scaled to something other then a Graphics window\n");
return FALSE;
}
}