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

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

imlib: refactor GetClip?, SetClip? etc. to use vec2i.

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