summaryrefslogtreecommitdiff
path: root/test/pencil_test.c
blob: 04522ee42f6011cbcf47bc5dc2e368ff3dbabbda (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * This file is part of Pencil
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license
 * Copyright 2005 James Bursa <james@semichrome.net>
 */

#include <assert.h>
#include <stdio.h>
#include <oslib/osfile.h>
#include <oslib/osspriteop.h>
#include <rufl.h>
#include "pencil.h"


#define SPRITE "Resources:$.Resources.Desktop.Sprites"


static void test_pencil(void);


int main(int argc, char *argv[])
{
	rufl_code code;

	(void) argc;
	(void) argv;

	code = rufl_init();
	if (code != rufl_OK) {
		printf("rufl_init failed: %i\n", code);
		return 1;
	}

	test_pencil();

	rufl_quit();

	return 0;
}


void test_pencil(void)
{
	struct pencil_diagram *diagram;
	pencil_code code;
	int path[] = {2, 100, 40, 8, 100, 400, 8, 300, 300, 0};
	char utf8_test[] = "Hello,	world! ὕαλον "
			"Uherské Hradiště. 𐀀";
	char *drawfile_buffer;
	size_t drawfile_size;
	os_error *error;
	fileswitch_object_type obj_type;
	int size;
	osspriteop_area *area;

	diagram = pencil_create();
	if (!diagram) {
		printf("pencil_create failed\n");
		return;
	}

	code = pencil_text(diagram,
			100, 40,
			"Homerton", rufl_WEIGHT_400,
			320,
			"Hello, world!", 13,
			0x000000);
	if (code != pencil_OK) {
		printf("pencil_text failed: %i\n", code);
		return;
	}

	code = pencil_path(diagram,
			path, sizeof path / sizeof path[0],
			0x00ff00, 0x0000ff,
			5, pencil_JOIN_ROUND,
			pencil_CAP_BUTT, pencil_CAP_TRIANGLE,
			15, 20,
			false, pencil_SOLID);
	if (code != pencil_OK) {
		printf("pencil_path failed: %i\n", code);
		return;
	}

	code = pencil_text(diagram,
			100, 400,
			"NewHall", rufl_WEIGHT_400,
			320,
			utf8_test, sizeof utf8_test,
			0xff0000);
	if (code != pencil_OK) {
		printf("pencil_text failed: %i\n", code);
		return;
	}

	error = xosfile_read_no_path(SPRITE, &obj_type, 0, 0, &size, 0);
	if (error) {
		printf("xosfile_read_no_path failed: 0x%x: %s\n",
				error->errnum, error->errmess);
		return;
	}
	if (obj_type != fileswitch_IS_FILE) {
		printf("File " SPRITE " does not exist\n");
		return;
	}

	area = malloc(size + 4);
	if (!area) {
		printf("Out of memory\n");
		return;
	}
	area->size = size + 4;
	area->sprite_count = 0;
	area->first = 0;
	area->used = 16;

	error = xosspriteop_load_sprite_file(osspriteop_USER_AREA,
			area, SPRITE);
	if (error) {
		printf("xosspriteop_load_sprite_file failed: 0x%x: %s\n",
				error->errnum, error->errmess);
		return;
	}

	code = pencil_sprite(diagram, 400, 200, 200, 100,
			((char *) area) + area->first);
	if (code != pencil_OK) {
		printf("pencil_sprite failed: %i\n", code);
		return;
	}

	pencil_dump(diagram);

	code = pencil_save_drawfile(diagram, "Pencil-Test",
			&drawfile_buffer, &drawfile_size);
	if (code != pencil_OK) {
		printf("pencil_save_drawfile failed: %i\n", code);
		return;
	}
	assert(drawfile_buffer);

	error = xosfile_save_stamped("DrawFile", osfile_TYPE_DRAW,
			(byte *) drawfile_buffer, 
			(byte *) drawfile_buffer + drawfile_size);
	if (error) {
		printf("xosfile_save_stamped failed: 0x%x: %s\n",
				error->errnum, error->errmess);
		return;
	}

	pencil_free(diagram);
}