DLL——SDL_PingGe
这篇随笔专门做SDL的DLL开发。
下面这个版本暂且称为Beta版本吧。
/*
typedef void (*FUNCTION)(void);
HMODULE HDll;
HDll = LoadLibrary("SDL_PingGe.dll");
if(HDll == NULL)
{
printf("Load library failed!\n");
FreeLibrary(HDll);
return 0;
}
FUNCTION fun = FUNCTION(GetProcAddress(HDll,MAKEINTRESOURCE(1)));
*/
#ifndef __SDL_PINGGE_H__
#define __SDL_PINGGE_H__ //To use this exported function of dll, include this header in your project.
#include <windows.h> //The SDL header
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_mixer.h"
#include "SDL/SDL_net.h" #include <string>
#include <sstream>
#include <queue>
#include <cmath> #ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif #ifdef __cplusplus
extern "C"
{
#endif namespace PingGe
{
/*
Load the picture and optimize it!
If you want to set the colorkey,make the falg on true!
Return the surface!
*/
DLL_EXPORT SDL_Surface*
Load_Image(std::string filename, Uint8 R = 0x00, Uint8 G = 0x00, Uint8 B = 0x00, bool flag = false); /*
Set the color key
*/
DLL_EXPORT void
Color_Key(SDL_Surface* surface, Uint8 R, Uint8 G, Uint8 B); /*
Return true if lock success!
*/
DLL_EXPORT bool
Lock(SDL_Surface* surface); /*
Ulock the surface, if the surface is Locked!
*/
DLL_EXPORT void
ULock(SDL_Surface* surface); /*
Apply the source_surface on the destination_surface!
Return true if success!
*/
DLL_EXPORT bool
Apply_Surface(SDL_Surface* destination, Sint32 x, Sint32 y, SDL_Surface* source, SDL_Rect* clip = NULL); /*
Return the pixel value at (x, y)!
NOTE: The surface must be locked before calling this!
*/
DLL_EXPORT Uint32
SDL_GetPixel(SDL_Surface* surface, Sint32 x, Sint32 y); /*
Set the pixel at (x, y) to the given value!
NOTE: The surface must be locked before calling this!
*/
DLL_EXPORT void
SDL_PutPixel(SDL_Surface* surface, Sint32 x, Sint32 y, Uint32 pixel); /*
Seedfill the surface if the pixel isn't Fillcolor and Boundarycolor!
NOTE: The surface must be locked before calling this!
*/
DLL_EXPORT void
Seed_Fill(SDL_Surface *surface, Sint32 x, Sint32 y, Uint32 Fill_Color, Uint32 Boundary_Color); /*
Draw a line from (x1,y1) to (x2,y2)!
NOTE: The surface must be locked before calling this!
*/
DLL_EXPORT void
Draw_Line(SDL_Surface* surface, Sint32 x1, Sint32 y1, Sint32 x2, Sint32 y2, Uint32 pixel); /*
Draw a circle which the C is (xc,yc), R is radius!
NOTE: The surface must be locked before calling this!
*/
DLL_EXPORT void
Draw_Circle(SDL_Surface* surface, Sint32 xc, Sint32 yc, Sint32 radius, Uint32 pixel); /*
Draw a rect!
NOTE: The surface must be locked before calling this!
*/
DLL_EXPORT void
Draw_Rect(SDL_Surface* surface, Sint32 x1, Sint32 y1, Sint32 x2, Sint32 y2, Uint32 pixel); /*
Draw a Fillrect with pixel!
NOTE: The surface must be locked before calling this!
*/
DLL_EXPORT void
Draw_FillRect(SDL_Surface* surface, Sint32 x1, Sint32 y1, Sint32 x2, Sint32 y2, Uint32 pixel); /*
Fill color with color_fill in the boundary!
NOTE: The surface must be locked before calling this!
*/
DLL_EXPORT void
Color_Fill(SDL_Surface *surface, Sint32 x, Sint32 y, Uint32 fill_color, Uint32 boundary_color); /*
Zoom the surface as W:width and H:height!
If you don't use the old surface, better to free it!
NOTE: The surface must be locked before calling this!
*/
DLL_EXPORT SDL_Surface*
SDL_ScaleSurface(SDL_Surface *surface, Sint32 width, Sint32 height); //The timer
class DLL_EXPORT Timer
{
public:
//Initializes variables
Timer(void); //The various clock actions
void Start(void);
void Stop(void);
void Pause(void);
void Unpause(void); //Gets the timer's time
int Get_Ticks(void); //Checks the status of the timer
bool Is_Started(void);
bool Is_Paused(void);
private:
//The clock time when the timer started
int startTicks; //The ticks stored when the timer was paused
int pausedTicks; //The timer status
bool paused;
bool started;
}; //The logo shower
class DLL_EXPORT Logo
{
public:
//Initializes variables
Logo(SDL_Surface* _Logo, SDL_Surface* _Destination, Sint32 _Center_x, Sint32 _Center_y); //Reduce the logo 'Time' times, Fps
void Act1(Sint32 Time, Sint32 _Fps); //Fly in from left
void Act2(Sint32 _Fps); //Up to down show
void Act3(Sint32 _Fps); private:
SDL_Surface *logo, *destination; //The center-coordinate of the logo in the destination surface
Sint32 Center_x, Center_y;
}; //Hide the old_cursor and show the new_cursor
class DLL_EXPORT Cursor
{
public:
//Load the screen and the cursor_surface
Cursor(SDL_Surface *_background, SDL_Surface *_cursor, Sint32 _click_x = , Sint32 _click_y = ); void Get_Background(void);
void Cursor_Blit(void);
void Blit_Background(void);
void Update_Background(void); //You only need to use this function
void Show_Cursor(Sint32 _x, Sint32 _y); private:
SDL_Surface *cursor;
SDL_Surface *cursor_s;
SDL_Surface *background;
Sint32 x, y;
Sint32 click_x, click_y;
SDL_Rect old_rect;
}; //The button
class DLL_EXPORT Button
{
public:
//Initialize the variables
Button(SDL_Surface *surface, Sint32 x, Sint32 y, Sint32 w, Sint32 h); //Set the button nature
void Set_Button_Pic (SDL_Surface* surface);
void Set_MOUSEOVER_Clip (Sint32 x, Sint32 y, Sint32 w, Sint32 h);
void Set_MOUSEOUT_Clip (Sint32 x, Sint32 y, Sint32 w, Sint32 h);
void Set_MOUSEDOWN_Clip (Sint32 x, Sint32 y, Sint32 w, Sint32 h);
void Set_MOUSEUP_Clip (Sint32 x, Sint32 y, Sint32 w, Sint32 h); /*
Handles events and set the button's sprite region
Return Button::CLIP_MOUSEOVER, if cursor is over the button
etc..
*/
Sint32 Handle_Events(SDL_Event event); //Shows the button on the screen
void Show(void); private:
//The attributes of the button
SDL_Rect box; //The part of the button sprite sheet that will be shown
SDL_Rect* clip; //The button surface
SDL_Surface* button; //The background surface
SDL_Surface* background; //The clip cut a surface in four pieces
SDL_Rect clips[]; public:
//The button states in the sprite sheet
Sint32 CLIP_MOUSEOVER,
CLIP_MOUSEOUT,
CLIP_MOUSEDOWN,
CLIP_MOUSEUP;
};
} #ifdef __cplusplus
}
#endif //#undef VS_DLL #endif // __SDL_PINGGE_H__
#include "SDL_PingGe.h" using namespace std; namespace PingGe
{ DLL_EXPORT SDL_Surface*
Load_Image(std::string filename, Uint8 R, Uint8 G, Uint8 B, bool flag)
{
//The image that's loaded
SDL_Surface* loadedImage = NULL; //The optimized image that will be used
SDL_Surface* optimizedImage = NULL; //Load the image
loadedImage = IMG_Load(filename.c_str()); //If the image loaded
if(loadedImage != NULL)
{
//Create an optimized image
optimizedImage = SDL_DisplayFormatAlpha(loadedImage); //Free the old image
SDL_FreeSurface(loadedImage); //If the image was optimized just fine
if(optimizedImage != NULL && flag == true)
{
//Map the color key
Uint32 colorkey = SDL_MapRGB(optimizedImage->format, R, G, B); //Set all pixels of color R, G, B to be transparent
SDL_SetColorKey(optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey);
}
} //Return the optimized image
return optimizedImage;
} DLL_EXPORT void
Color_Key(SDL_Surface* surface, Uint8 R, Uint8 G, Uint8 B)
{
//Map the color key
Uint32 colorkey = SDL_MapRGB(surface->format, R, G, B); //Set all pixels of color R, G, B to be transparent
SDL_SetColorKey(surface, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey);
} DLL_EXPORT bool
Lock(SDL_Surface* surface)
{
if(SDL_MUSTLOCK(surface))
{
if(SDL_LockSurface(surface)<)
{
return false;
}
}
return true;
} DLL_EXPORT void
ULock(SDL_Surface* surface)
{
if(SDL_MUSTLOCK(surface))
{
SDL_UnlockSurface(surface);
}
} DLL_EXPORT bool
Apply_Surface(SDL_Surface* destination, Sint32 x, Sint32 y, SDL_Surface* source, SDL_Rect* clip)
{
//Holds offsets
SDL_Rect offset; //Get offsets
offset.x = x;
offset.y = y; //Blit
if(SDL_BlitSurface(source, clip, destination, &offset) == -)
{
return false;
}
return true;
} DLL_EXPORT Uint32
SDL_GetPixel(SDL_Surface* surface, Sint32 x, Sint32 y)
{
Sint32 bpp = surface->format->BytesPerPixel; /* Here p is the address to the pixel we want to retrieve */
Uint8 *p=(Uint8*)surface->pixels + y*surface->pitch + x*bpp;//? switch(bpp)
{
case :
{
return *p;
}
case :
{
return *(Uint16*)p;
}
case :
{
if(SDL_BYTEORDER==SDL_BIG_ENDIAN)
{
return p[]<<|p[]<<|p[];
}
else
{
return p[]|p[]<<|p[]<<;
}
}
case :
{
return *(Uint32*)p;
}
default:
{
return ; /* shouldn't happen, but avoids warnings */
}
}
} DLL_EXPORT void
SDL_PutPixel(SDL_Surface* surface, Sint32 x, Sint32 y, Uint32 pixel)
{
Sint32 bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8*)surface->pixels + y * surface->pitch + x * bpp; switch(bpp)
{
case :
{
*p = pixel;
}
break;
case :
{
*(Uint16*)p = pixel;
}
break;
case :
{
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
{
p[] = (pixel>>) & 0xff;
p[] = (pixel>>) & 0xff;
p[] = (pixel) & 0xff;
}
else
{
p[] = (pixel) & 0xff;
p[] = (pixel>>) & 0xff;
p[] = (pixel>>) & 0xff;
}
}
break;
case :
{
*(Uint32*)p = pixel;
}
break;
}
} DLL_EXPORT void
Seed_Fill(SDL_Surface *surface, Sint32 x, Sint32 y, Uint32 Fill_Color, Uint32 Boundary_Color)
{
Uint32 color = PingGe::SDL_GetPixel(surface, x, y); if((color != Boundary_Color) && (color != Fill_Color))
{
PingGe::SDL_PutPixel(surface, x, y, Fill_Color);
}
} DLL_EXPORT void
Draw_Line(SDL_Surface* surface, Sint32 x1, Sint32 y1, Sint32 x2, Sint32 y2, Uint32 pixel)
{
double delta_x, delta_y, x, y;
Sint32 dx, dy, steps, k;
dx = x2 - x1;
dy = y2 - y1; if(abs(dx) > abs(dy))
{
steps = abs(dx);
}
else
{
steps = abs(dy);
} delta_x = (double)dx / (double)steps;
delta_y = (double)dy / (double)steps; x = x1;
y = y1; PingGe::SDL_PutPixel(surface, x, y, pixel);
for(k = ; k < steps; k++)
{
x += delta_x;
y += delta_y;
PingGe::SDL_PutPixel(surface, x, y, pixel);
}
} DLL_EXPORT void
Draw_Circle(SDL_Surface* surface, Sint32 xc, Sint32 yc, Sint32 radius, Uint32 pixel)
{
Sint32 x, y, p;
x = ;
y = radius;
p = - * radius; while(x < y)
{
PingGe::SDL_PutPixel(surface, xc + x, yc + y, pixel);
PingGe::SDL_PutPixel(surface, xc - x, yc + y, pixel);
PingGe::SDL_PutPixel(surface, xc + x, yc - y, pixel);
PingGe::SDL_PutPixel(surface, xc - x, yc - y, pixel);
PingGe::SDL_PutPixel(surface, xc + y, yc + x, pixel);
PingGe::SDL_PutPixel(surface, xc - y, yc + x, pixel);
PingGe::SDL_PutPixel(surface, xc + y, yc - x, pixel);
PingGe::SDL_PutPixel(surface, xc - y, yc - x, pixel);
if(p < )
{
p = p + * x + ;
}
else
{
p = p + * (x - y) + ;
y -= ;
}
x += ;
}
if(x == y)
{
PingGe::SDL_PutPixel(surface, xc + x, yc + y, pixel);
PingGe::SDL_PutPixel(surface, xc - x, yc + y, pixel);
PingGe::SDL_PutPixel(surface, xc + x, yc - y, pixel);
PingGe::SDL_PutPixel(surface, xc - x, yc - y, pixel);
PingGe::SDL_PutPixel(surface, xc + y, yc + x, pixel);
PingGe::SDL_PutPixel(surface, xc - y, yc + x, pixel);
PingGe::SDL_PutPixel(surface, xc + y, yc - x, pixel);
PingGe::SDL_PutPixel(surface, xc - y, yc - x, pixel);
}
} DLL_EXPORT void
Draw_Rect(SDL_Surface* surface, Sint32 x1, Sint32 y1, Sint32 x2, Sint32 y2, Uint32 pixel)
{
PingGe::Draw_Line(surface, x1, y1, x2, y1, pixel);
PingGe::Draw_Line(surface, x2, y1, x2, y2, pixel);
PingGe::Draw_Line(surface, x2, y2, x1, y2, pixel);
PingGe::Draw_Line(surface, x1, y2, x1, y1, pixel);
} DLL_EXPORT void
Draw_FillRect(SDL_Surface* surface, Sint32 x1, Sint32 y1, Sint32 x2, Sint32 y2, Uint32 pixel)
{
for(int x = x1; x <= x2; x++)
{
PingGe::Draw_Line(surface, x, y1, x, y2, pixel);
}
} struct node
{
int x, y;
}temp, next;
DLL_EXPORT void
Color_Fill(SDL_Surface *surface, Sint32 x, Sint32 y, Uint32 fill_color, Uint32 boundary_color)
{
const int dir[][] = {{,},{,-},{,},{-,}};
temp.x=x;temp.y=y; queue<node>que;
que.push(temp);
while(!que.empty())
{
temp = que.front();
que.pop(); Uint32 color = PingGe::SDL_GetPixel(surface, temp.x, temp.y);
if((color == fill_color) || (color == boundary_color))
{
continue;
}
PingGe::SDL_PutPixel(surface, temp.x, temp.y, fill_color);
for(int i = ; i < ; i++)
{
next.x = temp.x + dir[i][];
next.y = temp.y + dir[i][];
que.push(next);
}
}
} DLL_EXPORT SDL_Surface*
SDL_ScaleSurface(SDL_Surface *surface, Sint32 width, Sint32 height)
{
SDL_Surface *_ret = SDL_CreateRGBSurface(surface->flags, width, height, surface->format->BitsPerPixel,
surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, surface->format->Amask); double _stretch_factor_x = (static_cast <double> (width) / static_cast <double> (surface->w)),
_stretch_factor_y = (static_cast <double> (height) / static_cast <double> (surface->h)); PingGe::Lock(_ret);
for(Sint32 y = ; y < surface->h; y++) //Run across all Y pixels.
{
for(Sint32 x = ; x < surface->w; x++) //Run across all X pixels.
{
for(Sint32 o_y = ; o_y < _stretch_factor_y; ++o_y) //Draw _stretch_factor_y pixels for each Y pixel.
{
for(Sint32 o_x = ; o_x < _stretch_factor_x; ++o_x) //Draw _stretch_factor_x pixels for each X pixel.
{
PingGe::SDL_PutPixel(_ret, static_cast<Sint32>(_stretch_factor_x * x) + o_x,
static_cast<Sint32>(_stretch_factor_y * y) + o_y, PingGe::SDL_GetPixel(surface, x, y));
}
}
}
}
PingGe::ULock(_ret); return _ret;
} /*****************Timer*****************/
Timer::Timer(void)
{
//Initialize the variables
startTicks = ;
pausedTicks = ;
paused = false;
started = false;
}
void Timer::Start(void)
{
//Start the timer
started = true; //Unpause the timer
paused = false; //Get the current clock time
startTicks = SDL_GetTicks();
}
void Timer::Stop(void)
{
//Stop the timer
started = false; //Unpause the timer
paused = false;
}
void Timer::Pause(void)
{
//If the timer is running and isn't already paused
if( ( started == true ) && ( paused == false ) )
{
//Pause the timer
paused = true; //Calculate the paused ticks
pausedTicks = SDL_GetTicks() - startTicks;
}
}
void Timer::Unpause(void)
{
//If the timer is paused
if( paused == true )
{
//Unpause the timer
paused = false; //Reset the starting ticks
startTicks = SDL_GetTicks() - pausedTicks; //Reset the paused ticks
pausedTicks = ;
}
}
int Timer::Get_Ticks(void)
{
//If the timer is running
if( started == true )
{
//If the timer is paused
if( paused == true )
{
//Return the number of ticks when the timer was paused
return pausedTicks;
}
else
{
//Return the current time minus the start time
return SDL_GetTicks() - startTicks;
}
} //If the timer isn't running
return ;
}
bool Timer::Is_Started(void)
{
return started;
}
bool Timer::Is_Paused(void)
{
return paused;
}
/*****************Timer*****************/ /***************Logo_Show***************/
Logo::Logo(SDL_Surface* _Logo, SDL_Surface* _Destination, Sint32 _Center_x, Sint32 _Center_y)
{
logo = _Logo;
destination = _Destination;
Center_x = _Center_x;
Center_y = _Center_y;
}
void Logo::Act1(Sint32 Time, Sint32 _Fps)
{
Timer Fps;
for(Sint32 i = ; i <= Time; i++)
{
Fps.Start();
SDL_Surface* img = PingGe::SDL_ScaleSurface(logo, (logo->w) - *i, (logo->h) - i); SDL_Rect Dest_rect;
Dest_rect.x = (Center_x - (img->w)/);
Dest_rect.y = (Center_y - (img->h)/); SDL_FillRect(destination, , SDL_MapRGB(destination->format, 0xff, 0xff, 0xff));
SDL_BlitSurface(img, , destination, &Dest_rect);
SDL_FreeSurface(img);
SDL_Flip(destination); if(Fps.Get_Ticks() < / _Fps)
{
SDL_Delay(( / _Fps) - Fps.Get_Ticks());
}
}
}
void Logo::Act2(Sint32 _Fps)
{
Timer Fps; SDL_Rect Dest_rect, clip[], Update_rect[]; Dest_rect.x = (destination->w - logo->w) / ;
Dest_rect.y = (destination->h - logo->h) / ; clip[].x = ; Update_rect[].x = ;
clip[].y = ; Update_rect[].y = Dest_rect.y;
clip[].w = ; Update_rect[].w = Dest_rect.x + ;
clip[].h = ; Update_rect[].h = logo->h; clip[].x = ; Update_rect[].x = ;
clip[].y = ; Update_rect[].y = Dest_rect.y;
clip[].w = ; Update_rect[].w = Dest_rect.x + ;
clip[].h = ; Update_rect[].h = logo->h; clip[].x = ; Update_rect[].x = ;
clip[].y = ; Update_rect[].y = Dest_rect.y;
clip[].w = ; Update_rect[].w = Dest_rect.x + ;
clip[].h = ; Update_rect[].h = logo->h; SDL_FillRect(destination, , SDL_MapRGB(destination->format, 0xff, 0xff, 0xff));
SDL_Flip(destination);
for(int i = -clip[].w; i <= Dest_rect.x + clip[].x; i++)
{
Fps.Start(); SDL_FillRect(destination, , SDL_MapRGB(destination->format, 0xff, 0xff, 0xff));
PingGe::Apply_Surface(destination, i, Dest_rect.y + clip[].y, logo, &clip[]);
SDL_UpdateRects(destination, , &Update_rect[]); if(Fps.Get_Ticks() < / _Fps)
{
SDL_Delay(( / _Fps) - Fps.Get_Ticks());
}
}
for(int i = -clip[].w; i <= Dest_rect.x + clip[].x; i++)
{
Fps.Start(); SDL_FillRect(destination, , SDL_MapRGB(destination->format, 0xff, 0xff, 0xff));
PingGe::Apply_Surface(destination, i, Dest_rect.y + clip[].y, logo, &clip[]);
SDL_UpdateRects(destination, , &Update_rect[]); if(Fps.Get_Ticks() < / _Fps)
{
SDL_Delay(( / _Fps) - Fps.Get_Ticks());
}
}
for(int i = -clip[].w; i <= Dest_rect.x + clip[].x; i++)
{
Fps.Start(); SDL_FillRect(destination, , SDL_MapRGB(destination->format, 0xff, 0xff, 0xff));
PingGe::Apply_Surface(destination, i, Dest_rect.y + clip[].y, logo, &clip[]);
SDL_UpdateRects(destination, , &Update_rect[]); if(Fps.Get_Ticks() < / _Fps)
{
SDL_Delay(( / _Fps) - Fps.Get_Ticks());
}
}
Act3(_Fps);
}
void Logo::Act3(Sint32 _Fps)
{
Timer Fps; SDL_Rect Dest_rect, up_down_clip, right_left_clip, small_rect_clip; Dest_rect.x = (destination->w - logo->w) / ;
Dest_rect.y = (destination->h - logo->h) / ; up_down_clip.x = Dest_rect.x;
up_down_clip.y = Dest_rect.y;
up_down_clip.w = logo->w;
up_down_clip.h = ; right_left_clip.x = Dest_rect.x + ;
right_left_clip.y = Dest_rect.y + ;
right_left_clip.w = ;
right_left_clip.h = logo->h; small_rect_clip.x = Dest_rect.x + ;
small_rect_clip.y = Dest_rect.y + ;
small_rect_clip.w = ;
small_rect_clip.h = ; PingGe::Apply_Surface(destination, Dest_rect.x, Dest_rect.y, logo);
for(int i = up_down_clip.y; i <= right_left_clip.y; i++)
{
Fps.Start(); up_down_clip.y++;
SDL_UpdateRects(destination, , &up_down_clip); if(Fps.Get_Ticks() < / _Fps)
{
SDL_Delay(( / _Fps) - Fps.Get_Ticks());
}
} SDL_UpdateRects(destination, , &small_rect_clip); for(int i = right_left_clip.x; i >= up_down_clip.x; i--)
{
Fps.Start(); right_left_clip.x--;
SDL_UpdateRects(destination, , &right_left_clip); if(Fps.Get_Ticks() < / _Fps)
{
SDL_Delay(( / _Fps) - Fps.Get_Ticks());
}
}
}
/***************Logo_Show***************/ /****************Cursor****************/
Cursor::Cursor(SDL_Surface *_background, SDL_Surface *_cursor, Sint32 _click_x, Sint32 _click_y)
{
background = _background;
cursor = _cursor;
cursor_s = SDL_CreateRGBSurface(_cursor->flags, _cursor->w, _cursor->h, _cursor->format->BitsPerPixel,
_cursor->format->Rmask, _cursor->format->Gmask, _cursor->format->Bmask, _cursor->format->Amask); click_x = _click_x;
click_y = _click_y; //Close the old cursor
SDL_ShowCursor(); old_rect.x = ;
old_rect.y = ;
old_rect.w = ;
old_rect.h = ;
}
void Cursor::Get_Background(void)
{
/* Blits a surface sized chunk of background to that surface */
SDL_Rect src;
SDL_Rect dst; src.x = x;
src.y = y;
src.w = cursor_s->w;
src.h = cursor_s->h; dst.x = ;
dst.y = ;
dst.w = cursor_s->w;
dst.h = cursor_s->h;
SDL_BlitSurface(background, &src, cursor_s, &dst);
}
void Cursor::Cursor_Blit(void)
{
SDL_Rect dest; dest.x = x;
dest.y = y;
dest.w = cursor->w;
dest.h = cursor->h;
SDL_BlitSurface(cursor, NULL, background, &dest);
}
void Cursor::Blit_Background(void)
{
SDL_Rect dest; dest.x = x;
dest.y = y;
dest.w = cursor_s->w;
dest.h = cursor_s->h;
SDL_BlitSurface(cursor_s, NULL, background, &dest);
old_rect = dest;
}
void Cursor::Update_Background(void)
{
SDL_Rect clip[];
clip[].x = x-;
clip[].y = y-;
clip[].w = cursor->w+;
clip[].h = cursor->h+;
clip[] = old_rect; clip[].x = clip[].x-;
clip[].y = clip[].y-;
clip[].w = clip[].w+;
clip[].h = clip[].h+;
SDL_UpdateRects(background, , clip);
}
void Cursor::Show_Cursor(Sint32 _x, Sint32 _y)
{
x = _x - click_x;
y = _y - click_y;
Get_Background();
Cursor_Blit();
Update_Background();
Blit_Background();
}
/****************Cursor****************/ /****************Button****************/
Button::Button(SDL_Surface *surface, Sint32 x, Sint32 y, Sint32 w, Sint32 h)
{
//Init
CLIP_MOUSEOVER = ;
CLIP_MOUSEOUT = ;
CLIP_MOUSEDOWN = ;
CLIP_MOUSEUP = ; //Set the background surface
background = surface; //Set the button's attributes
box.x = x;
box.y = y;
box.w = w;
box.h = h; //Set the default sprite
clip = &clips[ CLIP_MOUSEOUT ];
}
void Button::Set_Button_Pic(SDL_Surface* surface)
{
button = surface;
}
void Button::Set_MOUSEOVER_Clip (Sint32 x, Sint32 y, Sint32 w, Sint32 h)
{
clips[ CLIP_MOUSEOVER ].x = x;
clips[ CLIP_MOUSEOVER ].y = y;
clips[ CLIP_MOUSEOVER ].w = w;
clips[ CLIP_MOUSEOVER ].h = h;
}
void Button::Set_MOUSEOUT_Clip (Sint32 x, Sint32 y, Sint32 w, Sint32 h)
{
clips[ CLIP_MOUSEOUT ].x = x;
clips[ CLIP_MOUSEOUT ].y = y;
clips[ CLIP_MOUSEOUT ].w = w;
clips[ CLIP_MOUSEOUT ].h = h;
}
void Button::Set_MOUSEDOWN_Clip (Sint32 x, Sint32 y, Sint32 w, Sint32 h)
{
clips[ CLIP_MOUSEDOWN ].x = x;
clips[ CLIP_MOUSEDOWN ].y = y;
clips[ CLIP_MOUSEDOWN ].w = w;
clips[ CLIP_MOUSEDOWN ].h = h;
}
void Button::Set_MOUSEUP_Clip (Sint32 x, Sint32 y, Sint32 w, Sint32 h)
{
clips[ CLIP_MOUSEUP ].x = x;
clips[ CLIP_MOUSEUP ].y = y;
clips[ CLIP_MOUSEUP ].w = w;
clips[ CLIP_MOUSEUP ].h = h;
}
Sint32 Button::Handle_Events(SDL_Event event)
{
//The mouse offsets
Sint32 x = , y = ; //If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y; //If the mouse is over the button
if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )
{
//Set the button sprite
clip = &clips[ CLIP_MOUSEOVER ]; return CLIP_MOUSEOVER;
}
//If not
else
{
//Set the button sprite
clip = &clips[ CLIP_MOUSEOUT ]; return CLIP_MOUSEOUT;
}
}
//If a mouse button was pressed
if( event.type == SDL_MOUSEBUTTONDOWN )
{
//If the left mouse button was pressed
if( event.button.button == SDL_BUTTON_LEFT )
{
//Get the mouse offsets
x = event.button.x;
y = event.button.y; //If the mouse is over the button
if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )
{
//Set the button sprite
clip = &clips[ CLIP_MOUSEDOWN ]; return CLIP_MOUSEDOWN;
}
}
}
//If a mouse button was released
if( event.type == SDL_MOUSEBUTTONUP )
{
//If the left mouse button was released
if( event.button.button == SDL_BUTTON_LEFT )
{
//Get the mouse offsets
x = event.button.x;
y = event.button.y; //If the mouse is over the button
if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )
{
//Set the button sprite
clip = &clips[ CLIP_MOUSEUP ]; return CLIP_MOUSEUP;
}
}
}
return -;
}
void Button::Show(void)
{
//Show the button
PingGe::Apply_Surface(background, box.x, box.y, button, clip);
}
/****************Button****************/
}
DLL——SDL_PingGe的更多相关文章
- SDL_PingGe 1.2
加入了像素填充函数,必须要在一个指定颜色的边界范围内,边界必须没有缺口. 加入了鼠标类 /* typedef void (*FUNCTION)(void); HMODULE HDll; HDll = ...
- dll文件32位64位检测工具以及Windows文件夹SysWow64的坑
自从操作系统升级到64位以后,就要不断的需要面对32位.64位的问题.相信有很多人并不是很清楚32位程序与64位程序的区别,以及Program Files (x86),Program Files的区别 ...
- C#创建dll类库
类库让我们的代码可复用,我们只需要在类库中声明变量一次,就能在接下来的过程中无数次地使用,而无需在每次使用前都要声明它.这样一来,就节省了我们的内存空间.而想要在类库添加什么类,还需取决于类库要实现哪 ...
- 关于Linux和Windows下部署mysql.data.dll的注册问题
mysql ado.net connector下载地址: http://dev.mysql.com/downloads/connector/net/ 选择版本: Generally Available ...
- Windows平台Go调用DLL的坑
最近的项目中,使用了GO来开发一些服务中转程序.业务比较简单,但是有一些业务需要复用原有C++开发的代码.而在WINDOWS,用CGO方式来集成C/C++代码并不是太方便.所以用DLL把C++的代码封 ...
- C#开发奇技淫巧三:把dll放在不同的目录让你的程序更整洁
系列文章 C#开发奇技淫巧一:调试windows系统服务 C#开发奇技淫巧二:根据dll文件加载C++或者Delphi插件 C#开发奇技淫巧三:把dll放在不同的目录让你的程序更整洁 程序目录的整理 ...
- .Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程
JSON作为一种轻量级的数据交换格式,简单灵活,被很多系统用来数据交互,作为一名.NET开发人员,JSON.NET无疑是最好的序列化框架,支持XML和JSON序列化,高性能,免费开源,支持LINQ查询 ...
- 使用Newtonsoft.Json.dll(JSON.NET)动态解析JSON、.net 的json的序列化与反序列化(一)
在开发中,我非常喜欢动态语言和匿名对象带来的方便,JSON.NET具有动态序列化和反序列化任意JSON内容的能力,不必将它映射到具体的强类型对象,它可以处理不确定的类型(集合.字典.动态对象和匿名对象 ...
- ILMerge合并多个DLL
序言 如果你的项目要提供多个dll给别人用,那么不妨让你的dll合并为一个,让别人看起来简洁,引用起来不会过于繁琐. 本篇比较少,但也算是比较实用吧. 下载微软的辅助工具ILMerge Imerge下 ...
随机推荐
- 深入探索C++对象模型-5
虚拟继承下的对象构造: 由于虚拟基类对象在子类中只能保持一个实例,那么,子类构造的时候调用父类的构造函数的时候必须保证虚拟基类对象不能够重复构造. 那么如何保证基类对象的唯一性? C++规定虚拟基类对 ...
- Linux FTP YUM源搭建简单记录
1. 挂载iso镜像,进入Packages目录下安装FTP服务 #rpm -ivh vsftpd-3.0.2-10.el7.x86_64.rpm 2. 修改vsftpd配置文件如下 [root@rus ...
- hibernate_validator_06
认证组(校验组) 校验组能够让你在验证的时候选择应用哪些约束条件. 这样在某些情况下( 例如向导 ) 就可以对每一步进行校验的时候, 选取对应这步的那些约束条件进行验证了. 校验组是通过可变参数传递给 ...
- 爬虫爬oj用户头像
import requests import Queue import urllib import urllib2 import re import requests alreadyImg = set ...
- 你好,C++(5)如何输出数据到屏幕、从屏幕输入数据与读写文件?
2.2 基本输入/输出流 听过HelloWorld.exe的自我介绍之后,大家已经知道了一个C++程序的任务就是描述数据和处理数据.这两大任务的对象都是数据,可现在的问题是,数据不可能无中生有地产生 ...
- Git对于单个文件的分批提交方式的使用
很多时候,对于一个大的文件,可能有的同学改完之后不想一次提交,想分批提交.但这个时候由于git add的机制往往add之后就是整个一个文件被放到stage区了,这个时候肯定会想能不能对一个文件可以进行 ...
- CentOS7 开启关闭网卡
ifdown ifcfg-enp7s0 关闭网卡 ifup ifcfg-enp7s0 开启网卡
- 【转载】Think as Customer 以客户为中心的测试理念
纵观各大公司的核心理念,往往都有一条类似“以客户为中心”的价值观.华为公司更是把“以客户为中心”放在其核心价值观的第一条,以显示它的重要性.从我 们入职培训开始,公司就反复强调并引导大家深入讨论,希望 ...
- sublime常用插件及配置,自留自用
1.Angularjs 写angularjs经常操作template文件,没有一个ng-xx的提示真的很蛋疼是不是,有些服务的名字记不住是不是,那就用这个插件吧 2.AutoFileName 如果你的 ...
- php while循环 指定显示内容 例如不想显示前10条和后10条
<?php //查询信息总的条数 $db_num = query_num("表","where 1=1"); //每页显示的条数 $page_size=2 ...