summaryrefslogtreecommitdiff
path: root/riscos/sprite.c
blob: a68cd57cec0c9387eb8590ee965b55bcf2311995 (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 NetSurf, http://netsurf.sourceforge.net/
 * Licensed under the GNU General Public License,
 *                http://www.opensource.org/licenses/gpl-license
 * Copyright 2003 John M Bell <jmb202@ecs.soton.ac.uk>
 */

/** \file
 * Content for image/x-riscos-sprite (RISC OS implementation).
 *
 * No conversion is necessary: we can render RISC OS sprites directly under
 * RISC OS.
 *
 * Unfortunately we have to make a copy of the bitmap data, because sprite areas
 * need a length word at the start.
 */

#include <string.h>
#include <stdlib.h>
#include "oslib/osspriteop.h"
#include "netsurf/utils/config.h"
#include "netsurf/content/content.h"
#include "netsurf/riscos/gui.h"
#include "netsurf/riscos/image.h"
#include "netsurf/riscos/sprite.h"
#include "netsurf/utils/log.h"
#include "netsurf/utils/messages.h"
#include "netsurf/utils/utils.h"

#ifdef WITH_SPRITE


/**
 * Create a new CONTENT_SPRITE.
 *
 * A new empty sprite area is allocated.
 */

bool sprite_create(struct content *c, const char *params[])
{
	union content_msg_data msg_data;

	c->data.sprite.data = malloc(4);
	if (!c->data.sprite.data) {
		msg_data.error = messages_get("NoMemory");
		content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
		warn_user("NoMemory", 0);
		return false;
	}
	c->data.sprite.length = 4;
	return true;
}


/**
 * Process data for a CONTENT_SPRITE.
 *
 * The data is just copied into the sprite area.
 */

bool sprite_process_data(struct content *c, char *data, unsigned int size)
{
	char *sprite_data;
	union content_msg_data msg_data;

	sprite_data = realloc(c->data.sprite.data,
			c->data.sprite.length + size);
	if (!sprite_data) {
		msg_data.error = messages_get("NoMemory");
		content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
		warn_user("NoMemory", 0);
		return false;
	}
	c->data.sprite.data = sprite_data;
	memcpy((char*)(c->data.sprite.data) + c->data.sprite.length,
			data, size);
	c->data.sprite.length += size;
	c->size += size;
	return true;
}


/**
 * Convert a CONTENT_SPRITE for display.
 *
 * No conversion is necessary. We merely read the sprite dimensions.
 */

bool sprite_convert(struct content *c, int width, int height)
{
	os_error *error;
	int w, h;
	union content_msg_data msg_data;
	osspriteop_area *area = (osspriteop_area*)c->data.sprite.data;

	/* fill in the size (first word) of the area */
	area->size = c->data.sprite.length;

	error = xosspriteop_read_sprite_info(osspriteop_PTR,
			area,
			(osspriteop_id) ((char *) area + area->first),
			&w, &h, NULL, NULL);
	if (error) {
		LOG(("xosspriteop_read_sprite_info: 0x%x: %s",
				error->errnum, error->errmess));
		msg_data.error = error->errmess;
		content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
		return false;
	}

	c->width = w;
	c->height = h;
	c->title = malloc(100);
	if (c->title)
		snprintf(c->title, 100, messages_get("SpriteTitle"), c->width,
				c->height, c->data.sprite.length);
	c->status = CONTENT_STATUS_DONE;
	return true;
}


/**
 * Destroy a CONTENT_SPRITE and free all resources it owns.
 */

void sprite_destroy(struct content *c)
{
	free(c->data.sprite.data);
	free(c->title);
}


/**
 * Redraw a CONTENT_SPRITE.
 */

bool sprite_redraw(struct content *c, int x, int y,
		int width, int height,
		int clip_x0, int clip_y0, int clip_x1, int clip_y1,
		float scale, colour background_colour)
{
	return image_redraw(c->data.sprite.data,
			ro_plot_origin_x + x * 2,
			ro_plot_origin_y - y * 2,
			width, height,
			c->width,
			c->height,
			background_colour,
			false, false, false,
			IMAGE_PLOT_OS);
}

#endif