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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
/*
graphic.cc
(c) Richard Thrippleton
Licensing terms are in the 'LICENSE' file
If that file is not included with this source then permission is not given to use this source in any way whatsoever.
*/
#include <SDL.h>
#include <string.h>
#include "SDL_rotozoom.h"
#include "SDL_gfxPrimitives.h"
#include "calc.h"
#include "error.h"
#include "interface.h"
#include "graphic.h"
void graphic::init()
{
screen=NULL;
for(int i=0;i<ISIZE;i++)
graphics[i]=NULL;
nd=0;
}
void graphic::setup(bool big,bool full)
{
Uint32 flags; //Flags for setting video;
SDL_Surface* tmp; //Temporary holding place while sprites are converted
char* path; //Path to load bmp from
if(SDL_InitSubSystem(SDL_INIT_VIDEO)==-1)
throw error(SDL_GetError());
flags=SDL_DOUBLEBUF|SDL_HWSURFACE;
if(full)
flags=flags|SDL_FULLSCREEN;
if(big)
screen=SDL_SetVideoMode(800,600,0,flags);
else
screen=SDL_SetVideoMode(600,400,0,flags);
if(!screen)
throw error(SDL_GetError());
crct.x=0;
crct.y=0;
crct.w=screen->w;
crct.h=screen->h;
SDL_WM_SetCaption("Star Voyager","Star Voyager");
SDL_ShowCursor(0);
cols[BLACK]=SDL_MapRGB(screen->format,0,0,0);
cols[RED]=SDL_MapRGB(screen->format,255,0,0);
cols[LIGHTRED]=SDL_MapRGB(screen->format,255,100,100);
cols[GREEN]=SDL_MapRGB(screen->format,0,255,0);
cols[LIGHTGREEN]=SDL_MapRGB(screen->format,100,255,100);
cols[BLUE]=SDL_MapRGB(screen->format,0,0,255);
cols[LIGHTBLUE]=SDL_MapRGB(screen->format,100,100,255);
cols[YELLOW]=SDL_MapRGB(screen->format,255,255,0);
cols[ORANGE]=SDL_MapRGB(screen->format,255,100,100);
cols[PURPLE]=SDL_MapRGB(screen->format,255,0,255);
cols[GREY]=SDL_MapRGB(screen->format,180,180,180);
cols[DARKGREY]=SDL_MapRGB(screen->format,64,64,64);
cols[WHITE]=SDL_MapRGB(screen->format,255,255,255);
path=new char[strlen(DATADIR)+32];
sprintf(path,"%s/gfx/font.bmp",DATADIR);
font=SDL_LoadBMP(path);
delete[] path;
if(!font)
throw error(SDL_GetError());
tmp=SDL_ConvertSurface(font,screen->format,SDL_SWSURFACE);
SDL_FreeSurface(font);
font=tmp;
SDL_SetColorKey(font,SDL_SRCCOLORKEY|SDL_RLEACCEL,cols[BLACK]);
path=new char[strlen(DATADIR)+32];
sprintf(path,"%s/gfx/haze.bmp",DATADIR);
cloak=SDL_LoadBMP(path);
delete[] path;
if(!cloak)
throw error(SDL_GetError());
tmp=SDL_ConvertSurface(cloak,screen->format,SDL_SWSURFACE);
SDL_FreeSurface(cloak);
cloak=tmp;
SDL_SetColorKey(cloak,SDL_SRCCOLORKEY|SDL_RLEACCEL,cols[WHITE]);
}
void graphic::blit()
{
SDL_Flip(screen);
}
graphic* graphic::get(int indx)
{
if(!(indx>=0 && indx<ISIZE))
return NULL;
if(!graphics[indx])
{
graphics[indx]=new graphic(indx);
}
return graphics[indx];
}
void graphic::string(char* str,int x,short y,bool opq)
{
SDL_Rect srct,drct; //Source rect and destination rect
int i; //Position in text
char c; //Character in use
char l; //Letter to print
if(opq)
{
drct.x=x;
drct.y=y;
drct.w=6*strlen(str);
drct.h=5;
SDL_FillRect(screen,&drct,cols[BLACK]);
}
i=0;
srct.y=1;
srct.w=5;
srct.h=5;
drct.x=x;
drct.y=y;
drct.w=5;
drct.h=5;
while(str[i]!=0)
{
c=str[i];
if(c=='\n')
c=32;
if(c>32 && c<127)
{
l=c-33;
srct.x=l*6+1;
SDL_BlitSurface(font,&srct,screen,&drct);
drct.x+=6;
}
if(c==' ')
drct.x+=6;
i++;
}
srct.x=x;
srct.y=y;
srct.w=drct.x-x;
srct.h=5;
if(nd<1024)
{
dtyp[nd]=DTYP_RECT;
dpos[nd]=srct;
nd++;
}
}
void graphic::box(sbox* box,int col)
{
SDL_Rect rect; //Rectangle to draw
rect.x=box->x;
rect.y=box->y;
rect.w=box->w;
rect.h=box->h;
SDL_FillRect(screen,&rect,cols[col]);
if(nd<1024)
{
dtyp[nd]=DTYP_RECT;
dpos[nd]=rect;
nd++;
}
}
void graphic::clip(sbox* box)
{
SDL_Rect rect; //Rectangle to clip with
rect.x=box->x;
rect.y=box->y;
rect.w=box->w;
rect.h=box->h;
crct=rect;
SDL_SetClipRect(screen,&rect);
if(nd<1024)
{
dtyp[nd]=DTYP_CLIP;
dpos[nd]=rect;
nd++;
}
}
void graphic::pix(int x,short y,short col)
{
fastPixelColor(screen,x,y,cols[col]);
if(nd<1024)
{
dtyp[nd]=DTYP_PIX;
dpos[nd].x=x;
dpos[nd].y=y;
nd++;
}
}
sbox graphic::dimension()
{
sbox out; //Value to output
out.x=0;
out.y=0;
out.w=screen->w;
out.h=screen->h;
return out;
}
void graphic::line(int x1,short y1,short x2,short y2,short col)
{
if(col>=0 && col<16)
{
lineColor(screen,x1,y1,x2,y2,cols[col]);
if(nd<1024)
{
dtyp[nd]=DTYP_LINE;
dpos[nd].x=x1;
dpos[nd].y=y1;
dpos[nd].w=x2;
dpos[nd].h=y2;
nd++;
}
}
}
void graphic::draw(int x,short y,short rot,short zout,short haze,bool trg)
{
graphic* tspr; //Targetting sprite
SDL_Rect dst; //Destination rect
int hw,hh; //Half-width and half-height, for centering
if(miss)
{
string("Graphic missing",x,y,true);
}
else
{
if(!(rot>=0 && rot<36 && zout>=0 && zout<4))
return;
if(!rots[rot][zout-1])
calculate(rot,zout);
hw=(rots[rot][zout-1]->w)/2;
hh=(rots[rot][zout-1]->h)/2;
dst.x=x-hw;
dst.y=y-hh;
dst.w=hw*2;
dst.h=hh*2;
SDL_BlitSurface(rots[rot][zout-1],NULL,screen,&dst);
if(trg)
{
tspr=get(TRG);
if(tspr)
{
tspr->draw(x,y-hh,0,1,0,false);
tspr->draw(x,y+hh,18,1,0,false);
tspr->draw(x-hw,y,27,1,0,false);
tspr->draw(x+hw,y,9,1,0,false);
}
}
if(nd<1024)
{
dtyp[nd]=DTYP_RECT;
dpos[nd]=dst;
nd++;
}
if(haze>0 && hw*2>cloak->w && hh*2>cloak->h)
{
dst.w=2;
dst.h=2;
for(int i=0;i<haze;i++)
{
dst.x=x-hw+calc::rnd(hw*2-cloak->w);
dst.y=y-hh+calc::rnd(hh*2-cloak->h);
SDL_BlitSurface(cloak,NULL,screen,&dst);
}
}
}
}
void graphic::embed()
{
nd=0;
}
void graphic::clean()
{
int j; //Loop limiter
j=nd;
SDL_SetClipRect(screen,NULL);
for(int i=0;i<nd;i++)
{
switch(dtyp[i])
{
case DTYP_CLIP:
SDL_SetClipRect(screen,&dpos[i]);
break;
case DTYP_PIX:
fastPixelColor(screen,dpos[i].x,dpos[i].y,cols[BLACK]);
break;
case DTYP_LINE:
lineColor(screen,dpos[i].x,dpos[i].y,dpos[i].w,dpos[i].h,cols[BLACK]);
break;
case DTYP_RECT:
SDL_FillRect(screen,&dpos[i],cols[BLACK]);
break;
}
}
nd=0;
}
graphic::graphic(int indx)
{
self=indx;
miss=false;
for(int i=0;i<36;i++)
for(int j=0;j<4;j++)
rots[i][j]=NULL;
try
{
load();
}
catch(error it)
{
miss=true;
}
}
void graphic::load()
{
SDL_Surface* tmp; //Temporary holding place while sprites are converted
char* path; //Path to load sprite from
path=new char[strlen(DATADIR)+32];
sprintf(path,"%s/gfx/%i.bmp",DATADIR,self);
orig=SDL_LoadBMP(path);
delete[] path;
if(!orig)
throw error(SDL_GetError());
tmp=SDL_ConvertSurface(orig,screen->format,SDL_SWSURFACE);
SDL_FreeSurface(orig);
orig=tmp;
SDL_SetColorKey(orig,SDL_SRCCOLORKEY|SDL_RLEACCEL,cols[BLACK]);
calculate(0,1);
}
void graphic::calculate(int rot,short zout)
{
int ang; //Angle to rotate by
ang=360-rot*10;
if(self>=16)
rots[rot][zout-1]=rotozoomSurface(orig,ang,1.0/zout,1);
else
rots[rot][zout-1]=rotozoomSurface(orig,ang,1.0/zout,0);
SDL_SetColorKey(rots[rot][zout-1],SDL_SRCCOLORKEY|SDL_RLEACCEL,cols[BLACK]);
if(!rots[rot][zout-1])
throw error(SDL_GetError());
}
graphic* graphic::graphics[ISIZE];
SDL_Surface* graphic::screen;
SDL_Surface* graphic::font;
SDL_Surface* graphic::cloak;
unsigned long graphic::cols[16];
int graphic::nd;
int graphic::dtyp[1024];
SDL_Rect graphic::dpos[1024];
SDL_Rect graphic::crct;
|