From: Holger Schemel Date: Sun, 6 Aug 2000 16:37:52 +0000 (+0200) Subject: rnd-20000806-1-src X-Git-Tag: 2.0.0^2~51 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=41cfe645942062496cd063eb46e17d677a76c645;p=rocksndiamonds.git rnd-20000806-1-src --- diff --git a/src/Makefile b/src/Makefile index 17615f90..f9a71038 100644 --- a/src/Makefile +++ b/src/Makefile @@ -35,12 +35,12 @@ EXTRA_LIBS = -lnsl -lsocket -R$(XLIB_PATH) endif USE_SDL = true -USE_SDL_OLD_LIBS = true +USE_SDL_OLD_LIBS = false ifeq ($(USE_SDL_OLD_LIBS),true) -SDL_EXTRA_LIBS = -lIMG +SDL_EXTRA_LIBS = -lIMG -lmixer else -SDL_EXTRA_LIBS = -lSDL_image +SDL_EXTRA_LIBS = -lSDL_image -lSDL_mixer endif SDL_CFLAGS := -DUSE_SDL_LIBRARY $(shell sdl-config --cflags) diff --git a/src/events.c b/src/events.c index a2b346c0..e2f9db8e 100644 --- a/src/events.c +++ b/src/events.c @@ -28,6 +28,23 @@ #define KEY_RELEASED FALSE #define KEY_PRESSED TRUE + +/* event filter especially needed for SDL event filtering due to + delay problems with lots of mouse motion events when mouse + button not pressed */ + +int EventFilter(const Event *event) +{ + if (event->type != EVENT_MOTIONNOTIFY) + return 1; + + /* get mouse motion events without pressed button only in level editor */ + if (button_status == MB_RELEASED && game_status != LEVELED) + return 0; + else + return 1; +} + void EventLoop(void) { while(1) @@ -38,25 +55,28 @@ void EventLoop(void) NextEvent(&event); - switch(event.type) + if (EventFilter(&event)) { - case EVENT_BUTTONPRESS: - case EVENT_BUTTONRELEASE: - HandleButtonEvent((ButtonEvent *) &event); - break; - - case EVENT_MOTIONNOTIFY: - HandleMotionEvent((MotionEvent *) &event); - break; - - case EVENT_KEYPRESS: - case EVENT_KEYRELEASE: - HandleKeyEvent((KeyEvent *) &event); - break; - - default: - HandleOtherEvents(&event); - break; + switch(event.type) + { + case EVENT_BUTTONPRESS: + case EVENT_BUTTONRELEASE: + HandleButtonEvent((ButtonEvent *) &event); + break; + + case EVENT_MOTIONNOTIFY: + HandleMotionEvent((MotionEvent *) &event); + break; + + case EVENT_KEYPRESS: + case EVENT_KEYRELEASE: + HandleKeyEvent((KeyEvent *) &event); + break; + + default: + HandleOtherEvents(&event); + break; + } } } @@ -101,6 +121,14 @@ void HandleOtherEvents(Event *event) HandleClientMessageEvent((ClientMessageEvent *) event); break; +#ifdef USE_SDL_LIBRARY + case SDL_JOYAXISMOTION: + case SDL_JOYBUTTONDOWN: + case SDL_JOYBUTTONUP: + HandleJoystickEvent(event); + break; +#endif + default: break; } @@ -227,17 +255,17 @@ void HandleButtonEvent(ButtonEvent *event) void HandleMotionEvent(MotionEvent *event) { - int win_x, win_y; - - if (!QueryPointer(window, &win_x, &win_y)) + if (!PointerInWindow(window)) return; /* window and pointer are on different screens */ - if (!button_status && game_status != LEVELED) +#if 0 + if (button_status == MB_RELEASED && game_status != LEVELED) return; +#endif motion_status = TRUE; - HandleButton(win_x, win_y, button_status); + HandleButton(event->x, event->y, button_status); } void HandleKeyEvent(KeyEvent *event) diff --git a/src/events.h b/src/events.h index 931aa7ce..a1388b2c 100644 --- a/src/events.h +++ b/src/events.h @@ -16,6 +16,7 @@ #include "main.h" +int EventFilter(const Event *); void EventLoop(void); void HandleOtherEvents(Event *); void ClearEventQueue(void); diff --git a/src/init.c b/src/init.c index ab51dd9f..92f52c56 100644 --- a/src/init.c +++ b/src/init.c @@ -14,6 +14,7 @@ #include #include "init.h" +#include "events.h" #include "misc.h" #include "sound.h" #include "screens.h" @@ -146,6 +147,31 @@ void InitSound() if (sound_status == SOUND_OFF) return; +#ifdef USE_SDL_LIBRARY + /* initialize SDL audio */ + + if (SDL_Init(SDL_INIT_AUDIO) < 0) + { + Error(ERR_WARN, "SDL_Init() failed: %s\n", SDL_GetError()); + sound_status = SOUND_OFF; + return; + } + + if (Mix_OpenAudio(22050, AUDIO_S16, 2, 256) < 0) + { + Error(ERR_WARN, "Mix_OpenAudio() failed: %s\n", SDL_GetError()); + sound_status = SOUND_OFF; + return; + } + + Mix_Volume(-1, SDL_MIX_MAXVOLUME / 4); + Mix_VolumeMusic(SDL_MIX_MAXVOLUME / 4); + + sound_status = SOUND_AVAILABLE; + sound_loops_allowed = TRUE; + +#else /* !USE_SDL_LIBRARY */ + #ifndef MSDOS if (access(sound_device_name, W_OK) != 0) { @@ -180,6 +206,7 @@ void InitSound() */ #endif /* MSDOS */ +#endif /* !USE_SDL_LIBRARY */ for(i=0; i= SDL_NumJoysticks()) + joystick_nr = -1; + + /* misuse joystick file descriptor variable to store joystick number */ + stored_player[i].joystick_fd = joystick_nr; + + /* this allows subsequent calls to 'InitJoysticks' for re-initialization */ + if (Check_SDL_JoystickOpened(joystick_nr)) + Close_SDL_Joystick(joystick_nr); + + if (!setup.input[i].use_joystick) + continue; + + if (!Open_SDL_Joystick(joystick_nr)) + { + Error(ERR_WARN, "cannot open joystick %d", joystick_nr); + continue; + } + + joystick_status = JOYSTICK_AVAILABLE; + } + +#else /* !USE_SDL_LIBRARY */ + #ifndef MSDOS for (i=0; i middle) + return 0; + if (margin > middle && actual < middle) + return 0; + + range = ABS(margin - middle); + pos = ABS(actual - middle); + percentage = (int)(pos * 100 / range); + + if (percentage > 100) + percentage = 100; + + return percentage; +} +#endif + +#ifdef USE_SDL_LIBRARY + +static SDL_Joystick *sdl_joystick[MAX_PLAYERS] = { NULL, NULL, NULL, NULL }; +static int sdl_js_axis[MAX_PLAYERS][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} }; +static int sdl_js_button[MAX_PLAYERS][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} }; + +SDL_Joystick *Get_SDL_Joystick(int nr) +{ + return sdl_joystick[nr]; +} + +boolean Open_SDL_Joystick(int nr) +{ + if (nr < 0 || nr > MAX_PLAYERS) + return FALSE; + + return ((sdl_joystick[nr] = SDL_JoystickOpen(nr)) == NULL ? FALSE : TRUE); +} + +void Close_SDL_Joystick(int nr) +{ + if (nr < 0 || nr > MAX_PLAYERS) + return; + + SDL_JoystickClose(sdl_joystick[nr]); +} + +boolean Check_SDL_JoystickOpened(int nr) +{ + if (nr < 0 || nr > MAX_PLAYERS) + return FALSE; + + return (SDL_JoystickOpened(nr) ? TRUE : FALSE); +} + +void HandleJoystickEvent(Event *event) +{ + switch(event->type) + { + case SDL_JOYAXISMOTION: + if (event->jaxis.axis < 2) + { + sdl_js_axis[event->jaxis.which][event->jaxis.axis]= event->jaxis.value; + +#if 0 + printf("js_%d %s-axis: %d\n", + event->jaxis.which, + (event->jaxis.axis == 0 ? "x" : "y"), + event->jaxis.value); +#endif + } + break; + + case SDL_JOYBUTTONDOWN: + if (event->jbutton.button < 2) + { + sdl_js_button[event->jbutton.which][event->jbutton.button] = TRUE; + +#if 0 + printf("js_%d button %d: pressed\n", + event->jbutton.which, + event->jbutton.button); +#endif + } + break; + + case SDL_JOYBUTTONUP: + if (event->jbutton.button < 2) + { + sdl_js_button[event->jbutton.which][event->jbutton.button] = FALSE; + +#if 0 + printf("js_%d button %d: released\n", + event->jbutton.which, + event->jbutton.button); +#endif + } + break; + + default: + break; + } +} + +int Get_SDL_Joystick_Axis(int nr, int axis) +{ + if (nr < 0 || nr > MAX_PLAYERS) + return 0; + + if (axis < 0 || axis > 1) + return 0; + + return sdl_js_axis[nr][axis]; +} + +void CheckJoystickData() +{ +} + +int Joystick(int player_nr) +{ + int joystick_nr = stored_player[player_nr].joystick_fd; + int js_x,js_y, js_b1,js_b2; + int left, right, up, down; + int result = 0; + + if (joystick_status == JOYSTICK_OFF) + return 0; + + if (game_status == SETUPINPUT) + return 0; + + if (!setup.input[player_nr].use_joystick || + !Check_SDL_JoystickOpened(joystick_nr)) + return 0; + + js_x = sdl_js_axis[joystick_nr][0]; + js_y = sdl_js_axis[joystick_nr][1]; + + js_b1 = sdl_js_button[joystick_nr][0]; + js_b2 = sdl_js_button[joystick_nr][1]; + + + +#if 0 + printf("JOYSTICK %d: js_x == %d, js_y == %d, js_b1 == %d, js_b2 == %d\n", + joystick_nr, js_x, js_y, js_b1, js_b2); +#endif + + + + left = JoystickPosition(setup.input[player_nr].joy.xmiddle, + setup.input[player_nr].joy.xleft, js_x); + right = JoystickPosition(setup.input[player_nr].joy.xmiddle, + setup.input[player_nr].joy.xright, js_x); + up = JoystickPosition(setup.input[player_nr].joy.ymiddle, + setup.input[player_nr].joy.yupper, js_y); + down = JoystickPosition(setup.input[player_nr].joy.ymiddle, + setup.input[player_nr].joy.ylower, js_y); + + if (left > JOYSTICK_PERCENT) + result |= JOY_LEFT; + else if (right > JOYSTICK_PERCENT) + result |= JOY_RIGHT; + if (up > JOYSTICK_PERCENT) + result |= JOY_UP; + else if (down > JOYSTICK_PERCENT) + result |= JOY_DOWN; + + if (js_b1) + result |= JOY_BUTTON_1; + if (js_b2) + result |= JOY_BUTTON_2; + + + +#if 0 + printf("result == 0x%08x\n", result); +#endif + + + + return result; +} + +#else /* !USE_SDL_LIBRARY */ + void CheckJoystickData() { int i; @@ -42,28 +231,6 @@ void CheckJoystickData() } } -#ifndef MSDOS -static int JoystickPosition(int middle, int margin, int actual) -{ - long range, pos; - int percentage; - - if (margin < middle && actual > middle) - return 0; - if (margin > middle && actual < middle) - return 0; - - range = ABS(margin - middle); - pos = ABS(actual - middle); - percentage = (int)(pos * 100 / range); - - if (percentage > 100) - percentage = 100; - - return percentage; -} -#endif - #ifndef MSDOS int Joystick(int player_nr) { @@ -196,6 +363,8 @@ int Joystick(int player_nr) } #endif /* MSDOS */ +#endif /* !USE_SDL_LIBRARY */ + int JoystickButton(int player_nr) { static int last_joy_button[MAX_PLAYERS] = { 0, 0, 0, 0 }; diff --git a/src/joystick.h b/src/joystick.h index a48c2bb3..64fd0069 100644 --- a/src/joystick.h +++ b/src/joystick.h @@ -34,12 +34,22 @@ /* get these values from the program 'js' from the joystick package, */ /* set JOYSTICK_PERCENT to a threshold appropriate for your joystick */ + +#ifdef USE_SDL_LIBRARY +#define JOYSTICK_XLEFT -32767 +#define JOYSTICK_XMIDDLE 0 +#define JOYSTICK_XRIGHT 32767 +#define JOYSTICK_YUPPER -32767 +#define JOYSTICK_YMIDDLE 0 +#define JOYSTICK_YLOWER 32767 +#else #define JOYSTICK_XLEFT 30 #define JOYSTICK_XMIDDLE 530 #define JOYSTICK_XRIGHT 1250 #define JOYSTICK_YUPPER 40 #define JOYSTICK_YMIDDLE 680 #define JOYSTICK_YLOWER 1440 +#endif #define JOYSTICK_PERCENT 25 @@ -63,6 +73,15 @@ #endif +#ifdef USE_SDL_LIBRARY +SDL_Joystick *Get_SDL_Joystick(int); +boolean Open_SDL_Joystick(int); +void Close_SDL_Joystick(int); +boolean Check_SDL_JoystickOpened(int); +void HandleJoystickEvent(Event *); +int Get_SDL_Joystick_Axis(int, int); +#endif + void CheckJoystickData(void); int Joystick(int); int JoystickButton(int); @@ -70,3 +89,4 @@ int AnyJoystick(void); int AnyJoystickButton(void); #endif + diff --git a/src/main.c b/src/main.c index 86b7eb17..7c51c2b6 100644 --- a/src/main.c +++ b/src/main.c @@ -747,6 +747,193 @@ void TEST_SDL_EVENT_LOOP() SDL_Quit(); } +#define SCREEN_WIDTH 640 +#define SCREEN_HEIGHT 480 + +void WatchJoysticks() +{ + SDL_Surface *screen; + const char *name; + int i, done; + SDL_Event event; + int x, y, draw; + SDL_Rect axis_area[2]; + int joystick_nr = 0; + SDL_Joystick *joystick = Get_SDL_Joystick(joystick_nr); + + /* Set a video mode to display joystick axis position */ + screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0); + if ( screen == NULL ) { + fprintf(stderr, "Couldn't set video mode: %s\n",SDL_GetError()); + return; + } + + /* Print info about the joysticks we are watching */ + for (i=0; i<2; i++) + { + joystick = Get_SDL_Joystick(i); + + name = SDL_JoystickName(i); + printf("Watching joystick %d: (%s)\n", i, + name ? name : "Unknown Joystick"); + printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n", + SDL_JoystickNumAxes(joystick), + SDL_JoystickNumHats(joystick), + SDL_JoystickNumBalls(joystick), + SDL_JoystickNumButtons(joystick)); + } + + /* Initialize drawing rectangles */ + memset(axis_area, 0, (sizeof axis_area)); + draw = 0; + + /* Loop, getting joystick events! */ + done = 0; + while ( ! done ) { + while ( SDL_PollEvent(&event) ) { + switch (event.type) { + case SDL_JOYAXISMOTION: + joystick_nr = event.jaxis.which; + printf("Joystick %d axis %d value: %d\n", + event.jaxis.which, + event.jaxis.axis, + event.jaxis.value); + break; + case SDL_JOYHATMOTION: + joystick_nr = event.jaxis.which; + printf("Joystick %d hat %d value:", + event.jhat.which, + event.jhat.hat); + if ( event.jhat.value == SDL_HAT_CENTERED ) + printf(" centered"); + if ( event.jhat.value & SDL_HAT_UP ) + printf(" up"); + if ( event.jhat.value & SDL_HAT_RIGHT ) + printf(" right"); + if ( event.jhat.value & SDL_HAT_DOWN ) + printf(" down"); + if ( event.jhat.value & SDL_HAT_LEFT ) + printf(" left"); + printf("\n"); + break; + case SDL_JOYBALLMOTION: + joystick_nr = event.jaxis.which; + printf("Joystick %d ball %d delta: (%d,%d)\n", + event.jball.which, + event.jball.ball, + event.jball.xrel, + event.jball.yrel); + break; + case SDL_JOYBUTTONDOWN: + joystick_nr = event.jaxis.which; + printf("Joystick %d button %d down\n", + event.jbutton.which, + event.jbutton.button); + break; + case SDL_JOYBUTTONUP: + joystick_nr = event.jaxis.which; + printf("Joystick %d button %d up\n", + event.jbutton.which, + event.jbutton.button); + break; + case SDL_KEYDOWN: + if ( event.key.keysym.sym != SDLK_ESCAPE ) { + break; + } + /* Fall through to signal quit */ + case SDL_QUIT: + done = 1; + break; + default: + break; + } + } + + joystick = Get_SDL_Joystick(joystick_nr); + + /* Update visual joystick state */ + for ( i=0; i (SCREEN_WIDTH-16) ) { + x = SCREEN_WIDTH-16; + } + y = (((int)SDL_JoystickGetAxis(joystick, 1))+32768); + y *= SCREEN_HEIGHT; + y /= 65535; + if ( y < 0 ) { + y = 0; + } else + if ( y > (SCREEN_HEIGHT-16) ) { + y = SCREEN_HEIGHT-16; + } + axis_area[draw].x = (Sint16)x; + axis_area[draw].y = (Sint16)y; + axis_area[draw].w = 16; + axis_area[draw].h = 16; + SDL_FillRect(screen, &axis_area[draw], 0xFFFF); + + SDL_UpdateRects(screen, 2, axis_area); + } +} + +void TEST_SDL_JOYSTICK() +{ + const char *name; + int i; + + /* Initialize SDL (Note: video is required to start event loop) */ + if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) < 0 ) + { + fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); + exit(1); + } + + /* Print information about the joysticks */ + printf("There are %d joysticks attached\n", SDL_NumJoysticks()); + for ( i=0; i= 1 && SDL_MINOR_VERSION >= 1 #include "SDL_image.h" +#include "SDL_mixer.h" #else #include "IMG.h" +#include "mixer.h" #endif /* SDL type definitions */ diff --git a/src/sound.c b/src/sound.c index 7839902a..e89a153f 100644 --- a/src/sound.c +++ b/src/sound.c @@ -715,18 +715,31 @@ boolean LoadSound(struct SoundInfo *snd_info) { char filename[256]; char *sound_ext = "wav"; +#ifndef USE_SDL_LIBRARY #ifndef MSDOS byte sound_header_buffer[WAV_HEADER_SIZE]; char chunk[CHUNK_ID_LEN + 1]; int chunk_length, dummy; FILE *file; int i; +#endif #endif sprintf(filename, "%s/%s/%s.%s", options.ro_base_directory, SOUNDS_DIRECTORY, snd_info->name, sound_ext); +#ifdef USE_SDL_LIBRARY + + snd_info->mix_chunk = Mix_LoadWAV(filename); + if (snd_info->mix_chunk == NULL) + { + Error(ERR_WARN, "cannot read sound file '%s' - no sounds", filename); + return FALSE; + } + +#else /* !USE_SDL_LIBRARY */ + #ifndef MSDOS if ((file = fopen(filename, "r")) == NULL) @@ -789,12 +802,13 @@ boolean LoadSound(struct SoundInfo *snd_info) if (!snd_info->sample_ptr) { Error(ERR_WARN, "cannot read sound file '%s' - no sounds", filename); - return(FALSE); + return FALSE; } #endif /* MSDOS */ +#endif /* !USE_SDL_LIBRARY */ - return(TRUE); + return TRUE; } void PlaySound(int nr) @@ -816,6 +830,17 @@ void PlaySoundExt(int nr, int volume, int stereo, boolean loop) { struct SoundControl snd_ctrl = emptySoundControl; +#ifdef USE_SDL_LIBRARY + Mix_PlayChannel(-1, Sound[nr].mix_chunk, 0); + + /* + Mix_Volume(-1, SDL_MIX_MAXVOLUME / 4); + Mix_VolumeMusic(SDL_MIX_MAXVOLUME / 4); + */ + + return; +#endif + if (sound_status==SOUND_OFF || !setup.sound) return; @@ -873,6 +898,11 @@ void StopSoundExt(int nr, int method) { struct SoundControl snd_ctrl = emptySoundControl; +#ifdef USE_SDL_LIBRARY + Mix_HaltMusic(); + return; +#endif + if (sound_status==SOUND_OFF) return; diff --git a/src/sound.h b/src/sound.h index 2e934709..dada0e89 100644 --- a/src/sound.h +++ b/src/sound.h @@ -130,9 +130,14 @@ struct SoundInfo char *name; byte *data_ptr; long data_len; + #ifdef MSDOS SAMPLE *sample_ptr; #endif + +#ifdef USE_SDL_LIBRARY + Mix_Chunk *mix_chunk; +#endif }; struct SoundControl diff --git a/src/system.c b/src/system.c index 3c182357..6b85ccb3 100644 --- a/src/system.c +++ b/src/system.c @@ -120,20 +120,20 @@ inline void KeyboardAutoRepeatOff() #endif } -inline boolean QueryPointer(DrawWindow window, int *win_x, int *win_y) +inline boolean PointerInWindow(DrawWindow window) { #ifdef USE_SDL_LIBRARY - SDL_GetMouseState(win_x, win_y); return TRUE; #else DrawWindow root, child; int root_x, root_y; unsigned int mask; + int win_x, win_y; /* if XQueryPointer() returns False, the pointer is not on the same screen as the specified window */ return XQueryPointer(display, window, &root, &child, &root_x, &root_y, - win_x, win_y, &mask); + &win_x, &win_y, &mask); #endif } diff --git a/src/system.h b/src/system.h index 01f5afe8..3ccdbb6a 100644 --- a/src/system.h +++ b/src/system.h @@ -45,7 +45,7 @@ inline void FlushDisplay(); inline void SyncDisplay(); inline void KeyboardAutoRepeatOn(); inline void KeyboardAutoRepeatOff(); -inline boolean QueryPointer(DrawWindow, int *, int *); +inline boolean PointerInWindow(DrawWindow); inline boolean PendingEvent(); inline void NextEvent(Event *event); diff --git a/src/tools.c b/src/tools.c index 3bf2c5a5..559e06bf 100644 --- a/src/tools.c +++ b/src/tools.c @@ -1888,9 +1888,7 @@ boolean Request(char *text, unsigned int req_state) { if (event.type == EVENT_MOTIONNOTIFY) { - int win_x, win_y; - - if (!QueryPointer(window, &win_x, &win_y)) + if (!PointerInWindow(window)) continue; /* window and pointer are on different screens */ if (!button_status)