home *** CD-ROM | disk | FTP | other *** search
- /************************ RISCOS stuff **************************/
-
- #include "wimp.h"
- #include "wimpt.h"
- #include "event.h"
- #include "sprite.h"
- #include "res.h"
- #include "template.h"
- #include "werr.h"
- #include "colourtran.h"
- #include "kernel.h"
- #include "swis.h"
- #include "swiv.h"
- #include "bbc.h"
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #include "window.h"
-
- static int wimpversion;
- static wimp_w w;
- static sprite_state s;
- static sprite_area *area;
- static int *save_area;
- static char *pixtrans;
- static int osx,osy;
- static int xeig=1,yeig=2;
- static int charx,chary;
- static int mode = 12;
- static int l2bpp = 2;
- static int bpp = 4;
-
- static int pal16[] = { /* bbggrrnn */
- 0x00000000, 0xf0f0f001, 0x00f0f002, 0x0080f003, 0xf0000004, 0xf000f005, 0xf0f00006, 0xf0f0f007,
- 0x00206008, 0x0050e009, 0x10f0900a, 0x00b0e00b, 0xf050500c, 0xf020900d, 0x80f0000e, 0xc0c0c00f,
- };
-
-
- static void coltable
- ( void )
- {
- wimpt_noerr(colourtran_select_table(mode,(wimp_paletteword *)pal16,-1,(wimp_paletteword *)-1,pixtrans));
- }
-
-
- static sprite_id *SprPtr
- (sprite_header *s)
- {
- static sprite_id id;
- id.tag = sprite_id_addr;
- id.s.addr = s;
- return &id;
- }
-
- static sprite_id *SprNam
- (char *s)
- {
- static sprite_id id;
- id.tag = sprite_id_name;
- id.s.name = s;
- return &id;
- }
-
- static void grabcaret
- ( void )
- { wimp_caretstr c;
- c.w = w;
- c.i = -1;
- c.x = 100;
- c.y = -100;
- c.height = 0|(1<<25); /* invisible */
- c.index = 0;
- wimpt_noerr(wimp_set_caret_pos(&c));
- }
-
- void window_init( int m, int x, int y )
- {
- wimp_wind *t;
- wimp_redrawstr r;
- wimp_wstate state;
- int size;
- mode = m;
- if (!x)
- x = bbc_modevar(mode,bbc_XWindLimit)+1;
- if (!y)
- y = bbc_modevar(mode,bbc_YWindLimit)+1;
- l2bpp = bbc_modevar(mode,bbc_Log2BPP);
- bpp = 1 << l2bpp;
- xeig=bbc_modevar(mode,bbc_XEigFactor);
- yeig=bbc_modevar(mode,bbc_YEigFactor);
- osx = x<<xeig;
- osy = y<<yeig;
- charx = 8 << xeig;
- chary = (bbc_modevar(mode,bbc_ModeFlags)&(1<<5)?16:8) << yeig;
- wimpversion = wimpt_init("Curses");
- #ifdef DEBUG
- signal(SIGABRT, SIG_DFL);
- signal(SIGFPE, SIG_DFL);
- signal(SIGILL, SIG_DFL);
- signal(SIGSEGV, SIG_DFL);
- signal(SIGTERM, SIG_DFL);
- #endif
- res_init("curses");
- template_init();
- t = template_syshandle( "window" );
- wimpt_noerr( wimp_create_wind( t, &w ) );
- size = sizeof(sprite_area)+sizeof(sprite_header)+x*y*bpp/8;
- area = malloc(size);
- sprite_area_initialise(area, size);
- wimpt_noerr(sprite_create(area, "screen", sprite_nopalette, x, y, mode));
- wimpt_noerr(sprite_sizeof_spritecontext(area, SprNam("screen"), &size));
- save_area = malloc(size);
- save_area[0]=0;
- pixtrans = malloc(1<<bpp);
- coltable();
- r.w = w;
- r.box.x0 = 0; r.box.x1 = osx; r.box.y0 = -osy; r.box.y1 = 0;
- wimpt_noerr( wimp_get_wind_state( w, &state ) );
- if (state.o.box.x1 > state.o.box.x0+osx)
- state.o.box.x1 = state.o.box.x0+osx;
- if (state.o.box.y0 < state.o.box.y1-osy)
- state.o.box.y0 = state.o.box.y1-osy;
- if (state.o.x < osx - (state.o.box.x1 - state.o.box.x0))
- state.o.x = osx - (state.o.box.x1 - state.o.box.x0);
- if (state.o.y < (state.o.box.y1 - state.o.box.y0) - osy)
- state.o.y = (state.o.box.y1 - state.o.box.y0) - osy;
- wimpt_noerr(wimp_open_wind(&state.o));
- wimpt_noerr(wimp_set_extent(&r));
- wimpt_noerr(wimp_force_redraw(&r));
- grabcaret();
- wimpt_noerr(sprite_outputtosprite(area, SprNam("screen"), save_area, &s));
- _swix(OS_ChangedBox,_IN(R0),1);
- _swix(OS_ChangedBox,_IN(R0),2);
- }
-
- void window_finish
- (void)
- {
- wimpt_noerr(sprite_restorestate(s));
- wimpt_noerr(wimp_closedown());
- free(area);
- free(save_area);
- free(pixtrans);
- }
-
- void window_sleep
- (int n)
- {
- /* should use PollIdle */
- n = _swi(OS_ReadMonotonicTime,0)+n*100;
- do
- { window_getchar();
- } while (n>_swi(OS_ReadMonotonicTime,0));
- }
-
- int window_getchar
- (void)
- {
- int *box;
- wimp_redrawstr r;
- wimp_eventstr ev,*e=&ev;
- int key;
- int more;
- _swix(OS_ChangedBox,_IN(R0)|_OUT(R1),-1,&box);
- r.w = w;
- r.box.x0 = box[1]<<xeig;
- r.box.y0 = (box[2]<<yeig)-osy;
- r.box.x1 = (box[3]+1<<xeig);
- r.box.y1 = (box[4]+1<<yeig)-osy;
- _swix(OS_ChangedBox,_IN(R0),2);
- if (r.box.x1>r.box.x0 && r.box.y1>r.box.y0)
- wimpt_noerr(wimp_force_redraw(&r));
- wimpt_noerr(sprite_restorestate(s));
- wimp_save_fp_state_on_poll();
- for (;;)
- { wimpt_noerr(wimp_poll(0,e));
- switch ( e->e )
- {
- case wimp_ENULL:
- key = 0; goto ret;
- case wimp_EREDRAW:
- if (wimpt_checkmode())
- coltable();
- r.w = e->data.o.w;
- wimpt_noerr(wimp_redraw_wind(&r,&more));
- while (more)
- {
- wimpt_noerr(sprite_put_scaled( area, SprNam("screen"), 0,
- r.box.x0-r.scx, r.box.y1-r.scy-osy, NULL, pixtrans));
- wimpt_noerr(wimp_get_rectangle(&r,&more));
- }
- break;
- case wimp_EOPEN:
- wimp_open_wind( &e->data.o );
- break;
- case wimp_EBUT:
- {
- switch ( e->data.but.m.bbits )
- { case wimp_BCLICKLEFT:
- case wimp_BCLICKRIGHT:
- case wimp_BDRAGLEFT:
- case wimp_BDRAGRIGHT:
- case wimp_BRIGHT:
- case wimp_BLEFT:
- grabcaret();
- break;
- }
- break;
- }
- case wimp_ECLOSE:
- exit(0);
- case wimp_ESEND: case wimp_ESENDWANTACK:
- switch ( e->data.msg.hdr.action )
- { case wimp_MCLOSEDOWN:
- exit(0);
- break;
- }
- case wimp_EKEY:
- { key = e->data.key.chcode;
- if (key>0xFF)
- wimp_processkey(key);
- else
- {
- ret: wimpt_noerr(sprite_outputtosprite(area, SprNam("screen"), save_area, &s));
- return key;
- }
- continue;
- }
- }
- }
- }
-
- #define A_STANDOUT 0020000
- #define A_UNDERLINE 0000400
- #define A_REVERSE 0001000
- #define A_BLINK 0002000
- #define A_DIM 0004000
- #define A_BOLD 0010000
-
- void TextAt(int x,int y,int attr,char *p,int n)
- { int fg,bg,t;
- _kernel_swi_regs r;
-
- fg = attr&0xf;
- bg = (attr>>4)&0xf;
- if (attr&(A_STANDOUT|A_REVERSE))
- t=fg,fg=bg,bg=t;
- bbc_tab(x,y);
- bbc_colour(fg);
- bbc_colour(bg+128);
- r.r[0] = (int) p; r.r[1] = n;
- _kernel_swi(OS_WriteN,&r,&r);
- if (attr&A_BOLD)
- { bbc_vdu(bbc_TextToGraph);
- bbc_gcol(0,fg);
- bbc_move(x*charx+(1<<xeig),osy-y*chary-(1<<yeig));
- r.r[0] = (int) p; r.r[1] = n;
- _kernel_swi(OS_WriteN,&r,&r);
- bbc_vdu(bbc_TextToText);
- }
- if (attr&A_UNDERLINE)
- { bbc_gcol(0,fg);
- bbc_move(x*charx,osy-(y+1)*chary);
- bbc_drawby(n*charx,0);
- }
- }
-
- void TextRectFill(int x, int y,int w, int h, int c)
- {
- if (c==-1)
- bbc_gcol(4,0);
- else
- bbc_gcol(0,(c>>4)&0xf);
- bbc_rectanglefill(x*charx,osy-(y+h)*chary,w*charx-(1<<xeig),h*chary-(1<<yeig));
- }
-