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

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

imlib: refactor dirty_rect clipping coordiantes so that the upper
bound is no longer inclusive. It will make things easier in the future.

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