PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/RTS2/graphics.c

https://github.com/vscuorzo/mandest
C | 862 lines | 722 code | 61 blank | 79 comment | 91 complexity | 5d43f42fb79067d58e35a041bace88ae MD5 | raw file
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include "graphics.h"
  4. #define MaxSprites 255
  5. #define ERR_SPRITE "spr/esprite.png" // Used as default sprite
  6. #define errlog "error.txt"
  7. struct
  8. {
  9. Uint32 state;
  10. Uint32 shown;
  11. Uint32 frame;
  12. Uint16 x, y;
  13. }Mouse;
  14. SDL_Surface *screen; /*pointer to the draw buffer*/
  15. SDL_Surface *buffer; /*pointer to the background image buffer*/
  16. SDL_Surface *videobuffer; /*pointer to the actual video surface*/
  17. SDL_Rect Camera; /*x & y are the coordinates for the background map, w and h are of the screen*/
  18. Sprite SpriteList[MaxSprites];
  19. Sprite *Msprite;
  20. int NumSprites;
  21. Uint32 NOW; /*the current time since program started*/
  22. /*some data on the video settings that can be useful for a lot of functions*/
  23. Uint32 rmask,gmask,bmask,amask;
  24. ScreenData S_Data;
  25. int changeRes(int w, int h, int depth, Uint32 flags)
  26. {
  27. if( SDL_VideoModeOK(w, h, depth, flags) )
  28. {
  29. fprintf(stderr,"Changing resolution to %i x %i...", w, h);
  30. SDL_FreeSurface(videobuffer);
  31. videobuffer = SDL_SetVideoMode(w, h, depth, flags);
  32. screen = SDL_DisplayFormat(videobuffer);
  33. Camera.w = w;
  34. Camera.h = h;
  35. fprintf(stderr,"done.\n");
  36. return 1;
  37. }
  38. return 0;
  39. }
  40. void Init_Graphics()
  41. {
  42. Uint32 Vflags = SDL_FULLSCREEN | SDL_ANYFORMAT;
  43. Uint32 HWflag = 0;
  44. SDL_Surface *temp;
  45. //SDL_Rect **reslist;
  46. S_Data.xres = 1024;
  47. S_Data.yres = 768;
  48. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  49. rmask = 0xff000000;
  50. gmask = 0x00ff0000;
  51. bmask = 0x0000ff00;
  52. amask = 0x000000ff;
  53. #else
  54. rmask = 0x000000ff;
  55. gmask = 0x0000ff00;
  56. bmask = 0x00ff0000;
  57. amask = 0xff000000;
  58. #endif
  59. if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO|SDL_DOUBLEBUF) < 0 )
  60. {
  61. fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
  62. exit(1);
  63. }
  64. atexit(SDL_Quit);
  65. reslist = SDL_ListModes(NULL,SDL_FULLSCREEN | SDL_HWSURFACE);
  66. // Tries max supported resolution first
  67. if(SDL_VideoModeOK(reslist[0]->w,reslist[0]->h, 32, SDL_FULLSCREEN | SDL_ANYFORMAT | SDL_HWSURFACE) )
  68. {
  69. S_Data.xres = reslist[0]->w;
  70. S_Data.yres = reslist[0]->h;
  71. S_Data.depth = 32;
  72. Vflags = SDL_FULLSCREEN | SDL_ANYFORMAT | SDL_HWSURFACE;
  73. HWflag = SDL_HWSURFACE;
  74. }
  75. else if(SDL_VideoModeOK(reslist[0]->w,reslist[0]->h, 16, SDL_FULLSCREEN | SDL_ANYFORMAT | SDL_HWSURFACE) )
  76. {
  77. S_Data.xres = reslist[0]->w;
  78. S_Data.yres = reslist[0]->h;
  79. S_Data.depth = 16;
  80. Vflags = SDL_FULLSCREEN | SDL_ANYFORMAT | SDL_HWSURFACE;
  81. HWflag = SDL_HWSURFACE;
  82. }
  83. else if(SDL_VideoModeOK(1024, 768, 32, SDL_FULLSCREEN | SDL_ANYFORMAT | SDL_HWSURFACE))
  84. {
  85. S_Data.xres = 1024;
  86. S_Data.yres = 768;
  87. S_Data.depth = 32;
  88. Vflags = SDL_FULLSCREEN | SDL_ANYFORMAT | SDL_HWSURFACE;
  89. HWflag = SDL_HWSURFACE;
  90. }
  91. else if(SDL_VideoModeOK(1024, 768, 16, SDL_FULLSCREEN | SDL_ANYFORMAT | SDL_HWSURFACE))
  92. {
  93. S_Data.xres = 1024;
  94. S_Data.yres = 768;
  95. S_Data.depth = 16;
  96. Vflags = SDL_RESIZABLE | SDL_ANYFORMAT | SDL_HWSURFACE;
  97. HWflag = SDL_HWSURFACE;
  98. }
  99. else if(SDL_VideoModeOK(1024, 768, 16, SDL_FULLSCREEN | SDL_ANYFORMAT))
  100. {
  101. S_Data.xres = 1024;
  102. S_Data.yres = 768;
  103. S_Data.depth = 16;
  104. Vflags = SDL_RESIZABLE | SDL_ANYFORMAT;
  105. HWflag = SDL_SWSURFACE;
  106. }
  107. videobuffer = SDL_SetVideoMode(S_Data.xres, S_Data.yres,S_Data.depth, Vflags);
  108. if ( videobuffer == NULL )
  109. {
  110. fprintf(stderr, "Unable to set 1024x768 video: %s\n", SDL_GetError());
  111. exit(1);
  112. }
  113. temp = SDL_CreateRGBSurface(SDL_HWSURFACE, S_Data.xres, S_Data.yres, S_Data.depth,rmask, gmask,bmask,amask);
  114. if(temp == NULL)
  115. {
  116. fprintf(stderr,"Couldn't initialize background buffer: %s\n", SDL_GetError());
  117. exit(1);
  118. }
  119. /* Just to make sure that the surface we create is compatible with the screen*/
  120. screen = SDL_DisplayFormat(temp);
  121. SDL_FreeSurface(temp);
  122. temp = SDL_CreateRGBSurface(Vflags, 2048, 768, S_Data.depth,rmask, gmask,bmask,amask);
  123. if(temp == NULL)
  124. {
  125. fprintf(stderr,"Couldn't initialize Video buffer: %s\n", SDL_GetError());
  126. exit(1);
  127. }
  128. buffer = SDL_DisplayFormat(temp);
  129. SDL_FreeSurface(temp);
  130. Camera.x = 0;
  131. Camera.y = 0;
  132. Camera.w = screen->w;/*we want to make sure that our camera is the same size of the video screen*/
  133. Camera.h = screen->h;
  134. SDL_ShowCursor(SDL_DISABLE);/*don't show the mouse */
  135. SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
  136. }
  137. void ResetBuffer()
  138. {
  139. SDL_BlitSurface(buffer,&Camera,screen,NULL);
  140. }
  141. void NextFrame()
  142. {
  143. Uint32 Then;
  144. SDL_BlitSurface(screen,NULL,videobuffer,NULL);/*copy everything we did to the video surface*/
  145. SDL_Flip(videobuffer); /*and then update the screen*/
  146. Then = NOW; /*these next few lines are used to show how long each frame takes to update. */
  147. NOW = SDL_GetTicks();
  148. // fprintf(stdout,"Ticks passed this frame: %i\n", NOW - Then);
  149. FrameDelay(20); /*FrameDelay(33); this will make your frame rate about 30 frames per second. If you want 60 fps then set it to about 15 or 16*/
  150. }
  151. /*
  152. InitSpriteList is called when the program is first started.
  153. It just sets everything to zero and sets all pointers to NULL.
  154. It should never be called again.
  155. */
  156. void InitSpriteList()
  157. {
  158. int x;
  159. NumSprites = 0;
  160. memset(SpriteList,0,sizeof(Sprite) * MaxSprites);
  161. for(x = 0;x < MaxSprites;x++)SpriteList[x].image = NULL;
  162. }
  163. /*Create a sprite from a file with an alpha channel*/
  164. Sprite *LoadSpriteAlpha(char *filename,int sizex, int sizey)
  165. {
  166. int i;
  167. SDL_Surface *temp;
  168. /*first search to see if the requested sprite image is already loaded*/
  169. for(i = 0; i < NumSprites; i++)
  170. {
  171. if(strncmp(filename,SpriteList[i].filename,20)==0)
  172. {
  173. SpriteList[i].used++;
  174. return &SpriteList[i];
  175. }
  176. }
  177. /*makesure we have the room for a new sprite*/
  178. if(NumSprites + 1 >= MaxSprites)
  179. {
  180. fprintf(stderr, "Maximum Sprites Reached.\n");
  181. filename = ERR_SPRITE; //exit(1);
  182. }
  183. /*if its not already in memory, then load it.*/
  184. NumSprites++;
  185. for(i = 0;i <= NumSprites;i++)
  186. {
  187. if(!SpriteList[i].used)break;
  188. }
  189. temp = IMG_Load(filename);
  190. if(temp == NULL)
  191. {
  192. fprintf(stderr,"The following sprite failed to load: %s\n",SDL_GetError());
  193. temp = IMG_Load(ERR_SPRITE); //exit(0);
  194. if(temp == NULL)
  195. {
  196. fprintf(stderr,"The default sprite cannot be loaded: %s\n",SDL_GetError());
  197. return NULL;
  198. }
  199. }
  200. SpriteList[i].image = SDL_DisplayFormatAlpha(temp);
  201. SDL_FreeSurface(temp);
  202. /*sets alpha channel to max by default*/
  203. SDL_SetAlpha(SpriteList[i].image, SDL_SRCALPHA|SDL_RLEACCEL, 255 );
  204. /*then copy the given information to the sprite*/
  205. strncpy(SpriteList[i].filename,filename,20);
  206. /*now sprites don't have to be 16 frames per line, but most will be.*/
  207. SpriteList[i].framesperline = 16;
  208. SpriteList[i].w = sizex;
  209. SpriteList[i].h = sizey;
  210. SpriteList[i].used++;
  211. return &SpriteList[i];
  212. }
  213. Sprite *LoadSprite(char *filename,int sizex, int sizey)
  214. {
  215. int i;
  216. SDL_Surface *temp;
  217. /*first search to see if the requested sprite image is already loaded*/
  218. for(i = 0; i < NumSprites; i++)
  219. {
  220. if(strncmp(filename,SpriteList[i].filename,20)==0)
  221. {
  222. SpriteList[i].used++;
  223. return &SpriteList[i];
  224. }
  225. }
  226. /*makesure we have the room for a new sprite*/
  227. if(NumSprites + 1 >= MaxSprites)
  228. {
  229. fprintf(stderr, "Maximum Sprites Reached.\n");
  230. filename = ERR_SPRITE; //exit(1);
  231. }
  232. /*if its not already in memory, then load it.*/
  233. NumSprites++;
  234. for(i = 0;i <= NumSprites;i++)
  235. {
  236. if(!SpriteList[i].used)break;
  237. }
  238. temp = IMG_Load(filename);
  239. if(temp == NULL)
  240. {
  241. fprintf(stderr,"The following sprite failed to load: %s\n",SDL_GetError());
  242. temp = IMG_Load(ERR_SPRITE); //exit(0);
  243. if(temp == NULL)
  244. {
  245. fprintf(stderr,"The default sprite cannot be loaded: %s\n",SDL_GetError());
  246. return NULL;
  247. }
  248. }
  249. SpriteList[i].image = SDL_DisplayFormat(temp);
  250. SDL_FreeSurface(temp);
  251. /*sets a transparent color for blitting.*/
  252. SDL_SetColorKey(SpriteList[i].image, SDL_SRCCOLORKEY, SDL_MapRGB(SpriteList[i].image->format, 255,255,255));
  253. /*then copy the given information to the sprite*/
  254. strncpy(SpriteList[i].filename,filename,20);
  255. /*now sprites don't have to be 16 frames per line, but most will be.*/
  256. SpriteList[i].framesperline = 16;
  257. SpriteList[i].w = sizex;
  258. SpriteList[i].h = sizey;
  259. SpriteList[i].used++;
  260. return &SpriteList[i];
  261. }
  262. /*the palette swapping version of LoadSprite. It will check the file loaded to see if there is any pure colors for swapping them.*/
  263. Sprite *LoadSwappedSprite(char *filename,int sizex, int sizey, int c1, int c2, int c3)
  264. {
  265. int i;
  266. SDL_Surface *temp;
  267. /*first search to see if the requested sprite image is alreday loaded*/
  268. for(i = 0; i < NumSprites; i++)
  269. {
  270. if((strncmp(filename,SpriteList[i].filename,20)==0)&&(SpriteList[i].used >= 1)&&(c1 == SpriteList[i].color1)&&(c2 == SpriteList[i].color2)&&(c3 == SpriteList[i].color3))
  271. {
  272. SpriteList[i].used++;
  273. return &SpriteList[i];
  274. }
  275. }
  276. /*makesure we have the room for a new sprite*/
  277. if(NumSprites + 1 >= MaxSprites)
  278. {
  279. fprintf(stderr, "Maximum Sprites Reached.\n");
  280. exit(1);
  281. }
  282. /*if its not already in memory, then load it.*/
  283. NumSprites++;
  284. for(i = 0;i <= NumSprites;i++)
  285. {
  286. if(!SpriteList[i].used)break;
  287. }
  288. temp = IMG_Load(filename);
  289. if(temp == NULL)
  290. {
  291. fprintf(stderr, "FAILED TO LOAD A VITAL Sprite.\n");
  292. exit(1);
  293. }
  294. SpriteList[i].image = SDL_DisplayFormat(temp);
  295. SDL_FreeSurface(temp);
  296. /*sets a transparent color for blitting.*/
  297. SDL_SetColorKey(SpriteList[i].image, SDL_SRCCOLORKEY , SDL_MapRGB(SpriteList[i].image->format, 255,255,255));
  298. //fprintf(stderr,"asked for colors: %d,%d,%d \n",c1,c2,c3);
  299. SwapSprite(SpriteList[i].image,c1,c2,c3);
  300. /*then copy the given information to the sprite*/
  301. strcpy(SpriteList[i].filename,filename);
  302. /*now sprites don't have to be 16 frames per line, but most will be.*/
  303. SpriteList[i].framesperline = 16;
  304. SpriteList[i].w = sizex;
  305. SpriteList[i].h = sizey;
  306. SpriteList[i].color1 = c1;
  307. SpriteList[i].color2 = c2;
  308. SpriteList[i].color3 = c3;
  309. SpriteList[i].used++;
  310. return &SpriteList[i];
  311. }
  312. /*
  313. * When we are done with a sprite, lets give the resources back to the system...
  314. * so we can get them again later.
  315. */
  316. void FreeSprite(Sprite *sprite)
  317. {
  318. /*first lets check to see if the sprite is still being used.*/
  319. sprite->used--;
  320. if(sprite->used == 0)
  321. {
  322. strcpy(sprite->filename,"\0");
  323. /*just to be anal retentive, check to see if the image is already freed*/
  324. if(sprite->image != NULL)SDL_FreeSurface(sprite->image);
  325. sprite->image = NULL;
  326. }
  327. /*and then lets make sure we don't leave any potential seg faults
  328. lying around*/
  329. }
  330. void CloseSprites()
  331. {
  332. int i;
  333. for(i = 0;i < MaxSprites;i++)
  334. {
  335. /*it shouldn't matter if the sprite is already freed,
  336. FreeSprite checks for that*/
  337. FreeSprite(&SpriteList[i]);
  338. }
  339. }
  340. void DrawSprite(Sprite *sprite,SDL_Surface *surface,int sx,int sy, int frame)
  341. {
  342. SDL_Rect src,dest;
  343. if(sprite == NULL || surface == NULL)
  344. {
  345. fprintf(stderr,"NULL was passed to DrawSprite()\n");
  346. return;
  347. }
  348. src.x = frame%sprite->framesperline * sprite->w;
  349. src.y = frame/sprite->framesperline * sprite->h;
  350. src.w = sprite->w;
  351. src.h = sprite->h;
  352. dest.x = sx;
  353. dest.y = sy;
  354. dest.w = sprite->w;
  355. dest.h = sprite->h;
  356. SDL_BlitSurface(sprite->image, &src, surface, &dest);
  357. }
  358. /*
  359. This will draw a pixel on the surface that is past at the x and y coordinates of the color given;
  360. */
  361. void DrawPixel(SDL_Surface *screen, Uint8 R, Uint8 G, Uint8 B, int x, int y)
  362. {
  363. Uint32 color = SDL_MapRGB(screen->format, R, G, B);
  364. if ( SDL_MUSTLOCK(screen) )
  365. {
  366. if ( SDL_LockSurface(screen) < 0 )
  367. {
  368. return;
  369. }
  370. }
  371. switch (screen->format->BytesPerPixel)
  372. {
  373. case 1:
  374. { /* Assuming 8-bpp */
  375. Uint8 *bufp;
  376. bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
  377. *bufp = color;
  378. }
  379. break;
  380. case 2:
  381. { /* Probably 15-bpp or 16-bpp */
  382. Uint16 *bufp;
  383. bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
  384. *bufp = color;
  385. }
  386. break;
  387. case 3:
  388. { /* Slow 24-bpp mode, usually not used */
  389. Uint8 *bufp;
  390. bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
  391. *(bufp+screen->format->Rshift/8) = R;
  392. *(bufp+screen->format->Gshift/8) = G;
  393. *(bufp+screen->format->Bshift/8) = B;
  394. }
  395. break;
  396. case 4:
  397. { /* Probably 32-bpp */
  398. Uint32 *bufp;
  399. bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
  400. *bufp = color;
  401. }
  402. break;
  403. }
  404. if ( SDL_MUSTLOCK(screen) )
  405. {
  406. SDL_UnlockSurface(screen);
  407. }
  408. SDL_UpdateRect(screen, x, y, 1, 1);
  409. }
  410. Uint32 getpixel(SDL_Surface *surface, int x, int y)
  411. {
  412. /* Here p is the address to the pixel we want to retrieve*/
  413. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel;
  414. switch(surface->format->BytesPerPixel)
  415. {
  416. case 1:
  417. return *p;
  418. case 2:
  419. return *(Uint16 *)p;
  420. case 3:
  421. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  422. return p[0] << 16 | p[1] << 8 | p[2];
  423. else
  424. return p[0] | p[1] << 8 | p[2] << 16;
  425. case 4:
  426. return *(Uint32 *)p;
  427. default:
  428. return 0; /*shouldn't happen, but avoids warnings*/
  429. }
  430. }
  431. /*
  432. * Set the pixel at (x, y) to the given value
  433. * NOTE: The surface must be locked before calling this!
  434. */
  435. void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
  436. {
  437. /* Here p is the address to the pixel we want to set */
  438. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel;
  439. switch(surface->format->BytesPerPixel)
  440. {
  441. case 1:
  442. *p = pixel;
  443. break;
  444. case 2:
  445. *(Uint16 *)p = pixel;
  446. break;
  447. case 3:
  448. if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
  449. p[0] = (pixel >> 16) & 0xff;
  450. p[1] = (pixel >> 8) & 0xff;
  451. p[2] = pixel & 0xff;
  452. } else {
  453. p[0] = pixel & 0xff;
  454. p[1] = (pixel >> 8) & 0xff;
  455. p[2] = (pixel >> 16) & 0xff;
  456. }
  457. break;
  458. case 4:
  459. *(Uint32 *)p = pixel;
  460. break;
  461. }
  462. }
  463. /*
  464. makes sure a minimum number of ticks is waited between frames
  465. this is to ensure that on faster machines the game won't move so fast that
  466. it will look terrible.
  467. This is a very handy function in game programming.
  468. */
  469. void FrameDelay(Uint32 delay)
  470. {
  471. static Uint32 pass = 100;
  472. Uint32 dif;
  473. dif = SDL_GetTicks() - pass;
  474. if(dif < delay)SDL_Delay( delay - dif);
  475. pass = SDL_GetTicks();
  476. }
  477. /*sets an sdl surface to all color.*/
  478. void BlankScreen(SDL_Surface *buf,Uint32 color)
  479. {
  480. SDL_LockSurface(buf);
  481. memset(buf->pixels, (Uint8)color,buf->format->BytesPerPixel * buf->w *buf->h);
  482. SDL_UnlockSurface(buf);
  483. }
  484. /*
  485. * This is the beginning of my Palette swapping scheme. It checks the value
  486. * of the color it is given to see if the given color is PURE red, PURE green,
  487. * or PURE blue. If it is, it takes the value as a percentage to apply to
  488. * the new color. It returns either the old color untouched (if it wasn't a
  489. * special case) or the new color.
  490. */
  491. Uint32 SetColor(Uint32 color, int newcolor1,int newcolor2, int newcolor3)
  492. {
  493. Uint8 r,g,b;
  494. Uint8 intensity;
  495. int newcolor;
  496. SDL_GetRGB(color, screen->format, &r, &g, &b);
  497. if((r == 0) && (g == 0)&&(b !=0))
  498. {
  499. intensity = b;
  500. newcolor = newcolor3;
  501. }
  502. else if((r ==0)&&(b == 0)&&(g != 0))
  503. {
  504. intensity = g;
  505. newcolor = newcolor2;
  506. }
  507. else if((g == 0)&&(b == 0)&&(r != 0))
  508. {
  509. intensity = r;
  510. newcolor = newcolor1;
  511. }
  512. else return color;
  513. switch(newcolor)
  514. {
  515. case Red:
  516. r = intensity;
  517. g = 0;
  518. b = 0;
  519. break;
  520. case Green:
  521. r = 0;
  522. g = intensity;
  523. b = 0;
  524. break;
  525. case Blue:
  526. r = 0;
  527. g = 0;
  528. b = intensity;
  529. break;
  530. case Yellow:
  531. r = (Uint8)intensity * 0.7;
  532. g = (Uint8)intensity * 0.7;
  533. b = 0;
  534. break;
  535. case Orange:
  536. r = (Uint8)intensity * 0.9;
  537. g = (Uint8)intensity * 0.4;
  538. b = (Uint8)intensity * 0.1;
  539. break;
  540. case Violet:
  541. r = (Uint8)intensity * 0.7;
  542. g = 0;
  543. b = (Uint8)intensity * 0.7;
  544. break;
  545. case Brown:
  546. r = (Uint8)intensity * 0.6;
  547. g = (Uint8)intensity * 0.3;
  548. b = (Uint8)intensity * 0.15;
  549. break;
  550. case Grey:
  551. r = (Uint8)intensity * 0.5;
  552. g = (Uint8)intensity * 0.5;
  553. b = (Uint8)intensity * 0.5;
  554. break;
  555. case DarkRed:
  556. r = (Uint8)intensity * 0.5;
  557. g = 0;
  558. b = 0;
  559. break;
  560. case DarkGreen:
  561. r = 0;
  562. g = (Uint8)intensity * 0.5;
  563. b = 0;
  564. break;
  565. case DarkBlue:
  566. r = 0;
  567. g = 0;
  568. b = (Uint8)intensity * 0.5;
  569. break;
  570. case DarkYellow:
  571. r = (Uint8)intensity * 0.4;
  572. g = (Uint8)intensity * 0.4;
  573. b = 0;
  574. break;
  575. case DarkOrange:
  576. r = (Uint8)intensity * 0.6;
  577. g = (Uint8)intensity * 0.2;
  578. b = (Uint8)intensity * 0.1;
  579. break;
  580. case DarkViolet:
  581. r = (Uint8)intensity * 0.4;
  582. g = 0;
  583. b = (Uint8)intensity * 0.4;
  584. break;
  585. case DarkBrown:
  586. r = (Uint8)intensity * 0.2;
  587. g = (Uint8)intensity * 0.1;
  588. b = (Uint8)intensity * 0.05;
  589. break;
  590. case DarkGrey:
  591. r = (Uint8)intensity * 0.3;
  592. g = (Uint8)intensity * 0.3;
  593. b = (Uint8)intensity * 0.3;
  594. break;
  595. case LightRed:
  596. r = intensity;
  597. g = (Uint8)intensity * 0.45;
  598. b = (Uint8)intensity * 0.45;
  599. break;
  600. case LightGreen:
  601. r = (Uint8)intensity * 0.45;
  602. g = intensity;
  603. b = (Uint8)intensity * 0.45;
  604. break;
  605. case LightBlue:
  606. r = (Uint8)intensity * 0.45;
  607. b = intensity;
  608. g = (Uint8)intensity * 0.45;
  609. break;
  610. case LightYellow:
  611. r = intensity;
  612. g = intensity;
  613. b = (Uint8)intensity * 0.45;
  614. break;
  615. case LightOrange:
  616. r = intensity;
  617. g = (Uint8)intensity * 0.75;
  618. b = (Uint8)intensity * 0.35;
  619. break;
  620. case LightViolet:
  621. r = intensity;
  622. g = (Uint8)intensity * 0.45;
  623. b = intensity;
  624. break;
  625. case LightBrown:
  626. r = intensity;
  627. g = (Uint8)intensity * 0.85;
  628. b = (Uint8)intensity * 0.45;
  629. break;
  630. case LightGrey:
  631. r = (Uint8)intensity * 0.85;
  632. g = (Uint8)intensity * 0.85;
  633. b = (Uint8)intensity * 0.85;
  634. break;
  635. case Black:
  636. r = (Uint8)intensity * 0.15;
  637. g = (Uint8)intensity * 0.15;
  638. b = (Uint8)intensity * 0.15;
  639. break;
  640. case White:
  641. r = intensity;
  642. g = intensity;
  643. b = intensity;
  644. break;
  645. case Tan:
  646. r = intensity;
  647. g = (Uint8)intensity * 0.9;
  648. b = (Uint8)intensity * 0.6;
  649. break;
  650. case Gold:
  651. r = (Uint8)intensity * 0.8;
  652. g = (Uint8)intensity * 0.7;
  653. b = (Uint8)intensity * 0.2;
  654. break;
  655. case Silver:
  656. r = (Uint8)intensity * 0.95;
  657. g = (Uint8)intensity * 0.95;
  658. b = intensity;
  659. break;
  660. case YellowGreen:
  661. r = (Uint8)intensity * 0.45;
  662. g = (Uint8)intensity * 0.75;
  663. b = (Uint8)intensity * 0.2;
  664. break;
  665. case Cyan:
  666. r = 0;
  667. g = (Uint8)intensity * 0.85;
  668. b = (Uint8)intensity * 0.85;
  669. break;
  670. case Magenta:
  671. r = (Uint8)intensity * 0.7;
  672. g = 0;
  673. b = (Uint8)intensity * 0.7;
  674. break;
  675. }
  676. return SDL_MapRGB(screen->format,r,g,b);
  677. }
  678. /* This will probably never have to be called, returns the hex code for the
  679. * enumerated color
  680. */
  681. Uint32 IndexColor(int color)
  682. {
  683. switch(color)
  684. {
  685. case Red:
  686. return Red_;
  687. case Green:
  688. return Green_;
  689. case Blue:
  690. return Blue_;
  691. case Yellow:
  692. return Yellow_;
  693. case Orange:
  694. return Orange_;
  695. case Violet:
  696. return Violet_;
  697. case Brown:
  698. return Brown_;
  699. case Grey:
  700. return Grey_;
  701. case DarkRed:
  702. return DarkRed_;
  703. case DarkGreen:
  704. return DarkGreen_;
  705. case DarkBlue:
  706. return DarkBlue_;
  707. case DarkYellow:
  708. return DarkYellow_;
  709. case DarkOrange:
  710. return DarkOrange_;
  711. case DarkViolet:
  712. return DarkViolet_;
  713. case DarkBrown:
  714. return DarkBrown_;
  715. case DarkGrey:
  716. return DarkGrey_;
  717. case LightRed:
  718. return LightRed_;
  719. case LightGreen:
  720. return LightGreen_;
  721. case LightBlue:
  722. return LightBlue_;
  723. case LightYellow:
  724. return LightYellow_;
  725. case LightOrange:
  726. return LightOrange_;
  727. case LightViolet:
  728. return LightViolet_;
  729. case LightBrown:
  730. return LightBrown_;
  731. case LightGrey:
  732. return LightGrey_;
  733. case Black:
  734. return Black_;
  735. case White:
  736. return White_;
  737. case Tan:
  738. return Tan_;
  739. case Gold:
  740. return Gold_;
  741. case Silver:
  742. return Silver_;
  743. case YellowGreen:
  744. return YellowGreen_;
  745. case Cyan:
  746. return Cyan_;
  747. case Magenta:
  748. return Magenta_;
  749. }
  750. return Black_;
  751. }
  752. /*
  753. * and now bringing it all together, we swap the pure colors in the sprite out
  754. * and put the new colors in. This maintains any of the artist's shading and
  755. * detail, but still lets us have that old school palette swapping.
  756. */
  757. void SwapSprite(SDL_Surface *sprite,int color1,int color2,int color3)
  758. {
  759. int x, y;
  760. Uint32 pixel;
  761. /*First the precautions, that are tedious, but necessary*/
  762. if(color1 == -1)return;
  763. if(sprite == NULL)return;
  764. if ( SDL_LockSurface(sprite) < 0 )
  765. {
  766. fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
  767. exit(1);
  768. }
  769. /*now step through our sprite, pixel by pixel*/
  770. for(y = 0;y < sprite->h ;y++)
  771. {
  772. for(x = 0;x < sprite->w ;x++)
  773. {
  774. pixel = getpixel(sprite,x,y);/*and swap it*/
  775. putpixel(sprite,x,y,SetColor(pixel,color1,color2,color3));
  776. }
  777. }
  778. SDL_UnlockSurface(sprite);
  779. }
  780. /*mouse handling functions*/
  781. /*this only handles the drawing and animation of. Assuming you have a 16 by 16 tiled sprite sheet. This will not handle input*/
  782. void InitMouse()
  783. {
  784. Msprite = LoadSprite("images/mouse.png",16, 16);
  785. if(Msprite == NULL)fprintf(stdout,"mouse didn't load\n");
  786. Mouse.state = 0;
  787. Mouse.shown = 0;
  788. Mouse.frame = 0;
  789. }
  790. /*draws to the screen immediately before the blit, after all
  791. it wouldn't be a very good mouse if it got covered up by the
  792. game content*/
  793. void DrawMouse()
  794. {
  795. int mx,my;
  796. SDL_GetMouseState(&mx,&my);
  797. if(Msprite != NULL) DrawSprite(Msprite,screen,mx,my,Mouse.frame);
  798. Mouse.frame = (Mouse.frame + 1)%16;
  799. Mouse.x = mx;
  800. Mouse.y = my;
  801. }