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