fontmaker.h
///////////////////////////////////////////////////////////////////////////// // ELEMENT GDI/DX Game Engine [Version 2002/3/1] ///////////////////////////////////////////////////////////////////////////// // Original Author : 邱海峰[Southdy] // OICQ : 359766 // EMAIL: topmud@263.net ///////////////////////////////////////////////////////////////////////////// // DESCRIPTION: // OTHER: 邱海峰创建于2002/3/1 ///////////////////////////////////////////////////////////////////////////// #pragma once ///////////////////////////////////////////////////////////////////////////// // 包含文件 ///////////////////////////////////////////////////////////////////////////// #include "ekwindow.h"// 窗口程序框架接口 /////////////////////////////////////////////////////////////////////////////
struct BufferDesc { int w,h,pitch; void* data; BufferDesc(int width,int height):w(width),h(height),pitch((width*2+7)&~7),data(new unsigned char[pitch*height]){} ~BufferDesc(){delete [] data;} };
///////////////////////////////////////////////////////////////////////////// class MyApp : public EK_Application { ///////////////////////////////////////////////////////////////////// HFONT m_Font; HBITMAP m_FontBMP; unsigned short* m_FontMap; HDC m_FontDC; BufferDesc* m_surface; ///////////////////////////////////////////////////////////////////// EK_TMessage<MyApp> MsgDestroy; EK_TMessage<MyApp> MsgCommand; ///////////////////////////////////////////////////////////////////// LRESULT OnDestroy(WPARAM wParam, LPARAM lParam, BOOL& bHandled); LRESULT OnCommand(WPARAM wParam, LPARAM lParam, BOOL& bHandled); ///////////////////////////////////////////////////////////////////// void AfterCreate(); void Idle(); void BeforeExit(); ///////////////////////////////////////////////////////////////////// void InitFont(); void DestroyFont(); void DrawChar(unsigned int c); ///////////////////////////////////////////////////////////////////// public: ///////////////////////////////////////////////////////////////////// MyApp():EK_Application(),m_surface(0){} MyApp(const HINSTANCE hInstance):EK_Application(hInstance),m_surface(0){} ~MyApp(){} ///////////////////////////////////////////////////////////////////// }; /////////////////////////////////////////////////////////////////////////////
fontmaker.cpp
///////////////////////////////////////////////////////////////////////////// // ELEMENT GDI/DX Game Engine [Version 2002/3/1] ///////////////////////////////////////////////////////////////////////////// // Original Author : 邱海峰[Southdy] // OICQ : 359766 // EMAIL: topmud@263.net ///////////////////////////////////////////////////////////////////////////// // DESCRIPTION: // OTHER: 邱海峰创建于2002/3/1 ///////////////////////////////////////////////////////////////////////////// #pragma message("点阵汉字制作简单演示") ///////////////////////////////////////////////////////////////////////////// // 包含文件 /////////////////////////////////////////////////////////////////////////////
#include "fontmaker.h" #include <fstream.h>
#define FONTPOINT 12
///////////////////////////////////////////////////////////////////////////// // FUNCTION: // PURPOSE: 窗口程序入口 // ENTRY: // EXITS: // SUCCESS : // FAILURE : // OTHER: ///////////////////////////////////////////////////////////////////////////// int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MyApp mywin(hInstance); if(!mywin.InitMain(NULL,"ELEMENT","云海孤峰", WS_VISIBLE|WS_SYSMENU|WS_MINIMIZEBOX|WS_OVERLAPPED,0,0,640,480,NULL,"EK_MENU")) return 0; mywin.Excute(); return 0; }
///////////////////////////////////////////////////////////////////////////// // FUNCTION: // PURPOSE: 主窗口WM_DESTROY消息处理函数 // ENTRY: // EXITS: // SUCCESS : // FAILURE : // OTHER: ///////////////////////////////////////////////////////////////////////////// LRESULT MyApp::OnDestroy(WPARAM wParam, LPARAM lParam, BOOL& bHandled) { PostQuitMessage(0); bHandled = 1; return 0; }
///////////////////////////////////////////////////////////////////////////// // FUNCTION: // PURPOSE: 主窗口WM_COMMAND消息处理函数 // ENTRY: // EXITS: // SUCCESS : // FAILURE : // OTHER: ///////////////////////////////////////////////////////////////////////////// LRESULT MyApp::OnCommand(WPARAM wParam, LPARAM lParam, BOOL& bHandled) { switch(LOWORD(wParam)) { case 10: break; case 11: DestroyWindow(GetWnd()); break; }
bHandled = 0; return 0; }
///////////////////////////////////////////////////////////////////////////// // FUNCTION: void MyApp::AfterCreate() // PURPOSE: 运行前期准备函数 // ENTRY: // EXITS: // SUCCESS : // FAILURE : // OTHER: ///////////////////////////////////////////////////////////////////////////// void MyApp::AfterCreate() { MsgDestroy.SetMsgHandler(this,&MyApp::OnDestroy); MsgCommand.SetMsgHandler(this,&MyApp::OnCommand); RegMsgHandler(WM_DESTROY,&MsgDestroy); RegMsgHandler(WM_COMMAND,&MsgCommand); m_surface=new BufferDesc(640,480); InitFont(); DrawChar('家'); }
///////////////////////////////////////////////////////////////////////////// // FUNCTION: void MyApp::Idle() // PURPOSE: 循环工作函数 // ENTRY: // EXITS: // SUCCESS : // FAILURE : // OTHER: ///////////////////////////////////////////////////////////////////////////// static int GDIbmpinfo16[13]={sizeof(BITMAPINFOHEADER),0,0,0x100001,BI_BITFIELDS,0,0,0,0,0,0xf800,0x7e0,0x1f}; ///////////////////////////////////////////////////////////////////////////// void MyApp::Idle() { HDC myhdc=GetDC(GetWnd()); if(!myhdc) return; GDIbmpinfo16[1]=m_surface->pitch>>1,GDIbmpinfo16[2]=-m_surface->h; SetDIBitsToDevice(myhdc,0,0,m_surface->w,m_surface->h,0,0,0,m_surface->h,m_surface->data,(BITMAPINFO*)&(GDIbmpinfo16[0]),0); ReleaseDC(GetWnd(),myhdc); }
///////////////////////////////////////////////////////////////////////////// // FUNCTION: void MyApp::BeforeExit() // PURPOSE: 退出前期准备函数 // ENTRY: // EXITS: // SUCCESS : // FAILURE : // OTHER: ///////////////////////////////////////////////////////////////////////////// void MyApp::BeforeExit() { DestroyFont(); delete m_surface; }
///////////////////////////////////////////////////////////////////////////// void MyApp::InitFont() { GDIbmpinfo16[1]=FONTPOINT,GDIbmpinfo16[2]=-FONTPOINT; HDC myhdc=GetDC(GetWnd()); if(!myhdc) return; m_FontBMP = CreateDIBSection(myhdc,(BITMAPINFO*)&(GDIbmpinfo16[0]),DIB_RGB_COLORS,(void**)&m_FontMap,0,0); ReleaseDC(GetWnd(),myhdc); for(int i=0;i<FONTPOINT*FONTPOINT;i++) m_FontMap=0x1f; m_FontDC=CreateCompatibleDC(NULL); SelectObject(m_FontDC,m_FontBMP); m_Font=CreateFont( FONTPOINT,0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,PROOF_QUALITY,DEFAULT_QUALITY,"宋体"); SelectObject(m_FontDC,m_Font); }
///////////////////////////////////////////////////////////////////////////// void MyApp::DestroyFont() { DeleteDC(m_FontDC); DeleteObject(m_FontBMP); DeleteObject(m_Font); }
///////////////////////////////////////////////////////////////////////////// static RECT rect={0,0,FONTPOINT,FONTPOINT}; /////////////////////////////////////////////////////////////////////////////// void MyApp::DrawChar(unsigned int c) { char str[3]={(c&0xff00)>>8,c&0xff,0}; if(c<0x80) { str[0]=str[1]; str[1]=0; }
DrawText(m_FontDC,str,1,&rect,DT_CENTER); int i=0,j=0; ofstream myoutput("test.txt"); if(!myoutput.is_open()) return;
for(i=0;i<FONTPOINT;i++) { for(j=0;j<FONTPOINT;j++) if(m_FontMap[i*FONTPOINT+j]==0) myoutput<<"*"; else myoutput<<" "; myoutput<<endl; } myoutput.close(); } /////////////////////////////////////////////////////////////////////////////
|