c++ -保存前的值变化

dnph8jn4  于 2023-01-18  发布在  其他
关注(0)|答案(1)|浏览(149)

我正在从一个游戏(SharedMemoryMapped)阅读数据,一切正常,我得到了一圈的时间(毫秒),得到秒=毫秒/1000 f,
之后,我跟踪汽车的位置,直到1.000f,这意味着结束,我需要将一些数据保存到文件中,但lapTimeInSeconds在循环结束时不断变化,直到我得到所需的值(分钟:秒:毫秒)

while (true) {

//........code........

      // laptime
        float lapTimeInSeconds = (float) ((float)graphic.iCurrentTime / 1000.0f);
        printf("\rLAP: %.3f", lapTimeInSeconds);
        // save
        memcpy(&save_struct.lapTime_seconds, &lapTimeInSeconds, 4);
        memcpy(&save_struct.normalizedCarPosition, &graphic.normalizedCarPosition, 4);
        memcpy(&save_struct.gaz, &physics.gaz, 4);
        memcpy(&save_struct.brake, &physics.brake, 4);
        memcpy(&save_struct.speedKmh, &physics.speedKmh, 4);
        memcpy(&save_struct.steerAngle, &physics.steerAngle, 4);
        memcpy(&save_struct.gear, &physics.gear, 4);
        memcpy(&save_struct.carCoordiantes, &graphic.carCoordiantes, sizeof(save_struct.carCoordiantes));
    
        // write bytes into the file
        fwrite(&save_struct, sizeof(SaveStruct), 1, saveFile);
    
        if (graphic.normalizedCarPosition == 1) {
          fclose(saveFile);
          printf("\nFINAL LAP TIME: %.3f\n", lapTimeInSeconds); // HERE IS THE PROBLEM IT RESET TO 0.000 seconds, the VALUE GOT CHANGED AND SOMETIMES 0.654s 
          uint8_t seconds = (int) lapTimeInSeconds % 60;
          uint8_t minutes = lapTimeInSeconds / 60;
          uint16_t millisecond = (lapTimeInSeconds - ((minutes * 60) + seconds)) * 1000;
          std::string final_file_name = "laps/lap-" + std::to_string(lapCount) + "-" + std::to_string(minutes) + "-" + std::to_string(seconds) + "-" + std::to_string(millisecond) + ".lap";
          rename("laps/lap-not-completed.lap", final_file_name.c_str());
          lapCount++;
          saveFile = fopen("laps/lap-not-completed.lap", "wb");
        }

}

文件名变为0分钟、0秒、659毫秒

  • 如何使lapTimeInSeconds不改变,直到我保存文件?!

谢谢你。

qpgpyjmq

qpgpyjmq1#

问题是:赛车位置(1.000)意味着圈末,但时间在(0.996)被重置,所以我不得不改变条件。
@tadman谢谢你我改变了:

// save
        memcpy(&save_struct.lapTime_seconds, &lapTimeInSeconds, 4);
        memcpy(&save_struct.normalizedCarPosition, &graphic.normalizedCarPosition, 4);
        memcpy(&save_struct.gaz, &physics.gaz, 4);
        memcpy(&save_struct.brake, &physics.brake, 4);
        memcpy(&save_struct.speedKmh, &physics.speedKmh, 4);
        memcpy(&save_struct.steerAngle, &physics.steerAngle, 4);
        memcpy(&save_struct.gear, &physics.gear, 4);
        memcpy(&save_struct.carCoordiantes, &graphic.carCoordiantes, sizeof(save_struct.carCoordiantes));

收件人:

save_struct.lapTime_seconds = (float) ((float)graphic.iCurrentTime / 1000.0f);
  save_struct.normalizedCarPosition = graphic.normalizedCarPosition;
  save_struct.gaz = physics.gaz;
  save_struct.brake = physics.brake;
  save_struct.speedKmh = physics.speedKmh;
  save_struct.steerAngle = physics.steerAngle;
  save_struct.gear = physics.gear;
  save_struct.carCoordiantes[0][0] = graphic.carCoordiantes[0][0]; // x
  save_struct.carCoordiantes[0][1] = graphic.carCoordiantes[0][1]; // y
  save_struct.carCoordiantes[0][2] = graphic.carCoordiantes[0][2]; // z

相关问题