DEADSOFTWARE

portability: avoid errors on some compilers
[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 "common/endianness.h"
29 #include <string.h>
30 #include <assert.h>
32 int SCRW = 800;
33 int SCRH = 600;
34 char fullscreen = OFF;
36 byte bright[256];
37 byte *mixmap; /* [256][256] */
38 byte *clrmap; /* [256*12] */
40 byte *buffer;
41 int buf_w, buf_h, pitch;
42 static int offx, offy;
43 static int cx1, cx2, cy1, cy2;
44 static byte flametab[16] = {
45 0xBC,0xBA,0xB8,0xB6,0xB4,0xB2,0xB0,0xD5,0xD6,0xD7,0xA1,0xA0,0xE3,0xE2,0xE1,0xE0
46 };
48 vgaimg *V_getvgaimg (int id) {
49 int loaded = M_was_locked(id);
50 vgaimg *v = M_lock(id);
51 if (v != NULL && !loaded) {
52 v->w = short2host(v->w);
53 v->h = short2host(v->h);
54 v->sx = short2host(v->sx);
55 v->sy = short2host(v->sy);
56 }
57 return v;
58 }
60 vgaimg *V_loadvgaimg (char *name) {
61 return V_getvgaimg(F_getresid(name));
62 }
64 void V_update_buffer (void) {
65 Y_get_buffer(&buffer, &buf_w, &buf_h, &pitch);
66 V_setrect(0, 0, buf_w, buf_h);
67 }
69 static void draw_rect (int x, int y, int w, int h, int c) {
70 int i;
71 int x0 = max(x, cx1);
72 int y0 = max(y, cy1);
73 int x1 = min(x + w - 1, cx2);
74 int y1 = min(y + h - 1, cy2);
75 int len = x1 - x0;
76 for (i = y0; i <= y1; i++) {
77 memset(&buffer[i * pitch + x0], c, len);
78 }
79 }
81 void V_setrect (short x, short w, short y, short h) {
82 assert(w >= 0);
83 assert(h >= 0);
84 cx1 = max(x, 0);
85 cx2 = min(x + w - 1, buf_w - 1);
86 cy1 = max(y, 0);
87 cy2 = min(y + h - 1, buf_h - 1);
88 }
90 static void putpixel (int x, int y, byte color) {
91 if (x >= cx1 && x <= cx2 && y >= cy1 && y <= cy2) {
92 buffer[y * pitch + x] = color;
93 }
94 }
96 static byte getpixel (int x, int y) {
97 return x >= cx1 && x <= cx2 && y >= cy1 && y <= cy2 ? buffer[y * pitch + x] : 0;
98 }
100 static void mappixel (int x, int y, byte *cmap) {
101 byte c = getpixel(x, y);
102 putpixel(x, y, cmap[c]);
105 void V_center (int f) {
106 if (f) {
107 V_offset(buf_w / 2 - 320 / 2, buf_h / 2 - 200 / 2);
108 } else {
109 V_offset(0, 0);
113 void V_offset (int ox, int oy) {
114 offx = ox;
115 offy = oy;
118 static void draw_spr (short x, short y, vgaimg *i, int d, int c) {
119 if (i==NULL) return;
120 x += offx;
121 y += offy;
122 if (d & 1) x=x-i->w+i->sx; else x-=i->sx;
123 if (d & 2) y=y-i->h+i->sy; else y-=i->sy;
124 if(x+i->w>=cx1 && x<=cx2 && y+i->h>=cy1 && y<=cy2) {
125 int lx, ly;
126 byte *p = (byte*)i + sizeof(vgaimg);
127 for (ly=0; ly<i->h; ly++) {
128 for(lx=0; lx<i->w; lx++) {
129 int rx,ry;
130 rx = (d & 1) ? (i->w-lx-1) : (lx);
131 ry = (d & 2) ? (i->h-ly-1) : (ly);
132 if (*p) {
133 byte t = *p;
134 if (c) if (t>=0x70 && t<=0x7F) t=t-0x70+c;
135 putpixel(x+rx,y+ry,t);
137 p++;
143 void V_rotspr (int x, int y, vgaimg* i, int d) {
144 x+=i->w*((d&1)?1:0);
145 y+=i->h*((d&2)?1:0);
146 draw_spr(x,y,i,d,0);
149 void V_pic (short x, short y, vgaimg *i) {
150 draw_spr(x, y, i, 0, 0);
153 void V_manspr (int x, int y, void *p, unsigned char c) {
154 draw_spr(x, y, p, 0, c);
157 void V_manspr2(int x,int y,void *p, unsigned char c) {
158 draw_spr(x, y, p, 1, c);
161 void V_dot (short x, short y, unsigned char c) {
162 putpixel(x, y, c);
165 void smoke_sprf (int x, int y, byte c) {
166 byte t = getpixel(x,y);
167 c = c + bright[t];
168 c += 0x60;
169 c ^= 0xF;
170 //putpixel(x,y,mixmap[c][t]);
171 putpixel(x, y, mixmap[t * 256 + c]);
174 void flame_sprf (int x, int y, byte c) {
175 byte t = getpixel(x,y);
176 c = c + bright[t];
177 putpixel(x,y,flametab[c]);
180 void V_sprf (short x,short y,vgaimg *i,spr_f *f) {
181 if (i==NULL) return;
182 x-=i->sx;
183 y-=i->sy;
184 int cx, cy;
185 byte *p = (byte*)i;
186 p+=sizeof(vgaimg);
187 for (cy=y; cy<y+i->h; cy++) {
188 for(cx=x; cx<x+i->w; cx++) {
189 if (*p) {
190 (*f)(cx, cy, *p);
192 p++;
197 void V_spr (short x, short y, vgaimg *i) {
198 draw_spr(x, y, i, 0, 0);
201 void V_spr2 (short x, short y, vgaimg *i) {
202 draw_spr(x,y,i,1,0);
205 void V_clr (short x, short w, short y, short h, unsigned char c) {
206 draw_rect(x, y, w, h, c);
209 void V_setscr (void *p) {
210 Y_repaint();
213 void V_copytoscr (short x, short w, short y, short h) {
214 Y_repaint_rect(x, y, w, h);
217 void V_maptoscr (int x, int w, int y, int h, void *cmap) {
218 int cx,cy;
219 for (cx=x; cx<x+w; cx++)
220 for (cy=y; cy<y+h; cy++)
221 mappixel(cx,cy,(byte*)cmap);
222 V_copytoscr(x,w,y,h);
225 void V_remap_rect (int x, int y, int w, int h, byte *cmap) {
226 int cx,cy;
227 for (cx=x; cx<x+w; cx++)
228 for (cy=y; cy<y+h; cy++)
229 mappixel(cx,cy,cmap);