Skip to content

Commit f27fc61

Browse files
authored
Merge pull request BrunoLevy#107 from M4rkoHR/master
Doom Nearest Neighbor scaling LUT implementation
2 parents ff401af + 68db73f commit f27fc61

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

LiteX/software/Doom/mc1-doom/src/i_video_oled.c

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,34 @@
1717

1818
static uint16_t s_palette[256] __attribute((section(".fastdata")));
1919

20+
static uint16_t nearest_x_lut[96] __attribute((section(".fastdata")));
21+
static uint8_t nearest_y_lut[64] __attribute((section(".fastdata")));
22+
2023
void I_InitGraphics (void) {
21-
// Only initialize once.
22-
static int initialized = 0;
23-
if (initialized!=0)
24-
return;
25-
initialized = 1;
26-
screens[0] = (unsigned char*)malloc (SCREENWIDTH * SCREENHEIGHT);
24+
// Only initialize once.
25+
static int initialized = 0;
26+
if (initialized!=0)
27+
return;
28+
initialized = 1;
29+
30+
screens[0] = (unsigned char*)malloc (SCREENWIDTH * SCREENHEIGHT);
2731
if (screens[0] == NULL)
2832
I_Error ("Couldn't allocate screen memory");
29-
oled_init();
33+
34+
float scaleX = (float)SCREENWIDTH / OLED_WIDTH;
35+
float scaleY = (float)SCREENHEIGHT / OLED_HEIGHT;
36+
37+
for (uint8_t y = 0; y < OLED_HEIGHT; ++y) {
38+
uint8_t iy = (uint8_t)(y * scaleY);
39+
nearest_y_lut[y] = iy;
40+
}
41+
42+
for(uint16_t x = 0; x < OLED_WIDTH; ++x){
43+
uint16_t ix = (uint16_t)(x * scaleX);
44+
nearest_x_lut[x] = ix;
45+
}
46+
47+
oled_init();
3048
}
3149

3250
void I_ShutdownGraphics (void) {
@@ -66,19 +84,24 @@ static inline void oled_data_uint16_raw(uint16_t RGB) {
6684
void I_FinishUpdate (void) {
6785
#ifdef CSR_OLED_SPI_BASE
6886
const unsigned char* src = (const unsigned char*)screens[0];
69-
// Resolution / 4, a centered 80x50 window
70-
oled_write_window(8,7,87,56);
87+
88+
oled_write_window(0,0,95,63);
89+
7190
const unsigned char* line_ptr = src;
7291

7392
oled_ctl_out_write(OLED_SPI_DAT);
7493
oled_spi_cs_write(OLED_SPI_CS_LOW);
75-
for(int y=0; y<200; y+=4) {
76-
for(int x=0; x<320; x+=4) {
77-
uint16_t pixelvalue = s_palette[line_ptr[x]];
94+
95+
for(int y=0; y<OLED_HEIGHT; y++) {
96+
for(int x=0; x<OLED_WIDTH; x++) {
97+
98+
uint16_t pixelvalue = s_palette[line_ptr[(int)nearest_x_lut[x]]];
7899
oled_data_uint16_raw(pixelvalue);
100+
79101
}
80-
line_ptr += 4*320;
102+
line_ptr = src + nearest_y_lut[y]*SCREENWIDTH;
81103
}
104+
82105
oled_spi_cs_write(OLED_SPI_CS_HIGH);
83106
#endif
84107

0 commit comments

Comments
 (0)