DEADSOFTWARE

5db6b53cf0c7cf457785348c5c8bace494811406
[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"
30 #include <assert.h>
33 // адрес экранного буфера
34 unsigned char *scra;
36 // виртуальный экран
37 unsigned char scrbuf[64000];
40 int SCRW = 800;
41 int SCRH = 600;
43 SDL_Surface* screen = NULL;
45 int cx1,cx2,cy1,cy2;
47 char fullscreen = OFF;
49 #define HQ 2
51 vgaimg *V_getvgaimg (int id) {
52 int loaded = M_was_locked(id);
53 vgaimg *v = M_lock(id);
54 if (v != NULL && !loaded) {
55 v->w = short2host(v->w);
56 v->h = short2host(v->h);
57 v->sx = short2host(v->sx);
58 v->sy = short2host(v->sy);
59 }
60 return v;
61 }
63 vgaimg *V_loadvgaimg (char *name) {
64 return V_getvgaimg(F_getresid(name));
65 }
67 short V_init(void)
68 {
69 Uint32 flags = SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_HWPALETTE;
70 if (fullscreen) flags = flags | SDL_FULLSCREEN;
71 screen = SDL_SetVideoMode(SCRW, SCRH, 8, flags);
72 if (!screen) ERR_failinit("Unable to set video mode: %s\n", SDL_GetError());
73 SCRW /= HQ;
74 SCRH /= HQ;
75 return 0;
76 }
78 // переключение в текстовый режим
79 void V_done(void)
80 {
81 SDL_Quit();
82 }
84 void draw_rect (int x, int y, int w, int h, int c)
85 {
86 SDL_Rect dstrect;
87 dstrect.x = x*HQ;
88 dstrect.y = y*HQ;
89 dstrect.w = w*HQ;
90 dstrect.h = h*HQ;
91 SDL_FillRect(screen, &dstrect, c);
92 }
94 // установить область вывода
95 void V_setrect(short x,short w,short y,short h)
96 {
97 SDL_Rect r;
98 r.x=x*HQ;
99 r.y=y*HQ;
100 r.w=w*HQ;
101 r.h=h*HQ;
102 SDL_SetClipRect(screen, &r);
103 SDL_GetClipRect(screen, &r);
104 cx1 = x;
105 cx2 = x+w-1;
106 cy1 = y;
107 cy2 = y+h-1;
108 if (cx1<0) cx1=0;
109 if (cx2>=SCRW) cx2=SCRW-1;
110 if (cy1<0) cy1=0;
111 if (cy2>=SCRH) cy2=SCRH-1;
114 void putpixel(int x, int y, Uint8 color)
116 if(x>=cx1 && x<=cx2 && y>=cy1 && y<=cy2) {
117 x*=HQ;
118 y*=HQ;
119 Uint8 *p = (Uint8 *)screen->pixels + y*screen->pitch + x;
120 *p = color;
121 *(p+1) = color;
122 p += screen->pitch;
123 *p = color;
124 *(p+1) = color;
128 byte getpixel(int x, int y)
130 if(x>=cx1 && x<=cx2 && y>=cy1 && y<=cy2) {
131 x*=HQ;
132 y*=HQ;
133 return *((Uint8 *)screen->pixels + y*screen->pitch + x);
135 return 0;
138 void mappixel(int x,int y,byte* cmap)
140 byte c = getpixel(x,y);
141 putpixel(x,y,cmap[c]);
144 int offx = 0;
145 int offy = 0;
147 void V_center(int f)
149 if (f) V_offset(SCRW/2-320/2, SCRH/2-200/2);
150 else V_offset(0, 0);
153 void V_offset(int ox, int oy)
155 offx=ox;
156 offy=oy;
159 void draw_spr(short x,short y,vgaimg *i, int d, int c)
161 if (i==NULL) return;
162 x += offx;
163 y += offy;
164 if (d & 1) x=x-i->w+i->sx; else x-=i->sx;
165 if (d & 2) y=y-i->h+i->sy; else y-=i->sy;
166 if(x+i->w>=cx1 && x<=cx2 && y+i->h>=cy1 && y<=cy2) {
167 int lx, ly;
168 byte *p = (byte*)i + sizeof(vgaimg);
169 for (ly=0; ly<i->h; ly++) {
170 for(lx=0; lx<i->w; lx++) {
171 int rx,ry;
172 rx = (d & 1) ? (i->w-lx-1) : (rx=lx);
173 ry = (d & 2) ? (i->h-ly-1) : (ry=ly);
174 if (*p) {
175 byte t = *p;
176 if (c) if (t>=0x70 && t<=0x7F) t=t-0x70+c;
177 putpixel(x+rx,y+ry,t);
179 p++;
185 void V_rotspr (int x, int y, vgaimg* i, int d)
187 x+=i->w*((d&1)?1:0);
188 y+=i->h*((d&2)?1:0);
189 draw_spr(x,y,i,d,0);
192 void V_pic(short x,short y,vgaimg *i)
194 draw_spr(x,y,i, 0, 0);
197 void V_manspr(int x,int y,void *p, unsigned char c)
199 draw_spr(x,y,p, 0, c);
202 void V_manspr2(int x,int y,void *p, unsigned char c)
204 draw_spr(x,y,p, 1, c);
207 // вывести точку цвета c в координатах (x,y)
208 void V_dot(short x,short y, unsigned char c)
210 putpixel(x,y,c);
214 extern byte bright[256];
215 extern byte flametab[16];
216 extern byte mixmap[256][256];
218 void smoke_sprf(int x, int y, byte c)
220 byte t = getpixel(x,y);
221 c = c + bright[t];
222 c += 0x60;
223 c ^= 0xF;
224 putpixel(x,y,mixmap[c][t]);
227 void flame_sprf(int x, int y, byte c)
229 byte t = getpixel(x,y);
230 c = c + bright[t];
231 putpixel(x,y,flametab[c]);
234 void V_sprf(short x,short y,vgaimg *i,spr_f *f)
236 if (i==NULL) return;
237 x-=i->sx;
238 y-=i->sy;
239 int cx, cy;
240 byte *p = (byte*)i;
241 p+=sizeof(vgaimg);
242 for (cy=y; cy<y+i->h; cy++) {
243 for(cx=x; cx<x+i->w; cx++) {
244 if (*p) {
245 (*f)(cx, cy, *p);
247 p++;
252 void V_spr(short x,short y,vgaimg *i)
254 draw_spr(x,y,i,0, 0);
257 void V_spr2(short x,short y,vgaimg *i)
259 draw_spr(x,y,i,1,0);
262 void V_clr(short x,short w,short y,short h,unsigned char c)
264 draw_rect(x,y,w,h, c);
267 // установить палитру из массива p
268 void VP_setall(void *p)
270 VP_set(p, 0, 256);
273 // установить n цветов, начиная с f, из массива p
274 void VP_set(void *p,short f,short n)
276 byte *ptr = (byte*)p;
277 SDL_Color colors[256];
278 int i;
279 for(i=f;i<f+n;i++)
281 colors[i].r=ptr[0]*4;
282 colors[i].g=ptr[1]*4;
283 colors[i].b=ptr[2]*4;
284 ptr+=3;
286 SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, f, n);
289 // установить адрес экранного буфера
290 // NULL - реальный экран
291 void V_setscr(void *p)
293 if (screen) SDL_Flip(screen);
296 // скопировать прямоугольник на экран
297 void V_copytoscr(short x,short w,short y,short h)
299 x*=HQ; y*=HQ; w*=HQ; h*=HQ;
300 SDL_UpdateRect(screen, x, y, w, h);
303 void V_maptoscr(int x,int w,int y,int h,void *cmap)
305 int cx,cy;
306 for (cx=x; cx<x+w; cx++)
307 for (cy=y; cy<y+h; cy++)
308 mappixel(cx,cy,(byte*)cmap);
309 V_copytoscr(x,w,y,h);
312 void V_remap_rect(int x,int y,int w,int h,byte *cmap)
314 int cx,cy;
315 for (cx=x; cx<x+w; cx++)
316 for (cy=y; cy<y+h; cy++)
317 mappixel(cx,cy,cmap);
320 extern void *walp[256];
321 extern byte clrmap[256*12];
323 void Z_drawfld(byte *fld, int bg)
325 byte *p = fld;
326 int x,y;
327 for (y=0; y<FLDH; y++)
329 for (x=0; x<FLDW; x++)
331 int sx = x*CELW-w_x+WD/2;
332 int sy = y*CELH-w_y+HT/2+1+w_o;
335 if (*p) {
336 vgaimg *pic = walp[*p];
337 if ((int)pic <= 3) {
338 if (!bg) {
339 byte *cmap = clrmap + ((int)pic+7)*256;
340 V_remap_rect(sx, sy, CELW, CELH, cmap);
343 else {
344 V_pic(sx, sy, pic);
348 p++;
353 void V_toggle()
355 if (!SDL_WM_ToggleFullScreen(screen)) {
356 int ncolors = screen->format->palette->ncolors;
357 SDL_Color colors[256];
358 int i;
359 for (i=0; i<ncolors; i++) {
360 colors[i].r = screen->format->palette->colors[i].r;
361 colors[i].g = screen->format->palette->colors[i].g;
362 colors[i].b = screen->format->palette->colors[i].b;
365 Uint32 flags = screen->flags;
367 SDL_FreeSurface(screen);
369 screen = SDL_SetVideoMode(0, 0, 0, flags ^ SDL_FULLSCREEN);
370 if(screen == NULL) {
371 ERR_fatal("Unable to set video mode\n");
372 exit(1);
375 SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, ncolors);