Advice for making a menu with dbgio

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
Protofall
DCEmu Freak
DCEmu Freak
Posts: 78
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Sun Jan 14, 2018 8:03 pm
Location: Emu land
Has thanked: 21 times
Been thanked: 18 times
Contact:

Advice for making a menu with dbgio

Post by Protofall »

I want to make a basic menu for a VFS explorer that supports ext2 formatted sd cards. I know how to do the sd card stuff and launching executables, but I'm having trouble with dbgio. By default the dbgio library can display 17 lines of text at once (At 640 x 480 resolution). In my program's current state it only lists the root of the VFS (.. vmu, cd, ram, pty). They are prefixed by either two spaces or one of the items is prefixed with a right arrow and a space (The cursor). Up and Down on the Dpad will move your cursor, which later will be used to select the executable/directory you want to explore. The problem is that dbgio is like a console log so when I do something minor like move the cursor I have to re-display the directory's content followed by enough new lines characters to make sure my text is at the top of the screen.

This would be fine except when I do this you can see the text moving up the page as new characters get displayed which isn't very pleasing to the eye when doing something like scrolling through a menu. I tried to find a dbgio command that clears the screen or some way I could print everything I want in one go then flush it (In hopes it just all updates at once), but I couldn't find anything. The only solutions I could think of is setting up another framebuffer and switch between them (But I don't know how that works in KOS without the PVR system), use a different method of drawing with the BIOS font or maybe there is a way to clear the console. Any advice on what should I be doing?

Here's the code I'm currently using
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#include <dc/maple.h>
#include <dc/maple/controller.h>

#include <kos/fs.h>	//Might need it for fs_load()
#include <arch/exec.h>	//arch_exec()
#include <assert.h>	//assert()
#include <kos/dbgio.h>	//Better debug output to the screen

#include <kos/fs.h>	//fs_open, fs_readdir

void indent(uint8_t hovering){
	if(hovering){
		dbgio_printf("> ");
	}
	else{
		dbgio_printf("  ");
	}
	return;
}

void print_dir(uint16_t cursor_pos, uint16_t counter_pos, uint16_t * file_count){
	file_t d;
	dirent_t * de;
	d = fs_open("/", O_RDONLY | O_DIR);
	while(1){
		if(counter_pos == 0){
			indent(counter_pos == cursor_pos);
			dbgio_printf("..\n");
		}
		else{
			de = fs_readdir(d);
			if(de == NULL){
				break;
			}
			indent(counter_pos == cursor_pos);
			dbgio_printf("/%s\n", de->name);
		}
		counter_pos++;
	}
	*file_count = counter_pos;	//Update the number of files
	int i;
	for(i = 1 + *file_count; i < 17; i++){
		dbgio_printf("\n");
	}
	fs_close(d);
	return;
}

int main(){
	dbgio_dev_select("fb");
	uint16_t cursor_pos = 0;
	uint16_t counter_pos = 0;
	uint16_t file_count;

	print_dir(cursor_pos, counter_pos, &file_count);

	uint32_t previous_buttons = 0;	//Records the previous buttons polled
	maple_device_t  * controller;
	cont_state_t * st;

	while(1){
		controller = maple_enum_type(0, MAPLE_FUNC_CONTROLLER);	//Reads the first plugged in controller
		st = (cont_state_t *)maple_dev_status(controller);	//State of controller

		//Up press
		if((st->buttons & CONT_DPAD_UP) && !(previous_buttons & CONT_DPAD_UP)){
			if(cursor_pos != 0){
				cursor_pos--;
			}
			print_dir(cursor_pos, counter_pos, &file_count);
		}

		//Down press
		if((st->buttons & CONT_DPAD_DOWN) && !(previous_buttons & CONT_DPAD_DOWN)){
			if(cursor_pos < file_count - 1){
				cursor_pos++;
			}
			print_dir(cursor_pos, counter_pos, &file_count);
		}

		previous_buttons = st->buttons;
	}
	return 0;
}
I don't think I'm able to add attachments to this post so no cdi file, but if you have KOS installed it should be simple enough to re-create this.
Last edited by Protofall on Mon Sep 10, 2018 8:05 am, edited 1 time in total.
Moving Day: A clone of Dr Mario with 8-player support <https://dcemulation.org/phpBB/viewtopic ... 4&t=105389>
A recreation of Minesweeper for the Dreamcast <viewtopic.php?f=34&t=104820>

Twitter <https://twitter.com/ProfessorToffal>
YouTube (Not much there, but there are a few things) <https://www.youtube.com/user/TrueMenfa>
User avatar
lerabot
Insane DCEmu
Insane DCEmu
Posts: 134
Joined: Sun Nov 01, 2015 8:25 pm
Has thanked: 2 times
Been thanked: 19 times

Re: Advice for making a menu with dbgio

Post by lerabot »

I've tried your code and couldn't get anything to display on screen. Also the clear_screen(); function couldn't be found.
User avatar
Protofall
DCEmu Freak
DCEmu Freak
Posts: 78
Joined: Sun Jan 14, 2018 8:03 pm
Location: Emu land
Has thanked: 21 times
Been thanked: 18 times
Contact:

Re: Advice for making a menu with dbgio

Post by Protofall »

lerabot wrote: Mon Sep 10, 2018 7:37 am I've tried your code and couldn't get anything to display on screen. Also the clear_screen(); function couldn't be found.
Thanks for that, the version I posted is a cropped version of the program I was compiling, but should still behave the same. Functions like clear_screen() are already covered by other code present here so I removed it. I've edited the OP to remove those old calls. I just tried compiling the new version and running it in the lxdream emulator and it still works as I described. I haven't tried it on hardware yet, but I feel it would still behave the same.

I know multiple emulators don't support software rendering. I can confirm it displays nothing in redream and I don't think reicast has software rendering support (And hence I doubt nullDC, its ancestor, would either). DEMUL might work, but I'm on Linux so I can't confirm.
Moving Day: A clone of Dr Mario with 8-player support <https://dcemulation.org/phpBB/viewtopic ... 4&t=105389>
A recreation of Minesweeper for the Dreamcast <viewtopic.php?f=34&t=104820>

Twitter <https://twitter.com/ProfessorToffal>
YouTube (Not much there, but there are a few things) <https://www.youtube.com/user/TrueMenfa>
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5658
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: Advice for making a menu with dbgio

Post by BlueCrab »

You don't want to use fb_console to do a menu like that, because it just ends up being more work than it's worth (and that's not what it was designed for). Writing strings directly to the framebuffer with the bfont_* functions is pretty easy, and that's basically all the fb_console stuff is doing anyway. In fact, it's not all that much more difficult to do so with the PVR either.

If you want to try to tackle doing so with the PVR, you're welcome to look at the code from CrabEmu for inspiration. The font.[ch] files and menu.c file in there are probably the most relevant to what you're doing (menu.c also calls stuff from pvrutils.c). I don't claim that's the cleanest/simplest file menu code out there, but it does work. ;-)
User avatar
Protofall
DCEmu Freak
DCEmu Freak
Posts: 78
Joined: Sun Jan 14, 2018 8:03 pm
Location: Emu land
Has thanked: 21 times
Been thanked: 18 times
Contact:

Re: Advice for making a menu with dbgio

Post by Protofall »

BlueCrab wrote: Mon Sep 10, 2018 9:02 am You don't want to use fb_console to do a menu like that, because it just ends up being more work than it's worth (and that's not what it was designed for). Writing strings directly to the framebuffer with the bfont_* functions is pretty easy, and that's basically all the fb_console stuff is doing anyway. In fact, it's not all that much more difficult to do so with the PVR either.

If you want to try to tackle doing so with the PVR, you're welcome to look at the code from CrabEmu for inspiration. The font.[ch] files and menu.c file in there are probably the most relevant to what you're doing (menu.c also calls stuff from pvrutils.c). I don't claim that's the cleanest/simplest file menu code out there, but it does work. ;-)
Thanks for the advice, I'll look at that code.
Moving Day: A clone of Dr Mario with 8-player support <https://dcemulation.org/phpBB/viewtopic ... 4&t=105389>
A recreation of Minesweeper for the Dreamcast <viewtopic.php?f=34&t=104820>

Twitter <https://twitter.com/ProfessorToffal>
YouTube (Not much there, but there are a few things) <https://www.youtube.com/user/TrueMenfa>
Post Reply