-
Notifications
You must be signed in to change notification settings - Fork 5
/
Grid.h
145 lines (139 loc) · 4.59 KB
/
Grid.h
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
#pragma once
#include "common.h"
#include "Mesh.h"
class Deck;
#include "FragmentShader.h"
class TrackerGrid;
class Grid {
public:
static int idgen;
int id;
int width, height;
int *index_table;
bool *xflip_table;
bool *yflip_table;
Vec2 *texofs_table;
bool *rot_table;
Color *color_table;
Deck *deck;
FragmentShader *fragment_shader;
bool visible;
void *parent_prop; // Used only when headless server
Mesh *mesh;
bool color_changed;
bool uv_changed;
int debug;
Vec2 rel_scl, rel_loc;
TrackerGrid *tracker;
float uv_margin;
static const int GRID_FLAG_XFLIP = 1;
static const int GRID_FLAG_YFLIP = 2;
static const int GRID_NOT_USED = -1;
Grid(int w, int h ) : width(w), height(h), index_table(NULL), xflip_table(NULL), yflip_table(NULL), texofs_table(NULL), rot_table(NULL), color_table(NULL), deck(NULL), fragment_shader(NULL), visible(true), parent_prop(NULL), mesh(NULL), color_changed(false), uv_changed(false), debug(0), rel_scl(1,1), rel_loc(0,0), tracker(NULL), uv_margin(0) {
id = idgen++;
}
~Grid();
void setDeck( Deck *d ){
deck = d;
}
inline int index(int x, int y){
assertmsg(x>=0 && x<width,"invalid x:%d",x);
assertmsg(y>=0 && y<height,"invalid y:%d",y);
return ( x + y * width );
}
inline int getCellNum() { return width * height; }
#define ENSURE_GRID_TABLE( membname, T, inival) if( !membname ){ membname = (T*) MALLOC(width*height*sizeof(T)); int i=0; for(int y=0;y<height;y++){ for(int x=0;x<width;x++){ membname[i++] = inival; }}}
inline void set(int x, int y, int ind ){
ENSURE_GRID_TABLE( index_table, int, GRID_NOT_USED );
int orig = index_table[ index(x,y) ];
index_table[ index(x,y) ] = ind;
if(orig!=ind) uv_changed = true;
}
inline int get(int x, int y){
if(!index_table){
return GRID_NOT_USED;
}
return index_table[ index(x,y) ];
}
void bulkSetIndex( int *inds ) {
ENSURE_GRID_TABLE( index_table, int, GRID_NOT_USED );
memcpy( index_table, inds, width*height*sizeof(int) );
uv_changed = true;
}
inline void setXFlip( int x, int y, bool flag ){
ENSURE_GRID_TABLE( xflip_table, bool, false );
xflip_table[ index(x,y) ] = flag;
uv_changed = true;
}
inline void setXFlipIndex( int i, bool flag ) {
setXFlip( i % width, i / width, flag );
}
inline bool getXFlip( int x, int y ) {
if( !xflip_table ) return false;
return xflip_table[ index(x,y) ];
}
inline void setYFlip( int x, int y, bool flag ){
ENSURE_GRID_TABLE( yflip_table, bool, false );
yflip_table[ index(x,y) ] = flag;
uv_changed = true;
}
inline void setYFlipIndex( int i, bool flag ) {
setYFlip( i % width, i / width, flag );
}
inline bool getYFlip( int x, int y ) {
if( !yflip_table ) return false;
return yflip_table[ index(x,y) ];
}
// 0~1. 1 for just a cell. not by whole texture
inline void setTexOffset( int x, int y, Vec2 *v ) {
ENSURE_GRID_TABLE( texofs_table, Vec2, Vec2(0,0) );
int i = index(x,y);
texofs_table[i].x = v->x;
texofs_table[i].y = v->y;
uv_changed = true;
}
inline void setTexOffsetIndex( int ind, Vec2 *v ) {
setTexOffset( ind % width, ind / width, v );
}
inline void getTexOffset( int x, int y, Vec2 *outv ) {
if( !texofs_table ) {
outv->x = outv->y = 0;
} else {
int i = index(x,y);
outv->x = texofs_table[i].x;
outv->y = texofs_table[i].y;
}
}
inline void setUVRot( int x, int y, bool flag ){
ENSURE_GRID_TABLE( rot_table, bool, false );
rot_table[ index(x,y) ] = flag;
uv_changed = true;
}
inline void setUVRotIndex( int i, bool flag ) {
setUVRot( i % width, i / width, flag );
}
inline bool getUVRot( int x, int y ) {
ENSURE_GRID_TABLE( rot_table, bool, false );
return rot_table[ index(x,y) ];
}
inline void setColor( int x, int y, Color col ) {
ENSURE_GRID_TABLE( color_table, Color, Color(1,1,1,1) );
color_table[ index(x,y) ] = col;
color_changed = true;
}
inline void setColorIndex( int ind, Color col ) {
setColor( ind % width, ind / width, col );
}
inline Color getColor( int x, int y ) {
if(!color_table) return Color(1,1,1,1);
return color_table[ index(x,y) ];
}
inline void setFragmentShader( FragmentShader *fs ){
fragment_shader = fs;
}
inline void setVisible( bool flg ){ visible = flg; }
inline bool getVisible() { return visible; }
inline void clear(int x, int y) { set(x,y,GRID_NOT_USED); }
void clear( int ind = GRID_NOT_USED );
void fillColor( Color c );
};