summaryrefslogtreecommitdiff
path: root/tests/suite/ecore/src/include/eina_inline_tiler.x
blob: ffc34c1cf65822802da68702b76185e0b3239ac1 (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
/* EINA - EFL data type library
 * Copyright (C) 2002-2009 Rafael Antognolli
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;
 * if not, see <http://www.gnu.org/licenses/>.
 */

#ifndef EINA_TILER_INLINE_H_
#define EINA_TILER_INLINE_H_

#include "eina_safety_checks.h"

/**
 * @cond LOCAL
 * This struct should not be accessed directly, it is used by
 * eina_tile_grid_slicer functions to maintain context and fill "info"
 * member with correct values for given iteration.
 */
struct _Eina_Tile_Grid_Slicer
{
   unsigned long col1, col2, row1, row2; // initial and final col,row
   int tile_w, tile_h; // tile width, height
   int x_rel, y_rel; // starting x,y coordinates of the first col,row
   int w1_rel, h1_rel; // width,height of the first col,row
   int w2_rel, h2_rel; // width,height of the last col,row
   struct Eina_Tile_Grid_Info info; // info about the current tile
   Eina_Bool first;
};

/**
 * @endcond
 */

/**
 * @brief Iterates over the tiles set by eina_tile_grid_slicer_setup().
 *
 * @param   slc Pointer to an Eina_Tile_Grid_Slicer struct.
 * @param   rect Pointer to a struct Eina_Tile_Grid_Info *.
 * @return  @c EINA_TRUE if the current rect is valid.
 *          @c EINA_FALSE if there is no more rects to iterate over (and
 *	       thus the current one isn't valid).
 *
 * This functions iterates over each Eina_Tile_Grid_Info *rect of the grid.
 * eina_tile_grid_slicer_setup() must be called first, and *rect is only valid
 * if this function returns EINA_TRUE. Its content shouldn't be modified.
 */
static inline Eina_Bool
eina_tile_grid_slicer_next(Eina_Tile_Grid_Slicer *slc, const Eina_Tile_Grid_Info **rect)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(slc, 0);

   if (slc->first)
     {
	slc->first = 0;
	*rect = &slc->info;
	return EINA_TRUE;
     }

   slc->info.col++;

   if (slc->info.col > slc->col2)
     {
	slc->info.row++;
	if (slc->info.row > slc->row2)
	  return EINA_FALSE;
	else if (slc->info.row < slc->row2)
	  slc->info.rect.h = slc->tile_h;
	else
	  slc->info.rect.h = slc->h2_rel;
	slc->info.rect.y = 0;
	slc->info.col = slc->col1;
	slc->info.rect.x = slc->x_rel;
	slc->info.rect.w = slc->w1_rel;
     }
   else
     {
	slc->info.rect.x = 0;
	if (slc->info.col < slc->col2)
	  slc->info.rect.w = slc->tile_w;
	else
	  slc->info.rect.w = slc->w2_rel;
     }

   if (slc->info.rect.w == slc->tile_w && slc->info.rect.h == slc->tile_h)
     slc->info.full = EINA_TRUE;
   else
     slc->info.full = EINA_FALSE;

   *rect = &slc->info;

   return EINA_TRUE;
}

/**
 * @brief Setup an Eina_Tile_Grid_Slicer struct.
 *
 * @param   slc Pointer to an Eina_Tile_Grid_Slicer struct.
 * @param   x X axis coordinate.
 * @param   y Y axis coordinate.
 * @param   w width.
 * @param   h height.
 * @param   tile_w tile width.
 * @param   tile_h tile height.
 * @return  A pointer to the Eina_Iterator.
 *          @c NULL on failure.
 *
 * This function splits the rectangle given as argument into tiles of size
 * tile_w X tile_h, and returns an iterator to them. The geometry of each
 * tile can be accessed with eina_tile_grid_slicer_next, where rect
 * will be a pointer to a struct Eina_Tile_Grid_Info.
 */
static inline Eina_Bool
eina_tile_grid_slicer_setup(Eina_Tile_Grid_Slicer *slc, int x, int y, int w, int h, int tile_w, int tile_h)
{
   int tx1, tx2, ty1, ty2;

   EINA_SAFETY_ON_NULL_RETURN_VAL(slc, 0);

   tx1 = x;
   ty1 = y;
   tx2 = x + w - 1;
   ty2 = y + h - 1;

   if (x < 0 || y < 0 || w <= 0 || h <= 0 || tile_w <= 0 || tile_h <= 0)
     {
	slc->first = 0;
	slc->col1 = slc->row1 = 0;
	slc->col2 = slc->row2 = 0;
	slc->info.col = slc->col1;
	slc->info.row = slc->row1;
	return EINA_TRUE;
     }

   slc->col1 = tx1 / tile_w;
   slc->row1 = ty1 / tile_h;
   slc->col2 = (tx2 - 0) / tile_w;
   slc->row2 = (ty2 - 0) / tile_h;
   slc->x_rel = tx1 % tile_w;
   slc->y_rel = ty1 % tile_h;
   slc->w1_rel = tile_w - slc->x_rel;
   slc->h1_rel = tile_h - slc->y_rel;
   slc->w2_rel = tx2 % tile_w + 1;
   slc->h2_rel = ty2 % tile_h + 1;

   slc->tile_w = tile_w;
   slc->tile_h = tile_h;

   slc->first = 1;
   slc->info.col = slc->col1;
   slc->info.row = slc->row1;
   slc->info.rect.x = slc->x_rel;
   slc->info.rect.y = slc->y_rel;

   if (slc->info.col == slc->col2)
     slc->w1_rel = slc->w2_rel - slc->x_rel;

   if (slc->info.row == slc->row2)
     slc->h1_rel = slc->h2_rel - slc->y_rel;

   slc->info.rect.w = slc->w1_rel;
   slc->info.rect.h = slc->h1_rel;

   if (slc->info.rect.w == slc->tile_w && slc->info.rect.h == slc->tile_h)
     slc->info.full = EINA_TRUE;
   else
     slc->info.full = EINA_FALSE;

   return EINA_TRUE;
}

#endif