[ Create new paste | Recent pastes ]
| Description: TrackDrawer, Author: PG, Time: 2009-11-07 19:01, Language: C http://nopaste.gamedev.pl?&id=4746 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | #include <windows.h> #include <stdio.h> #include <math.h> #define MEMORY_LIMIT 15 // Limit of spybot's memory #define GRID_DETAIL 15 #define GRID_COLOUR RGB (200, 200, 200) #define WAYPOINT_FILL_COLOUR RGB (0, 250, 0) #define WAYPOINT_OUTLINE_COLOUR RGB (0, 0, 0) #define LINES_COLOUR RGB (0, 0, 0) LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; void CreateHeader () ; void SaveTrack() ; float Diagonal (float, float) ; static int iScaleNumerator, iScaleDominator ; // Scale numbers (px to cm convertion) int cWPoints ; int Angles[MEMORY_LIMIT], Distances[MEMORY_LIMIT] ; // Store angles and distances between waypoints POINT xyWayPoints [MEMORY_LIMIT] ; // This array store coordinates of waypoints int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT ("TrackDrawer") ; HWND hwnd ; MSG msg ; WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass)) { MessageBox (NULL, TEXT ("Memory error!"), szAppName, MB_ICONERROR) ; return 0 ; } hwnd = CreateWindow (szAppName, TEXT ("TrackDrawer"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return msg.wParam ; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { TCHAR szBuffer [16] ; HDC hdc ; PAINTSTRUCT ps ; TEXTMETRIC tm ; static int cxClient, cyClient ; // They're necessary to draw a grid in a background static int cxChar, cyChar, cxCaps ; // These make possible to draw a suitable rectangle under text informations int xTextBoxLeft, yTextBoxTop, iStrLenght; // Variables which define coordinates of rectangle under text information int i ; // Variables using in loops switch (message) { case WM_CREATE: hdc = BeginPaint (hwnd, &ps) ; // Getting info abou font GetTextMetrics (hdc, &tm) ; cxChar = tm.tmAveCharWidth ; cyChar = tm.tmHeight ; cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ; // Defining start values of some variables cWPoints = 0 ; iScaleDominator = 1 ; iScaleNumerator = 1 ; EndPaint (hwnd, &ps) ; return 0 ; case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ; // Drawing gray grid in a background with PS_DOT pen SelectObject (hdc, CreatePen (PS_DOT, 0, GRID_COLOUR)) ; for (i = GRID_DETAIL ; i < cxClient ; i+=GRID_DETAIL) { MoveToEx (hdc, i, 0, NULL) ; LineTo (hdc, i, cyClient) ; } for( i = GRID_DETAIL ; i < cyClient ; i+=GRID_DETAIL) { MoveToEx (hdc, 0, i, NULL) ; LineTo (hdc, cxClient, i) ; } // Drawing waypoints SelectObject (hdc, CreatePen (PS_SOLID, 2, WAYPOINT_OUTLINE_COLOUR)) ; SelectObject (hdc, CreateSolidBrush (WAYPOINT_FILL_COLOUR)) ; for (i = 0 ; i < cWPoints ; i++) Ellipse(hdc, xyWayPoints[i].x-5, xyWayPoints[i].y-5, xyWayPoints[i].x+5, xyWayPoints[i].y+5) ; // Linking waypoints SelectObject (hdc, CreatePen (PS_SOLID, 2, LINES_COLOUR)) ; Polyline (hdc, xyWayPoints, cWPoints) ; // Pritnig text information about distances between waypoints if (cWPoints > 1) { for (i=0 ; i < cWPoints - 1 ; i++) { iStrLenght = wsprintf (szBuffer, TEXT ("%d cm"), \ (int) Diagonal(xyWayPoints[i].x - xyWayPoints[i+1].x, xyWayPoints[i].y - xyWayPoints[i+1].y) / iScaleNumerator * iScaleDominator) ; // Defining position for text information xTextBoxLeft = ((xyWayPoints[i].x + xyWayPoints[i+1].x) / 2) - (iStrLenght / 2 * cxCaps) ; yTextBoxTop = ((xyWayPoints[i].y + xyWayPoints[i+1].y) / 2) - (cyChar / 2); //iStrLenght++ ; TextOut (hdc, xTextBoxLeft, yTextBoxTop, szBuffer, iStrLenght) ; } } // Printing text info about actual scale iStrLenght = wsprintf (szBuffer, TEXT ("%d px - %d cm"), iScaleNumerator, iScaleDominator) ; TextOut (hdc, 0, 0, szBuffer, iStrLenght) ; EndPaint (hwnd, &ps) ; return 0 ; case WM_SIZE: //Getting information about window's size cxClient = LOWORD(lParam) ; cyClient = HIWORD(lParam) ; return 0; case WM_LBUTTONDOWN: if (wParam & MK_LBUTTON) { // Save point of mouse button click as a waypoint if (cWPoints < MEMORY_LIMIT) { xyWayPoints[cWPoints].x = LOWORD (lParam) ; xyWayPoints[cWPoints].y = HIWORD (lParam) ; cWPoints++ ; InvalidateRect (hwnd, NULL, FALSE) ; } } return 0 ; case WM_KEYDOWN: switch (wParam) { case VK_F1: SaveTrack() ; break ; case VK_F5: iScaleNumerator++ ; InvalidateRect (hwnd, NULL, TRUE) ; return ; case VK_F6: if( iScaleNumerator > 1) iScaleNumerator-- ; InvalidateRect (hwnd, NULL, TRUE) ; return ; case VK_F7: iScaleDominator++ ; InvalidateRect (hwnd, NULL, TRUE) ; return ; case VK_F8: if( iScaleDominator > 1) iScaleDominator-- ; InvalidateRect (hwnd, NULL, TRUE) ; return ; case VK_DELETE: cWPoints = 0 ; InvalidateRect (hwnd, NULL, TRUE) ; return; case VK_BACK: if(cWPoints > 0) cWPoints-- ; InvalidateRect (hwnd, NULL, TRUE) ; return; default: return 0; } return 0 ; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; } void SaveTrack() { int i ; // Variables using in loops float fAngle, fTurnAngle, fATangens, fOrientation, fxTemp, fyTemp, fDistance, AbsDivisionXY ; // Start direction of spybot is a top of screen (mathematical grid, not geographic) fOrientation = 90 ; for (i=0 ; i < cWPoints -1; i++) { // It's an algorytm which calculate angle of turn which spybot has to do to get in correct direction fxTemp = -1 * (xyWayPoints[i].x - xyWayPoints[i+1].x) ; // X distance beetwen start and destination positions, -1 because program works in MM_TEXT mode fyTemp = xyWayPoints[i].y - xyWayPoints[i+1].y ; // Y distance beetwen start and destination positions AbsDivisionXY = fxTemp/fyTemp ; AbsDivisionXY = (AbsDivisionXY > 0 ? AbsDivisionXY : (-1 * AbsDivisionXY)) ; fATangens = 90 - atan(AbsDivisionXY) * 180 / M_PI ; // Calculating atan() // These fragment define in which quadrant destination point is (point of reference = start point) and calculate absolute angle (0 deg. - right side of the screen) if (fxTemp >= 0 && fyTemp >= 0) fAngle = fATangens ; else if (fxTemp <= 0 && fyTemp >= 0) fAngle = 180 - fATangens ; else if (fxTemp <= 0 && fyTemp <= 0) fAngle = 180 + fATangens ; else if (fxTemp >= 0 && fyTemp <= 0) fAngle = 360 - fATangens ; // In these fragment program calculate a turn angle which spybot has to do to turn in correct direction. // fOrrientation = angle (in reference to the right of screen) of spybot after previous move if (fOrientation > fAngle) { if (fOrientation - fAngle <= 180) fTurnAngle = fOrientation - fAngle ; else fTurnAngle = -1 * (360 - fOrientation + fAngle) ; } else if (fOrientation < fAngle) { if (fAngle - fOrientation <= 180) fTurnAngle = - (fAngle - fOrientation) ; else fTurnAngle = 360 - fAngle + fOrientation ; } else fTurnAngle = 0 ; // End of algorytm // For calculating angles in next steps it's necessary to save azimuth as an orientation fOrientation = fAngle ; // Writing to arrays Angles[i] = (int) fTurnAngle ; Distances[i] = (int) Diagonal(fxTemp, fyTemp) / iScaleNumerator * iScaleDominator ; } // Creating header file which is using by spybot's program CreateHeader() ; return ; } void CreateHeader() { FILE *fSource ; int i ; fSource = fopen ("track.nqh", "w") ; fprintf (fSource, "int Lines = %d ;\nint AngleTab[%d], DistanceTab[%d] ;\n\nsub initiate ()\n{\n", cWPoints - 1, cWPoints - 1, cWPoints - 1) ; for (i=0 ; i < cWPoints - 1; i++) fprintf (fSource, "\tAngleTab[%d] = %d ;\n\tDistanceTab[%d] = %d ;\n\n", i, Angles[i], i, Distances[i]) ; fprintf (fSource, "\treturn ;\n}") ; fclose (fSource) ; return ; } float Diagonal (float a, float b) { return sqrt(pow(abs(a),2) + pow(abs(b),2)) ; } |
|
|
Copyright © 2007 ayufan @ Warsztat
Based on GeSHi |