Skip to content

Commit ef09a9d

Browse files
committed
console: refactor code to correctly write to the buffer offsets
1 parent ac64a5e commit ef09a9d

1 file changed

Lines changed: 71 additions & 60 deletions

File tree

libogc/console.c

Lines changed: 71 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,35 @@ static u32 __gecko_safe = 0;
7676

7777
extern u8 console_font_8x16[];
7878

79+
static u32 __console_get_offset()
80+
{
81+
if(!curr_con)
82+
return 0;
83+
84+
return _console_buffer != NULL
85+
? 0
86+
: (curr_con->target_y * curr_con->tgt_stride) + (curr_con->target_x * VI_DISPLAY_PIX_SZ);
87+
}
88+
7989
void __console_vipostcb(u32 retraceCnt)
8090
{
81-
u32 ycnt,xcnt, fb_stride;
82-
u32 *fb,*ptr;
83-
u32 offset;
91+
u8 *fb,*ptr;
8492

8593
do_xfb_copy = true;
86-
87-
offset = (curr_con->target_y*curr_con->tgt_stride) + curr_con->target_x*VI_DISPLAY_PIX_SZ;
8894
ptr = curr_con->destbuffer;
89-
fb = VIDEO_GetCurrentFramebuffer()+offset;
90-
fb_stride = curr_con->tgt_stride/4 - (curr_con->con_xres/VI_DISPLAY_PIX_SZ);
95+
fb = (u8*)((u32)VIDEO_GetCurrentFramebuffer() + (curr_con->target_y * curr_con->tgt_stride) + (curr_con->target_x * VI_DISPLAY_PIX_SZ));
9196

92-
for(ycnt=curr_con->con_yres;ycnt>0;ycnt--)
97+
// Clears 1 line of pixels at a time
98+
for(u16 ycnt = 0; ycnt < curr_con->con_yres; ycnt++)
9399
{
94-
for(xcnt=curr_con->con_xres;xcnt>0;xcnt-=VI_DISPLAY_PIX_SZ)
95-
{
96-
*fb++ = *ptr++;
97-
}
98-
fb += fb_stride;
100+
memcpy(fb, ptr, curr_con->con_xres * VI_DISPLAY_PIX_SZ);
101+
ptr+= curr_con->con_stride;
102+
fb+= curr_con->tgt_stride;
99103
}
100104

101105
do_xfb_copy = false;
102106
}
103107

104-
105108
static void __console_drawc(int c)
106109
{
107110
console_data_s *con;
@@ -118,7 +121,7 @@ static void __console_drawc(int c)
118121
con = curr_con;
119122

120123
ptr = (unsigned int*)(con->destbuffer + ( con->con_stride * con->cursor_row * FONT_YSIZE ) + ((con->cursor_col * FONT_XSIZE / 2) * 4)
121-
+ (_console_buffer != NULL ? 0 : (curr_con->target_y*curr_con->tgt_stride) + curr_con->target_x*VI_DISPLAY_PIX_SZ));
124+
+ __console_get_offset());
122125
pbits = &con->font[c * FONT_YSIZE];
123126
nextline = con->con_stride/4 - 4;
124127
fgcolor = con->foreground;
@@ -181,47 +184,48 @@ static void __console_drawc(int c)
181184
#endif
182185
}
183186
}
184-
static void __console_clear_line( int line, int from, int to ) {
187+
188+
static void __console_clear_line(int line, int from, int to )
189+
{
185190
console_data_s *con;
186-
unsigned int c;
187-
unsigned int *p;
188-
unsigned int x_pixels;
189-
unsigned int px_per_col = FONT_XSIZE/2;
190-
unsigned int line_height = FONT_YSIZE;
191-
unsigned int line_width;
192-
191+
u8* p;
192+
// Each character is FONT_XSIZE * VI_DISPLAY_PIX_SZ wide
193+
const u32 line_width = (to - from) * (FONT_XSIZE * VI_DISPLAY_PIX_SZ);
194+
193195
if( !(con = curr_con) ) return;
194-
// For some reason there are xres/2 pixels per screen width
195-
x_pixels = con->con_xres / 2;
196-
197-
line_width = (to - from)*px_per_col;
198-
p = (unsigned int*)con->destbuffer;
199-
200-
// Move pointer to the current line and column offset
201-
p += line*(FONT_YSIZE*x_pixels) + from*px_per_col;
202-
203-
// Clears 1 line of pixels at a time, line_height times
204-
while( line_height-- ) {
205-
c = line_width;
206-
while( c-- )
207-
*p++ = con->background;
208-
p -= line_width;
209-
p += x_pixels;
210-
}
196+
197+
//add the given row & column to the offset & assign the pointer
198+
const u32 offset = __console_get_offset() + (line * con->con_stride * FONT_YSIZE) + (from * FONT_XSIZE * VI_DISPLAY_PIX_SZ);
199+
p = (u8*)((u32)con->destbuffer + offset);
200+
201+
// Clears 1 line of pixels at a time
202+
for(u16 ycnt = 0; ycnt < FONT_YSIZE; ycnt++)
203+
{
204+
for(u32 xcnt = 0; xcnt < line_width; xcnt += 4)
205+
*(u32*)((u32)p + xcnt) = con->background;
206+
207+
p += con->con_stride;
208+
}
211209
}
210+
212211
static void __console_clear(void)
213212
{
214213
console_data_s *con;
215-
unsigned int c;
216-
unsigned int *p;
214+
u8* p;
217215

218216
if( !(con = curr_con) ) return;
219217

220-
c = (con->con_xres*con->con_yres)/2;
221-
p = (unsigned int*)con->destbuffer;
222-
223-
while(c--)
224-
*p++ = con->background;
218+
//get console pointer
219+
p = (u8*)((u32)con->destbuffer + __console_get_offset());
220+
221+
// Clears 1 line of pixels at a time
222+
for(u16 ycnt = 0; ycnt < con->con_yres; ycnt++)
223+
{
224+
for(u32 xcnt = 0; xcnt < con->con_xres * VI_DISPLAY_PIX_SZ; xcnt+=4)
225+
*(u32*)((u32)p + xcnt) = con->background;
226+
227+
p += con->con_stride;
228+
}
225229

226230
con->cursor_row = 0;
227231
con->cursor_col = 0;
@@ -264,8 +268,8 @@ void __console_init(void *framebuffer,int xstart,int ystart,int xres,int yres,in
264268
con->destbuffer = framebuffer;
265269
con->con_xres = xres;
266270
con->con_yres = yres;
267-
con->con_cols = (xres - xstart) / FONT_XSIZE;
268-
con->con_rows = (yres - ystart) / FONT_YSIZE;
271+
con->con_cols = xres / FONT_XSIZE;
272+
con->con_rows = yres / FONT_YSIZE;
269273
con->con_stride = con->tgt_stride = stride;
270274
con->target_x = xstart;
271275
con->target_y = ystart;
@@ -305,8 +309,8 @@ void __console_init_ex(void *conbuffer,int tgt_xstart,int tgt_ystart,int tgt_str
305309
con->con_yres = con_yres;
306310
con->tgt_stride = tgt_stride;
307311
con->con_stride = con_stride;
308-
con->con_cols = (con_xres - tgt_xstart) / FONT_XSIZE;
309-
con->con_rows = (con_yres - tgt_ystart) / FONT_YSIZE;
312+
con->con_cols = con_xres / FONT_XSIZE;
313+
con->con_rows = con_yres / FONT_YSIZE;
310314
con->cursor_row = 0;
311315
con->cursor_col = 0;
312316
con->saved_row = 0;
@@ -578,17 +582,24 @@ int __console_write(struct _reent *r,void *fd,const char *ptr,size_t len)
578582
}
579583
}
580584

585+
/* if bottom border reached, scroll */
581586
if( con->cursor_row >= con->con_rows)
582587
{
583-
/* if bottom border reached scroll */
584-
memcpy(con->destbuffer,
585-
con->destbuffer+con->con_stride*(FONT_YSIZE*FONT_YFACTOR+FONT_YGAP),
586-
con->con_stride*con->con_yres-FONT_YSIZE);
587-
588-
unsigned int cnt = (con->con_stride * (FONT_YSIZE * FONT_YFACTOR + FONT_YGAP))/4;
589-
unsigned int *ptr = (unsigned int*)(con->destbuffer + con->con_stride * (con->con_yres - FONT_YSIZE));
590-
while(cnt--)
591-
*ptr++ = con->background;
588+
const u8* console = (u8*)((u32)con->destbuffer + __console_get_offset());
589+
const u32 last_console_row = curr_con->con_rows == 0
590+
? 0
591+
: (curr_con->con_rows-1);
592+
593+
//copy lines upwards
594+
u8* ptr = (u8*)console;
595+
for(u32 ycnt = 0; ycnt < (last_console_row * FONT_YSIZE); ycnt++)
596+
{
597+
memmove(ptr, ptr + curr_con->con_stride * FONT_YSIZE, curr_con->con_cols * FONT_XSIZE * VI_DISPLAY_PIX_SZ);
598+
ptr+= curr_con->con_stride;
599+
}
600+
601+
//clear last line
602+
__console_clear_line(last_console_row, 0, con->con_cols);
592603
con->cursor_row--;
593604
}
594605
}

0 commit comments

Comments
 (0)