source: abuse/trunk/src/console.cpp

Last change on this file was 682, checked in by Sam Hocevar, 8 years ago

core: rename vec2i to ivec2 and update matrix.h from Lol Engine.

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