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

📄 surfaceclass.cpp

📁 SDL_Draw chinese test
💻 CPP
字号:
//UVi Soft (2008)
//Long Fei (lf426), E-mail: zbln426@163.com

#include "SurfaceClass.h"
#include "font.h"

//VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
//class ScreenSurface

int ScreenSurface::screenNum = 0;

ScreenSurface::ScreenSurface():
width(640), height(480), bpp(32), flags(0), windowName("NULL")
{
	if ( screenNum > 0 )
		throw ErrorInfo("DONOT create more than ONE screen!");
	if ( SDL_Init(SDL_INIT_VIDEO < 0 ) )
		throw ErrorInfo(SDL_GetError());
	pScreen = SDL_SetVideoMode(width, height, bpp, flags);
	screenNum++;
}

ScreenSurface::ScreenSurface(int w, int h, const std::string& window_name, int b, Uint32 f):
width(w), height(h), bpp(b), flags(f)
{
	if ( screenNum > 0 )
		throw ErrorInfo("DONOT create more than ONE screen!");
	if ( SDL_Init(SDL_INIT_VIDEO < 0 ) )
		throw ErrorInfo(SDL_GetError());
	pScreen = SDL_SetVideoMode(width, height, bpp, flags);
	screenNum++;
	if ( window_name != "NULL" ) {
		windowName = window_name;
		SDL_WM_SetCaption(windowName.c_str(), 0);
	}
	else
		windowName = "NULL";
}

ScreenSurface::~ScreenSurface()
{
	SDL_Quit();
}

SDL_Surface* ScreenSurface::point() const
{
	return pScreen;
}

void ScreenSurface::flip() const
{
	if ( SDL_Flip(pScreen) < 0 )
		throw ErrorInfo(SDL_GetError());
}


void ScreenSurface::fillColor(Uint8 r, Uint8 g, Uint8 b) const
{
	 if ( SDL_FillRect(pScreen, 0, SDL_MapRGB(pScreen->format, r, g, b)) < 0 )
		 throw ErrorInfo(SDL_GetError());
}

//AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

//VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
//class DisplaySurface

int DisplaySurface::textNum = 0;

DisplaySurface::DisplaySurface(const std::string& file_name, const ScreenSurface& screen):
fileName(file_name), pFont(0)
{
	SDL_Surface* pSurfaceTemp = IMG_Load(file_name.c_str());
	if ( pSurfaceTemp == 0 )
		throw ErrorInfo(SDL_GetError());
	pSurface = SDL_DisplayFormat(pSurfaceTemp);
	if ( pSurface == 0 )
		throw ErrorInfo(SDL_GetError());
	SDL_FreeSurface(pSurfaceTemp);
	pScreen = screen.point();
}

DisplaySurface::~DisplaySurface()
{
	SDL_FreeSurface(pSurface);
}

SDL_Surface* DisplaySurface::point() const
{
	return pSurface;
}

void DisplaySurface::blit() const
{
	if ( SDL_BlitSurface(pSurface, 0, pScreen, 0) < 0 )
		throw ErrorInfo(SDL_GetError());
}


void DisplaySurface::blit(int at_x, int at_y) const
{
	SDL_Rect offset;
	offset.x = at_x;
	offset.y = at_y;

	if ( SDL_BlitSurface(pSurface, 0, pScreen, &offset) < 0 )
		throw ErrorInfo(SDL_GetError());
}

void DisplaySurface::blit(int at_x, int at_y,
						  int from_x, int from_y, int w, int h,
						  int delta_x, int delta_y) const
{
	SDL_Rect offset;
	offset.x = at_x - delta_x;
	offset.y = at_y - delta_y;

	SDL_Rect dest;
	dest.x = from_x - delta_x;
	dest.y = from_y - delta_y;
	dest.w = w + delta_x*2;
	dest.h = h + delta_y*2;

	if ( SDL_BlitSurface(pSurface, &dest, pScreen, &offset) < 0 )
		throw ErrorInfo(SDL_GetError());
}

void DisplaySurface::colorKey(Uint8 r, Uint8 g, Uint8 b, Uint32 flag)
{
	Uint32 colorkey = SDL_MapRGB(pSurface->format, r, g, b);
	if ( SDL_SetColorKey(pSurface, flag, colorkey ) < 0 )
		throw ErrorInfo(SDL_GetError());
}

//for TextSurface
DisplaySurface::DisplaySurface(const std::string& msg_name, const std::string& message, const ScreenSurface& screen,
					Uint8 r, Uint8 g , Uint8 b, 
					int ttf_size, const std::string& ttf_fileName):
fileName(msg_name)
{
	if ( textNum == 0 )
		if ( TTF_Init() < 0 )
			throw ErrorInfo("TTF_Init() failed!");
	
	SDL_Color textColor;
	textColor.r = r;
	textColor.g = g;
	textColor.b = b;

	pFont = TTF_OpenFont(ttf_fileName.c_str(), ttf_size);
	if ( pFont == 0 )
		throw ErrorInfo("TTF_OpenFont() failed!");

	pSurface = myTTF_RenderString_Blended(pFont, message, textColor);
	if ( pSurface == 0 )
		throw ErrorInfo("myTTF_RenderString_Blended() failed!");
	pScreen = screen.point();

	textNum++;
}

int DisplaySurface::tellTextNum() const
{
	return textNum;
}

void DisplaySurface::reduceTextNum()
{
	textNum--;
}

void DisplaySurface::deleteFontPoint()
{
	TTF_CloseFont(pFont);
}

//AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

//VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
//class TextSurface

TextSurface::TextSurface(const std::string& msg_name, const std::string& message, const ScreenSurface& screen,
					Uint8 r, Uint8 g, Uint8 b, 
					int ttf_size, const std::string& ttf_fileName):
DisplaySurface(msg_name, message, screen, r, g, b, ttf_size, ttf_fileName)
{}

TextSurface::~TextSurface()
{
	deleteFontPoint();
	reduceTextNum();
	if ( tellTextNum() == 0 )
		TTF_Quit();
}

//AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

⌨️ 快捷键说明

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