-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.cpp
More file actions
executable file
·189 lines (173 loc) · 7.44 KB
/
Copy pathImage.cpp
File metadata and controls
executable file
·189 lines (173 loc) · 7.44 KB
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
#include "Image.h"
#include "GameData.h"
Images::Images(GameData* GD)
{
Gamedata = GD;
LargePs=NULL; SmallPs=NULL; SingleP=NULL; SSingleP=NULL; LargeCs=NULL; SmallCs=NULL;
char *filename=new char[11];
char *sfilename=new char[12];
char *specname=new char[13];
sprintf(filename,"Pieces.bmp%c",'\0');
sprintf(sfilename,"SPieces.bmp%c",'\0');
sprintf(specname,"Specials.bmp%c",'\0');
// Loads Pieces.bmp from the game dir or if a Theme is set it loads it from
// the specified Theme folder
if (Gamedata->settings->ThemePath != NULL) {
delete [] filename; delete [] sfilename; delete [] specname;
filename=new char[strlen(Gamedata->settings->ThemePath)+11];
sfilename=new char[strlen(Gamedata->settings->ThemePath)+12];
specname=new char[strlen(Gamedata->settings->ThemePath)+13];
if (Gamedata->settings->ThemePath[strlen(Gamedata->settings->ThemePath)-1] != '\\'
&& Gamedata->settings->ThemePath[strlen(Gamedata->settings->ThemePath)-1] != '/') {
sprintf(filename,"%s\\Pieces.bmp%c",Gamedata->settings->ThemePath,'\0');
sprintf(sfilename,"%s\\SPieces.bmp%c",Gamedata->settings->ThemePath,'\0');
sprintf(specname,"%s\\Specials.bmp%c",Gamedata->settings->ThemePath,'\0');
}
else {
sprintf(filename,"%sPieces.bmp%c",Gamedata->settings->ThemePath,'\0');
sprintf(sfilename,"%sSPieces.bmp%c",Gamedata->settings->ThemePath,'\0');
sprintf(specname,"%sSpecials.bmp%c",Gamedata->settings->ThemePath,'\0');
}
}
SDL_Surface *piece=SDL_LoadBMP(filename);
SDL_Surface *spiece=SDL_LoadBMP(sfilename);
SDL_Surface *specials=SDL_LoadBMP(specname);
delete [] filename; delete [] sfilename; delete [] specname;
if (piece == NULL) {
Error("Can't load Pieces.bmp!");
exit(0);
}
if (specials == NULL) {
Error("Can't load Specials.bmp!");
exit(0);
}
if (piece->format->BitsPerPixel != 8) {
Error("Invalid Pieces.bmp file");
exit(0);
}
// Create the Large and Small Pieces
CreateLargePs(piece);
CreateSmallPs(spiece);
// Create the Large and Small Clear Piecese
CreateLargeCs();
CreateSmallCs();
// Setup the Specials
CreateSpecials(specials);
if (spiece != NULL)
SDL_FreeSurface(spiece);
SDL_FreeSurface(piece);
SDL_FreeSurface(specials);
return;
}
Images::~Images()
{
if (LargePs != NULL)
SDL_FreeSurface(LargePs);
if (SmallPs != NULL)
SDL_FreeSurface(SmallPs);
if (SingleP != NULL)
SDL_FreeSurface(SingleP);
if (SSingleP != NULL)
SDL_FreeSurface(SSingleP);
if (LargeCs != NULL)
SDL_FreeSurface(LargeCs);
if (SmallCs != NULL)
SDL_FreeSurface(SmallCs);
return;
}
void Images::CreateLargePs(SDL_Surface *piece)
{
int psize=piece->w/4; // The size of the piece
Frames=piece->h / (psize*4); // The number of frames
// This is the adjusted size of the piece based on the users resolution
int newpsize=int(((double(Gamedata->settings->X) / 640) * 30)+.5);
// Create a LargePs, SmallPs, LCear & SClear surfaces of the screens format
// Adjust the piece to fit the size based on resolution
LargePs=ResizeImageToFit(piece,4*newpsize,4*newpsize*Frames);
// Create a single Large piece surface for usage when rendering color
SingleP=SDL_CreateRGBSurface(SDL_SWSURFACE, newpsize, newpsize, 8, LargePs->format->Rmask, LargePs->format->Gmask, LargePs->format->Bmask, LargePs->format->Amask);
SDL_SetPalette(SingleP, SDL_LOGPAL, LargePs->format->palette->colors, 0, 256);
return;
}
void Images::CreateSmallPs(SDL_Surface *spiece)
{
int psize;
int newpsize=SingleP->w/2;
if (spiece != NULL) {
psize=spiece->w /4;
SFrames=spiece->h / (psize * 4);
}
else {
psize=LargePs->w /4;
SFrames=LargePs->h /(psize * 4);
}
// If SPieces.bmp doesn't exist create one from Pieces.bmp
if (spiece == NULL) {
SmallPs=ResizeImageToFit(LargePs, LargePs->w/2, LargePs->h/2);
SDL_SetPalette(SmallPs,SDL_LOGPAL,LargePs->format->palette->colors,0,256);
}
else {
SmallPs=ResizeImageToFit(spiece,SingleP->w*2, SingleP->h*2*SFrames);
SDL_SetPalette(SmallPs,SDL_LOGPAL,spiece->format->palette->colors,0,256);
}
// Create a single Small piece surface for usage when rending color
SSingleP=SDL_CreateRGBSurface(SDL_SWSURFACE, newpsize, newpsize, 8, SmallPs->format->Rmask, SmallPs->format->Gmask, SmallPs->format->Bmask, SmallPs->format->Amask);
SDL_SetPalette(SSingleP, SDL_LOGPAL, SmallPs->format->palette->colors, 0, 256);
return;
}
void Images::CreateLargeCs()
{
SDL_Rect src,dest;
// First create a surface
LargeCs=SDL_CreateRGBSurface(SDL_SWSURFACE,SingleP->w,SingleP->h * Frames, 8, LargePs->format->Rmask, LargePs->format->Gmask, LargePs->format->Bmask, LargePs->format->Amask);
SDL_SetPalette(LargeCs, SDL_LOGPAL, LargePs->format->palette->colors, 0, 256);
// Now blit the frames into place
dest.w=src.w=SingleP->w; dest.h=src.h=SingleP->h;
dest.x=src.x=0; dest.y=src.y=0;
for (int i=0; i < Frames;++i) {
SDL_BlitSurface(LargePs,&src,LargeCs,&dest);
src.y=src.y+SingleP->h*4;
dest.y=dest.y+SingleP->h;
}
// Now update the pixel information
SDL_Color* cols=LargeCs->format->palette->colors;
for (int x=0; x < LargeCs->w;++x)
for (int y=0; y < LargeCs->h;++y)
for (int i=128; i < 255;++i)
if (getpixel(LargeCs,x,y) == SDL_MapRGB(LargeCs->format, cols[i].r,cols[i].g,cols[i].b))
putpixel(LargeCs,x,y,SDL_MapRGB(LargeCs->format,cols[127].r,cols[127].g,cols[127].b));
return;
}
void Images::CreateSmallCs()
{
SDL_Rect src,dest;
// First create a surface
SmallCs=SDL_CreateRGBSurface(SDL_SWSURFACE,SSingleP->w,SingleP->h * SFrames, 8, SmallPs->format->Rmask, SmallPs->format->Gmask, SmallPs->format->Bmask, SmallPs->format->Amask);
SDL_SetPalette(SmallCs, SDL_LOGPAL, SmallPs->format->palette->colors, 0, 256);
// Now blit the frames into place
dest.w=src.w=SSingleP->w; dest.h=src.h=SSingleP->h;
dest.x=src.x=0; dest.y=src.y=0;
for (int i=0; i < SFrames;++i) {
SDL_BlitSurface(SmallPs,&src,SmallCs,&dest);
src.y=src.y+SSingleP->h*4;
dest.y=dest.y+SSingleP->h;
}
// Now update the pixel information
SDL_Color* cols=SmallCs->format->palette->colors;
for (int x=0; x < SmallCs->w;++x)
for (int y=0; y < SmallCs->h;++y)
for (int i=128; i < 255;++i)
if (getpixel(SmallCs,x,y) == SDL_MapRGB(SmallCs->format, cols[i].r,cols[i].g,cols[i].b))
putpixel(SmallCs,x,y,SDL_MapRGB(SmallCs->format,cols[127].r,cols[127].g,cols[127].b));
return;
}
void Images::CreateSpecials(SDL_Surface *specials)
{
int x=int(double(Gamedata->settings->X)/640 * 16 * 7);
int y=int(double(Gamedata->settings->Y)/480 * 16);
Specials=ResizeImageToFit(specials,x,y);
SDL_SetPalette(Specials,SDL_LOGPAL,specials->format->palette->colors,0,256);
SDL_Color *trans=Specials->format->palette->colors; // Set the last color to the color key
SDL_SetColorKey(Specials, SDL_SRCCOLORKEY, SDL_MapRGB(Specials->format,trans[255].r,trans[255].g,trans[255].b));
return;
}