summaryrefslogtreecommitdiff
path: root/csrc/mkf/graph_struct.c
blob: 1c4253c4cb00d733b148999e41a685cda350ae27 (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
/*
 * Copyright (C)  2018 Iru Cai <mytbk920423@gmail.com>
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include <stdlib.h>
#include <string.h>
#include "graph_struct.h"

struct graph_st * allocate_graph_st(int w, int h, int x, int y)
{
	struct graph_st * newst = (struct graph_st *)
		malloc(w * h * 2 + sizeof(struct graph_st));
	newst->width = w;
	newst->height = h;
	newst->x = x;
	newst->y = y;
	newst->gdata = newst->data;
	return newst;
}

/* crop graph a1 starting at (x, y) as laft-top coordinate,
 * and get graph a2 with size w*h.
 *
 * this function looks buggy and can crash the game */

struct graph_st * crop_graph(struct graph_st *a1, struct graph_st *a2, int x, int y, int w, int h)
{
	if (a2 == NULL) {
		a2 = allocate_graph_st(w, h, 0, 0);
	}

	int16_t *src = &a1->gdata[a1->width * y + x];
	int16_t *dst = a2->gdata;

	for (int i = 0; i < h; i++) {
		memcpy(dst, src, w * 2);
		dst = dst + w;
		src = src + a1->width;
	}
	return a2;
}