The current repo belongs to Closed status, and some functions are restricted. For details, please refer to the description of repo status
Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
seg.cpp 30.83 KB
Copy Edit Raw Blame History
Luminecraft authored 2023-12-12 15:07 . start.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
/*
Simple and easier game eng.
*/
#pragma once
#include "seg.h"
#ifndef _SEG_LIBC
#define _SEG_LIBC
auto seg::Times::get_ticks_ns(){
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
};
long long seg::Times::get_ticks()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
unsigned int seg::Times::Clock::get_fps()
{
return this->fps;
}
void seg::Times::Clock::tick(int t)
{
this ->Tick = t + 8;
//Sleep((DWORD)(1000.0/t));
}
void seg::Times::Clock::tick()
{
if (this->Tick < 900) Sleep((1000 / this->Tick));
(this->frame)++;
long long fmt_tmp = seg::Times::get_ticks_ns();
unsigned int fmin_tmp = (unsigned int)(1000000.0 / ((fmt_tmp - this->fps_min_tick)/*>0 ? (fmt_tmp - this->fps_min_tick) : 0.1*/) );
if (fmin_tmp != 0 && fmin_tmp < this->fps_min) this->fps_min = fmin_tmp;
this->fps_min_tick = fmt_tmp;
if (time(nullptr) > this->T0){
this->T0 = time(nullptr);
this->fps = this->frame;
this->frame = 0;
this->tps = this->ticks_in_one_second;
this->ticks_in_one_second = 0;
this->fps_min_ask = this->fps_min;
this->fps_min = 65535;
}
}
void seg::Times::Clock::init()
{
this->T0 = time(nullptr);
this->Tick = 60;
this->frame = 0;
this->fps = 0;
this->ticks_in_one_second = 0;
this->tps = 0;
//
this->fps_min_tick = seg::Times::get_ticks_ns();
this->fps_min = UINT_MAX;
this->fps_min_ask = 0;
}
void seg::render::text(int x, int y, TCHAR *s, seg::render::Place dtype)
{
switch (dtype)
{
case seg::render::lefttop:{
outtextxy(int(x), (int)y, static_cast<LPCTSTR>(s));
}
break;
case seg::render::righttop: {
outtextxy(int((int)x - textwidth(s)), (int)y, (LPCTSTR)s);
}
break;
case seg::render::leftbottom: {
outtextxy(int(x), (int)y - textheight(s), (LPCTSTR)s);
}
break;
case seg::render::rightbottom: {
outtextxy(int(x) - textwidth(s), (int)y - textheight(s), (LPCTSTR)s);
}
break;
//
case seg::render::left:{
outtextxy(int(x), (int)y - int(textheight(s) / 2), static_cast<LPCTSTR>(s));
}
break;
case seg::render::top:{
outtextxy(int(x) - int(textwidth(s) / 2), (int)y, static_cast<LPCTSTR>(s));
}
break;
case seg::render::right:{
outtextxy(int(x) - textwidth(s), (int)y - int(textheight(s) / 2), static_cast<LPCTSTR>(s));
}
break;
case seg::render::bottom:{
outtextxy(int(x) - int(textwidth(s) / 2), (int)y - textheight(s), static_cast<LPCTSTR>(s));
}
break;
case seg::render::center:{
outtextxy(int(x) - int(textwidth(s) / 2), (int)y - int(textheight(s) / 2), static_cast<LPCTSTR>(s));
}
break;
default:
outtextxy(int(x), (int)y, static_cast<LPCTSTR>(s));
break;
}
}
void seg::EasyX_Initialize(int width, int height, LPCWSTR WinTitle)
{
seg::MainHandle = initgraph(width, height, 0);
seg::Window.width = width;
seg::Window.height = height;
seg::Window.SCR = GetWorkingImage();
SetWindowTextW(seg::MainHandle, WinTitle);
setbkmode(TRANSPARENT);
BeginBatchDraw();
}
void seg::EasyX_Initialize(int w, int h, LPCWSTR WinTitle, int flag)
{
seg::MainHandle = initgraph(w, h, flag);
seg::Window.width = w;
seg::Window.height = h;
seg::Window.SCR = GetWorkingImage();
SetWindowTextW(seg::MainHandle, WinTitle);
setbkmode(TRANSPARENT);
BeginBatchDraw();
}
int seg::ClickableObject::_on_click()
{
int ret = 0;
if (this->ClickAsInput == false)
{
seg::ClickableObject* temp = this;
ret = this->OnClick(*temp);
}
else
{
pass;//拦截该消息
}
return ret;
}
int seg::ClickableObject::_on_click(MOUSEMSG & _m, double perx, double pery)
{
int ret = 0;
if (this->ClickAsInput == false)
{
seg::ClickableObject* temp = this;
ret = this->OnClickEx(*temp, _m, perx, pery);
}
else
{
pass;//拦截该消息
}
return ret;
}
void seg::ClickableObject::_render()
{
if (this->AutoRender == true)
{
setlinecolor(this->fg);
settextcolor(this->TextColor);
setfillcolor(this->bg);
setlinestyle(this->LineStyle, this->LineWidth);
fillrectangle(this->x, this->y, (this->x + this->width), (this->y + this->height));
rectangle(this->x, this->y, (this->x + this->width), (this->y + this->height));
if (this->s.count_line() > 1)
{
seg::render::multiline
(
this->x, this->y, this->s, (this->height / this->s.count_line()), seg::render::Place::lefttop
);
} else
seg::render::text(
(this->x + this->x + this->width) / 2,
(this->y + this->y + this->height ) / 2,
(TCHAR*)(this->s.get()),
seg::render::Place::center
);
}
else
{
seg::ClickableObject* temp = this;
this->render(*temp);
}
}
void seg::NoneTypeAllFuncPtr(seg::ClickableObject & p)
{
return;
}
int seg::NoneTypeOfFuncPtr(seg::ClickableObject & p)
{
return 0;
}
int seg::NoneTypeOfFuncPtrEx(seg::ClickableObject & p, MOUSEMSG & m, double perx, double pery)
{
return 0;
}
void seg::ClickableObject::init(
int x0, int y0, int w0, int h0, int lw, int ct,
COLORREF fg0, COLORREF bg0, bool ar, int Id, bool cai, bool uoce, COLORREF tc, int ls
)
{
this->x = x0;
this->y = y0;
this->width = w0;
this->height = h0;
this->LineWidth = lw;
this->ComType = ct;
this->fg = fg0;
this->bg = bg0;
this->AutoRender = ar;
this->id = Id;
this->ClickAsInput = cai;
this->s.set(L"\0\0");
this->UsingOnclickEx = uoce;
this->TextColor = tc;
this->LineStyle = ls;
this->OnClick = seg::NoneTypeOfFuncPtr;
this->render = seg::NoneTypeAllFuncPtr;
this->OnClickEx = seg::NoneTypeOfFuncPtrEx;
}
seg::Interface::Interface(int num, COLORREF BG, bool AutoExitBtn)
{
this->num = num;
this->ChooseID = -114514;
this->bg = BG;
this->CreateAutoQuitButton = AutoExitBtn;
this->cbs = new seg::ClickableObject[num]; // 记得释放
}
bool seg::Interface::CheckChoose(MOUSEMSG Mmsg)
{
int xpos = Mmsg.x;
int ypos = Mmsg.y;
int x, y;
int xr, yr;
int ret = 0;
if (this->CreateAutoQuitButton == true)
{
if (0 <= xpos && xpos <= 50 && 0 <= ypos && ypos <= 50)
{
return true;
}
}
for (int i = 0; i < this->num; i++)
{
x = this->cbs[i].x;
y = this->cbs[i].y;
xr= x + this->cbs[i].width;
yr= y + this->cbs[i].height;
if (x <= xpos && xpos <= xr && y <= ypos && ypos <= yr)
{
// click
if (this->cbs[i].UsingOnclickEx == false)
{
ret = this->cbs[i]._on_click();
}
else
{
double px = (double)(xpos - x) / this->cbs[i].width;
double py = (double)(ypos - y) / this->cbs[i].height;
ret = this->cbs[i]._on_click(Mmsg, px, py );
}
if (this->cbs[i].ClickAsInput == true)
{
this->ChooseID = i;
}
if (ret == 0)
{
return false;
}
else {
return true; //
}
}
}
// None
this->ChooseID = -1;
return false;
}
void seg::Interface::mainloop()
{
//LINESTYLE OldLS;
//getlinestyle(&OldLS);
MOUSEMSG msgs;
FlushMouseMsgBuffer();
ExMessage exm;
bool lbtdown = false;
TCHAR LastChar = TCHAR('\0');
while (true)
{
seg::render::fill(this->bg);
for (int i = 0; i < this->num; i++)
{
this->cbs[i]._render();
}
if (this->CreateAutoQuitButton == true)
{
setlinecolor(RGB(20, 200, 120));
setfillcolor(RGB(20, 80, 252));
fillrectangle(0, 0, 50, 50);
setlinestyle(PS_SOLID, 4);
line(5, 5, 45, 45);
line(5, 45, 45, 5);
}
getmessage(&exm, EX_MOUSE | EX_KEY | EX_CHAR | EX_WINDOW); //https://tieba.baidu.com/p/7457305944
switch (exm.message)
{
case WM_LBUTTONDOWN:
lbtdown = true;
msgs.x = exm.x;
msgs.y = exm.y;
break;
case WM_CHAR:
LastChar = exm.ch;
break;
default:
break;
}
if (lbtdown == true)
{
if (this->CheckChoose(msgs))
{
seg::render::flip();
//setlinestyle(&OldLS);
return;
}
}
if (this->ChooseID >= 0 && this->cbs[this->ChooseID].ClickAsInput == true)
{
if (LastChar != ((TCHAR)('\0')))
{
if (LastChar != ((TCHAR)('\b')))
this->cbs[this->ChooseID].s.join((wchar_t)LastChar);
else if (LastChar == ((TCHAR)('\b')))
this->cbs[this->ChooseID].s.back();
}
}
lbtdown = false;
LastChar = ((TCHAR)('\0'));
seg::render::flip();
Sleep(8);
}
}
// include A-Z, 0-9, num 0-9, F1-F12, .(0x6E)
int seg::Key::GetRecentRealKey()
{
if (GetForegroundWindow() != seg::MainHandle)
{
return 0;
}
for (int i = 0x30; i <= 0x39; i++)
{
if (seg::Key::GetGlobalKeyPressed(i))
{
return i;
}
}
for (int i = 0x60; i <= 0x69; i++)
{
if (seg::Key::GetGlobalKeyPressed(i))
{
return i - 0x60;
}
}
if (seg::Key::GetGlobalKeyPressed(VK_SPACE)) return int(L' ');
if (seg::Key::GetGlobalKeyPressed(VK_BACK)) return 0x08;
if (seg::Key::GetGlobalKeyPressed(VK_RETURN)) return 0x0D;
for (int i = 0x41; i < 0x5B; i++)
{
if (seg::Key::GetGlobalKeyPressed(i))
{
if (seg::Key::GetGlobalKeyPressed(VK_SHIFT))
{
return i;
}
else {
return i + 'a' - 'A';
}
}
}
return 0;
}
inline bool seg::Key::GetGlobalKeyPressed(int VK)
{
return GetAsyncKeyState(VK)& 0x8000 ? 1:0;
}
int seg::Key::GetVirtualKey(int VK)
{
if (GetForegroundWindow() != seg::MainHandle)
{
return 0;
}
return GetAsyncKeyState(VK)& 0x8000 ? 1:0;
}
seg::String::String(){
;
}
int seg::String::len()
{
return this->_len;
}
int seg::String::join(const wchar_t * const buf)
{
const wchar_t * newbuf = buf;
int o = 0;
while ((*newbuf != (wchar_t)('\0')) && (this->_len < Seg_StringLengthMax))
{
this->s[this->_len] = *newbuf;
newbuf++;
this->_len += 1;
o++;
}
return o;
}
bool seg::String::join(const wchar_t c)
{
if (this->_len < Seg_StringLengthMax)
{
if (iswprint(c))
{
this->s[this->_len] = c;
this->_len += 1;
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
void seg::String::back()
{
this->s[this->_len] = (wchar_t)'\0';
if (this->_len > 0)
{
this->_len -= 1;
this->s[this->_len] = (wchar_t)'\0';
}
}
void seg::String::set(const wchar_t *const buf)
{
for (int i = 0; i < Seg_StringLengthMax; i++)
{
this->s[i] = ((wchar_t)('\0'));
}
this->_len = 0;
this->join(buf);
}
const wchar_t * seg::String::get()
{
return this->s;
}
// Pillow lib
#ifndef GetArrayIndex
#define GetArrayIndex(x, y, X, Y) ((y * X + x))
#define GetIndex2Y(x, y, X, Y) ((GetArrayIndex(x, (Y-y - 1), X, Y)))
#endif
void seg::Pillow::Flip(int direct, IMAGE * src, IMAGE * out)
{
const int w = src->getwidth(); // 获取源图像宽度
const int h = src->getheight(); // 获取源图像高度
// 如果目标图像与源图像尺寸不同,调整目标图像
if (out->getwidth() != w || out->getheight() != h)
out->Resize(w, h);
// 获取源图像与目标图像的显示缓冲区地址
DWORD* s = GetImageBuffer(src);
DWORD* d = GetImageBuffer(out);
if (direct & seg::Pillow::Directs::X)
{
// 左右翻转每一行的像素
for (int x = 0; x < w; x++)
for (int y = 0; y < h; y++)
d[y * w + w - x - 1] = s[y * w + x]; // ??? -1 or not?
}
if (direct & seg::Pillow::Directs::Y)
{
// 上下翻转
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
d[GetIndex2Y(x, y, w, h)] = s[GetArrayIndex(x, y, w, h)];
}
}
}
return;
}
void seg::Pillow::transparentimage(IMAGE *dstimg, int x, int y, IMAGE *srcimg, UINT transparentcolor)
{
// 变量初始化
DWORD *dst = GetImageBuffer(dstimg);
DWORD *src = GetImageBuffer(srcimg);
int src_width = srcimg->getwidth();
int src_height = srcimg->getheight();
int dst_width = (dstimg == NULL ? getwidth() : dstimg->getwidth());
int dst_height = (dstimg == NULL ? getheight() : dstimg->getheight());
// 计算贴图的实际长宽
int iwidth = (x + src_width > dst_width) ? dst_width - x : src_width;
int iheight = (y + src_height > dst_height) ? dst_height - y : src_height;
// 修正贴图起始位置
dst += dst_width * y + x;
// 修正透明色,显示缓冲区中的数据结构为 0xaarrggbb
transparentcolor = 0xff000000 | BGR(transparentcolor);
// 实现透明贴图
for (int iy = 0; iy < iheight; iy++)
{
for (int ix = 0; ix < iwidth; ix++)
{
if (src[ix] != transparentcolor)
dst[ix] = src[ix];
}
dst += dst_width;
src += src_width;
}
}
seg::Pillow::PIC::PIC(IMAGE * self , int ColorKey )
{
if (self != NULL)
{
this->img = *(self);
}
this->colorkey = (color_t)ColorKey;
}
inline void seg::Pillow::PIC::ToBuffer()
{
this->IMGBUF = GetImageBuffer(&(this->img));
}
bool seg::Pillow::PIC::load(wchar_t *filename, bool _raise )
{
// file exists?
if (PathFileExistsW((LPCWSTR)filename))
{
loadimage(&this->img, (LPCTSTR)filename, 0, 0, true);
return true;
}
else{
if (_raise == true)
{
wchar_t filename1[300];
wsprintfW(filename1, L"File %s doesn't exists.", filename);
seg::Traceback::raise(L"FileNotExistError", L"seg.pillow.pic.load", filename1);
}
return false;
}
}
void seg::Pillow::PIC::blit(int x, int y)
{
if (this->colorkey < 0 || this->colorkey > 255)
{
putimage(x, y, &(this->img));
}
else
{
COLORREF color = RGB(this->AlphaColor.r, this->AlphaColor.g, this->AlphaColor.b);
DWORD *imgp = GetImageBuffer(&(this->img));
DWORD *bgimgp = GetImageBuffer();
int w, bw, h, i, j;
w = this->img.getwidth();
bw = getwidth();
const int MaxXY = bw * getheight();
h = this->img.getheight();
color += 0xff000000;
for (i = 0; i < h; ++i)
{
for (j = 0; j < w; ++j)
{
if (imgp[i*w + j] != color && ((i + y)*bw + j + x) < MaxXY)
bgimgp[(i + y)*bw + j + x] = imgp[i*w + j];
}
}
//FlushBatchDraw();
}
}
void seg::Pillow::PIC::blit(int x, int y, color_t alpha)
{
DWORD *imgp = GetImageBuffer(&(this->img));
DWORD *bgimgp = GetImageBuffer();
COLORREF color = RGB(this->AlphaColor.r, this->AlphaColor.g, this->AlphaColor.b);
const int &dstx = x;
const int & dsty = y;
//const int xymax = seg::Window.GetWidth() * seg::Window.GetHeight();
int w, bw, h, i, j;
w = this->img.getwidth();
bw = getwidth();
const int MaxXY = bw * getheight();
h = this->img.getheight();
color += 0xff000000;
if (alpha < 0)alpha = 0;
else if(alpha>255)alpha=255;
for (i = 0; i < h; ++i)
for (j = 0; j < w; ++j)
if (imgp[i*w + j] != color && ((i + y)*bw + j + x) < MaxXY)
bgimgp[(i + y)*bw + j + x] =
RGB
(
((int)((double)alpha / 255.0* GetRValue(imgp[i*w + j]) + (1 - alpha / 255.0)* GetRValue(bgimgp[(i + dsty)*bw + j + dstx]))),
((int)((double)alpha / 255.0* GetGValue(imgp[i*w + j]) + (1 - alpha / 255.0)* GetGValue(bgimgp[(i + dsty)*bw + j + dstx]))),
((int)((double)alpha / 255.0* GetBValue(imgp[i*w + j]) + (1 - alpha / 255.0)* GetBValue(bgimgp[(i + dsty)*bw + j + dstx])))
);
//FlushBatchDraw();
}
// https://pan.baidu.com/s/1sjoIWtZ
// https://tieba.baidu.com/p/3587996765
/**/
void seg::Pillow::PIC::scale(IMAGE* P,IMAGE* Q,double ZoomRate,bool HighQuality,double ZoomRate2)
{
//����д�ڶ����Ų�����Ĭ�Ϻ͵�һ���
if(ZoomRate2==0)
ZoomRate2=ZoomRate;
//�������ű����趨Ŀ��ͼ���С
const int QW = Q->getwidth();
const int QH = Q->getheight();
P->Resize((int)(QW*ZoomRate),(int)(QH*ZoomRate2));
const int PW = P->getwidth();
const int PH = P->getheight();
//�ֱ��ԭͼ���Ŀ��ͼ���ȡָ��
DWORD* M=GetImageBuffer(P);
DWORD* N=GetImageBuffer(Q);
//ѡ���������ʹ��˫���Բ�ֵ�㷨
if(HighQuality)
{
for(int i=0;i<PH - 1;i++)
{
for(int j=0;j<PW - 1;j++)
{
//���Ŀ��ͼ���Ӧ���ص���ԭͼ�ĸ������겢ȡ��
int X_=(int)((j+0.5)/ZoomRate-0.5);
int Y_=(int)((i+0.5)/ZoomRate2-0.5);
//����ȡ��������A1(X,Y), A2(X+1,Y), A3(X,Y+1), A4(X+1,Y+1)�����������ٽ�4�������ɫƽ��ֵ��
M[j+i*PW] =
RGB
(
(GetRValue(N[X_+Y_*QW])+GetRValue(N[(X_+1)+Y_*QW])+GetRValue(N[X_+(Y_+1)*QW])+GetRValue(N[(X_+1)+(Y_+1)*QW]))/4,
(GetGValue(N[X_+Y_*QW])+GetGValue(N[(X_+1)+Y_*QW])+GetGValue(N[X_+(Y_+1)*QW])+GetGValue(N[(X_+1)+(Y_+1)*QW]))/4,
(GetBValue(N[X_+Y_*QW])+GetBValue(N[(X_+1)+Y_*QW])+GetBValue(N[X_+(Y_+1)*QW])+GetBValue(N[(X_+1)+(Y_+1)*QW]))/4
);
}
}
}
else
//ѡ��������򰴳��淽������
{
for(int i=0;i<PH - 1 ;i++)
for(int j=0;j<PW -1;j++)
//����Ŀ��ͼ�����ص�λ��������ԭͼ�����ص㸳ֵ
M[j+i*PW]=N[(int)(j/ZoomRate)+(int)(i/ZoomRate2)*QW];
}
}
void seg::Pillow::PIC::scale(double ZoomRate,bool HighQuality,double ZoomRate2)
{
if(ZoomRate2==0)
ZoomRate2=ZoomRate;
IMAGE tmp;
IMAGE *P = &tmp;
IMAGE *Q = &(this->img);
const int QW = Q->getwidth();
const int QH = Q->getheight();
P->Resize((int)(QW*ZoomRate),(int)(QH*ZoomRate2));
const int PW = P->getwidth();
const int PH = P->getheight();
DWORD* M=GetImageBuffer(P);
DWORD* N=GetImageBuffer(Q);
if(HighQuality)
{
for(int i = 0 ; i < PH - 1 ; i ++)
{
for(int j = 0 ; j < PW - 1 ; j++)
{
int X_=(int)((j+0.5)/ZoomRate-0.5);
int Y_=(int)((i+0.5)/ZoomRate2-0.5);
M[j+i*PW] =
RGB
(
(GetRValue(N[X_+Y_*QW])+GetRValue(N[(X_+1)+Y_*QW])+GetRValue(N[X_+(Y_+1)*QW])+GetRValue(N[(X_+1)+(Y_+1)*QW]))/4,
(GetGValue(N[X_+Y_*QW])+GetGValue(N[(X_+1)+Y_*QW])+GetGValue(N[X_+(Y_+1)*QW])+GetGValue(N[(X_+1)+(Y_+1)*QW]))/4,
(GetBValue(N[X_+Y_*QW])+GetBValue(N[(X_+1)+Y_*QW])+GetBValue(N[X_+(Y_+1)*QW])+GetBValue(N[(X_+1)+(Y_+1)*QW]))/4
);
}
}
}
else
{
for(int i=0;i<PH-1;i++)
for(int j=0;j<PW-1;j++)
M[j+i*PW]=N[(int)(j/ZoomRate)+(int)(i/ZoomRate2)*QW];
}
this->img = tmp;
}
inline void seg::Pillow::PIC::rotate(IMAGE *dstimg,IMAGE *srcimg,double radian,COLORREF bkcolor ,bool autosize ,bool highquality )
{
rotateimage(dstimg, srcimg, radian * (3.141592653589 / 180), bkcolor, autosize, highquality);
}
inline void seg::Pillow::PIC::rotate(double radius,COLORREF bkcolor ,bool autosize ,bool highquality )
{
IMAGE tmp;
rotateimage(&tmp, &(this->img), radius * (3.141592653589 / 180), bkcolor, autosize, highquality);
this->img = tmp;
}
void seg::Traceback::raise(wchar_t *en, wchar_t *fn, wchar_t *ed)
{
throw seg::Traceback::Exception(en, fn, ed);
}
seg::Traceback::Exception::Exception(wchar_t * ErrName, wchar_t *FuncName, wchar_t* ErrDesc)
{
this->errName.set(ErrName);
this->funcName.set(FuncName);
this->errDesc.set(ErrDesc);
this->format_exc.set(L"Program crashed! An exception occured: ");
this->format_exc.join(ErrName);
this->format_exc.join(L"\n at ");
this->format_exc.join(FuncName);
this->format_exc.join(L"\nDetails: ");
this->format_exc.join(ErrDesc);
}
int seg::String::count_line(wchar_t sep )
{
int ret = 0;
for (size_t i = 0; i < this->_len; i++)
{
if (this->s[i] == sep)
{
ret++;
}
}
ret ++;
return ret;
}
bool seg::String::get_line(wchar_t * out, int line , wchar_t sep )
{
bool ret = false;
int linenow = 0;
for (size_t i = 0; i < this->_len; i++)
{
if (linenow == line)
{
// readstr
int k = 0;
for (size_t j = i; j < this->_len; j++)
{
if (this->s[j] == sep)
{
break;
}
else
{
out[k] = this->s[j];
k++;
}
}
out[k] = (wchar_t)('\0');
return true;
}
if (this->s[i] == sep)
{
linenow++;
}
}
return false;
}
bool seg::String::isint()
{
for (size_t i = 0; i < this->_len; i++)
{
if (this->s[i] >= (wchar_t)('0') && this->s[i] <= (wchar_t)('9'))
{
pass;//ok
}
else {
return false;
}
}
return true;
}
bool seg::String::isfloat()
{
int i = 0;
for ( i = 0; i < this->_len; i++)
{
if (this->s[i] >= (wchar_t)('0') && this->s[i] <= (wchar_t)('9'))
{
pass;//ok
}
else if (this->s[i] == (wchar_t)('.'))
{
break;
}
else {
return false;
}
}
i++;
if (i >= this->_len)
{
return false;
}
for (; i < this->_len; i++)
{
if (this->s[i] >= (wchar_t)('0') && this->s[i] <= (wchar_t)('9'))
{
pass;//ok
} else {
return false;
}
}
return true;
}
int seg::String::find(wchar_t c, int index)
{
int ind = index;
for ( ; ind < this->_len ; ind++ ) {
if (this->s[ind] == c)
{
return ind;
}
else{
pass;
}
}
return -114514;
}
seg::String seg::String::lstrip(wchar_t *c)
{
seg::String s;
seg::String From;
s.set(L"");
From.set(c);
const wchar_t *ptr = this->get();
int i = 0;
while (From.find(*ptr, 0) >= 0)
{
ptr++;
i++;
}
if (i >= this->_len)
{
return s;
}
else
{
s.set(ptr);
return s;
}
}
// new
seg::String seg::String::rstrip(wchar_t * c)
{
seg::String str;
str.set(this->get());
str.rstrip(c, false);
return str;
}
// inplace
void seg::String:: rstrip(wchar_t * c, bool get_a_new_string )
{
if (this->_len < 1)
{
return;
}
else
{
seg::String From;
From.set(c);
while (this->_len > 0)
{
if (From.find(this->s[this->_len - 1], 0) >= 0)
{
this->back();
}
else
{
break;
}
}
return;
}
}
seg::String seg::String::strip(wchar_t *c)
{
seg::String str = this->lstrip(c);
str.rstrip(c, false);
return str;
}
// 2022 create. 20230926 last change.
// 祖传代码请勿乱动 ^v^
bool seg::String::startswith(wchar_t *s)
{
const wchar_t *ss = this->get();
const wchar_t *buf = s;
CmPnExT:
if (*buf == wchar_t('\0')){
return true;
} if (*ss == wchar_t('\0')){
return false;
}
if (*buf != *ss) {
return false;
}
ss ++;
buf++;
goto CmPnExT;
return 0;
}
bool seg::String::endswith(wchar_t *s)
{
int l = 0;
while (s[l] != (wchar_t)'\0')
{
l++;
}
if (l > this->len())
{
return false;
}
l--;
int i = this->_len - 1;
while (l >= 0)
{
if (i < 0)
{
return false;
}
if (s[l] != this->s[i])
{
return false;
}
l--;
i--;
}
return true;
}
seg::String seg::String::reverse()
{
seg::String str;
str.set(L"\0");
int i = this->_len - 1;
while (i >= 0)
{
str.join(this->s[i]);
i--;
}
return str;
}
bool seg::String::equals( const wchar_t *s)
{
const wchar_t *ss = this->get();
const wchar_t *buf = s;
CmPnExT2:
if (*buf == wchar_t('\0')){
if (*ss == *buf)
return true;
else
return false;
} if (*ss == wchar_t('\0')){
if (*ss == *buf)
return true;
else
return false;
}
if (*buf != *ss) {
return false;
}
ss ++;
buf++;
goto CmPnExT2;
return 0;
}
bool seg::String:: operator ==(const wchar_t *s)
{
int i = 0;
while (i < this->_len)
{
if (this->s[i] != s[i])
{
return false;
}
if (s[i] == wchar_t('\0'))
{
break;
}
i++;
}
if (this->s[i] == s[i])
return true;
else
return false;
}
bool seg::String::operator== (seg::String &ss)
{
if (ss.len() != this->_len)
{
return false;
}
return this->startswith(const_cast<wchar_t *>(ss.get()));
}
seg::String seg::String::get(int start, int stop , int step )
{
seg::String str;
str.set(L"");
if (stop < 0)
{
stop = stop + this->_len + 1;
}
if (start >= stop || start < 0 || stop >= this->_len)
{
// ERROR
return str;
}
for ( ; start < stop; start += step)
{
str.join(this->s[start]);
}
return str;
}
wchar_t seg::String::index(int _index)
{
if (_index >= 0 && _index < this->_len)
return this->s[_index];
else
return (wchar_t)'\0';
}
wchar_t seg::String::operator[](int _index)
{
if (_index >= 0 && _index < this->_len)
return this->s[_index];
else
return (wchar_t)'\0';
}
wchar_t seg::String::operator[](int _index) const
{
if (_index >= 0 && _index < this->_len)
return this->s[_index];
else
return (wchar_t)'\0';
}
/*x, y: pos
yadd: 行距*/
void seg::render::multiline(int x, int y, seg::String str, int yadd, seg::render::Place CharacterPos)
{
const int lineA = str.count_line();
wchar_t buf[Seg_StringLengthMax];
for (int i = 0; i <= lineA; i++)
{
if (!str.get_line(buf, i))
{
break;
}
else
{
seg::render::text(x, y + i * yadd, (TCHAR*)buf, CharacterPos);
}
}
}
int seg::Traceback::errorscreen(seg::Traceback::Exception & se)
{
seg::Interface errscr(2, RGB(236, 218, 192), true);
errscr.cbs[0].init(
120, 120, getwidth()*0.6, getheight()*0.48, 3, 0, RGB(255, 60, 236), RGB(10, 30, 69), true, 0, false, false
);
errscr.cbs[1].init
(
getwidth() * 0.6, getwidth() * 0.69, getwidth()*0.6, getheight()*0.23, 3, 0,
RGB(255, 60, 236), RGB(10, 30, 69), true, 0, false, false
);
errscr.cbs[0].s.set(se.format_exc.get());
errscr.cbs[1].s.set(L"From: UserDefine");
errscr.mainloop();
return 0;
}
int seg::Traceback::errorscreen(const std::exception &e)
{
seg::Interface errscr(2, RGB(20, 30, 60), true);
errscr.cbs[0].init(
120, 120, getwidth()*0.6, getheight()*0.48, 3, 0, RGB(255, 60, 236), RGB(10, 30, 69), true, 0, false, false
);
errscr.cbs[1].init
(
getwidth() * 0.6, getwidth() * 0.69, getwidth()*0.6, getheight()*0.23, 3, 0,
RGB(255, 60, 236), RGB(10, 30, 69), true, 0, false, false
);
errscr.cbs[0].s.set(L"Unknown exception");
errscr.cbs[1].s.set(L"From: System");
errscr.mainloop();
return 0;
}
int seg::Math::randint(int _min, int _max)
{
std::uniform_int_distribution<int> ri(_min, _max);
return ri(seg::Math::eng);
}
double seg::Math::random(double _min, double _max)
{
std::uniform_real_distribution<double> ru(_min, _max);
return ru(seg::Math::eng);
}
seg::Interface::~Interface()
{
delete[] this->cbs;
}
/*
#################
基本类型(int + double + wchar_t* + dict)
可用于读取配置文件, 不建议做复杂工作
*****************
*/
int seg::String::count(wchar_t c)
{
int ret = 0;
for (int j = 0; j < this->_len; j++)
{
if (this->s[j] == c)
{
ret++;
}
}
return ret;
}
void seg::String::from_cchar(const char *cs)
{
this->set(L"");
wchar_t str[Seg_StringLengthMax];
swprintf(str, L"%hs", cs);
this->join((const wchar_t *)str);
}
void seg::String::to_cchar(char *out)
{
sprintf(out, "%ws", this->get());
}
seg::SegObject_BasicObject::SegObject_BasicObject()
{
this->_t = seg::SegType::sNone;
}
seg::SegType seg::SegObject_BasicObject::ctype()
{
return this->_t;
}
using String = seg::String;
#endif // _SEG_LIBC
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化