DEADSOFTWARE

21e8b4c363c99b63bb81b169c79fd59aa9ce8cdb
[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"
31 #include <assert.h>
34 // адрес экранного буфера
35 unsigned char *scra;
37 // виртуальный экран
38 unsigned char scrbuf[64000];
41 int SCRW = 800;
42 int SCRH = 600;
44 SDL_Surface* screen = NULL;
46 int cx1,cx2,cy1,cy2;
48 char fullscreen = OFF;
50 byte bright[256];
51 byte mixmap[256][256];
52 byte clrmap[256*12];
54 static byte flametab[16] = {
55 0xBC,0xBA,0xB8,0xB6,0xB4,0xB2,0xB0,0xD5,0xD6,0xD7,0xA1,0xA0,0xE3,0xE2,0xE1,0xE0
56 };
58 extern void *walp[256];
60 #define HQ 2
62 vgaimg *V_getvgaimg (int id) {
63 int loaded = M_was_locked(id);
64 vgaimg *v = M_lock(id);
65 if (v != NULL && !loaded) {
66 v->w = short2host(v->w);
67 v->h = short2host(v->h);
68 v->sx = short2host(v->sx);
69 v->sy = short2host(v->sy);
70 }
71 return v;
72 }
74 vgaimg *V_loadvgaimg (char *name) {
75 return V_getvgaimg(F_getresid(name));
76 }
78 short V_init(void)
79 {
80 Uint32 flags = SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_HWPALETTE;
81 if (fullscreen) flags = flags | SDL_FULLSCREEN;
82 screen = SDL_SetVideoMode(SCRW, SCRH, 8, flags);
83 if (!screen) ERR_failinit("Unable to set video mode: %s\n", SDL_GetError());
84 SCRW /= HQ;
85 SCRH /= HQ;
86 return 0;
87 }
89 // переключение в текстовый режим
90 void V_done(void)
91 {
92 SDL_Quit();
93 }
95 void draw_rect (int x, int y, int w, int h, int c)
96 {
97 SDL_Rect dstrect;
98 dstrect.x = x*HQ;
99 dstrect.y = y*HQ;
100 dstrect.w = w*HQ;
101 dstrect.h = h*HQ;
102 SDL_FillRect(screen, &dstrect, c);
105 // установить область вывода
106 void V_setrect(short x,short w,short y,short h)
108 SDL_Rect r;
109 r.x=x*HQ;
110 r.y=y*HQ;
111 r.w=w*HQ;
112 r.h=h*HQ;
113 SDL_SetClipRect(screen, &r);
114 SDL_GetClipRect(screen, &r);
115 cx1 = x;
116 cx2 = x+w-1;
117 cy1 = y;
118 cy2 = y+h-1;
119 if (cx1<0) cx1=0;
120 if (cx2>=SCRW) cx2=SCRW-1;
121 if (cy1<0) cy1=0;
122 if (cy2>=SCRH) cy2=SCRH-1;
125 void putpixel(int x, int y, Uint8 color)
127 if(x>=cx1 && x<=cx2 && y>=cy1 && y<=cy2) {
128 x*=HQ;
129 y*=HQ;
130 Uint8 *p = (Uint8 *)screen->pixels + y*screen->pitch + x;
131 *p = color;
132 *(p+1) = color;
133 p += screen->pitch;
134 *p = color;
135 *(p+1) = color;
139 byte getpixel(int x, int y)
141 if(x>=cx1 && x<=cx2 && y>=cy1 && y<=cy2) {
142 x*=HQ;
143 y*=HQ;
144 return *((Uint8 *)screen->pixels + y*screen->pitch + x);
146 return 0;
149 void mappixel(int x,int y,byte* cmap)
151 byte c = getpixel(x,y);
152 putpixel(x,y,cmap[c]);
155 int offx = 0;
156 int offy = 0;
158 void V_center(int f)
160 if (f) V_offset(SCRW/2-320/2, SCRH/2-200/2);
161 else V_offset(0, 0);
164 void V_offset(int ox, int oy)
166 offx=ox;
167 offy=oy;
170 void draw_spr(short x,short y,vgaimg *i, int d, int c)
172 if (i==NULL) return;
173 x += offx;
174 y += offy;
175 if (d & 1) x=x-i->w+i->sx; else x-=i->sx;
176 if (d & 2) y=y-i->h+i->sy; else y-=i->sy;
177 if(x+i->w>=cx1 && x<=cx2 && y+i->h>=cy1 && y<=cy2) {
178 int lx, ly;
179 byte *p = (byte*)i + sizeof(vgaimg);
180 for (ly=0; ly<i->h; ly++) {
181 for(lx=0; lx<i->w; lx++) {
182 int rx,ry;
183 rx = (d & 1) ? (i->w-lx-1) : (rx=lx);
184 ry = (d & 2) ? (i->h-ly-1) : (ry=ly);
185 if (*p) {
186 byte t = *p;
187 if (c) if (t>=0x70 && t<=0x7F) t=t-0x70+c;
188 putpixel(x+rx,y+ry,t);
190 p++;
196 void V_rotspr (int x, int y, vgaimg* i, int d)
198 x+=i->w*((d&1)?1:0);
199 y+=i->h*((d&2)?1:0);
200 draw_spr(x,y,i,d,0);
203 void V_pic(short x,short y,vgaimg *i)
205 draw_spr(x,y,i, 0, 0);
208 void V_manspr(int x,int y,void *p, unsigned char c)
210 draw_spr(x,y,p, 0, c);
213 void V_manspr2(int x,int y,void *p, unsigned char c)
215 draw_spr(x,y,p, 1, c);
218 // вывести точку цвета c в координатах (x,y)
219 void V_dot(short x,short y, unsigned char c)
221 putpixel(x,y,c);
224 void smoke_sprf(int x, int y, byte c)
226 byte t = getpixel(x,y);
227 c = c + bright[t];
228 c += 0x60;
229 c ^= 0xF;
230 putpixel(x,y,mixmap[c][t]);
233 void flame_sprf(int x, int y, byte c)
235 byte t = getpixel(x,y);
236 c = c + bright[t];
237 putpixel(x,y,flametab[c]);
240 void V_sprf(short x,short y,vgaimg *i,spr_f *f)
242 if (i==NULL) return;
243 x-=i->sx;
244 y-=i->sy;
245 int cx, cy;
246 byte *p = (byte*)i;
247 p+=sizeof(vgaimg);
248 for (cy=y; cy<y+i->h; cy++) {
249 for(cx=x; cx<x+i->w; cx++) {
250 if (*p) {
251 (*f)(cx, cy, *p);
253 p++;
258 void V_spr(short x,short y,vgaimg *i)
260 draw_spr(x,y,i,0, 0);
263 void V_spr2(short x,short y,vgaimg *i)
265 draw_spr(x,y,i,1,0);
268 void V_clr(short x,short w,short y,short h,unsigned char c)
270 draw_rect(x,y,w,h, c);
273 // установить палитру из массива p
274 void VP_setall(void *p)
276 VP_set(p, 0, 256);
279 // установить n цветов, начиная с f, из массива p
280 void VP_set(void *p,short f,short n)
282 byte *ptr = (byte*)p;
283 SDL_Color colors[256];
284 int i;
285 for(i=f;i<f+n;i++)
287 colors[i].r=ptr[0]*4;
288 colors[i].g=ptr[1]*4;
289 colors[i].b=ptr[2]*4;
290 ptr+=3;
292 SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, f, n);
295 // установить адрес экранного буфера
296 // NULL - реальный экран
297 void V_setscr(void *p)
299 if (screen) SDL_Flip(screen);
302 // скопировать прямоугольник на экран
303 void V_copytoscr(short x,short w,short y,short h)
305 x*=HQ; y*=HQ; w*=HQ; h*=HQ;
306 SDL_UpdateRect(screen, x, y, w, h);
309 void V_maptoscr(int x,int w,int y,int h,void *cmap)
311 int cx,cy;
312 for (cx=x; cx<x+w; cx++)
313 for (cy=y; cy<y+h; cy++)
314 mappixel(cx,cy,(byte*)cmap);
315 V_copytoscr(x,w,y,h);
318 void V_remap_rect(int x,int y,int w,int h,byte *cmap)
320 int cx,cy;
321 for (cx=x; cx<x+w; cx++)
322 for (cy=y; cy<y+h; cy++)
323 mappixel(cx,cy,cmap);
326 void Z_drawfld (byte *fld, int bg)
328 byte *p = fld;
329 int x, y;
330 for (y = 0; y < FLDH; y++) {
331 for (x = 0; x < FLDW; x++) {
332 int sx = x * CELW - w_x + WD / 2;
333 int sy = y * CELH - w_y + HT / 2 + 1 + w_o;
334 if (*p) {
335 vgaimg *pic = walp[*p];
336 // special pointer value setted for _WATER_* in view.c
337 if ((intptr_t)pic <= 3) {
338 if (!bg) {
339 byte *cmap = clrmap + ((intptr_t)pic+7)*256;
340 V_remap_rect(sx, sy, CELW, CELH, cmap);
342 } else {
343 V_pic(sx, sy, pic);
346 p++;
351 void V_toggle()
353 if (!SDL_WM_ToggleFullScreen(screen)) {
354 int ncolors = screen->format->palette->ncolors;
355 SDL_Color colors[256];
356 int i;
357 for (i=0; i<ncolors; i++) {
358 colors[i].r = screen->format->palette->colors[i].r;
359 colors[i].g = screen->format->palette->colors[i].g;
360 colors[i].b = screen->format->palette->colors[i].b;
363 Uint32 flags = screen->flags;
365 SDL_FreeSurface(screen);
367 screen = SDL_SetVideoMode(0, 0, 0, flags ^ SDL_FULLSCREEN);
368 if(screen == NULL) {
369 ERR_fatal("Unable to set video mode\n");
370 exit(1);
373 SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, ncolors);