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

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