home · contact · privacy
Added hitpoints and death for player.
[plomrogue] / src / draw_wins.c
1 #include "draw_wins.h"
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <string.h>
5 #include <ncurses.h>
6 #include "windows.h"
7 #include "roguelike.h"
8 #include "keybindings.h"
9 #include "objects_on_map.h"
10
11 static void draw_map_objects (void *, struct Map *, struct Win *);
12
13 extern void draw_with_linebreaks (struct Win * win, char * text, uint16_t start_y) {
14 // Write text into window content space. Start on row start_y. Fill unused rows with whitespace.
15   uint16_t x, y;
16   char toggle;
17   char fin = 0;
18   int16_t z = -1;
19   for (y = start_y; y < win->frame.size.y; y++) {
20     if (0 == fin)
21       toggle = 0;
22     for (x = 0; x < win->frame.size.x; x++) {
23        if (0 == toggle) {
24          z++;
25          if ('\n' == text[z]) {
26            toggle = 1;
27            continue; }
28          else
29            mvwaddch(win->frame.curses_win, y, x, text[z]);
30          if ('\n' == text[z+1]) {
31            z++;
32            toggle = 1; }
33          else if (0 == text[z+1]) {
34             toggle = 1;
35             fin = 1; } } } } }
36
37 extern void draw_text_from_bottom (struct Win * win, char * text) {
38 // Draw text from end/bottom to the top.
39   char toggle = 0;
40   uint16_t x, y, offset;
41   int16_t z = -1;
42   for (y = 0; 0 == toggle; y++)                           // Determine number of lines text would have in
43     for (x = 0; x < win->frame.size.x; x++) {             // a window of available width, but infinite height.
44       z++;
45       if ('\n' == text[z])            // Treat \n and \0 as control characters for incrementing y and stopping
46         break;                        // the loop. Make sure they don't count as cell space themselves.
47       if ('\n' == text[z+1]) {
48         z++;
49         break; }
50       else if (0 == text[z+1]) {
51         toggle = 1;
52         break; } }
53   z = -1;
54   uint16_t start_y = 0;
55   if (y < win->frame.size.y)       // Depending on what is bigger, determine start point in window or in text.
56     start_y = win->frame.size.y - y;
57   else if (y > win->frame.size.y) {
58     offset = y - win->frame.size.y;
59     for (y = 0; y < offset; y++)
60       for (x = 0; x < win->frame.size.x; x++) {
61         z++;
62         if ('\n' == text[z])
63           break;
64         if ('\n' == text[z+1]) {
65           z++;
66           break; } }
67     text = text + (sizeof(char) * (z + 1)); }
68   draw_with_linebreaks(win, text, start_y); }
69
70 extern void draw_log_win (struct Win * win) {
71 // Draw log text from world struct in win->data from bottom to top.
72   struct World * world = (struct World *) win->data;
73   draw_text_from_bottom(win, world->log); }
74
75 static void draw_map_objects (void * start, struct Map * map, struct Win * win) {
76 // Draw onto map in win the objects in the chain at start.
77   struct ChainMapObject * cmo;
78   for (cmo = start; cmo != 0; cmo = cmo->next)
79     if (   cmo->pos.y >= map->offset.y && cmo->pos.y < map->offset.y + win->frame.size.y
80         && cmo->pos.x >= map->offset.x && cmo->pos.x < map->offset.x + win->frame.size.x)
81       mvwaddch(win->frame.curses_win, cmo->pos.y - map->offset.y, cmo->pos.x - map->offset.x, cmo->name); }
82
83 extern void draw_map_win (struct Win * win) {
84 // Draw map determined by map (from win->data) and various actors/objects into window. Respect scroll offset.
85   struct World * world = (struct World *) win->data;
86   struct Map * map = world->map;
87   struct Player * player = world->player;
88   char * cells = map->cells;
89   uint16_t width_map_av  = map->size.x  - map->offset.x;
90   uint16_t height_map_av = map->size.y - map->offset.y;
91   uint16_t x, y, z;
92   for (y = 0; y < win->frame.size.y; y++) {
93     z = map->offset.x + (map->offset.y + y) * (map->size.x);
94     for (x = 0; x < win->frame.size.x; x++) {
95       if (y < height_map_av && x < width_map_av) {
96           mvwaddch(win->frame.curses_win, y, x, cells[z]);
97         z++; } } }
98   draw_map_objects (world->item, map, win);
99   draw_map_objects (world->monster, map, win);
100   if (   player->pos.y >= map->offset.y && player->pos.y < map->offset.y + win->frame.size.y
101       && player->pos.x >= map->offset.x && player->pos.x < map->offset.x + win->frame.size.x)
102     mvwaddch(win->frame.curses_win, player->pos.y - map->offset.y, player->pos.x - map->offset.x, '@'); }
103
104 extern void draw_info_win (struct Win * win) {
105 // Draw info window by appending win->data integer value to "Turn: " display.
106   struct World * world = (struct World *) win->data;
107   char text[100];
108   snprintf(text, 100, "Turn: %d\nHitpoints: %d", world->turn, world->player->hitpoints);
109   draw_with_linebreaks(win, text, 0); }
110
111 extern void draw_keys_win (struct Win * win) {
112 // Draw keybindings window.
113   struct World * world = (struct World *) win->data;
114   uint16_t offset = 0, y, x;
115   if (world->keyswindata->max >= win->frame.size.y) {
116     if (world->keyswindata->select > win->frame.size.y / 2) {
117       if (world->keyswindata->select < (world->keyswindata->max - (win->frame.size.y / 2)))
118         offset = world->keyswindata->select - (win->frame.size.y / 2);
119       else
120         offset = world->keyswindata->max - win->frame.size.y + 1; } }
121   uint8_t keydescwidth = 9 + 1; // max length assured by get_keyname() + \0
122   char * keydesc = malloc(keydescwidth), * keyname;
123   attr_t attri;
124   for (y = 0; y <= world->keyswindata->max && y < win->frame.size.y; y++) {
125     if (0 == y && offset > 0) {
126       draw_scroll_hint(&win->frame, y, offset + 1, '^');
127       continue; }
128     else if (win->frame.size.y == y + 1 && 0 < world->keyswindata->max - (win->frame.size.y + offset - 1)) {
129       draw_scroll_hint(&win->frame, y, world->keyswindata->max - (offset + win->frame.size.y) + 2, 'v');
130       continue; }
131     attri = 0;
132     if (y == world->keyswindata->select - offset) {
133       attri = A_REVERSE;
134       if (1 == world->keyswindata->edit)
135         attri = attri | A_BLINK; }
136     keyname = get_keyname(world->keybindings[y + offset].key);
137     snprintf(keydesc, keydescwidth, "%-9s", keyname);
138     free(keyname);
139     for (x = 0; x < win->frame.size.x; x++)
140       if (x < strlen(keydesc))
141         mvwaddch(win->frame.curses_win, y, x, keydesc[x] | attri);
142       else if (strlen(keydesc) < x && x < strlen(world->keybindings[y + offset].name) + strlen(keydesc) + 1)
143         mvwaddch(win->frame.curses_win, y, x,
144                  world->keybindings[y + offset].name[x - strlen(keydesc) - 1] | attri);
145       else
146         mvwaddch(win->frame.curses_win, y, x, ' ' | attri); }
147   free(keydesc); }