DEADSOFTWARE

fix some warnings
[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 #define HQ 2
52 vgaimg *V_getvgaimg (int id) {
53 int loaded = M_was_locked(id);
54 vgaimg *v = M_lock(id);
55 if (v != NULL && !loaded) {
56 v->w = short2host(v->w);
57 v->h = short2host(v->h);
58 v->sx = short2host(v->sx);
59 v->sy = short2host(v->sy);
60 }
61 return v;
62 }
64 vgaimg *V_loadvgaimg (char *name) {
65 return V_getvgaimg(F_getresid(name));
66 }
68 short V_init(void)
69 {
70 Uint32 flags = SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_HWPALETTE;
71 if (fullscreen) flags = flags | SDL_FULLSCREEN;
72 screen = SDL_SetVideoMode(SCRW, SCRH, 8, flags);
73 if (!screen) ERR_failinit("Unable to set video mode: %s\n", SDL_GetError());
74 SCRW /= HQ;
75 SCRH /= HQ;
76 return 0;
77 }
79 // переключение в текстовый режим
80 void V_done(void)
81 {
82 SDL_Quit();
83 }
85 void draw_rect (int x, int y, int w, int h, int c)
86 {
87 SDL_Rect dstrect;
88 dstrect.x = x*HQ;
89 dstrect.y = y*HQ;
90 dstrect.w = w*HQ;
91 dstrect.h = h*HQ;
92 SDL_FillRect(screen, &dstrect, c);
93 }
95 // установить область вывода
96 void V_setrect(short x,short w,short y,short h)
97 {
98 SDL_Rect r;
99 r.x=x*HQ;
100 r.y=y*HQ;
101 r.w=w*HQ;
102 r.h=h*HQ;
103 SDL_SetClipRect(screen, &r);
104 SDL_GetClipRect(screen, &r);
105 cx1 = x;
106 cx2 = x+w-1;
107 cy1 = y;
108 cy2 = y+h-1;
109 if (cx1<0) cx1=0;
110 if (cx2>=SCRW) cx2=SCRW-1;
111 if (cy1<0) cy1=0;
112 if (cy2>=SCRH) cy2=SCRH-1;
115 void putpixel(int x, int y, Uint8 color)
117 if(x>=cx1 && x<=cx2 && y>=cy1 && y<=cy2) {
118 x*=HQ;
119 y*=HQ;
120 Uint8 *p = (Uint8 *)screen->pixels + y*screen->pitch + x;
121 *p = color;
122 *(p+1) = color;
123 p += screen->pitch;
124 *p = color;
125 *(p+1) = color;
129 byte getpixel(int x, int y)
131 if(x>=cx1 && x<=cx2 && y>=cy1 && y<=cy2) {
132 x*=HQ;
133 y*=HQ;
134 return *((Uint8 *)screen->pixels + y*screen->pitch + x);
136 return 0;
139 void mappixel(int x,int y,byte* cmap)
141 byte c = getpixel(x,y);
142 putpixel(x,y,cmap[c]);
145 int offx = 0;
146 int offy = 0;
148 void V_center(int f)
150 if (f) V_offset(SCRW/2-320/2, SCRH/2-200/2);
151 else V_offset(0, 0);
154 void V_offset(int ox, int oy)
156 offx=ox;
157 offy=oy;
160 void draw_spr(short x,short y,vgaimg *i, int d, int c)
162 if (i==NULL) return;
163 x += offx;
164 y += offy;
165 if (d & 1) x=x-i->w+i->sx; else x-=i->sx;
166 if (d & 2) y=y-i->h+i->sy; else y-=i->sy;
167 if(x+i->w>=cx1 && x<=cx2 && y+i->h>=cy1 && y<=cy2) {
168 int lx, ly;
169 byte *p = (byte*)i + sizeof(vgaimg);
170 for (ly=0; ly<i->h; ly++) {
171 for(lx=0; lx<i->w; lx++) {
172 int rx,ry;
173 rx = (d & 1) ? (i->w-lx-1) : (rx=lx);
174 ry = (d & 2) ? (i->h-ly-1) : (ry=ly);
175 if (*p) {
176 byte t = *p;
177 if (c) if (t>=0x70 && t<=0x7F) t=t-0x70+c;
178 putpixel(x+rx,y+ry,t);
180 p++;
186 void V_rotspr (int x, int y, vgaimg* i, int d)
188 x+=i->w*((d&1)?1:0);
189 y+=i->h*((d&2)?1:0);
190 draw_spr(x,y,i,d,0);
193 void V_pic(short x,short y,vgaimg *i)
195 draw_spr(x,y,i, 0, 0);
198 void V_manspr(int x,int y,void *p, unsigned char c)
200 draw_spr(x,y,p, 0, c);
203 void V_manspr2(int x,int y,void *p, unsigned char c)
205 draw_spr(x,y,p, 1, c);
208 // вывести точку цвета c в координатах (x,y)
209 void V_dot(short x,short y, unsigned char c)
211 putpixel(x,y,c);
215 extern byte bright[256];
216 extern byte flametab[16];
217 extern byte mixmap[256][256];
219 void smoke_sprf(int x, int y, byte c)
221 byte t = getpixel(x,y);
222 c = c + bright[t];
223 c += 0x60;
224 c ^= 0xF;
225 putpixel(x,y,mixmap[c][t]);
228 void flame_sprf(int x, int y, byte c)
230 byte t = getpixel(x,y);
231 c = c + bright[t];
232 putpixel(x,y,flametab[c]);
235 void V_sprf(short x,short y,vgaimg *i,spr_f *f)
237 if (i==NULL) return;
238 x-=i->sx;
239 y-=i->sy;
240 int cx, cy;
241 byte *p = (byte*)i;
242 p+=sizeof(vgaimg);
243 for (cy=y; cy<y+i->h; cy++) {
244 for(cx=x; cx<x+i->w; cx++) {
245 if (*p) {
246 (*f)(cx, cy, *p);
248 p++;
253 void V_spr(short x,short y,vgaimg *i)
255 draw_spr(x,y,i,0, 0);
258 void V_spr2(short x,short y,vgaimg *i)
260 draw_spr(x,y,i,1,0);
263 void V_clr(short x,short w,short y,short h,unsigned char c)
265 draw_rect(x,y,w,h, c);
268 // установить палитру из массива p
269 void VP_setall(void *p)
271 VP_set(p, 0, 256);
274 // установить n цветов, начиная с f, из массива p
275 void VP_set(void *p,short f,short n)
277 byte *ptr = (byte*)p;
278 SDL_Color colors[256];
279 int i;
280 for(i=f;i<f+n;i++)
282 colors[i].r=ptr[0]*4;
283 colors[i].g=ptr[1]*4;
284 colors[i].b=ptr[2]*4;
285 ptr+=3;
287 SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, f, n);
290 // установить адрес экранного буфера
291 // NULL - реальный экран
292 void V_setscr(void *p)
294 if (screen) SDL_Flip(screen);
297 // скопировать прямоугольник на экран
298 void V_copytoscr(short x,short w,short y,short h)
300 x*=HQ; y*=HQ; w*=HQ; h*=HQ;
301 SDL_UpdateRect(screen, x, y, w, h);
304 void V_maptoscr(int x,int w,int y,int h,void *cmap)
306 int cx,cy;
307 for (cx=x; cx<x+w; cx++)
308 for (cy=y; cy<y+h; cy++)
309 mappixel(cx,cy,(byte*)cmap);
310 V_copytoscr(x,w,y,h);
313 void V_remap_rect(int x,int y,int w,int h,byte *cmap)
315 int cx,cy;
316 for (cx=x; cx<x+w; cx++)
317 for (cy=y; cy<y+h; cy++)
318 mappixel(cx,cy,cmap);
321 extern void *walp[256];
322 extern byte clrmap[256*12];
324 void Z_drawfld(byte *fld, int bg)
326 byte *p = fld;
327 int x,y;
328 for (y=0; y<FLDH; y++)
330 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;
336 if (*p) {
337 vgaimg *pic = walp[*p];
338 if ((int)pic <= 3) {
339 if (!bg) {
340 byte *cmap = clrmap + ((int)pic+7)*256;
341 V_remap_rect(sx, sy, CELW, CELH, cmap);
344 else {
345 V_pic(sx, sy, pic);
349 p++;
354 void V_toggle()
356 if (!SDL_WM_ToggleFullScreen(screen)) {
357 int ncolors = screen->format->palette->ncolors;
358 SDL_Color colors[256];
359 int i;
360 for (i=0; i<ncolors; i++) {
361 colors[i].r = screen->format->palette->colors[i].r;
362 colors[i].g = screen->format->palette->colors[i].g;
363 colors[i].b = screen->format->palette->colors[i].b;
366 Uint32 flags = screen->flags;
368 SDL_FreeSurface(screen);
370 screen = SDL_SetVideoMode(0, 0, 0, flags ^ SDL_FULLSCREEN);
371 if(screen == NULL) {
372 ERR_fatal("Unable to set video mode\n");
373 exit(1);
376 SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, ncolors);