Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » A5 Linux to Windows (Porting Blues!)

This thread is locked; no one can reply to it. rss feed Print
A5 Linux to Windows (Porting Blues!)
KeithS
Member #11,950
May 2010

Hello,

I recently put together a small prototype application on A5. The main coding platform was Debian, using CodeBlocks as the IDE. I have had no problems with the Linux code compiling or running (A-5.0.4 w/ g++), and indeed the ported code with a few tweaks runs on Windows 7 64 bit, but gets into some trouble on Win_XP 32 bit.

In short, I compiled the program on my 32 bit / Win_XP platform, using the A-5.0.4 Allegro binaries. When I run the executable on this platform, it functions normally until I click on the "close window" X in the TLH corner. It then gives me that "The program is not functioning.. blah.. blah.. send a report to MicroSoft?... etc" window. I repeat, this only happens on the 32 bit. Even the same 32 bit binary on Win_7_64 apparently works without a hitch.

If anyone can please have a look at the source code contained along with the binary here (the 7z window binary is the one I would like checking, please) and maybe suggest what I might be doing wrong (or if you know of this problem already with A5 on XP). I would be very grateful!

Post Script:

What I originally assumed the problem might be was my (possibly bad) habit in g++ of passing character arrays as char pointers to functions and then using sprintf to populate the array. Like this...

#SelectExpand
1 2void txt_output(char *buff) 3{ 4 sprintf(buff, "Lo and Behold!"); 5} 6 7int main() 8{ 9 char buffer[100]; 10 txt_output(buffer); 11 12 .... 13}

I put this "right" for MSVC++ 2008 by declaring the buffer globally and using sprintf_s() with strlen(buffer) instead, but it still shows that report window after closing down the program. It is vaguely reminiscent of the occasional GDI leak problem I used to get every now and then by the odd bit of frenetic, careless coding in WINAPI. I would just like to be sure it isn't this.

My thanks in advance for any help!

* * * * * * * * * * *

My Noobish Blog

someone972
Member #7,719
August 2006
avatar

I hate to tell you, but you have the syntax for the sprintf_s calls wrong. It is int sprintf_s(char *buffer,size_t sizeOfBuffer,const char *format [,argument] ... );, so you should be putting the buffer length as the second argument, not the last. The second thing wrong with it is that you are trying to find the length of the buffer using strlen, which will find the length until the first null, which could be sooner than the end giving you a smaller buffer, or worse it could cause it to extend past the bounds of the buffer. What you want to do is add another argument to the draw_phase_X functions where you can pass in the buffer size. I'm not certain that's the problem, but with the code included in the 7z it should not have even compiled.

EDIT: Also, globals are bad in most situations, so the way you had it originally would have been better, so long as you add the length argument to the function.

EDIT2: The problem is actually in the last lines. You don't need this part:

Allegro automatically calls all those functions when the program exits, and the al_uninstall_system() call was doing something strange with a ttf font. I found this using a debugger, which gave me a segfault and call stack to work with.

Here is the complete source for GunsimV11.cpp that has the changes, I replaced the sprintf_s calls with snprintf since I'm using Code::Blocks, but it has the same syntax.

#SelectExpand
1//#define _CRT_SECURE_NO_WARNINGS 2#define M_PI 3.141593 3#include "GunSim.h" 4#include "al5setp.h" 5#include <fstream> 6//#include <string> 7//#include <stdio.h> 8 9 10GunPhysics gun_phys; 11const int ZF_SIZE = 8; 12const float SCREEN_1_Y_LOWER_LIMIT = 430.0; 13const float SCREEN_1_Y_UPPER_LIMIT = 127.0; 14 15struct record_position 16{ 17 double x_pos; 18 double y_pos; 19 double velocity; 20 double angle; 21 double flight_time; 22 double mach_no; 23 double rho; 24 double Cd; 25}; 26 27struct button_position 28{ 29 float x1; 30 float x2; 31 float y1; 32 float y2; 33 int rgb[3]; 34 string label; 35}; 36//**************************************************** 37 38bool set_rho_values() 39{ 40 size_t vec_size; 41 int i; 42 ifstream ref_file("rhos.txt"); 43 44 if(ref_file.is_open()) 45 { 46 ref_file >> gun_phys.rho_alt_gradient; 47 ref_file >> gun_phys.rho_at_SL; 48 ref_file >> vec_size; 49 gun_phys.ref_rho_table.resize(vec_size); 50 for(i = 0; i < (int)vec_size; i++) 51 { 52 if(!ref_file.eof()) 53 ref_file >> gun_phys.ref_rho_table.at(i); 54 else 55 { 56 ref_file.close(); 57 return false; 58 } 59 } 60 } 61 else 62 return false; 63 64 ref_file.close(); 65 return true; 66} 67//**************************************************** 68bool set_Mach_Cd_values() 69{ 70 //size_t vec_size; 71 int i; 72 ifstream ref_file("mach_cd.txt"); 73 74 if(ref_file.is_open()) 75 { 76 ref_file >> gun_phys.Mach_fall_off; 77 ref_file >> gun_phys.ref_Mach_1; 78 ref_file >> gun_phys.temp_at_SL; 79 ref_file >> gun_phys.temp_lapse; 80 ref_file >> gun_phys.gravity_accel; 81 ref_file >> gun_phys.cd_table_size; 82 83 gun_phys.ref_Cd_table.resize(gun_phys.cd_table_size); 84 gun_phys.ref_Mach_table.resize(gun_phys.cd_table_size); 85 86 for(i = 0; i < (int)gun_phys.cd_table_size; i++) 87 { 88 if(!ref_file.eof()) 89 { 90 ref_file >> gun_phys.ref_Mach_table.at(i); 91 ref_file >> gun_phys.ref_Cd_table.at(i); 92 } 93 else 94 { 95 ref_file.close(); 96 return false; 97 } 98 } 99 } 100 else 101 return false; 102 103 ref_file.close(); 104 return true; 105} 106 107//**************************************************** 108 109void reset_gun_params(/*double gun_ang, double muz_vel*//*, double muz_alt,*/ int &zf_i) 110{ 111 zf_i = 0; 112 //gun_phys.shell_FP_angle = gun_ang; 113 //gun_phys.shell_velocity = muz_vel; 114 gun_phys.shell_FP_angle = gun_phys.gun_angle; 115 gun_phys.shell_velocity = gun_phys.muzzle_velocity; 116 gun_phys.shell_FP_degrees = (gun_phys.shell_FP_angle / M_PI) * 180.0; 117 gun_phys.init_muzzle_velocity_components(); 118 //gun_phys.shell_y_pos = muz_alt; 119 gun_phys.shell_y_pos = gun_phys.gun_deck_altitude; 120 gun_phys.shell_x_pos = 0; 121 gun_phys.flight_time_100_s = 0; 122 123} 124 125//**************************************************** 126 127void update_shell_trajectory(double BPS_Tm) 128{ 129 gun_phys.rho_at_alt = gun_phys.Rho_Calculator(); 130 gun_phys.shell_Mach = gun_phys.Mach_Calculator(); 131 gun_phys.Cd = gun_phys.Cd_Calculator(); 132 gun_phys.shell_velocity = gun_phys.New_Velocity_Calculator(BPS_Tm); 133 gun_phys.update_shell_position(BPS_Tm); 134 gun_phys.shell_FP_degrees = (gun_phys.shell_FP_angle / M_PI) * 180; 135 gun_phys.flight_time_100_s++; 136} 137//**************************************************** 138void shell_position_recording(vector<record_position> *posit, double BPS_Tm) 139{ 140 record_position temp_position = {0}; 141 temp_position.flight_time = (double)gun_phys.flight_time_100_s / BPS_Tm;//BPS; 142 temp_position.velocity = gun_phys.shell_velocity; 143 temp_position.angle = gun_phys.shell_FP_degrees; 144 temp_position.x_pos = gun_phys.shell_x_pos; 145 temp_position.y_pos = gun_phys.shell_y_pos; 146 temp_position.mach_no = gun_phys.shell_Mach; 147 temp_position.Cd = gun_phys.Cd; 148 temp_position.rho = gun_phys.rho_at_alt; 149 posit->push_back(temp_position); 150} 151//**************************************************** 152 153void init_phase_0_buttons(button_position *buts) 154// Be careful here. the number of buttons on phase 0 is currently 12 155// This one might be a case for a dynamic array or a vector, in future. 156{ 157 float posit_xy[12][2] = {{15,370}, {15,410}, {15,460}, {15,500}, 158 {280,370}, {280,410}, {280,460}, {280,500}, 159 {545,370}, {545,410}, {545,460}, {545,500}}; 160 161 int RGB[12][3] = {{0,255,100}, {0,255,100}, {0,255,100}, {0,255,100}, 162 {0,255,100}, {0,255,100}, {0,255,100}, {0,255,100}, 163 {0,255,100}, {0,255,100}, {170,205,0}, {255,0,0}}; 164 165 int i; 166 for(i = 0; i < 12; i++) 167 { 168 buts[i].x1 = posit_xy[i][0]; 169 buts[i].x2 = posit_xy[i][0] + 30; 170 buts[i].y1 = posit_xy[i][1]; 171 buts[i].y2 = posit_xy[i][1] + 30; 172 buts[i].rgb[0] = RGB[i][0]; 173 buts[i].rgb[1] = RGB[i][1]; 174 buts[i].rgb[2] = RGB[i][2]; 175 } 176 buts[0].label.assign("Elevate Gun"); 177 buts[1].label.assign("Depress Gun"); 178 buts[2].label.assign("Increase Velocity"); 179 buts[3].label.assign("Decrease Velocity"); 180 buts[4].label.assign("Increase Mass"); 181 buts[5].label.assign("Decrease Mass"); 182 buts[6].label.assign("Increase Bore"); 183 buts[7].label.assign("Decrease Bore"); 184 buts[8].label.assign("Raise Turret"); 185 buts[9].label.assign("Lower Turret"); 186 buts[10].label.assign("Toggle Units"); 187 buts[11].label.assign("FIRE!"); 188} 189 190//**************************************************** 191 192void init_phase_2_buttons(button_position *buts) 193// Be careful here. the number of buttons on phase 0 is currently 3 194// This one might be a case for a dynamic array or a vector, in future. 195{ 196 float posit_xy[3][2] = {{45,500}, {45,540}, {460,540}}; 197 198 int RGB[3][3] = {{0,255,100}, {0,255,100}, {170,205,0}}; 199 200 int i; 201 for(i = 0; i < 3; i++) 202 { 203 buts[i].x1 = posit_xy[i][0]; 204 buts[i].x2 = posit_xy[i][0] + 30; 205 buts[i].y1 = posit_xy[i][1]; 206 buts[i].y2 = posit_xy[i][1] + 30; 207 buts[i].rgb[0] = RGB[i][0]; 208 buts[i].rgb[1] = RGB[i][1]; 209 buts[i].rgb[2] = RGB[i][2]; 210 } 211 buts[0].label.assign("Next Segment"); 212 buts[1].label.assign("Previous Segment"); 213 buts[2].label.assign("End Analysis (Restart)"); 214 215} 216 217//**************************************************** 218 219int mouse_handler(int x, int y, button_position *buts, int length) 220{ 221 int i; 222 bool pressed = false; 223 for(i = 0; i < length; i++) 224 { 225 if(x > buts[i].x1 && x < buts[i].x2 && y > buts[i].y1 && y < buts[i].y2) 226 { 227 pressed = true; 228 break; 229 } 230 } 231 232 if(pressed == true) 233 return i; 234 else 235 return -1; 236} 237//**************************************************** 238 239 240void phase_0_action_handler(int id, bool go, int &faze, bool &moused, bool &imp_bool/*, double &gun_ang*//*, double & muz_alt*/) 241{ 242 switch(id) 243 { 244 case 0: 245 if(go == true) // Only if the timer geow is fed through... 246 { 247 248 gun_phys.shell_FP_angle += 0.001745; 249 if(gun_phys.shell_FP_angle > gun_phys.elevation_limits[1]) 250 gun_phys.shell_FP_angle = gun_phys.elevation_limits[1]; 251 252 gun_phys.gun_angle = gun_phys.shell_FP_angle; 253 gun_phys.shell_FP_degrees = (gun_phys.shell_FP_angle / M_PI) * 180; 254 gun_phys.init_muzzle_velocity_components(); 255 256 257 //gun_ang = gun_phys.shell_FP_angle; 258 259 //gun_phys.shell_y_pos = gun_phys.init_muzzle_position(); 260 //muz_alt = gun_phys.shell_y_pos; 261 // mouse_down = false; a way to stop the repeat function of mouse press 262 //button_id = -1; 263 } 264 265 break; 266 267 case 1: 268 if(go == true) // Only if the timer geow is fed through... 269 { 270 gun_phys.shell_FP_angle -= 0.001745; 271 if(gun_phys.shell_FP_angle < gun_phys.elevation_limits[0]) 272 gun_phys.shell_FP_angle = gun_phys.elevation_limits[0]; 273 274 gun_phys.gun_angle = gun_phys.shell_FP_angle; 275 gun_phys.shell_FP_degrees = (gun_phys.shell_FP_angle / M_PI) * 180; 276 gun_phys.init_muzzle_velocity_components(); 277 //gun_ang = gun_phys.shell_FP_angle; 278 279 //gun_phys.shell_y_pos = gun_phys.init_muzzle_position(); 280 //muz_alt = gun_phys.shell_y_pos; 281 } 282 break; 283 284 case 2: 285 if(go == true) // Only if the timer geow is fed through... 286 { 287 gun_phys.shell_velocity += 0.2; 288 if(gun_phys.shell_velocity > gun_phys.velocity_limits[1]) 289 gun_phys.shell_velocity = gun_phys.velocity_limits[1]; 290 291 gun_phys.muzzle_velocity = gun_phys.shell_velocity; 292 gun_phys.init_muzzle_velocity_components(); 293 } 294 break; 295 296 case 3: 297 if(go == true) // Only if the timer geow is fed through... 298 { 299 gun_phys.shell_velocity -= 0.2; 300 if(gun_phys.shell_velocity < gun_phys.velocity_limits[0]) 301 gun_phys.shell_velocity = gun_phys.velocity_limits[0]; 302 303 gun_phys.muzzle_velocity = gun_phys.shell_velocity; 304 gun_phys.init_muzzle_velocity_components(); 305 } 306 break; 307 308 case 4: 309 if(go == true) // Only if the timer geow is fed through... 310 { 311 gun_phys.shell_mass += 0.5; 312 if(gun_phys.shell_mass > gun_phys.mass_limits[1]) 313 gun_phys.shell_mass = gun_phys.mass_limits[1]; 314 } 315 break; 316 317 case 5: 318 if(go == true) // Only if the timer geow is fed through... 319 { 320 gun_phys.shell_mass -= 0.5; 321 if(gun_phys.shell_mass < gun_phys.mass_limits[0]) 322 gun_phys.shell_mass = gun_phys.mass_limits[0]; 323 } 324 break; 325 326 case 6: // increase the shell diameter 327 if(go == true) // Only if the timer geow is fed through... 328 { 329 gun_phys.shell_diameter += 0.1; 330 if(gun_phys.shell_diameter > gun_phys.bore_limits[1]) 331 gun_phys.shell_diameter = gun_phys.bore_limits[1]; 332 } 333 break; 334 335 case 7: // decrease shell diameter 336 if(go == true) // Only if the timer geow is fed through... 337 { 338 gun_phys.shell_diameter -= 0.1; 339 if(gun_phys.shell_diameter < gun_phys.bore_limits[0]) 340 gun_phys.shell_diameter = gun_phys.bore_limits[0]; 341 } 342 break; 343 344 case 8: // Raise the turret 345 if(go == true) // Only if the timer geow is fed through... 346 { 347 gun_phys.gun_deck_altitude += 0.1; 348 if(gun_phys.gun_deck_altitude > gun_phys.deck_limits[1]) 349 gun_phys.gun_deck_altitude = gun_phys.deck_limits[1]; 350 351 gun_phys.shell_y_pos = gun_phys.gun_deck_altitude; 352 } 353 break; 354 355 case 9: // Lower the turret 356 if(go == true) // Only if the timer geow is fed through... 357 { 358 gun_phys.gun_deck_altitude -= 0.1; 359 if(gun_phys.gun_deck_altitude < gun_phys.deck_limits[0]) 360 gun_phys.gun_deck_altitude = gun_phys.deck_limits[0]; 361 362 gun_phys.shell_y_pos = gun_phys.gun_deck_altitude; 363 } 364 break; 365 366 case 10: // Change between imperial and SI units 367 if(go == true) // Only if the timer geow is fed through... 368 { 369 if(imp_bool == true) 370 imp_bool = false; 371 else 372 imp_bool = true; 373 374 moused = false; // a way to stop the repeat function of mouse press 375 376 } 377 break; 378 379 case 11: // Fire the gun 380 if(go == true) // Only if the timer geow is fed through... 381 { 382 //gun_phys.shell_y_pos = gun_phys.init_muzzle_position(); 383 //muz_alt = gun_phys.shell_y_pos; 384 385 faze = 1; 386 moused = false; 387 } 388 break; 389 390 default: 391 break; 392 } 393} 394 395// *************************************************** 396 397void phase_2_action_handler(int id, bool go, int &faze, bool &moused, vector<record_position> &posit, size_t v_sz, size_t &sel_seg/*, double gun_ang, double muz_vel*//*, double muz_alt*/, int &zf_i) 398{ 399 switch(id) 400 { 401 case 0: 402 if(go == true) // Only if the timer geow is fed through... 403 { 404 if(sel_seg < (v_sz - 1)) 405 { 406 sel_seg++; 407 } 408 moused = false; 409 } 410 break; 411 case 1: 412 if(go == true) // Only if the timer geow is fed through... 413 { 414 if(sel_seg > 1) 415 { 416 sel_seg--; 417 } 418 moused = false; 419 } 420 break; 421 case 2: 422 if(go == true) // Only if the timer geow is fed through... 423 { 424 posit.resize(0); 425 posit.clear(); 426 reset_gun_params(/*gun_ang,*/ /*muz_vel*//*, muz_alt,*/ zf_i); 427 sel_seg = 1; 428 faze = 0; 429 // RESET THINGS NOW... 430 moused = false; 431 } 432 break; 433 434 435 default: 436 break; 437 438 } 439} 440// *************************************************** 441 442void draw_phase_0_screen(char *buff, int buffsize, ALLEGRO_FONT *F_font, button_position *buts_p0, ALLEGRO_BITMAP *bmp, bool imp_bool) 443{ 444 445 al_draw_bitmap(bmp, 0, 300, 0); 446 int i; 447 for(i = 0; i < 12; i++) 448 { 449 al_draw_rectangle(buts_p0[i].x1, buts_p0[i].y1, buts_p0[i].x2, buts_p0[i].y2, 450 al_map_rgb(buts_p0[i].rgb[0],buts_p0[i].rgb[1],buts_p0[i].rgb[2]), 2); 451 al_draw_text(F_font, al_map_rgb(0,255,100), buts_p0[i].x2 + 5, buts_p0[i].y1 + 5, ALLEGRO_ALIGN_LEFT, buts_p0[i].label.c_str()); 452 } 453 al_draw_line(0,300,800,300, al_map_rgb(255,255,255),0); 454 455 al_draw_rectangle(5,5,398,295, al_map_rgb(0,220,175), 0); 456 al_draw_rectangle(403,5,795,295, al_map_rgb(0,220,175), 0); 457 458 snprintf(buff, buffsize, "GUN PARAMETERS %s", 459 (imp_bool == true ? "(Imperial Units)" : "(SI Units)")); 460 al_draw_text(F_font, al_map_rgb(220,240,0), 409, 10, ALLEGRO_ALIGN_LEFT, buff); 461 462 //------------------------------- 463 // GUN ELEVATION 464 snprintf(buff, buffsize, "Gun Elevation (deg):" ); 465 al_draw_text(F_font, al_map_rgb(0,255,150), 409, 35, ALLEGRO_ALIGN_LEFT, buff); 466 467 snprintf(buff, buffsize, "%0.1f", gun_phys.shell_FP_degrees ); 468 al_draw_text(F_font, al_map_rgb(0,255,150), 685, 35, ALLEGRO_ALIGN_LEFT, buff); 469 470 //------------------------------- 471 // MUZZLE VELOCITY 472 snprintf(buff, buffsize, "Muzzle Velocity %s", 473 (imp_bool == true ? "(ft/s):" : "(m/s):") ); 474 al_draw_text(F_font, al_map_rgb(0,255,150), 409, 53, ALLEGRO_ALIGN_LEFT, buff); 475 476 snprintf(buff, buffsize, "%0.1f", 477 (imp_bool == true ? (gun_phys.shell_velocity * 3.281) : gun_phys.shell_velocity) ); 478 al_draw_text(F_font, al_map_rgb(0,255,150), 685, 53, ALLEGRO_ALIGN_LEFT, buff); 479 480 //------------------------------- 481 // SHELL MASS 482 snprintf(buff, buffsize, "Shell Mass %s", 483 (imp_bool == true ? "(lb):" : "(kg):") ); 484 485 al_draw_text(F_font, al_map_rgb(0,255,150), 409, 71, ALLEGRO_ALIGN_LEFT, buff); 486 487 snprintf(buff, buffsize, "%0.1f", 488 (imp_bool == true ? (gun_phys.shell_mass * 2.2046) : gun_phys.shell_mass) ); 489 490 al_draw_text(F_font, al_map_rgb(0,255,150), 685, 71, ALLEGRO_ALIGN_LEFT, buff); 491 492 //------------------------------- 493 // GUN BORE 494 snprintf(buff, buffsize, "Gun Bore %s", 495 (imp_bool == true ? "(in):" : "(cm):") ); 496 497 al_draw_text(F_font, al_map_rgb(0,255,150), 409, 89, ALLEGRO_ALIGN_LEFT, buff); 498 499 snprintf(buff, buffsize, "%0.1f", 500 (imp_bool == true ? (gun_phys.shell_diameter * 0.3937) : gun_phys.shell_diameter) ); 501 502 al_draw_text(F_font, al_map_rgb(0,255,150), 685, 89, ALLEGRO_ALIGN_LEFT, buff); 503 504 //------------------------------- 505 // GUN BORE 506 snprintf(buff, buffsize, "Turret Height %s", 507 (imp_bool == true ? "(ft):" : "(m):") ); 508 509 al_draw_text(F_font, al_map_rgb(0,255,150), 409, 107, ALLEGRO_ALIGN_LEFT, buff); 510 511 snprintf(buff, buffsize, "%0.1f", 512 (imp_bool == true ? (gun_phys.gun_deck_altitude * 3.281) : gun_phys.gun_deck_altitude) ); 513 514 al_draw_text(F_font, al_map_rgb(0,255,150), 685, 107, ALLEGRO_ALIGN_LEFT, buff); 515 516 //------------------------------- 517 snprintf(buff, buffsize, "ATMOSPHERIC PARAMETERS" ); 518 al_draw_text(F_font, al_map_rgb(220,240,0), 409, 143, ALLEGRO_ALIGN_LEFT, buff); 519 snprintf(buff, buffsize, "currently not editable" ); 520 al_draw_text(F_font, al_map_rgb(220,240,0), 409, 161, ALLEGRO_ALIGN_LEFT, buff); 521 522 snprintf(buff, buffsize, "S/L Temperature (C):" ); 523 al_draw_text(F_font, al_map_rgb(0,255,150), 409, 186, ALLEGRO_ALIGN_LEFT, buff); 524 525 snprintf(buff, buffsize, "%0.1f", gun_phys.temp_at_SL ); 526 al_draw_text(F_font, al_map_rgb(0,255,150), 685, 186, ALLEGRO_ALIGN_LEFT, buff); 527 528 snprintf(buff, buffsize, "S/L Air Density (kg/m3):" ); 529 al_draw_text(F_font, al_map_rgb(0,255,150), 409, 204, ALLEGRO_ALIGN_LEFT, buff); 530 531 snprintf(buff, buffsize, "%0.3f", gun_phys.rho_at_SL ); 532 al_draw_text(F_font, al_map_rgb(0,255,150), 685, 204, ALLEGRO_ALIGN_LEFT, buff); 533 534 //Draw the turret 535 float barrel_tip_x = cos(gun_phys.shell_FP_angle) * 80.0; 536 float barrel_tip_y = sin(gun_phys.shell_FP_angle) * 80.0; 537 float x_offset = 210.0; 538 float y_offset = 255.0; 539 540 al_draw_line(x_offset, 541 y_offset - (gun_phys.gun_deck_altitude * 5), 542 (x_offset + barrel_tip_x), 543 (y_offset - barrel_tip_y) - (gun_phys.gun_deck_altitude * 5), 544 al_map_rgb(0,255,150), 2); 545 546 al_draw_filled_rectangle(145, 547 235 - (gun_phys.gun_deck_altitude * 5), 548 230, 549 270 - (gun_phys.gun_deck_altitude * 5), 550 al_map_rgb(0,0,0)); 551 552 al_draw_rectangle(145, 553 235 - (gun_phys.gun_deck_altitude * 5), 554 230, 555 270 - (gun_phys.gun_deck_altitude * 5), 556 al_map_rgb(0,255,150), 0); 557 558 al_draw_line(5,270,398,270, al_map_rgb(0,255,150),0); 559 560 al_draw_rectangle(155, 561 270 - (gun_phys.gun_deck_altitude * 5), 562 220, 563 270, 564 al_map_rgb(0,255,150), 0); 565 566 567} 568//**************************************************** 569 570void draw_phase_1_screen(char *buff, int buffsize, ALLEGRO_FONT *F_font, vector<record_position> *posit, size_t v_sz, ALLEGRO_BITMAP *bmp, bool imp_bool, double zm_f, int &zf_i, bool trace) 571{ 572 al_draw_bitmap(bmp, 0, 460, 0); 573 //TITLE 574 snprintf(buff, buffsize, "SHELL PROGRESS (Recording at 1 second interval)" ); 575 576 al_draw_text(F_font, al_map_rgb(220,240,0), 400, 12, ALLEGRO_ALIGN_CENTRE, buff); 577 578 // DISTANCE 579 snprintf(buff, buffsize, "Distance %s", 580 (imp_bool == true ? "(yd):" : "(m):") ); 581 582 al_draw_text(F_font, al_map_rgb(0,255,200), 30, 35, ALLEGRO_ALIGN_LEFT, buff); 583 584 snprintf(buff, buffsize, "%0.0f", 585 (imp_bool == true ? (gun_phys.shell_x_pos * 1.093) : gun_phys.shell_x_pos) ); 586 587 al_draw_text(F_font, al_map_rgb(0,255,200), 275, 35, ALLEGRO_ALIGN_LEFT, buff); 588 589 // ALTITUDE 590 snprintf(buff, buffsize, "Altitude %s", 591 (imp_bool == true ? "(yd):" : "(m):") ); 592 593 al_draw_text(F_font, al_map_rgb(0,255,200), 30, 53, ALLEGRO_ALIGN_LEFT, buff); 594 595 snprintf(buff, buffsize, "%0.0f", 596 (imp_bool == true ? (gun_phys.shell_y_pos * 1.093) : gun_phys.shell_y_pos) ); 597 598 al_draw_text(F_font, al_map_rgb(0,255,200), 275, 53, ALLEGRO_ALIGN_LEFT, buff); 599 600 // SHELL ANGLE 601 snprintf(buff, buffsize, "Path Angle (deg):" ); 602 al_draw_text(F_font, al_map_rgb(0,255,200), 30, 71, ALLEGRO_ALIGN_LEFT, buff); 603 604 snprintf(buff, buffsize, "%0.1f", gun_phys.shell_FP_degrees ); 605 al_draw_text(F_font, al_map_rgb(0,255,200), 275, 71, ALLEGRO_ALIGN_LEFT, buff); 606 607 // TIME 608 snprintf(buff, buffsize, "Flight Time (sec):" ); 609 al_draw_text(F_font, al_map_rgb(0,255,200), 30, 89, ALLEGRO_ALIGN_LEFT, buff); 610 611 snprintf(buff, buffsize, "%0.1f", ((double)gun_phys.flight_time_100_s / 100.0) ); 612 al_draw_text(F_font, al_map_rgb(0,255,200), 275, 89, ALLEGRO_ALIGN_LEFT, buff); 613 614 // VELOCITY 615 snprintf(buff, buffsize, "Velocity %s", 616 (imp_bool == true ? "(ft/s):" : "(m/s):") ); 617 al_draw_text(F_font, al_map_rgb(0,255,200), 430, 35, ALLEGRO_ALIGN_LEFT, buff); 618 619 snprintf(buff, buffsize, "%0.0f", 620 imp_bool == true ? (gun_phys.shell_velocity * 3.281) : gun_phys.shell_velocity ); 621 al_draw_text(F_font, al_map_rgb(0,255,200), 685, 35, ALLEGRO_ALIGN_LEFT, buff); 622 623 // MACH 624 snprintf(buff, buffsize, "Mach:" ); 625 al_draw_text(F_font, al_map_rgb(0,255,200), 430, 53, ALLEGRO_ALIGN_LEFT, buff); 626 627 snprintf(buff, buffsize, "%0.2f", gun_phys.shell_Mach ); 628 al_draw_text(F_font, al_map_rgb(0,255,200), 685, 53, ALLEGRO_ALIGN_LEFT, buff); 629 630 // Rho 631 snprintf(buff, buffsize, "Air Density (kg/m3):" ); 632 al_draw_text(F_font, al_map_rgb(0,255,200), 430, 71, ALLEGRO_ALIGN_LEFT, buff); 633 634 snprintf(buff, buffsize, "%0.3f", gun_phys.rho_at_alt ); 635 al_draw_text(F_font, al_map_rgb(0,255,200), 685, 71, ALLEGRO_ALIGN_LEFT, buff); 636 637 // temp_at_SL - (shell_y_pos / temp_lapse) 638 639 // TEMP 640 snprintf(buff, buffsize, "Air Temp (C):" ); 641 al_draw_text(F_font, al_map_rgb(0,255,200), 430, 89, ALLEGRO_ALIGN_LEFT, buff); 642 643 //temporary measure; 644 double air_temp = gun_phys.temp_at_SL - (gun_phys.shell_y_pos / gun_phys.temp_lapse); 645 if(air_temp < -56.5) 646 air_temp = -56.5; 647 648 snprintf(buff, buffsize, "%0.1f", air_temp ); 649 al_draw_text(F_font, al_map_rgb(0,255,200), 685, 89, ALLEGRO_ALIGN_LEFT, buff); 650 651 // DRAW THE SHELL 652 float screen_X_pos = 0.0; 653 float screen_Y_pos = 0.0; 654 screen_X_pos = float(2 + (gun_phys.shell_x_pos / zm_f)); 655 screen_Y_pos = float(SCREEN_1_Y_LOWER_LIMIT - (gun_phys.shell_y_pos / zm_f)); 656 657 if(screen_X_pos > 0 && screen_X_pos < 800 && screen_Y_pos < (SCREEN_1_Y_LOWER_LIMIT + 1) && screen_Y_pos > SCREEN_1_Y_UPPER_LIMIT) 658 al_draw_circle(screen_X_pos, screen_Y_pos, 2, al_map_rgb(0,250,0), 0); 659 660 661 al_draw_line(0.0, (SCREEN_1_Y_UPPER_LIMIT - 2.0), 800.0, (SCREEN_1_Y_UPPER_LIMIT - 2.0), al_map_rgb(200,200,200), 0); 662 al_draw_line(0.0, SCREEN_1_Y_LOWER_LIMIT, 800.0, SCREEN_1_Y_LOWER_LIMIT, al_map_rgb(0,100,200), 0); 663 al_draw_line(0.0, (SCREEN_1_Y_LOWER_LIMIT + 30), 800.0, (SCREEN_1_Y_LOWER_LIMIT + 30), al_map_rgb(200,200,200), 0); 664 665 snprintf(buff, buffsize, "Display Width Scale: %0.1f %s", 666 (imp_bool == true ? ((zm_f * 800.0) / 1000.0) * 1.093 : (zm_f * 800.0) / 1000.0), 667 (imp_bool == true ? "kyd" : "km")); 668 al_draw_text(F_font, al_map_rgb(220,240,0), 400, (SCREEN_1_Y_LOWER_LIMIT + 5.0), ALLEGRO_ALIGN_CENTRE, buff); 669 670 // DRAW THE TRACING OF SHELL PATH, IF ENABLED 671 672 673 if(trace == true && v_sz > 1) 674 { 675 size_t s; 676 float screen_X_pos_0 = 0.0; 677 float screen_Y_pos_0 = 0.0; 678 float screen_X_pos_1 = 0.0; 679 float screen_Y_pos_1 = 0.0; 680 681 for(s = 0; s < v_sz; s++) 682 { 683 if(s > 0) 684 { 685 screen_X_pos_0 = float(2 + (posit->at(s-1).x_pos / zm_f)); 686 screen_Y_pos_0 = float(SCREEN_1_Y_LOWER_LIMIT - (posit->at(s-1).y_pos / zm_f)); 687 screen_X_pos_1 = float(2 + (posit->at(s).x_pos / zm_f)); 688 screen_Y_pos_1 = float(SCREEN_1_Y_LOWER_LIMIT - (posit->at(s).y_pos / zm_f)); 689 // Thanks to Salem on cboard forums. 690 691 if(screen_X_pos_1 > 0 && screen_X_pos_1 < 800 && screen_Y_pos_1 < (SCREEN_1_Y_LOWER_LIMIT + 1) && screen_Y_pos_1 > SCREEN_1_Y_UPPER_LIMIT) 692 al_draw_line(screen_X_pos_0, screen_Y_pos_0, screen_X_pos_1, screen_Y_pos_1, al_map_rgb(80,120,120), 0); 693 } 694 } 695 } 696 697 // RESET the Zoom_Factor if the shell is going off the screen 698 if(screen_X_pos > 800 || screen_Y_pos < SCREEN_1_Y_UPPER_LIMIT) 699 if(zf_i < (ZF_SIZE - 1)) 700 zf_i++; 701} 702//**************************************************** 703 704void draw_phase_2_screen(char *buff, int buffsize, ALLEGRO_FONT *F_font, vector<record_position> *posit, size_t v_sz, ALLEGRO_BITMAP *bmp, button_position *buts_p2, bool imp_bool, double zm_f, size_t sel_seg) 705{ 706 al_draw_bitmap(bmp, 0, 460, 0); 707 int i; 708 for(i = 0; i < 3; i++) 709 { 710 al_draw_rectangle(buts_p2[i].x1, buts_p2[i].y1, buts_p2[i].x2, buts_p2[i].y2, 711 al_map_rgb(buts_p2[i].rgb[0],buts_p2[i].rgb[1],buts_p2[i].rgb[2]), 2); 712 al_draw_text(F_font, al_map_rgb(0,255,100), buts_p2[i].x2 + 5, buts_p2[i].y1 + 5, ALLEGRO_ALIGN_LEFT, buts_p2[i].label.c_str()); 713 } 714 715 al_draw_rectangle(5.0, 5.0, 795.0, 120.0, al_map_rgb(0,220,175), 0); 716 al_draw_line(0.0, (SCREEN_1_Y_UPPER_LIMIT - 2.0), 800.0, (SCREEN_1_Y_UPPER_LIMIT - 2.0), al_map_rgb(200,200,200), 0); 717 al_draw_line(0.0, SCREEN_1_Y_LOWER_LIMIT, 800.0, SCREEN_1_Y_LOWER_LIMIT, al_map_rgb(0,100,200), 0); 718 al_draw_line(0.0, (SCREEN_1_Y_LOWER_LIMIT + 30), 800.0, (SCREEN_1_Y_LOWER_LIMIT + 30), al_map_rgb(200,200,200), 0); 719 720 snprintf(buff, buffsize, "Analysing Segment:" ); 721 al_draw_text(F_font, al_map_rgb(220,240,0), 15, 10, ALLEGRO_ALIGN_LEFT, buff); 722 723 snprintf(buff, buffsize, "%i", (int)sel_seg ); 724 al_draw_text(F_font, al_map_rgb(220,240,0), 350, 10, ALLEGRO_ALIGN_LEFT, buff); 725 726 snprintf(buff, buffsize, "Distance horiz to point %s", 727 (imp_bool == true ? "(yd):" : "(m):") ); 728 al_draw_text(F_font, al_map_rgb(0,255,200), 15, 28, ALLEGRO_ALIGN_LEFT, buff); 729 730 snprintf(buff, buffsize, "%0.0f", 731 (imp_bool == true ? (posit->at(sel_seg).x_pos * 1.093) : posit->at(sel_seg).x_pos) ); 732 al_draw_text(F_font, al_map_rgb(0,255,200), 350, 28, ALLEGRO_ALIGN_LEFT, buff); 733 734 snprintf(buff, buffsize, "Segment horiz distance %s", 735 (imp_bool == true ? "(yd):" : "(m):") );; 736 al_draw_text(F_font, al_map_rgb(0,255,200), 15, 46, ALLEGRO_ALIGN_LEFT, buff); 737 738 snprintf(buff, buffsize, "%0.0f", 739 (imp_bool == true ? (posit->at(sel_seg).x_pos - posit->at(sel_seg - 1).x_pos) * 1.093 : (posit->at(sel_seg).x_pos - posit->at(sel_seg - 1).x_pos)) ); 740 al_draw_text(F_font, al_map_rgb(0,255,200), 350, 46, ALLEGRO_ALIGN_LEFT, buff); 741 742 snprintf(buff, buffsize, "Altitude at point %s", 743 (imp_bool == true ? "(yd):" : "(m):") ); 744 al_draw_text(F_font, al_map_rgb(0,255,200), 15, 64, ALLEGRO_ALIGN_LEFT, buff); 745 746 snprintf(buff, buffsize, "%0.0f", 747 (imp_bool == true ? (posit->at(sel_seg).y_pos * 1.093) : posit->at(sel_seg).y_pos) ); 748 al_draw_text(F_font, al_map_rgb(0,255,200), 350, 64, ALLEGRO_ALIGN_LEFT, buff); 749 750 snprintf(buff, buffsize, "Avg Shell Velocity %s", 751 (imp_bool == true ? "(ft/s):" : "(m/s):") ); 752 al_draw_text(F_font, al_map_rgb(0,255,200), 15, 82, ALLEGRO_ALIGN_LEFT, buff); 753 754 snprintf(buff, buffsize, "%0.0f", 755 (imp_bool == true ? ((posit->at(sel_seg).velocity + posit->at(sel_seg - 1).velocity) / 2.0) * 3.281 : 756 (posit->at(sel_seg).velocity + posit->at(sel_seg - 1).velocity) / 2.0) ); 757 al_draw_text(F_font, al_map_rgb(0,255,200), 350, 82, ALLEGRO_ALIGN_LEFT, buff); 758 759 //************************************************ 760 snprintf(buff, buffsize, "Display Width Scale: %0.1f %s", 761 (imp_bool == true ? ((zm_f * 800.0) / 1000.0) * 1.093 : (zm_f * 800.0) / 1000.0), 762 (imp_bool == true ? "kyd" : "km") ); 763 al_draw_text(F_font, al_map_rgb(220,240,0), 400, (SCREEN_1_Y_LOWER_LIMIT + 5.0), ALLEGRO_ALIGN_CENTRE, buff); 764 765 snprintf(buff, buffsize, "ANANLYSE SEGMENTS (1 second intervals)" ); 766 al_draw_text(F_font, al_map_rgb(220,240,0), 400, (SCREEN_1_Y_LOWER_LIMIT + 37.0), ALLEGRO_ALIGN_CENTRE, buff); 767 768 /* 769 struct record_position 770{ 771 double x_pos; 772 double y_pos; 773 double velocity; 774 double angle; 775 double flight_time; 776 double mach_no; 777 double rho; 778 double Cd; 779}; 780*/ 781 size_t s; 782 float screen_X_pos_0 = 0.0; 783 float screen_Y_pos_0 = 0.0; 784 float screen_X_pos_1 = 0.0; 785 float screen_Y_pos_1 = 0.0; 786 787 for(s = 0; s < v_sz; s++) 788 { 789 if(s > 0) 790 { 791 screen_X_pos_0 = float(2 + (posit->at(s-1).x_pos / zm_f)); 792 screen_Y_pos_0 = float(SCREEN_1_Y_LOWER_LIMIT - (posit->at(s-1).y_pos / zm_f)); 793 screen_X_pos_1 = float(2 + (posit->at(s).x_pos / zm_f)); 794 screen_Y_pos_1 = float(SCREEN_1_Y_LOWER_LIMIT - (posit->at(s).y_pos / zm_f)); 795 // Thanks to Salem on cboard forums. 796 797 if(screen_X_pos_1 > 0 && screen_X_pos_1 < 800 && screen_Y_pos_1 < (SCREEN_1_Y_LOWER_LIMIT + 1) && screen_Y_pos_1 > SCREEN_1_Y_UPPER_LIMIT) 798 { 799 if(s == sel_seg) 800 { 801 al_draw_line(screen_X_pos_0, screen_Y_pos_0, screen_X_pos_1, screen_Y_pos_1, al_map_rgb(255,0,0), 0); 802 } 803 else 804 { 805 al_draw_line(screen_X_pos_0, screen_Y_pos_0, screen_X_pos_1, screen_Y_pos_1, al_map_rgb(80,120,120), 0); 806 } 807 } 808 } 809 } 810 811 812} 813 814 815//**************************************************** 816int main() 817{ 818 ALLEGRO_FONT *font = NULL; 819 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 820 ALLEGRO_TIMER *FPS_timer = NULL; 821 ALLEGRO_TIMER *BPS_timer = NULL; 822 ALLEGRO_DISPLAY *display = NULL; 823 824 ALLEGRO_BITMAP *bmp_1; 825 ALLEGRO_BITMAP *bmp_2; 826 827 828 bool mouse_down = false; 829 int mx = 0; 830 int my = 0; 831 size_t selected_segment = 1; 832 833 const float BPS = 100; 834 const float FPS = 60; 835 bool geow = false; // Special variable to feed the timer to mouse press repeat functions 836 837 bool imperial = true; 838 //double gun_angle; 839 //double muzzle_vel; 840 //double muzzle_alt; 841 842 double zoom_factor[ZF_SIZE] = {6.25, 12.5, 25.0, 37.5, 50.0, 62.5, 75.0, 88.125}; 843 // BE CAREFUL HERE... 844 // The elements MUST always be the same number of as the ZF_SIZE global. 845 int zf_index = 0; 846 // double zoom_factor = 25.0; // vestigial 847 double trace_on = true; 848 849 char buffer[100]; 850 bool loop = true; 851 int phase = 0; 852 int button_id = -1; // No button pressed... 853 // The phase variable is used this way; 854 // phase = 0 is the gun configuration screen 855 // phase = 1 is the gun fired and shell in the air screen 856 // phase = 2 will be the after shot screen to analyze the trajectory (TO DO) 857 // phase = 3 will be a shell drag coefficient editing screen (TO DO) 858 // phase = 4 will be the atmospheric parameter editing screen (TO DO) 859 // Exiting phase 2 or aborting phase 1 will return to phase 0. 860 // The gun parameters will be maintained, but the recorded 861 // trajectory of phase 1 will be dumped and the record 862 // vector cleared and resized back to 0 863 864 vector<record_position> positions; 865 //record_position temp_position = {0}; // vestigial 866 button_position phase_0_buttons[12]; // Phase 0 button array 867 init_phase_0_buttons(phase_0_buttons); // Phase 0 button SET ARRAY function 868 button_position phase_2_buttons[3]; 869 init_phase_2_buttons(phase_2_buttons); 870 871 al5_setup(); 872 al5_initialize(&font, &display, &event_queue, &FPS_timer, &BPS_timer, FPS, BPS); 873 // Thanks to Matthew Leverton at allegro.cc forums. 874 875 if(!(bmp_1 = al_load_bitmap("PE.jpg"))) // Just for a laugh! 876 { 877 bmp_1 = al_create_bitmap(800, 300); 878 al_set_target_bitmap(bmp_1); 879 al_clear_to_color(al_map_rgb(35,35,35)); 880 } 881 bmp_2 = al_create_bitmap(800, 140); 882 al_set_target_bitmap(bmp_2); 883 al_clear_to_color(al_map_rgb(35,35,35)); 884 885 // Make sure the correct backbuffer is selected for all future drawing operations. 886 al_set_target_bitmap(al_get_backbuffer(display)); 887 888 889 //Set some gun parameters... 890 gun_phys.shell_FP_angle = (5.0 / 180.0) * M_PI; 891 gun_phys.gun_angle = gun_phys.shell_FP_angle; 892 //gun_angle = gun_phys.shell_FP_angle; 893 gun_phys.shell_FP_degrees = (gun_phys.shell_FP_angle / M_PI) * 180.0; 894 gun_phys.gun_deck_altitude = 9.8; 895 gun_phys.length_of_barrel = 12.15; 896 gun_phys.shell_diameter = 35.6; 897 gun_phys.shell_frontal_area = pow((gun_phys.shell_diameter / 2 / 100), 2) * M_PI; 898 gun_phys.shell_mass = 720.0; 899 gun_phys.shell_velocity = 757.0; 900 gun_phys.muzzle_velocity = gun_phys.shell_velocity; 901 //muzzle_vel = gun_phys.shell_velocity; 902 //gun_phys.shell_y_pos = gun_phys.init_muzzle_position(); 903 //muzzle_alt = gun_phys.shell_y_pos; 904 gun_phys.shell_y_pos = gun_phys.gun_deck_altitude; 905 gun_phys.shell_x_pos = 0; 906 gun_phys.flight_time_100_s = 0; 907 908 gun_phys.init_muzzle_velocity_components(); 909 910 gun_phys.elevation_limits[0] = -0.174533; 911 gun_phys.elevation_limits[1] = 0.907571; 912 gun_phys.bore_limits[0] = 7.5; 913 gun_phys.bore_limits[1] = 61.0; 914 gun_phys.velocity_limits[0] = 400.0; 915 gun_phys.velocity_limits[1] = 1500.0; 916 gun_phys.mass_limits[0] = 5.0; 917 gun_phys.mass_limits[1] = 1550.0; 918 gun_phys.deck_limits[0] = 3.0; 919 gun_phys.deck_limits[1] = 30.5; // A shore battery, perhaps... 920 // Eventually, these parameters could be set from selection of a specific gun type at the beginning 921 // of the program, the data being read from a file containing the specifics for that particular gun. 922 // At the moment the data corresponds to the Hipper Class German heavy cruiser. (except for the limits) 923 // 924 925 // Load txt tables... 926 if(false == set_rho_values()) 927 return -1; 928 if(false == set_Mach_Cd_values()) 929 return -1; 930 931 al5_register(&event_queue, &display, &FPS_timer, &BPS_timer); // Get Allegro up and running. 932 933 //shell_position_recording(&positions, (double) BPS); // put the first position in the alalysis vector 934 935 while (loop) // while MAIN GAME LOOP *** STARTS HERE 936 { 937 ALLEGRO_EVENT ev; 938 al_wait_for_event(event_queue, &ev); 939 940 switch(ev.type) // switch EV.TYPE *** STARTS HERE 941 { 942 case ALLEGRO_EVENT_TIMER: // Catch a Timer event 943 if(ev.timer.source == BPS_timer) // If it is the logic timer... 944 { 945 switch(phase) // switch PHASE for ALLEGRO_EVENT_TIMER BPS *** STARTS HERE 946 { 947 case 0: 948 break; 949 // case 0 *** ENDS: 950 // Unlikely there will ever be anything here, no calculations on this screen 951 952 case 1: 953 if(gun_phys.shell_x_pos == 0) 954 { 955 shell_position_recording(&positions, (double)BPS); 956 } 957 958 if(gun_phys.shell_y_pos > 0) 959 { 960 update_shell_trajectory((double)BPS); 961 if(gun_phys.flight_time_100_s % 100 == 0) // every second do a record action 962 { 963 shell_position_recording(&positions, (double)BPS); 964 } 965 } 966 else if(gun_phys.shell_y_pos < 0) 967 { 968 gun_phys.end_clip((double)BPS); 969 shell_position_recording(&positions, (double)BPS); 970 phase = 2; 971 } 972 973 974 break; 975 // case 1 *** ENDS 976 977 default: 978 break; 979 // case 1 *** ENDS: 980 } // switch PHASE for ALLEGRO_EVENT_TIMER BPS *** ENDS 981 982 } 983 else if(ev.timer.source == FPS_timer) // For screen updates 984 { 985 geow = true; // Feed out the 1/60 Timer. 986 switch(phase) // switch PHASE for ALLEGRO_EVENT_TIMER FPS *** STARTS HERE 987 { 988 case 0: 989 al_clear_to_color(al_map_rgb(0,0,0)); 990 draw_phase_0_screen(buffer, 100, font, phase_0_buttons, bmp_1, imperial); 991 al_flip_display(); 992 993 break; 994 // case 0 *** ENDS 995 996 case 1: 997 al_clear_to_color(al_map_rgb(0,0,0)); 998 999 draw_phase_1_screen(buffer, 100, font, &positions, positions.size(), bmp_2, imperial, zoom_factor[zf_index], zf_index, trace_on); 1000 1001 /*al5_shell_progress_text_output(buffer, 1002 gun_phys.shell_x_pos, 1003 gun_phys.shell_y_pos, 1004 gun_phys.shell_FP_degrees, 1005 gun_phys.flight_time_100_s, 1006 &font); 1007 // vestigial 1008 */ 1009 al_flip_display(); 1010 1011 break; 1012 // case 1 *** ENDS 1013 1014 case 2: // PHASE 2 Shell Flight Analysis 1015 al_clear_to_color(al_map_rgb(0,0,0)); 1016 draw_phase_2_screen(buffer, 100, font, &positions, positions.size(), bmp_2, phase_2_buttons, imperial, zoom_factor[zf_index], selected_segment); 1017 al_flip_display(); 1018 break; 1019 1020 default: 1021 break; 1022 } // switch PHASE for ALLEGRO_EVENT_TIMER FPS *** ENDS 1023 } 1024 break; 1025 // case ALLEGRO_EVENT_TIMER *** ENDS 1026 1027 case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: 1028 switch(phase) 1029 { 1030 case 0: 1031 mouse_down = true; 1032 mx = int(ev.mouse.x); 1033 my = int(ev.mouse.y); 1034 break; 1035 // case PHASE 0 for MB_DOWN *** ENDS 1036 case 2: 1037 mouse_down = true; 1038 mx = int(ev.mouse.x); 1039 my = int(ev.mouse.y); 1040 break; 1041 } 1042 break; 1043 // case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN *** ENDS 1044 1045 case ALLEGRO_EVENT_MOUSE_BUTTON_UP: 1046 mouse_down = false; 1047 break; 1048 // case ALLEGRO_EVENT_MOUSE_BUTTON_UP *** ENDS 1049 1050 //case ALLEGRO_EVENT_MOUSE_AXES: 1051 1052 // break; 1053 1054 case ALLEGRO_EVENT_DISPLAY_CLOSE: 1055 if(al_is_event_queue_empty(event_queue)) 1056 loop = false; 1057 break; 1058 // case ALLEGRO_EVENT_DISPLAY_CLOSE *** ENDS 1059 1060 default: 1061 break; 1062 } // switch EV.TYPE *** ENDS 1063 1064 //------------------------------ 1065 // Other activites after EVENTS *** STARTS HERE 1066 if(mouse_down == true) 1067 { 1068 switch(phase) 1069 { 1070 case 0: 1071 button_id = mouse_handler(mx, my, phase_0_buttons, 12); // 12 Buttons, BE CAREFUL! 1072 phase_0_action_handler(button_id, geow, phase, mouse_down, imperial/*, gun_angle*//*, muzzle_alt*/); 1073 break; 1074 case 1: 1075 break; 1076 1077 case 2: 1078 button_id = mouse_handler(mx, my, phase_2_buttons, 3); // 3 buttons phase 2 screen 1079 phase_2_action_handler(button_id, geow, phase, mouse_down, positions, positions.size(), selected_segment/*, gun_angle, muzzle_vel*//*, muzzle_alt*/, zf_index); 1080 1081 break; 1082 1083 default: 1084 break; 1085 } 1086 } 1087 if(geow == true) 1088 geow = false; 1089 } // while MAIN GAME LOOP *** ENDS 1090 1091 1092 al_destroy_bitmap(bmp_1); 1093 al_destroy_bitmap(bmp_2); 1094 //al5_terminate(&display, &BPS_timer, &FPS_timer, &event_queue); 1095 1096 al_destroy_display(display); 1097 al_destroy_timer(BPS_timer); 1098 al_destroy_timer(FPS_timer); 1099 al_destroy_event_queue(event_queue); 1100 1101 1102 /* al_shutdown_image_addon(); 1103 al_shutdown_primitives_addon(); 1104 al_shutdown_ttf_addon(); 1105 al_shutdown_font_addon(); 1106 al_uninstall_mouse(); 1107 al_uninstall_system();*/ 1108 1109 /* 1110 ALLEGRO_FONT *font = NULL; 1111 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 1112 ALLEGRO_TIMER *FPS_timer = NULL; 1113 ALLEGRO_TIMER *BPS_timer = NULL; 1114 ALLEGRO_DISPLAY *display = NULL; 1115 1116 ALLEGRO_BITMAP *bmp_1; 1117 ALLEGRO_BITMAP *bmp_2; 1118 */ 1119 1120 return 0; 1121} 1122 1123// The Console Prototype Pre-Alpha V-1.0 1124/*int main() 1125{ 1126 double BPS = 100; 1127 int BPS_seconds = 0; 1128 int seconds = 0; 1129 cout << "Set elevation angle: "; 1130 cin >> gun_phys.shell_FP_degrees; 1131 gun_phys.shell_FP_angle = (gun_phys.shell_FP_degrees / 180) * M_PI; 1132 cout << "In radians that is: " << gun_phys.shell_FP_angle << endl; 1133 cout << "Suggested data is for Prinz Eugen's 8 inch main guns." << endl; 1134 cout << "Input muzzle velocity in m/s (eg: 925): "; 1135 cin >> gun_phys.shell_velocity; 1136 cout << "Input shell mass in kg (eg: 122): "; 1137 cin >> gun_phys.shell_mass; 1138 cout << "Input shell diameter in cm (eg: 20.3): "; 1139 cin >> gun_phys.shell_diameter; 1140 gun_phys.shell_frontal_area = pow((gun_phys.shell_diameter / 2 / 100), 2) * M_PI; 1141 cout << "Input height of gun turret in meters (eg: 9.75): "; 1142 cin >> gun_phys.gun_deck_altitude; 1143 cout << endl; 1144 1145 1146 if(false == set_rho_values()) 1147 return -1; 1148 1149 if(false == set_Mach_Cd_values()) 1150 return -1; 1151 1152 gun_phys.shell_y_pos = gun_phys.init_muzzle_position(); 1153 gun_phys.shell_x_pos = 0; 1154 1155 while(gun_phys.shell_y_pos > 0) 1156 { 1157 gun_phys.rho_at_alt = gun_phys.Rho_Calculator(); 1158 //cout << "Rho interpolation okay..." << gun_phys.rho_at_alt << endl; 1159 gun_phys.shell_Mach = gun_phys.Mach_Calculator(); 1160 //cout << "Mach calc okay... " << gun_phys.shell_Mach << endl; 1161 gun_phys.Cd = gun_phys.Cd_Calculator(); 1162 //cout << "Cd interpolation okay..." << endl; 1163 gun_phys.shell_velocity = gun_phys.New_Velocity_Calculator(BPS); 1164 //cout << "Velocity okay..." << endl; 1165 gun_phys.update_shell_position(BPS); 1166 //cout << "Shell position okay (X, Y)... " << gun_phys.shell_x_pos << ", " << gun_phys.shell_y_pos << endl; 1167 gun_phys.shell_FP_degrees = (gun_phys.shell_FP_angle / M_PI) * 180; 1168 1169 1170 BPS_seconds++; 1171 if(BPS_seconds >= BPS) 1172 { 1173 seconds++; 1174 BPS_seconds = 0; 1175 } 1176 1177 1178 cout << "Seconds of flight: " << seconds << "." << BPS_seconds << endl; 1179 cout << "Shell altitude: " << gun_phys.shell_y_pos << " m" << endl; 1180 cout << "Shell range: " << gun_phys.shell_x_pos << " m" << endl; 1181 cout << "Shell Mach: " << gun_phys.shell_Mach << endl; 1182 cout << "Shell true Velocity (m/s): " << gun_phys.shell_velocity << endl; 1183 cout << "Shell flight path angle: " << gun_phys.shell_FP_degrees << endl; 1184 cout << "Shell Cd: " << gun_phys.Cd << endl << endl; 1185 1186 if(gun_phys.shell_y_pos < 0) 1187 { 1188 gun_phys.end_clip(); 1189 cout << "Corrected X position: " << gun_phys.shell_x_pos << endl << endl; 1190 } 1191 1192 usleep(useconds_t(1000000 / BPS)); 1193 } 1194 1195 return 0; 1196} 1197 1198// Ah! Things were so easy then! 1199*/

______________________________________
As long as it remains classified how long it took me to make I'll be deemed a computer game genius. - William Labbett
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why. -Unknown
I have recklessly set in motion a chain of events with the potential to so-drastically change the path of my life that I can only find it to be beautifully frightening.

KeithS
Member #11,950
May 2010

Thank you for your help and patience with this, someone972. That was it, indeed.

Turns out I was in such a hurry to get out of Windows and MSVC++ and back to Debian and CodeBlocks that I trusted my memory of more than a year ago and mixed up sprintf_s() with the WINAPI function DrawText(), which can use strlen(). But even then, the syntax was butt-backwards. My fault entirely for not treating the platform with it's due respect and review the msdn docs.

Yes, looking at it now I, too, am surprised that it compiled at all, but I assure you the code you got from that download was the very same code that did compile on MSVC++ 2008. If it hadn't, I would have been forced to recap and probably would not have posted this shamefully embarrassing thread at all... :-/

Again, thank you very much!

* * * * * * * * * * *

My Noobish Blog

someone972
Member #7,719
August 2006
avatar

I've made that sort of mistake many times, as have many others, so you aren't alone in that ;D. I like finding bugs in code as well, since it helps me in my own code and is also just fun, so thanks for the post ;).

______________________________________
As long as it remains classified how long it took me to make I'll be deemed a computer game genius. - William Labbett
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why. -Unknown
I have recklessly set in motion a chain of events with the potential to so-drastically change the path of my life that I can only find it to be beautifully frightening.

Go to: