diff --git a/include/DMDUtil/DMD.h b/include/DMDUtil/DMD.h index abac80a..5cf96d5 100644 --- a/include/DMDUtil/DMD.h +++ b/include/DMDUtil/DMD.h @@ -10,6 +10,7 @@ #define DMDUTIL_FRAME_BUFFER_SIZE 16 #define DMDUTIL_MAX_NAME_SIZE 32 +#define DMDUTIL_MAX_TRANSITIONAL_FRAME_DURATION 25 #include #include diff --git a/src/DMD.cpp b/src/DMD.cpp index 1d41789..3a36823 100644 --- a/src/DMD.cpp +++ b/src/DMD.cpp @@ -974,9 +974,12 @@ void DMD::DumpDMDTxtThread() char name[DMDUTIL_MAX_NAME_SIZE] = ""; char filename[128]; int bufferPosition = 0; - uint8_t renderBuffer[256 * 64] = {0}; + int renderBufferPosition = 1; + uint8_t renderBuffer[3][256 * 64] = {0}; + uint32_t passed[3] = {0}; bool update = false; std::chrono::steady_clock::time_point start; + FILE* f = nullptr; while (true) { @@ -985,6 +988,7 @@ void DMD::DumpDMDTxtThread() sl.unlock(); if (m_stopFlag) { + if (f) fclose(f); return; } @@ -999,36 +1003,62 @@ void DMD::DumpDMDTxtThread() if (strcmp(m_updateBuffer[m_updateBufferPosition]->name, name) != 0) { // New game ROM. + start = std::chrono::steady_clock::now(); + if (f) fclose(f); strcpy(name, m_updateBuffer[m_updateBufferPosition]->name); snprintf(filename, DMDUTIL_MAX_NAME_SIZE + 5, "%s.txt", name); + f = fopen(filename, "a"); update = true; - start = std::chrono::steady_clock::now(); + memset(renderBuffer, 0, 2 * 256 * 64); + passed[0] = passed[1] = 0; } if (name[0] != '\0') { int length = m_updateBuffer[bufferPosition]->width * m_updateBuffer[bufferPosition]->height; - if (update || (memcmp(renderBuffer, m_updateBuffer[bufferPosition]->data, length) != 0)) + if (update || (memcmp(renderBuffer[1], m_updateBuffer[bufferPosition]->data, length) != 0)) { - memcpy(renderBuffer, m_updateBuffer[bufferPosition]->data, length); - FILE* f = fopen(filename, "a"); + passed[2] = (uint32_t)(std::chrono::duration_cast( + std::chrono::steady_clock::now() - start) + .count()); + memcpy(renderBuffer[2], m_updateBuffer[bufferPosition]->data, length); + + if (m_updateBuffer[bufferPosition]->depth == 2 && + (passed[2] - passed[1]) < DMDUTIL_MAX_TRANSITIONAL_FRAME_DURATION) + { + int i = 0; + while (i < length && + ((renderBuffer[0][i] == 2) || + ((renderBuffer[0][i] == 3) || (renderBuffer[2][i] > 1)) == (renderBuffer[1][i] > 0))) + { + i++; + } + if (i == length) + { + // renderBuffer[1] is a transitional frame, delete it. + memcpy(renderBuffer[1], renderBuffer[2], length); + passed[1] += passed[2]; + continue; + } + } + if (f) { - fprintf(f, "0x%08x\n", - (uint32_t)(std::chrono::duration_cast( - std::chrono::steady_clock::now() - start) - .count())); + fprintf(f, "0x%08x\n", passed[0]); for (int y = 0; y < m_updateBuffer[bufferPosition]->height; y++) { for (int x = 0; x < m_updateBuffer[bufferPosition]->width; x++) { - fprintf(f, "%x", renderBuffer[y * m_updateBuffer[bufferPosition]->width + x]); + fprintf(f, "%x", renderBuffer[0][y * m_updateBuffer[bufferPosition]->width + x]); } fprintf(f, "\n"); } fprintf(f, "\n"); - fclose(f); } + memcpy(renderBuffer[0], renderBuffer[1], length); + passed[0] = passed[1]; + memcpy(renderBuffer[1], renderBuffer[2], length); + passed[1] = passed[2]; } } } @@ -1042,6 +1072,7 @@ void DMD::DumpDMDRawThread() char filename[128]; int bufferPosition = 0; std::chrono::steady_clock::time_point start; + FILE* f = nullptr; while (true) { @@ -1050,6 +1081,7 @@ void DMD::DumpDMDRawThread() sl.unlock(); if (m_stopFlag) { + if (f) fclose(f); return; } @@ -1062,15 +1094,17 @@ void DMD::DumpDMDRawThread() if (strcmp(m_updateBuffer[m_updateBufferPosition]->name, name) != 0) { // New game ROM. + start = std::chrono::steady_clock::now(); + if (f) fclose(f); strcpy(name, m_updateBuffer[m_updateBufferPosition]->name); snprintf(filename, DMDUTIL_MAX_NAME_SIZE + 5, "%s.raw", name); - start = std::chrono::steady_clock::now(); + f = fopen(filename, "ab"); } if (name[0] != '\0') { int length = m_updateBuffer[bufferPosition]->width * m_updateBuffer[bufferPosition]->height; - FILE* f = fopen(filename, "ab"); + if (f) { auto current = @@ -1081,8 +1115,6 @@ void DMD::DumpDMDRawThread() fwrite(&size, 1, 4, f); fwrite(m_updateBuffer[bufferPosition], 1, size, f); - - fclose(f); } } } diff --git a/src/test.cpp b/src/test.cpp index 90a652c..1dec99b 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -171,8 +171,7 @@ int main(int argc, const char* argv[]) for (int i = 1; i <= 100; i++) { - snprintf(filename, 28, "test/rgb565_%dx%d/%04d.raw", - width, height, i); + snprintf(filename, 28, "test/rgb565_%dx%d/%04d.raw", width, height, i); printf("Render raw: %s\n", filename); fileptr = fopen(filename, "rb"); fread(buffer, size, 1, fileptr);