DEADSOFTWARE

fix palette
[flatwaifu.git] / src / vga.c
1 /*
2 Copyright (C) Prikol Software 1996-1997
3 Copyright (C) Aleksey Volynskov 1996-1997
4 Copyright (C) <ARembo@gmail.com> 2011
6 This file is part of the Doom2D:Rembo project.
8 Doom2D:Rembo is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation.
12 Doom2D:Rembo 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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/> or
19 write to the Free Software Foundation, Inc.,
20 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
23 #include "glob.h"
24 #include "vga.h"
25 #include <SDL.h>
26 #include "error.h"
27 #include "view.h"
28 #include "memory.h"
29 #include "misc.h"
30 #include "files.h"
32 #include <assert.h>
35 // адрес экранного буфера
36 unsigned char *scra;
38 // виртуальный экран
39 unsigned char scrbuf[64000];
42 int SCRW = 800;
43 int SCRH = 600;
45 SDL_Surface* screen = NULL;
47 int cx1,cx2,cy1,cy2;
49 char fullscreen = OFF;
51 byte bright[256];
52 byte mixmap[256][256];
53 byte clrmap[256*12];
55 static byte flametab[16] = {
56 0xBC,0xBA,0xB8,0xB6,0xB4,0xB2,0xB0,0xD5,0xD6,0xD7,0xA1,0xA0,0xE3,0xE2,0xE1,0xE0
57 };
59 extern void *walp[256];
61 #define HQ 2
63 vgaimg *V_getvgaimg (int id) {
64 int loaded = M_was_locked(id);
65 vgaimg *v = M_lock(id);
66 if (v != NULL && !loaded) {
67 v->w = short2host(v->w);
68 v->h = short2host(v->h);
69 v->sx = short2host(v->sx);
70 v->sy = short2host(v->sy);
71 }
72 return v;
73 }
75 vgaimg *V_loadvgaimg (char *name) {
76 return V_getvgaimg(F_getresid(name));
77 }
79 short V_init(void)
80 {
81 Uint32 flags = SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_HWPALETTE;
82 if (fullscreen) flags = flags | SDL_FULLSCREEN;
83 screen = SDL_SetVideoMode(SCRW, SCRH, 8, flags);
84 if (!screen) ERR_failinit("Unable to set video mode: %s\n", SDL_GetError());
85 SCRW /= HQ;
86 SCRH /= HQ;
87 return 0;
88 }
90 // переключение в текстовый режим
91 void V_done(void)
92 {
93 SDL_Quit();
94 }
96 void draw_rect (int x, int y, int w, int h, int c)
97 {
98 SDL_Rect dstrect;
99 dstrect.x = x*HQ;
100 dstrect.y = y*HQ;
101 dstrect.w = w*HQ;
102 dstrect.h = h*HQ;
103 SDL_FillRect(screen, &dstrect, c);
106 // установить область вывода
107 void V_setrect(short x,short w,short y,short h)
109 SDL_Rect r;
110 r.x=x*HQ;
111 r.y=y*HQ;
112 r.w=w*HQ;
113 r.h=h*HQ;
114 SDL_SetClipRect(screen, &r);
115 SDL_GetClipRect(screen, &r);
116 cx1 = x;
117 cx2 = x+w-1;
118 cy1 = y;
119 cy2 = y+h-1;
120 if (cx1<0) cx1=0;
121 if (cx2>=SCRW) cx2=SCRW-1;
122 if (cy1<0) cy1=0;
123 if (cy2>=SCRH) cy2=SCRH-1;
126 void putpixel(int x, int y, Uint8 color)
128 if(x>=cx1 && x<=cx2 && y>=cy1 && y<=cy2) {
129 x*=HQ;
130 y*=HQ;
131 Uint8 *p = (Uint8 *)screen->pixels + y*screen->pitch + x;
132 *p = color;
133 *(p+1) = color;
134 p += screen->pitch;
135 *p = color;
136 *(p+1) = color;
140 byte getpixel(int x, int y)
142 if(x>=cx1 && x<=cx2 && y>=cy1 && y<=cy2) {
143 x*=HQ;
144 y*=HQ;
145 return *((Uint8 *)screen->pixels + y*screen->pitch + x);
147 return 0;
150 void mappixel(int x,int y,byte* cmap)
152 byte c = getpixel(x,y);
153 putpixel(x,y,cmap[c]);
156 int offx = 0;
157 int offy = 0;
159 void V_center(int f)
161 if (f) V_offset(SCRW/2-320/2, SCRH/2-200/2);
162 else V_offset(0, 0);
165 void V_offset(int ox, int oy)
167 offx=ox;
168 offy=oy;
171 void draw_spr(short x,short y,vgaimg *i, int d, int c)
173 if (i==NULL) return;
174 x += offx;
175 y += offy;
176 if (d & 1) x=x-i->w+i->sx; else x-=i->sx;
177 if (d & 2) y=y-i->h+i->sy; else y-=i->sy;
178 if(x+i->w>=cx1 && x<=cx2 && y+i->h>=cy1 && y<=cy2) {
179 int lx, ly;
180 byte *p = (byte*)i + sizeof(vgaimg);
181 for (ly=0; ly<i->h; ly++) {
182 for(lx=0; lx<i->w; lx++) {
183 int rx,ry;
184 rx = (d & 1) ? (i->w-lx-1) : (rx=lx);
185 ry = (d & 2) ? (i->h-ly-1) : (ry=ly);
186 if (*p) {
187 byte t = *p;
188 if (c) if (t>=0x70 && t<=0x7F) t=t-0x70+c;
189 putpixel(x+rx,y+ry,t);
191 p++;
197 void V_rotspr (int x, int y, vgaimg* i, int d)
199 x+=i->w*((d&1)?1:0);
200 y+=i->h*((d&2)?1:0);
201 draw_spr(x,y,i,d,0);
204 void V_pic(short x,short y,vgaimg *i)
206 draw_spr(x,y,i, 0, 0);
209 void V_manspr(int x,int y,void *p, unsigned char c)
211 draw_spr(x,y,p, 0, c);
214 void V_manspr2(int x,int y,void *p, unsigned char c)
216 draw_spr(x,y,p, 1, c);
219 // вывести точку цвета c в координатах (x,y)
220 void V_dot(short x,short y, unsigned char c)
222 putpixel(x,y,c);
225 void smoke_sprf(int x, int y, byte c)
227 byte t = getpixel(x,y);
228 c = c + bright[t];
229 c += 0x60;
230 c ^= 0xF;
231 putpixel(x,y,mixmap[c][t]);
234 void flame_sprf(int x, int y, byte c)
236 byte t = getpixel(x,y);
237 c = c + bright[t];
238 putpixel(x,y,flametab[c]);
241 void V_sprf(short x,short y,vgaimg *i,spr_f *f)
243 if (i==NULL) return;
244 x-=i->sx;
245 y-=i->sy;
246 int cx, cy;
247 byte *p = (byte*)i;
248 p+=sizeof(vgaimg);
249 for (cy=y; cy<y+i->h; cy++) {
250 for(cx=x; cx<x+i->w; cx++) {
251 if (*p) {
252 (*f)(cx, cy, *p);
254 p++;
259 void V_spr(short x,short y,vgaimg *i)
261 draw_spr(x,y,i,0, 0);
264 void V_spr2(short x,short y,vgaimg *i)
266 draw_spr(x,y,i,1,0);
269 void V_clr(short x,short w,short y,short h,unsigned char c)
271 draw_rect(x,y,w,h, c);
274 // установить палитру из массива p
275 void VP_setall(void *p)
277 VP_set(p, 0, 256);
280 // установить n цветов, начиная с f, из массива p
281 void VP_set(void *p,short f,short n)
283 byte *ptr = (byte*)p;
284 SDL_Color colors[256];
285 int i;
286 for(i=f;i<f+n;i++)
288 colors[i].r=ptr[0]*4;
289 colors[i].g=ptr[1]*4;
290 colors[i].b=ptr[2]*4;
291 ptr+=3;
293 SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, f, n);
296 // установить адрес экранного буфера
297 // NULL - реальный экран
298 void V_setscr(void *p)
300 if (screen) SDL_Flip(screen);
303 // скопировать прямоугольник на экран
304 void V_copytoscr(short x,short w,short y,short h)
306 x*=HQ; y*=HQ; w*=HQ; h*=HQ;
307 SDL_UpdateRect(screen, x, y, w, h);
310 void V_maptoscr(int x,int w,int y,int h,void *cmap)
312 int cx,cy;
313 for (cx=x; cx<x+w; cx++)
314 for (cy=y; cy<y+h; cy++)
315 mappixel(cx,cy,(byte*)cmap);
316 V_copytoscr(x,w,y,h);
319 void V_remap_rect(int x,int y,int w,int h,byte *cmap)
321 int cx,cy;
322 for (cx=x; cx<x+w; cx++)
323 for (cy=y; cy<y+h; cy++)
324 mappixel(cx,cy,cmap);
327 void V_toggle()
329 if (!SDL_WM_ToggleFullScreen(screen)) {
330 int ncolors = screen->format->palette->ncolors;
331 SDL_Color colors[256];
332 int i;
333 for (i=0; i<ncolors; i++) {
334 colors[i].r = screen->format->palette->colors[i].r;
335 colors[i].g = screen->format->palette->colors[i].g;
336 colors[i].b = screen->format->palette->colors[i].b;
339 Uint32 flags = screen->flags;
341 SDL_FreeSurface(screen);
343 screen = SDL_SetVideoMode(0, 0, 0, flags ^ SDL_FULLSCREEN);
344 if(screen == NULL) {
345 ERR_fatal("Unable to set video mode\n");
346 exit(1);
349 SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, ncolors);