DEADSOFTWARE

headers describes that c-files implements
[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];
41 int SCRW = 800;
42 int SCRH = 600;
43 char fullscreen = OFF;
45 byte bright[256];
46 byte mixmap[256][256];
47 byte clrmap[256*12];
49 static SDL_Surface* screen = NULL;
50 static int cx1,cx2,cy1,cy2;
51 static byte flametab[16] = {
52 0xBC,0xBA,0xB8,0xB6,0xB4,0xB2,0xB0,0xD5,0xD6,0xD7,0xA1,0xA0,0xE3,0xE2,0xE1,0xE0
53 };
54 static int offx = 0;
55 static int offy = 0;
57 #define HQ 2
59 vgaimg *V_getvgaimg (int id) {
60 int loaded = M_was_locked(id);
61 vgaimg *v = M_lock(id);
62 if (v != NULL && !loaded) {
63 v->w = short2host(v->w);
64 v->h = short2host(v->h);
65 v->sx = short2host(v->sx);
66 v->sy = short2host(v->sy);
67 }
68 return v;
69 }
71 vgaimg *V_loadvgaimg (char *name) {
72 return V_getvgaimg(F_getresid(name));
73 }
75 short V_init (void) {
76 Uint32 flags = SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_HWPALETTE;
77 if (fullscreen) flags = flags | SDL_FULLSCREEN;
78 screen = SDL_SetVideoMode(SCRW, SCRH, 8, flags);
79 if (!screen) ERR_failinit("Unable to set video mode: %s\n", SDL_GetError());
80 SCRW /= HQ;
81 SCRH /= HQ;
82 return 0;
83 }
85 // переключение в текстовый режим
86 void V_done (void) {
87 SDL_Quit();
88 }
90 static void draw_rect (int x, int y, int w, int h, int c) {
91 SDL_Rect dstrect;
92 dstrect.x = x*HQ;
93 dstrect.y = y*HQ;
94 dstrect.w = w*HQ;
95 dstrect.h = h*HQ;
96 SDL_FillRect(screen, &dstrect, c);
97 }
99 // установить область вывода
100 void V_setrect (short x,short w,short y,short h) {
101 SDL_Rect r;
102 r.x=x*HQ;
103 r.y=y*HQ;
104 r.w=w*HQ;
105 r.h=h*HQ;
106 SDL_SetClipRect(screen, &r);
107 SDL_GetClipRect(screen, &r);
108 cx1 = x;
109 cx2 = x+w-1;
110 cy1 = y;
111 cy2 = y+h-1;
112 if (cx1<0) cx1=0;
113 if (cx2>=SCRW) cx2=SCRW-1;
114 if (cy1<0) cy1=0;
115 if (cy2>=SCRH) cy2=SCRH-1;
118 static void putpixel (int x, int y, Uint8 color) {
119 if(x>=cx1 && x<=cx2 && y>=cy1 && y<=cy2) {
120 x*=HQ;
121 y*=HQ;
122 Uint8 *p = (Uint8 *)screen->pixels + y*screen->pitch + x;
123 *p = color;
124 *(p+1) = color;
125 p += screen->pitch;
126 *p = color;
127 *(p+1) = color;
131 static byte getpixel (int x, int y) {
132 if(x>=cx1 && x<=cx2 && y>=cy1 && y<=cy2) {
133 x*=HQ;
134 y*=HQ;
135 return *((Uint8 *)screen->pixels + y*screen->pitch + x);
137 return 0;
140 static void mappixel (int x, int y, byte* cmap) {
141 byte c = getpixel(x,y);
142 putpixel(x,y,cmap[c]);
145 void V_center (int f) {
146 if (f) V_offset(SCRW/2-320/2, SCRH/2-200/2);
147 else V_offset(0, 0);
150 void V_offset (int ox, int oy) {
151 offx=ox;
152 offy=oy;
155 static void draw_spr (short x, short y, vgaimg *i, int d, int c) {
156 if (i==NULL) return;
157 x += offx;
158 y += offy;
159 if (d & 1) x=x-i->w+i->sx; else x-=i->sx;
160 if (d & 2) y=y-i->h+i->sy; else y-=i->sy;
161 if(x+i->w>=cx1 && x<=cx2 && y+i->h>=cy1 && y<=cy2) {
162 int lx, ly;
163 byte *p = (byte*)i + sizeof(vgaimg);
164 for (ly=0; ly<i->h; ly++) {
165 for(lx=0; lx<i->w; lx++) {
166 int rx,ry;
167 rx = (d & 1) ? (i->w-lx-1) : (rx=lx);
168 ry = (d & 2) ? (i->h-ly-1) : (ry=ly);
169 if (*p) {
170 byte t = *p;
171 if (c) if (t>=0x70 && t<=0x7F) t=t-0x70+c;
172 putpixel(x+rx,y+ry,t);
174 p++;
180 void V_rotspr (int x, int y, vgaimg* i, int d) {
181 x+=i->w*((d&1)?1:0);
182 y+=i->h*((d&2)?1:0);
183 draw_spr(x,y,i,d,0);
186 void V_pic (short x, short y, vgaimg *i) {
187 draw_spr(x, y, i, 0, 0);
190 void V_manspr (int x, int y, void *p, unsigned char c) {
191 draw_spr(x, y, p, 0, c);
194 void V_manspr2(int x,int y,void *p, unsigned char c) {
195 draw_spr(x, y, p, 1, c);
198 // вывести точку цвета c в координатах (x,y)
199 void V_dot (short x,short y, unsigned char c) {
200 putpixel(x,y,c);
203 void smoke_sprf (int x, int y, byte c) {
204 byte t = getpixel(x,y);
205 c = c + bright[t];
206 c += 0x60;
207 c ^= 0xF;
208 putpixel(x,y,mixmap[c][t]);
211 void flame_sprf (int x, int y, byte c) {
212 byte t = getpixel(x,y);
213 c = c + bright[t];
214 putpixel(x,y,flametab[c]);
217 void V_sprf (short x,short y,vgaimg *i,spr_f *f) {
218 if (i==NULL) return;
219 x-=i->sx;
220 y-=i->sy;
221 int cx, cy;
222 byte *p = (byte*)i;
223 p+=sizeof(vgaimg);
224 for (cy=y; cy<y+i->h; cy++) {
225 for(cx=x; cx<x+i->w; cx++) {
226 if (*p) {
227 (*f)(cx, cy, *p);
229 p++;
234 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) {
239 draw_spr(x,y,i,1,0);
242 void V_clr (short x, short w, short y, short h, unsigned char c) {
243 draw_rect(x, y, w, h, c);
246 // установить n цветов, начиная с f, из массива p
247 static void VP_set (void *p, short f, short n) {
248 byte *ptr = (byte*)p;
249 SDL_Color colors[256];
250 int i;
251 for(i=f;i<f+n;i++)
253 colors[i].r=ptr[0]*4;
254 colors[i].g=ptr[1]*4;
255 colors[i].b=ptr[2]*4;
256 ptr+=3;
258 SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, f, n);
261 // установить палитру из массива p
262 void VP_setall (void *p) {
263 VP_set(p, 0, 256);
266 // установить адрес экранного буфера
267 // NULL - реальный экран
268 void V_setscr (void *p) {
269 if (screen) SDL_Flip(screen);
272 // скопировать прямоугольник на экран
273 void V_copytoscr (short x, short w, short y, short h) {
274 x*=HQ; y*=HQ; w*=HQ; h*=HQ;
275 SDL_UpdateRect(screen, x, y, w, h);
278 void V_maptoscr (int x, int w, int y, int h, void *cmap) {
279 int cx,cy;
280 for (cx=x; cx<x+w; cx++)
281 for (cy=y; cy<y+h; cy++)
282 mappixel(cx,cy,(byte*)cmap);
283 V_copytoscr(x,w,y,h);
286 void V_remap_rect (int x, int y, int w, int h, byte *cmap) {
287 int cx,cy;
288 for (cx=x; cx<x+w; cx++)
289 for (cy=y; cy<y+h; cy++)
290 mappixel(cx,cy,cmap);
293 void V_toggle (void) {
294 if (!SDL_WM_ToggleFullScreen(screen)) {
295 int ncolors = screen->format->palette->ncolors;
296 SDL_Color colors[256];
297 int i;
298 for (i=0; i<ncolors; i++) {
299 colors[i].r = screen->format->palette->colors[i].r;
300 colors[i].g = screen->format->palette->colors[i].g;
301 colors[i].b = screen->format->palette->colors[i].b;
304 Uint32 flags = screen->flags;
306 SDL_FreeSurface(screen);
308 screen = SDL_SetVideoMode(0, 0, 0, flags ^ SDL_FULLSCREEN);
309 if(screen == NULL) {
310 ERR_fatal("Unable to set video mode\n");
311 exit(1);
314 SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, ncolors);