1 /* Public Domain Curses */
5 RCSID("$Id: pdcscrn.c,v 1.34 2008/07/14 04:24:52 wmcbrine Exp $")
11 SDL_Surface
*pdc_screen
= NULL
, *pdc_font
= NULL
, *pdc_icon
= NULL
,
12 *pdc_back
= NULL
, *pdc_tileback
= NULL
;
13 int pdc_sheight
= 0, pdc_swidth
= 0, pdc_yoffset
= 0, pdc_xoffset
= 0;
15 SDL_Color pdc_color
[16];
16 Uint32 pdc_mapped
[16];
17 int pdc_fheight
, pdc_fwidth
, pdc_flastc
;
20 /* COLOR_PAIR to attribute encoding table. */
22 static struct {short f
, b
;} atrtab
[PDC_COLOR_PAIRS
];
27 SDL_FreeSurface(pdc_tileback
);
29 pdc_tileback
= SDL_DisplayFormat(pdc_screen
);
37 while (dest
.y
< pdc_tileback
->h
)
41 while (dest
.x
< pdc_tileback
->w
)
43 SDL_BlitSurface(pdc_back
, 0, pdc_tileback
, &dest
);
44 dest
.x
+= pdc_back
->w
;
47 dest
.y
+= pdc_back
->h
;
50 SDL_BlitSurface(pdc_tileback
, 0, pdc_screen
, 0);
54 void PDC_scr_close(void)
56 PDC_LOG(("PDC_scr_close() - called\n"));
59 void PDC_scr_free(void)
65 /* open the physical screen -- allocate SP, miscellaneous intialization */
67 int PDC_scr_open(int argc
, char **argv
)
71 PDC_LOG(("PDC_scr_open() - called\n"));
73 SP
= calloc(1, sizeof(SCREEN
));
78 pdc_own_screen
= !pdc_screen
;
82 if (SDL_Init(SDL_INIT_VIDEO
|SDL_INIT_TIMER
) < 0)
84 fprintf(stderr
, "Could not start SDL: %s\n", SDL_GetError());
93 const char *fname
= getenv("PDC_FONT");
94 pdc_font
= SDL_LoadBMP(fname
? fname
: "pdcfont.bmp");
98 pdc_font
= SDL_LoadBMP_RW(SDL_RWFromMem(deffont
, sizeof(deffont
)), 0);
102 fprintf(stderr
, "Could not load font\n");
106 SP
->mono
= !pdc_font
->format
->palette
;
108 if (!SP
->mono
&& !pdc_back
)
110 const char *bname
= getenv("PDC_BACKGROUND");
111 pdc_back
= SDL_LoadBMP(bname
? bname
: "pdcback.bmp");
114 if (!SP
->mono
&& (pdc_back
|| !pdc_own_screen
))
116 SP
->orig_attr
= TRUE
;
117 SP
->orig_fore
= COLOR_WHITE
;
121 SP
->orig_attr
= FALSE
;
123 pdc_fheight
= pdc_font
->h
/ 8;
124 pdc_fwidth
= pdc_font
->w
/ 32;
127 pdc_flastc
= pdc_font
->format
->palette
->ncolors
- 1;
129 if (pdc_own_screen
&& !pdc_icon
)
131 const char *iname
= getenv("PDC_ICON");
132 pdc_icon
= SDL_LoadBMP(iname
? iname
: "pdcicon.bmp");
135 pdc_icon
= SDL_LoadBMP_RW(SDL_RWFromMem(deficon
,
136 sizeof(deficon
)), 0);
139 SDL_WM_SetIcon(pdc_icon
, NULL
);
144 const char *env
= getenv("PDC_LINES");
145 pdc_sheight
= (env
? atoi(env
) : 25) * pdc_fheight
;
147 env
= getenv("PDC_COLS");
148 pdc_swidth
= (env
? atoi(env
) : 80) * pdc_fwidth
;
150 pdc_screen
= SDL_SetVideoMode(pdc_swidth
, pdc_sheight
, 0,
151 SDL_SWSURFACE
|SDL_ANYFORMAT
|SDL_RESIZABLE
);
156 pdc_sheight
= pdc_screen
->h
- pdc_yoffset
;
159 pdc_swidth
= pdc_screen
->w
- pdc_xoffset
;
164 fprintf(stderr
, "Couldn't create a surface: %s\n", SDL_GetError());
171 for (i
= 0; i
< 8; i
++)
173 pdc_color
[i
].r
= (i
& COLOR_RED
) ? 0xc0 : 0;
174 pdc_color
[i
].g
= (i
& COLOR_GREEN
) ? 0xc0 : 0;
175 pdc_color
[i
].b
= (i
& COLOR_BLUE
) ? 0xc0 : 0;
177 pdc_color
[i
+ 8].r
= (i
& COLOR_RED
) ? 0xff : 0x40;
178 pdc_color
[i
+ 8].g
= (i
& COLOR_GREEN
) ? 0xff : 0x40;
179 pdc_color
[i
+ 8].b
= (i
& COLOR_BLUE
) ? 0xff : 0x40;
182 for (i
= 0; i
< 16; i
++)
183 pdc_mapped
[i
] = SDL_MapRGB(pdc_screen
->format
, pdc_color
[i
].r
,
184 pdc_color
[i
].g
, pdc_color
[i
].b
);
186 SDL_EnableUNICODE(1);
191 PDC_set_title(argc
? argv
[0] : "PDCurses");
193 SP
->lines
= PDC_get_rows();
194 SP
->cols
= PDC_get_columns();
196 SP
->mouse_wait
= PDC_CLICK_PERIOD
;
199 PDC_reset_prog_mode();
204 /* the core of resize_term() */
206 int PDC_resize_screen(int nlines
, int ncols
)
213 pdc_sheight
= nlines
* pdc_fheight
;
214 pdc_swidth
= ncols
* pdc_fwidth
;
217 SDL_FreeSurface(pdc_screen
);
219 pdc_screen
= SDL_SetVideoMode(pdc_swidth
, pdc_sheight
, 0,
220 SDL_SWSURFACE
|SDL_ANYFORMAT
|SDL_RESIZABLE
);
226 SP
->cursrow
= SP
->curscol
= 0;
231 void PDC_reset_prog_mode(void)
233 PDC_LOG(("PDC_reset_prog_mode() - called.\n"));
236 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
239 void PDC_reset_shell_mode(void)
241 PDC_LOG(("PDC_reset_shell_mode() - called.\n"));
243 SDL_EnableKeyRepeat(0, 0);
247 void PDC_restore_screen_mode(int i
)
251 void PDC_save_screen_mode(int i
)
255 void PDC_init_pair(short pair
, short fg
, short bg
)
261 int PDC_pair_content(short pair
, short *fg
, short *bg
)
263 *fg
= atrtab
[pair
].f
;
264 *bg
= atrtab
[pair
].b
;
269 bool PDC_can_change_color(void)
274 int PDC_color_content(short color
, short *red
, short *green
, short *blue
)
276 *red
= DIVROUND(pdc_color
[color
].r
* 1000, 255);
277 *green
= DIVROUND(pdc_color
[color
].g
* 1000, 255);
278 *blue
= DIVROUND(pdc_color
[color
].b
* 1000, 255);
283 int PDC_init_color(short color
, short red
, short green
, short blue
)
285 pdc_color
[color
].r
= DIVROUND(red
* 255, 1000);
286 pdc_color
[color
].g
= DIVROUND(green
* 255, 1000);
287 pdc_color
[color
].b
= DIVROUND(blue
* 255, 1000);
289 pdc_mapped
[color
] = SDL_MapRGB(pdc_screen
->format
, pdc_color
[color
].r
,
290 pdc_color
[color
].g
, pdc_color
[color
].b
);