summaryrefslogtreecommitdiff
path: root/saedit/imageset.c
blob: 9ae2ea0fe951690c8a45bada47230afb115dc550 (plain) (blame)
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
#include "imageset.h"
#include <string.h>

struct _Imageset {
	gchar *name;
	gint width, height;
	gint offsetX, offsetY;

	GdkPixbuf *pixbuf;
};

Imageset *
imageset_new (
	const XMLNode *node,
	const gchar *cdf_filename
) {
	Imageset *imgset; 
	gchar    *src, *filename;
	GError   *err = NULL;
	gboolean  fail = FALSE;

	g_return_val_if_fail (g_strcmp0 (node->name, "imageset") == 0, NULL);

	imgset = (Imageset *) g_new0 (Imageset, 1);

	imgset->name = xml_node_get_attr_value (node, "name");
	if (imgset->name == NULL) {
		/* TODO: report error */
		fail = TRUE;
	}

	src = xml_node_get_attr_value (node, "src");
	if (src == NULL) {
		/* TODO: report error */
		fail = TRUE;
	} else {
		gchar *delim = strchr (src, '|');
		if (delim != NULL)
			*delim = 0;
		/* TODO: process palettes? */
	}

	imgset->width  = xml_node_get_int_attr_value (node,  "width", -1);
	imgset->height = xml_node_get_int_attr_value (node, "height", -1);
	if (imgset->width <= 0 || imgset->height <= 0) {
		/* TODO: report error */
		fail = TRUE;
	}

	imgset->offsetX = xml_node_get_int_attr_value (node, "offsetX", 0);
	imgset->offsetY = xml_node_get_int_attr_value (node, "offsetY", 0);

	filename = g_strconcat (cdf_filename, "/", src, NULL);
	imgset->pixbuf = gdk_pixbuf_new_from_file (filename, &err);

	if (imgset->pixbuf == NULL) {
		/* TODO handle err and report error */
		fail = TRUE;
	}

	if (fail) {
		imageset_free (imgset);
		return NULL;
	}

	return imgset;
}

gint
imageset_compare_by_name (
	const Imageset *first,
	const Imageset *second
) {
	return g_strcmp0 (first->name, second->name);
}

GdkPixbuf *
imageset_get_sprite_by_index (
	const Imageset *imageset,
	gint index
) {
	gint w = gdk_pixbuf_get_width (imageset->pixbuf);
	w /= imageset->width;

	g_return_val_if_fail (index >= 0, NULL);

	return gdk_pixbuf_new_subpixbuf (
		imageset->pixbuf,
		(index % w) * imageset->width,
		(index / w) * imageset->height,
		imageset->width,
		imageset->height
	);
}

void
imageset_free (Imageset *imgset) {
	g_free (imgset->name);
	if (imgset->pixbuf != NULL)
		g_object_unref (imgset->pixbuf);
	g_free (imgset);
}

gboolean
imageset_name_equals (
	const Imageset *imageset,
	const gchar *name
) {
	return g_strcmp0 (imageset->name, name) == 0;
}

void
imageset_get_offset (
	const Imageset *imageset,
	gint *offsetX,
	gint *offsetY
) {
	*offsetX = imageset->offsetX;
	*offsetY = imageset->offsetY;
}

void
imageset_get_size (
	const Imageset *imageset,
	gint *width,
	gint *height
) {
	*width = imageset->width;
	*height = imageset->height;
}