source: abuse/branches/lol/src/director.cpp

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

build: SDL2 compilation fixes.

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