comparison lamertetris/sources/szlam.C @ 0:2787f5e749ae

INIT
author prymula <prymula76@outlook.com>
date Thu, 21 Sep 2023 22:33:57 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:2787f5e749ae
1 /*
2 project: Lamer Teris
3 file: szlam.C
4 write: PRP
5 licence: Public Domain
6 date: 9-10-2018
7 editor: VSCode
8 */
9 #include "field.h"
10 #include "szlam.h"
11 #include <iostream>
12
13 char szlamField [30][20]; // 600/20x400/20
14 bool szlamGhost [30] = {SDL_FALSE};
15
16 Szlam::Szlam(){
17
18 field=this->createField(400,600, 255, 255, 255);
19 SDL_SetColorKey(field, SDL_TRUE, getpixel(field,0,0)); //getpixel2
20
21 }
22 SDL_Surface * Szlam::get() {
23
24 return field;
25 }
26
27 void Szlam::reset()
28 {
29 int i,j;
30
31 for (i=0; i<600/20; i++){
32 for (j=0; j<400/20; j++) {
33 szlamField[i][j]=0;
34 }
35 szlamGhost[i]=SDL_FALSE;
36 }
37 }
38
39 void Szlam::draw(){
40
41 SDL_Surface *px;
42 SDL_Rect box;
43 int i,j;
44 int r,g,b;
45
46 px=this->createField(20,20, 128, 128, 100);
47 SDL_FillRect(field, NULL, SDL_MapRGB(field->format, 255, 255, 255));
48 SDL_SetColorKey(field,SDL_TRUE, getpixel(field,0,0)); //getpixel2
49
50 for (i=0; i<600/20; i++){
51 for (j=0; j<400/20; j++) {
52 if (szlamField[i][j]==0) {
53 asm("nop");
54 //putchar(' ');
55 }
56 else if (szlamField[i][j]!=0) {
57 box = set_rect(j*20, i*20, 20, 20);
58 this->setColor(szlamField[i][j],&r,&g,&b);
59 SDL_FillRect(px, NULL, SDL_MapRGB(field->format, r,g,b));
60 SDL_BlitSurface( px, NULL, field, &box );
61 //putchar('#');
62 }
63 }
64 //putchar('\n');
65 }
66
67 SDL_FreeSurface(px);
68 }
69
70 bool Szlam::addBottom(SDL_Surface *s, int n_figure, int n_element, int color, int posX, int posY) {
71 Uint32 c;
72 Uint8 r,g,b,a;
73 int j,i,down = 0, v = 0, vv = 0;
74
75 for(j=0;j<80;j+=20){ //j++
76 for (i=0;i<80;i+=1){ //i++
77 c=getpixel(s, j, i); //getpixel2
78 SDL_GetRGBA(c, s->format, &r, &g, &b, &a);
79 if (r !=255 || g !=255 || b !=255) {
80 if (i>down)
81 {
82 down=i;
83
84 switch (80 % down){
85 case 1:
86 v = 0;
87 vv = 0;
88 break;
89 case 21:
90 v = 1;
91 vv = 20;
92 break;
93 case 2:
94 v = 2;
95 vv = 40;
96 break;
97 }
98 }
99 }
100 else {
101
102 }
103 }
104 }
105
106 if (down+posY+vv>=600) {
107 for (i=0; i<4; i++){
108 for (j=0; j<4; j++) {
109 if (this->elements[n_figure][i+n_element*4][j]==0) {
110 asm("nop");
111 putchar(' ');
112 }
113 else if (this->elements[n_figure][i+n_element*4][j]==1) {
114 szlamField[posY/20+i+v][posX/20+j]=color;
115
116 putchar('#');
117 }
118 }
119 putchar('\n');
120 }
121 return true;
122 }
123 return false;
124 }
125
126 bool Szlam::collisionLeft(SDL_Surface *s, int n_element,
127 int figure, int x_s, int y_s, int color){
128
129 Uint32 cf, cs;
130 Uint8 r,g,b,a, r2,g2,b2,a2;
131 int i, j=0;
132
133 for(j=0;j<80;j+=1){ // y j++
134 for (i=0;i<80;i+=1){ // x i++
135
136 cs=getpixel(s, i, j); //getpixel2
137 SDL_GetRGBA(cs, s->format, &r, &g, &b, &a);
138 cf=getpixel(field, i+x_s-20, j+y_s); //getpixel2
139 printf("2\n");
140 SDL_GetRGBA(cf, field->format, &r2, &g2, &b2, &a2);
141
142 if ((r2!=255 || g2!=255 || b2!=255) &&
143 (r!=255 || g!=255 || b!=255)) {
144 //*bottom=0;
145 //printf ("----bottom: %d\n", *bottom);
146 return SDL_TRUE;
147 //break;
148 }
149 }
150 }
151 return SDL_FALSE;
152 }
153
154 bool Szlam::collisionRight(SDL_Surface *s, int n_element,
155 int figure, int x_s, int y_s, int color){
156
157 Uint32 cf, cs;
158 Uint8 r,g,b,a, r2,g2,b2,a2;
159 int i, j=0;
160
161 for(j=0;j<80;j+=1){ // y j++
162 for (i=0;i<80;i+=1){ // x i++
163
164 cs=getpixel(s, i, j); //getpixel2
165 SDL_GetRGBA(cs, s->format, &r, &g, &b, &a);
166 cf=getpixel(field, i+x_s+20, j+y_s); //getpixel2
167 SDL_GetRGBA(cf, field->format, &r2, &g2, &b2, &a2);
168
169 if ((r2!=255 || g2!=255 || b2!=255) &&
170 (r!=255 || g!=255 || b!=255)) {
171 //*bottom=0;
172 return SDL_TRUE;
173 //break;
174 }
175 }
176 }
177 return SDL_FALSE;
178 }
179
180 bool Szlam::collisionRotate(SDL_Surface *s, int n_element,
181 int figure, int x_s, int y_s, int color){
182
183 Uint32 cf, cs;
184 Uint8 r,g,b,a, r2,g2,b2,a2;
185 int i, j=0;
186
187 for(j=0;j<80;j+=1){ // y j++
188 for (i=0;i<80;i+=1){ // x i++
189
190 cs=getpixel(s, i, j); //getpixel2
191 SDL_GetRGBA(cs, s->format, &r, &g, &b, &a);
192 cf=getpixel(field, i+x_s, j+y_s); //getpixel2
193 SDL_GetRGBA(cf, field->format, &r2, &g2, &b2, &a2);
194
195 if ((r2!=255 || g2!=255 || b2!=255) &&
196 (r!=255 || g!=255 || b!=255)) {
197 // *bottom=0;
198 return SDL_TRUE;
199 //break;
200 }
201 }
202 }
203 return SDL_FALSE;
204 }
205
206 bool Szlam::collisionBottom(SDL_Surface *s, int n_element,
207 int figure, int x_s, int y_s, int color) {
208
209
210 Uint32 cf, cs;
211 Uint8 r,g,b,a, r2,g2,b2,a2;
212 int i, j=0;
213 bool go_szlam=false;
214
215 for(j=0;j<80;j+=1){ // y j++
216 for (i=0;i<80;i+=20){ // x i++
217 cs=getpixel(s, i, j); //getpixel2
218 SDL_GetRGBA(cs, s->format, &r, &g, &b, &a);
219 cf=getpixel(field, i+x_s, j+y_s); //getpixel2
220 SDL_GetRGBA(cf, field->format, &r2, &g2, &b2, &a2);
221
222 if ((r2!=255 || g2!=255 || b2!=255) &&
223 (r!=255 || g!=255 || b!=255)) {
224 go_szlam=true;
225 break;
226 }
227 }
228 }
229
230
231 if (go_szlam) {
232 for (i=0; i<4; i++){
233 for (j=0; j<4; j++) {
234 if (this->elements[figure][i+n_element*4][j]==0) {
235 asm("nop");
236 }
237 else if (this->elements[figure][i+n_element*4][j]==1) {
238 szlamField[y_s/20+i][x_s/20+j]=color;
239 }
240 }
241 }
242 return true;
243 }
244
245 return false;
246 }
247
248 // sprawdza czy równa powierzchnia
249 int Szlam::evenSurface(int *height)
250 {
251 int i, j, count=0;
252 int full[600/20] = {0};
253 //bool flag[30] = SDL_FALSE;
254
255 for (i=0; i<600/20; i++){
256 for (j=0; j<400/20; j++) {
257 if (szlamField[i][j] !=0 && szlamGhost[i]==SDL_FALSE) {
258 full[i]+=1;
259
260 }
261 }
262 }
263 for (i=0; i < 600/20; i++){
264 if (full[i]==20) {
265 count++;
266 szlamGhost[i]=SDL_TRUE;
267 }
268 if (full[i]!=0) (*height)++;
269 }
270 return count;
271 }
272
273 // skraca studnie
274 void Szlam::cutSurface(int n, int h)
275 {
276 int i, j, k;
277
278 while (n-->0) {
279 //for (i=600/20-1, k=h; k>0; i--, k--) {
280 for (i=600/20-1, k=h; i>0; i--, k--) {
281 for (j=400/20-1; j>=0; j--){
282 szlamField[i][j] = szlamField[i-1][j];
283 szlamGhost[i] = szlamGhost[i-1];
284
285 }
286 }
287 }
288 }
289
290 bool Szlam::highFull()
291 {
292 int j;
293
294 for (j=0; j<400/20; j++){
295 if (szlamField[0][j] != 0 || szlamField[1][j] != 0)
296 return SDL_TRUE;
297 }
298 return SDL_FALSE;
299 }
300