-void draw_text_from_bottom (struct Win * win) {
-// Draw text in win->data from end/bottom to the top.
- char * text = (char *) win->data;
+void draw_text_from_bottom (struct Win * win, char * text) {
+// Draw text from end/bottom to the top.
- for (y = 0; y < start_y; y++)
- for (x = 0; x < win->width; x++)
- mvwaddch(win->curses_win, y, x, ' '); }
else if (y > win->height) {
offset = y - win->height;
for (y = 0; y < offset; y++)
else if (y > win->height) {
offset = y - win->height;
for (y = 0; y < offset; y++)
text = text + (sizeof(char) * (z + 1)); }
draw_with_linebreaks(win, text, start_y); }
text = text + (sizeof(char) * (z + 1)); }
draw_with_linebreaks(win, text, start_y); }
+void draw_log (struct Win * win) {
+// Draw log text from world struct in win->data from bottom to top.
+ struct World world = * (struct World *) win->data;
+ draw_text_from_bottom(win, world.log); }
+
void draw_map (struct Win * win) {
// Draw map determined by win->data Map struct into window. Respect offset.
struct Map map = * (struct Map *) win->data;
void draw_map (struct Win * win) {
// Draw map determined by win->data Map struct into window. Respect offset.
struct Map map = * (struct Map *) win->data;
mvwaddch(win->curses_win, y, x, '@');
else
mvwaddch(win->curses_win, y, x, cells[z]);
mvwaddch(win->curses_win, y, x, '@');
else
mvwaddch(win->curses_win, y, x, cells[z]);
void draw_info (struct Win * win) {
// Draw info window by appending win->data integer value to "Turn: " display.
void draw_info (struct Win * win) {
// Draw info window by appending win->data integer value to "Turn: " display.
char text[100];
snprintf(text, 100, "Turn: %d", count);
draw_with_linebreaks(win, text, 0); }
char text[100];
snprintf(text, 100, "Turn: %d", count);
draw_with_linebreaks(win, text, 0); }
-void update_log (struct Win * win, char * text) {
+//void update_log (struct Win * win, char * text) {
+void update_log (struct World * world, char * text) {
int len_new = strlen(text);
int len_whole = len_old + len_new + 1;
new_text = calloc(len_whole, sizeof(char));
int len_new = strlen(text);
int len_whole = len_old + len_new + 1;
new_text = calloc(len_whole, sizeof(char));
int get_action_key (struct KeyBinding * keybindings, char * name) {
// Return key matching name in keybindings.
int get_action_key (struct KeyBinding * keybindings, char * name) {
// Return key matching name in keybindings.
if (keyswindata->select < (keyswindata->max - (win->height / 2)))
offset = keyswindata->select - (win->height / 2);
else
if (keyswindata->select < (keyswindata->max - (win->height / 2)))
offset = keyswindata->select - (win->height / 2);
else
- offset = keyswindata->max - win->height + 1; }
- int keydescwidth = 9; // max length assured by get_keyname()
- char * keydesc = malloc(keydescwidth + 1);
+ offset = keyswindata->max - win->height + 1; } }
+ int keydescwidth = 9 + 1; // max length assured by get_keyname() + \0
+ char * keydesc = malloc(keydescwidth);
attri = 0;
if (y == keyswindata->select - offset) {
attri = A_REVERSE;
if (1 == keyswindata->edit)
attri = attri | A_BLINK; }
keyname = get_keyname(keybindings[y + offset].key);
attri = 0;
if (y == keyswindata->select - offset) {
attri = A_REVERSE;
if (1 == keyswindata->edit)
attri = attri | A_BLINK; }
keyname = get_keyname(keybindings[y + offset].key);
- snprintf(keydesc, keydescwidth + 1, "%-9s", keyname);
+ snprintf(keydesc, keydescwidth, "%-9s", keyname);
mvwaddch(win->curses_win, y, x, keydesc[x] | attri);
else if (strlen(keydesc) < x && x < strlen(keybindings[y + offset].name) + strlen(keydesc) + 1)
mvwaddch(win->curses_win, y, x, keybindings[y + offset].name[x - strlen(keydesc) - 1] | attri);
else
mvwaddch(win->curses_win, y, x, ' ' | attri); }
mvwaddch(win->curses_win, y, x, keydesc[x] | attri);
else if (strlen(keydesc) < x && x < strlen(keybindings[y + offset].name) + strlen(keydesc) + 1)
mvwaddch(win->curses_win, y, x, keybindings[y + offset].name[x - strlen(keydesc) - 1] | attri);
else
mvwaddch(win->curses_win, y, x, ' ' | attri); }
- free(keydesc);
- if (y < win->height)
- for (; y < win->height; y++)
- for (x = 0; x < win->width; x++)
- mvwaddch(win->curses_win, y, x, ' '); }
struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding));
fseek(file, 0, SEEK_SET);
char * command = malloc(linemax);
struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding));
fseek(file, 0, SEEK_SET);
char * command = malloc(linemax);
memcpy(keybindings[keycount].name, command, commcount);
keybindings[keycount].key = key;
keycount++;
memcpy(keybindings[keycount].name, command, commcount);
keybindings[keycount].key = key;
keycount++;
struct Win win_info = init_window(&win_meta, "Info");
win_info.draw = draw_info;
struct Win win_info = init_window(&win_meta, "Info");
win_info.draw = draw_info;
- win_log.draw = draw_text_from_bottom;
- win_log.data = calloc(1, sizeof(char));
- update_log (&win_log, "Start!");
+ win_log.draw = draw_log;
+ win_log.data = &world;
+ update_log (&world, "Start!");
else if (key == get_action_key(world.keybindings, "map left") && map.offset_x > 0)
map.offset_x--;
else if (key == get_action_key(world.keybindings, "player down") && map.player_y < map.height - 1) {
else if (key == get_action_key(world.keybindings, "map left") && map.offset_x > 0)
map.offset_x--;
else if (key == get_action_key(world.keybindings, "player down") && map.player_y < map.height - 1) {
- update_info (&win_info);
- update_log (&win_log, "\nYou wait."); } }
+ update_info (&world);
+ update_log (&world, "\nYou wait."); } }
+
+ free(map.cells);
+ for (key = 0; key <= world.keyswindata->max; key++)
+ free(world.keybindings[key].name);
+ free(world.keybindings);
+ free(world.keyswindata);
+ free(world.log);