source: abuse/tags/pd/macabuse/imlib/port/svga/video.c @ 49

Last change on this file since 49 was 49, checked in by Sam Hocevar, 11 years ago
  • Imported original public domain release, for future reference.
  • Property svn:keywords set to Id
File size: 4.5 KB
Line 
1#include "globals.hpp"
2#include "system.h"
3#include "video.hpp"
4#include "dos.h"
5#include "macs.hpp"
6#include "image.hpp"
7#include "vga.h"
8#include "palette.hpp"
9#include "jmalloc.hpp"
10
11
12unsigned char current_background;
13extern unsigned long xres, yres;
14
15
16extern palette *lastl;
17int vmode;
18image *screen;
19uchar *v_addr;
20
21int get_vmode()
22{ return vmode; }
23
24void set_mode(video_mode enum_mode, int argc, char **argv)
25{
26  int fail=0,i;
27  int mode=0;
28  switch (enum_mode)
29  {
30    case VMODE_320x200 :
31    { mode=G320x200x256; } break;
32    case VMODE_640x480 :
33    { mode=G640x480x256; } break;
34  }
35
36  for (i=1;i<argc;i++)
37  {
38    if (!strcmp(argv[i],"-vmode"))
39    { i++;
40      if (!strcmp(argv[i],"G320x200x256")) mode=G320x200x256;
41      else if (!strcmp(argv[i],"G640x480x256")) mode=G640x480x256;
42      else if (!strcmp(argv[i],"G800x600x256")) mode=G800x600x256;
43      else if (!strcmp(argv[i],"G1024x768x256")) mode=G1024x768x256;
44      else fail=1;
45    }
46  } 
47  if (fail)
48  { printf("Graphics modes supported :\n"
49            "  G320x200x256, G640x480x256, G800x600x256, G1024x768x256\n"
50            "  usage : %s [program options] [-vmode G....]\n",argv[0]);
51    exit(1);
52  }
53  vga_init();
54  vga_setmode(mode);
55  v_addr=vga_getgraphmem();
56  xres=vga_getxdim()-1;
57  yres=vga_getydim()-1;
58
59  vmode=mode;
60  screen=new image(xres+1,yres+1,NULL,2);
61  screen->clear();
62  update_dirty(screen);
63}
64
65void close_graphics()
66{
67  if (lastl)
68    delete lastl;
69  lastl=NULL;
70  vga_setmode(0);
71}
72
73inline void jmemcpy(long *dest, long *src, long size)
74{
75  register long *s=src;
76  register long *d=dest;
77
78  size>>=2;  // doing word at a time
79  while (size--)
80    *(d++)=*(s++); 
81}
82
83void put_part(image *im, int x, int y, int x1, int y1, int x2, int y2)
84{
85  unsigned long screen_off;
86  int ys,ye,         // ystart, yend
87        xs,xe,     
88        page,last_page=-1,yy;
89  long breaker;
90  unsigned char *line_addr;
91
92  if (y>(int)yres || x>(int)xres) return ;
93  CHECK(y1>=0 && y2>=y1 && x1>=0 && x2>=x1);
94
95
96  if (y<0)
97  { y1+=-y; y=0; }
98  ys=y1;
99  if (y+(y2-y1)>=(int)yres)
100    ye=(int)yres-y+y1-1;
101  else ye=y2;
102
103  if (x<0)
104  { x1+=-x; x=0; }
105  xs=x1;
106  if (x+(x2-x1)>=(int)xres)
107    xe=(int)xres-x+x1-1;
108  else xe=x2;
109  if (xs>xe || ys>ye) return ;
110
111  // find the memory offset for the scan line of interest
112  screen_off=((long)y*(long)(xres+1));
113
114
115  for (yy=ys;yy<=ye;yy++,screen_off+=(xres+1))
116  {
117    page=screen_off>>16;     // which page of 64k are we on?
118    if (page!=last_page)     
119    { last_page=page;
120      vga_setpage(page);     // switch to new bank
121    }
122
123    line_addr=im->scan_line(yy)+xs;  // get address of image scan line
124
125    // breaker is the number of bytes before the page split
126    breaker=(long)0xffff-(long)(screen_off&0xffff)+1;
127
128
129    // see if the slam gets split by the page break
130    if (breaker>x+xe-xs)
131    {
132      void *dest=v_addr+(screen_off&0xffff)+x;
133      int size=xe-xs+1;
134      memcpy(dest,line_addr,size);
135    }
136    else if (breaker<=x)
137    { last_page++;
138      vga_setpage(last_page);
139      memcpy(v_addr+x-breaker,line_addr,xe-xs+1);
140    }
141    else
142    {
143      memcpy(v_addr+(screen_off&0xffff)+x,line_addr,breaker-x);
144      last_page++;
145      vga_setpage(last_page);
146      memcpy(v_addr,line_addr+breaker-x,xe-xs-(breaker-x)+1);
147    }
148    y++;   
149  }
150}
151
152void put_image(image *im, int x, int y)
153{ put_part(im,x,y,0,0,im->width()-1,im->height()-1); }
154
155
156void update_dirty(image *im, int xoff, int yoff)
157{
158
159  int count;
160  dirty_rect *dr,*q;
161  CHECK(im->special);  // make sure the image has the ablity to contain dirty areas
162  if (im->special->keep_dirt==0)
163    put_image(im,0,0);
164  else
165  {
166    count=im->special->dirties.number_nodes();
167    if (!count) return;  // if nothing to update, return
168    (linked_node *) dr=im->special->dirties.first();
169    while (count>0)
170    {
171      put_part(im,dr->dx1+xoff,dr->dy1+yoff,dr->dx1,dr->dy1,dr->dx2,dr->dy2);
172      q=dr;
173      (linked_node *)dr=dr->next();
174      im->special->dirties.unlink((linked_node *)q);
175      delete q;
176      count--;
177    }
178  }
179}
180
181
182void palette::load()
183{
184  if (lastl)
185    delete lastl;
186  lastl=copy();
187  for (int i=0;i<ncolors;i++)
188    vga_setpalette(i,red(i)>>2,green(i)>>2,blue(i)>>2);
189}
190
191void palette::load_nice()
192{ load(); }
193
194
195void image::make_page(short width, short height, unsigned char *page_buffer)
196{
197  if (page_buffer)
198    data=page_buffer;
199  else data=(unsigned char *)jmalloc(width*height,"image::data");
200}
201void image::delete_page()
202{
203  if (!special || !special->static_mem)
204    jfree(data);     
205}
206
207void switch_mode(video_mode new_mode)
208{
209  close_graphics();
210  char *argv[]= {"game"};
211  set_mode(new_mode,0,argv);
212}
Note: See TracBrowser for help on using the repository browser.