You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

873 lines
13KB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include "bomber.h"
  6. #include "gfx.h"
  7. #define MAXCOLORS 256
  8. int usedcolors=0;
  9. SDL_Surface *thescreen;
  10. SDL_Color themap[256];
  11. uchar *videomem;
  12. int stride;
  13. int mousex,mousey,mouseb;
  14. uchar mustlock=0,locked=0;
  15. uchar *block64;
  16. int buttonstate=0,buttondown=0;
  17. int mxpos,mypos;
  18. int pressedcodes[KEYMAX],downcodes[KEYMAX],numpressed,numdown;
  19. void dumpgfx()
  20. {
  21. usedcolors = 0;
  22. }
  23. static int bestmatch(int red,int green,int blue)
  24. {
  25. int i;
  26. int bestcolor,bestdelta=0;
  27. int rdelta,gdelta,bdelta;
  28. int delta;
  29. i=0;
  30. bestcolor=-1;
  31. while(i<usedcolors)
  32. {
  33. rdelta=themap[i].r;
  34. rdelta-=red;
  35. rdelta*=rdelta;
  36. gdelta=themap[i].g;
  37. gdelta-=green;
  38. gdelta*=gdelta;
  39. bdelta=themap[i].b;
  40. bdelta-=blue;
  41. bdelta*=bdelta;
  42. delta=rdelta+gdelta+bdelta;
  43. if(bestcolor<0 || delta<bestdelta)
  44. {
  45. bestdelta=delta;
  46. bestcolor=i;
  47. }
  48. i++;
  49. }
  50. return bestcolor;
  51. }
  52. static void updatemap(void)
  53. {
  54. SDL_SetColors(thescreen, themap, 0, 256);
  55. }
  56. void createinout(gfxset *gs)
  57. {
  58. uchar *p;
  59. int i,j,counts[256];
  60. uchar red,green,blue;
  61. int cnt;
  62. p=gs->gs_pic;
  63. for(i=0;i<256;i++) counts[i]=0;
  64. i=gs->gs_xsize*gs->gs_ysize;
  65. while(i--)
  66. counts[*p++]++;
  67. cnt=0;
  68. gs->gs_inout[0]=0;
  69. for(i=1;i<256;i++)
  70. {
  71. if(counts[i])
  72. {
  73. cnt++;
  74. p=gs->gs_colormap+i+i+i;
  75. red=*p++;
  76. green=*p++;
  77. blue=*p++;
  78. for(j=0;j<usedcolors;j++)
  79. {
  80. if(red==themap[j].r &&
  81. green==themap[j].g && blue==themap[j].b)
  82. {
  83. gs->gs_inout[i]=j;
  84. break;
  85. }
  86. }
  87. if(j==usedcolors)
  88. {
  89. if(usedcolors<MAXCOLORS)
  90. {
  91. themap[j].r=red;
  92. themap[j].g=green;
  93. themap[j].b=blue;
  94. gs->gs_inout[i]=usedcolors;
  95. ++usedcolors;
  96. } else
  97. gs->gs_inout[i]=bestmatch(red,green,blue);
  98. }
  99. }
  100. }
  101. updatemap();
  102. }
  103. static uchar *compressfig(uchar *put,gfxset *gs,
  104. int sourcex,int sourcey,int sizex,int sizey)
  105. {
  106. int j,gswidth;
  107. uchar *p, *p2,pixel,*map1;
  108. int dx,dy;
  109. gswidth=gs->gs_xsize;
  110. map1=gs->gs_inout;
  111. p=gs->gs_pic+sourcex+gswidth*sourcey;
  112. for(dy=0;dy<sizey;dy++)
  113. {
  114. p2=p;
  115. j=0;
  116. for(dx=0;dx<=sizex;dx++)
  117. {
  118. if(dx<sizex) pixel=*p2;
  119. else pixel=0;
  120. ++p2;
  121. if(pixel)
  122. ++j;
  123. else if(j)
  124. {
  125. *put++=j;
  126. *put++=dx-j;
  127. *put++=dy;
  128. p2-=j+1;
  129. while(j)
  130. *put++=map1[*p2++],--j;
  131. ++p2;
  132. }
  133. }
  134. p+=gswidth;
  135. }
  136. *put++=0;
  137. return put;
  138. }
  139. static void gfxfetchsingle(figure *fig,gfxset *gs,int sourcex,int sourcey,int sizex,int sizey)
  140. {
  141. uchar *p,*p2;
  142. int dx,dy;
  143. uchar *map1;
  144. int gswidth;
  145. int minx,miny,maxx,maxy;
  146. int tx,ty;
  147. map1=gs->gs_inout;
  148. gswidth=gs->gs_xsize;
  149. p=gs->gs_pic+sourcex+gswidth*sourcey;
  150. minx=miny=maxx=maxy=-1;
  151. for(dy=0;dy<sizey;dy++)
  152. {
  153. p2=p;
  154. ty=sourcey+dy;
  155. for(dx=0;dx<sizex;dx++)
  156. {
  157. if(!*p2++) continue;
  158. if(miny==-1 || ty<miny) miny=ty;
  159. if(maxy==-1 || ty>maxy) maxy=ty;
  160. tx=sourcex+dx;
  161. if(minx==-1 || tx<minx) minx=tx;
  162. if(maxx==-1 || tx>maxx) maxx=tx;
  163. }
  164. p+=gswidth;
  165. }
  166. if(minx==-1)
  167. {
  168. minx=maxx=sourcex;
  169. miny=maxy=sourcey;
  170. }
  171. fig->xdelta=minx-sourcex;
  172. fig->ydelta=miny-sourcey;
  173. sourcex=minx;
  174. sourcey=miny;
  175. fig->xsize=sizex=maxx-minx+1;
  176. fig->ysize=sizey=maxy-miny+1;
  177. p=compressfig(block64,gs,sourcex,sourcey,sizex,sizey);
  178. fig->graphics=malloc(p-block64);
  179. if(fig->graphics)
  180. memcpy(fig->graphics,block64,p-block64);
  181. }
  182. //(gfxset *gs,figure *fig,int sourcex,int sourcey,int sizex,int sizey)
  183. void gfxfetch(gfxset *gs,figure *fig,int num)
  184. {
  185. int x,y;
  186. int xsize,ysize;
  187. int fxsize,fysize;
  188. unsigned char *p,*p2;
  189. xsize=gs->gs_xsize;
  190. ysize=gs->gs_ysize;
  191. p2=p=gs->gs_pic+xsize+1;
  192. fxsize=2;
  193. while(*p++==0) ++fxsize;
  194. fysize=2;
  195. while(*p2==0) ++fysize,p2+=xsize;
  196. x=fxsize;
  197. y=0;
  198. while(num--)
  199. {
  200. gfxfetchsingle(fig,gs,x,y,fxsize,fysize);
  201. x+=fxsize;
  202. if(x>xsize-fxsize)
  203. {
  204. x=0;
  205. y+=fysize;
  206. if(y>ysize-fysize)
  207. y=0;
  208. }
  209. fig++;
  210. }
  211. }
  212. void solidfetch(gfxset *gs,solid *dest)
  213. {
  214. int xsize,ysize;
  215. int i;
  216. unsigned char *p,*map;
  217. uchar *gfx;
  218. memset(dest,0,sizeof(solid));
  219. xsize=gs->gs_xsize;
  220. ysize=gs->gs_ysize;
  221. i=xsize*ysize;
  222. gfx=dest->graphics=malloc(i);
  223. if(!gfx) return;
  224. dest->xsize=xsize;
  225. dest->ysize=ysize;
  226. map=gs->gs_inout;
  227. memcpy(gfx,gs->gs_pic,i);
  228. p=gfx;
  229. while(i--)
  230. {
  231. if(*p) *p=map[*p];
  232. ++p;
  233. }
  234. }
  235. void solidcopyany(solid *src,solid *dest,int destx,int desty,int sizex,int sizey)
  236. {
  237. int xmax,ymax;
  238. int j;
  239. uchar *p1,*p2;
  240. int w;
  241. xmax=src->xsize;
  242. ymax=src->ysize;
  243. if(destx>=xmax || desty>=ymax || destx+sizex<=0 || desty+sizey<=0)
  244. return;
  245. if(destx<0)
  246. {
  247. sizex+=destx;
  248. destx=0;
  249. }
  250. if(desty<0)
  251. {
  252. sizey+=desty;
  253. desty=0;
  254. }
  255. if(destx+sizex>xmax)
  256. sizex=xmax-destx;
  257. if(desty+sizey>ymax)
  258. sizey=ymax-desty;
  259. if(dest)
  260. {
  261. w=dest->xsize;
  262. p1=dest->graphics+desty*w+destx;
  263. } else
  264. {
  265. gfxlock();
  266. w=stride;
  267. p1=videomem+desty*stride+destx;
  268. }
  269. p2=src->graphics+desty*xmax+destx;
  270. for(j=0;j<sizey;++j)
  271. {
  272. memcpy(p1,p2,sizex);
  273. p1+=w;
  274. p2+=xmax;
  275. }
  276. }
  277. void solidcopy(solid *solid,int destx,int desty,int sizex,int sizey)
  278. {
  279. solidcopyany(solid,0,destx,desty,sizex,sizey);
  280. }
  281. void drawfigureany(int x,int y,figure *fig,solid *dest)
  282. {
  283. int run;
  284. int dx,dy;
  285. int xsize,ysize;
  286. int clipx,clipy,w;
  287. unsigned char *pc,*p,*p2,*take;
  288. take=fig->graphics;
  289. if(dest)
  290. {
  291. w=clipx=dest->xsize;
  292. clipy=dest->ysize;
  293. pc=dest->graphics;
  294. } else
  295. {
  296. gfxlock();
  297. w=stride;
  298. clipx=IXSIZE;
  299. clipy=IYSIZE;
  300. pc=videomem;
  301. }
  302. dx=fig->xdelta;
  303. dy=fig->ydelta;
  304. xsize=fig->xsize;
  305. ysize=fig->ysize;
  306. x+=dx;
  307. y+=dy;
  308. if(x>=0 && y>=0 && x<=clipx-xsize && y<=clipy-ysize)
  309. {
  310. while((run=*take++))
  311. {
  312. dx=*((signed char *)take); ++take;
  313. dy=*((signed char *)take); ++take;
  314. p=pc+w*(y+dy)+x+dx;
  315. while(run--)
  316. *p++=*take++;
  317. }
  318. } else
  319. {
  320. while((run=*take++))
  321. {
  322. dx=*((signed char *)take); ++take;
  323. dy=*((signed char *)take); ++take;
  324. dx+=x;
  325. dy+=y;
  326. p2=take;
  327. take+=run;
  328. if(dy<0 || dy>=clipy) continue;
  329. if(dx>=clipx) continue;
  330. if(dx<0)
  331. {
  332. p2-=dx;
  333. run+=dx;
  334. dx=0;
  335. } else if(dx+run>clipx)
  336. run=clipx-dx;
  337. p=pc+w*dy+dx;
  338. if(run)
  339. memcpy(p,p2,run);
  340. }
  341. }
  342. }
  343. void drawfigure(int destx,int desty,figure *fig)
  344. {
  345. drawfigureany(destx,desty,fig,0);
  346. }
  347. void copyup()
  348. {
  349. gfxunlock();
  350. SDL_UpdateRect(thescreen, 0, 0, 0, 0);
  351. needwhole=0;
  352. }
  353. void copyupxy(int x,int y)
  354. {
  355. gfxunlock();
  356. SDL_UpdateRect(thescreen,x,y,24,24);
  357. }
  358. void copyupxysize(int x,int y,int xsize,int ysize)
  359. {
  360. gfxunlock();
  361. SDL_UpdateRect(thescreen,x,y,xsize,ysize);
  362. }
  363. // static void set_color(int color, int red, int green, int blue) {
  364. // themap[color].r=red;
  365. // themap[color].g=green;
  366. // themap[color].b=blue;
  367. // }
  368. void opengfx(int argc, char **argv) {
  369. unsigned long videoflags;
  370. themap[0].r=0;
  371. themap[0].g=0;
  372. themap[0].b=0;
  373. usedcolors=1;
  374. block64=malloc(65536);
  375. if(!block64)
  376. {
  377. printf("Couldn't allocate block64\n");
  378. exit(50);
  379. }
  380. if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO) < 0 )
  381. {
  382. fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
  383. exit(1);
  384. }
  385. videoflags = SDL_SWSURFACE|SDL_HWPALETTE; //|SDL_FULLSCREEN;
  386. thescreen = SDL_SetVideoMode(IXSIZE, IYSIZE, 8, videoflags);
  387. if ( thescreen == NULL )
  388. {
  389. fprintf(stderr, "Couldn't set display mode: %s\n",
  390. SDL_GetError());
  391. SDL_Quit();
  392. exit(5);
  393. }
  394. stride=thescreen->pitch;
  395. videomem=thescreen->pixels;
  396. mustlock=SDL_MUSTLOCK(thescreen);
  397. locked=0;
  398. SDL_ShowCursor(0);
  399. }
  400. void closegfx(void)
  401. {
  402. SDL_Quit();
  403. }
  404. int checkpressed(int code)
  405. {
  406. int *p,i;
  407. i=numpressed;
  408. p=pressedcodes;
  409. while(i--)
  410. if(*p++==code) return 1;
  411. return 0;
  412. }
  413. int checkdown(int code)
  414. {
  415. int *p,i;
  416. i=numdown;
  417. p=downcodes;
  418. while(i--)
  419. if(*p++==code) return 1;
  420. return 0;
  421. }
  422. int checkbutton(int button)
  423. {
  424. return buttonstate & (1<<button);
  425. }
  426. int checkbuttondown(int button)
  427. {
  428. return buttondown & (1<<button);
  429. }
  430. int anydown()
  431. {
  432. return numdown;
  433. }
  434. int takedown(void)
  435. {
  436. int res=0;
  437. if(numdown)
  438. {
  439. res=*downcodes;
  440. --numdown;
  441. if(numdown)
  442. memmove(downcodes,downcodes+1,numdown*sizeof(int));
  443. }
  444. return res;
  445. }
  446. int firstdown(vopid)
  447. {
  448. return *downcodes;
  449. }
  450. #define ENDMARK 0xaabacada
  451. int sdlinoutnormal[]={
  452. SDLK_0,'0',
  453. SDLK_1,'1',
  454. SDLK_2,'2',
  455. SDLK_3,'3',
  456. SDLK_4,'4',
  457. SDLK_5,'5',
  458. SDLK_6,'6',
  459. SDLK_7,'7',
  460. SDLK_8,'8',
  461. SDLK_9,'9',
  462. SDLK_a,'a',
  463. SDLK_b,'b',
  464. SDLK_c,'c',
  465. SDLK_d,'d',
  466. SDLK_e,'e',
  467. SDLK_f,'f',
  468. SDLK_g,'g',
  469. SDLK_h,'h',
  470. SDLK_i,'i',
  471. SDLK_j,'j',
  472. SDLK_k,'k',
  473. SDLK_l,'l',
  474. SDLK_m,'m',
  475. SDLK_n,'n',
  476. SDLK_o,'o',
  477. SDLK_p,'p',
  478. SDLK_q,'q',
  479. SDLK_r,'r',
  480. SDLK_s,'s',
  481. SDLK_t,'t',
  482. SDLK_u,'u',
  483. SDLK_v,'v',
  484. SDLK_w,'w',
  485. SDLK_x,'x',
  486. SDLK_y,'y',
  487. SDLK_z,'z',
  488. SDLK_MINUS,'-',
  489. SDLK_EQUALS,'=',
  490. SDLK_LEFTBRACKET,'[',
  491. SDLK_RIGHTBRACKET,']',
  492. SDLK_SEMICOLON,';',
  493. SDLK_QUOTE,'\'',
  494. SDLK_SLASH,'/',
  495. SDLK_PERIOD,'.',
  496. SDLK_COMMA,',',
  497. SDLK_BACKQUOTE,'`',
  498. SDLK_BACKSPACE,8,
  499. SDLK_TAB,9,
  500. SDLK_DELETE,MYDELETE,
  501. SDLK_RETURN,13,
  502. SDLK_F1,MYF1,
  503. SDLK_F2,MYF2,
  504. SDLK_F3,MYF3,
  505. SDLK_F4,MYF4,
  506. SDLK_F5,MYF5,
  507. SDLK_F6,MYF6,
  508. SDLK_F7,MYF7,
  509. SDLK_F8,MYF8,
  510. SDLK_F9,MYF9,
  511. SDLK_F10,MYF10,
  512. SDLK_ESCAPE,0x1b,
  513. SDLK_LEFT,MYLEFT,
  514. SDLK_RIGHT,MYRIGHT,
  515. SDLK_UP,MYUP,
  516. SDLK_DOWN,MYDOWN,
  517. SDLK_PAGEUP,MYPAGEUP,
  518. SDLK_PAGEDOWN,MYPAGEDOWN,
  519. SDLK_SPACE,' ',
  520. SDLK_HOME,MYHOME,
  521. SDLK_END,MYEND,
  522. SDLK_LALT,MYALTL,
  523. SDLK_RALT,MYALTR,
  524. ENDMARK
  525. };
  526. int sdlinoutshifted[]={
  527. SDLK_0,')',
  528. SDLK_1,'!',
  529. SDLK_2,'@',
  530. SDLK_3,'#',
  531. SDLK_4,'$',
  532. SDLK_5,'%',
  533. SDLK_6,'^',
  534. SDLK_7,'&',
  535. SDLK_8,'*',
  536. SDLK_9,'(',
  537. SDLK_a,'A',
  538. SDLK_b,'B',
  539. SDLK_c,'C',
  540. SDLK_d,'D',
  541. SDLK_e,'E',
  542. SDLK_f,'F',
  543. SDLK_g,'G',
  544. SDLK_h,'H',
  545. SDLK_i,'I',
  546. SDLK_j,'J',
  547. SDLK_k,'K',
  548. SDLK_l,'L',
  549. SDLK_m,'M',
  550. SDLK_n,'N',
  551. SDLK_o,'O',
  552. SDLK_p,'P',
  553. SDLK_q,'Q',
  554. SDLK_r,'R',
  555. SDLK_s,'S',
  556. SDLK_t,'T',
  557. SDLK_u,'U',
  558. SDLK_v,'V',
  559. SDLK_w,'W',
  560. SDLK_x,'X',
  561. SDLK_y,'Y',
  562. SDLK_z,'Z',
  563. SDLK_MINUS,'_',
  564. SDLK_EQUALS,'+',
  565. SDLK_LEFTBRACKET,'{',
  566. SDLK_RIGHTBRACKET,'}',
  567. SDLK_SEMICOLON,':',
  568. SDLK_QUOTE,'"',
  569. SDLK_SLASH,'?',
  570. SDLK_PERIOD,'>',
  571. SDLK_COMMA,'<',
  572. SDLK_BACKQUOTE,'~',
  573. SDLK_BACKSPACE,8,
  574. SDLK_TAB,9,
  575. SDLK_DELETE,MYDELETE,
  576. SDLK_RETURN,13,
  577. SDLK_F1,MYF1+MYSHIFTED,
  578. SDLK_F2,MYF2+MYSHIFTED,
  579. SDLK_F3,MYF3+MYSHIFTED,
  580. SDLK_F4,MYF4+MYSHIFTED,
  581. SDLK_F5,MYF5+MYSHIFTED,
  582. SDLK_F6,MYF6+MYSHIFTED,
  583. SDLK_F7,MYF7+MYSHIFTED,
  584. SDLK_F8,MYF8+MYSHIFTED,
  585. SDLK_F9,MYF9+MYSHIFTED,
  586. SDLK_F10,MYF10+MYSHIFTED,
  587. SDLK_ESCAPE,0x1b,
  588. SDLK_LEFT,MYLEFT+MYSHIFTED,
  589. SDLK_RIGHT,MYRIGHT+MYSHIFTED,
  590. SDLK_UP,MYUP+MYSHIFTED,
  591. SDLK_DOWN,MYDOWN+MYSHIFTED,
  592. SDLK_PAGEUP,MYPAGEUP,
  593. SDLK_PAGEDOWN,MYPAGEDOWN,
  594. SDLK_SPACE,' ',
  595. SDLK_HOME,MYHOME,
  596. SDLK_END,MYEND,
  597. SDLK_LALT,MYALTL,
  598. SDLK_RALT,MYALTR,
  599. ENDMARK
  600. };
  601. static int looklist(int code,int *list)
  602. {
  603. while((unsigned int)*list!=ENDMARK) {
  604. if(*list==code)
  605. return list[1];
  606. list+=2;
  607. }
  608. return -1;
  609. }
  610. static int mapkey(int code,int qual)
  611. {
  612. if(qual & KMOD_SHIFT)
  613. code=looklist(code,sdlinoutshifted);
  614. else
  615. code=looklist(code,sdlinoutnormal);
  616. if(code<0) return -1;
  617. if(qual & KMOD_ALT)
  618. code|=MYALTED;
  619. return code;
  620. }
  621. static void markkey(int code,int status)
  622. {
  623. int i;
  624. int *ip;
  625. if(status)
  626. {
  627. if(numdown<KEYMAX)
  628. downcodes[numdown++]=code;
  629. ip=pressedcodes;
  630. i=numpressed;
  631. while(i)
  632. if(*ip++==code) break;
  633. else i--;
  634. if(!i && numpressed<KEYMAX)
  635. pressedcodes[numpressed++]=code;
  636. } else
  637. {
  638. i=numpressed;
  639. ip=pressedcodes;
  640. while(i)
  641. if(*ip++==code)
  642. {
  643. *--ip=pressedcodes[--numpressed];
  644. break;
  645. } else i--;
  646. }
  647. }
  648. void pollinput(void)
  649. {
  650. SDL_PollEvent(0);
  651. }
  652. void scaninput(void)
  653. {
  654. SDL_Event event;
  655. int key,mod;
  656. static int bs=0;
  657. numdown=0;
  658. buttondown=0;
  659. while(SDL_PollEvent(&event))
  660. {
  661. switch(event.type)
  662. {
  663. case SDL_KEYDOWN:
  664. key=event.key.keysym.sym;
  665. mod=event.key.keysym.mod;
  666. markkey(mapkey(key,mod),1);
  667. break;
  668. case SDL_KEYUP:
  669. key=event.key.keysym.sym;
  670. mod=event.key.keysym.mod;
  671. markkey(mapkey(key,mod),0);
  672. break;
  673. case SDL_MOUSEBUTTONUP:
  674. bs&=~(1<<(event.button.button-1));
  675. mousex=event.button.x>>1;
  676. mousey=event.button.y>>1;
  677. mouseb=bs;
  678. break;
  679. case SDL_MOUSEBUTTONDOWN:
  680. bs|=1<<(event.button.button-1);
  681. mousex=event.button.x>>1;
  682. mousey=event.button.y>>1;
  683. mouseb=bs;
  684. break;
  685. case SDL_MOUSEMOTION:
  686. mousex=event.motion.x>>1;
  687. mousey=event.motion.y>>1;
  688. break;
  689. case SDL_QUIT:
  690. exitflag = 1;
  691. break;
  692. }
  693. }
  694. }
  695. /*
  696. void scaninput()
  697. {
  698. int i,*ip,code;
  699. numdown=0;
  700. buttondown=0;
  701. while(XCheckMaskEvent(dp,~0,&event))
  702. switch(event.type)
  703. {
  704. case KeyPress:
  705. code=XLookupKeysym(keyevent,0);
  706. if(numdown<KEYMAX)
  707. downcodes[numdown++]=code;
  708. ip=pressedcodes;
  709. i=numpressed;
  710. while(i)
  711. if(*ip++==code) break;
  712. else i--;
  713. if(!i && numpressed<KEYMAX)
  714. pressedcodes[numpressed++]=code;
  715. break;
  716. case KeyRelease:
  717. code=XLookupKeysym(keyevent,0);
  718. i=numpressed;
  719. ip=pressedcodes;
  720. while(i)
  721. if(*ip++==code)
  722. {
  723. *--ip=pressedcodes[--numpressed];
  724. break;
  725. } else i--;
  726. break;
  727. case ButtonPress:
  728. i=1<<buttonevent->button;
  729. buttonstate|=i;
  730. buttondown|=i;
  731. break;
  732. case ButtonRelease:
  733. buttonstate&=~(1<<buttonevent->button);
  734. break;
  735. case MotionNotify:
  736. mxpos=motionevent->x;
  737. mypos=motionevent->y;
  738. break;
  739. case Expose:
  740. copyup();
  741. needwhole=0;
  742. break;
  743. case FocusOut:
  744. numpressed=0;
  745. break;
  746. }
  747. }
  748. */
  749. static void drawrect(int x,int y,int xs,int ys,int c)
  750. {
  751. uchar *p;
  752. gfxlock();
  753. p=videomem+y*stride+x;
  754. while(ys--)
  755. {
  756. memset(p,c,xs);
  757. p+=stride;
  758. }
  759. }
  760. void greyrect(int x,int y,int xsize,int ysize)
  761. {
  762. static int greycolor=-1;
  763. if(greycolor==-1)
  764. greycolor=bestmatch(0,0,0x70);
  765. drawrect(x,y,xsize,ysize,greycolor);
  766. }
  767. void clear()
  768. {
  769. int j;
  770. uchar *p;
  771. gfxlock();
  772. p=videomem;
  773. for(j=0;j<IYSIZE;++j)
  774. {
  775. memset(p,0,IXSIZE);
  776. p+=stride;
  777. }
  778. }
  779. void clearrect(int x,int y,int xsize,int ysize)
  780. {
  781. drawrect(x,y,xsize,ysize,0);
  782. }
  783. void gfxlock(void)
  784. {
  785. if(locked || !mustlock) return;
  786. if ( SDL_LockSurface(thescreen) < 0 )
  787. {
  788. fprintf(stderr, "Couldn't lock display surface: %s\n",
  789. SDL_GetError());
  790. }
  791. locked=1;
  792. }
  793. void gfxunlock(void)
  794. {
  795. if(!locked || !mustlock) return;
  796. SDL_UnlockSurface(thescreen);
  797. locked=0;
  798. }