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

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

imlib: make JCFont use vec2i classes.

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