⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdl_cursor.c

📁 MPEG-4编解码的实现(包括MPEG4视音频编解码)
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
    SDL - Simple DirectMedia Layer
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Sam Lantinga
    slouken@libsdl.org
*/

#ifdef SAVE_RCSID
static char rcsid =
 "@(#) $Id: SDL_cursor.c,v 1.4 2002/04/22 21:38:03 wmay Exp $";
#endif

/* General cursor handling code for SDL */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "SDL_mutex.h"
#include "SDL_error.h"
#include "SDL_video.h"
#include "SDL_mouse.h"
#include "SDL_blit.h"
#include "SDL_events_c.h"
#include "SDL_sysvideo.h"
#include "SDL_sysevents.h"
#include "SDL_cursor_c.h"
#include "SDL_pixels_c.h"
#include "default_cursor.h"

/* These are static for our cursor handling code */
volatile int SDL_cursorstate = CURSOR_VISIBLE;
SDL_Cursor *SDL_cursor = NULL;
static SDL_Cursor *SDL_defcursor = NULL;
SDL_mutex *SDL_cursorlock = NULL;

/* Public functions */
void SDL_CursorQuit(void)
{
	if ( SDL_cursor != NULL ) {
		SDL_Cursor *cursor;

		SDL_cursorstate &= ~CURSOR_VISIBLE;
		if ( SDL_cursor != SDL_defcursor ) {
			SDL_FreeCursor(SDL_cursor);
		}
		SDL_cursor = NULL;
		if ( SDL_defcursor != NULL ) {
			cursor = SDL_defcursor;
			SDL_defcursor = NULL;
			SDL_FreeCursor(cursor);
		}
	}
	if ( SDL_cursorlock != NULL ) {
		SDL_DestroyMutex(SDL_cursorlock);
		SDL_cursorlock = NULL;
	}
}
int SDL_CursorInit(Uint32 multithreaded)
{
	/* We don't have mouse focus, and the cursor isn't drawn yet */
	SDL_cursorstate = CURSOR_VISIBLE;

	/* Create the default cursor */
	if ( SDL_defcursor == NULL ) {
		SDL_defcursor = SDL_CreateCursor(default_cdata, default_cmask,
					DEFAULT_CWIDTH, DEFAULT_CHEIGHT,
						DEFAULT_CHOTX, DEFAULT_CHOTY);
		SDL_SetCursor(SDL_defcursor);
	}

	/* Create a lock if necessary */
	if ( multithreaded ) {
		SDL_cursorlock = SDL_CreateMutex();
	}

	/* That's it! */
	return(0);
}

/* Multi-thread support for cursors */
#ifndef SDL_LockCursor
void SDL_LockCursor(void)
{
	if ( SDL_cursorlock ) {
		SDL_mutexP(SDL_cursorlock);
	}
}
#endif
#ifndef SDL_UnlockCursor
void SDL_UnlockCursor(void)
{
	if ( SDL_cursorlock ) {
		SDL_mutexV(SDL_cursorlock);
	}
}
#endif

/* Software cursor drawing support */
SDL_Cursor * SDL_CreateCursor (Uint8 *data, Uint8 *mask, 
					int w, int h, int hot_x, int hot_y)
{
	SDL_VideoDevice *video = current_video;
	int savelen;
	int i;
	SDL_Cursor *cursor;

	/* Make sure the width is a multiple of 8 */
	w = ((w+7)&~7);

	/* Sanity check the hot spot */
	if ( (hot_x < 0) || (hot_y < 0) || (hot_x >= w) || (hot_y >= h) ) {
		SDL_SetError("Cursor hot spot doesn't lie within cursor");
		return(NULL);
	}

	/* Allocate memory for the cursor */
	cursor = (SDL_Cursor *)malloc(sizeof *cursor);
	if ( cursor == NULL ) {
		SDL_OutOfMemory();
		return(NULL);
	}
	savelen = (w*4)*h;
	cursor->area.x = 0;
	cursor->area.y = 0;
	cursor->area.w = w;
	cursor->area.h = h;
	cursor->hot_x = hot_x;
	cursor->hot_y = hot_y;
	cursor->data = (Uint8 *)malloc((w/8)*h*2);
	cursor->mask = cursor->data+((w/8)*h);
	cursor->save[0] = (Uint8 *)malloc(savelen*2);
	cursor->save[1] = cursor->save[0] + savelen;
	cursor->wm_cursor = NULL;
	if ( ! cursor->data || ! cursor->save[0] ) {
		SDL_FreeCursor(cursor);
		SDL_OutOfMemory();
		return(NULL);
	}
	for ( i=((w/8)*h)-1; i>=0; --i ) {
		cursor->data[i] = data[i];
		cursor->mask[i] = mask[i] | data[i];
	}
	memset(cursor->save[0], 0, savelen*2);

	/* If the window manager gives us a good cursor, we're done! */
	if ( video->CreateWMCursor ) {
		cursor->wm_cursor = video->CreateWMCursor(video, data, mask,
							w, h, hot_x, hot_y);
	} else {
		cursor->wm_cursor = NULL;
	}
	return(cursor);
}

/* SDL_SetCursor(NULL) can be used to force the cursor redraw,
   if this is desired for any reason.  This is used when setting
   the video mode and when the SDL window gains the mouse focus.
 */
void SDL_SetCursor (SDL_Cursor *cursor)
{
	SDL_VideoDevice *video = current_video;
	SDL_VideoDevice *this  = current_video;

	/* Make sure that the video subsystem has been initialized */
	if ( ! video ) {
		return;
	}

	/* Prevent the event thread from moving the mouse */
	SDL_LockCursor();

	/* Set the new cursor */
	if ( cursor && (cursor != SDL_cursor) ) {
		/* Erase the current mouse position */
		if ( SHOULD_DRAWCURSOR(SDL_cursorstate) ) {
			SDL_EraseCursor(SDL_VideoSurface);
		} else if ( video->MoveWMCursor ) {
			/* If the video driver is moving the cursor directly,
			   it needs to hide the old cursor before (possibly)
			   showing the new one.  (But don't erase NULL cursor)
			 */
			if ( SDL_cursor ) {
				video->ShowWMCursor(this, NULL);
			}
		}
		SDL_cursor = cursor;
	}

	/* Draw the new mouse cursor */
	if ( SDL_cursor && (SDL_cursorstate&CURSOR_VISIBLE) ) {
		/* Use window manager cursor if possible */
		if ( SDL_cursor->wm_cursor && 
	             video->ShowWMCursor(this, SDL_cursor->wm_cursor) )
			SDL_cursorstate &= ~CURSOR_USINGSW;
		else {
			SDL_cursorstate |= CURSOR_USINGSW;
			if ( video->ShowWMCursor ) {
				video->ShowWMCursor(this, NULL);
			}
			{ int x, y;
				SDL_GetMouseState(&x, &y);
				SDL_cursor->area.x = (x - SDL_cursor->hot_x);
				SDL_cursor->area.y = (y - SDL_cursor->hot_y);
			}
			SDL_DrawCursor(SDL_VideoSurface);
		}
	} else {
		/* Erase window manager mouse (cursor not visible) */
		if ( SDL_cursor && (SDL_cursorstate & CURSOR_USINGSW) ) {
			SDL_EraseCursor(SDL_VideoSurface);
		} else {
			if ( video ) {
				video->ShowWMCursor(this, NULL);
			}
		}
	}
	SDL_UnlockCursor();
}

SDL_Cursor * SDL_GetCursor (void)
{
	return(SDL_cursor);
}

void SDL_FreeCursor (SDL_Cursor *cursor)
{
	if ( cursor ) {
		if ( cursor == SDL_cursor ) {
			SDL_SetCursor(SDL_defcursor);
		}
		if ( cursor != SDL_defcursor ) {
			SDL_VideoDevice *video = current_video;
			SDL_VideoDevice *this  = current_video;

			if ( cursor->data ) {
				free(cursor->data);
			}
			if ( cursor->save[0] ) {
				free(cursor->save[0]);
			}
			if ( video && cursor->wm_cursor ) {
				video->FreeWMCursor(this, cursor->wm_cursor);
			}
			free(cursor);
		}
	}
}

int SDL_ShowCursor (int toggle)
{
	int showing;

	showing = (SDL_cursorstate & CURSOR_VISIBLE);
	if ( toggle >= 0 ) {
		SDL_LockCursor();
		if ( toggle ) {
			SDL_cursorstate |= CURSOR_VISIBLE;
		} else {
			SDL_cursorstate &= ~CURSOR_VISIBLE;
		}
		SDL_UnlockCursor();
		if ( (SDL_cursorstate & CURSOR_VISIBLE) != showing ) {
			SDL_VideoDevice *video = current_video;
			SDL_VideoDevice *this  = current_video;

			SDL_SetCursor(NULL);
			if ( video && video->CheckMouseMode ) {
				video->CheckMouseMode(this);
			}
		}
	} else {
		/* Query current state */ ;
	}
	return(showing ? 1 : 0);
}

void SDL_WarpMouse (Uint16 x, Uint16 y)
{
	SDL_VideoDevice *video = current_video;
	SDL_VideoDevice *this  = current_video;

	/* This generates a mouse motion event */
	if ( video->WarpWMCursor ) {
		video->WarpWMCursor(this, x, y);
	} else {
		x += (this->screen->offset % this->screen->pitch) /
		      this->screen->format->BytesPerPixel;
		y += (this->screen->offset / this->screen->pitch);
		SDL_PrivateMouseMotion(0, 0, x, y);
	}
}

void SDL_MoveCursor(int x, int y)
{
	SDL_VideoDevice *video = current_video;

	/* Erase and update the current mouse position */
	if ( SHOULD_DRAWCURSOR(SDL_cursorstate) ) {
		/* Erase and redraw mouse cursor in new position */
		SDL_LockCursor();
		SDL_EraseCursor(SDL_VideoSurface);
		SDL_cursor->area.x = (x - SDL_cursor->hot_x);
		SDL_cursor->area.y = (y - SDL_cursor->hot_y);
		SDL_DrawCursor(SDL_VideoSurface);
		SDL_UnlockCursor();
	} else if ( video->MoveWMCursor ) {
		video->MoveWMCursor(video, x, y);
	}
}

/* Keep track of the current cursor colors */
static int palette_changed = 1;
static Uint32 pixels8[2];

void SDL_CursorPaletteChanged(void)
{
	palette_changed = 1;
}

void SDL_MouseRect(SDL_Rect *area)
{
	int clip_diff;

	*area = SDL_cursor->area;
	if ( area->x < 0 ) {
		area->w += area->x;
		area->x = 0;
	}
	if ( area->y < 0 ) {
		area->h += area->y;
		area->y = 0;
	}
	clip_diff = (area->x+area->w)-SDL_VideoSurface->w;
	if ( clip_diff > 0 ) {
		area->w = area->w < clip_diff ? 0 : area->w-clip_diff;
	}
	clip_diff = (area->y+area->h)-SDL_VideoSurface->h;
	if ( clip_diff > 0 ) {
		area->h = area->h < clip_diff ? 0 : area->h-clip_diff;
	}
}

static void SDL_DrawCursorFast(SDL_Surface *screen, SDL_Rect *area)
{
	const Uint32 pixels[2] = { 0xFFFFFFFF, 0x00000000 };
	int i, w, h;
	Uint8 *data, datab;
	Uint8 *mask, maskb;

	data = SDL_cursor->data + area->y * SDL_cursor->area.w/8;
	mask = SDL_cursor->mask + area->y * SDL_cursor->area.w/8;
	switch (screen->format->BytesPerPixel) {

	    case 1: {
		Uint8 *dst;
		int dstskip;

		if ( palette_changed ) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -