summaryrefslogtreecommitdiff
path: root/src/client/layer.c
blob: 22c037dc5debb3f4cdf47e94e00f484a3af39af5 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <stdlib.h>
#include <assert.h>

#include "main.h"
#include "list.h"
#include "tux.h"

#include "interface.h"
#include "layer.h"
#include "image.h"

#include "world.h"

static list_t *listLayer;

static bool_t isLayerInit = FALSE;

bool_t layer_is_inicialized()
{
	return isLayerInit;
}

/*
 * Initialize global list of the layers
 */
void layer_init()
{
	listLayer = list_new();
	isLayerInit = TRUE;
}

/*
 * Add image to queue for rendering
 * *img - image to be rendered
 * x,y - coordinates where to draw the image
 * w,h - width and height of rendered part of image
 * px,py - coordinates of upper left corner of rendered part of image
 * layer - on which layer to draw
 *         [0] - under Tuxanek; [1] same as Tuxanek; [2] over Tuxanek
 */
void addLayer(image_t *img, int x, int y, int px, int py, int w, int h, int player)
{
	layer_t *new;
	layer_t *actual;
	int actual_value;
	int new_value;
	int i;

	assert(img != NULL);

	new = malloc(sizeof(layer_t));
	new->x = x;
	new->y = y;
	new->w = w;
	new->h = h;
	new->px = px;
	new->py = py;
	new->layer = player;
	new->image = img;

	new_value = player * WINDOW_SIZE_Y + y + h;

	/* here we are sure, that we want to insert it somewhere else than the begining */
	for (i = 0; i < listLayer->count; i++) {
		actual = list_get(listLayer, i);

		actual_value = actual->layer * WINDOW_SIZE_Y + actual->y + actual->h;

		if (actual_value > new_value) {
			list_ins(listLayer, i, new);
			return;
		}
	}

	list_add(listLayer, new);
}

/*
 * Draws all items onto the screen according to the layer and coordinate Y+H
 * After drawing frees the list
 */
void layer_draw_all()
{
	layer_t *this;
	int i;

	for (i = 0; i < listLayer->count; i++) {
		this = (layer_t *) listLayer->list[i];

		image_draw(this->image, this->x, this->y, this->px, this->py, this->w, this->h);
	}

	/*printf("listLayer->count = %d\n", listLayer->count);*/

	list_destroy_item(listLayer, free);
	listLayer = list_new();
}

void layer_draw_center(int x, int y)
{
	layer_t *this;
	int screen_x, screen_y;
	int i;
	/*int count = 0;*/

	arena_get_center_screen(&screen_x, &screen_y, x, y, WINDOW_SIZE_X, WINDOW_SIZE_Y);

	for (i = 0; i < listLayer->count; i++) {
		this = (layer_t *) listLayer->list[i];

		if (this->x + this->w < screen_x ||
		    this->x > screen_x + WINDOW_SIZE_X ||
		    this->y + this->h < screen_y ||
		    this->y > screen_y + WINDOW_SIZE_Y) {
			continue;
		}
		/*count++;*/

		image_draw(this->image, this->x - screen_x, this->y - screen_y,
					this->px, this->py, this->w, this->h);

		/*printf("%d %d\n", this->x - screen_x, this->y - screen_y);*/
	}

	/*printf("count = %d\n", count);*/

	list_destroy_item(listLayer, free);
	listLayer = list_new();
}

void layer_draw_slpit(int local_x, int local_y, int x, int y, int w, int h)
{
	layer_t *this;
	int i;
	/*int count = 0;*/

	for (i = 0; i < listLayer->count; i++) {
		this = (layer_t *) listLayer->list[i];

		if (this->x + this->w < x || this->x > x + w ||
		    this->y + this->h < y || this->y > y + h) {
			continue;
		}

		/*count++;*/

		if (local_x + (this->x - x) < local_x) {
			int z;

			z = local_x - (local_x + (this->x - x));

			this->x += z;
			this->px += z;
			this->w -= z;
		}

		if (local_x + (this->x - x) + this->w > local_x + w) {
			this->w -= (local_x + (this->x - x) + this->w) - (local_x + w);
		}

		if (local_y + (this->y - y) < local_y) {
			int z;

			z = local_y - (local_y + (this->y - y));

			this->y += z;
			this->py += z;
			this->h -= z;
		}

		if (local_y + (this->y - y) + this->h > local_y + h) {
			this->h -= (local_y + (this->y - y) + this->h) - (local_y + h);
		}

		image_draw(this->image, local_x + (this->x - x), local_y + (this->y - y),
				       this->px, this->py, this->w, this->h);

		/*printf("%d %d\n", this->x - screen_x, this->y - screen_y);*/
	}

	/*printf("count = %d\n", count);*/

	list_destroy_item(listLayer, free);
	listLayer = list_new();
}

void layer_flush()
{
	list_destroy_item(listLayer, free);
	listLayer = list_new();
}

/*
 * Delete list of layers from memory
 */
void layer_quit()
{
	list_destroy_item(listLayer, free);
	isLayerInit = FALSE;
}