📄 sim.cpp
字号:
/*---------------------------------------------------------------------*
* *
* THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY *
* INFORMATION. IF PUBLICATION OCCURS, THE FOLLOWING NOTICE APPLIES: *
* "COPYRIGHT 2001 MICHAEL TSIROULNIKOV, ALL RIGHTS RESERVED" *
* *
* DO NOT REMOVE THIS COPYRIGHT NOTICE. *
* *
*---------------------------------------------------------------------*/
#include <stdio.h>
#include "stdafx.h"
#include "resource.h"
#include "sim.h"
#include "demo.h"
#define MAX_LOADSTRING 100
// Global Variables:
HDC SIM_hDc;
HBRUSH SIM_hBlackBrush;
HPEN SIM_ahPen[SIM_PENS];
COLORREF SIM_aColor[SIM_COLORS];
HINSTANCE SIM_hInst; // current instance
HWND SIM_hWnd;
static TCHAR __szTitle[MAX_LOADSTRING]; // The title bar text
static TCHAR __szWindowClass[MAX_LOADSTRING]; // The title bar text
static char __aBuff[201];
static int __InitErr;
// Foward declarations of functions included in this code module:
static BOOL __init_instance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
/* ------------------------------------------------------------------- */
int APIENTRY WinMain
/* ------------------------------------------------------------------- */
(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
MSG msg;
BOOL Rc;
// Perform application initialization:
if (__init_instance (hInstance, nCmdShow) == FALSE)
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_SIM);
__InitErr = demo_get_cfg();
if (__InitErr == 0)
{
demo_init(__InitErr);
}
else
{
DEMO_StepMode = TRUE;
}
// Main message loop:
for (;;)
{
if (DEMO_StepMode)
{
Rc = GetMessage(&msg, NULL, 0, 0);
if (Rc)
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
break; // WM_QUIT
}
}
else
{
Rc = PeekMessage(&msg, SIM_hWnd, 0, 0, PM_REMOVE);
if (Rc)
{
if (msg.message != WM_QUIT)
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else // WM_QUIT
{
break;
}
}
else // no message
{
if (!__InitErr) demo_run();
}
}
}
return 0;
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
/* ------------------------------------------------------------------- */
BOOL __init_instance
/* ------------------------------------------------------------------- */
(
HINSTANCE hInstance,
int nCmdShow
)
{
WNDCLASSEX wcex;
LOGBRUSH lb;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, __szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_SIM, __szWindowClass, MAX_LOADSTRING);
SIM_hBlackBrush = CreateSolidBrush(RGB(0,0,0)); // deleted by system ??
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_OWNDC;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, (LPCTSTR)IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = SIM_hBlackBrush; // deleted by system
wcex.lpszMenuName = (LPCSTR)IDC_SIM;
wcex.lpszClassName = __szWindowClass;
wcex.hIconSm = LoadIcon(NULL, (LPCTSTR)IDI_APPLICATION);
RegisterClassEx(&wcex);
SIM_hWnd = CreateWindow(
__szWindowClass, //
__szTitle,
WS_OVERLAPPEDWINDOW, // style
CW_USEDEFAULT, // h pos
0, // v pos
1200,//CW_USEDEFAULT, // h size
900, // v size
NULL, // parent
NULL, // menu
hInstance, // app
NULL); // data
if (!SIM_hWnd)
{
return FALSE;
}
SIM_hInst = hInstance; // Store instance handle in our global variable
SIM_hDc = GetDC(SIM_hWnd);
// create pens and colors
lb.lbStyle = BS_SOLID;
lb.lbHatch = 0;
#define CM 255
for (int k = 0; k < SIM_COLORS; k++)
{
SIM_aColor[k] = RGB(
((k & 0x03)>>0)*(CM/3),
((k & 0x0c)>>2)*(CM/3),
((k & 0x30)>>4)*(CM/3));
lb.lbColor = SIM_aColor[k];
SIM_ahPen[k] = ExtCreatePen(
PS_COSMETIC | PS_SOLID, // style
1, // width
&lb,
0,
NULL);
}
SetBkColor(SIM_hDc,RGB(0,0,0));
ShowWindow(SIM_hWnd, nCmdShow);
UpdateWindow(SIM_hWnd);
return TRUE;
}
/* ------------------------------------------------------------------- */
void __delete_instance
/* ------------------------------------------------------------------- */
(
HWND hWnd
)
{
for (int k = 0; k < SIM_COLORS; k++)
{
DeleteObject(SIM_ahPen[k]);
}
DeleteObject(SIM_hBlackBrush);
ReleaseDC(hWnd, SIM_hDc);
demo_exit();
DEMO_StepMode = TRUE;
PostQuitMessage(0);
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
/* ------------------------------------------------------------------- */
LRESULT CALLBACK WndProc
/* ------------------------------------------------------------------- */
(
HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam
)
{
switch (message)
{
case WM_COMMAND:
{
int wmId, wmEvent;
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(SIM_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case ID_EXIT:
DestroyWindow(hWnd);
break;
case ID_RUN:
DEMO_StepMode = FALSE;
break;
case ID_STOP:
DEMO_StepMode = TRUE;
break;
case ID_STEP:
if (__InitErr == 0)
{
demo_run();
}
DEMO_StepMode = TRUE;
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
// TCHAR szHello[MAX_LOADSTRING];
// LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
// RECT rt;
// GetClientRect(hWnd, &rt);
// DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
}
case WM_DESTROY:
{
__delete_instance(hWnd);
break;
}
default:
{
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
return 0;
}
// Mesage handler for about box.
/* ------------------------------------------------------------------- */
LRESULT CALLBACK About
/* ------------------------------------------------------------------- */
(
HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam
)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
// UTILITY
/* ------------------------------------------------------------------- */
void text_out
/* ------------------------------------------------------------------- */
(
int x,
int y,
int ColorIdx,
char *pTxt
)
{
if (ColorIdx < SIM_COLORS)
{
SetTextColor(SIM_hDc, SIM_aColor[ColorIdx]);
TextOut(SIM_hDc, x, y, pTxt, strlen(pTxt));
}
}
/* ------------------------------------------------------------------- */
void val_out
/* ------------------------------------------------------------------- */
(
int x,
int y,
int ColorIdx,
char *pFmt, ...
)
{
va_list argPtr; /* Argument list pointer */
va_start(argPtr, pFmt ); /* Initialize va_ functions */
vsprintf(__aBuff, pFmt, argPtr);
if (ColorIdx < SIM_COLORS)
{
SetTextColor(SIM_hDc, SIM_aColor[ColorIdx]);
}
else ; // leave color as is
TextOut(SIM_hDc, x, y, __aBuff, strlen(__aBuff));
va_end( argPtr ); /* Close va_ functions */
}
/* ------------------------------------------------------------------- */
void set_color
/* ------------------------------------------------------------------- */
(
int idx
)
{
if (idx < SIM_PENS)
{
SelectObject(SIM_hDc, SIM_ahPen[idx]);
SetTextColor(SIM_hDc, SIM_aColor[idx]);
}
}
/* ------------------------------------------------------------------- */
void cline
/* ------------------------------------------------------------------- */
(
int xFrom,
int yFrom,
int xTo,
int yTo,
int PenIdx
)
{
POINT prev;
if (PenIdx < SIM_PENS)
{
SelectObject(SIM_hDc, SIM_ahPen[PenIdx]);
}
MoveToEx(SIM_hDc, xFrom, yFrom, &prev);
LineTo(SIM_hDc, xTo, yTo);
}
/* ------------------------------------------------------------------- */
void line
/* ------------------------------------------------------------------- */
(
int xFrom,
int yFrom,
int xTo,
int yTo
)
{
POINT prev;
MoveToEx(SIM_hDc, xFrom, yFrom, &prev);
LineTo(SIM_hDc, xTo, yTo);
}
/* ------------------------------------------------------------------- */
void pixel
/* ------------------------------------------------------------------- */
(
int x,
int y,
int clr
)
{
SetPixel(SIM_hDc, x, y, RGB(((clr & 0x03)>>0)*(CM/3),
((clr & 0x0c)>>2)*(CM/3),
((clr & 0x30)>>4)*(CM/3)));
}
/* ------------------------------------------------------------------- */
void bar
/* ------------------------------------------------------------------- */
(
int xl,
int yt,
int xr,
int yb
)
{
RECT rect;
SetRect(&rect, xl,yt,xr,yb);
FillRect(SIM_hDc, &rect, SIM_hBlackBrush);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -