diff options
Diffstat (limited to 'Cube.cpp')
| -rwxr-xr-x | Cube.cpp | 243 |
1 files changed, 243 insertions, 0 deletions
diff --git a/Cube.cpp b/Cube.cpp new file mode 100755 index 0000000..d1ce6b2 --- /dev/null +++ b/Cube.cpp | |||
| @@ -0,0 +1,243 @@ | |||
| 1 | // Cube.cpp : Defines the entry point for the application. | ||
| 2 | // | ||
| 3 | |||
| 4 | #include "stdafx.h" | ||
| 5 | #include "Cube.h" | ||
| 6 | |||
| 7 | #define MAX_LOADSTRING 100 | ||
| 8 | #define BORDERLEN 400 | ||
| 9 | #define NUMPTZ 8 | ||
| 10 | |||
| 11 | int startx, starty; | ||
| 12 | |||
| 13 | // Global Variables: | ||
| 14 | HINSTANCE hInst = NULL; // current instance | ||
| 15 | TCHAR szTitle[MAX_LOADSTRING]; // The title bar text | ||
| 16 | TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name | ||
| 17 | |||
| 18 | // Forward declarations of functions included in this code module: | ||
| 19 | ATOM MyRegisterClass(HINSTANCE hInstance); | ||
| 20 | BOOL InitInstance(HINSTANCE, int); | ||
| 21 | LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); | ||
| 22 | LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM); | ||
| 23 | |||
| 24 | int APIENTRY _tWinMain(HINSTANCE hInstance, | ||
| 25 | HINSTANCE hPrevInstance, | ||
| 26 | LPTSTR lpCmdLine, | ||
| 27 | int nCmdShow) | ||
| 28 | { | ||
| 29 | // TODO: Place code here. | ||
| 30 | MSG msg; | ||
| 31 | HACCEL hAccelTable; | ||
| 32 | |||
| 33 | // Initialize global strings | ||
| 34 | LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); | ||
| 35 | LoadString(hInstance, IDC_CUBE, szWindowClass, MAX_LOADSTRING); | ||
| 36 | MyRegisterClass(hInstance); | ||
| 37 | |||
| 38 | // Perform application initialization: | ||
| 39 | if (!InitInstance (hInstance, nCmdShow)) | ||
| 40 | { | ||
| 41 | return FALSE; | ||
| 42 | } | ||
| 43 | |||
| 44 | hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_CUBE); | ||
| 45 | |||
| 46 | // Main message loop: | ||
| 47 | while (GetMessage(&msg, NULL, 0, 0)) | ||
| 48 | { | ||
| 49 | if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) | ||
| 50 | { | ||
| 51 | TranslateMessage(&msg); | ||
| 52 | DispatchMessage(&msg); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | return (int) msg.wParam; | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | // | ||
| 62 | // FUNCTION: MyRegisterClass() | ||
| 63 | // | ||
| 64 | // PURPOSE: Registers the window class. | ||
| 65 | // | ||
| 66 | // COMMENTS: | ||
| 67 | // | ||
| 68 | // This function and its usage are only necessary if you want this code | ||
| 69 | // to be compatible with Win32 systems prior to the 'RegisterClassEx' | ||
| 70 | // function that was added to Windows 95. It is important to call this function | ||
| 71 | // so that the application will get 'well formed' small icons associated | ||
| 72 | // with it. | ||
| 73 | // | ||
| 74 | ATOM MyRegisterClass(HINSTANCE hInstance) | ||
| 75 | { | ||
| 76 | WNDCLASSEX wcex; | ||
| 77 | |||
| 78 | wcex.cbSize = sizeof(WNDCLASSEX); | ||
| 79 | |||
| 80 | wcex.style = CS_HREDRAW | CS_VREDRAW; | ||
| 81 | wcex.lpfnWndProc = (WNDPROC)WndProc; | ||
| 82 | wcex.cbClsExtra = 0; | ||
| 83 | wcex.cbWndExtra = 0; | ||
| 84 | wcex.hInstance = hInstance; | ||
| 85 | wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_CUBE); | ||
| 86 | wcex.hCursor = LoadCursor(NULL, IDC_ARROW); | ||
| 87 | wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+2); | ||
| 88 | wcex.lpszMenuName = (LPCTSTR)IDC_CUBE; | ||
| 89 | wcex.lpszClassName = szWindowClass; | ||
| 90 | wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); | ||
| 91 | |||
| 92 | return RegisterClassEx(&wcex); | ||
| 93 | } | ||
| 94 | |||
| 95 | // | ||
| 96 | // FUNCTION: InitInstance(HANDLE, int) | ||
| 97 | // | ||
| 98 | // PURPOSE: Saves instance handle and creates main window | ||
| 99 | // | ||
| 100 | // COMMENTS: | ||
| 101 | // | ||
| 102 | // In this function, we save the instance handle in a global variable and | ||
| 103 | // create and display the main program window. | ||
| 104 | // | ||
| 105 | BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) | ||
| 106 | { | ||
| 107 | HWND hWnd; | ||
| 108 | int i,j,k; | ||
| 109 | |||
| 110 | hInst = hInstance; // Store instance handle in our global variable | ||
| 111 | |||
| 112 | hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, | ||
| 113 | CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); | ||
| 114 | |||
| 115 | if (!hWnd) | ||
| 116 | { | ||
| 117 | return FALSE; | ||
| 118 | } | ||
| 119 | |||
| 120 | Gfx_init( ); | ||
| 121 | Vector_init( NUMPTS, BORDERLEN ); | ||
| 122 | |||
| 123 | ShowWindow(hWnd, nCmdShow); | ||
| 124 | UpdateWindow(hWnd); | ||
| 125 | |||
| 126 | return TRUE; | ||
| 127 | } | ||
| 128 | |||
| 129 | #include <math.h> | ||
| 130 | #include <search.h> | ||
| 131 | |||
| 132 | // | ||
| 133 | // FUNCTION: WndProc(HWND, unsigned, WORD, LONG) | ||
| 134 | // | ||
| 135 | // PURPOSE: Processes messages for the main window. | ||
| 136 | // | ||
| 137 | // WM_COMMAND - process the application menu | ||
| 138 | // WM_PAINT - Paint the main window | ||
| 139 | // WM_DESTROY - post a quit message and return | ||
| 140 | // | ||
| 141 | // | ||
| 142 | LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) | ||
| 143 | { | ||
| 144 | int wmId, wmEvent; | ||
| 145 | PAINTSTRUCT ps; | ||
| 146 | HDC hdc; | ||
| 147 | |||
| 148 | switch (message) | ||
| 149 | { | ||
| 150 | case WM_COMMAND: | ||
| 151 | wmId = LOWORD(wParam); | ||
| 152 | wmEvent = HIWORD(wParam); | ||
| 153 | // Parse the menu selections: | ||
| 154 | switch (wmId) | ||
| 155 | { | ||
| 156 | case IDM_ABOUT: | ||
| 157 | DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); | ||
| 158 | break; | ||
| 159 | case IDM_EXIT: | ||
| 160 | DestroyWindow(hWnd); | ||
| 161 | break; | ||
| 162 | default: | ||
| 163 | return DefWindowProc(hWnd, message, wParam, lParam); | ||
| 164 | } | ||
| 165 | break; | ||
| 166 | case WM_KEYDOWN: | ||
| 167 | switch ( wParam ) { | ||
| 168 | case 'A': | ||
| 169 | Vector_angle( -0.01, 0, 0); | ||
| 170 | InvalidateRect(hWnd, NULL, TRUE); | ||
| 171 | break; | ||
| 172 | case 'S': | ||
| 173 | Vector_angle( +0.01, 0, 0); | ||
| 174 | InvalidateRect(hWnd, NULL, TRUE); | ||
| 175 | break; | ||
| 176 | case 'Q': | ||
| 177 | Vector_angle( 0, -0.01, 0); | ||
| 178 | InvalidateRect(hWnd, NULL, TRUE); | ||
| 179 | break; | ||
| 180 | case 'W': | ||
| 181 | Vector_angle( 0, +0.01, 0); | ||
| 182 | InvalidateRect(hWnd, NULL, TRUE); | ||
| 183 | break; | ||
| 184 | case 'Y': | ||
| 185 | Vector_angle( 0, 0, -0.01); | ||
| 186 | InvalidateRect(hWnd, NULL, TRUE); | ||
| 187 | break; | ||
| 188 | case 'X': | ||
| 189 | Vector_angle( 0, 0, +0.01); | ||
| 190 | InvalidateRect(hWnd, NULL, TRUE); | ||
| 191 | break; | ||
| 192 | |||
| 193 | } | ||
| 194 | break; | ||
| 195 | case WM_LBUTTONDOWN: | ||
| 196 | startx = HIWORD(lParam); | ||
| 197 | starty = LOWORD(lParam); | ||
| 198 | |||
| 199 | break; | ||
| 200 | case WM_MOUSEMOVE: | ||
| 201 | if ( wParam & MK_LBUTTON) { | ||
| 202 | Vector_move( startx - HIWORD(lParam) , LOWORD(lParam) - starty ); | ||
| 203 | |||
| 204 | startx = HIWORD(lParam); | ||
| 205 | starty = LOWORD(lParam); | ||
| 206 | |||
| 207 | InvalidateRect( hWnd, NULL, TRUE); | ||
| 208 | } | ||
| 209 | break; | ||
| 210 | case WM_PAINT: | ||
| 211 | { | ||
| 212 | hdc = BeginPaint(hWnd, &ps); | ||
| 213 | |||
| 214 | EndPaint(hWnd, &ps); | ||
| 215 | break; | ||
| 216 | } | ||
| 217 | case WM_DESTROY: | ||
| 218 | PostQuitMessage(0); | ||
| 219 | break; | ||
| 220 | default: | ||
| 221 | return DefWindowProc(hWnd, message, wParam, lParam); | ||
| 222 | } | ||
| 223 | return 0; | ||
| 224 | } | ||
| 225 | |||
| 226 | // Message handler for about box. | ||
| 227 | LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) | ||
| 228 | { | ||
| 229 | switch (message) | ||
| 230 | { | ||
| 231 | case WM_INITDIALOG: | ||
| 232 | return TRUE; | ||
| 233 | |||
| 234 | case WM_COMMAND: | ||
| 235 | if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) | ||
| 236 | { | ||
| 237 | EndDialog(hDlg, LOWORD(wParam)); | ||
| 238 | return TRUE; | ||
| 239 | } | ||
| 240 | break; | ||
| 241 | } | ||
| 242 | return FALSE; | ||
| 243 | } | ||
