From 608be3bcd270eb45628a274eddb6dbcc8940accf Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Sat, 5 Apr 2003 01:48:12 +0200 Subject: [PATCH] rnd-20030405-1-src --- src/conftime.h | 2 +- src/events.c | 30 ++++++++++---- src/libgame/misc.c | 12 +++--- src/libgame/misc.h | 4 ++ src/libgame/sdl.c | 56 +++++--------------------- src/libgame/sdl.h | 14 ++++++- src/libgame/system.c | 96 ++++++++++++++++++++------------------------ src/libgame/system.h | 12 ------ src/libgame/x11.c | 70 ++++++++------------------------ src/libgame/x11.h | 14 ++++++- 10 files changed, 131 insertions(+), 179 deletions(-) diff --git a/src/conftime.h b/src/conftime.h index 90226b29..ab0f201c 100644 --- a/src/conftime.h +++ b/src/conftime.h @@ -1 +1 @@ -#define COMPILE_DATE_STRING "[2003-04-04 23:16]" +#define COMPILE_DATE_STRING "[2003-04-05 01:37]" diff --git a/src/events.c b/src/events.c index b5be2a16..c88a27b8 100644 --- a/src/events.c +++ b/src/events.c @@ -29,6 +29,11 @@ #define KEY_PRESSED TRUE +static boolean cursor_inside_playfield = FALSE; +static boolean playfield_cursor_set = FALSE; +static unsigned long playfield_cursor_delay = 0; + + /* event filter especially needed for SDL event filtering due to delay problems with lots of mouse motion events when mouse button not pressed (X11 can handle this with 'PointerMotionHintMask') */ @@ -39,18 +44,19 @@ int FilterMouseMotionEvents(const Event *event) if (event->type != EVENT_MOTIONNOTIFY) return 1; - /* when playing, display a different mouse pointer inside the playfield */ if (game_status == PLAYING) { - static boolean inside_field = FALSE; MotionEvent *motion = (MotionEvent *)event; - if ((motion->x >= SX && motion->x < SX + SXSIZE && - motion->y >= SY && motion->y < SY + SYSIZE) != inside_field) - { - inside_field = !inside_field; + cursor_inside_playfield = + (motion->x >= SX && motion->x < SX + SXSIZE && + motion->y >= SY && motion->y < SY + SYSIZE); - SetMouseCursor(inside_field ? CURSOR_PLAYFIELD : CURSOR_DEFAULT); + if (playfield_cursor_set) + { + SetMouseCursor(CURSOR_DEFAULT); + DelayReached(&playfield_cursor_delay, 0); + playfield_cursor_set = FALSE; } } @@ -111,7 +117,17 @@ void EventLoop(void) } } else + { + /* when playing, display a special mouse pointer inside the playfield */ + if (game_status == PLAYING && cursor_inside_playfield && + DelayReached(&playfield_cursor_delay, 1000)) + { + SetMouseCursor(CURSOR_PLAYFIELD); + playfield_cursor_set = TRUE; + } + HandleNoEvent(); + } /* don't use all CPU time when idle; the main loop while playing has its own synchronization and is CPU friendly, too */ diff --git a/src/libgame/misc.c b/src/libgame/misc.c index cffa180b..32137253 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -215,8 +215,8 @@ boolean FrameReached(unsigned long *frame_counter_var, { unsigned long actual_frame_counter = FrameCounter; - if (actual_frame_counter < *frame_counter_var + frame_delay && - actual_frame_counter >= *frame_counter_var) + if (actual_frame_counter >= *frame_counter_var && + actual_frame_counter < *frame_counter_var + frame_delay) return FALSE; *frame_counter_var = actual_frame_counter; @@ -229,8 +229,8 @@ boolean DelayReached(unsigned long *counter_var, { unsigned long actual_counter = Counter(); - if (actual_counter < *counter_var + delay && - actual_counter >= *counter_var) + if (actual_counter >= *counter_var && + actual_counter < *counter_var + delay) return FALSE; *counter_var = actual_counter; @@ -246,8 +246,8 @@ void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay) { actual_counter = Counter(); - if (actual_counter < *counter_var + delay && - actual_counter >= *counter_var) + if (actual_counter >= *counter_var && + actual_counter < *counter_var + delay) sleep_milliseconds((*counter_var + delay - actual_counter) / 2); else break; diff --git a/src/libgame/misc.h b/src/libgame/misc.h index 2a727278..3b9d945b 100644 --- a/src/libgame/misc.h +++ b/src/libgame/misc.h @@ -51,6 +51,10 @@ #define BYTE_ORDER_BIG_ENDIAN 0 #define BYTE_ORDER_LITTLE_ENDIAN 1 +/* values for cursor bitmap creation */ +#define BIT_ORDER_MSB 0 +#define BIT_ORDER_LSB 1 + /* values for createDirectory() */ #define PERMS_PRIVATE 0 #define PERMS_PUBLIC 1 diff --git a/src/libgame/sdl.c b/src/libgame/sdl.c index 51c2baf8..f479fd99 100644 --- a/src/libgame/sdl.c +++ b/src/libgame/sdl.c @@ -1237,65 +1237,29 @@ Bitmap *SDLLoadImage(char *filename) /* custom cursor fuctions */ /* ------------------------------------------------------------------------- */ -static SDL_Cursor *create_cursor(const char **image) +static SDL_Cursor *create_cursor(struct MouseCursorInfo *cursor_info) { - int i, row, col; - Uint8 data[4*32]; - Uint8 mask[4*32]; - int hot_x, hot_y; - - i = -1; - for (row=0; row<32; ++row) - { - for (col=0; col<32; ++col) - { - if (col % 8) - { - data[i] <<= 1; - mask[i] <<= 1; - } - else - { - i++; - data[i] = mask[i] = 0; - } - - switch (image[4+row][col]) - { - case 'X': - data[i] |= 0x01; - mask[i] |= 0x01; - break; - case '.': - mask[i] |= 0x01; - break; - case ' ': - break; - } - } - } - - sscanf(image[4+row], "%d,%d", &hot_x, &hot_y); - - return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y); + return SDL_CreateCursor(cursor_info->data, cursor_info->mask, + cursor_info->width, cursor_info->height, + cursor_info->hot_x, cursor_info->hot_y); } -void SDLSetMouseCursor(const char **cursor_image) +void SDLSetMouseCursor(struct MouseCursorInfo *cursor_info) { - static const char **last_cursor_image = NULL; + static struct MouseCursorInfo *last_cursor_info = NULL; static SDL_Cursor *cursor_default = NULL; static SDL_Cursor *cursor_current = NULL; if (cursor_default == NULL) cursor_default = SDL_GetCursor(); - if (cursor_image != NULL && cursor_image != last_cursor_image) + if (cursor_info != NULL && cursor_info != last_cursor_info) { - cursor_current = create_cursor(cursor_image); - last_cursor_image = cursor_image; + cursor_current = create_cursor(cursor_info); + last_cursor_info = cursor_info; } - SDL_SetCursor(cursor_image ? cursor_current : cursor_default); + SDL_SetCursor(cursor_info ? cursor_current : cursor_default); } diff --git a/src/libgame/sdl.h b/src/libgame/sdl.h index c2f55ee9..fcf19341 100644 --- a/src/libgame/sdl.h +++ b/src/libgame/sdl.h @@ -28,6 +28,9 @@ #define TARGET_STRING "SDL" #define FULLSCREEN_STATUS FULLSCREEN_AVAILABLE +#define CURSOR_MAX_WIDTH 32 +#define CURSOR_MAX_HEIGHT 32 + /* SDL type definitions */ @@ -67,6 +70,15 @@ struct SDLSurfaceInfo GC stored_clip_gc; }; +struct MouseCursorInfo +{ + int width, height; + int hot_x, hot_y; + + char data[CURSOR_MAX_WIDTH * CURSOR_MAX_HEIGHT / 8]; + char mask[CURSOR_MAX_WIDTH * CURSOR_MAX_HEIGHT / 8]; +}; + struct XY { short x, y; @@ -339,7 +351,7 @@ void SDLZoomBitmap(Bitmap *, Bitmap *); Bitmap *SDLLoadImage(char *); -void SDLSetMouseCursor(const char **); +void SDLSetMouseCursor(struct MouseCursorInfo *); inline void SDLOpenAudio(void); inline void SDLCloseAudio(void); diff --git a/src/libgame/system.c b/src/libgame/system.c index 45fb77c1..18776bd4 100644 --- a/src/libgame/system.c +++ b/src/libgame/system.c @@ -855,84 +855,79 @@ void CreateBitmapWithSmallBitmaps(Bitmap *src_bitmap) /* mouse pointer functions */ /* ------------------------------------------------------------------------- */ -/* cursor bitmap in XPM format */ +/* XPM */ static const char *cursor_image_playfield[] = { /* width height num_colors chars_per_pixel */ - " 32 32 3 1", + " 16 16 3 1", /* colors */ "X c #000000", ". c #ffffff", " c None", - " X ", - "X.X ", - " X ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", + /* pixels */ + " X ", + "X.X ", + " X ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + + /* hot spot */ "1,1" }; +#if defined(TARGET_SDL) +static const int cursor_bit_order = BIT_ORDER_MSB; +#elif defined(TARGET_X11_NATIVE) +static const int cursor_bit_order = BIT_ORDER_LSB; +#endif + static struct MouseCursorInfo *get_cursor_from_image(const char **image) { struct MouseCursorInfo *cursor; - int row, col, i; + boolean bit_order_msb = (cursor_bit_order == BIT_ORDER_MSB); + int header_lines = 4; + int x, y, i; cursor = checked_calloc(sizeof(struct MouseCursorInfo)); + sscanf(image[0], " %d %d ", &cursor->width, &cursor->height); + i = -1; - for (row=0; row<32; ++row) + for (y=0; y < cursor->width; y++) { - for (col=0; col<32; ++col) + for (x=0; x < cursor->height; x++) { - if (col % 8) - { - cursor->data[i] <<= 1; - cursor->mask[i] <<= 1; - } - else + int bit_nr = x % 8; + int bit_mask = 0x01 << (bit_order_msb ? 7 - bit_nr : bit_nr ); + + if (bit_nr == 0) { i++; cursor->data[i] = cursor->mask[i] = 0; } - switch (image[4+row][col]) + switch (image[header_lines + y][x]) { case 'X': - cursor->data[i] |= 0x01; - cursor->mask[i] |= 0x01; + cursor->data[i] |= bit_mask; + cursor->mask[i] |= bit_mask; break; case '.': - cursor->mask[i] |= 0x01; + cursor->mask[i] |= bit_mask; break; case ' ': @@ -941,17 +936,14 @@ static struct MouseCursorInfo *get_cursor_from_image(const char **image) } } - sscanf(image[4+row], "%d,%d", &cursor->hot_x, &cursor->hot_y); - - cursor->width = 32; - cursor->height = 32; + sscanf(image[header_lines + y], "%d,%d", &cursor->hot_x, &cursor->hot_y); return cursor; } void SetMouseCursor(int mode) { - struct MouseCursorInfo *cursor_playfield = NULL; + static struct MouseCursorInfo *cursor_playfield = NULL; if (cursor_playfield == NULL) cursor_playfield = get_cursor_from_image(cursor_image_playfield); diff --git a/src/libgame/system.h b/src/libgame/system.h index c8afb2de..a2eda6d6 100644 --- a/src/libgame/system.h +++ b/src/libgame/system.h @@ -144,9 +144,6 @@ #define CURSOR_DEFAULT 0 #define CURSOR_PLAYFIELD 1 -#define CURSOR_MAX_WIDTH 32 -#define CURSOR_MAX_HEIGHT 32 - /* maximum number of parallel players supported by libgame functions */ #define MAX_PLAYERS 4 @@ -363,15 +360,6 @@ struct JoystickInfo int fd[MAX_PLAYERS]; /* file descriptor of player's joystick */ }; -struct MouseCursorInfo -{ - int width, height; - int hot_x, hot_y; - - char data[CURSOR_MAX_WIDTH * CURSOR_MAX_HEIGHT / 8]; - char mask[CURSOR_MAX_WIDTH * CURSOR_MAX_HEIGHT / 8]; -}; - struct SetupJoystickInfo { char *device_name; /* device name of player's joystick */ diff --git a/src/libgame/x11.c b/src/libgame/x11.c index 61d1a1a5..278230de 100644 --- a/src/libgame/x11.c +++ b/src/libgame/x11.c @@ -429,82 +429,46 @@ inline Pixel X11GetPixelFromRGB(unsigned int color_r, unsigned int color_g, #if defined(TARGET_X11_NATIVE) -static Cursor create_cursor(const char **image) +static Cursor create_cursor(struct MouseCursorInfo *cursor_info) { Pixmap pixmap_data, pixmap_mask; XColor color_fg, color_bg; Cursor cursor; - int i, row, col; - char data[4*32]; - char mask[4*32]; - int hot_x, hot_y; - - int data_width = 32, data_height = 32; - int mask_width = 32, mask_height = 32; - - i = -1; - for (row=0; row<32; ++row) - { - for (col=0; col<32; ++col) - { - if (col % 8) - { - data[i] <<= 1; - mask[i] <<= 1; - } - else - { - i++; - data[i] = mask[i] = 0; - } - - switch (image[4+row][col]) - { - case 'X': - data[i] |= 0x01; - mask[i] |= 0x01; - break; - case '.': - mask[i] |= 0x01; - break; - case ' ': - break; - } - } - } - - sscanf(image[4+row], "%d,%d", &hot_x, &hot_y); - /* shape and mask are single plane pixmaps */ - pixmap_data = XCreatePixmapFromBitmapData(display, window->drawable, data, - data_width, data_height, 1, 0, 1); - pixmap_mask = XCreatePixmapFromBitmapData(display, window->drawable, mask, - mask_width, mask_height, 1, 0, 1); + pixmap_data = + XCreatePixmapFromBitmapData(display, window->drawable, cursor_info->data, + cursor_info->width, cursor_info->height, + 1, 0, 1); + pixmap_mask = + XCreatePixmapFromBitmapData(display, window->drawable, cursor_info->mask, + cursor_info->width, cursor_info->height, + 1, 0, 1); XParseColor(display, cmap, "black", &color_fg); XParseColor(display, cmap, "white", &color_bg); cursor = XCreatePixmapCursor(display, pixmap_data, pixmap_mask, - &color_fg, &color_bg, hot_x, hot_y); + &color_fg, &color_bg, + cursor_info->hot_x, cursor_info->hot_y); return cursor; } -void X11SetMouseCursor(const char **cursor_image) +void X11SetMouseCursor(struct MouseCursorInfo *cursor_info) { - static const char **last_cursor_image = NULL; + static struct MouseCursorInfo *last_cursor_info = NULL; static Cursor cursor_default = None; static Cursor cursor_current = None; - if (cursor_image != NULL && cursor_image != last_cursor_image) + if (cursor_info != NULL && cursor_info != last_cursor_info) { - cursor_current = create_cursor(cursor_image); - last_cursor_image = cursor_image; + cursor_current = create_cursor(cursor_info); + last_cursor_info = cursor_info; } XDefineCursor(display, window->drawable, - cursor_image ? cursor_current : cursor_default); + cursor_info ? cursor_current : cursor_default); } #endif /* TARGET_X11_NATIVE */ diff --git a/src/libgame/x11.h b/src/libgame/x11.h index a4a0577f..4ef96acb 100644 --- a/src/libgame/x11.h +++ b/src/libgame/x11.h @@ -45,6 +45,9 @@ #define FULLSCREEN_STATUS FULLSCREEN_NOT_AVAILABLE +#define CURSOR_MAX_WIDTH 32 +#define CURSOR_MAX_HEIGHT 32 + /* X11 type definitions */ @@ -80,6 +83,15 @@ struct X11DrawableInfo GC clip_gc; /* can be 'stored_clip_gc' or one-tile-only clip GC */ }; +struct MouseCursorInfo +{ + int width, height; + int hot_x, hot_y; + + char data[CURSOR_MAX_WIDTH * CURSOR_MAX_HEIGHT / 8]; + char mask[CURSOR_MAX_WIDTH * CURSOR_MAX_HEIGHT / 8]; +}; + struct XY { short x, y; @@ -321,7 +333,7 @@ inline Pixel X11GetPixel(Bitmap *, int, int); inline Pixel X11GetPixelFromRGB(unsigned int, unsigned int, unsigned int); #if defined(TARGET_X11_NATIVE) -void X11SetMouseCursor(const char **); +void X11SetMouseCursor(struct MouseCursorInfo *); #endif #endif /* X11_H */ -- 2.34.1