annotate lamertetris/sources/field.C @ 4:0962f0296c8a

ColorsMemory-0.210503-1.1
author prymula <prymula76@outlook.com>
date Tue, 30 Jan 2024 16:43:10 +0100
parents 2787f5e749ae
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
prymula <prymula76@outlook.com>
parents:
diff changeset
1 #include "game.h"
prymula <prymula76@outlook.com>
parents:
diff changeset
2 #include "field.h"
prymula <prymula76@outlook.com>
parents:
diff changeset
3
prymula <prymula76@outlook.com>
parents:
diff changeset
4
prymula <prymula76@outlook.com>
parents:
diff changeset
5 Uint32 getpixel(SDL_Surface *surface, int x, int y)
prymula <prymula76@outlook.com>
parents:
diff changeset
6 {
prymula <prymula76@outlook.com>
parents:
diff changeset
7 int bpp = surface->format->BytesPerPixel;
prymula <prymula76@outlook.com>
parents:
diff changeset
8 /* Here p is the address to the pixel we want to retrieve */
prymula <prymula76@outlook.com>
parents:
diff changeset
9 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
prymula <prymula76@outlook.com>
parents:
diff changeset
10
prymula <prymula76@outlook.com>
parents:
diff changeset
11 switch(bpp) {
prymula <prymula76@outlook.com>
parents:
diff changeset
12 case 1:
prymula <prymula76@outlook.com>
parents:
diff changeset
13 return *p;
prymula <prymula76@outlook.com>
parents:
diff changeset
14 case 2:
prymula <prymula76@outlook.com>
parents:
diff changeset
15 return *(Uint16 *)p;
prymula <prymula76@outlook.com>
parents:
diff changeset
16 case 3:
prymula <prymula76@outlook.com>
parents:
diff changeset
17 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
prymula <prymula76@outlook.com>
parents:
diff changeset
18 return p[0] << 16 | p[1] << 8 | p[2];
prymula <prymula76@outlook.com>
parents:
diff changeset
19 else
prymula <prymula76@outlook.com>
parents:
diff changeset
20 return p[0] | p[1] << 8 | p[2] << 16;
prymula <prymula76@outlook.com>
parents:
diff changeset
21 case 4:
prymula <prymula76@outlook.com>
parents:
diff changeset
22 return *(Uint32 *)p;
prymula <prymula76@outlook.com>
parents:
diff changeset
23 default:
prymula <prymula76@outlook.com>
parents:
diff changeset
24 return 0;
prymula <prymula76@outlook.com>
parents:
diff changeset
25 }
prymula <prymula76@outlook.com>
parents:
diff changeset
26 }
prymula <prymula76@outlook.com>
parents:
diff changeset
27
prymula <prymula76@outlook.com>
parents:
diff changeset
28
prymula <prymula76@outlook.com>
parents:
diff changeset
29 void putpixel(SDL_Surface *surface, int x, int y, Uint8 R, Uint8 G, Uint8 B, Uint8 A)
prymula <prymula76@outlook.com>
parents:
diff changeset
30 {
prymula <prymula76@outlook.com>
parents:
diff changeset
31 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x *
prymula <prymula76@outlook.com>
parents:
diff changeset
32 surface->format->BytesPerPixel;
prymula <prymula76@outlook.com>
parents:
diff changeset
33 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
prymula <prymula76@outlook.com>
parents:
diff changeset
34 {
prymula <prymula76@outlook.com>
parents:
diff changeset
35 p[0] = R;
prymula <prymula76@outlook.com>
parents:
diff changeset
36 p[1] = G;
prymula <prymula76@outlook.com>
parents:
diff changeset
37 p[2] = B;
prymula <prymula76@outlook.com>
parents:
diff changeset
38 p[3] = A;
prymula <prymula76@outlook.com>
parents:
diff changeset
39 }
prymula <prymula76@outlook.com>
parents:
diff changeset
40 else
prymula <prymula76@outlook.com>
parents:
diff changeset
41 {
prymula <prymula76@outlook.com>
parents:
diff changeset
42 p[0] = A;
prymula <prymula76@outlook.com>
parents:
diff changeset
43 p[1] = B;
prymula <prymula76@outlook.com>
parents:
diff changeset
44 p[2] = G;
prymula <prymula76@outlook.com>
parents:
diff changeset
45 p[3] = R;
prymula <prymula76@outlook.com>
parents:
diff changeset
46 }
prymula <prymula76@outlook.com>
parents:
diff changeset
47 }
prymula <prymula76@outlook.com>
parents:
diff changeset
48
prymula <prymula76@outlook.com>
parents:
diff changeset
49
prymula <prymula76@outlook.com>
parents:
diff changeset
50 Field::Field (){
prymula <prymula76@outlook.com>
parents:
diff changeset
51
prymula <prymula76@outlook.com>
parents:
diff changeset
52 int i,j,k;
prymula <prymula76@outlook.com>
parents:
diff changeset
53 int figure=0, color=1;
prymula <prymula76@outlook.com>
parents:
diff changeset
54
prymula <prymula76@outlook.com>
parents:
diff changeset
55 // ustawia field i ustawia kolor przekazywany do szlam
prymula <prymula76@outlook.com>
parents:
diff changeset
56 Field::set(NFIGURE,NELEMENT, color);
prymula <prymula76@outlook.com>
parents:
diff changeset
57 for (figure=0;figure<NFIGURE;figure++){
prymula <prymula76@outlook.com>
parents:
diff changeset
58 for(k=0;k<NELEMENT;k++){
prymula <prymula76@outlook.com>
parents:
diff changeset
59
prymula <prymula76@outlook.com>
parents:
diff changeset
60 for (j=0; j<4; j++){
prymula <prymula76@outlook.com>
parents:
diff changeset
61 for (i=0; i<4; i++) {
prymula <prymula76@outlook.com>
parents:
diff changeset
62 if (this->elements[figure][j+k*4][i]==0) {
prymula <prymula76@outlook.com>
parents:
diff changeset
63 asm("nop");
prymula <prymula76@outlook.com>
parents:
diff changeset
64 // putchar(' ');
prymula <prymula76@outlook.com>
parents:
diff changeset
65 }
prymula <prymula76@outlook.com>
parents:
diff changeset
66 // rysuje field
prymula <prymula76@outlook.com>
parents:
diff changeset
67 else if (this->elements[figure][j+k*4][i]==1) {
prymula <prymula76@outlook.com>
parents:
diff changeset
68 Field::draw(figure,k,color,i*20,j*20); //rysuje punkty na field
prymula <prymula76@outlook.com>
parents:
diff changeset
69 // putchar('#');
prymula <prymula76@outlook.com>
parents:
diff changeset
70 }
prymula <prymula76@outlook.com>
parents:
diff changeset
71 }
prymula <prymula76@outlook.com>
parents:
diff changeset
72 //putchar('\n');
prymula <prymula76@outlook.com>
parents:
diff changeset
73 }
prymula <prymula76@outlook.com>
parents:
diff changeset
74 }
prymula <prymula76@outlook.com>
parents:
diff changeset
75 color++;// dla kazdej figury po kolorze
prymula <prymula76@outlook.com>
parents:
diff changeset
76 }
prymula <prymula76@outlook.com>
parents:
diff changeset
77
prymula <prymula76@outlook.com>
parents:
diff changeset
78
prymula <prymula76@outlook.com>
parents:
diff changeset
79
prymula <prymula76@outlook.com>
parents:
diff changeset
80 }
prymula <prymula76@outlook.com>
parents:
diff changeset
81
prymula <prymula76@outlook.com>
parents:
diff changeset
82
prymula <prymula76@outlook.com>
parents:
diff changeset
83
prymula <prymula76@outlook.com>
parents:
diff changeset
84
prymula <prymula76@outlook.com>
parents:
diff changeset
85 void Field::draw(int n_figure, int n_element,int color,int x, int y){
prymula <prymula76@outlook.com>
parents:
diff changeset
86
prymula <prymula76@outlook.com>
parents:
diff changeset
87 SDL_Rect srcrect;
prymula <prymula76@outlook.com>
parents:
diff changeset
88 SDL_Rect dstrect;
prymula <prymula76@outlook.com>
parents:
diff changeset
89 SDL_Surface *p;
prymula <prymula76@outlook.com>
parents:
diff changeset
90 int r,g,b;
prymula <prymula76@outlook.com>
parents:
diff changeset
91
prymula <prymula76@outlook.com>
parents:
diff changeset
92 srcrect.x = 0;
prymula <prymula76@outlook.com>
parents:
diff changeset
93 srcrect.y = 0;
prymula <prymula76@outlook.com>
parents:
diff changeset
94 srcrect.w = 20;
prymula <prymula76@outlook.com>
parents:
diff changeset
95 srcrect.h = 20;
prymula <prymula76@outlook.com>
parents:
diff changeset
96 dstrect.x = x;
prymula <prymula76@outlook.com>
parents:
diff changeset
97 dstrect.y = y;
prymula <prymula76@outlook.com>
parents:
diff changeset
98 dstrect.w = 80;
prymula <prymula76@outlook.com>
parents:
diff changeset
99 dstrect.h = 80;
prymula <prymula76@outlook.com>
parents:
diff changeset
100 //std::cout<<"color:draw: "<<color<<std::endl;
prymula <prymula76@outlook.com>
parents:
diff changeset
101 //Field::setColor(color,&r, &g, &b); //ustawiamy kolor szary
prymula <prymula76@outlook.com>
parents:
diff changeset
102 this->setColor(color,&r, &g, &b); //ustawiamy kolor
prymula <prymula76@outlook.com>
parents:
diff changeset
103 //p=Field::createField(20,20,r,g,b);
prymula <prymula76@outlook.com>
parents:
diff changeset
104 p=this->createField(20,20,r,g,b);
prymula <prymula76@outlook.com>
parents:
diff changeset
105
prymula <prymula76@outlook.com>
parents:
diff changeset
106 SDL_BlitSurface(p, &srcrect, field[n_figure].element[n_element], &dstrect);
prymula <prymula76@outlook.com>
parents:
diff changeset
107 SDL_SetColorKey(field[n_figure].element[n_element], SDL_TRUE, getpixel(field[n_figure].element[n_element],79, 79));
prymula <prymula76@outlook.com>
parents:
diff changeset
108
prymula <prymula76@outlook.com>
parents:
diff changeset
109 SDL_FreeSurface(p);
prymula <prymula76@outlook.com>
parents:
diff changeset
110 }
prymula <prymula76@outlook.com>
parents:
diff changeset
111
prymula <prymula76@outlook.com>
parents:
diff changeset
112 void Field::set(int n_figure, int n_element, int color){
prymula <prymula76@outlook.com>
parents:
diff changeset
113
prymula <prymula76@outlook.com>
parents:
diff changeset
114 int i,j;
prymula <prymula76@outlook.com>
parents:
diff changeset
115 int r,g,b;
prymula <prymula76@outlook.com>
parents:
diff changeset
116 //int color=0;
prymula <prymula76@outlook.com>
parents:
diff changeset
117 for (i=0;i<NFIGURE;i++){
prymula <prymula76@outlook.com>
parents:
diff changeset
118 field[i].color=color++; // stad jest przekazywany kolor do szlam
prymula <prymula76@outlook.com>
parents:
diff changeset
119 this->setColor(WHITE,&r, &g, &b); //ustawiamy kolor bialy
prymula <prymula76@outlook.com>
parents:
diff changeset
120 for(j=0;j<n_element;j++){
prymula <prymula76@outlook.com>
parents:
diff changeset
121 // tworzy cztery instancje rysunku n_element
prymula <prymula76@outlook.com>
parents:
diff changeset
122 field[i].element[j]=this->createField(80,80,r,g,b);
prymula <prymula76@outlook.com>
parents:
diff changeset
123 }
prymula <prymula76@outlook.com>
parents:
diff changeset
124 }
prymula <prymula76@outlook.com>
parents:
diff changeset
125 }
prymula <prymula76@outlook.com>
parents:
diff changeset
126
prymula <prymula76@outlook.com>
parents:
diff changeset
127 SDL_Surface * Field::get(int n_figure, int n_element) {
prymula <prymula76@outlook.com>
parents:
diff changeset
128
prymula <prymula76@outlook.com>
parents:
diff changeset
129 return field[n_figure].element[n_element];
prymula <prymula76@outlook.com>
parents:
diff changeset
130 }
prymula <prymula76@outlook.com>
parents:
diff changeset
131
prymula <prymula76@outlook.com>
parents:
diff changeset
132 int Field::getColor(int n_figure) {
prymula <prymula76@outlook.com>
parents:
diff changeset
133 return field[n_figure].color;
prymula <prymula76@outlook.com>
parents:
diff changeset
134 }
prymula <prymula76@outlook.com>
parents:
diff changeset
135
prymula <prymula76@outlook.com>
parents:
diff changeset
136 bool Field::checkEdgeLeft(SDL_Surface *f, int x_field) {
prymula <prymula76@outlook.com>
parents:
diff changeset
137
prymula <prymula76@outlook.com>
parents:
diff changeset
138 int i, j;
prymula <prymula76@outlook.com>
parents:
diff changeset
139 Uint32 c;
prymula <prymula76@outlook.com>
parents:
diff changeset
140 Uint8 r, g, b, a;
prymula <prymula76@outlook.com>
parents:
diff changeset
141
prymula <prymula76@outlook.com>
parents:
diff changeset
142 for (i=0;i<80;i+=20){ // y
prymula <prymula76@outlook.com>
parents:
diff changeset
143 for (j=0;j<80;j+=20){ // x
prymula <prymula76@outlook.com>
parents:
diff changeset
144 c=getpixel(f, j, i); //getpixel2
prymula <prymula76@outlook.com>
parents:
diff changeset
145 SDL_GetRGBA(c, f->format, &r, &g, &b, &a);
prymula <prymula76@outlook.com>
parents:
diff changeset
146
prymula <prymula76@outlook.com>
parents:
diff changeset
147 if (x_field+j <=0)
prymula <prymula76@outlook.com>
parents:
diff changeset
148 {
prymula <prymula76@outlook.com>
parents:
diff changeset
149 printf ("ZERO!!!\n");
prymula <prymula76@outlook.com>
parents:
diff changeset
150 if (r!=255 || g!=255 || b!=255)
prymula <prymula76@outlook.com>
parents:
diff changeset
151 return SDL_FALSE;
prymula <prymula76@outlook.com>
parents:
diff changeset
152 }
prymula <prymula76@outlook.com>
parents:
diff changeset
153 }
prymula <prymula76@outlook.com>
parents:
diff changeset
154 }
prymula <prymula76@outlook.com>
parents:
diff changeset
155 return SDL_TRUE;
prymula <prymula76@outlook.com>
parents:
diff changeset
156 }
prymula <prymula76@outlook.com>
parents:
diff changeset
157
prymula <prymula76@outlook.com>
parents:
diff changeset
158 bool Field::checkEdgeRight(SDL_Surface *f, int x_field) {
prymula <prymula76@outlook.com>
parents:
diff changeset
159
prymula <prymula76@outlook.com>
parents:
diff changeset
160 int i, j;
prymula <prymula76@outlook.com>
parents:
diff changeset
161 Uint32 c;
prymula <prymula76@outlook.com>
parents:
diff changeset
162 Uint8 r, g, b, a;
prymula <prymula76@outlook.com>
parents:
diff changeset
163
prymula <prymula76@outlook.com>
parents:
diff changeset
164 for (i=0;i<80;i+=20){ // y
prymula <prymula76@outlook.com>
parents:
diff changeset
165 for (j=0;j<80;j+=20){ // x
prymula <prymula76@outlook.com>
parents:
diff changeset
166 c=getpixel(f, j, i); //getpixel2
prymula <prymula76@outlook.com>
parents:
diff changeset
167 SDL_GetRGBA(c, f->format, &r, &g, &b, &a);
prymula <prymula76@outlook.com>
parents:
diff changeset
168 if (x_field+j >= 380)
prymula <prymula76@outlook.com>
parents:
diff changeset
169 {
prymula <prymula76@outlook.com>
parents:
diff changeset
170 if (r!=255 || g!=255 || b!=255)
prymula <prymula76@outlook.com>
parents:
diff changeset
171 return SDL_FALSE;
prymula <prymula76@outlook.com>
parents:
diff changeset
172 }
prymula <prymula76@outlook.com>
parents:
diff changeset
173 }
prymula <prymula76@outlook.com>
parents:
diff changeset
174 }
prymula <prymula76@outlook.com>
parents:
diff changeset
175 return SDL_TRUE;
prymula <prymula76@outlook.com>
parents:
diff changeset
176 }
prymula <prymula76@outlook.com>
parents:
diff changeset
177
prymula <prymula76@outlook.com>
parents:
diff changeset
178 bool Field::checkEdgeRotate(SDL_Surface *f, int x_field) {
prymula <prymula76@outlook.com>
parents:
diff changeset
179
prymula <prymula76@outlook.com>
parents:
diff changeset
180 int i, j;
prymula <prymula76@outlook.com>
parents:
diff changeset
181 Uint32 c;
prymula <prymula76@outlook.com>
parents:
diff changeset
182 Uint8 r, g, b, a;
prymula <prymula76@outlook.com>
parents:
diff changeset
183
prymula <prymula76@outlook.com>
parents:
diff changeset
184 for (i=0;i<80;i+=20){ // y
prymula <prymula76@outlook.com>
parents:
diff changeset
185 for (j=0;j<80;j+=20){ // x
prymula <prymula76@outlook.com>
parents:
diff changeset
186 c=getpixel(f, j, i); //getpixel2
prymula <prymula76@outlook.com>
parents:
diff changeset
187 SDL_GetRGBA(c, f->format, &r, &g, &b, &a);
prymula <prymula76@outlook.com>
parents:
diff changeset
188 printf ("y: %d x: %d r: %d g: %d b: %d a: %d\n", i, j, r, g, b, a);
prymula <prymula76@outlook.com>
parents:
diff changeset
189 printf ("X_FIELD: %d X_FILED+j: %d\n", x_field, x_field+j);
prymula <prymula76@outlook.com>
parents:
diff changeset
190 if (x_field+j >= 400)
prymula <prymula76@outlook.com>
parents:
diff changeset
191 {
prymula <prymula76@outlook.com>
parents:
diff changeset
192 printf ("ZERO!!!\n");
prymula <prymula76@outlook.com>
parents:
diff changeset
193 if (r!=255 || g!=255 || b!=255)
prymula <prymula76@outlook.com>
parents:
diff changeset
194 return SDL_FALSE;
prymula <prymula76@outlook.com>
parents:
diff changeset
195 }
prymula <prymula76@outlook.com>
parents:
diff changeset
196 }
prymula <prymula76@outlook.com>
parents:
diff changeset
197 }
prymula <prymula76@outlook.com>
parents:
diff changeset
198 return SDL_TRUE;
prymula <prymula76@outlook.com>
parents:
diff changeset
199 }
prymula <prymula76@outlook.com>
parents:
diff changeset
200
prymula <prymula76@outlook.com>
parents:
diff changeset
201
prymula <prymula76@outlook.com>
parents:
diff changeset
202
prymula <prymula76@outlook.com>
parents:
diff changeset
203
prymula <prymula76@outlook.com>
parents:
diff changeset
204