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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
|
/***********************************************************
* K O U L E S *
*----------------------------------------------------------*
* C1995 JAHUSOFT *
* Jan Hubicka *
* Dukelskych Bojovniku 1944 *
* 390 03 Tabor *
* Czech Republic *
* Phone: 0041-361-32613 *
* eMail: hubicka@limax.paru.cas.cz *
*----------------------------------------------------------*
* Copyright(c)1995,1996 by Jan Hubicka.See README for *
* licence details. *
*----------------------------------------------------------*
* interface.c interface xlib graphics functions *
***********************************************************/
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/keysymdef.h>
#include "../koules.h"
#include "../framebuffer.h"
#include <sys/time.h>
#include <unistd.h>
#undef next
struct keyrec *
findkey (CONST int sc)
{
struct keyrec *kkey = ffirstkey;
while (kkey != NULL && kkey->scancode != sc)
kkey = kkey->next;
return (kkey);
}
void
addkey (CONST int scancode)
{
struct keyrec *key;
if (!findkey (scancode))
{
key = malloc (sizeof (struct keyrec));
key->last = NULL;
key->next = ffirstkey;
if (ffirstkey != NULL)
ffirstkey->last = key;
key->scancode = scancode;
ffirstkey = key;
}
}
void
deletekey (CONST int scancode)
{
struct keyrec *key;
if (!(key = findkey (scancode)))
{
printf ("Key not found!\n");
return;
}
if (key->last)
key->last->next = key->next;
else
ffirstkey = key->next;
if (key->next)
key->next->last = key->last;
free (key);
}
int
GetKey ()
{
if (ffirstkey != NULL /* && ffirstkey->next != NULL */ )
return (ffirstkey->scancode);
return (0);
}
void
DrawRectangle (CONST int x1, CONST int y1, CONST int x2, CONST int y2, CONST int color)
{
#ifdef MITSHM
if (shm)
{
Line (x1, y1, x2, y1, color);
Line (x2, y1, x2, y2, color);
Line (x2, y2, x1, y2, color);
Line (x1, y2, x1, y1, color);
}
else
#endif
{
SetColor (color);
XDrawRectangle (dp, current.pixmap, gc, (int) x1, (int) y1, (int) x2 - x1, (int) y2 - y1);
}
}
void
ClearScreen ()
{
#ifdef MITSHM
if (shm && current.pixmap != wi)
memset (current.vbuff, pixels[0], MAPWIDTH * (MAPHEIGHT + 20));
else
#endif
{
XFillRectangle (dp, current.pixmap, blackgc, 0, 0, MAPWIDTH, MAPHEIGHT + 20);
}
}
void
CopyVSToVS (CONST VScreenType source, CONST VScreenType destination)
{
#ifdef MITSHM
if (shm)
#if defined(__i386__)&&defined(ASSEMBLY)
__memcpy (destination.vbuff, source.vbuff, MAPWIDTH * (MAPHEIGHT + 20));
#else
memcpy (destination.vbuff, source.vbuff, MAPWIDTH * (MAPHEIGHT + 20));
#endif
else
#endif
{
XCopyArea (dp, source.pixmap, destination.pixmap, whitegc, 0, 0, MAPWIDTH, MAPHEIGHT + 20, 0, 0);
}
}
void
CopyToScreen (CONST VScreenType source)
{
#ifdef MITSHM
if (shm)
{
switch (depth)
{
case 16:
{
register unsigned char *data = (unsigned char *) source.vbuff,
*end = (unsigned char *) source.vbuff + MAPWIDTH * (MAPHEIGHT + 20);
register unsigned short *out = (unsigned short *) source.ximage->data;
while (data < end)
{
*out = rpixels[*data];
*(out + 1) = rpixels[*(data + 1)];
*(out + 2) = rpixels[*(data + 2)];
*(out + 3) = rpixels[*(data + 3)];
*(out + 4) = rpixels[*(data + 4)];
*(out + 5) = rpixels[*(data + 5)];
*(out + 6) = rpixels[*(data + 6)];
*(out + 7) = rpixels[*(data + 7)];
out += 8;
data += 8;
}
}
break;
case 32:
case 24:
{
register unsigned char *data = (unsigned char *) source.vbuff,
*end = (unsigned char *) source.vbuff + MAPWIDTH * (MAPHEIGHT + 20);
register unsigned long *out = (unsigned long *) source.ximage->data;
while (data < end)
{
*out = rpixels[*data];
*(out + 1) = rpixels[*(data + 1)];
*(out + 2) = rpixels[*(data + 2)];
*(out + 3) = rpixels[*(data + 3)];
*(out + 4) = rpixels[*(data + 4)];
*(out + 5) = rpixels[*(data + 5)];
*(out + 6) = rpixels[*(data + 6)];
*(out + 7) = rpixels[*(data + 7)];
out += 8;
data += 8;
}
}
break;
}
if (notrealshm)
XPutImage (dp, wi, whitegc, source.ximage, 0, 0, 0, 0, MAPWIDTH, MAPHEIGHT + 20);
else
XShmPutImage (dp, wi, whitegc, source.ximage, 0, 0, 0, 0, MAPWIDTH, MAPHEIGHT + 20, True);
}
else
#endif
{
if (screenpixmap != source.pixmap)
XSetWindowBackgroundPixmap (dp, wi, source.pixmap);
XClearWindow (dp, wi);
}
}
BitmapType
CompileBitmap (CONST int x, CONST int y, CONST RawBitmapType bitmap)
{
BitmapType pm;
#ifdef MITSHM
int size1;
char *cbitmap;
#endif
#ifdef MITSHM
if (shm)
{
size1 = ShmBitmapSize (x, y, bitmap.vbuff);
if ((cbitmap = malloc (size1)) == NULL)
perror ("create_bitmap"), exit (1);
ShmCompileBitmap (x, y, bitmap.vbuff, cbitmap);
pm.vbuff = (unsigned char *) cbitmap;
free (bitmap.vbuff);
}
else
#endif
{
XFlush (dp);
pm.bitmap = XCreatePixmap (dp, wi, x, y, DefaultDepth (dp, screen));
XPutImage (dp, pm.bitmap, whitegc, bitmap.bitmap, 0, 0, 0, 0, x, y);
pm.mask = XCreateBitmapFromData (dp, wi, bitmap.mask, x, y);
free (bitmap.mask);
XSync (dp, 0);
XDestroyImage (bitmap.bitmap);
}
return (pm);
}
RawBitmapType
CreateBitmap (CONST int xv, CONST int yv)
{
RawBitmapType img;
char *data;
#ifdef MITSHM
if (shm)
{
img.xsize = xv;
img.vbuff = malloc (xv * yv + 10);
return (img);
}
else
#endif
{
if ((data = (char *) calloc ((xv + BitmapPad (dp)) * yv * 4, 1)) == NULL)
perror ("Memory Error"), exit (2);
img.bitmap = XCreateImage (dp, DefaultVisual (dp, screen), DefaultDepth (dp, screen),
ZPixmap, 0, data, xv, yv,
BitmapPad (dp), 0);
if ((img.mask = (char *) calloc ((xv) * yv, 1)) == NULL)
perror ("Memory Error"), exit (2);
}
return (img);
}
static int lastcolor = 1;
static int lastwidth = 1;
static int needflush = 0;
static int lastx = 0, lasty = 0;
static Pixmap lastpixmap = None;
void
FlushParam ()
{
XGCValues val;
val.clip_x_origin = lastx;
val.clip_y_origin = lasty;
val.clip_mask = lastpixmap;
val.foreground = lastcolor;
val.line_width = lastwidth;
if (needflush)
XChangeGC (dp, gc, needflush, &val);
needflush = 0;
}
void
SetColor (CONST int color)
{
int color1 = cpixels (color);
if (color1 != lastcolor)
{
lastcolor = color1;
if (!needflush)
{
XSetForeground (dp, gc, color1);
return;
}
else
{
needflush |= GCForeground;
}
}
FlushParam ();
}
void
SetWidth (int width)
{
if (width == 1)
width = 0;
if (lastwidth != width)
{
lastwidth = width;
needflush |= GCLineWidth;
}
}
void
SetStipple (int x, int y, Pixmap stipple)
{
if (lastx != x)
{
lastx = x;
needflush |= GCClipXOrigin;
}
if (lasty != y)
{
lasty = y;
needflush |= GCClipYOrigin;
}
if (lastpixmap != stipple)
{
lastpixmap = stipple;
needflush |= GCClipMask;
}
if (needflush)
FlushParam ();
}
void
UnSetStipple ()
{
lastpixmap = None;
needflush |= GCClipMask;
}
void
MyPause ()
{
XEvent myEvent;
tbreak = 1;
pesc = 0;
pup = 0;
pdown = 0;
pleft = 0;
pright = 0;
pp = 0;
penter = 0;
while (1)
{
XNextEvent (dp, &myEvent);
switch (myEvent.type)
{
case FocusIn:
/*if(myEvent.xfocus.detail==NotifyInferior) */
return;
case GraphicsExpose:
case Expose:
CopyToScreen (backscreen);
}
}
}
void
ProcessEvents ()
{
XEvent myEvent;
int rootx, rooty;
static int fout = 0;
Window rootreturn, childreturn;
XFlush (dp);
while (XEventsQueued (dp, QueuedAlready))
{
XNextEvent (dp, &myEvent);
switch (myEvent.type)
{
case KeyRelease:
if (fout)
break;
deletekey (XLookupKeysym (&myEvent.xkey, 0));
/*printf ("Released:%i\n", myEvent.xkey.keycode); */
switch (XLookupKeysym (&myEvent.xkey, 0))
{
case XK_Escape:
pesc = 0;
break;
case XK_Up:
pup = 0;
break;
case XK_Down:
pdown = 0;
break;
case XK_Left:
pleft = 0;
break;
case XK_Right:
pright = 0;
break;
case XK_H:
case XK_h:
ph = 0;
break;
case XK_P:
case XK_p:
case XK_Pause:
pp = 0;
break;
case XK_KP_Enter:
case XK_KP_Space:
case XK_Return:
penter = 0;
break;
}
break;
case KeyPress:
if (fout)
break;
addkey (XLookupKeysym (&myEvent.xkey, 0));
/*printf ("Pressed:%i\n", myEvent.xkey.keycode); */
switch (XLookupKeysym (&myEvent.xkey, 0))
{
case XK_Escape:
pesc = 1;
break;
case XK_Up:
pup = 1;
break;
case XK_Down:
pdown = 1;
break;
case XK_Right:
pright = 1;
break;
case XK_Left:
pleft = 1;
break;
case XK_H:
case XK_h:
ph = 1;
break;
case XK_P:
case XK_p:
case XK_Pause:
pp = 1;
break;
case XK_KP_Enter:
case XK_KP_Space:
case XK_Return:
penter = 1;
break;
}
break;
case FocusOut:
while (ffirstkey)
deletekey (ffirstkey->scancode);
/*if(myEvent.xfocus.detail==NotifyPointer) */
if (!nopause /*&& gamemode == GAME */ && !client)
MyPause ();
else
fout = 1;
break;
case FocusIn:
fout = 0;
}
}
childreturn = 0;
XQueryPointer (dp, wi, &rootreturn, &childreturn, &rootx, &rooty, &mousex, &mousey, &mousebuttons);
mousebuttons&=256;
if (fout)
mousex = 0,
mousey = 0,
mousebuttons = 0;
}
void
PutBitmap (CONST int x, CONST int y, CONST int xsize, CONST int ysize, CONST BitmapType bitmap)
{
#ifdef MITSHM
if (shm)
ShmPutBitmap (x, y, xsize, ysize, bitmap.vbuff);
else
#endif
{
XCopyPlane (dp, (bitmap).mask, current.pixmap, maskgc,
0, 0, xsize, ysize, x, y, 1);
XCopyArea (dp, bitmap.bitmap, current.pixmap, orgc,
0, 0, xsize, ysize, x, y);
}
}
|