整个过程大约用了1-2个月。先得有c++

98升到c++17.再有杂合在一起的函数分成协程化多函数。

鼓励一下,谢谢。

源码,为一个函数 ,耦合很严重。逻辑判断,数据传输,图形显示,按键操作都在一个函数里面。你说这个函数想读懂得会很多函数的api。

VOID
PAL_EquipItemMenu(
WORD wItem
)
/*++
Purpose:

Show the menu which allow players to equip the specified item.

Parameters:

[IN] wItem - the object ID of the item.

Return value:

None.

--*/
{
PAL_LARGE BYTE bufBackground[320 * 200];
PAL_LARGE BYTE bufImage[2048];
WORD w;
int iCurrentPlayer, i;
BYTE bColor, bSelectedColor;
DWORD dwColorChangeTime;

gpGlobals->wLastUnequippedItem = wItem;

PAL_MKFDecompressChunk(bufBackground, 320 * 200, EQUIPMENU_BACKGROUND_FBPNUM,
gpGlobals->f.fpFBP);

iCurrentPlayer = 0;
bSelectedColor = MENUITEM_COLOR_SELECTED_FIRST;
dwColorChangeTime = SDL_GetTicks() + (600 / MENUITEM_COLOR_SELECTED_TOTALNUM);

while (TRUE)
{
wItem = gpGlobals->wLastUnequippedItem;

//
// Draw the background
//
PAL_FBPBlitToSurface(bufBackground, gpScreen);

//
// Draw the item picture
//
if (PAL_MKFReadChunk(bufImage, 2048,
gpGlobals->g.rgObject[wItem].item.wBitmap, gpGlobals->f.fpBALL) > 0)
{
PAL_RLEBlitToSurface(bufImage, gpScreen, PAL_XY(16, 16));
}

//
// Draw the current equipment of the selected player
//
w = gpGlobals->rgParty[iCurrentPlayer].wPlayerRole;
for (i = 0; i < MAX_PLAYER_EQUIPMENTS; i++)
{
if (gpGlobals->g.PlayerRoles.rgwEquipment[i][w] != 0)
{
PAL_DrawText(PAL_GetWord(gpGlobals->g.PlayerRoles.rgwEquipment[i][w]),
PAL_XY(130, 11 + i * 22), MENUITEM_COLOR, TRUE, FALSE);
}
}

//
// Draw the stats of the currently selected player
//
PAL_DrawNumber(PAL_GetPlayerAttackStrength(w), 4, PAL_XY(260, 14),
kNumColorCyan, kNumAlignRight);
PAL_DrawNumber(PAL_GetPlayerMagicStrength(w), 4, PAL_XY(260, 36),
kNumColorCyan, kNumAlignRight);
PAL_DrawNumber(PAL_GetPlayerDefense(w), 4, PAL_XY(260, 58),
kNumColorCyan, kNumAlignRight);
PAL_DrawNumber(PAL_GetPlayerDexterity(w), 4, PAL_XY(260, 80),
kNumColorCyan, kNumAlignRight);
PAL_DrawNumber(PAL_GetPlayerFleeRate(w), 4, PAL_XY(260, 102),
kNumColorCyan, kNumAlignRight);

//
// Draw a box for player selection
//
PAL_CreateBox(PAL_XY(2, 95), gpGlobals->wMaxPartyMemberIndex, 2, 0, FALSE);

//
// Draw the label of players
//
for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
{
w = gpGlobals->rgParty[i].wPlayerRole;

if (iCurrentPlayer == i)
{
if (gpGlobals->g.rgObject[wItem].item.wFlags & (kItemFlagEquipableByPlayerRole_First << w))
{
bColor = bSelectedColor;
}
else
{
bColor = MENUITEM_COLOR_SELECTED_INACTIVE;
}
}
else
{
if (gpGlobals->g.rgObject[wItem].item.wFlags & (kItemFlagEquipableByPlayerRole_First << w))
{
bColor = MENUITEM_COLOR;
}
else
{
bColor = MENUITEM_COLOR_INACTIVE;
}
}

PAL_DrawText(PAL_GetWord(gpGlobals->g.PlayerRoles.rgwName[w]),
PAL_XY(15, 108 + 18 * i), bColor, TRUE, FALSE);
}

//
// Draw the text label and amount of the item
//
if (wItem != 0)
{
PAL_DrawText(PAL_GetWord(wItem), PAL_XY(5, 70), MENUITEM_COLOR_CONFIRMED, TRUE, FALSE);
PAL_DrawNumber(PAL_GetItemAmount(wItem), 2, PAL_XY(65, 73), kNumColorCyan, kNumAlignRight);
}

//
// Update the screen
//
VIDEO_UpdateScreen(NULL);

//
// Accept input
//
PAL_ClearKeyState();

while (TRUE)
{
PAL_ProcessEvent();

//
// See if we should change the highlight color
//
if (SDL_GetTicks() > dwColorChangeTime)
{
if ((WORD)bSelectedColor + 1 >=
(WORD)MENUITEM_COLOR_SELECTED_FIRST + MENUITEM_COLOR_SELECTED_TOTALNUM)
{
bSelectedColor = MENUITEM_COLOR_SELECTED_FIRST;
}
else
{
bSelectedColor++;
}

dwColorChangeTime = SDL_GetTicks() + (600 / MENUITEM_COLOR_SELECTED_TOTALNUM);

//
// Redraw the selected item if needed.
//
w = gpGlobals->rgParty[iCurrentPlayer].wPlayerRole;

if (gpGlobals->g.rgObject[wItem].item.wFlags & (kItemFlagEquipableByPlayerRole_First << w))
{
PAL_DrawText(PAL_GetWord(gpGlobals->g.PlayerRoles.rgwName[w]),
PAL_XY(15, 108 + 18 * iCurrentPlayer), bSelectedColor, TRUE, TRUE);
}
}

if (g_InputState.dwKeyPress != 0)
{
break;
}

SDL_Delay(1);
}

if (wItem == 0)
{
return;
}

if (g_InputState.dwKeyPress & (kKeyUp | kKeyLeft))
{
iCurrentPlayer--;
if (iCurrentPlayer < 0)
{
iCurrentPlayer = 0;
}
}
else if (g_InputState.dwKeyPress & (kKeyDown | kKeyRight))
{
iCurrentPlayer++;
if (iCurrentPlayer > gpGlobals->wMaxPartyMemberIndex)
{
iCurrentPlayer = gpGlobals->wMaxPartyMemberIndex;
}
}
else if (g_InputState.dwKeyPress & kKeyMenu)
{
return;
}
else if (g_InputState.dwKeyPress & kKeySearch)
{
w = gpGlobals->rgParty[iCurrentPlayer].wPlayerRole;

if (gpGlobals->g.rgObject[wItem].item.wFlags & (kItemFlagEquipableByPlayerRole_First << w))
{
//
// Run the equip script
//
gpGlobals->g.rgObject[wItem].item.wScriptOnEquip =
PAL_RunTriggerScript(gpGlobals->g.rgObject[wItem].item.wScriptOnEquip,
gpGlobals->rgParty[iCurrentPlayer].wPlayerRole);
}
}
}
}

下面则是 协程化,分出来一个主函数,显示协程,按键协程,数据协程。逻辑很简单,很容易懂。

主流程看起来很简单。不像原来那么难了。

下面是显示协程。

是不是逻辑很简单。

再看看按键协程

简单到是个人都能看懂。

下面是数据传输协程

这个看起来复杂,是因为没有归类。 归完类,就是函数调用了。

协程化的好处是什么?

原来是1个人要会很多内容。

现在则变成了,

  1. 主程只需要把内容分配,
  2. 显示程序员处理显示相关,
  3. 按键程序员处理按键相关,
  4. 数据程序员处理数据传输,
  5. 等等

那么,这样好处变成了,专业分工,几个人配合,各干各的,可以很容易把一些老旧的项目给翻新了。哪些部分需要分离,去掉用了协程之后,可以轻易地替换任何一个模块。

这里用的是c++的 coroutine2库。因为不是太了解这个库,所以,参数传递和返回值的传递使用了类的成员变数来替换。

协程是未来,估计在任何编程语言里面,都会大肆使用甚至是滥用。那么协程思想肯定是大势所趋,先学先受益。

源码奉上。

#include "PAL_EquipItemDialog.h"

extern "C" {
#include "SDL_video.h"
extern SDL_Surface *gpScreenReal;
}
#include"EquipItemDialog_Node.h"

#undef CURRENT_CLASS
#define CURRENT_CLASS PAL_EquipItemDialog

PAL_EquipItemDialog::PAL_EquipItemDialog()
{
//step one
dd = std::make_shared<EquipItemDialog_Node>();
}

PAL_EquipItemDialog::~PAL_EquipItemDialog()
{
}
template<typename T>
void show_rect_info(T &r) {
printf("%d %d %d %d
", r.x, r.y, r.w, r.h);
}

void PAL_EquipItemDialog::exec()
{
printf("exec 0000 run here
");
Co_t::pull_type key_co(std::bind(&CURRENT_CLASS::key_event_co, this, std::placeholders::_1)); // main pull sub push
Co_t::pull_type data_co(std::bind(&CURRENT_CLASS::collect_data_co, this, std::placeholders::_1)); // main pull sub push
printf("exec 0000001
");
Co_t::push_type paint_co(std::bind(&CURRENT_CLASS::paint_event_co, this, std::placeholders::_1)); //no arg has rtn
printf("exec 0000002
");

//init
gpGlobals->wLastUnequippedItem =dd->wItem;

PAL_MKFDecompressChunk(dd->bufBackground, 320 * 200, EQUIPMENU_BACKGROUND_FBPNUM,
gpGlobals->f.fpFBP);
dd->iCurrentPlayer = 0;
dd->bSelectedColor = MENUITEM_COLOR_SELECTED_FIRST;
// input arg
auto &wItem =dd->wItem;
auto &wPlayerRole = dd->wPlayerRole;

auto &player_id = dd->player_id;
auto &item_id = dd->equip_item_id;
int name_id;
while (true){
wItem = gpGlobals->wLastUnequippedItem;
paint_co(1);
data_co();

paint_co(2);
// Draw the current equipment of the selected player
wPlayerRole = gpGlobals->rgParty[dd->iCurrentPlayer].wPlayerRole;
dd->cond_drawItem = true;
for (item_id = 0; item_id < MAX_PLAYER_EQUIPMENTS; item_id++) {
data_co();
paint_co(3);
} ;
dd->cond_drawItem = false;
//--5 equip item for player name---------------------------------------
for (int i = 0; i < 5; i++) {
data_co();
paint_co(4);
}
dd->cond_drawItem = true; //printf("exec 00006
");
// Draw the label of players
for (player_id = 0; player_id <= gpGlobals->wMaxPartyMemberIndex; player_id++){
data_co();
paint_co(5);
}
dd->cond_drawItem = false;

// Draw the text label and amount of the item
if (wItem != 0)
{
data_co();
paint_co(6);
}

update(nullptr);

dd->cond_no_key = true; //must exec here !!! mustnt in key_co
while (true){
paint_co(7);
update(&dd->item_rect);
key_co();
data_co(); // process different yellow color to select
if (dd->cond_no_key){
continue;
} else{
data_co();//has key,then process key press
if (!data_co){
return;
}else {
break;

}

}

}
/*
dd->cond_no_key = true; //must exec here !!! mustnt in key_co
while(dd->cond_no_key)
{
paint_co(7);
//show_rect_info(dd->item_rect);
update(&dd->item_rect);
key_co();
//data_co();
//if (!data_co) { return; }

if (!key_co) {
return;
}else {

data_co();
}
}*/
//data_co();
}
}

void PAL_EquipItemDialog::paint_event()
{
while (true){

}
}

void PAL_EquipItemDialog::key_event()
{
}

void PAL_EquipItemDialog::paint_event_co(Co_t::pull_type & co_source)
{
//PAL_FBPBlitToSurface(bufBackground, gpScreen);
PAL_Painter painter(dd->sf);
dd->painter = &painter;
auto node = painter.loadTempData();
node->spriteUI = gpSpriteUI;
auto & item_rect=dd->item_rect ;

auto &cna = dd->curr_number_arg;
auto &cta = dd->curr_text_arg;

auto &curr_draw_number=std::bind(&PAL_Painter::drawNumber,&painter,
std::cref( cna.iNum ),std::cref(cna.nLength),std::cref(cna.pos),std::cref(cna.color),std::cref(cna.align));
auto &curr_draw_text = std::bind(&PAL_Painter::drawText, &painter,
std::cref(cta.lpszText), std::cref(cta.pos), std::cref(cta.bColor), std::cref(cta.fShadow), std::cref(cta.fUpdate));

while (true){
painter.FBPBlitToSurface(dd->bufBackground);// Draw the background
co_source();
if (dd->cond_image) painter.RLEBlitToSurface(dd->bufImage, PAL_XY(16, 16));// Draw the item picture
co_source();
// Draw the current equipment of the selected player
int draw_id = 0;
while (dd->cond_drawItem){
curr_draw_text();
co_source();
}
// Draw the stats of the currently selected player
for (int i = 0; i < 5; i++) {
curr_draw_number();
co_source();
}
// Draw a box for player selection
createBox(PAL_XY(2, 95), gpGlobals->wMaxPartyMemberIndex, 2, 0, FALSE);
// Draw the label of players
while (dd->cond_drawItem) {
curr_draw_text();
co_source();
}
// Draw the text label and amount of the item
if (dd->wItem) {
curr_draw_number();
curr_draw_text();
co_source();
} //printf("run 000000000000
");
while (dd->cond_no_key){
curr_draw_text();
item_rect = { node->x,node->y,node->w,node->h };
co_source();
}

}
}

uint32_t PAL_EquipItemDialog::key_event_co(Co_t::push_type & co_source)
{
DWORD &dwColorChangeTime = dd->dwColorChangeTime;
DWORD dwCurrTime;

auto & bSelectedColor = dd->bSelectedColor;
auto & wItem = dd->wItem;
auto & iCurrentPlayer = dd->iCurrentPlayer;

printf("key co 00000
");
WORD wPlayerRole;
while (true) {
PAL_ClearKeyState();
dwColorChangeTime = SDL_GetTicks() + (600 / MENUITEM_COLOR_SELECTED_TOTALNUM);
//dd->cond_no_key = true; must not here

while (TRUE)
{

// See if we should change the highlight color
if (SDL_GetTicks() > dwColorChangeTime){
co_source(1);
}
PAL_ProcessEvent();
if (g_InputState.dwKeyPress != 0){
printf("key co 003 key=%d
", g_InputState.dwKeyPress);
break;
}
SDL_Delay(1);
}
printf("key co 004
");

dd->cond_no_key = false;

co_source(2);

/*

if (wItem == 0){
return 0;
}

if (g_InputState.dwKeyPress & (kKeyUp | kKeyLeft)) {
iCurrentPlayer--;
if (iCurrentPlayer < 0){
iCurrentPlayer = 0;
}
}else if (g_InputState.dwKeyPress & (kKeyDown | kKeyRight)){
iCurrentPlayer++;
if (iCurrentPlayer > gpGlobals->wMaxPartyMemberIndex){
iCurrentPlayer = gpGlobals->wMaxPartyMemberIndex;
}
}
else if (g_InputState.dwKeyPress & kKeyMenu){
return 0;
}else if (g_InputState.dwKeyPress & kKeySearch) {
wPlayerRole = gpGlobals->rgParty[iCurrentPlayer].wPlayerRole;
if (gpGlobals->g.rgObject[wItem].item.wFlags & (kItemFlagEquipableByPlayerRole_First << wPlayerRole)) {
// Run the equip script
gpGlobals->g.rgObject[wItem].item.wScriptOnEquip =
PAL_RunTriggerScript(gpGlobals->g.rgObject[wItem].item.wScriptOnEquip,
gpGlobals->rgParty[iCurrentPlayer].wPlayerRole);
}
}*/
//---------------------------------

//co_source(1);
printf("key co 005
");
}

return 1;
}

uint32_t PAL_EquipItemDialog::collect_data_co(Co_t::push_type & co_source)
{
auto & bSelectedColor = dd->bSelectedColor;
auto & wItem = dd->wItem;
auto & iCurrentPlayer = dd->iCurrentPlayer;

auto &dwColorChangeTime = dd->dwColorChangeTime;
auto &wPlayerRole = dd->wPlayerRole;

auto &player_id = dd->player_id;
auto &item_id = dd->equip_item_id;
WORD name_id;
printf("data co init
");
//co_source(0);
while (true)
{
co_source(0); // one is first,two more after (key_co dir key)
// total store to temp data
//printf("data co 00000
");
dd->cond_image = PAL_MKFReadChunk(dd->bufImage, 2048, gpGlobals->g.rgObject[wItem].item.wBitmap, gpGlobals->f.fpBALL) > 0;
co_source(1);
//printf("data co 0011,item_id=%d
", item_id);
// step 03
while (dd->cond_drawItem){
name_id = gpGlobals->g.PlayerRoles.rgwEquipment[item_id][wPlayerRole];
if (name_id != 0) {
dd->curr_text_arg = { PAL_GetWord(name_id),PAL_XY(130, 11 + item_id * 22), MENUITEM_COLOR, TRUE, FALSE };
// printf("data co 0014
");
co_source(3);
}

}
//for (int item_id = 0; item_id < MAX_PLAYER_EQUIPMENTS; item_id++) { }
//printf("data co 0012 stop
");
//dd->cond_drawItem = false;
//co_source(3);
//printf("data co 0013
");

// step 04
dd->curr_number_arg = { PAL_GetPlayerAttackStrength(wPlayerRole), 4, PAL_XY(260, 14),kNumColorCyan, kNumAlignRight };
co_source(4);
printf("data co 00111
");
dd->curr_number_arg = { PAL_GetPlayerMagicStrength(wPlayerRole), 4, PAL_XY(260, 36),kNumColorCyan, kNumAlignRight };
co_source(4);
printf("data co 00112
");
dd->curr_number_arg = { PAL_GetPlayerDefense(wPlayerRole), 4, PAL_XY(260, 58),kNumColorCyan, kNumAlignRight };
co_source(4);
printf("data co 00113
");
dd->curr_number_arg = { PAL_GetPlayerDexterity(wPlayerRole), 4, PAL_XY(260, 80),kNumColorCyan, kNumAlignRight };
co_source(4);
printf("data co 00114
");
dd->curr_number_arg = { PAL_GetPlayerFleeRate(wPlayerRole), 4, PAL_XY(260, 102),kNumColorCyan, kNumAlignRight };
co_source(4);
printf("data co 00115
");

// Draw the label of players step 05
while (dd->cond_drawItem){
//printf("data co 001151
");
wPlayerRole = gpGlobals->rgParty[player_id].wPlayerRole;
//printf("data co 001152
");
if (dd->iCurrentPlayer == player_id) {
//printf("data co 001153
");
if (gpGlobals->g.rgObject[wItem].item.wFlags & (kItemFlagEquipableByPlayerRole_First << wPlayerRole)) {
dd->bColor = dd->bSelectedColor;
}
else {
dd->bColor = MENUITEM_COLOR_SELECTED_INACTIVE;
}
}
else {
//printf("data co 001154
");
if (gpGlobals->g.rgObject[wItem].item.wFlags & (kItemFlagEquipableByPlayerRole_First << wPlayerRole)) {
dd->bColor = MENUITEM_COLOR;
}
else {
dd->bColor = MENUITEM_COLOR_INACTIVE;
}
}
//printf("data co 001155
");
dd->curr_text_arg = { PAL_GetWord(gpGlobals->g.PlayerRoles.rgwName[wPlayerRole]),PAL_XY(15, 108 + 18 * player_id), dd->bColor, TRUE, FALSE };
co_source(5);

}
// step 06
if (wItem != 0)
{
dd->curr_number_arg = { PAL_GetItemAmount(wItem), 2, PAL_XY(65, 73), kNumColorCyan, kNumAlignRight };
dd->curr_text_arg = { PAL_GetWord(wItem), PAL_XY(5, 70), MENUITEM_COLOR_CONFIRMED, TRUE, FALSE };
printf("data co 00116
");
co_source(6);
//data_co();
//paint_co(6);
}

// step 07
printf("data co 000
");
while (dd->cond_no_key)
{
//printf("data co 001
");
if ((WORD)bSelectedColor + 1 >= (WORD)MENUITEM_COLOR_SELECTED_FIRST + MENUITEM_COLOR_SELECTED_TOTALNUM) {
bSelectedColor = MENUITEM_COLOR_SELECTED_FIRST;
}
else {
bSelectedColor++;
}
//printf("data co 002
");
dwColorChangeTime = SDL_GetTicks() + (600 / MENUITEM_COLOR_SELECTED_TOTALNUM);
//printf("data co 003
");
// Redraw the selected item if needed.
wPlayerRole = gpGlobals->rgParty[dd->iCurrentPlayer].wPlayerRole;
//printf("data co 004
");
if (gpGlobals->g.rgObject[wItem].item.wFlags & (kItemFlagEquipableByPlayerRole_First << wPlayerRole)) {
dd->curr_text_arg = { PAL_GetWord(gpGlobals->g.PlayerRoles.rgwName[wPlayerRole]),PAL_XY(15, 108 + 18 * iCurrentPlayer), bSelectedColor, TRUE, TRUE };
printf("data co 0021
");
//co_source(1);
//PAL_DrawText(PAL_GetWord(gpGlobals->g.PlayerRoles.rgwName[wPlayerRole]),PAL_XY(15, 108 + 18 * iCurrentPlayer), bSelectedColor, TRUE, TRUE);
}
co_source(1);
}
//step 008
// process key_state and set mean

if (wItem == 0) {
return 0;
}

if (g_InputState.dwKeyPress & (kKeyUp | kKeyLeft)) {
iCurrentPlayer--;
if (iCurrentPlayer < 0) {
iCurrentPlayer = 0;
}
}
else if (g_InputState.dwKeyPress & (kKeyDown | kKeyRight)) {
iCurrentPlayer++;
if (iCurrentPlayer > gpGlobals->wMaxPartyMemberIndex) {
iCurrentPlayer = gpGlobals->wMaxPartyMemberIndex;
}
}
else if (g_InputState.dwKeyPress & kKeyMenu) {
return 0;
}
else if (g_InputState.dwKeyPress & kKeySearch) {
wPlayerRole = gpGlobals->rgParty[iCurrentPlayer].wPlayerRole;
if (gpGlobals->g.rgObject[wItem].item.wFlags & (kItemFlagEquipableByPlayerRole_First << wPlayerRole)) {
// Run the equip script
gpGlobals->g.rgObject[wItem].item.wScriptOnEquip =
PAL_RunTriggerScript(gpGlobals->g.rgObject[wItem].item.wScriptOnEquip,
gpGlobals->rgParty[iCurrentPlayer].wPlayerRole);
}
}
co_source(2);

}
return uint32_t();
}

// common
void PAL_EquipItemDialog::update(SDL_Rect * lpRect, SDL_Surface * src)
{
if (lpRect && lpRect->w==0 ){ return; }
SDL_Rect srcrect, dstrect;
short offset = 240 - 200;
short screenRealHeight = gpScreenReal->h;
short screenRealY = 0;

auto curr = src;
if (!curr) {
curr = dd->sf;
}
offset = gpScreenReal->h - curr->h;

// Lock surface if needed
if (SDL_MUSTLOCK(gpScreenReal)) {
if (SDL_LockSurface(gpScreenReal) < 0)
return;
}

if (!dd->bScaleScreen) {
screenRealHeight -= offset;
screenRealY = offset / 2;
}

//auto col_key = SDL_MapRGB(curr->format, 0, 0, 0);
//SDL_SetColorKey(curr, SDL_SRCCOLORKEY, col_key);

if (lpRect != NULL) {
//printf("lpRect=%d %d %d %d
", lpRect->x, lpRect->y, lpRect->w, lpRect->h);
dstrect.x = (SHORT)((INT)(lpRect->x) * gpScreenReal->w / curr->w);
dstrect.y = (SHORT)((INT)(screenRealY + lpRect->y) * screenRealHeight / curr->h);
dstrect.w = (WORD)((DWORD)(lpRect->w) * gpScreenReal->w / curr->w);
dstrect.h = (WORD)((DWORD)(lpRect->h) * screenRealHeight / curr->h);

SDL_SoftStretch(curr, (SDL_Rect *)lpRect, gpScreenReal, &dstrect);

if (SDL_MUSTLOCK(gpScreenReal)) {
SDL_UnlockSurface(gpScreenReal);
}
//printf("%d %d %d %d
", dstrect.x, dstrect.y, dstrect.w, dstrect.h);
//SDL_Flip(gpScreenReal);
SDL_UpdateRect(gpScreenReal, dstrect.x, dstrect.y, dstrect.w, dstrect.h);
}
else
{
dstrect = { 0,screenRealY,(unsigned short)gpScreenReal->w,(unsigned short)screenRealHeight };
// dstrect.x = 0;
// dstrect.y = screenRealY;
// dstrect.w = gpScreenReal->w;
// dstrect.h = screenRealHeight;

SDL_SoftStretch(curr, NULL, gpScreenReal, &dstrect);

if (SDL_MUSTLOCK(gpScreenReal)) {
SDL_UnlockSurface(gpScreenReal);
}
SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
}
}
LPBOX PAL_EquipItemDialog::createBox(uint32_t pos, int32_t nRows, int32_t nColumns, int32_t iStyle, int32_t fSaveScreen)
{
//printf("%d %d %d %d %d
",pos,nRows,nColumns,iStyle,fSaveScreen);
int i, j, x, m, n;
LPCBITMAPRLE rglpBorderBitmap[3][3];
LPBOX lpBox = NULL;
SDL_Surface *save;
SDL_Rect rect;
//printf("");
//
// Get the bitmaps bg source
//
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
rglpBorderBitmap[i][j] = PAL_SpriteGetFrame(gpSpriteUI, i * 3 + j + iStyle * 9);
}
}

rect = { PAL_X(pos),PAL_Y(pos),0,0 };
// rect.x = PAL_X(pos);
// rect.y = PAL_Y(pos);
// rect.w = 0;
// rect.h = 0;
// Get the total width and total height of the box
//
for (i = 0; i < 3; i++) {
if (i == 1) {
rect.w += PAL_RLEGetWidth(rglpBorderBitmap[0][i]) * nColumns;
rect.h += PAL_RLEGetHeight(rglpBorderBitmap[i][0]) * nRows;
}
else {
rect.w += PAL_RLEGetWidth(rglpBorderBitmap[0][i]);
rect.h += PAL_RLEGetHeight(rglpBorderBitmap[i][0]);
}
}

if (fSaveScreen) {
// Save the used part of the screen
save = SDL_CreateRGBSurface(gpScreen->flags, rect.w, rect.h, 8,
gpScreen->format->Rmask, gpScreen->format->Gmask,
gpScreen->format->Bmask, gpScreen->format->Amask);

if (save == NULL) {
return NULL;
}

lpBox = (LPBOX)calloc(1, sizeof(BOX));
if (lpBox == NULL) {
SDL_FreeSurface(save);
return NULL;
}

//SDL_BlitSurface(gpScreen, &rect, save, NULL);
SDL_BlitSurface(dd->sf, &rect, save, NULL);

lpBox->lpSavedArea = save;
lpBox->pos = pos;
lpBox->wWidth = rect.w;
lpBox->wHeight = rect.h;
}
// Border takes 2 additional rows and columns...
nRows += 2;
nColumns += 2;

// Draw the box
for (i = 0; i < nRows; i++) {
x = rect.x;
m = (i == 0) ? 0 : ((i == nRows - 1) ? 2 : 1);

for (j = 0; j < nColumns; j++) {
n = (j == 0) ? 0 : ((j == nColumns - 1) ? 2 : 1);

dd->painter->RLEBlitToSurface(rglpBorderBitmap[m][n], PAL_XY(x, rect.y));
//PAL_RLEBlitToSurface(rglpBorderBitmap[m][n], gpScreen, PAL_XY(x, rect.y));

//------------------------------------------------------------
x += PAL_RLEGetWidth(rglpBorderBitmap[m][n]);
}
rect.y += PAL_RLEGetHeight(rglpBorderBitmap[m][0]);
}

return lpBox;
}

Dialog_Node * PAL_EquipItemDialog::loadTempData()
{
return static_cast<Dialog_Node*>(&(*dd));;
}

void PAL_EquipItemDialog::update()
{
}

void PAL_EquipItemDialog::updateItem()
{
}

推荐阅读:

相关文章