Skip to content

Commit

Permalink
doom: simplify frame rendering code
Browse files Browse the repository at this point in the history
  • Loading branch information
and3rson committed Feb 14, 2024
1 parent e8045e3 commit 800db90
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion firmware-doom/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Це - порт Doom для Лілки.

[lib/doomgeneric](./lib/doomgeneric) містить модифікований код рушія Doom Generic для запуску на ESP32-S3. Я додав динамічну алокацію пам'яті для деяких "товстих" масивів, щоб використовувати PSRAM. Таким чином весь основний код Doom тепер спокійно поміщається в 400 КБ основної RAM.
[lib/doomgeneric](./lib/doomgeneric) містить модифікований код рушія Doom Generic для запуску на ESP32-S3. Я додав динамічну алокацію пам'яті для деяких "товстих" масивів, щоб використовувати PSRAM. Таким чином весь основний код Doom тепер спокійно поміщається в 400 КБ основної RAM. Крім цього, є ще деякі зміни (див. [README](./lib/doomgeneric)).

[src/main.cpp](./src/main.cpp) містить код, специфічний для Лілки (малювання екрана, ініціалізація, etc).

Expand Down
6 changes: 6 additions & 0 deletions firmware-doom/lib/doomgeneric/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Ця бібліотека містить модифікований код з <https://github.com/ozkl/doomgeneric>.

## Зміна 1: зменшення розміру кадру

Я зменшив SCREENWIDTH/SCREENHEIGHT і DOOMGENERIC_RESX/DOOMGENERIC_RESY до розміру екрана Лілки.

## Зміна 2: динамічна алокація пам'яті

Оскільки ESP32-S3 має всього 400 КБ SRAM, деякі масиви не поміщаються в RAM, і їх потрібно динамічно ініціалізувати в heap всередині PSRAM (якої може бути аж до 16 МБ).

Тому найважливіша зміна - це заміна деяких стекових алокацій пам'яті на динамічну (malloc/free), наприклад:
Expand Down
37 changes: 15 additions & 22 deletions firmware-doom/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,31 @@ extern "C" void DG_DrawFrame() {
lilka::display.startWrite();
lilka::display.writeAddrWindow(0, 65, 240, 150);
uint16_t row[240];
// Цей код - застарілий. Я адаптував Doom Generic для роботи з 240x150 за замовчуванням.
// Convert 640x400 to 240x150
// for (int y = 0; y < 150; y++) {
// for (int x = 0; x < 240; x++) {
// int yy = y * 8 / 3;
// int xx = x * 8 / 3;
// uint32_t pixel = DG_ScreenBuffer[yy * 640 + xx];
// uint8_t r = (pixel >> 16) & 0xff;
// uint8_t g = (pixel >> 8) & 0xff;
// uint8_t b = pixel & 0xff;
// row[x] = lilka::display.color565(r, g, b);
// }
// lilka::display.writePixels(row, 240);
// }
// Нова версія:
for (int y = 0; y < 150; y++) {
for (int x = 0; x < 240; x++) {
int yy = y * 8 / 3;
int xx = x * 8 / 3;
uint32_t pixel = DG_ScreenBuffer[yy * 640 + xx];
uint32_t pixel = DG_ScreenBuffer[y * 240 + x];
uint8_t r = (pixel >> 16) & 0xff;
uint8_t g = (pixel >> 8) & 0xff;
uint8_t b = pixel & 0xff;
row[x] = lilka::display.color565(r, g, b);
}
lilka::display.writePixels(row, 240);
}
// for (int y = 0; y < 200; y++) {
// // For every 8 rows, draw only 3
// if (y % 8 != 0 && y % 8 != 3 && y % 8 != 6) {
// continue;
// }
// uint8_t pixelIndex = 0;
// for (int x = 0; x < 320; x++) {
// // For every 8 pixels, draw only 3
// if (x % 8 != 0 && x % 8 != 3 && x % 8 != 6) {
// continue;
// }
// uint32_t pixel = DG_ScreenBuffer[y * 320 + x];
// uint8_t r = (pixel >> 16) & 0xff;
// uint8_t g = (pixel >> 8) & 0xff;
// uint8_t b = pixel & 0xff;
// row[pixelIndex++] = lilka::display.color565(r, g, b);
// }
// lilka::display.writePixels(row, 240);
// }
lilka::display.endWrite();
}

Expand Down

0 comments on commit 800db90

Please sign in to comment.