ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/Frodo4/Src/Display_SDL.h
Revision: 1.7
Committed: 2005-06-27T19:55:48Z (19 years, 4 months ago) by cebix
Content type: text/plain
Branch: MAIN
CVS Tags: VERSION_4_2
Changes since 1.6: +1 -1 lines
Log Message:
updated copyright dates

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * Display_SDL.h - C64 graphics display, emulator window handling,
3     * SDL specific stuff
4     *
5 cebix 1.7 * Frodo (C) 1994-1997,2002-2005 Christian Bauer
6 cebix 1.1 *
7     * This program is free software; you can redistribute it and/or modify
8     * it under the terms of the GNU General Public License as published by
9     * the Free Software Foundation; either version 2 of the License, or
10     * (at your option) any later version.
11     *
12     * This program is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     * GNU General Public License for more details.
16     *
17     * You should have received a copy of the GNU General Public License
18     * along with this program; if not, write to the Free Software
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20     */
21    
22     #include "C64.h"
23     #include "SAM.h"
24     #include "Version.h"
25    
26     #include <SDL.h>
27    
28    
29     // Display surface
30     static SDL_Surface *screen = NULL;
31    
32     // Keyboard
33     static bool num_locked = false;
34    
35     // For LED error blinking
36     static C64Display *c64_disp;
37     static struct sigaction pulse_sa;
38     static itimerval pulse_tv;
39    
40 cebix 1.4 // SDL joysticks
41     static SDL_Joystick *joy[2] = {NULL, NULL};
42    
43 cebix 1.1 // Colors for speedometer/drive LEDs
44     enum {
45     black = 0,
46     white = 1,
47     fill_gray = 16,
48     shine_gray = 17,
49     shadow_gray = 18,
50     red = 19,
51     green = 20,
52     PALETTE_SIZE = 21
53     };
54    
55     /*
56     C64 keyboard matrix:
57    
58     Bit 7 6 5 4 3 2 1 0
59     0 CUD F5 F3 F1 F7 CLR RET DEL
60     1 SHL E S Z 4 A W 3
61     2 X T F C 6 D R 5
62     3 V U H B 8 G Y 7
63     4 N O K M 0 J I 9
64     5 , @ : . - L P +
65     6 / ^ = SHR HOM ; * £
66     7 R/S Q C= SPC 2 CTL <- 1
67     */
68    
69     #define MATRIX(a,b) (((a) << 3) | (b))
70    
71    
72     /*
73     * Open window
74     */
75    
76     int init_graphics(void)
77     {
78     // Init SDL
79 cebix 1.4 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
80 cebix 1.1 fprintf(stderr, "Couldn't initialize SDL (%s)\n", SDL_GetError());
81     return 0;
82     }
83    
84     return 1;
85     }
86    
87    
88     /*
89     * Display constructor
90     */
91    
92     C64Display::C64Display(C64 *the_c64) : TheC64(the_c64)
93     {
94     quit_requested = false;
95     speedometer_string[0] = 0;
96 cebix 1.5
97     // Open window
98     SDL_WM_SetCaption(VERSION_STRING, "Frodo");
99     screen = SDL_SetVideoMode(DISPLAY_X, DISPLAY_Y + 17, 8, SDL_DOUBLEBUF | (ThePrefs.DisplayType == DISPTYPE_SCREEN ? SDL_FULLSCREEN : 0));
100 cebix 1.1
101     // LEDs off
102     for (int i=0; i<4; i++)
103     led_state[i] = old_led_state[i] = LED_OFF;
104    
105     // Start timer for LED error blinking
106     c64_disp = this;
107     pulse_sa.sa_handler = (void (*)(int))pulse_handler;
108 cebix 1.3 pulse_sa.sa_flags = SA_RESTART;
109 cebix 1.1 sigemptyset(&pulse_sa.sa_mask);
110     sigaction(SIGALRM, &pulse_sa, NULL);
111     pulse_tv.it_interval.tv_sec = 0;
112     pulse_tv.it_interval.tv_usec = 400000;
113     pulse_tv.it_value.tv_sec = 0;
114     pulse_tv.it_value.tv_usec = 400000;
115     setitimer(ITIMER_REAL, &pulse_tv, NULL);
116     }
117    
118    
119     /*
120     * Display destructor
121     */
122    
123     C64Display::~C64Display()
124     {
125     SDL_Quit();
126     }
127    
128    
129     /*
130     * Prefs may have changed
131     */
132    
133     void C64Display::NewPrefs(Prefs *prefs)
134     {
135     }
136    
137    
138     /*
139     * Redraw bitmap
140     */
141    
142     void C64Display::Update(void)
143     {
144     // Draw speedometer/LEDs
145     SDL_Rect r = {0, DISPLAY_Y, DISPLAY_X, 15};
146     SDL_FillRect(screen, &r, fill_gray);
147     r.w = DISPLAY_X; r.h = 1;
148     SDL_FillRect(screen, &r, shine_gray);
149     r.y = DISPLAY_Y + 14;
150     SDL_FillRect(screen, &r, shadow_gray);
151     r.w = 16;
152     for (int i=2; i<6; i++) {
153     r.x = DISPLAY_X * i/5 - 24; r.y = DISPLAY_Y + 4;
154     SDL_FillRect(screen, &r, shadow_gray);
155     r.y = DISPLAY_Y + 10;
156     SDL_FillRect(screen, &r, shine_gray);
157     }
158     r.y = DISPLAY_Y; r.w = 1; r.h = 15;
159     for (int i=0; i<5; i++) {
160     r.x = DISPLAY_X * i / 5;
161     SDL_FillRect(screen, &r, shine_gray);
162     r.x = DISPLAY_X * (i+1) / 5 - 1;
163     SDL_FillRect(screen, &r, shadow_gray);
164     }
165     r.y = DISPLAY_Y + 4; r.h = 7;
166     for (int i=2; i<6; i++) {
167     r.x = DISPLAY_X * i/5 - 24;
168     SDL_FillRect(screen, &r, shadow_gray);
169     r.x = DISPLAY_X * i/5 - 9;
170     SDL_FillRect(screen, &r, shine_gray);
171     }
172     r.y = DISPLAY_Y + 5; r.w = 14; r.h = 5;
173     for (int i=0; i<4; i++) {
174     r.x = DISPLAY_X * (i+2) / 5 - 23;
175     int c;
176     switch (led_state[i]) {
177     case LED_ON:
178     c = green;
179     break;
180     case LED_ERROR_ON:
181     c = red;
182     break;
183     default:
184     c = black;
185     break;
186     }
187     SDL_FillRect(screen, &r, c);
188     }
189    
190     draw_string(screen, DISPLAY_X * 1/5 + 8, DISPLAY_Y + 4, "D\x12 8", black, fill_gray);
191     draw_string(screen, DISPLAY_X * 2/5 + 8, DISPLAY_Y + 4, "D\x12 9", black, fill_gray);
192     draw_string(screen, DISPLAY_X * 3/5 + 8, DISPLAY_Y + 4, "D\x12 10", black, fill_gray);
193     draw_string(screen, DISPLAY_X * 4/5 + 8, DISPLAY_Y + 4, "D\x12 11", black, fill_gray);
194     draw_string(screen, 24, DISPLAY_Y + 4, speedometer_string, black, fill_gray);
195    
196     // Update display
197     SDL_Flip(screen);
198     }
199    
200    
201     /*
202     * Draw string into surface using the C64 ROM font
203     */
204    
205     void C64Display::draw_string(SDL_Surface *s, int x, int y, const char *str, uint8 front_color, uint8 back_color)
206     {
207     uint8 *pb = (uint8 *)s->pixels + s->pitch*y + x;
208     char c;
209     while ((c = *str++) != 0) {
210     uint8 *q = TheC64->Char + c*8 + 0x800;
211     uint8 *p = pb;
212     for (int y=0; y<8; y++) {
213     uint8 v = *q++;
214     p[0] = (v & 0x80) ? front_color : back_color;
215     p[1] = (v & 0x40) ? front_color : back_color;
216     p[2] = (v & 0x20) ? front_color : back_color;
217     p[3] = (v & 0x10) ? front_color : back_color;
218     p[4] = (v & 0x08) ? front_color : back_color;
219     p[5] = (v & 0x04) ? front_color : back_color;
220     p[6] = (v & 0x02) ? front_color : back_color;
221     p[7] = (v & 0x01) ? front_color : back_color;
222     p += s->pitch;
223     }
224     pb += 8;
225     }
226     }
227    
228    
229     /*
230     * LED error blink
231     */
232    
233     void C64Display::pulse_handler(...)
234     {
235     for (int i=0; i<4; i++)
236     switch (c64_disp->led_state[i]) {
237     case LED_ERROR_ON:
238     c64_disp->led_state[i] = LED_ERROR_OFF;
239     break;
240     case LED_ERROR_OFF:
241     c64_disp->led_state[i] = LED_ERROR_ON;
242     break;
243     }
244     }
245    
246    
247     /*
248     * Draw speedometer
249     */
250    
251     void C64Display::Speedometer(int speed)
252     {
253     static int delay = 0;
254    
255     if (delay >= 20) {
256     delay = 0;
257     sprintf(speedometer_string, "%d%%", speed);
258     } else
259     delay++;
260     }
261    
262    
263     /*
264     * Return pointer to bitmap data
265     */
266    
267     uint8 *C64Display::BitmapBase(void)
268     {
269     return (uint8 *)screen->pixels;
270     }
271    
272    
273     /*
274     * Return number of bytes per row
275     */
276    
277     int C64Display::BitmapXMod(void)
278     {
279     return screen->pitch;
280     }
281    
282    
283     /*
284     * Poll the keyboard
285     */
286    
287     static void translate_key(SDLKey key, bool key_up, uint8 *key_matrix, uint8 *rev_matrix, uint8 *joystick)
288     {
289     int c64_key = -1;
290     switch (key) {
291     case SDLK_a: c64_key = MATRIX(1,2); break;
292     case SDLK_b: c64_key = MATRIX(3,4); break;
293     case SDLK_c: c64_key = MATRIX(2,4); break;
294     case SDLK_d: c64_key = MATRIX(2,2); break;
295     case SDLK_e: c64_key = MATRIX(1,6); break;
296     case SDLK_f: c64_key = MATRIX(2,5); break;
297     case SDLK_g: c64_key = MATRIX(3,2); break;
298     case SDLK_h: c64_key = MATRIX(3,5); break;
299     case SDLK_i: c64_key = MATRIX(4,1); break;
300     case SDLK_j: c64_key = MATRIX(4,2); break;
301     case SDLK_k: c64_key = MATRIX(4,5); break;
302     case SDLK_l: c64_key = MATRIX(5,2); break;
303     case SDLK_m: c64_key = MATRIX(4,4); break;
304     case SDLK_n: c64_key = MATRIX(4,7); break;
305     case SDLK_o: c64_key = MATRIX(4,6); break;
306     case SDLK_p: c64_key = MATRIX(5,1); break;
307     case SDLK_q: c64_key = MATRIX(7,6); break;
308     case SDLK_r: c64_key = MATRIX(2,1); break;
309     case SDLK_s: c64_key = MATRIX(1,5); break;
310     case SDLK_t: c64_key = MATRIX(2,6); break;
311     case SDLK_u: c64_key = MATRIX(3,6); break;
312     case SDLK_v: c64_key = MATRIX(3,7); break;
313     case SDLK_w: c64_key = MATRIX(1,1); break;
314     case SDLK_x: c64_key = MATRIX(2,7); break;
315     case SDLK_y: c64_key = MATRIX(3,1); break;
316     case SDLK_z: c64_key = MATRIX(1,4); break;
317    
318     case SDLK_0: c64_key = MATRIX(4,3); break;
319     case SDLK_1: c64_key = MATRIX(7,0); break;
320     case SDLK_2: c64_key = MATRIX(7,3); break;
321     case SDLK_3: c64_key = MATRIX(1,0); break;
322     case SDLK_4: c64_key = MATRIX(1,3); break;
323     case SDLK_5: c64_key = MATRIX(2,0); break;
324     case SDLK_6: c64_key = MATRIX(2,3); break;
325     case SDLK_7: c64_key = MATRIX(3,0); break;
326     case SDLK_8: c64_key = MATRIX(3,3); break;
327     case SDLK_9: c64_key = MATRIX(4,0); break;
328    
329     case SDLK_SPACE: c64_key = MATRIX(7,4); break;
330     case SDLK_BACKQUOTE: c64_key = MATRIX(7,1); break;
331     case SDLK_BACKSLASH: c64_key = MATRIX(6,6); break;
332     case SDLK_COMMA: c64_key = MATRIX(5,7); break;
333     case SDLK_PERIOD: c64_key = MATRIX(5,4); break;
334     case SDLK_MINUS: c64_key = MATRIX(5,0); break;
335     case SDLK_EQUALS: c64_key = MATRIX(5,3); break;
336     case SDLK_LEFTBRACKET: c64_key = MATRIX(5,6); break;
337     case SDLK_RIGHTBRACKET: c64_key = MATRIX(6,1); break;
338     case SDLK_SEMICOLON: c64_key = MATRIX(5,5); break;
339     case SDLK_QUOTE: c64_key = MATRIX(6,2); break;
340     case SDLK_SLASH: c64_key = MATRIX(6,7); break;
341    
342     case SDLK_ESCAPE: c64_key = MATRIX(7,7); break;
343     case SDLK_RETURN: c64_key = MATRIX(0,1); break;
344     case SDLK_BACKSPACE: case SDLK_DELETE: c64_key = MATRIX(0,0); break;
345     case SDLK_INSERT: c64_key = MATRIX(6,3); break;
346     case SDLK_HOME: c64_key = MATRIX(6,3); break;
347     case SDLK_END: c64_key = MATRIX(6,0); break;
348     case SDLK_PAGEUP: c64_key = MATRIX(6,0); break;
349     case SDLK_PAGEDOWN: c64_key = MATRIX(6,5); break;
350    
351     case SDLK_LCTRL: case SDLK_TAB: c64_key = MATRIX(7,2); break;
352     case SDLK_RCTRL: c64_key = MATRIX(7,5); break;
353     case SDLK_LSHIFT: c64_key = MATRIX(1,7); break;
354     case SDLK_RSHIFT: c64_key = MATRIX(6,4); break;
355     case SDLK_LALT: case SDLK_LMETA: c64_key = MATRIX(7,5); break;
356     case SDLK_RALT: case SDLK_RMETA: c64_key = MATRIX(7,5); break;
357    
358     case SDLK_UP: c64_key = MATRIX(0,7)| 0x80; break;
359     case SDLK_DOWN: c64_key = MATRIX(0,7); break;
360     case SDLK_LEFT: c64_key = MATRIX(0,2) | 0x80; break;
361     case SDLK_RIGHT: c64_key = MATRIX(0,2); break;
362    
363     case SDLK_F1: c64_key = MATRIX(0,4); break;
364     case SDLK_F2: c64_key = MATRIX(0,4) | 0x80; break;
365     case SDLK_F3: c64_key = MATRIX(0,5); break;
366     case SDLK_F4: c64_key = MATRIX(0,5) | 0x80; break;
367     case SDLK_F5: c64_key = MATRIX(0,6); break;
368     case SDLK_F6: c64_key = MATRIX(0,6) | 0x80; break;
369     case SDLK_F7: c64_key = MATRIX(0,3); break;
370     case SDLK_F8: c64_key = MATRIX(0,3) | 0x80; break;
371    
372     case SDLK_KP0: case SDLK_KP5: c64_key = 0x10 | 0x40; break;
373     case SDLK_KP1: c64_key = 0x06 | 0x40; break;
374     case SDLK_KP2: c64_key = 0x02 | 0x40; break;
375     case SDLK_KP3: c64_key = 0x0a | 0x40; break;
376     case SDLK_KP4: c64_key = 0x04 | 0x40; break;
377     case SDLK_KP6: c64_key = 0x08 | 0x40; break;
378     case SDLK_KP7: c64_key = 0x05 | 0x40; break;
379     case SDLK_KP8: c64_key = 0x01 | 0x40; break;
380     case SDLK_KP9: c64_key = 0x09 | 0x40; break;
381    
382     case SDLK_KP_DIVIDE: c64_key = MATRIX(6,7); break;
383     case SDLK_KP_ENTER: c64_key = MATRIX(0,1); break;
384     default: break;
385     }
386    
387     if (c64_key < 0)
388     return;
389    
390     // Handle joystick emulation
391     if (c64_key & 0x40) {
392     c64_key &= 0x1f;
393     if (key_up)
394     *joystick |= c64_key;
395     else
396     *joystick &= ~c64_key;
397     return;
398     }
399    
400     // Handle other keys
401     bool shifted = c64_key & 0x80;
402     int c64_byte = (c64_key >> 3) & 7;
403     int c64_bit = c64_key & 7;
404     if (key_up) {
405     if (shifted) {
406     key_matrix[6] |= 0x10;
407     rev_matrix[4] |= 0x40;
408     }
409     key_matrix[c64_byte] |= (1 << c64_bit);
410     rev_matrix[c64_bit] |= (1 << c64_byte);
411     } else {
412     if (shifted) {
413     key_matrix[6] &= 0xef;
414     rev_matrix[4] &= 0xbf;
415     }
416     key_matrix[c64_byte] &= ~(1 << c64_bit);
417     rev_matrix[c64_bit] &= ~(1 << c64_byte);
418     }
419     }
420    
421     void C64Display::PollKeyboard(uint8 *key_matrix, uint8 *rev_matrix, uint8 *joystick)
422     {
423     SDL_Event event;
424     while (SDL_PollEvent(&event)) {
425     switch (event.type) {
426    
427     // Key pressed
428     case SDL_KEYDOWN:
429     switch (event.key.keysym.sym) {
430    
431     case SDLK_F9: // F9: Invoke SAM
432     SAM(TheC64);
433     break;
434    
435     case SDLK_F10: // F10: Quit
436     quit_requested = true;
437     break;
438    
439     case SDLK_F11: // F11: NMI (Restore)
440     TheC64->NMI();
441     break;
442    
443     case SDLK_F12: // F12: Reset
444     TheC64->Reset();
445     break;
446    
447     case SDLK_NUMLOCK:
448     num_locked = true;
449     break;
450    
451     case SDLK_KP_PLUS: // '+' on keypad: Increase SkipFrames
452     ThePrefs.SkipFrames++;
453     break;
454    
455     case SDLK_KP_MINUS: // '-' on keypad: Decrease SkipFrames
456     if (ThePrefs.SkipFrames > 1)
457     ThePrefs.SkipFrames--;
458     break;
459    
460     case SDLK_KP_MULTIPLY: // '*' on keypad: Toggle speed limiter
461     ThePrefs.LimitSpeed = !ThePrefs.LimitSpeed;
462     break;
463    
464     default:
465     translate_key(event.key.keysym.sym, false, key_matrix, rev_matrix, joystick);
466     break;
467     }
468     break;
469    
470     // Key released
471     case SDL_KEYUP:
472     if (event.key.keysym.sym == SDLK_NUMLOCK)
473     num_locked = false;
474     else
475     translate_key(event.key.keysym.sym, true, key_matrix, rev_matrix, joystick);
476     break;
477    
478     // Quit Frodo
479     case SDL_QUIT:
480     quit_requested = true;
481     break;
482     }
483     }
484     }
485    
486    
487     /*
488     * Check if NumLock is down (for switching the joystick keyboard emulation)
489     */
490    
491     bool C64Display::NumLock(void)
492     {
493     return num_locked;
494 cebix 1.4 }
495    
496    
497     /*
498     * Open/close joystick drivers given old and new state of
499     * joystick preferences
500     */
501    
502     void C64::open_close_joystick(int port, int oldjoy, int newjoy)
503     {
504     if (oldjoy != newjoy) {
505     joy_minx[port] = joy_miny[port] = 32767; // Reset calibration
506     joy_maxx[port] = joy_maxy[port] = -32768;
507     if (newjoy) {
508     joy[port] = SDL_JoystickOpen(newjoy - 1);
509     if (joy[port] == NULL)
510     fprintf(stderr, "Couldn't open joystick %d\n", port + 1);
511     } else {
512     if (joy[port]) {
513     SDL_JoystickClose(joy[port]);
514     joy[port] = NULL;
515     }
516     }
517     }
518     }
519    
520     void C64::open_close_joysticks(int oldjoy1, int oldjoy2, int newjoy1, int newjoy2)
521     {
522     open_close_joystick(0, oldjoy1, newjoy1);
523     open_close_joystick(1, oldjoy2, newjoy2);
524     }
525    
526    
527     /*
528     * Poll joystick port, return CIA mask
529     */
530    
531     uint8 C64::poll_joystick(int port)
532     {
533     uint8 j = 0xff;
534    
535     if (port == 0 && (joy[0] || joy[1]))
536     SDL_JoystickUpdate();
537    
538     if (joy[port]) {
539     int x = SDL_JoystickGetAxis(joy[port], 0), y = SDL_JoystickGetAxis(joy[port], 1);
540    
541     if (x > joy_maxx[port])
542     joy_maxx[port] = x;
543     if (x < joy_minx[port])
544     joy_minx[port] = x;
545     if (y > joy_maxy[port])
546     joy_maxy[port] = y;
547     if (y < joy_miny[port])
548     joy_miny[port] = y;
549    
550     if (joy_maxx[port] - joy_minx[port] < 100 || joy_maxy[port] - joy_miny[port] < 100)
551     return 0xff;
552    
553     if (x < (joy_minx[port] + (joy_maxx[port]-joy_minx[port])/3))
554     j &= 0xfb; // Left
555     else if (x > (joy_minx[port] + 2*(joy_maxx[port]-joy_minx[port])/3))
556     j &= 0xf7; // Right
557    
558     if (y < (joy_miny[port] + (joy_maxy[port]-joy_miny[port])/3))
559     j &= 0xfe; // Up
560     else if (y > (joy_miny[port] + 2*(joy_maxy[port]-joy_miny[port])/3))
561     j &= 0xfd; // Down
562    
563     if (SDL_JoystickGetButton(joy[port], 0))
564     j &= 0xef; // Button
565     }
566    
567     return j;
568 cebix 1.1 }
569    
570    
571     /*
572     * Allocate C64 colors
573     */
574    
575     void C64Display::InitColors(uint8 *colors)
576     {
577     SDL_Color palette[PALETTE_SIZE];
578     for (int i=0; i<16; i++) {
579     palette[i].r = palette_red[i];
580     palette[i].g = palette_green[i];
581     palette[i].b = palette_blue[i];
582     }
583     palette[fill_gray].r = palette[fill_gray].g = palette[fill_gray].b = 0xd0;
584     palette[shine_gray].r = palette[shine_gray].g = palette[shine_gray].b = 0xf0;
585     palette[shadow_gray].r = palette[shadow_gray].g = palette[shadow_gray].b = 0x80;
586     palette[red].r = 0xf0;
587     palette[red].g = palette[red].b = 0;
588     palette[green].g = 0xf0;
589     palette[green].r = palette[green].b = 0;
590     SDL_SetColors(screen, palette, 0, PALETTE_SIZE);
591    
592     for (int i=0; i<256; i++)
593     colors[i] = i & 0x0f;
594     }
595    
596    
597     /*
598     * Show a requester (error message)
599     */
600    
601     long int ShowRequester(char *a,char *b,char *)
602     {
603     printf("%s: %s\n", a, b);
604     return 1;
605     }