4f1fbb2fae822d6754d89d978d51220ea1c61056
[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 #ifndef USE_NCURSES
51   int x,y;
52 #endif
53
54 #ifdef USE_NCURSES
55   mvprintw(0,0,"%s",display->screen);
56   //for(y=0;y<display->max_y;y++)
57   //  for(x=0;x<display->max_x;x++)
58   //    mvaddch(y,x,*(display->screen+y*display->max_x+x));
59   refresh();
60 #else
61   for(y=0;y<display->max_y;y++) {
62     for(x=0;x<display->max_x;x++)
63       fprintf(stderr,"%c",*(display->screen+y*display->max_x+x));
64     fprintf(stderr,"\n");
65   }
66 #endif
67
68   return D_SUCCESS;
69 }
70
71 int display_draw_until_line(t_display *display,int line) {
72   
73   int x,y;
74
75 #ifdef USE_NCURSES
76   for(y=0;y<line;y++) {
77     for(x=0;x<display->max_x;x++)
78       mvaddch(y,x,*(display->screen+y*display->max_x+x));
79   refresh();
80   }
81 #else
82   for(y=0;y<line;y++) {
83     for(x=0;x<display->max_x;x++) 
84       fprintf(stderr,"%c",*(display->screen+y*display->max_x+x));
85     fprintf(stderr,"\n");
86   }
87 #endif
88
89   return D_SUCCESS;
90 }
91
92 int display_set_cursor(t_display *display,int x,int y) {
93
94 #ifdef USE_NCURSES
95   move(y,x);
96   refresh();
97 #endif
98
99   return D_SUCCESS;
100 }
101
102 int display_clear_screen(t_display *display) {
103  
104   memset(display->screen,0x20,display->max_x*display->max_y);
105
106   return D_SUCCESS;
107 }
108
109 int display_shutdown(t_display *display) {
110
111 #ifdef USE_NCURSES
112   endwin();
113 #endif
114
115   free(display->screen);
116
117   dprintf(display->outfd,"[display] shutdown\n");
118
119   return D_SUCCESS;
120 }
121
122 int display_line(t_display *display,int X,int Y,int X_,int Y_,char sym) {
123
124   double m;
125   int x,y;
126
127   m=(Y_-Y)/(X_-X);
128
129   for(x=X;x<=X_;x++) {
130     y=(x-X)*m+Y;
131     *(display->screen+y*display->max_x+x)=sym;
132   }
133
134   return D_SUCCESS;
135 }
136
137 int display_string(t_display *display,int x,int y,char *string,int len) {
138
139   if(len>display->max_x-x) return D_INV_STRING_LEN;
140
141   memcpy(display->screen+y*display->max_x+x,string,len);
142
143   return D_SUCCESS;
144 }
145
146 int display_string_vert(t_display *display,int x,int y,char *string,int len) {
147
148   int i;
149
150   if(len>display->max_y-y) return D_INV_STRING_LEN;
151   for(i=y*display->max_x+x;i<(y+len)*display->max_x+x;i+=display->max_x)
152     *(display->screen+i)=*(string++);
153
154   return D_SUCCESS;
155 }