-
Notifications
You must be signed in to change notification settings - Fork 7
/
penguin.c
236 lines (199 loc) · 5.35 KB
/
penguin.c
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* IceBreaker
* Copyright (c) 2000-2020 Matthew Miller <mattdm@mattdm.org>
*
* <http://www.mattdm.org/icebreaker/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program 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 General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <SDL.h>
#include <stdlib.h>
#include "icebreaker.h"
#include "globals.h"
#include "penguin.h"
#include "grid.h"
#include "line.h"
#include "level.h"
#include "options.h"
#include "themes.h"
static int createpenguinbgsave(Penguin* p);
Penguin createpenguin()
{
return(createpenguinxy(BORDERLEFT + (random() % (PLAYWIDTH-BLOCKWIDTH)),BORDERTOP + (random() % (PLAYHEIGHT-BLOCKWIDTH))));
}
Penguin createpenguinxy(int x, int y)
{
Penguin p;
switch (random() % 4)
{
case 0:
p.xdelta= PENGUINSPEED;p.ydelta= PENGUINSPEED;
p.image = spritemirrorimage;
break;
case 1:
p.xdelta= PENGUINSPEED;p.ydelta=-PENGUINSPEED;
p.image = spritemirrorimage;
break;
case 2:
p.xdelta=-PENGUINSPEED;p.ydelta= PENGUINSPEED;
p.image = spriteimage;
break;
case 3:
p.xdelta=-PENGUINSPEED;p.ydelta=-PENGUINSPEED;
p.image = spriteimage;
break;
default: // this won't happen, but it shuts up the warning.
p.xdelta=-PENGUINSPEED;p.ydelta=-PENGUINSPEED;
p.image = spriteimage;
break;
}
// For debugging only, of course -- this makes crippled penguins.
//p.xdelta=0; p.ydelta=0;
p.speedslower=0;
p.geom.w=p.image->w;
p.geom.h=p.image->h;
//p.geom.x=BORDERLEFT + (PLAYWIDTH +BLOCKWIDTH )/2;
//p.geom.y=BORDERTOP + (PLAYHEIGHT+BLOCKHEIGHT)/2;
p.geom.x=x;
p.geom.y=y;
createpenguinbgsave(&p);
return(p);
}
void deletepenguin(Penguin* p)
{
SDL_FreeSurface(p->bgsave);
}
void resetpenguinimage(Penguin* p)
{
SDL_FreeSurface(p->bgsave);
if (p->xdelta>0)
p->image=spritemirrorimage;
else
p->image=spriteimage;
p->geom.w=p->image->w;
p->geom.h=p->image->h;
createpenguinbgsave(p);
}
void savebehindpenguin(Penguin* p)
{
SDL_BlitSurface(screen, &(p->geom), p->bgsave, NULL);
}
void drawpenguin(Penguin* p)
{
SDL_BlitSurface(p->image, NULL, screen, &(p->geom));
}
void erasepenguin(Penguin* p)
{
SDL_BlitSurface(p->bgsave, NULL, screen, &(p->geom));
}
void movepenguin(Penguin* p)
{
int newx, newy;
int checkx,checky;
int movex=0,movey=0;
switch (options.difficulty)
{
case NORMAL:
if (p->speedslower)
{ movex=p->xdelta/2; movey=p->ydelta/2; }
else
{ movex=p->xdelta; movey=p->ydelta; }
p->speedslower=!p->speedslower;
break;
case HARD:
movex=p->xdelta; movey=p->ydelta;
break;
case EASY:
movex=p->xdelta/2; movey=p->ydelta/2;
break;
}
newx=p->geom.x+movex;
newy=p->geom.y+movey;
markgrid(p->geom.x,p->geom.y,BLOCKWIDTH,BLOCKHEIGHT,' ');
if (movex>0) checkx = newx+BLOCKWIDTH;
else checkx = newx;
if (grid[checkx][p->geom.y]==' ' && grid[checkx][p->geom.y+BLOCKHEIGHT-1]==' ')
{
p->geom.x+=movex;
}
else if (grid[checkx][p->geom.y]=='1' || grid[checkx][p->geom.y+BLOCKHEIGHT-1]=='1')
{
line1.dead=true;
p->geom.x+=movex;
}
else if (grid[checkx][p->geom.y]=='2' || grid[checkx][p->geom.y+BLOCKHEIGHT-1]=='2')
{
line2.dead=true;
p->geom.x+=movex;
}
else if ((grid[checkx][p->geom.y]=='w' || grid[checkx][p->geom.y]==' ' )
&& (grid[checkx][p->geom.y+BLOCKHEIGHT-1]=='w' || grid[checkx][p->geom.y+BLOCKHEIGHT-1]==' '))
{
// this is used in the intro. maybe some place else too in the future.
// should it be merged into the first line above? maybe.
p->geom.x+=movex;
}
else
{
p->xdelta=-p->xdelta;
if (p->image==spriteimage)
p->image=spritemirrorimage;
else
p->image=spriteimage;
}
if (movey>0) checky = newy+BLOCKHEIGHT;
else checky = newy;
if (grid[p->geom.x][checky]==' ' && grid[p->geom.x+BLOCKWIDTH-1][checky]==' ')
{
p->geom.y+=movey;
}
else if (grid[p->geom.x][checky]=='1' || grid[p->geom.x+BLOCKWIDTH-1][checky]=='1')
{
//printf("Hit 1\n");
line1.dead=true;
p->geom.y+=movey;
}
else if (grid[p->geom.x][checky]=='2' || grid[p->geom.x+BLOCKWIDTH-1][checky]=='2')
{
//printf("Hit 2\n");
line2.dead=true;
p->geom.y+=movey;
}
else if ((grid[p->geom.x][checky]=='w' || grid[p->geom.x][checky]==' ')
&& (grid[p->geom.x+BLOCKWIDTH-1][checky]=='w' || grid[p->geom.x+BLOCKWIDTH-1][checky]==' '))
{
// this is used in the intro. maybe some place else too in the future.
// should it be merged into the first line above? maybe.
p->geom.y+=movey;
}
else
{
p->ydelta=-p->ydelta;
}
markgrid(p->geom.x,p->geom.y,BLOCKWIDTH,BLOCKHEIGHT,'*');
}
int createpenguinbgsave(Penguin* p)
{
SDL_Surface* tmpsurface;
tmpsurface = SDL_CreateRGBSurface(SDL_SWSURFACE,p->geom.w,p->geom.h,screen->format->BitsPerPixel,0,0,0,0);
// fix -- add error checking
p->bgsave = SDL_DisplayFormat(tmpsurface);
if (p->bgsave==NULL)
p->bgsave=tmpsurface;
else
SDL_FreeSurface(tmpsurface);
return 0;
}