source: abuse/trunk/src/director.cpp @ 528

Last change on this file since 528 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: 4.5 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 or
8 *  Jonathan Clark.
9 */
10
11#include "config.h"
12
13#include "common.h"
14
15#include "director.h"
16#include "game.h"
17#include "lisp.h"
18#include "fonts.h"
19
20director scene_director;
21
22void director::scroll_text(char *Text)
23{ text=Text;
24  text_y=the_game->first_view->cy2-the_game->first_view->cy1+1;
25}
26
27director::director()
28{
29  tleft=ttop=tright=tbottom=pan_xv=pan_yv=0;
30  text_step=-2;
31  frame_speed=100;
32  scroll_speed=60;
33  pan_speed=60;
34  scene_abort=0;
35}
36
37extern unsigned char *white_light;
38
39
40
41int text_draw(int y, int x1, int y1, int x2, int y2, char const *buf, JCFont *font, uint8_t *cmap, char color)
42{
43  int cx1, cy1, cx2, cy2, word_size, word_len;
44  screen->GetClip(cx1, cy1, cx2, cy2);
45  screen->InClip(x1,y1,x2+1,y2+1);
46  int h=font->height()+2,w=font->width(),x=x1,dist;
47  y+=y1;
48  char const *word_start;
49
50  while (buf && *buf)
51  {
52    do
53    {
54      if (*buf=='\\' && buf[1]=='n')
55      {
56    x=x1;
57    y+=h*2;
58    buf+=2;
59      }
60
61      // skip space
62      if (*buf==' ' || *buf=='\r' || *buf=='\n' || *buf=='\t')
63      {
64    x+=w;
65    while (*buf==' ' || *buf=='\r' || *buf=='\n' || *buf=='\t')   // skip space until next word
66          buf++;
67      }
68
69      word_start=buf;
70      for (word_len=0,word_start=buf,word_size=0; *buf && *buf!=' ' && *buf!='\r' && *buf!='\n' &&
71       *buf!='\t' && (*buf!='\\' || buf[1]!='n'); buf++,word_size+=w,word_len++);
72
73      if (word_size<x2-x1) // make sure the word can fit on the screen
74      {
75    if (word_size+x>x2)    // does word not fit on line?
76    {
77      y+=h;                // go to next line
78      x=x1;
79    }
80      }
81
82
83      if (y+h<y1)         // word on screen yet?
84    x+=word_size;
85
86    } while (*buf && y+h<y1);     // if not on screen yet, fetch next word
87
88    dist=31;
89    if (y-y1<dist)
90    {
91      if (y-y1<1) dist=0;
92      else dist=y-y1;
93    }
94
95    if (y2-y<dist)
96    {
97      if (y2-y<1) dist=0;
98      else dist=y2-y;
99    }
100
101    int c=cmap[dist];
102    if (y>y2) { buf=NULL; }
103    else
104    {
105      while (word_len--)
106      {
107    font->put_char(screen,x+1,y+1,*word_start,0);
108    font->put_char(screen,x,y,*word_start,c);
109    word_start++;
110    x+=w;
111      }
112    }
113
114  }
115  screen->SetClip(cx1,cy1,cx2,cy2);
116  return (y<=y1);
117}
118
119void director::wait(void *arg)
120{
121  if (scene_abort) return ;
122  pan_time=frame_time=text_time=NULL;
123  int done=0;
124  LSymbol *pan_symbol = LSymbol::FindOrCreate("pan"),
125             *text_symbol = LSymbol::FindOrCreate("text");
126
127  JCFont *font=wm->font();
128
129  do
130  {
131    the_game->draw_map(the_game->first_view);
132    time_marker cur_time;
133
134    if (pan_steps)
135    {
136      if (pan_time)
137      {
138    if ((int)(cur_time.diff_time(pan_time)*1000)>pan_speed)
139    {
140      the_game->pan(pan_xv,pan_yv);
141      pan_steps--;
142      delete pan_time;
143      if (pan_steps)
144          pan_time=new time_marker;
145      else pan_time=NULL;
146    }
147      } else pan_time=new time_marker;
148    } else if (arg==pan_symbol) done=1;
149
150    if (text)
151    {
152      if (text_draw(text_y,
153            the_game->first_view->cx1+tleft,
154            the_game->first_view->cy1+ttop,
155            the_game->first_view->cx2-tright,
156            the_game->first_view->cy2-tbottom,text,font,
157            white_light+32*256,
158            wm->bright_color()
159
160            ))
161        text=NULL;
162      if (text_time)
163      {
164    if ((int)(cur_time.diff_time(text_time)*1000)>scroll_speed)
165    {
166      text_y+=text_step;
167      delete text_time;
168      if (text)
169        text_time=new time_marker;
170      else
171        text_time=NULL;
172    }
173      } else text_time=new time_marker;
174    } else if (arg==text_symbol) done=1;
175
176    wm->flush_screen();
177    while (wm->event_waiting())
178    {
179      event ev;
180      wm->get_event(ev);
181      if (ev.type==EV_KEY)
182      {
183    switch (ev.key)
184    {
185      case JK_UP :
186      case JK_LEFT :
187      {
188        if (scroll_speed>=20)
189        scroll_speed-=20;
190          else text_step--;
191      }
192      break;
193      case JK_RIGHT :
194      case JK_DOWN :
195      {
196        if (text_step<-2)
197        text_step++;
198        else if (scroll_speed<200) scroll_speed+=20;
199        break;
200        case JK_ESC : set_abort(1); done=1; break;
201        case JK_ENTER : done=1; break;
202      }
203    }
204      }
205    }
206  } while (!done);
207  if (pan_time) delete pan_time;
208  if (frame_time) delete frame_time;
209  if (text_time) delete text_time;
210}
211
Note: See TracBrowser for help on using the repository browser.