DEADSOFTWARE

e06baf482dd3ed2a891a5e154a5ce38f434b54ac
[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"
30 // адрес экранного буфера
31 unsigned char *scra;
33 // виртуальный экран
34 unsigned char scrbuf[64000];
37 int SCRW = 800;
38 int SCRH = 600;
40 SDL_Surface* screen = NULL;
42 int cx1,cx2,cy1,cy2;
44 char fullscreen = OFF;
46 #define HQ 2
48 short V_init(void)
49 {
50 Uint32 flags = SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_HWPALETTE;
51 if (fullscreen) flags = flags | SDL_FULLSCREEN;
52 screen = SDL_SetVideoMode(SCRW, SCRH, 8, flags);
53 if (!screen) ERR_failinit("Unable to set video mode: %s\n", SDL_GetError());
54 SCRW /= HQ;
55 SCRH /= HQ;
56 return 0;
57 }
59 // переключение в текстовый режим
60 void V_done(void)
61 {
62 SDL_Quit();
63 }
65 void draw_rect (int x, int y, int w, int h, int c)
66 {
67 SDL_Rect dstrect;
68 dstrect.x = x*HQ;
69 dstrect.y = y*HQ;
70 dstrect.w = w*HQ;
71 dstrect.h = h*HQ;
72 SDL_FillRect(screen, &dstrect, c);
73 }
75 // установить область вывода
76 void V_setrect(short x,short w,short y,short h)
77 {
78 SDL_Rect r;
79 r.x=x*HQ;
80 r.y=y*HQ;
81 r.w=w*HQ;
82 r.h=h*HQ;
83 SDL_SetClipRect(screen, &r);
84 SDL_GetClipRect(screen, &r);
85 cx1 = x;
86 cx2 = x+w-1;
87 cy1 = y;
88 cy2 = y+h-1;
89 if (cx1<0) cx1=0;
90 if (cx2>=SCRW) cx2=SCRW-1;
91 if (cy1<0) cy1=0;
92 if (cy2>=SCRH) cy2=SCRH-1;
93 }
95 void putpixel(int x, int y, Uint8 color)
96 {
97 if(x>=cx1 && x<=cx2 && y>=cy1 && y<=cy2) {
98 x*=HQ;
99 y*=HQ;
100 Uint8 *p = (Uint8 *)screen->pixels + y*screen->pitch + x;
101 *p = color;
102 *(p+1) = color;
103 p += screen->pitch;
104 *p = color;
105 *(p+1) = color;
109 byte getpixel(int x, int y)
111 if(x>=cx1 && x<=cx2 && y>=cy1 && y<=cy2) {
112 x*=HQ;
113 y*=HQ;
114 return *((Uint8 *)screen->pixels + y*screen->pitch + x);
116 return 0;
119 void mappixel(int x,int y,byte* cmap)
121 byte c = getpixel(x,y);
122 putpixel(x,y,cmap[c]);
125 int offx = 0;
126 int offy = 0;
128 void V_center(int f)
130 if (f) V_offset(SCRW/2-320/2, SCRH/2-200/2);
131 else V_offset(0, 0);
134 void V_offset(int ox, int oy)
136 offx=ox;
137 offy=oy;
140 void draw_spr(short x,short y,vgaimg *i, int d, int c)
142 if (i==NULL) return;
143 x += offx;
144 y += offy;
145 if (d & 1) x=x-i->w+i->sx; else x-=i->sx;
146 if (d & 2) y=y-i->h+i->sy; else y-=i->sy;
147 if(x+i->w>=cx1 && x<=cx2 && y+i->h>=cy1 && y<=cy2) {
148 int lx, ly;
149 byte *p = (byte*)i + sizeof(vgaimg);
150 for (ly=0; ly<i->h; ly++) {
151 for(lx=0; lx<i->w; lx++) {
152 int rx,ry;
153 rx = (d & 1) ? (i->w-lx-1) : (rx=lx);
154 ry = (d & 2) ? (i->h-ly-1) : (ry=ly);
155 if (*p) {
156 byte t = *p;
157 if (c) if (t>=0x70 && t<=0x7F) t=t-0x70+c;
158 putpixel(x+rx,y+ry,t);
160 p++;
166 void V_rotspr (int x, int y, vgaimg* i, int d)
168 x+=i->w*((d&1)?1:0);
169 y+=i->h*((d&2)?1:0);
170 draw_spr(x,y,i,d,0);
173 void V_pic(short x,short y,vgaimg *i)
175 draw_spr(x,y,i, 0, 0);
178 void V_manspr(int x,int y,void *p, unsigned char c)
180 draw_spr(x,y,p, 0, c);
183 void V_manspr2(int x,int y,void *p, unsigned char c)
185 draw_spr(x,y,p, 1, c);
188 // вывести точку цвета c в координатах (x,y)
189 void V_dot(short x,short y, unsigned char c)
191 putpixel(x,y,c);
195 extern byte bright[256];
196 extern byte flametab[16];
197 extern byte mixmap[256][256];
199 void smoke_sprf(int x, int y, byte c)
201 byte t = getpixel(x,y);
202 c = c + bright[t];
203 c += 0x60;
204 c ^= 0xF;
205 putpixel(x,y,mixmap[c][t]);
208 void flame_sprf(int x, int y, byte c)
210 byte t = getpixel(x,y);
211 c = c + bright[t];
212 putpixel(x,y,flametab[c]);
215 void V_sprf(short x,short y,vgaimg *i,spr_f *f)
217 if (i==NULL) return;
218 x-=i->sx;
219 y-=i->sy;
220 int cx, cy;
221 byte *p = (byte*)i;
222 p+=sizeof(vgaimg);
223 for (cy=y; cy<y+i->h; cy++) {
224 for(cx=x; cx<x+i->w; cx++) {
225 if (*p) {
226 (*f)(cx, cy, *p);
228 p++;
233 void V_spr(short x,short y,vgaimg *i)
235 draw_spr(x,y,i,0, 0);
238 void V_spr2(short x,short y,vgaimg *i)
240 draw_spr(x,y,i,1,0);
243 void V_clr(short x,short w,short y,short h,unsigned char c)
245 draw_rect(x,y,w,h, c);
248 // установить палитру из массива p
249 void VP_setall(void *p)
251 VP_set(p, 0, 256);
254 // установить n цветов, начиная с f, из массива p
255 void VP_set(void *p,short f,short n)
257 byte *ptr = (byte*)p;
258 SDL_Color colors[256];
259 int i;
260 for(i=f;i<f+n;i++)
262 colors[i].r=ptr[0]*4;
263 colors[i].g=ptr[1]*4;
264 colors[i].b=ptr[2]*4;
265 ptr+=3;
267 SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, f, n);
270 // установить адрес экранного буфера
271 // NULL - реальный экран
272 void V_setscr(void *p)
274 if (screen) SDL_Flip(screen);
277 // скопировать прямоугольник на экран
278 void V_copytoscr(short x,short w,short y,short h)
280 x*=HQ; y*=HQ; w*=HQ; h*=HQ;
281 SDL_UpdateRect(screen, x, y, w, h);
284 void V_maptoscr(int x,int w,int y,int h,void *cmap)
286 int cx,cy;
287 for (cx=x; cx<x+w; cx++)
288 for (cy=y; cy<y+h; cy++)
289 mappixel(cx,cy,(byte*)cmap);
290 V_copytoscr(x,w,y,h);
293 void V_remap_rect(int x,int y,int w,int h,byte *cmap)
295 int cx,cy;
296 for (cx=x; cx<x+w; cx++)
297 for (cy=y; cy<y+h; cy++)
298 mappixel(cx,cy,cmap);
301 extern void *walp[256];
302 extern byte clrmap[256*12];
304 void Z_drawfld(byte *fld, int bg)
306 byte *p = fld;
307 int x,y;
308 for (y=0; y<FLDH; y++)
310 for (x=0; x<FLDW; x++)
312 int sx = x*CELW-w_x+WD/2;
313 int sy = y*CELH-w_y+HT/2+1+w_o;
316 if (*p) {
317 vgaimg *pic = walp[*p];
318 if ((int)pic <= 3) {
319 if (!bg) {
320 byte *cmap = clrmap + ((int)pic+7)*256;
321 V_remap_rect(sx, sy, CELW, CELH, cmap);
324 else {
325 V_pic(sx, sy, pic);
329 p++;
334 void V_toggle()
336 if (!SDL_WM_ToggleFullScreen(screen)) {
337 int ncolors = screen->format->palette->ncolors;
338 SDL_Color colors[256];
339 int i;
340 for (i=0; i<ncolors; i++) {
341 colors[i].r = screen->format->palette->colors[i].r;
342 colors[i].g = screen->format->palette->colors[i].g;
343 colors[i].b = screen->format->palette->colors[i].b;
346 Uint32 flags = screen->flags;
348 SDL_FreeSurface(screen);
350 screen = SDL_SetVideoMode(0, 0, 0, flags ^ SDL_FULLSCREEN);
351 if(screen == NULL) {
352 ERR_fatal("Unable to set video mode\n");
353 exit(1);
356 SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, ncolors);