source: abuse/trunk/src/console.cpp @ 56

Last change on this file since 56 was 56, checked in by Sam Hocevar, 11 years ago
  • Add licensing terms to most C / C++ files (Ref #5).
File size: 3.9 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *
5 *  This software was released into the Public Domain. As with most public
6 *  domain software, no warranty is made or implied by Crack dot Com or
7 *  Jonathan Clark.
8 */
9
10#include "config.h"
11
12#include <ctype.h>
13#include <stdarg.h>
14
15#include "linked.hpp"
16#include "console.hpp"
17#include "jmalloc.hpp"
18
19extern window_manager *eh;
20
21void console::put_string(char const *st)
22{
23  while (*st)
24  {
25    put_char(*st);
26    st++;
27  }
28}
29
30void console::redraw()
31{
32  if (con_win)
33  {
34    con_win->clear();
35    char *s=screen;
36    int dx,dy,xa=fnt->width(),ya=fnt->height(),i,j;
37    for (j=0,dy=wy();j<h;j++,dy+=ya)
38    {
39      for (i=0,dx=wx();i<w;i++,s++,dx+=xa)
40      {
41        if (*s)
42          fnt->put_char(con_win->screen,dx,dy,*s);
43      }
44    }
45    fnt->put_char(con_win->screen,wx()+cx*xa,wy()+cy*ya,'_');
46  }
47}
48
49void console::show()
50{
51  if (!con_win)
52  {
53    con_win=wm->new_window(lastx,lasty,screen_w(),screen_h(),NULL,name);
54    redraw();
55    con_win->screen->set_clip(con_win->x1(),con_win->y1(),con_win->x2(),con_win->y2());
56  }
57}
58
59void console::hide()
60{
61  if (con_win)
62  {
63    lastx=con_win->x;
64    lasty=con_win->y;
65    wm->close_window(con_win);
66    con_win=NULL;
67  }
68}
69
70console::~console()
71{
72  hide();
73  jfree(screen);
74  jfree(name);
75}
76
77console::console(window_manager *WM, JCFont *font, int width, int height, char const *Name)
78{
79  wm=WM;
80  con_win=NULL;
81  w=width;
82  h=height;
83  screen=(char *)jmalloc(w*h,"console screen");
84  memset(screen,' ',w*h);
85  cx=cy=0;
86  fnt=font;
87  lastx=xres/2-screen_w()/2;
88  lasty=yres/2-screen_h()/2;
89  name=(char *)strcpy((char *)jmalloc(strlen(Name)+1,"console name"),Name);
90}
91
92
93void console::draw_cursor()
94{
95  if (con_win)
96    fnt->put_char(con_win->screen,cx*fnt->width()+wx(),cy*fnt->height()+wy(),'_');
97}
98
99
100void console::draw_char(int x, int y, char ch)
101{
102  if (con_win)
103  {
104    int fw=fnt->width(),fh=fnt->height();
105    int dx=wx()+x*fw,dy=wy()+y*fh;
106    con_win->screen->bar(dx,dy,dx+fw-1,dy+fh-1,wm->black());
107    fnt->put_char(con_win->screen,dx,dy,ch);
108  }
109}
110
111void console::do_cr()
112{
113  if (cx<w && cy<h)  draw_char(cx,cy,screen[cy*w+cx]);
114  cx=0;
115  cy++; 
116  if (cy>=h)
117  {
118    cy=h-1;
119    if (con_win)
120    {
121      memmove(screen,screen+w,w*(h-1));
122      memset(screen+w*(h-1),' ',w);
123      redraw();
124      eh->flush_screen();
125    }
126  } else draw_cursor();   
127}
128
129void console::put_char(char ch)
130{
131
132 
133  switch (ch)
134  {
135    case JK_BACKSPACE :
136    {
137      if (cx)
138      {
139        if (con_win)
140          draw_char(cx,cy,screen[cy*w+cx]);
141        cx--;
142        if (con_win)
143          draw_cursor();
144      }
145    } break;
146    case '\n' :
147    case JK_ENTER :
148    {
149      do_cr();
150    } break;
151    default :
152    {
153      screen[cy*w+cx]=ch;
154      if (con_win)
155        draw_char(cx,cy,ch);
156      cx++;
157      if (cx>=w) do_cr(); else
158      if (con_win) draw_cursor();
159    }
160  } 
161}
162
163
164void console::print_f( const char *format, ...)
165{
166  char st[300];
167  va_list ap;
168  va_start(ap, format);
169  vsprintf(st,format,ap);
170  va_end(ap);
171  put_string(st);
172}
173
174
175shell_term::shell_term(window_manager *WM, JCFont *font, int width, int height, char const *Name) :
176  console(WM,font,width,height,Name)
177{
178  shcmd[0]=0;
179  prompt();
180}
181
182void shell_term::prompt()
183{
184  put_string("(?=help)>");
185}
186
187void shell_term::execute(char const *st)
188{
189  put_string(st);
190  put_string(" : unhandled\n");
191}
192
193int shell_term::handle_event(event &ev, window_manager *wm)
194{
195  if (ev.window==con_win && con_win)
196  {
197    switch (ev.type)
198    {
199      case EV_KEY :
200      {
201        switch (ev.key)
202        {
203          case JK_BACKSPACE:
204          {
205            if (shcmd[0]!=0)
206            {
207              shcmd[strlen(shcmd)-1]=0;
208              put_char(ev.key);
209            }
210          } break;
211          case JK_ENTER :
212          {
213            put_char(ev.key);
214            execute(shcmd);
215            prompt();
216            shcmd[0]=0;
217          } break;
218          default :
219          {
220            if (ev.key<256 && isprint(ev.key))
221            {
222              int x=strlen(shcmd);
223              shcmd[x+1]=0;
224              shcmd[x]=ev.key;
225              put_char(ev.key);
226            }
227          } break;
228        } break;
229      }
230    }
231    return 1;
232  }
233  return 0;
234}
235
Note: See TracBrowser for help on using the repository browser.