+#include <stdlib.h>
+#include <stdint.h>
#include <ncurses.h>
#include <string.h>
-#include <stdlib.h>
#include "windows.h"
#include "draw_wins.h"
#include "roguelike.h"
#include "keybindings.h"
-void draw_with_linebreaks (struct Win * win, char * text, int start_y) {
+void draw_with_linebreaks (struct Win * win, char * text, uint16_t start_y) {
// Write text into window content space. Start on row start_y. Fill unused rows with whitespace.
- int x, y;
+ uint16_t x, y;
char toggle;
char fin = 0;
- int z = -1;
+ int16_t z = -1;
for (y = start_y; y < win->height; y++) {
if (0 == fin)
toggle = 0;
void draw_text_from_bottom (struct Win * win, char * text) {
// Draw text from end/bottom to the top.
char toggle = 0;
- int x, y, offset;
- int z = -1;
+ uint16_t x, y, offset;
+ int16_t z = -1;
for (y = 0; 0 == toggle; y++) // Determine number of lines text would have in
for (x = 0; x < win->width; x++) { // a window of available width, but infinite height.
z++;
toggle = 1;
break; } }
z = -1;
- int start_y = 0;
+ uint16_t start_y = 0;
if (y < win->height) // Depending on what is bigger, determine start point in window or in text.
start_y = win->height - y;
else if (y > win->height) {
struct Player * player = world->player;
struct Monster * monster = world->monster;
char * cells = map->cells;
- int width_map_av = map->width - map->offset_x;
- int height_map_av = map->height - map->offset_y;
- int x, y, z;
+ uint16_t width_map_av = map->width - map->offset_x;
+ uint16_t height_map_av = map->height - map->offset_y;
+ uint16_t x, y, z;
for (y = 0; y < win->height; y++) {
z = map->offset_x + (map->offset_y + y) * (map->width);
for (x = 0; x < win->width; x++) {
void draw_info_win (struct Win * win) {
// Draw info window by appending win->data integer value to "Turn: " display.
struct World * world = (struct World *) win->data;
- int count = world->turn;
+ uint16_t count = world->turn;
char text[100];
snprintf(text, 100, "Turn: %d", count);
draw_with_linebreaks(win, text, 0); }
struct World * world = (struct World *) win->data;
struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata;
struct KeyBinding * keybindings = world->keybindings;
- int offset = 0;
+ uint16_t offset = 0;
if (keyswindata->max >= win->height) {
if (keyswindata->select > win->height / 2) {
if (keyswindata->select < (keyswindata->max - (win->height / 2)))
offset = keyswindata->select - (win->height / 2);
else
offset = keyswindata->max - win->height + 1; } }
- int keydescwidth = 9 + 1; // max length assured by get_keyname() + \0
+ uint8_t keydescwidth = 9 + 1; // max length assured by get_keyname() + \0
char * keydesc = malloc(keydescwidth);
attr_t attri;
- int y, x;
+ uint16_t y, x;
char * keyname;
for (y = 0; y <= keyswindata->max && y < win->height; y++) {
attri = 0;
-void draw_with_linebreaks (struct Win *, char *, int);
+void draw_with_linebreaks (struct Win *, char *, uint16_t);
void draw_text_from_bottom (struct Win *, char *);
void draw_log_win (struct Win *);
void draw_map_win (struct Win *);
+#include <stdlib.h>
+#include <stdint.h>
#include <ncurses.h>
#include <string.h>
-#include <stdlib.h>
#include "windows.h"
#include "roguelike.h"
#include "keybindings.h"
void init_keybindings(struct World * world) {
// Initialize keybindings from file "keybindings".
FILE * file = fopen("keybindings", "r");
- int lines = 0;
+ uint16_t lines = 0;
int c = 0;
- int linemax = 0;
- int c_count = 0;
+ uint16_t linemax = 0;
+ uint16_t c_count = 0;
while (EOF != c) {
c_count++;
c = getc(file);
struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding));
fseek(file, 0, SEEK_SET);
char * command = malloc(linemax);
- int commcount = 0;
+ uint16_t commcount = 0;
char * cmdptr;
while (fgets(command, linemax, file)) {
keybindings[commcount].key = atoi(command);
struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata;
struct KeyBinding * keybindings = world->keybindings;
FILE * file = fopen("keybindings", "w");
- int linemax = 0;
- int i;
+ uint16_t linemax = 0;
+ uint16_t i;
for (i = 0; i <= keyswindata->max; i++)
if (strlen(keybindings[i].name) > linemax)
linemax = strlen(keybindings[i].name);
free(line);
fclose(file); }
-int get_action_key (struct KeyBinding * keybindings, char * name) {
+uint16_t get_action_key (struct KeyBinding * keybindings, char * name) {
// Return key matching name in keybindings.
- int i = 0;
+ uint16_t i = 0;
while (strcmp(keybindings[i].name, name) )
i++;
return keybindings[i].key; }
-char * get_keyname(int keycode) {
+char * get_keyname(uint16_t keycode) {
// Translate some keycodes to readable names of max 9 chars.
char * keyname;
keyname = malloc(15);
else if (keycode == KEY_BACKSPACE)
sprintf(keyname, "BACKSPACE");
else if (keycode >= KEY_F0 && keycode <= KEY_F(63)) {
- int f = keycode - KEY_F0;
+ uint16_t f = keycode - KEY_F0;
sprintf(keyname, "F%d", f); }
else if (keycode == KEY_DC)
sprintf(keyname, "DELETE");
struct KeyBinding {
char * name;
- int key; };
+ uint16_t key; };
struct KeysWinData {
- int max;
+ uint16_t max;
char edit;
- int select; };
+ uint16_t select; };
void init_keybindings(struct World *);
void save_keybindings(struct World *);
-int get_action_key (struct KeyBinding *, char *);
-char * get_keyname(int);
+uint16_t get_action_key (struct KeyBinding *, char *);
+char * get_keyname(uint16_t);
void keyswin_mod_key (struct World *, struct WinMeta *);
void keyswin_move_selection (struct World *, char);
+#include <stdlib.h>
+#include <stdint.h>
#include <ncurses.h>
#include <string.h>
-#include <stdlib.h>
#include "windows.h"
#include "draw_wins.h"
#include "roguelike.h"
void growshrink_active_window (struct WinMeta * win_meta, char change) {
// Grow or shrink active window horizontally or vertically by one cell size.
if (0 != win_meta->active) {
- int height = win_meta->active->height;
- int width = win_meta->active->width;
+ uint16_t height = win_meta->active->height;
+ uint16_t width = win_meta->active->width;
if (change == '-')
height--;
else if (change == '+')
map.offset_x = 0;
map.offset_y = 0;
map.cells = malloc(map.width * map.height);
- int x, y, ran;
+ uint16_t x, y, ran;
char terrain;
for (y = 0; y < map.height; y++)
for (x = 0; x < map.width; x++) {
void update_log (struct World * world, char * text) {
// Update log with new text to be appended.
char * new_text;
- int len_old = strlen(world->log);
- int len_new = strlen(text);
- int len_whole = len_old + len_new + 1;
+ uint16_t len_old = strlen(world->log);
+ uint16_t len_new = strlen(text);
+ uint16_t len_whole = len_old + len_new + 1;
new_text = calloc(len_whole, sizeof(char));
memcpy(new_text, world->log, len_old);
memcpy(new_text + len_old, text, len_new);
free(world->log);
world->log = new_text; }
-char is_passable (struct World * world, int x, int y) {
+char is_passable (struct World * world, uint16_t x, uint16_t y) {
// Check if coordinate on (or beyond) map is accessible to movement.
char passable = 0;
if (0 <= x && x < world->map->width && 0 <= y && y < world->map->height)
struct World {
struct KeyBinding * keybindings;
struct KeysWinData * keyswindata;
- int turn;
+ uint16_t turn;
char * log;
struct Map * map;
struct Monster * monster;
struct Player * player; };
struct Map {
- int width;
- int height;
- int offset_x;
- int offset_y;
+ uint16_t width;
+ uint16_t height;
+ uint16_t offset_x;
+ uint16_t offset_y;
char * cells; };
struct Player {
- int y;
- int x; };
+ uint16_t y;
+ uint16_t x; };
struct Monster {
- int y;
- int x; };
+ uint16_t y;
+ uint16_t x; };
void toggle_window (struct WinMeta *, struct Win *);
void growshrink_active_window (struct WinMeta *, char);
void map_scroll (struct Map *, char);
void next_turn (struct World *);
void update_log (struct World *, char *);
-char is_passable (struct World *, int, int);
+char is_passable (struct World *, uint16_t, uint16_t);
void move_player (struct World *, char);
void player_wait(struct World *);
#include <stdlib.h>
+#include <stdint.h>
#include <ncurses.h>
#include <string.h>
#include "windows.h"
while (getbegy(win_top->curses) != 1)
win_top = win_top->prev; // else, default to placing window in new top
start.x = getbegx(win_top->curses) + win_top->width + 1; // column to the right of the last one
- int winprev_maxy = getbegy(win->prev->curses) + getmaxy(win->prev->curses);
+ uint16_t winprev_maxy = getbegy(win->prev->curses) + getmaxy(win->prev->curses);
if (win->width <= win->prev->width && win->height < win_meta->height - winprev_maxy) {
start.x = getbegx(win->prev->curses); // place window below previous window if it fits
start.y = winprev_maxy + 1; } // vertically and is not wider than its predecessor
else {
struct Win * win_up = win->prev;
struct Win * win_upup = win_up;
- int widthdiff;
+ uint16_t widthdiff;
while (win_up != win_top) {
win_upup = win_up->prev;
while (1) {
if (0 != win->curses)
destroy_window (win);
struct yx startyx = place_window(win_meta, win);
- int lastwincol = 0;
+ uint16_t lastwincol = 0;
struct Win * win_p = win_meta->chain_start;
while (win_p != 0) {
if (win_p != win && getbegx(win_p->curses) + win_p->width > lastwincol + 1)
void draw_window_borders (struct Win * win, char active) {
// Draw borders of window win, including title. Decorate in a special way if window is marked as active.
- int y, x;
+ uint16_t y, x;
for (y = getbegy(win->curses); y <= getbegy(win->curses) + win->height; y++) {
mvwaddch(wgetparent(win->curses), y, getbegx(win->curses) - 1, '|');
mvwaddch(wgetparent(win->curses), y, getbegx(win->curses) + win->width, '|'); }
mvwaddch(wgetparent(win->curses), getbegy(win->curses) + win->height, x, '-'); }
char min_title_length_visible = 3; // 1 char minimal, plus 2 chars for decoration left/right of title
if (win->width >= min_title_length_visible) {
- int title_offset = 0;
+ uint16_t title_offset = 0;
if (win->width > strlen(win->title) + 2)
title_offset = (win->width - (strlen(win->title) + 2)) / 2; // + 2 is for decoration
- int length_visible = strnlen(win->title, win->width - 2);
+ uint16_t length_visible = strnlen(win->title, win->width - 2);
char title[length_visible + 3];
char decoration = ' ';
if (1 == active)
title[length_visible + 2] = '\0';
mvwaddstr (wgetparent(win->curses), getbegy(win->curses)-1, getbegx(win->curses)+title_offset, title); } }
-void draw_windows_borders (struct Win * win, struct Win * win_active, struct Corners * corners, int ccount) {
+void draw_windows_borders (struct Win * win, struct Win * win_active, struct Corners * corners, uint16_t ccount) {
// Craw draw_window_borders() for all windows in chain from win on. Save current window's border corners.
char active = 0;
if (win == win_active)
wnoutrefresh(win_meta->screen);
werase(win_meta->pad);
if (win_meta->chain_start) {
- int n_wins = 1;
+ uint16_t n_wins = 1;
struct Win * win_p = win_meta->chain_start;
while (0 != win_p->next) {
win_p = win_p->next;
struct Corners * all_corners = malloc(sizeof(struct Corners) * n_wins);
draw_windows (win_meta->chain_start);
draw_windows_borders (win_meta->chain_start, win_meta->active, all_corners, 0);
- int i;
+ uint16_t i;
for (i = 0; i < n_wins; i++) {
mvwaddch(win_meta->pad, all_corners[i].tl.y, all_corners[i].tl.x, '+');
mvwaddch(win_meta->pad, all_corners[i].tr.y, all_corners[i].tr.x, '+');
free(all_corners); }
doupdate(); }
-void resize_active_window (struct WinMeta * win_meta, int height, int width) {
+void resize_active_window (struct WinMeta * win_meta, uint16_t height, uint16_t width) {
// Grow or shrink currently active window. Correct its geometry and that of its followers.
if (0 != win_meta->active && width > 0 && height > 0 && height < win_meta->height) {
win_meta->active->height = height;
void shift_active_window (struct WinMeta * win_meta, char dir) {
// Move active window forward/backward in window chain. If jumping beyond start/end, move to other chain end.
if (0 != win_meta->active && win_meta->chain_start != win_meta->chain_end && (dir == 'f' || dir == 'b')) {
- int i, i_max;
struct Win * win_shift = win_meta->active;
char wrap = 0;
if ((dir == 'f' && win_shift == win_meta->chain_end)
|| (dir == 'b' && win_shift == win_meta->chain_start))
wrap = 1;
+ uint16_t i, i_max;
struct Win * win_p, * win_p_next;
for (i_max = 1, win_p = win_meta->chain_start; win_p != win_meta->chain_end; i_max++)
win_p = win_p->next;
struct WinMeta {
WINDOW * screen;
WINDOW * pad;
- int pad_offset;
+ uint16_t pad_offset;
struct Win * chain_start;
struct Win * chain_end;
struct Win * active;
- int width;
- int height; };
+ uint16_t width;
+ uint16_t height; };
struct Win {
struct Win * prev;
struct Win * next;
- int width;
- int height;
+ uint16_t width;
+ uint16_t height;
WINDOW * curses;
char * title;
void (* draw) (struct Win *);
void * data; };
struct yx {
- int y;
- int x; };
+ uint16_t y;
+ uint16_t x; };
struct Corners {
struct yx tl;
void update_windows (struct WinMeta *, struct Win *);
void destroy_window (struct Win *);
void draw_windows (struct Win *);
-void draw_windows_borders (struct Win *, struct Win *, struct Corners *, int);
+void draw_windows_borders (struct Win *, struct Win *, struct Corners *, uint16_t);
void draw_window_borders (struct Win *, char);
void draw_all_windows (struct WinMeta *);
-void resize_active_window (struct WinMeta *, int, int);
+void resize_active_window (struct WinMeta *, uint16_t, uint16_t);
void cycle_active_window (struct WinMeta *, char);
void shift_active_window (struct WinMeta *, char);