Skip to content

Instantly share code, notes, and snippets.

@ryonagana ryonagana/readini.c
Last active Jul 4, 2019

Embed
What would you like to do?
#include "include/engine/menu.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
CB_MENU *menu_init(const char *menufile)
{
CB_MENU *menu_ptr = NULL;
menu_ptr = malloc(sizeof(CB_MENU));
menu_ptr->items = NULL;
menu_ptr->config = NULL;
menu_ptr->item_count = 0;
strncpy(menu_ptr->filename,menufile, 56);
menu_ptr->config = config_create(menufile);
return menu_ptr;
}
CB_MENU_ITEM *menu_item_init(int id, int x, int y, const char *name, CB_MENU_API api)
{
CB_MENU_ITEM *item_p = NULL;
item_p = malloc(sizeof(CB_MENU_ITEM));
assert(item_p != NULL);
item_p->id = id;
item_p->x = x;
item_p->y = y;
strncpy(item_p->name,name,56);
item_p->api = &api;
return item_p;
}
void menu_item_destroy(CB_MENU_ITEM *menu_item)
{
if(menu_item){
menu_item->api = NULL;
free(menu_item);
}
menu_item = NULL;
}
void menu_add_item(CB_MENU **menu_ref)
{
ALLEGRO_CONFIG_SECTION *section = NULL;
ALLEGRO_CONFIG_ENTRY *entry = NULL;
CB_MENU *menu_ptr = *menu_ref;
int count = 0;
al_get_first_config_section(menu_ptr->config->allegro_config, &section);
const char *section_name = NULL;
const char *entry_name = NULL;
int x = 0,y = 0,id = 0;
CB_MENU_PROC_CALLBACK callback = NULL;
while( (section_name = al_get_next_config_section(&section)) != NULL ){
if(section_name == NULL) break;
al_get_first_config_entry(menu_ptr->config->allegro_config, section_name, &entry);
while( (entry_name = al_get_next_config_entry(&entry)) != NULL ){
if(strncmp(entry_name, "id",1) == 0){
id = atoi(al_get_config_value(menu_ptr->config->allegro_config, section_name, entry_name));
continue;
}
if(strncmp(entry_name, "x",1) == 0){
x = atoi(al_get_config_value(menu_ptr->config->allegro_config, section_name, entry_name));
continue;
}
if(strncmp(entry_name, "y",1) == 0){
y = atoi(al_get_config_value(menu_ptr->config->allegro_config, section_name, entry_name));
continue;
}
if(strncmp(entry_name, "command",1) == 0){
const char *command_name = al_get_config_value(menu_ptr->config->allegro_config, section_name, entry_name);
continue;
}
}
if(menu_ptr->items == NULL){
menu_ptr->items = calloc(2,sizeof(CB_MENU_ITEM));
(&menu_ptr->items[0])->x = x;
(&menu_ptr->items[0])->y = y;
(&menu_ptr->items[0])->id = id;
strncpy((&menu_ptr->items[0])->name, section_name,56);
}else {
menu_ptr->items = realloc(menu_ptr->items, (size_t)count + 1);
(&menu_ptr->items[count])->x = x;
(&menu_ptr->items[count])->y = y;
(&menu_ptr->items[count])->id = id;
strncpy((&menu_ptr->items[count])->name, section_name,56);
}
count++;
}
// set last element null?
(&menu_ptr->items)[count + 1] = NULL;
//set count;
menu_ptr->item_count = count;
assert((&menu_ptr->items)[count + 1] == NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.