ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/SDL/video_sdl.cpp
(Generate patch)

Comparing BasiliskII/src/SDL/video_sdl.cpp (file contents):
Revision 1.3 by gbeauche, 2004-06-23T22:55:47Z vs.
Revision 1.5 by gbeauche, 2004-06-24T21:46:55Z

# Line 43 | Line 43
43   #include <SDL_mutex.h>
44   #include <SDL_thread.h>
45   #include <errno.h>
46 + #include <vector>
47  
48   #include "cpu_emulation.h"
49   #include "main.h"
# Line 51 | Line 52
52   #include "prefs.h"
53   #include "user_strings.h"
54   #include "video.h"
55 + #include "video_defs.h"
56   #include "video_blit.h"
57  
58   #define DEBUG 0
# Line 58 | Line 60
60  
61  
62   // Supported video modes
63 < static vector<video_mode> VideoModes;
63 > using std::vector;
64 > static vector<VIDEO_MODE> VideoModes;
65  
66   // Display types
67 + #ifdef SHEEPSHAVER
68   enum {
69 <        DISPLAY_WINDOW, // windowed display
70 <        DISPLAY_SCREEN  // fullscreen display
69 >        DISPLAY_WINDOW = DIS_WINDOW,                                    // windowed display
70 >        DISPLAY_SCREEN = DIS_SCREEN                                             // fullscreen display
71   };
72 + extern int display_type;                                                        // See enum above
73 + #else
74 + enum {
75 +        DISPLAY_WINDOW,                                                                 // windowed display
76 +        DISPLAY_SCREEN                                                                  // fullscreen display
77 + };
78 + static int display_type = DISPLAY_WINDOW;                       // See enum above
79 + #endif
80  
81   // Constants
82   const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
# Line 75 | Line 87 | static int32 frame_skip;                                                       // Prefs
87   static int16 mouse_wheel_mode;
88   static int16 mouse_wheel_lines;
89  
78 static int display_type = DISPLAY_WINDOW;                       // See enum above
90   static uint8 *the_buffer = NULL;                                        // Mac frame buffer (where MacOS draws into)
91   static uint8 *the_buffer_copy = NULL;                           // Copy of Mac frame buffer (for refreshed modes)
92   static uint32 the_buffer_size;                                          // Size of allocated the_buffer
# Line 130 | Line 141 | extern void SysMountFirstFloppy(void);
141  
142  
143   /*
144 + *  SheepShaver glue
145 + */
146 +
147 + #ifdef SHEEPSHAVER
148 + // Color depth modes type
149 + typedef int video_depth;
150 +
151 + // 1, 2, 4 and 8 bit depths use a color palette
152 + static inline bool IsDirectMode(VIDEO_MODE const & mode)
153 + {
154 +        return IsDirectMode(mode.viAppleMode);
155 + }
156 +
157 + // Abstract base class representing one (possibly virtual) monitor
158 + // ("monitor" = rectangular display with a contiguous frame buffer)
159 + class monitor_desc {
160 + public:
161 +        monitor_desc(const vector<VIDEO_MODE> &available_modes, video_depth default_depth, uint32 default_id) {}
162 +        virtual ~monitor_desc() {}
163 +
164 +        // Get current Mac frame buffer base address
165 +        uint32 get_mac_frame_base(void) const {return screen_base;}
166 +
167 +        // Set Mac frame buffer base address (called from switch_to_mode())
168 +        void set_mac_frame_base(uint32 base) {screen_base = base;}
169 +
170 +        // Get current video mode
171 +        const VIDEO_MODE &get_current_mode(void) const {return VModes[cur_mode];}
172 +
173 +        // Called by the video driver to switch the video mode on this display
174 +        // (must call set_mac_frame_base())
175 +        virtual void switch_to_current_mode(void) = 0;
176 +
177 +        // Called by the video driver to set the color palette (in indexed modes)
178 +        // or the gamma table (in direct modes)
179 +        virtual void set_palette(uint8 *pal, int num) = 0;
180 + };
181 +
182 + // Vector of pointers to available monitor descriptions, filled by VideoInit()
183 + static vector<monitor_desc *> VideoMonitors;
184 +
185 + // Find Apple mode matching best specified dimensions
186 + static int find_apple_resolution(int xsize, int ysize)
187 + {
188 +        int apple_id;
189 +        if (xsize < 800)
190 +                apple_id = APPLE_640x480;
191 +        else if (xsize < 1024)
192 +                apple_id = APPLE_800x600;
193 +        else if (xsize < 1152)
194 +                apple_id = APPLE_1024x768;
195 +        else if (xsize < 1280) {
196 +                if (ysize < 900)
197 +                        apple_id = APPLE_1152x768;
198 +                else
199 +                        apple_id = APPLE_1152x900;
200 +        }
201 +        else if (xsize < 1600)
202 +                apple_id = APPLE_1280x1024;
203 +        else
204 +                apple_id = APPLE_1600x1200;
205 +        return apple_id;
206 + }
207 +
208 + // Set parameters to specified Apple mode
209 + static void set_apple_resolution(int apple_id, int &xsize, int &ysize)
210 + {
211 +        switch (apple_id) {
212 +        case APPLE_640x480:
213 +                xsize = 640;
214 +                ysize = 480;
215 +                break;
216 +        case APPLE_800x600:
217 +                xsize = 800;
218 +                ysize = 600;
219 +                break;
220 +        case APPLE_1024x768:
221 +                xsize = 1024;
222 +                ysize = 768;
223 +                break;
224 +        case APPLE_1152x768:
225 +                xsize = 1152;
226 +                ysize = 768;
227 +                break;
228 +        case APPLE_1152x900:
229 +                xsize = 1152;
230 +                ysize = 900;
231 +                break;
232 +        case APPLE_1280x1024:
233 +                xsize = 1280;
234 +                ysize = 1024;
235 +                break;
236 +        case APPLE_1600x1200:
237 +                xsize = 1600;
238 +                ysize = 1200;
239 +                break;
240 +        default:
241 +                abort();
242 +        }
243 + }
244 +
245 + // Match Apple mode matching best specified dimensions
246 + static int match_apple_resolution(int &xsize, int &ysize)
247 + {
248 +        int apple_id = find_apple_resolution(xsize, ysize);
249 +        set_apple_resolution(apple_id, xsize, ysize);
250 +        return apple_id;
251 + }
252 +
253 + // Display error alert
254 + static void ErrorAlert(int error)
255 + {
256 +        ErrorAlert(GetString(error));
257 + }
258 + #endif
259 +
260 +
261 + /*
262   *  monitor_desc subclass for SDL display
263   */
264  
265   class SDL_monitor_desc : public monitor_desc {
266   public:
267 <        SDL_monitor_desc(const vector<video_mode> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
267 >        SDL_monitor_desc(const vector<VIDEO_MODE> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
268          ~SDL_monitor_desc() {}
269  
270          virtual void switch_to_current_mode(void);
# Line 150 | Line 279 | public:
279   *  Utility functions
280   */
281  
282 + // Find palette size for given color depth
283 + static int palette_size(int mode)
284 + {
285 +        switch (mode) {
286 +        case VIDEO_DEPTH_1BIT: return 2;
287 +        case VIDEO_DEPTH_2BIT: return 4;
288 +        case VIDEO_DEPTH_4BIT: return 16;
289 +        case VIDEO_DEPTH_8BIT: return 256;
290 +        case VIDEO_DEPTH_16BIT: return 32;
291 +        case VIDEO_DEPTH_32BIT: return 256;
292 +        default: return 0;
293 +        }
294 + }
295 +
296 + // Return bytes per pixel for requested depth
297 + static inline int bytes_per_pixel(int depth)
298 + {
299 +        int bpp;
300 +        switch (depth) {
301 +        case 8:
302 +                bpp = 1;
303 +                break;
304 +        case 15: case 16:
305 +                bpp = 2;
306 +                break;
307 +        case 24: case 32:
308 +                bpp = 4;
309 +                break;
310 +        default:
311 +                abort();
312 +        }
313 +        return bpp;
314 + }
315 +
316   // Map video_mode depth ID to numerical depth value
317   static int sdl_depth_of_video_depth(int video_depth)
318   {
319          int depth = -1;
320          switch (video_depth) {
321 <        case VDEPTH_1BIT:
321 >        case VIDEO_DEPTH_1BIT:
322                  depth = 1;
323                  break;
324 <        case VDEPTH_2BIT:
324 >        case VIDEO_DEPTH_2BIT:
325                  depth = 2;
326                  break;
327 <        case VDEPTH_4BIT:
327 >        case VIDEO_DEPTH_4BIT:
328                  depth = 4;
329                  break;
330 <        case VDEPTH_8BIT:
330 >        case VIDEO_DEPTH_8BIT:
331                  depth = 8;
332                  break;
333 <        case VDEPTH_16BIT:
333 >        case VIDEO_DEPTH_16BIT:
334                  depth = 16;
335                  break;
336 <        case VDEPTH_32BIT:
336 >        case VIDEO_DEPTH_32BIT:
337                  depth = 32;
338                  break;
339          default:
# Line 179 | Line 342 | static int sdl_depth_of_video_depth(int
342          return depth;
343   }
344  
345 + // Check wether specified mode is available
346 + static bool has_mode(int type, int width, int height)
347 + {
348 +        // FIXME: no fullscreen support yet
349 +        if (type == DISPLAY_SCREEN)
350 +                return false;
351 +
352 + #ifdef SHEEPSHAVER
353 +        // Filter out Classic resolutiosn
354 +        if (width == 512 && height == 384)
355 +                return false;
356 +
357 +        // Read window modes prefs
358 +        static uint32 window_modes = 0;
359 +        static uint32 screen_modes = 0;
360 +        if (window_modes == 0 || screen_modes == 0) {
361 +                window_modes = PrefsFindInt32("windowmodes");
362 +                screen_modes = PrefsFindInt32("screenmodes");
363 +                if (window_modes == 0 || screen_modes == 0)
364 +                        window_modes |= 3;                      // Allow at least 640x480 and 800x600 window modes
365 +        }
366 +
367 +        if (type == DISPLAY_WINDOW) {
368 +                int apple_mask, apple_id = find_apple_resolution(width, height);
369 +                switch (apple_id) {
370 +                case APPLE_640x480:             apple_mask = 0x01; break;
371 +                case APPLE_800x600:             apple_mask = 0x02; break;
372 +                case APPLE_1024x768:    apple_mask = 0x04; break;
373 +                case APPLE_1152x768:    apple_mask = 0x40; break;
374 +                case APPLE_1152x900:    apple_mask = 0x08; break;
375 +                case APPLE_1280x1024:   apple_mask = 0x10; break;
376 +                case APPLE_1600x1200:   apple_mask = 0x20; break;
377 +                default:                                apple_mask = 0x00; break;
378 +                }
379 +                return (window_modes & apple_mask);
380 +        }
381 + #else
382 +        return true;
383 + #endif
384 +        return false;
385 + }
386 +
387   // Add mode to list of supported modes
388 < static void add_mode(uint32 width, uint32 height, uint32 resolution_id, uint32 bytes_per_row, video_depth depth)
388 > static void add_mode(int type, int width, int height, int resolution_id, int bytes_per_row, int depth)
389   {
390 <        video_mode mode;
391 <        mode.x = width;
392 <        mode.y = height;
393 <        mode.resolution_id = resolution_id;
394 <        mode.bytes_per_row = bytes_per_row;
395 <        mode.depth = depth;
390 >        // Filter out unsupported modes
391 >        if (!has_mode(type, width, height))
392 >                return;
393 >
394 >        // Fill in VideoMode entry
395 >        VIDEO_MODE mode;
396 > #ifdef SHEEPSHAVER
397 >        // Recalculate dimensions to fit Apple modes
398 >        resolution_id = match_apple_resolution(width, height);
399 >        mode.viType = type;
400 > #endif
401 >        VIDEO_MODE_X = width;
402 >        VIDEO_MODE_Y = height;
403 >        VIDEO_MODE_RESOLUTION = resolution_id;
404 >        VIDEO_MODE_ROW_BYTES = bytes_per_row;
405 >        VIDEO_MODE_DEPTH = depth;
406          VideoModes.push_back(mode);
407   }
408  
409   // Add standard list of windowed modes for given color depth
410 < static void add_window_modes(video_depth depth)
410 > static void add_window_modes(int depth)
411   {
412 <        add_mode(512, 384, 0x80, TrivialBytesPerRow(512, depth), depth);
413 <        add_mode(640, 480, 0x81, TrivialBytesPerRow(640, depth), depth);
414 <        add_mode(800, 600, 0x82, TrivialBytesPerRow(800, depth), depth);
415 <        add_mode(1024, 768, 0x83, TrivialBytesPerRow(1024, depth), depth);
416 <        add_mode(1152, 870, 0x84, TrivialBytesPerRow(1152, depth), depth);
417 <        add_mode(1280, 1024, 0x85, TrivialBytesPerRow(1280, depth), depth);
418 <        add_mode(1600, 1200, 0x86, TrivialBytesPerRow(1600, depth), depth);
412 >        video_depth vdepth = (video_depth)depth;
413 >        add_mode(DISPLAY_WINDOW, 512, 384, 0x80, TrivialBytesPerRow(512, vdepth), depth);
414 >        add_mode(DISPLAY_WINDOW, 640, 480, 0x81, TrivialBytesPerRow(640, vdepth), depth);
415 >        add_mode(DISPLAY_WINDOW, 800, 600, 0x82, TrivialBytesPerRow(800, vdepth), depth);
416 >        add_mode(DISPLAY_WINDOW, 1024, 768, 0x83, TrivialBytesPerRow(1024, vdepth), depth);
417 >        add_mode(DISPLAY_WINDOW, 1152, 870, 0x84, TrivialBytesPerRow(1152, vdepth), depth);
418 >        add_mode(DISPLAY_WINDOW, 1280, 1024, 0x85, TrivialBytesPerRow(1280, vdepth), depth);
419 >        add_mode(DISPLAY_WINDOW, 1600, 1200, 0x86, TrivialBytesPerRow(1600, vdepth), depth);
420   }
421  
422   // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
423 < static void set_mac_frame_buffer(SDL_monitor_desc &monitor, video_depth depth, bool native_byte_order)
423 > static void set_mac_frame_buffer(SDL_monitor_desc &monitor, int depth, bool native_byte_order)
424   {
425   #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
426          int layout = FLAYOUT_DIRECT;
427 <        if (depth == VDEPTH_16BIT)
427 >        if (depth == VIDEO_DEPTH_16BIT)
428                  layout = (screen_depth == 15) ? FLAYOUT_HOST_555 : FLAYOUT_HOST_565;
429 <        else if (depth == VDEPTH_32BIT)
429 >        else if (depth == VIDEO_DEPTH_32BIT)
430                  layout = (screen_depth == 24) ? FLAYOUT_HOST_888 : FLAYOUT_DIRECT;
431          if (native_byte_order)
432                  MacFrameLayout = layout;
# Line 219 | Line 435 | static void set_mac_frame_buffer(SDL_mon
435          monitor.set_mac_frame_base(MacFrameBaseMac);
436  
437          // Set variables used by UAE memory banking
438 <        const video_mode &mode = monitor.get_current_mode();
438 >        const VIDEO_MODE &mode = monitor.get_current_mode();
439          MacFrameBaseHost = the_buffer;
440 <        MacFrameSize = mode.bytes_per_row * mode.y;
440 >        MacFrameSize = VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y;
441          InitFrameBufferMapping();
442   #else
443          monitor.set_mac_frame_base(Host2MacAddr(the_buffer));
# Line 270 | Line 486 | public:
486  
487   public:
488          SDL_monitor_desc &monitor; // Associated video monitor
489 <        const video_mode &mode;    // Video mode handled by the driver
489 >        const VIDEO_MODE &mode;    // Video mode handled by the driver
490  
491          bool init_ok;   // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
492          SDL_Surface *s; // The surface we draw into
# Line 358 | Line 574 | driver_base::~driver_base()
574   // Palette has changed
575   void driver_base::update_palette(void)
576   {
577 <        const video_mode &mode = monitor.get_current_mode();
577 >        const VIDEO_MODE &mode = monitor.get_current_mode();
578  
579 <        if (mode.depth <= VDEPTH_8BIT)
579 >        if (VIDEO_MODE_DEPTH <= VIDEO_DEPTH_8BIT)
580                  SDL_SetPalette(s, SDL_PHYSPAL, sdl_palette, 0, 256);
581   }
582  
# Line 383 | Line 599 | void driver_base::restore_mouse_accel(vo
599   driver_window::driver_window(SDL_monitor_desc &m)
600          : driver_base(m), mouse_grabbed(false)
601   {
602 <        int width = mode.x, height = mode.y;
602 >        int width = VIDEO_MODE_X, height = VIDEO_MODE_Y;
603          int aligned_width = (width + 15) & ~15;
604          int aligned_height = (height + 15) & ~15;
605  
# Line 391 | Line 607 | driver_window::driver_window(SDL_monitor
607          ADBSetRelMouseMode(mouse_grabbed);
608  
609          // Create surface
610 <        int depth = (mode.depth <= VDEPTH_8BIT ? 8 : screen_depth);
610 >        int depth = (VIDEO_MODE_DEPTH <= VIDEO_DEPTH_8BIT ? 8 : screen_depth);
611          if ((s = SDL_SetVideoMode(width, height, depth, SDL_HWSURFACE)) == NULL)
612                  return;
613  
# Line 424 | Line 640 | driver_window::driver_window(SDL_monitor
640          visualFormat.Rmask = f->Rmask;
641          visualFormat.Gmask = f->Gmask;
642          visualFormat.Bmask = f->Bmask;
643 <        Screen_blitter_init(visualFormat, true, sdl_depth_of_video_depth(mode.depth));
643 >        Screen_blitter_init(visualFormat, true, sdl_depth_of_video_depth(VIDEO_MODE_DEPTH));
644  
645          // Load gray ramp to 8->16/32 expand map
646          if (!IsDirectMode(mode))
# Line 432 | Line 648 | driver_window::driver_window(SDL_monitor
648                          ExpandMap[i] = SDL_MapRGB(f, i, i, i);
649  
650          // Set frame buffer base
651 <        set_mac_frame_buffer(monitor, mode.depth, true);
651 >        set_mac_frame_buffer(monitor, VIDEO_MODE_DEPTH, true);
652  
653          // Everything went well
654          init_ok = true;
# Line 574 | Line 790 | static void keycode_init(void)
790   bool SDL_monitor_desc::video_open(void)
791   {
792          D(bug("video_open()\n"));
793 <        const video_mode &mode = get_current_mode();
793 >        const VIDEO_MODE &mode = get_current_mode();
794 > #if DEBUG
795 >        D(bug("Current video mode:\n"));
796 >        D(bug(" %dx%d (ID %02x), %d bpp\n", VIDEO_MODE_X, VIDEO_MODE_Y, VIDEO_MODE_RESOLUTION, 1 << (VIDEO_MODE_DEPTH & 0x0f)));
797 > #endif
798  
799          // Create display driver object of requested type
800          switch (display_type) {
# Line 616 | Line 836 | bool SDL_monitor_desc::video_open(void)
836          return true;
837   }
838  
839 + #ifdef SHEEPSHAVER
840 + bool VideoInit(void)
841 + {
842 +        const bool classic = false;
843 + #else
844   bool VideoInit(bool classic)
845   {
846 + #endif
847          classic_mode = classic;
848  
849   #ifdef ENABLE_VOSF
# Line 641 | Line 867 | bool VideoInit(bool classic)
867          mouse_wheel_lines = PrefsFindInt32("mousewheellines");
868  
869          // Get screen mode from preferences
870 <        const char *mode_str;
870 >        const char *mode_str = NULL;
871 > #ifndef SHEEPSHAVER
872          if (classic_mode)
873                  mode_str = "win/512/342";
874          else
875                  mode_str = PrefsFindString("screen");
876 + #endif
877  
878          // Determine display type and default dimensions
879 <        int default_width = 512, default_height = 384;
879 >        int default_width, default_height;
880 >        if (classic) {
881 >                default_width = 512;
882 >                default_height = 384;
883 >        }
884 >        else {
885 >                default_width = 640;
886 >                default_height = 480;
887 >        }
888          display_type = DISPLAY_WINDOW;
889          if (mode_str) {
890                  if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2)
891                          display_type = DISPLAY_WINDOW;
892          }
893          int max_width = 640, max_height = 480;
894 <        if (display_type == DISPLAY_SCREEN) {
895 <                SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
896 <                if (modes && modes != (SDL_Rect **)-1) {
897 <                        max_width = modes[0]->w;
898 <                        max_height = modes[0]->h;
899 <                }
894 >        SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
895 >        if (modes && modes != (SDL_Rect **)-1) {
896 >                max_width = modes[0]->w;
897 >                max_height = modes[0]->h;
898 >                if (default_width > max_width)
899 >                        default_width = max_width;
900 >                if (default_height > max_height)
901 >                        default_height = max_height;
902          }
903          if (default_width <= 0)
904                  default_width = max_width;
# Line 669 | Line 907 | bool VideoInit(bool classic)
907  
908          // Mac screen depth follows X depth
909          screen_depth = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
910 <        video_depth default_depth;
910 >        int default_depth;
911          switch (screen_depth) {
912          case 8:
913 <                default_depth = VDEPTH_8BIT;
913 >                default_depth = VIDEO_DEPTH_8BIT;
914                  break;
915          case 15: case 16:
916 <                default_depth = VDEPTH_16BIT;
916 >                default_depth = VIDEO_DEPTH_16BIT;
917                  break;
918          case 24: case 32:
919 <                default_depth = VDEPTH_32BIT;
919 >                default_depth = VIDEO_DEPTH_32BIT;
920                  break;
921          default:
922 <                default_depth =  VDEPTH_1BIT;
922 >                default_depth =  VIDEO_DEPTH_1BIT;
923                  break;
924          }
925  
926          // Construct list of supported modes
927          if (display_type == DISPLAY_WINDOW) {
928                  if (classic)
929 <                        add_mode(512, 342, 0x80, 64, VDEPTH_1BIT);
929 >                        add_mode(display_type, 512, 342, 0x80, 64, VIDEO_DEPTH_1BIT);
930                  else {
931 <                        for (int d = VDEPTH_1BIT; d <= default_depth; d++) {
932 <                                int bpp = (d <= VDEPTH_8BIT ? 8 : sdl_depth_of_video_depth(d));
931 >                        for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++) {
932 >                                int bpp = (d <= VIDEO_DEPTH_8BIT ? 8 : sdl_depth_of_video_depth(d));
933                                  if (SDL_VideoModeOK(max_width, max_height, bpp, SDL_HWSURFACE))
934                                          add_window_modes(video_depth(d));
935                          }
936                  }
937          } else
938 <                add_mode(default_width, default_height, 0x80, TrivialBytesPerRow(default_width, default_depth), default_depth);
938 >                add_mode(display_type, default_width, default_height, 0x80, TrivialBytesPerRow(default_width, (video_depth)default_depth), default_depth);
939          if (VideoModes.empty()) {
940                  ErrorAlert(STR_NO_XVISUAL_ERR);
941                  return false;
# Line 705 | Line 943 | bool VideoInit(bool classic)
943  
944          // Find requested default mode with specified dimensions
945          uint32 default_id;
946 <        std::vector<video_mode>::const_iterator i, end = VideoModes.end();
946 >        std::vector<VIDEO_MODE>::const_iterator i, end = VideoModes.end();
947          for (i = VideoModes.begin(); i != end; ++i) {
948 <                if (i->x == default_width && i->y == default_height && i->depth == default_depth) {
949 <                        default_id = i->resolution_id;
948 >                const VIDEO_MODE & mode = (*i);
949 >                if (VIDEO_MODE_X == default_width && VIDEO_MODE_Y == default_height && VIDEO_MODE_DEPTH == default_depth) {
950 >                        default_id = VIDEO_MODE_RESOLUTION;
951 > #ifdef SHEEPSHAVER
952 >                        std::vector<VIDEO_MODE>::const_iterator begin = VideoModes.begin();
953 >                        cur_mode = distance(begin, i);
954 > #endif
955                          break;
956                  }
957          }
958          if (i == end) { // not found, use first available mode
959 <                default_depth = VideoModes[0].depth;
960 <                default_id = VideoModes[0].resolution_id;
959 >                const VIDEO_MODE & mode = VideoModes[0];
960 >                default_depth = VIDEO_MODE_DEPTH;
961 >                default_id = VIDEO_MODE_RESOLUTION;
962 > #ifdef SHEEPSHAVER
963 >                cur_mode = 0;
964 > #endif
965          }
966  
967 + #ifdef SHEEPSHAVER
968 +        for (int i = 0; i < VideoModes.size(); i++)
969 +                VModes[i] = VideoModes[i];
970 +        VideoInfo *p = &VModes[VideoModes.size()];
971 +        p->viType = DIS_INVALID;        // End marker
972 +        p->viRowBytes = 0;
973 +        p->viXsize = p->viYsize = 0;
974 +        p->viAppleMode = 0;
975 +        p->viAppleID = 0;
976 + #endif
977 +
978   #if DEBUG
979          D(bug("Available video modes:\n"));
980          for (i = VideoModes.begin(); i != end; ++i) {
981 <                int bits = 1 << i->depth;
981 >                const VIDEO_MODE & mode = (*i);
982 >                int bits = 1 << VIDEO_MODE_DEPTH;
983                  if (bits == 16)
984                          bits = 15;
985                  else if (bits == 32)
986                          bits = 24;
987 <                D(bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits));
987 >                D(bug(" %dx%d (ID %02x), %d colors\n", VIDEO_MODE_X, VIDEO_MODE_Y, VIDEO_MODE_RESOLUTION, 1 << bits));
988          }
989   #endif
990  
991          // Create SDL_monitor_desc for this (the only) display
992 <        SDL_monitor_desc *monitor = new SDL_monitor_desc(VideoModes, default_depth, default_id);
992 >        SDL_monitor_desc *monitor = new SDL_monitor_desc(VideoModes, (video_depth)default_depth, default_id);
993          VideoMonitors.push_back(monitor);
994  
995          // Open display
# Line 800 | Line 1059 | void VideoQuitFullScreen(void)
1059   *  Mac VBL interrupt
1060   */
1061  
1062 + /*
1063 + *  Execute video VBL routine
1064 + */
1065 +
1066 + #ifdef SHEEPSHAVER
1067 + void VideoVBL(void)
1068 + {
1069 +        // Emergency quit requested? Then quit
1070 +        if (emerg_quit)
1071 +                QuitEmulator();
1072 +
1073 +        // Temporarily give up frame buffer lock (this is the point where
1074 +        // we are suspended when the user presses Ctrl-Tab)
1075 +        UNLOCK_FRAME_BUFFER;
1076 +        LOCK_FRAME_BUFFER;
1077 +
1078 +        // Execute video VBL
1079 +        if (private_data != NULL && private_data->interruptsEnabled)
1080 +                VSLDoInterruptService(private_data->vslServiceID);
1081 + }
1082 + #else
1083   void VideoInterrupt(void)
1084   {
1085          // We must fill in the events queue in the same thread that did call SDL_SetVideoMode()
# Line 814 | Line 1094 | void VideoInterrupt(void)
1094          UNLOCK_FRAME_BUFFER;
1095          LOCK_FRAME_BUFFER;
1096   }
1097 + #endif
1098  
1099  
1100   /*
1101   *  Set palette
1102   */
1103  
1104 + #ifdef SHEEPSHAVER
1105 + void video_set_palette(void)
1106 + {
1107 +        monitor_desc * monitor = VideoMonitors[0];
1108 +        int n_colors = palette_size(monitor->get_current_mode().viAppleMode);
1109 +        uint8 pal[256 * 3];
1110 +        for (int c = 0; c < n_colors; c++) {
1111 +                pal[c*3 + 0] = mac_pal[c].red;
1112 +                pal[c*3 + 1] = mac_pal[c].green;
1113 +                pal[c*3 + 2] = mac_pal[c].blue;
1114 +        }
1115 +        monitor->set_palette(pal, n_colors);
1116 + }
1117 + #endif
1118 +
1119   void SDL_monitor_desc::set_palette(uint8 *pal, int num_in)
1120   {
1121 <        const video_mode &mode = get_current_mode();
1121 >        const VIDEO_MODE &mode = get_current_mode();
1122  
1123          // FIXME: how can we handle the gamma ramp?
1124 <        if (mode.depth > VDEPTH_8BIT)
1124 >        if (VIDEO_MODE_DEPTH > VIDEO_DEPTH_8BIT)
1125                  return;
1126  
1127          LOCK_PALETTE;
# Line 854 | Line 1150 | void SDL_monitor_desc::set_palette(uint8
1150                  LOCK_VOSF;
1151                  PFLAG_SET_ALL;
1152                  UNLOCK_VOSF;
1153 <                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1153 >                memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1154   #endif
1155          }
1156  
# Line 869 | Line 1165 | void SDL_monitor_desc::set_palette(uint8
1165   *  Switch video mode
1166   */
1167  
1168 + #ifdef SHEEPSHAVER
1169 + int16 video_mode_change(VidLocals *csSave, uint32 ParamPtr)
1170 + {
1171 +        /* return if no mode change */
1172 +        if ((csSave->saveData == ReadMacInt32(ParamPtr + csData)) &&
1173 +            (csSave->saveMode == ReadMacInt16(ParamPtr + csMode))) return noErr;
1174 +
1175 +        /* first find video mode in table */
1176 +        for (int i=0; VModes[i].viType != DIS_INVALID; i++) {
1177 +                if ((ReadMacInt16(ParamPtr + csMode) == VModes[i].viAppleMode) &&
1178 +                    (ReadMacInt32(ParamPtr + csData) == VModes[i].viAppleID)) {
1179 +                        csSave->saveMode = ReadMacInt16(ParamPtr + csMode);
1180 +                        csSave->saveData = ReadMacInt32(ParamPtr + csData);
1181 +                        csSave->savePage = ReadMacInt16(ParamPtr + csPage);
1182 +
1183 +                        // Disable interrupts
1184 +                        DisableInterrupt();
1185 +
1186 +                        cur_mode = i;
1187 +                        monitor_desc *monitor = VideoMonitors[0];
1188 +                        monitor->switch_to_current_mode();
1189 +
1190 +                        WriteMacInt32(ParamPtr + csBaseAddr, screen_base);
1191 +                        csSave->saveBaseAddr=screen_base;
1192 +                        csSave->saveData=VModes[cur_mode].viAppleID;/* First mode ... */
1193 +                        csSave->saveMode=VModes[cur_mode].viAppleMode;
1194 +
1195 +                        // Enable interrupts
1196 +                        EnableInterrupt();
1197 +                        return noErr;
1198 +                }
1199 +        }
1200 +        return paramErr;
1201 + }
1202 + #endif
1203 +
1204   void SDL_monitor_desc::switch_to_current_mode(void)
1205   {
1206          // Close and reopen display
# Line 883 | Line 1215 | void SDL_monitor_desc::switch_to_current
1215  
1216  
1217   /*
1218 + *  Can we set the MacOS cursor image into the window?
1219 + */
1220 +
1221 + #ifdef SHEEPSHAVER
1222 + bool video_can_change_cursor(void)
1223 + {
1224 + //      return hw_mac_cursor_accl && (display_type != DISPLAY_SCREEN);
1225 +        return false;
1226 + }
1227 + #endif
1228 +
1229 +
1230 + /*
1231 + *  Set cursor image for window
1232 + */
1233 +
1234 + #ifdef SHEEPSHAVER
1235 + void video_set_cursor(void)
1236 + {
1237 + //      cursor_changed = true;
1238 + }
1239 + #endif
1240 +
1241 +
1242 + /*
1243 + *  Install graphics acceleration
1244 + */
1245 +
1246 + #ifdef SHEEPSHAVER
1247 + // Rectangle inversion
1248 + template< int bpp >
1249 + static inline void do_invrect(uint8 *dest, uint32 length)
1250 + {
1251 + #define INVERT_1(PTR, OFS) ((uint8  *)(PTR))[OFS] = ~((uint8  *)(PTR))[OFS]
1252 + #define INVERT_2(PTR, OFS) ((uint16 *)(PTR))[OFS] = ~((uint16 *)(PTR))[OFS]
1253 + #define INVERT_4(PTR, OFS) ((uint32 *)(PTR))[OFS] = ~((uint32 *)(PTR))[OFS]
1254 + #define INVERT_8(PTR, OFS) ((uint64 *)(PTR))[OFS] = ~((uint64 *)(PTR))[OFS]
1255 +
1256 + #ifndef UNALIGNED_PROFITABLE
1257 +        // Align on 16-bit boundaries
1258 +        if (bpp < 16 && (((uintptr)dest) & 1)) {
1259 +                INVERT_1(dest, 0);
1260 +                dest += 1; length -= 1;
1261 +        }
1262 +
1263 +        // Align on 32-bit boundaries
1264 +        if (bpp < 32 && (((uintptr)dest) & 2)) {
1265 +                INVERT_2(dest, 0);
1266 +                dest += 2; length -= 2;
1267 +        }
1268 + #endif
1269 +
1270 +        // Invert 8-byte words
1271 +        if (length >= 8) {
1272 +                const int r = (length / 8) % 8;
1273 +                dest += r * 8;
1274 +
1275 +                int n = ((length / 8) + 7) / 8;
1276 +                switch (r) {
1277 +                case 0: do {
1278 +                                dest += 64;
1279 +                                INVERT_8(dest, -8);
1280 +                case 7: INVERT_8(dest, -7);
1281 +                case 6: INVERT_8(dest, -6);
1282 +                case 5: INVERT_8(dest, -5);
1283 +                case 4: INVERT_8(dest, -4);
1284 +                case 3: INVERT_8(dest, -3);
1285 +                case 2: INVERT_8(dest, -2);
1286 +                case 1: INVERT_8(dest, -1);
1287 +                                } while (--n > 0);
1288 +                }
1289 +        }
1290 +
1291 +        // 32-bit cell to invert?
1292 +        if (length & 4) {
1293 +                INVERT_4(dest, 0);
1294 +                if (bpp <= 16)
1295 +                        dest += 4;
1296 +        }
1297 +
1298 +        // 16-bit cell to invert?
1299 +        if (bpp <= 16 && (length & 2)) {
1300 +                INVERT_2(dest, 0);
1301 +                if (bpp <= 8)
1302 +                        dest += 2;
1303 +        }
1304 +
1305 +        // 8-bit cell to invert?
1306 +        if (bpp <= 8 && (length & 1))
1307 +                INVERT_1(dest, 0);
1308 +
1309 + #undef INVERT_1
1310 + #undef INVERT_2
1311 + #undef INVERT_4
1312 + #undef INVERT_8
1313 + }
1314 +
1315 + void NQD_invrect(uint32 p)
1316 + {
1317 +        D(bug("accl_invrect %08x\n", p));
1318 +
1319 +        // Get inversion parameters
1320 +        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1321 +        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1322 +        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1323 +        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1324 +        D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1325 +        D(bug(" width %d, height %d, bytes_per_row %d\n", width, height, (int32)ReadMacInt32(p + acclDestRowBytes)));
1326 +
1327 +        //!!?? pen_mode == 14
1328 +
1329 +        // And perform the inversion
1330 +        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1331 +        const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1332 +        uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1333 +        width *= bpp;
1334 +        switch (bpp) {
1335 +        case 1:
1336 +                for (int i = 0; i < height; i++) {
1337 +                        do_invrect<8>(dest, width);
1338 +                        dest += dest_row_bytes;
1339 +                }
1340 +                break;
1341 +        case 2:
1342 +                for (int i = 0; i < height; i++) {
1343 +                        do_invrect<16>(dest, width);
1344 +                        dest += dest_row_bytes;
1345 +                }
1346 +                break;
1347 +        case 4:
1348 +                for (int i = 0; i < height; i++) {
1349 +                        do_invrect<32>(dest, width);
1350 +                        dest += dest_row_bytes;
1351 +                }
1352 +                break;
1353 +        }
1354 + }
1355 +
1356 + // Rectangle filling
1357 + template< int bpp >
1358 + static inline void do_fillrect(uint8 *dest, uint32 color, uint32 length)
1359 + {
1360 + #define FILL_1(PTR, OFS, VAL) ((uint8  *)(PTR))[OFS] = (VAL)
1361 + #define FILL_2(PTR, OFS, VAL) ((uint16 *)(PTR))[OFS] = (VAL)
1362 + #define FILL_4(PTR, OFS, VAL) ((uint32 *)(PTR))[OFS] = (VAL)
1363 + #define FILL_8(PTR, OFS, VAL) ((uint64 *)(PTR))[OFS] = (VAL)
1364 +
1365 + #ifndef UNALIGNED_PROFITABLE
1366 +        // Align on 16-bit boundaries
1367 +        if (bpp < 16 && (((uintptr)dest) & 1)) {
1368 +                FILL_1(dest, 0, color);
1369 +                dest += 1; length -= 1;
1370 +        }
1371 +
1372 +        // Align on 32-bit boundaries
1373 +        if (bpp < 32 && (((uintptr)dest) & 2)) {
1374 +                FILL_2(dest, 0, color);
1375 +                dest += 2; length -= 2;
1376 +        }
1377 + #endif
1378 +
1379 +        // Fill 8-byte words
1380 +        if (length >= 8) {
1381 +                const uint64 c = (((uint64)color) << 32) | color;
1382 +                const int r = (length / 8) % 8;
1383 +                dest += r * 8;
1384 +
1385 +                int n = ((length / 8) + 7) / 8;
1386 +                switch (r) {
1387 +                case 0: do {
1388 +                                dest += 64;
1389 +                                FILL_8(dest, -8, c);
1390 +                case 7: FILL_8(dest, -7, c);
1391 +                case 6: FILL_8(dest, -6, c);
1392 +                case 5: FILL_8(dest, -5, c);
1393 +                case 4: FILL_8(dest, -4, c);
1394 +                case 3: FILL_8(dest, -3, c);
1395 +                case 2: FILL_8(dest, -2, c);
1396 +                case 1: FILL_8(dest, -1, c);
1397 +                                } while (--n > 0);
1398 +                }
1399 +        }
1400 +
1401 +        // 32-bit cell to fill?
1402 +        if (length & 4) {
1403 +                FILL_4(dest, 0, color);
1404 +                if (bpp <= 16)
1405 +                        dest += 4;
1406 +        }
1407 +
1408 +        // 16-bit cell to fill?
1409 +        if (bpp <= 16 && (length & 2)) {
1410 +                FILL_2(dest, 0, color);
1411 +                if (bpp <= 8)
1412 +                        dest += 2;
1413 +        }
1414 +
1415 +        // 8-bit cell to fill?
1416 +        if (bpp <= 8 && (length & 1))
1417 +                FILL_1(dest, 0, color);
1418 +
1419 + #undef FILL_1
1420 + #undef FILL_2
1421 + #undef FILL_4
1422 + #undef FILL_8
1423 + }
1424 +
1425 + void NQD_fillrect(uint32 p)
1426 + {
1427 +        D(bug("accl_fillrect %08x\n", p));
1428 +
1429 +        // Get filling parameters
1430 +        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1431 +        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1432 +        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1433 +        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1434 +        uint32 color = htonl(ReadMacInt32(p + acclPenMode) == 8 ? ReadMacInt32(p + acclForePen) : ReadMacInt32(p + acclBackPen));
1435 +        D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1436 +        D(bug(" width %d, height %d\n", width, height));
1437 +        D(bug(" bytes_per_row %d color %08x\n", (int32)ReadMacInt32(p + acclDestRowBytes), color));
1438 +
1439 +        // And perform the fill
1440 +        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1441 +        const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1442 +        uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1443 +        width *= bpp;
1444 +        switch (bpp) {
1445 +        case 1:
1446 +                for (int i = 0; i < height; i++) {
1447 +                        memset(dest, color, width);
1448 +                        dest += dest_row_bytes;
1449 +                }
1450 +                break;
1451 +        case 2:
1452 +                for (int i = 0; i < height; i++) {
1453 +                        do_fillrect<16>(dest, color, width);
1454 +                        dest += dest_row_bytes;
1455 +                }
1456 +                break;
1457 +        case 4:
1458 +                for (int i = 0; i < height; i++) {
1459 +                        do_fillrect<32>(dest, color, width);
1460 +                        dest += dest_row_bytes;
1461 +                }
1462 +                break;
1463 +        }
1464 + }
1465 +
1466 + bool NQD_fillrect_hook(uint32 p)
1467 + {
1468 +        D(bug("accl_fillrect_hook %08x\n", p));
1469 +
1470 +        // Check if we can accelerate this fillrect
1471 +        if (ReadMacInt32(p + 0x284) != 0 && ReadMacInt32(p + acclDestPixelSize) >= 8) {
1472 +                const int transfer_mode = ReadMacInt32(p + acclTransferMode);
1473 +                if (transfer_mode == 8) {
1474 +                        // Fill
1475 +                        WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_FILLRECT));
1476 +                        return true;
1477 +                }
1478 +                else if (transfer_mode == 10) {
1479 +                        // Invert
1480 +                        WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_INVRECT));
1481 +                        return true;
1482 +                }
1483 +        }
1484 +        return false;
1485 + }
1486 +
1487 + // Rectangle blitting
1488 + // TODO: optimize for VOSF and target pixmap == screen
1489 + void NQD_bitblt(uint32 p)
1490 + {
1491 +        D(bug("accl_bitblt %08x\n", p));
1492 +
1493 +        // Get blitting parameters
1494 +        int16 src_X  = (int16)ReadMacInt16(p + acclSrcRect + 2) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 2);
1495 +        int16 src_Y  = (int16)ReadMacInt16(p + acclSrcRect + 0) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 0);
1496 +        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1497 +        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1498 +        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1499 +        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1500 +        D(bug(" src addr %08x, dest addr %08x\n", ReadMacInt32(p + acclSrcBaseAddr), ReadMacInt32(p + acclDestBaseAddr)));
1501 +        D(bug(" src X %d, src Y %d, dest X %d, dest Y %d\n", src_X, src_Y, dest_X, dest_Y));
1502 +        D(bug(" width %d, height %d\n", width, height));
1503 +
1504 +        // And perform the blit
1505 +        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclSrcPixelSize));
1506 +        width *= bpp;
1507 +        if ((int32)ReadMacInt32(p + acclSrcRowBytes) > 0) {
1508 +                const int src_row_bytes = (int32)ReadMacInt32(p + acclSrcRowBytes);
1509 +                const int dst_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1510 +                uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + (src_Y * src_row_bytes) + (src_X * bpp));
1511 +                uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dst_row_bytes) + (dest_X * bpp));
1512 +                for (int i = 0; i < height; i++) {
1513 +                        memmove(dst, src, width);
1514 +                        src += src_row_bytes;
1515 +                        dst += dst_row_bytes;
1516 +                }
1517 +        }
1518 +        else {
1519 +                const int src_row_bytes = -(int32)ReadMacInt32(p + acclSrcRowBytes);
1520 +                const int dst_row_bytes = -(int32)ReadMacInt32(p + acclDestRowBytes);
1521 +                uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + ((src_Y + height - 1) * src_row_bytes) + (src_X * bpp));
1522 +                uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + ((dest_Y + height - 1) * dst_row_bytes) + (dest_X * bpp));
1523 +                for (int i = height - 1; i >= 0; i--) {
1524 +                        memmove(dst, src, width);
1525 +                        src -= src_row_bytes;
1526 +                        dst -= dst_row_bytes;
1527 +                }
1528 +        }
1529 + }
1530 +
1531 + /*
1532 +  BitBlt transfer modes:
1533 +  0 : srcCopy
1534 +  1 : srcOr
1535 +  2 : srcXor
1536 +  3 : srcBic
1537 +  4 : notSrcCopy
1538 +  5 : notSrcOr
1539 +  6 : notSrcXor
1540 +  7 : notSrcBic
1541 +  32 : blend
1542 +  33 : addPin
1543 +  34 : addOver
1544 +  35 : subPin
1545 +  36 : transparent
1546 +  37 : adMax
1547 +  38 : subOver
1548 +  39 : adMin
1549 +  50 : hilite
1550 + */
1551 +
1552 + bool NQD_bitblt_hook(uint32 p)
1553 + {
1554 +        D(bug("accl_draw_hook %08x\n", p));
1555 +
1556 +        // Check if we can accelerate this bitblt
1557 +        if (ReadMacInt32(p + 0x018) + ReadMacInt32(p + 0x128) == 0 &&
1558 +                ReadMacInt32(p + 0x130) == 0 &&
1559 +                ReadMacInt32(p + acclSrcPixelSize) >= 8 &&
1560 +                ReadMacInt32(p + acclSrcPixelSize) == ReadMacInt32(p + acclDestPixelSize) &&
1561 +                (ReadMacInt32(p + acclSrcRowBytes) ^ ReadMacInt32(p + acclDestRowBytes)) >= 0 && // same sign?
1562 +                ReadMacInt32(p + acclTransferMode) == 0 &&                                                                               // srcCopy?
1563 +                ReadMacInt32(p + 0x15c) > 0) {
1564 +
1565 +                // Yes, set function pointer
1566 +                WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_BITBLT));
1567 +                return true;
1568 +        }
1569 +        return false;
1570 + }
1571 +
1572 + // Wait for graphics operation to finish
1573 + bool NQD_sync_hook(uint32 arg)
1574 + {
1575 +        D(bug("accl_sync_hook %08x\n", arg));
1576 +        return true;
1577 + }
1578 +
1579 + void VideoInstallAccel(void)
1580 + {
1581 +        // Install acceleration hooks
1582 +        if (PrefsFindBool("gfxaccel")) {
1583 +                D(bug("Video: Installing acceleration hooks\n"));
1584 +                uint32 base;
1585 +
1586 +                SheepVar bitblt_hook_info(sizeof(accl_hook_info));
1587 +                base = bitblt_hook_info.addr();
1588 +                WriteMacInt32(base + 0, NativeTVECT(NATIVE_BITBLT_HOOK));
1589 +                WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
1590 +                WriteMacInt32(base + 8, ACCL_BITBLT);
1591 +                NQDMisc(6, bitblt_hook_info.ptr());
1592 +
1593 +                SheepVar fillrect_hook_info(sizeof(accl_hook_info));
1594 +                base = fillrect_hook_info.addr();
1595 +                WriteMacInt32(base + 0, NativeTVECT(NATIVE_FILLRECT_HOOK));
1596 +                WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
1597 +                WriteMacInt32(base + 8, ACCL_FILLRECT);
1598 +                NQDMisc(6, fillrect_hook_info.ptr());
1599 +        }
1600 + }
1601 + #endif
1602 +
1603 +
1604 + /*
1605   *  Translate key event to Mac keycode, returns -1 if no keycode was found
1606   *  and -2 if the key was recognized as a hotkey
1607   */
# Line 1115 | Line 1834 | static void handle_events(void)
1834                          // Hidden parts exposed, force complete refresh of window
1835                          case SDL_VIDEOEXPOSE:
1836                                  if (display_type == DISPLAY_WINDOW) {
1837 <                                        const video_mode &mode = VideoMonitors[0]->get_current_mode();
1837 >                                        const VIDEO_MODE &mode = VideoMonitors[0]->get_current_mode();
1838   #ifdef ENABLE_VOSF
1839                                          if (use_vosf) {                 // VOSF refresh
1840                                                  LOCK_VOSF;
1841                                                  PFLAG_SET_ALL;
1842                                                  UNLOCK_VOSF;
1843 <                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1843 >                                                memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1844                                          }
1845                                          else
1846   #endif
1847 <                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1847 >                                                memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1848                                  }
1849                                  break;
1850  
# Line 1149 | Line 1868 | static void update_display_static(driver
1868   {
1869          // Incremental update code
1870          int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1871 <        const video_mode &mode = drv->mode;
1872 <        int bytes_per_row = mode.bytes_per_row;
1871 >        const VIDEO_MODE &mode = drv->mode;
1872 >        int bytes_per_row = VIDEO_MODE_ROW_BYTES;
1873          uint8 *p, *p2;
1874  
1875          // Check for first line from top and first line from bottom that have changed
1876          y1 = 0;
1877 <        for (j=0; j<mode.y; j++) {
1877 >        for (j=0; j<VIDEO_MODE_Y; j++) {
1878                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1879                          y1 = j;
1880                          break;
1881                  }
1882          }
1883          y2 = y1 - 1;
1884 <        for (j=mode.y-1; j>=y1; j--) {
1884 >        for (j=VIDEO_MODE_Y-1; j>=y1; j--) {
1885                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1886                          y2 = j;
1887                          break;
# Line 1172 | Line 1891 | static void update_display_static(driver
1891  
1892          // Check for first column from left and first column from right that have changed
1893          if (high) {
1894 <                if (mode.depth < VDEPTH_8BIT) {
1894 >                if (VIDEO_MODE_DEPTH < VIDEO_DEPTH_8BIT) {
1895                          const int src_bytes_per_row = bytes_per_row;
1896                          const int dst_bytes_per_row = drv->s->pitch;
1897 <                        const int pixels_per_byte = mode.x / src_bytes_per_row;
1897 >                        const int pixels_per_byte = VIDEO_MODE_X / src_bytes_per_row;
1898  
1899 <                        x1 = mode.x / pixels_per_byte;
1899 >                        x1 = VIDEO_MODE_X / pixels_per_byte;
1900                          for (j = y1; j <= y2; j++) {
1901                                  p = &the_buffer[j * bytes_per_row];
1902                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 1195 | Line 1914 | static void update_display_static(driver
1914                                  p2 = &the_buffer_copy[j * bytes_per_row];
1915                                  p += bytes_per_row;
1916                                  p2 += bytes_per_row;
1917 <                                for (i = (mode.x / pixels_per_byte); i > x2; i--) {
1917 >                                for (i = (VIDEO_MODE_X / pixels_per_byte); i > x2; i--) {
1918                                          p--; p2--;
1919                                          if (*p != *p2) {
1920                                                  x2 = i;
# Line 1233 | Line 1952 | static void update_display_static(driver
1952                          }
1953  
1954                  } else {
1955 <                        const int bytes_per_pixel = mode.bytes_per_row / mode.x;
1955 >                        const int bytes_per_pixel = VIDEO_MODE_ROW_BYTES / VIDEO_MODE_X;
1956  
1957 <                        x1 = mode.x;
1957 >                        x1 = VIDEO_MODE_X;
1958                          for (j=y1; j<=y2; j++) {
1959                                  p = &the_buffer[j * bytes_per_row];
1960                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 1253 | Line 1972 | static void update_display_static(driver
1972                                  p2 = &the_buffer_copy[j * bytes_per_row];
1973                                  p += bytes_per_row;
1974                                  p2 += bytes_per_row;
1975 <                                for (i=mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1975 >                                for (i=VIDEO_MODE_X*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1976                                          p--;
1977                                          p2--;
1978                                          if (*p != *p2) {
# Line 1413 | Line 2132 | static void VideoRefreshInit(void)
2132          }
2133   }
2134  
2135 + const int VIDEO_REFRESH_HZ = 60;
2136 + const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
2137 +
2138   static int redraw_func(void *arg)
2139   {
2140          uint64 start = GetTicks_usec();
2141          int64 ticks = 0;
2142 +        uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
2143  
2144          while (!redraw_thread_cancel) {
2145  
2146                  // Wait
2147 <                Delay_usec(16667);
2147 >                next += VIDEO_REFRESH_DELAY;
2148 >                int64 delay = next - GetTicks_usec();
2149 >                if (delay > 0)
2150 >                        Delay_usec(delay);
2151 >                else if (delay < -VIDEO_REFRESH_DELAY)
2152 >                        next = GetTicks_usec();
2153 >                ticks++;
2154  
2155                  // Handle SDL events
2156                  handle_events();
2157  
2158                  // Refresh display
2159                  video_refresh();
1431                ticks++;
2160  
2161                  // Set new palette if it was changed
2162                  handle_palette_changes();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines