minor fixes + upgrades
[my-code/api.git] / display / display.c
1 /* display.c -- display management stuff
2  *
3  * hackbard@hackdaworld.dyndns.org
4  *
5  */
6
7 #include "display.h"
8
9 #define USE_NCURSES
10
11 int display_init(t_display *display,int outfd) {
12
13   struct winsize ws;
14
15   /* dimensions */
16   if(ioctl(1,TIOCGWINSZ,&ws)==-1) {
17     perror("[display] ioctl call");
18     return D_ERROR;
19   }
20   display->max_x=ws.ws_col;
21   display->max_y=ws.ws_row;
22
23   display->outfd=outfd;
24
25   dprintf(display->outfd,"[display] initializing display, w: %02d / h: %02d\n",
26           ws.ws_col,ws.ws_row);
27
28   /* allocating 'screen' buffer */
29   if((display->screen=(char *)malloc(display->max_x*display->max_y))
30      ==NULL) {
31     perror("[display] malloc call");
32     return D_ERROR;
33   }
34   /* space as display pixel default */
35   memset(display->screen,0x20,display->max_x*display->max_y);
36
37 #ifdef USE_NCURSES
38   initscr();
39   nonl();
40   noecho();
41   cbreak();
42   curs_set(0);
43 #endif
44
45   return D_SUCCESS;
46 }
47
48 int display_draw(t_display *display) {
49
50   int x,y;
51
52 #ifdef USE_NCURSES
53   mvprintw(0,0,"%s",display->screen);
54   for(y=0;y<display->max_y;y++)
55     for(x=0;x<display->max_x;x++)
56       mvaddch(y,x,*(display->screen+y*display->max_x+x));
57   refresh();
58 #else
59   for(y=0;y<display->max_y;y++) {
60     for(x=0;x<display->max_x;x++)
61       fprintf(stderr,"%c",*(display->screen+y*display->max_x+x));
62     fprintf(stderr,"\n");
63   }
64 #endif
65
66   return D_SUCCESS;
67 }
68
69 int display_draw_until_line(t_display *display,int line) {
70   
71   int x,y;
72
73 #ifdef USE_NCURSES
74   for(y=0;y<line;y++) {
75     for(x=0;x<display->max_x;x++)
76       mvaddch(y,x,*(display->screen+y*display->max_x+x));
77   refresh();
78   }
79 #else
80   for(y=0;y<line;y++) {
81     for(x=0;x<display->max_x;x++) 
82       fprintf(stderr,"%c",*(display->screen+y*display->max_x+x));
83     fprintf(stderr,"\n");
84   }
85 #endif
86
87   return D_SUCCESS;
88 }
89
90 int display_set_cursor(t_display *display,int x,int y) {
91
92 #ifdef USE_NCURSES
93   move(y,x);
94   refresh();
95 #endif
96
97   return D_SUCCESS;
98 }
99
100 int display_clear_screen(t_display *display) {
101  
102   memset(display->screen,0x20,display->max_x*display->max_y);
103
104   return D_SUCCESS;
105 }
106
107 int display_shutdown(t_display *display) {
108
109 #ifdef USE_NCURSES
110   endwin();
111 #endif
112
113   free(display->screen);
114
115   dprintf(display->outfd,"[display] shutdown\n");
116
117   return D_SUCCESS;
118 }
119
120 int display_line(t_display *display,int X,int Y,int X_,int Y_,char sym) {
121
122   double m;
123   int x,y;
124
125   m=(Y_-Y)/(X_-X);
126
127   for(y=0;y<display->max_y;y++)
128     for(x=0;x<display->max_x;x++)
129       if((int)((x-X)*m+Y)==y) *(display->screen+y*display->max_x+x)=sym;
130
131   return D_SUCCESS;
132 }
133
134 int display_string(t_display *display,int x,int y,char *string,int len) {
135
136   if(len>display->max_x-x) return D_INV_STRING_LEN;
137
138   memcpy(display->screen+y*display->max_x+x,string,len);
139
140   return D_SUCCESS;
141 }
142
143 int display_string_vert(t_display *display,int x,int y,char *string,int len) {
144
145   int i;
146
147   if(len>display->max_y-y) return D_INV_STRING_LEN;
148   for(i=y*display->max_x+x;i<(y+len)*display->max_x+x;i+=display->max_x)
149     *(display->screen+i)=*(string++);
150
151   return D_SUCCESS;
152 }