开通博客园第二天

昨天有时间把博客开通了,以后持续以语言、图形学、操作系统为主更博,github地址:https://github.com/Vcanccc

马上要考试了,抓紧时间复习中,最近一个星期花在编程上面的时间很少了。

先贴一段

  1 #pragma once
  2 #ifndef _SYSTEM_H_
  3 #define _SYSTEM_H_
  4 
  5 class cApplication
  6 {
  7 private:
  8     HINSTANCE     m_hInst;
  9     HWND          m_hWnd;
 10 
 11 protected:
 12     char          m_Class[MAX_PATH];
 13     char          m_Caption[MAX_PATH];
 14 
 15     WNDCLASSEX    m_wcex;
 16 
 17     DWORD         m_Style;
 18     DWORD         m_XPos;
 19     DWORD         m_YPos;
 20     DWORD         m_Width;
 21     DWORD         m_Height;
 22 
 23 public:
 24     cApplication();
 25 
 26     HWND      GethWnd();
 27     HINSTANCE GethInst();
 28 
 29     BOOL Run();
 30     BOOL Error(BOOL Fatal, char *Text, ...);
 31 
 32     BOOL Move(long XPos, long YPos);
 33     BOOL Resize(long Width, long Height);
 34 
 35     BOOL ShowMouse(BOOL Show = TRUE);
 36 
 37     virtual LRESULT CALLBACK MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { return DefWindowProc(hWnd, uMsg, wParam, lParam); }
 38     virtual BOOL Init() { return TRUE; }
 39     virtual BOOL Shutdown() { return TRUE; }
 40     virtual BOOL Frame() { return TRUE; }
 41 };
 42 
 43 static cApplication *g_pApplication = NULL;
 44 static long FAR PASCAL AppWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
 45 
 46 enum Purposes {
 47     NOPURPOSE = 0,
 48     INITPURPOSE,
 49     SHUTDOWNPURPOSE,
 50     FRAMEPURPOSE
 51 };
 52 
 53 class cStateManager
 54 {
 55     typedef struct sState {
 56         void(*Function)(void *Ptr, long Purpose);
 57         sState *Next;
 58 
 59         sState()
 60         {
 61             Function = NULL;
 62             Next = NULL;
 63         }
 64 
 65         ~sState()
 66         {
 67             delete Next;
 68         }
 69     } sState;
 70 
 71 protected:
 72     sState *m_StateParent;
 73 
 74 public:
 75     cStateManager();
 76     ~cStateManager();
 77 
 78     void Push(void(*Function)(void *Ptr, long Purpose), void *DataPtr = NULL);
 79     BOOL Pop(void *DataPtr = NULL);
 80     void PopAll(void *DataPtr = NULL);
 81     BOOL Process(void *DataPtr = NULL);
 82 };
 83 
 84 class cProcessManager
 85 {
 86     typedef struct sProcess {
 87         void(*Function)(void *Ptr, long Purpose);
 88         sProcess *Next;
 89 
 90         sProcess()
 91         {
 92             Function = NULL;
 93             Next = NULL;
 94         }
 95 
 96         ~sProcess()
 97         {
 98             delete Next; Next = NULL;
 99         }
100     } sProcess;
101 
102 protected:
103     sProcess *m_ProcessParent;
104 
105 public:
106     cProcessManager();
107     ~cProcessManager();
108 
109     void Push(void(*Process)(void *Ptr, long Purpose), void *DataPtr = NULL);
110     BOOL Pop(void *DataPtr = NULL);
111     void PopAll(void *DataPtr = NULL);
112     void Process(void *Ptr = NULL);
113 };
114 
115 class cDataPackage
116 {
117 protected:
118     void          *m_Buf;
119     unsigned long  m_Size;
120 
121 public:
122     cDataPackage();
123     ~cDataPackage();
124 
125     void *Create(unsigned long Size);
126     void Free();
127 
128     BOOL Save(char *Filename);
129     void *Load(char *Filename, unsigned long *Size);
130 
131     void          *GetPtr();
132     unsigned long  GetSize();
133 };
134 
135 #endif

下面是函数内容:

  1 #include "Core_Global.h"
  2 
  3 cApplication::cApplication()
  4 {
  5     // Save instance handle
  6     g_pApplication = this;
  7 
  8     // Get the instance handle
  9     m_hInst = GetModuleHandle(NULL);
 10 
 11     // Set a default window class and caption
 12     strcpy(m_Class, "AppClass");
 13     strcpy(m_Caption, "Application Caption");
 14 
 15     // Set default window style, position, width, height
 16     m_Style = WS_OVERLAPPEDWINDOW;
 17     m_XPos = 0;
 18     m_YPos = 0;
 19     m_Width = 256;
 20     m_Height = 256;
 21 
 22     // Set default WNDCLASSEX structure
 23     m_wcex.cbSize = sizeof(WNDCLASSEX);
 24     m_wcex.style = CS_CLASSDC;
 25     m_wcex.lpfnWndProc = AppWindowProc;
 26     m_wcex.cbClsExtra = 0;
 27     m_wcex.cbWndExtra = 0;
 28     m_wcex.hInstance = m_hInst;
 29     m_wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 30     m_wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
 31     m_wcex.hbrBackground = NULL;
 32     m_wcex.lpszMenuName = NULL;
 33     m_wcex.lpszClassName = m_Class;
 34     m_wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
 35 }
 36 
 37 HWND cApplication::GethWnd()
 38 {
 39     return m_hWnd;
 40 }
 41 
 42 HINSTANCE cApplication::GethInst()
 43 {
 44     return m_hInst;
 45 }
 46 
 47 BOOL cApplication::Run()
 48 {
 49     MSG Msg;
 50 
 51     // Register window class
 52     if (!RegisterClassEx(&m_wcex))
 53         return FALSE;
 54 
 55     // Create the Main Window
 56     m_hWnd = CreateWindow(m_Class, m_Caption,
 57         m_Style,
 58         m_XPos, m_YPos,
 59         m_Width, m_Height,
 60         NULL, NULL, m_hInst, NULL);
 61     if (!m_hWnd)
 62         return FALSE;
 63 
 64     // Show and update the window
 65     ShowWindow(m_hWnd, SW_NORMAL);
 66     UpdateWindow(m_hWnd);
 67 
 68     // Make sure client area is correct size
 69     Resize(m_Width, m_Height);
 70 
 71     // Initialize COM
 72     CoInitialize(NULL);
 73 
 74     if (Init() == TRUE) {
 75         // Enter the message pump
 76         ZeroMemory(&Msg, sizeof(MSG));
 77         while (Msg.message != WM_QUIT) {
 78 
 79             // Handle Windows messages (if any)
 80             if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) {
 81                 TranslateMessage(&Msg);
 82                 DispatchMessage(&Msg);
 83             }
 84             else {
 85                 // Do per-frame processing, break on FALSE return value
 86                 if (Frame() == FALSE)
 87                     break;
 88             }
 89         }
 90     }
 91 
 92     Shutdown();
 93 
 94     // Shutdown COM
 95     CoUninitialize();
 96 
 97     // Unregister the window class
 98     UnregisterClass(m_Class, m_hInst);
 99 
100     return TRUE;
101 }
102 
103 BOOL cApplication::Error(BOOL Fatal, char *Text, ...)
104 {
105     char CaptionText[12];
106     char ErrorText[2048];
107     va_list valist;
108 
109     // Build the message box caption based on fatal flag
110     if (Fatal == FALSE)
111         strcpy(CaptionText, "Error");
112     else
113         strcpy(CaptionText, "Fatal Error");
114 
115     // Build variable text buffer
116     va_start(valist, Text);
117     vsprintf(ErrorText, Text, valist);
118     va_end(valist);
119 
120     // Display the message box
121     MessageBox(NULL, ErrorText, CaptionText, MB_OK | MB_ICONEXCLAMATION);
122 
123     // Post a quit message if error was fatal
124     if (Fatal == TRUE)
125         PostQuitMessage(0);
126 
127     return TRUE;
128 }
129 
130 BOOL cApplication::Move(long XPos, long YPos)
131 {
132     RECT ClientRect;
133 
134     GetClientRect(m_hWnd, &ClientRect);
135     MoveWindow(m_hWnd, XPos, YPos, ClientRect.right, ClientRect.bottom, TRUE);
136 
137     return TRUE;
138 }
139 
140 BOOL cApplication::Resize(long Width, long Height)
141 {
142     RECT WndRect, ClientRect;
143     long WndWidth, WndHeight;
144 
145     GetWindowRect(m_hWnd, &WndRect);
146     GetClientRect(m_hWnd, &ClientRect);
147 
148     WndWidth = (WndRect.right - (ClientRect.right - Width)) - WndRect.left;
149     WndHeight = (WndRect.bottom - (ClientRect.bottom - Height)) - WndRect.top;
150 
151     MoveWindow(m_hWnd, WndRect.left, WndRect.top, WndWidth, WndHeight, TRUE);
152 
153     return TRUE;
154 }
155 
156 BOOL cApplication::ShowMouse(BOOL Show)
157 {
158     ShowCursor(Show);
159     return TRUE;
160 }
161 
162 // The message procedure - empty except for destroy message
163 long FAR PASCAL AppWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
164 {
165     switch (uMsg) {
166     case WM_DESTROY:
167         PostQuitMessage(0);
168         return 0;
169 
170     default: return g_pApplication->MsgProc(hWnd, uMsg, wParam, lParam);
171     }
172 }
173 
174 cStateManager::cStateManager()
175 {
176     m_StateParent = NULL;
177 }
178 
179 cStateManager::~cStateManager()
180 {
181     PopAll();
182 }
183 
184 // Push a function on to the stack
185 void cStateManager::Push(void(*Function)(void *Ptr, long Purpose), void *DataPtr)
186 {
187     sState *StatePtr;
188 
189     // Don't push a NULL value
190     if (Function != NULL) {
191         // Allocate a new state and push it on stack
192         StatePtr = new sState();
193 
194         StatePtr->Function = Function;
195         StatePtr->Next = m_StateParent;
196 
197         m_StateParent = StatePtr;
198 
199         // Call state with init purpose
200         StatePtr->Function(DataPtr, INITPURPOSE);
201     }
202 }
203 
204 BOOL cStateManager::Pop(void *DataPtr)
205 {
206     sState *StatePtr;
207 
208     // Remove the head of stack (if any)
209     if ((StatePtr = m_StateParent) != NULL) {
210         // First call with shutdown purpose
211         m_StateParent->Function(DataPtr, SHUTDOWNPURPOSE);
212 
213         m_StateParent = StatePtr->Next;
214         StatePtr->Next = NULL;
215         delete StatePtr;
216     }
217 
218     // return TRUE if more states exist, FALSE otherwise
219     if (m_StateParent == NULL)
220         return FALSE;
221     return TRUE;
222 }
223 
224 void cStateManager::PopAll(void *DataPtr)
225 {
226     while (Pop(DataPtr) == TRUE);
227 }
228 
229 BOOL cStateManager::Process(void *DataPtr)
230 {
231     // return an error if no more states
232     if (m_StateParent == NULL)
233         return FALSE;
234 
235     // Process the top-most state
236     m_StateParent->Function(DataPtr, FRAMEPURPOSE);
237 
238     return TRUE;
239 }
240 
241 cProcessManager::cProcessManager()
242 {
243     m_ProcessParent = NULL;
244 }
245 
246 cProcessManager::~cProcessManager()
247 {
248     // Pop each process
249     while (Pop() == TRUE);
250 }
251 
252 // Push a function on to the stack
253 void cProcessManager::Push(void(*Process)(void *Ptr, long Purpose), void *DataPtr)
254 {
255     // Don't push a NULL value
256     if (Process != NULL) {
257         // Allocate a new process and push it on stack
258         sProcess *ProcessPtr = new sProcess();
259         ProcessPtr->Function = Process;
260         ProcessPtr->Next = m_ProcessParent;
261         m_ProcessParent = ProcessPtr;
262 
263         // Call process with init purpose
264         ProcessPtr->Function(DataPtr, INITPURPOSE);
265     }
266 }
267 
268 // Pop top process from stack
269 BOOL cProcessManager::Pop(void *DataPtr)
270 {
271     sProcess *ProcessPtr;
272 
273     // Remove the head of stack (if any)
274     if ((ProcessPtr = m_ProcessParent) != NULL) {
275         // First call with shutdown purpose
276         m_ProcessParent->Function(DataPtr, SHUTDOWNPURPOSE);
277 
278         m_ProcessParent = ProcessPtr->Next;
279         ProcessPtr->Next = NULL;
280         delete ProcessPtr;
281     }
282 
283     // return TRUE if more processes exist, FALSE otherwise
284     if (m_ProcessParent == NULL)
285         return FALSE;
286     return TRUE;
287 }
288 
289 void cProcessManager::PopAll(void *DataPtr)
290 {
291     while (Pop(DataPtr) == TRUE);
292 }
293 
294 // Process all functions
295 void cProcessManager::Process(void *DataPtr)
296 {
297     sProcess *ProcessPtr = m_ProcessParent;
298 
299     while (ProcessPtr != NULL) {
300         ProcessPtr->Function(DataPtr, FRAMEPURPOSE);
301         ProcessPtr = ProcessPtr->Next;
302     }
303 }
304 
305 cDataPackage::cDataPackage()
306 {
307     m_Buf = NULL;
308     m_Size = 0;
309 }
310 
311 cDataPackage::~cDataPackage()
312 {
313     Free();
314 }
315 
316 void *cDataPackage::Create(unsigned long Size)
317 {
318     // Free a previously created buffer
319     Free();
320 
321     // Allocate some memory and return a pointer
322     return (m_Buf = (void*)new char[(m_Size = Size)]);
323 }
324 
325 // Free the allocated memory
326 void cDataPackage::Free()
327 {
328     delete m_Buf;
329     m_Buf = NULL;
330     m_Size = 0;
331 }
332 
333 BOOL cDataPackage::Save(char *Filename)
334 {
335     FILE *fp;
336 
337     // Make sure there's something to write
338     if (m_Buf != NULL && m_Size) {
339         // Open file, write size and data
340         if ((fp = fopen(Filename, "wb")) != NULL) {
341             fwrite(&m_Size, 1, 4, fp);
342             fwrite(m_Buf, 1, m_Size, fp);
343             fclose(fp);
344             return TRUE;
345         }
346     }
347 
348     return FALSE;
349 }
350 
351 void *cDataPackage::Load(char *Filename, unsigned long *Size)
352 {
353     FILE *fp;
354 
355     // Free a prior buffer
356     Free();
357 
358     if ((fp = fopen(Filename, "rb")) != NULL) {
359         // Read in size and data
360         fread(&m_Size, 1, 4, fp);
361         if ((m_Buf = (void*)new char[m_Size]) != NULL)
362             fread(m_Buf, 1, m_Size, fp);
363         fclose(fp);
364 
365         // Store size to return
366         if (Size != NULL)
367             *Size = m_Size;
368 
369         // return pointer
370         return m_Buf;
371     }
372 
373     return NULL;
374 }
375 
376 void *cDataPackage::GetPtr()
377 {
378     return m_Buf;
379 }
380 
381 unsigned long cDataPackage::GetSize()
382 {
383     return m_Size;
384 }

这是directx的一段内核的代码,不是我自己写的,照着敲过练手,觉得别人的思维真强,这个内核的图像部分太多就不贴了,慢慢进步。

原文地址:https://www.cnblogs.com/Vcanccc/p/5582335.html