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

Last change on this file since 489 was 481, checked in by Sam Hocevar, 10 years ago

Fuck the history, I'm renaming all .hpp files to .h for my own sanity.

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#include <string.h>
15
16#include "game.h"
17
18#include "linked.h"
19#include "console.h"
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  free(screen);
74  free(name);
75}
76
77console::console(JCFont *font, int width, int height, char const *Name)
78{
79  con_win=NULL;
80  w=width;
81  h=height;
82  screen=(char *)malloc(w*h);
83  memset(screen,' ',w*h);
84  cx=cy=0;
85  fnt=font;
86  lastx=xres/2-screen_w()/2;
87  lasty=yres/2-screen_h()/2;
88  name = strdup(Name);
89}
90
91
92void console::draw_cursor()
93{
94  if (con_win)
95    fnt->put_char(con_win->screen,cx*fnt->width()+wx(),cy*fnt->height()+wy(),'_');
96}
97
98
99void console::draw_char(int x, int y, char ch)
100{
101  if (con_win)
102  {
103    int fw=fnt->width(),fh=fnt->height();
104    int dx=wx()+x*fw,dy=wy()+y*fh;
105    con_win->screen->bar(dx,dy,dx+fw-1,dy+fh-1,wm->black());
106    fnt->put_char(con_win->screen,dx,dy,ch);
107  }
108}
109
110void console::do_cr()
111{
112  if (cx<w && cy<h)  draw_char(cx,cy,screen[cy*w+cx]);
113  cx=0;
114  cy++;
115  if (cy>=h)
116  {
117    cy=h-1;
118    if (con_win)
119    {
120      memmove(screen,screen+w,w*(h-1));
121      memset(screen+w*(h-1),' ',w);
122      redraw();
123      wm->flush_screen();
124    }
125  } else draw_cursor();
126}
127
128void console::put_char(char ch)
129{
130
131
132  switch (ch)
133  {
134    case JK_BACKSPACE :
135    {
136      if (cx)
137      {
138    if (con_win)
139      draw_char(cx,cy,screen[cy*w+cx]);
140    cx--;
141    if (con_win)
142      draw_cursor();
143      }
144    } break;
145    case '\n' :
146    case JK_ENTER :
147    {
148      do_cr();
149    } break;
150    default :
151    {
152      screen[cy*w+cx]=ch;
153      if (con_win)
154        draw_char(cx,cy,ch);
155      cx++;
156      if (cx>=w) do_cr(); else
157      if (con_win) draw_cursor();
158    }
159  }
160}
161
162
163void console::print_f( const char *format, ...)
164{
165  char st[300];
166  va_list ap;
167  va_start(ap, format);
168  vsprintf(st,format,ap);
169  va_end(ap);
170  put_string(st);
171}
172
173
174shell_term::shell_term(JCFont *font, int width, int height, char const *Name) :
175  console(font,width,height,Name)
176{
177  shcmd[0]=0;
178  prompt();
179}
180
181void shell_term::prompt()
182{
183  put_string("(?=help)>");
184}
185
186void shell_term::execute(char const *st)
187{
188  put_string(st);
189  put_string(" : unhandled\n");
190}
191
192int shell_term::handle_event(event &ev)
193{
194  if (ev.window==con_win && con_win)
195  {
196    switch (ev.type)
197    {
198      case EV_KEY :
199      {
200    switch (ev.key)
201    {
202      case JK_BACKSPACE:
203      {
204        if (shcmd[0]!=0)
205        {
206          shcmd[strlen(shcmd)-1]=0;
207          put_char(ev.key);
208        }
209      } break;
210      case JK_ENTER :
211      {
212        put_char(ev.key);
213        execute(shcmd);
214        prompt();
215        shcmd[0]=0;
216      } break;
217      default :
218      {
219        if (ev.key<256 && isprint(ev.key))
220        {
221          int x=strlen(shcmd);
222          shcmd[x+1]=0;
223          shcmd[x]=ev.key;
224          put_char(ev.key);
225        }
226      } break;
227    } break;
228      }
229    }
230    return 1;
231  }
232  return 0;
233}
234
Note: See TracBrowser for help on using the repository browser.