Main Page | Alphabetical List | Class List | File List | Class Members | File Members

engine/util.c

Go to the documentation of this file.
00001 #include "engine.h"
00002 
00003 //{{{
00004 int read_char(void)
00005 {
00006     // NOTE: The contents of this function have a large effect on battery
00007     // usage.
00008     unsigned short key;
00009     void *kbq = kbd_queue();
00010     
00011     OSTimerRestart(APD_TIMER);
00012     
00013     if(GrayCheckRunning())
00014     {
00015         while(OSdequeue (&key, kbq)) {
00016             GrayIdle();
00017             if(OSTimerExpired(APD_TIMER)) {
00018                 off();
00019                 OSTimerRestart(APD_TIMER);
00020             }
00021         }
00022     } else {
00023         while(OSdequeue (&key, kbq)) {
00024             idle();
00025             if(OSTimerExpired(APD_TIMER)) {
00026                 off();
00027                 OSTimerRestart(APD_TIMER);
00028             }
00029         }
00030     }
00031     return key;
00032 }
00033 //}}}
00034 //{{{
00035 int read_char_with_timeout()
00036 {
00037     // NOTE: The contents of this function have a large effect on battery
00038     // usage.
00039     unsigned short key;
00040     void *kbq = kbd_queue();
00041     
00042     while(OSdequeue (&key, kbq)) {
00043         GrayIdle();
00044         if(OSTimerExpired(APD_TIMER)) {
00045             off();
00046             OSTimerRestart(APD_TIMER);
00047         }
00048         if(OSTimerExpired(USER_TIMER)) {
00049             key = 0;
00050             break;
00051         }
00052     }
00053     
00054     if(key) {
00055         OSTimerRestart(APD_TIMER); // APD timer is restarted IFF a key was hit
00056     }
00057     
00058     return key;
00059 }
00060 //}}}
00061 //{{{
00062 // A non-blocking version of read_char. Returns 0 if no input is ready.
00063 int poll_char(void)
00064 {
00065     unsigned short key;
00066     if( OSdequeue (&key, kbd_queue()) )
00067         return 0;
00068     else
00069         return key;
00070 }
00071 //}}}
00072 //{{{
00073 void flush_input(void)
00074 {
00075     OSqclear(kbd_queue());
00076 }
00077 //}}}
00078 
00079 //{{{
00080 void delay(void)
00081 {
00082     OSRegisterTimer(USER_TIMER, 2);
00083     
00084     if(GrayCheckRunning())
00085     {
00086         do {
00087             GrayIdle();
00088         } while(!OSTimerExpired(USER_TIMER));
00089     } else {
00090         do {
00091             idle();
00092         } while(!OSTimerExpired(USER_TIMER));
00093     }
00094     OSFreeTimer(USER_TIMER);
00095 }
00096 //}}}
00097 
00098 //{{{
00099 void clear_screen(void)
00100 {
00101     if( GrayCheckRunning() ) {
00102         GraySetAMSPlane(LIGHT_PLANE);
00103         clrscr();
00104         GraySetAMSPlane(DARK_PLANE);
00105     }
00106     clrscr();
00107 }
00108 //}}}
00109 //{{{
00110 void clear_line_range(int top, int bottom)
00111 {
00112     if(GrayCheckRunning())
00113     {
00114         memset((char*)GrayGetPlane(LIGHT_PLANE) + top*30, 0, (bottom-top)*30);
00115         memset((char*)GrayGetPlane(DARK_PLANE) + top*30, 0, (bottom-top)*30);
00116     }
00117     memset((char*)LCD_MEM + top*30, 0, (bottom-top)*30);
00118 }
00119 //}}}
00120 //{{{
00125 void draw_sprite(const char *spr, int x, int y)
00126 {
00127     char *lightpos, *darkpos;
00128     int ii;
00129     
00130     x >>= 3;
00131     
00132     if( GrayCheckRunning() ) {
00133         lightpos = (char*)GrayGetPlane(LIGHT_PLANE) + y*30 + x;
00134         darkpos  = (char*)GrayGetPlane(DARK_PLANE)  + y*30 + x;
00135         spr += 8;
00136         for(ii=0; ii<8; ii++) {
00137             *darkpos  = *(spr++);
00138             *lightpos = *(spr++);
00139             darkpos  += 30;
00140             lightpos += 30;
00141         }
00142     } else {
00143         lightpos = LCD_MEM + y*30 + x;
00144         for(ii=0; ii<8; ii++) {
00145             *lightpos = *(spr++);
00146             lightpos += 30;
00147         }
00148     }
00149 }
00150 //}}}
00151 //{{{
00156 void draw_sprite_xor(const char *spr, int x, int y)
00157 {
00158     char *lightpos, *darkpos;
00159     int ii;
00160     
00161     x >>= 3;
00162     
00163     if( GrayCheckRunning() ) {
00164         lightpos = (char*)GrayGetPlane(LIGHT_PLANE) + y*30 + x;
00165         darkpos  = (char*)GrayGetPlane(DARK_PLANE)  + y*30 + x;
00166         spr += 8;
00167         for(ii=0; ii<8; ii++) {
00168             *darkpos  ^= *(spr++);
00169             *lightpos ^= *(spr++);
00170             darkpos  += 30;
00171             lightpos += 30;
00172         }
00173     } else {
00174         lightpos = LCD_MEM + y*30 + x;
00175         for(ii=0; ii<8; ii++) {
00176             *lightpos ^= *(spr++);
00177             lightpos += 30;
00178         }
00179     }
00180 }
00181 //}}}
00182 //{{{
00183 void draw_line(short x0, short y0, short x1, short y1, short Attr)
00184 {
00185     if(GrayCheckRunning())
00186     {
00187         GraySetAMSPlane(LIGHT_PLANE);
00188         DrawLine(x0, y0, x1, y1, Attr);
00189         GraySetAMSPlane(DARK_PLANE);
00190     }
00191     DrawLine(x0, y0, x1, y1, Attr);
00192 }
00193 //}}}
00194 //{{{
00195 void draw_string(const char *str, short x, short y, short font)
00196 {
00197     FontSetSys(font);
00198     if(GrayCheckRunning())
00199     {
00200         GraySetAMSPlane(LIGHT_PLANE);
00201         DrawStr(x, y, str, A_NORMAL);
00202         GraySetAMSPlane(DARK_PLANE);
00203     }
00204     DrawStr(x, y, str, A_NORMAL);
00205 }
00206 //}}}
00207 //{{{
00208 void clear_rect(int left, int top, int width, int height)
00209 {
00210     SCR_RECT r = {{ left, top, left+width-1, top+height-1}};
00211     
00212     if(GrayCheckRunning())
00213     {
00214         GraySetAMSPlane(LIGHT_PLANE);
00215         ScrRectFill(&r, &r, A_REVERSE);
00216         GraySetAMSPlane(DARK_PLANE);
00217     }
00218     ScrRectFill(&r, &r, A_REVERSE);
00219 }
00220 //}}}
00221 //{{{
00222 // draw_rect: Draws a rectangle with a black border and a white interior
00223 void draw_rect(int left, int top, int width, int height)
00224 {
00225     clear_rect(left, top, width, height);
00226     width--;
00227     height--;
00228     draw_line(left, top, left+width, top, A_NORMAL);
00229     draw_line(left+width, top, left+width, top+height, A_NORMAL);
00230     draw_line(left+width, top+height, left, top+height, A_NORMAL);
00231     draw_line(left, top+height, left, top, A_NORMAL);
00232 }
00233 //}}}
00234 //{{{
00235 void xor_rect(int left, int top, int width, int height)
00236 {
00237     SCR_RECT r = {{ left, top, left+width-1, top+height-1}};
00238     
00239     if(GrayCheckRunning())
00240     {
00241         GraySetAMSPlane(LIGHT_PLANE);
00242         ScrRectFill(&r, &r, A_XOR);
00243         GraySetAMSPlane(DARK_PLANE);
00244     }
00245     ScrRectFill(&r, &r, A_XOR);
00246 }
00247 //}}}
00248 /*
00249 //{{{
00250 // freadstr: Read a null-terminated string from fp. This is different from
00251 // fgets only in what marks the end of the string.
00252 char *freadstr(char *s, int n, FILE *fp)
00253 {
00254     // Source: tigcclib, with modifications
00255     short c=EOF;
00256     char *cs=s;
00257     while(--n>0&&(c=fgetc(fp))!=EOF) {
00258         if ((*cs++=c)=='\0') break;
00259     }
00260     *cs=0;
00261     return ((c==EOF&&cs==s)?0:s);
00262 }
00263 //}}}
00264 */
00265 
00266 //{{{
00267 void freadstr(char *s, int n, FILE *fp)
00268 {
00269     char *end = (char*)fp->base+peek_w(fp->base)+2;
00270     
00271     do {
00272         if(fp->fpos > end) {
00273             fp->fpos = end;
00274             break;
00275         }
00276         *(s++) = *(fp->fpos);
00277         n--;
00278         fp->fpos ++;
00279     } while( *(fp->fpos-1) != '\0' && n>1 );
00280     *s = '\0';
00281 }
00282 //}}}
00283 //{{{
00284 unsigned short fread_fast(void *ptr, short size, short n, FILE *f)
00285 {
00286     unsigned short bytes_remaining =
00287         peek_w(f->base) + (char*)f->base - (char*)f->fpos + 2;
00288     
00289     if(f->flags&_F_ERR) return 0;
00290     if(!(f->flags&_F_READ)) __FERROR(f);
00291 
00292     if(size*n > bytes_remaining) // More data requested than available
00293         n = bytes_remaining/size;
00294     else if(size*n == bytes_remaining)
00295         f->flags|=_F_EOF;
00296     
00297     size *= n;
00298     memcpy(ptr, f->fpos, size);
00299     f->fpos += size;
00300     return n;
00301 }
00302 //}}}
00303 //{{{
00304 // Somewhat less general, in that this omits support for ASCII junk, or
00305 // unget, or EOF tracking, or much error handling at all.
00306 short fseek_fast(FILE *f, long offset, short wh)
00307 {
00308     char *start = (char*)f->base+2;
00309     char *end = (char*)f->base+peek_w(f->base)+2;
00310     char *pos = ((wh==SEEK_SET)?start:((wh==SEEK_CUR)?(f->fpos):end))+offset;
00311     if(pos<start||pos>end) __FERROR(f);
00312     f->fpos=pos;
00313     return 0;
00314 }
00315 //}}}
00316 

Generated on Thu Apr 22 14:06:33 2004 for SKye by doxygen 1.3.6