From: Christian Heller Date: Sat, 25 May 2013 00:31:07 +0000 (+0200) Subject: Use stdint.h typedefs for ints. X-Git-Tag: tce~1278 X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/static/index.html?a=commitdiff_plain;h=cff3e45e2913b44b88bb75ed10a4931a224a943e;p=plomrogue Use stdint.h typedefs for ints. --- diff --git a/draw_wins.c b/draw_wins.c index 9b09593..acac2a5 100644 --- a/draw_wins.c +++ b/draw_wins.c @@ -1,17 +1,18 @@ +#include +#include #include #include -#include #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; @@ -33,8 +34,8 @@ void draw_with_linebreaks (struct Win * win, char * text, int start_y) { 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++; @@ -47,7 +48,7 @@ void draw_text_from_bottom (struct Win * win, char * text) { 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) { @@ -75,9 +76,9 @@ void draw_map_win (struct Win * win) { 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++) { @@ -93,7 +94,7 @@ void draw_map_win (struct Win * win) { 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); } @@ -103,17 +104,17 @@ void draw_keys_win (struct Win * win) { 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; diff --git a/draw_wins.h b/draw_wins.h index 39c5ce9..64f332c 100644 --- a/draw_wins.h +++ b/draw_wins.h @@ -1,4 +1,4 @@ -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 *); diff --git a/keybindings.c b/keybindings.c index 1906d1d..3e1b19a 100644 --- a/keybindings.c +++ b/keybindings.c @@ -1,6 +1,7 @@ +#include +#include #include #include -#include #include "windows.h" #include "roguelike.h" #include "keybindings.h" @@ -8,10 +9,10 @@ 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); @@ -23,7 +24,7 @@ void init_keybindings(struct World * world) { 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); @@ -46,8 +47,8 @@ void save_keybindings(struct World * world) { 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); @@ -59,14 +60,14 @@ void save_keybindings(struct World * world) { 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); @@ -93,7 +94,7 @@ char * get_keyname(int keycode) { 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"); diff --git a/keybindings.h b/keybindings.h index 3295f2d..b0ff912 100644 --- a/keybindings.h +++ b/keybindings.h @@ -1,15 +1,15 @@ 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); diff --git a/roguelike.c b/roguelike.c index 20181be..532ae70 100644 --- a/roguelike.c +++ b/roguelike.c @@ -1,6 +1,7 @@ +#include +#include #include #include -#include #include "windows.h" #include "draw_wins.h" #include "roguelike.h" @@ -16,8 +17,8 @@ void toggle_window (struct WinMeta * win_meta, struct Win * win) { 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 == '+') @@ -36,7 +37,7 @@ struct Map init_map () { 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++) { @@ -84,16 +85,16 @@ void next_turn (struct World * world) { 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) diff --git a/roguelike.h b/roguelike.h index da80489..668d52c 100644 --- a/roguelike.h +++ b/roguelike.h @@ -1,26 +1,26 @@ 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); @@ -28,6 +28,6 @@ struct Map init_map (); 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 *); diff --git a/windows.c b/windows.c index 6a649a3..cf64b4e 100644 --- a/windows.c +++ b/windows.c @@ -1,4 +1,5 @@ #include +#include #include #include #include "windows.h" @@ -76,14 +77,14 @@ struct yx place_window (struct WinMeta * win_meta, struct Win * win) { 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) { @@ -104,7 +105,7 @@ void update_windows (struct WinMeta * win_meta, struct Win * win) { 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) @@ -125,7 +126,7 @@ void destroy_window (struct Win * win) { 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, '|'); } @@ -134,10 +135,10 @@ void draw_window_borders (struct Win * win, char active) { 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) @@ -147,7 +148,7 @@ void draw_window_borders (struct Win * win, char 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) @@ -176,7 +177,7 @@ void draw_all_windows (struct WinMeta * win_meta) { 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; @@ -184,7 +185,7 @@ void draw_all_windows (struct WinMeta * win_meta) { 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, '+'); @@ -194,7 +195,7 @@ void draw_all_windows (struct WinMeta * win_meta) { 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; @@ -218,12 +219,12 @@ void cycle_active_window (struct WinMeta * win_meta, char dir) { 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; diff --git a/windows.h b/windows.h index b5df498..904cc9f 100644 --- a/windows.h +++ b/windows.h @@ -1,26 +1,26 @@ 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; @@ -37,9 +37,9 @@ struct yx place_window (struct WinMeta *, struct Win *); 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);