dynamic memory allocation
This commit is contained in:
@@ -1,7 +1,17 @@
|
||||
#ifndef INPUT_H
|
||||
#define INPUT_H
|
||||
|
||||
void waitForKey(char key);
|
||||
int selector_render(char menu_items[10][32], int items_length);
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
char* text;
|
||||
int id;
|
||||
} MenuItem;
|
||||
|
||||
MenuItem* create_menu(size_t item_count);
|
||||
void set_menu_item(MenuItem* menu, int index, const char* text);
|
||||
void destroy_menu(MenuItem* menu, size_t item_count);
|
||||
|
||||
int selector_render(MenuItem* menu, size_t items_length);
|
||||
|
||||
#endif // !INPUT_H
|
||||
|
@@ -1,54 +1,90 @@
|
||||
#include <3ds.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "navigation.h"
|
||||
|
||||
//void waitForKey(char key) {
|
||||
// while (true) {
|
||||
// hidScanInput();
|
||||
// u32 key_down = hidKeysDown();
|
||||
// if (key_down & key) break;
|
||||
// }
|
||||
//}
|
||||
MenuItem* create_menu(size_t item_count)
|
||||
{
|
||||
MenuItem* items = malloc(item_count * sizeof(MenuItem));
|
||||
if (!items)
|
||||
{
|
||||
printf("\x1b[1;1HMemory allocation failed!");
|
||||
return NULL;
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
int render_cursor(int items) {
|
||||
void set_menu_item(MenuItem* menu, int index, const char* text)
|
||||
{
|
||||
int id = index + 1;
|
||||
if (menu && text)
|
||||
{
|
||||
menu[index].text = (char*)malloc(strlen(text) + 1);
|
||||
if (menu[index].text)
|
||||
{
|
||||
strcpy(menu[index].text, text);
|
||||
menu[index].id = id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void destroy_menu(MenuItem* menu, size_t item_count)
|
||||
{
|
||||
if (menu)
|
||||
{
|
||||
for (int i = 0; i < item_count; i++)
|
||||
{
|
||||
if (menu[i].text)
|
||||
{
|
||||
free(menu[i].text);
|
||||
}
|
||||
}
|
||||
free(menu);
|
||||
}
|
||||
}
|
||||
|
||||
void selector_draw(MenuItem* menu, size_t items_count)
|
||||
{
|
||||
for (int i = 0; i < items_count; i = i + 1)
|
||||
{
|
||||
printf("\x1b[%d;2H%s", i+1, menu[i].text);
|
||||
}
|
||||
}
|
||||
|
||||
int render_cursor(int items)
|
||||
{
|
||||
int cursor_position = 1;
|
||||
|
||||
printf("\x1b[%d;1H>", cursor_position);
|
||||
while (true) {
|
||||
while (true)
|
||||
{
|
||||
hidScanInput();
|
||||
u32 key_down = hidKeysDown();
|
||||
|
||||
if (key_down & KEY_DDOWN) {
|
||||
if (key_down & KEY_DDOWN)
|
||||
{
|
||||
printf("\x1b[%d;1H ", cursor_position);
|
||||
cursor_position = (cursor_position >= items) ? 1 : cursor_position + 1;
|
||||
printf("\x1b[%d;1H>", cursor_position);
|
||||
}
|
||||
if (key_down & KEY_DUP) {
|
||||
if (key_down & KEY_DUP)
|
||||
{
|
||||
printf("\x1b[%d;1H ", cursor_position);
|
||||
cursor_position = (cursor_position <= 1) ? items : cursor_position - 1;
|
||||
printf("\x1b[%d;1H>", cursor_position);
|
||||
}
|
||||
if (key_down & KEY_B) {
|
||||
if (key_down & KEY_B)
|
||||
return 0;
|
||||
}
|
||||
if (key_down & KEY_A) {
|
||||
if (key_down & KEY_A)
|
||||
break;
|
||||
}
|
||||
|
||||
printf("\x1b[10;16Hcursorpos: %d", cursor_position);
|
||||
}
|
||||
return cursor_position;
|
||||
}
|
||||
|
||||
void selector_draw(char menu_items[10][32], int items_length) {
|
||||
for (int i = 0; i < items_length; i = i + 1) {
|
||||
printf("\x1b[%d;2H%s", i+1, menu_items[i]);
|
||||
}
|
||||
}
|
||||
|
||||
int selector_render(char menu_items[10][32], int items_length) {
|
||||
int selector_render(MenuItem* menu, size_t items_length)
|
||||
{
|
||||
consoleClear();
|
||||
selector_draw(menu_items, items_length);
|
||||
selector_draw(menu, items_length);
|
||||
int selection = render_cursor(items_length);
|
||||
return selection;
|
||||
}
|
||||
|
Reference in New Issue
Block a user