这篇随笔专门做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的更多相关文章

  1. SDL_PingGe 1.2

    加入了像素填充函数,必须要在一个指定颜色的边界范围内,边界必须没有缺口. 加入了鼠标类 /* typedef void (*FUNCTION)(void); HMODULE HDll; HDll = ...

  2. dll文件32位64位检测工具以及Windows文件夹SysWow64的坑

    自从操作系统升级到64位以后,就要不断的需要面对32位.64位的问题.相信有很多人并不是很清楚32位程序与64位程序的区别,以及Program Files (x86),Program Files的区别 ...

  3. C#创建dll类库

    类库让我们的代码可复用,我们只需要在类库中声明变量一次,就能在接下来的过程中无数次地使用,而无需在每次使用前都要声明它.这样一来,就节省了我们的内存空间.而想要在类库添加什么类,还需取决于类库要实现哪 ...

  4. 关于Linux和Windows下部署mysql.data.dll的注册问题

    mysql ado.net connector下载地址: http://dev.mysql.com/downloads/connector/net/ 选择版本: Generally Available ...

  5. Windows平台Go调用DLL的坑

    最近的项目中,使用了GO来开发一些服务中转程序.业务比较简单,但是有一些业务需要复用原有C++开发的代码.而在WINDOWS,用CGO方式来集成C/C++代码并不是太方便.所以用DLL把C++的代码封 ...

  6. C#开发奇技淫巧三:把dll放在不同的目录让你的程序更整洁

    系列文章 C#开发奇技淫巧一:调试windows系统服务 C#开发奇技淫巧二:根据dll文件加载C++或者Delphi插件 C#开发奇技淫巧三:把dll放在不同的目录让你的程序更整洁 程序目录的整理 ...

  7. .Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程

    JSON作为一种轻量级的数据交换格式,简单灵活,被很多系统用来数据交互,作为一名.NET开发人员,JSON.NET无疑是最好的序列化框架,支持XML和JSON序列化,高性能,免费开源,支持LINQ查询 ...

  8. 使用Newtonsoft.Json.dll(JSON.NET)动态解析JSON、.net 的json的序列化与反序列化(一)

    在开发中,我非常喜欢动态语言和匿名对象带来的方便,JSON.NET具有动态序列化和反序列化任意JSON内容的能力,不必将它映射到具体的强类型对象,它可以处理不确定的类型(集合.字典.动态对象和匿名对象 ...

  9. ILMerge合并多个DLL

    序言 如果你的项目要提供多个dll给别人用,那么不妨让你的dll合并为一个,让别人看起来简洁,引用起来不会过于繁琐. 本篇比较少,但也算是比较实用吧. 下载微软的辅助工具ILMerge Imerge下 ...

随机推荐

  1. 第三篇:数据仓库系统的实现与使用(含OLAP重点讲解)

    前言 上一篇重点讲解了数据仓库建模,它是数据仓库开发中最核心的部分.然而完整的数据仓库系统还会涉及其他一些组件的开发,其中最主要的是ETL工程,在线分析处理工具(OLAP)和商务智能(BI)应用等. ...

  2. [转] java中的匿名内部类总结

    匿名内部类也就是没有名字的内部类 正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写 但使用匿名内部类还有个前提条件:必须继承一个父类或实现一个接口 实例1:不使用匿名内部类来实现抽象 ...

  3. iOS 启动连续闪退保护方案

    引言 “如果某个实体表现出以下任何一种特性,它就具备自主性:自我修复.自我保护.自我维护.对目标的自我控制.自我改进.” —— 凯文·凯利 iOS App 有时可能遇到启动必 crash 的绝境:每次 ...

  4. php 链式操作的实现 学习记录

    php 面向对象中实现链式操作的关键部分:调用的方法中返回当前对象 ,从而实现链式操作: <?php namespace commom; class db { public function w ...

  5. markdown 简明语法

    今天同事聊到markdown用法 之前不怎么了解  先把网上的建明语法贴出来 以备后用. 基本符号 *,-,+ 3个符号效果都一样,这3个符号被称为 Markdown符号 空白行表示另起一个段落 `是 ...

  6. ajax请求aspx页面

    首先,这么用是不好的.最好用ashx,但也难免遇到这种需求.开发过这么一个系统,每天访问量最多100,web服务器压力很小,完全大马拉小车,主要压力都在数据库服务器上,要做大量的统计.所以页面直接全上 ...

  7. Java实现文件上传

    最近自己在做一个小系统玩的时候涉及到了文件的上传,于是在网上找到Java上传文件的方案,最后确定使用common-fileupload实现上传操作. 需求说明 用户添加页面有一个“上传”按钮,点击按钮 ...

  8. 简易google地图api调用

    代码如下: <!DOCTYPE html> <html> <head> <meta name="viewport" content=&qu ...

  9. Binary XML file : Error inflating class com.esri.android.map.MapView

    在测试esri arcgis for android的第一个程序Helloworld的时候,报这样的错: Binary XML file : Error inflating class com.esr ...

  10. 使用Instant Client配置PL/SQL Developer

    之前使用PL/SQL Developer都是直接在本机安装完整版的Oracle Database,一是省事,二是可以在本机做一些demo测试:最近换了台电脑,感觉Instant Client更简单一些 ...