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

Last change on this file since 494 was 494, checked in by Sam Hocevar, 11 years ago

style: remove trailing spaces, fix copyright statements.

File size: 3.9 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com or
8 *  Jonathan Clark.
9 */
10
11#include "config.h"
12
13#include <ctype.h>
14#include <stdarg.h>
15#include <string.h>
16
17#include "game.h"
18
19#include "linked.h"
20#include "console.h"
21
22void console::put_string(char const *st)
23{
24  while (*st)
25  {
26    put_char(*st);
27    st++;
28  }
29}
30
31void console::redraw()
32{
33  if (con_win)
34  {
35    con_win->clear();
36    char *s=screen;
37    int dx,dy,xa=fnt->width(),ya=fnt->height(),i,j;
38    for (j=0,dy=wy(); j<h; j++,dy+=ya)
39    {
40      for (i=0,dx=wx(); i<w; i++,s++,dx+=xa)
41      {
42    if (*s)
43      fnt->put_char(con_win->screen,dx,dy,*s);
44      }
45    }
46    fnt->put_char(con_win->screen,wx()+cx*xa,wy()+cy*ya,'_');
47  }
48}
49
50void console::show()
51{
52  if (!con_win)
53  {
54    con_win=wm->new_window(lastx,lasty,screen_w(),screen_h(),NULL,name);
55    redraw();
56    con_win->screen->set_clip(con_win->x1(),con_win->y1(),con_win->x2(),con_win->y2());
57  }
58}
59
60void console::hide()
61{
62  if (con_win)
63  {
64    lastx=con_win->x;
65    lasty=con_win->y;
66    wm->close_window(con_win);
67    con_win=NULL;
68  }
69}
70
71console::~console()
72{
73  hide();
74  free(screen);
75  free(name);
76}
77
78console::console(JCFont *font, int width, int height, char const *Name)
79{
80  con_win=NULL;
81  w=width;
82  h=height;
83  screen=(char *)malloc(w*h);
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 = strdup(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      wm->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(JCFont *font, int width, int height, char const *Name) :
176  console(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)
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.