DEADSOFTWARE

63b5f65b7f104ef2204b2058a6dfb6c6fe9266fa
[flatwaifu.git] / src / soft / vga.c
1 /* Copyright (C) 1996-1997 Aleksey Volynskov
2 * Copyright (C) 2011 Rambo
3 * Copyright (C) 2020 SovietPony
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
18 #include "glob.h"
19 #include "vga.h"
20 #include "error.h"
21 #include "view.h"
22 #include "memory.h"
23 #include "misc.h"
24 #include "files.h"
25 #include "system.h"
27 #include <string.h>
28 #include <assert.h>
30 int SCRW = 800;
31 int SCRH = 600;
32 char fullscreen = OFF;
34 byte bright[256];
35 byte mixmap[256][256];
36 byte clrmap[256*12];
38 byte *buffer;
39 int buf_w, buf_h, pitch;
40 static int offx, offy;
41 static int cx1, cx2, cy1, cy2;
42 static byte flametab[16] = {
43 0xBC,0xBA,0xB8,0xB6,0xB4,0xB2,0xB0,0xD5,0xD6,0xD7,0xA1,0xA0,0xE3,0xE2,0xE1,0xE0
44 };
46 vgaimg *V_getvgaimg (int id) {
47 int loaded = M_was_locked(id);
48 vgaimg *v = M_lock(id);
49 if (v != NULL && !loaded) {
50 v->w = short2host(v->w);
51 v->h = short2host(v->h);
52 v->sx = short2host(v->sx);
53 v->sy = short2host(v->sy);
54 }
55 return v;
56 }
58 vgaimg *V_loadvgaimg (char *name) {
59 return V_getvgaimg(F_getresid(name));
60 }
62 void V_update_buffer (void) {
63 Y_get_buffer(&buffer, &buf_w, &buf_h, &pitch);
64 V_setrect(0, 0, buf_w, buf_h);
65 }
67 static void draw_rect (int x, int y, int w, int h, int c) {
68 int i;
69 int x0 = max(x, cx1);
70 int y0 = max(y, cy1);
71 int x1 = min(x + w - 1, cx2);
72 int y1 = min(y + h - 1, cy2);
73 int len = x1 - x0;
74 for (i = y0; i <= y1; i++) {
75 memset(&buffer[i * pitch + x0], c, len);
76 }
77 }
79 void V_setrect (short x, short w, short y, short h) {
80 assert(w >= 0);
81 assert(h >= 0);
82 cx1 = max(x, 0);
83 cx2 = min(x + w - 1, buf_w - 1);
84 cy1 = max(y, 0);
85 cy2 = min(y + h - 1, buf_h - 1);
86 }
88 static void putpixel (int x, int y, byte color) {
89 if (x >= cx1 && x <= cx2 && y >= cy1 && y <= cy2) {
90 buffer[y * pitch + x] = color;
91 }
92 }
94 static byte getpixel (int x, int y) {
95 return x >= cx1 && x <= cx2 && y >= cy1 && y <= cy2 ? buffer[y * pitch + x] : 0;
96 }
98 static void mappixel (int x, int y, byte *cmap) {
99 byte c = getpixel(x, y);
100 putpixel(x, y, cmap[c]);
103 void V_center (int f) {
104 if (f) {
105 V_offset(buf_w / 2 - 320 / 2, buf_h / 2 - 200 / 2);
106 } else {
107 V_offset(0, 0);
111 void V_offset (int ox, int oy) {
112 offx = ox;
113 offy = oy;
116 static void draw_spr (short x, short y, vgaimg *i, int d, int c) {
117 if (i==NULL) return;
118 x += offx;
119 y += offy;
120 if (d & 1) x=x-i->w+i->sx; else x-=i->sx;
121 if (d & 2) y=y-i->h+i->sy; else y-=i->sy;
122 if(x+i->w>=cx1 && x<=cx2 && y+i->h>=cy1 && y<=cy2) {
123 int lx, ly;
124 byte *p = (byte*)i + sizeof(vgaimg);
125 for (ly=0; ly<i->h; ly++) {
126 for(lx=0; lx<i->w; lx++) {
127 int rx,ry;
128 rx = (d & 1) ? (i->w-lx-1) : (lx);
129 ry = (d & 2) ? (i->h-ly-1) : (ly);
130 if (*p) {
131 byte t = *p;
132 if (c) if (t>=0x70 && t<=0x7F) t=t-0x70+c;
133 putpixel(x+rx,y+ry,t);
135 p++;
141 void V_rotspr (int x, int y, vgaimg* i, int d) {
142 x+=i->w*((d&1)?1:0);
143 y+=i->h*((d&2)?1:0);
144 draw_spr(x,y,i,d,0);
147 void V_pic (short x, short y, vgaimg *i) {
148 draw_spr(x, y, i, 0, 0);
151 void V_manspr (int x, int y, void *p, unsigned char c) {
152 draw_spr(x, y, p, 0, c);
155 void V_manspr2(int x,int y,void *p, unsigned char c) {
156 draw_spr(x, y, p, 1, c);
159 void V_dot (short x, short y, unsigned char c) {
160 putpixel(x, y, c);
163 void smoke_sprf (int x, int y, byte c) {
164 byte t = getpixel(x,y);
165 c = c + bright[t];
166 c += 0x60;
167 c ^= 0xF;
168 putpixel(x,y,mixmap[c][t]);
171 void flame_sprf (int x, int y, byte c) {
172 byte t = getpixel(x,y);
173 c = c + bright[t];
174 putpixel(x,y,flametab[c]);
177 void V_sprf (short x,short y,vgaimg *i,spr_f *f) {
178 if (i==NULL) return;
179 x-=i->sx;
180 y-=i->sy;
181 int cx, cy;
182 byte *p = (byte*)i;
183 p+=sizeof(vgaimg);
184 for (cy=y; cy<y+i->h; cy++) {
185 for(cx=x; cx<x+i->w; cx++) {
186 if (*p) {
187 (*f)(cx, cy, *p);
189 p++;
194 void V_spr (short x, short y, vgaimg *i) {
195 draw_spr(x, y, i, 0, 0);
198 void V_spr2 (short x, short y, vgaimg *i) {
199 draw_spr(x,y,i,1,0);
202 void V_clr (short x, short w, short y, short h, unsigned char c) {
203 draw_rect(x, y, w, h, c);
206 void V_setscr (void *p) {
207 Y_repaint();
210 void V_copytoscr (short x, short w, short y, short h) {
211 Y_repaint_rect(x, y, w, h);
214 void V_maptoscr (int x, int w, int y, int h, void *cmap) {
215 int cx,cy;
216 for (cx=x; cx<x+w; cx++)
217 for (cy=y; cy<y+h; cy++)
218 mappixel(cx,cy,(byte*)cmap);
219 V_copytoscr(x,w,y,h);
222 void V_remap_rect (int x, int y, int w, int h, byte *cmap) {
223 int cx,cy;
224 for (cx=x; cx<x+w; cx++)
225 for (cy=y; cy<y+h; cy++)
226 mappixel(cx,cy,cmap);