人气 108

[游戏程序] 請問C++高手 main.cpp 如何編譯成DLL [复制链接]

九艺网 2017-3-10 17:00:08

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

x
請問C++高手 main.cpp 如何編譯成DLL
這是fallout_font.dll的C++跟 noword 要來的,搞了老半天沒法編譯成DLL

目前繁體化問題主要在提示框文字變亂碼如下圖
對話如果太長會有第二段以上時如果有繁體字時有時會變亂碼
有強者能幫忙修改一下嗎??

main.cpp

#ifdef     __cplusplus
#define EXPORT extern "C" __declspec (dllexport)
#else
#define EXPORT __declspec (dllexport)
#endif
#include

#define BMP_WIDTH 640

struct font_struct
{
  HFONT hFont;
  char name[255];
  int size;
  int weight;
} text_font, button_font;

DWORD *g_pBits;
HDC g_Hdc;
HBITMAP g_hBmp;
LONG g_FontHeight;
LONG g_Adjust;
UINT g_CodePage;

void
GetIni ()
{
  char ini_name[MAX_PATH];
  GetModuleFileName (NULL, ini_name, MAX_PATH);
  *(strrchr (ini_name, '\\') + 1) = 0;
  strcat (ini_name, "fallout_font.ini");

  GetPrivateProfileString ("TEXT", "Name", "Tahoma", text_font.name,
                           80, ini_name);
  text_font.size = GetPrivateProfileInt ("TEXT", "Size", 11, ini_name);
  text_font.weight = GetPrivateProfileInt ("TEXT", "Weight", 400, ini_name);

  GetPrivateProfileString ("BUTTON", "Name", "黑體", button_font.name,
                           80, ini_name);
  button_font.size = GetPrivateProfileInt ("BUTTON", "Size", 20, ini_name);
  button_font.weight =
    GetPrivateProfileInt ("BUTTON", "Weight", 100, ini_name);

  g_Adjust = GetPrivateProfileInt ("MISC", "Adjust", 3, ini_name);
  
  g_CodePage = GetPrivateProfileInt ("MISC", "CodePage", 0, ini_name);
  if (g_CodePage==0)
  {g_CodePage = GetACP();}
}

LONG
TextWidth (const char *string)
{
  SIZE size;
  GetTextExtentPoint32 (g_Hdc, string, lstrlen (string), &size);
  return size.cx;
}

int
RepFrontDot (char *string)
{
  int len = lstrlen (string);
  if ((unsigned char) *string == 0x95)
    {
      for (int i = len; i >= 1; i--)
        {
          string[i + 1] = string;
        }
      //●
      string[0] = 0xa1;
      string[1] = 0xf1;
      len += 2;
    }
  return len;
}

EXPORT void
InitFont ()
{
  //create HDC
  g_Hdc = CreateCompatibleDC (NULL);

  //create font
  text_font.hFont =
    CreateFont (-text_font.size, 0, 0, 0, text_font.weight, FALSE,
                FALSE, FALSE,
                DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
                VARIABLE_PITCH | FF_DONTCARE, text_font.name);
  button_font.hFont =
    CreateFont (-button_font.size, 0, 0, 0, button_font.weight, FALSE, FALSE,
                FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
                ANTIALIASED_QUALITY, VARIABLE_PITCH | FF_DONTCARE, button_font.name);

  //create bitmap
  BITMAPINFO bmi;
  ZeroMemory (&bmi.bmiHeader, sizeof (BITMAPINFOHEADER));
  bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
  bmi.bmiHeader.biWidth = BMP_WIDTH;
  bmi.bmiHeader.biHeight = -max (button_font.size, text_font.size) * 1.5;
  bmi.bmiHeader.biPlanes = 1;
  bmi.bmiHeader.biBitCount = 32;
  bmi.bmiHeader.biCompression = BI_RGB;
  g_hBmp =
    CreateDIBSection (g_Hdc, &bmi, DIB_RGB_COLORS, (void **) &g_pBits, NULL,
                      0);

  //set bmp, font, color
  SelectObject (g_Hdc, g_hBmp);
  SelectObject (g_Hdc, button_font.hFont);
  SetTextColor (g_Hdc, RGB (255, 255, 255));
  SetBkColor (g_Hdc, 0);
}

EXPORT void
ClearFont ()
{
  DeleteObject (g_hBmp);
  DeleteObject (text_font.hFont);
  DeleteObject (button_font.hFont);
  DeleteDC (g_Hdc);
}


EXPORT int
TextOutput (char *string, int string_width, int screen_width, BYTE * dest,
            BYTE color)
{/*
  for (int i=0;i16)
              {
                *dest = color-1;
                margin=false;
              }
              else if(g_pBits[i * BMP_WIDTH + j-2] != 0 && color>16)
              {
                    *dest=color-1;
                  margin=true;
              }
              else*/
              {*dest=color;}
              is_emptyline = false;
            }
          dest++;
        }
      dest -= size.cx;
      if (!is_emptyline)
        dest += screen_width;
    }
  return len;
}

EXPORT LONG
GetTextWidth (char *string)
{
  RepFrontDot (string);
  return TextWidth (string);
}

EXPORT LONG
GetTextHeight ()
{
  DWORD ret_address;
  __asm
  {
  mov eax, dword ptr ss:[esp + 0x10]
  mov ret_address, eax
  };

  switch (ret_address)
    {
    case 0x42bc17:
    case 0x42c0f2:
        return g_FontHeight + g_Adjust - 1;
    case 0x496cd1:
    case 0x44051a:             //對話列表
    case 0x46817a:             //交易價格
    case 0x4874f1:               //pipboy
    case 0x486ca7:               //pipboy初始化
    case 0x48924a:               //pipboy按鈕
    case 0x487534:               //pipboy橫線
    case 0x48754c:               //pipboy橫線
        return g_FontHeight + g_Adjust;
    default:
      return g_FontHeight;
    }
}

EXPORT void
SelectFont (int font)
{
  if (font < 0x66)
    SelectObject (g_Hdc, text_font.hFont);
  else
    SelectObject (g_Hdc, button_font.hFont);

  SIZE size;
  GetTextExtentPoint32 (g_Hdc, "啊g", 3, &size);
  g_FontHeight = size.cy;
  if (font == 0x65)
    g_FontHeight -= g_Adjust;
}

EXPORT int
SplitText (const char *string, WORD * split_pos, WORD * split_count,
           LONG width)
{
  int len = lstrlen (string);

  split_pos[0] = 0;
  width-=text_font.size;

  if (TextWidth (string)  0x80)
            {
              linewidth += text_font.size;
              j += 2;
            }
          else
            {
              SIZE size;
              GetTextExtentPoint32W (g_Hdc, wstring + i, 1, &size);
              linewidth += size.cx;
              j++;
            }
          if (linewidth > width)
            {
              //roll back
              do
                {
                  j--;
                  if (wstring > 0x80)
                    {
                      j--;
                    }
                  i--;
                  if (i == 0)
                    return 1;
                }
              while (wstring  0)
    {
      string_width -= text_font.size * 2;
    }
  if (TextWidth (string)  0x80)
            {
              linewidth += text_font.size;
              tail += 2;
            }
          else
            {
              SIZE size;
              GetTextExtentPoint32W (g_Hdc, wstring + i, 1, &size);
              linewidth += size.cx;
              tail++;
            }
          if (linewidth > string_width)
            {
              //roll back
              do
                {
                  tail--;
                  if (wstring > 0x80)
                    {
                      tail--;
                    }
                  i--;
                  if (i == 0)
                    break;
                }
              while (wstring right - rect->left;
  if (string_width - text_font.size * 2 > 0)
    {
      string_width -= text_font.size * 2;
    }
  dest += screen_width * rect->top;
  if (string_offset != NULL)
    string += *string_offset;

  if (TextWidth (string) top += GetTextHeight () + g_Adjust - 1;
    }
  else
    {
      int len = strlen (string);
      WCHAR *wstring = new WCHAR[len + 1];
      int lenW = MultiByteToWideChar (g_CodePage, 0, string, -1, wstring, len + 1);
      int linewidth = 0;
      int head = 0, tail = 0;
      for (int i = 0; i < lenW; i++)
        {
          if (wstring > 0x80)
            {
              linewidth += text_font.size;
              tail += 2;
            }
          else
            {
              SIZE size;
              GetTextExtentPoint32W (g_Hdc, wstring + i, 1, &size);
              linewidth += size.cx;
              tail++;
            }
          if (linewidth > string_width)
            {
              //roll back
              do
                {
                  tail--;
                  if (wstring > 0x80)
                    {
                      tail--;
                    }
                  i--;
                  if (i == 0)
                    break;
                }
              while (wstring top += GetTextHeight () + g_Adjust - 1;
              if (string_offset != NULL)
                *string_offset += tail - head;
              head = tail;
              linewidth = 0;
              if (rect->top + GetTextHeight () + g_Adjust - 1 > rect->bottom)
                break;
            } //end if (linewidth > string_width)
        } //end for
      if (rect->top + GetTextHeight () + g_Adjust - 1 < rect->bottom)
        {
          TextOutput (string + head, string_width, screen_width, dest, color);
          rect->top += GetTextHeight () + g_Adjust - 1;
          if (string_offset != NULL)
            *string_offset = 0;
        }
      delete[]wstring;
    }
    return rect->top;
}

BOOL APIENTRY
DllMain (HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
  switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
      GetIni ();
      break;
    case DLL_THREAD_ATTACH:
      break;
    case DLL_THREAD_DETACH:
      break;
    case DLL_PROCESS_DETACH:
      break;
    }
  return TRUE;
}

1140b3ea6c68ac40f4ae8dfe47d7ad9d.jpg



22bc549b0731b25e174aae01df89c034.jpg



fd21d28c5ebc61b20f69e0a14d77ba23.jpg



4fa5f17e567a3fb08af8177185dff690.jpg



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

QQ|手机版|小黑屋|九艺游戏动画论坛 ( 津ICP备2022000452号-1 )

GMT+8, 2024-5-19 00:59 , Processed in 0.144103 second(s), 26 queries .

Powered by Discuz! X3.4  © 2001-2017 Discuz Team.