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

editor/util.c

Go to the documentation of this file.
00001 #include "editor.h"
00002 
00003 #define GrayIdle() pokeIO(0x600005,0b10111)
00004 //{{{
00005 int read_char(void)
00006 {
00007     // NOTE: The contents of this function have a large effect on battery
00008     // usage.
00009     unsigned short key;
00010     void *kbq = kbd_queue();
00011     
00012     OSTimerRestart(APD_TIMER);
00013     
00014     if(GrayCheckRunning())
00015     {
00016         while(OSdequeue (&key, kbq)) {
00017             GrayIdle();
00018             if(OSTimerExpired(APD_TIMER)) {
00019                 off();
00020                 OSTimerRestart(APD_TIMER);
00021             }
00022         }
00023     } else {
00024         while(OSdequeue (&key, kbq)) {
00025             idle();
00026             if(OSTimerExpired(APD_TIMER)) {
00027                 off();
00028                 OSTimerRestart(APD_TIMER);
00029             }
00030         }
00031     }
00032     return key & ~0x800;
00033 }
00034 //}}}
00035 
00036 //{{{
00037 void clear_screen(void)
00038 {
00039     if( GrayCheckRunning() ) {
00040         GraySetAMSPlane(LIGHT_PLANE);
00041         clrscr();
00042         GraySetAMSPlane(DARK_PLANE);
00043     }
00044     clrscr();
00045 }
00046 //}}}
00047 //{{{
00048 void clear_line_range(int top, int bottom)
00049 {
00050     if(GrayCheckRunning())
00051     {
00052         memset((char*)GrayGetPlane(LIGHT_PLANE) + top*30, 0, (bottom-top)*30);
00053         memset((char*)GrayGetPlane(DARK_PLANE) + top*30, 0, (bottom-top)*30);
00054     }
00055     memset((char*)LCD_MEM + top*30, 0, (bottom-top)*30);
00056 }
00057 //}}}
00058 //{{{
00063 void draw_sprite(const char *spr, int x, int y)
00064 {
00065     char *lightpos, *darkpos;
00066     int ii;
00067     
00068     x >>= 3;
00069     
00070     if( GrayCheckRunning() ) {
00071         lightpos = (char*)GrayGetPlane(LIGHT_PLANE) + y*30 + x;
00072         darkpos  = (char*)GrayGetPlane(DARK_PLANE)  + y*30 + x;
00073         spr += 8;
00074         for(ii=0; ii<8; ii++) {
00075             *darkpos  = *(spr++);
00076             *lightpos = *(spr++);
00077             darkpos  += 30;
00078             lightpos += 30;
00079         }
00080     } else {
00081         lightpos = LCD_MEM + y*30 + x;
00082         for(ii=0; ii<8; ii++) {
00083             *lightpos = *(spr++);
00084             lightpos += 30;
00085         }
00086     }
00087 }
00088 //}}}
00089 //{{{
00094 void draw_sprite_xor(const char *spr, int x, int y)
00095 {
00096     char *lightpos, *darkpos;
00097     int ii;
00098     
00099     x >>= 3;
00100     
00101     if( GrayCheckRunning() ) {
00102         lightpos = (char*)GrayGetPlane(LIGHT_PLANE) + y*30 + x;
00103         darkpos  = (char*)GrayGetPlane(DARK_PLANE)  + y*30 + x;
00104         spr += 8;
00105         for(ii=0; ii<8; ii++) {
00106             *darkpos  ^= *(spr++);
00107             *lightpos ^= *(spr++);
00108             darkpos  += 30;
00109             lightpos += 30;
00110         }
00111     } else {
00112         lightpos = LCD_MEM + y*30 + x;
00113         for(ii=0; ii<8; ii++) {
00114             *lightpos ^= *(spr++);
00115             lightpos += 30;
00116         }
00117     }
00118 }
00119 //}}}
00120 //{{{
00121 void draw_line(short x0, short y0, short x1, short y1, short Attr)
00122 {
00123     if(GrayCheckRunning())
00124     {
00125         GraySetAMSPlane(LIGHT_PLANE);
00126         DrawLine(x0, y0, x1, y1, Attr);
00127         GraySetAMSPlane(DARK_PLANE);
00128     }
00129     DrawLine(x0, y0, x1, y1, Attr);
00130 }
00131 //}}}
00132 //{{{
00133 void draw_string(const char *str, short x, short y)
00134 {
00135     FontSetSys(F_4x6);
00136     if(GrayCheckRunning())
00137     {
00138         GraySetAMSPlane(LIGHT_PLANE);
00139         DrawStr(x, y, str, A_NORMAL);
00140         GraySetAMSPlane(DARK_PLANE);
00141     }
00142     DrawStr(x, y, str, A_NORMAL);
00143 }
00144 //}}}
00145 //{{{
00146 void draw_string_font(const char *str, short x, short y, short font)
00147 {
00148     FontSetSys(font);
00149     if(GrayCheckRunning())
00150     {
00151         GraySetAMSPlane(LIGHT_PLANE);
00152         DrawStr(x, y, str, A_NORMAL);
00153         GraySetAMSPlane(DARK_PLANE);
00154     }
00155     DrawStr(x, y, str, A_NORMAL);
00156 }
00157 //}}}
00158 //{{{
00159 void clear_rect(int left, int top, int width, int height)
00160 {
00161     SCR_RECT r = {{ left, top, left+width-1, top+height-1}};
00162     
00163     if(GrayCheckRunning())
00164     {
00165         GraySetAMSPlane(LIGHT_PLANE);
00166         ScrRectFill(&r, &r, A_REVERSE);
00167         GraySetAMSPlane(DARK_PLANE);
00168     }
00169     ScrRectFill(&r, &r, A_REVERSE);
00170 }
00171 //}}}
00172 //{{{
00173 // draw_rect: Draws a rectangle with a black border and a white interior
00174 void draw_rect(int left, int top, int width, int height)
00175 {
00176     clear_rect(left, top, width, height);
00177     width--;
00178     height--;
00179     draw_line(left, top, left+width, top, A_NORMAL);
00180     draw_line(left+width, top, left+width, top+height, A_NORMAL);
00181     draw_line(left+width, top+height, left, top+height, A_NORMAL);
00182     draw_line(left, top+height, left, top, A_NORMAL);
00183 }
00184 //}}}
00185 //{{{
00186 void xor_rect(int left, int top, int width, int height)
00187 {
00188     SCR_RECT r = {{ left, top, left+width-1, top+height-1}};
00189     
00190     if(GrayCheckRunning())
00191     {
00192         GraySetAMSPlane(LIGHT_PLANE);
00193         ScrRectFill(&r, &r, A_XOR);
00194         GraySetAMSPlane(DARK_PLANE);
00195     }
00196     ScrRectFill(&r, &r, A_XOR);
00197 }
00198 //}}}
00199 
00200 //{{{
00201 void debug_print(const char *fmt, ...)
00202 {
00203     char buf[512];
00204     va_list varargs;
00205     va_start(varargs, fmt);
00206     vsprintf(buf, fmt, varargs);
00207     va_end(varargs);
00208     draw_string(buf, 20, 30);
00209 }
00210 //}}}
00211 
00212 //{{{
00213 int is_in_bounds(int x, int y)
00214 {
00215     if(x<0 || y<0 || x>=FIELD_WIDTH || y>=FIELD_HEIGHT)
00216         return 0;
00217     else
00218         return 1;
00219 }
00220 //}}}
00221 
00222 //{{{
00223 // freadstr: Read a null-terminated string from fp. This is different from
00224 // fgets only in what marks the end of the string.
00225 char *freadstr(char *s, int n, FILE *fp)
00226 {
00227     // Source: tigcclib, with modifications
00228     short c=EOF;
00229     char *cs=s;
00230     while(--n>0&&(c=fgetc(fp))!=EOF) {
00231         if ((*cs++=c)=='\0') break;
00232     }
00233     *cs=0;
00234     return ((c==EOF&&cs==s)?0:s);
00235 }
00236 //}}}
00237 //{{{
00238 // fwritestr: Write a null-terminated string to fp.
00239 void fwritestr(const char *s, FILE *fp)
00240 {
00241     const char *cs = s;
00242     while(*cs != '\0') {
00243         fputc(*cs, fp);
00244         cs++;
00245     }
00246     fputc('\0', fp);
00247 }
00248 //}}}

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