DEADSOFTWARE

software: fix crashes on buffer resize
[flatwaifu.git] / src / soft / 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 "error.h"
26 #include "view.h"
27 #include "memory.h"
28 #include "misc.h"
29 #include "files.h"
30 #include "system.h"
32 #include <string.h>
33 #include <assert.h>
35 int SCRW = 800;
36 int SCRH = 600;
37 char fullscreen = OFF;
39 byte bright[256];
40 byte mixmap[256][256];
41 byte clrmap[256*12];
43 byte *buffer;
44 int buf_w, buf_h, pitch;
45 static int offx, offy;
46 static int cx1, cx2, cy1, cy2;
47 static byte flametab[16] = {
48 0xBC,0xBA,0xB8,0xB6,0xB4,0xB2,0xB0,0xD5,0xD6,0xD7,0xA1,0xA0,0xE3,0xE2,0xE1,0xE0
49 };
51 vgaimg *V_getvgaimg (int id) {
52 int loaded = M_was_locked(id);
53 vgaimg *v = M_lock(id);
54 if (v != NULL && !loaded) {
55 v->w = short2host(v->w);
56 v->h = short2host(v->h);
57 v->sx = short2host(v->sx);
58 v->sy = short2host(v->sy);
59 }
60 return v;
61 }
63 vgaimg *V_loadvgaimg (char *name) {
64 return V_getvgaimg(F_getresid(name));
65 }
67 void V_update_buffer (void) {
68 Y_get_buffer(&buffer, &buf_w, &buf_h, &pitch);
69 V_setrect(0, 0, buf_w, buf_h);
70 }
72 static void draw_rect (int x, int y, int w, int h, int c) {
73 int i;
74 int x0 = max(x, cx1);
75 int y0 = max(y, cy1);
76 int x1 = min(x + w - 1, cx2);
77 int y1 = min(y + h - 1, cy2);
78 int len = x1 - x0;
79 for (i = y0; i <= y1; i++) {
80 memset(&buffer[i * pitch + x0], c, len);
81 }
82 }
84 void V_setrect (short x, short w, short y, short h) {
85 assert(w >= 0);
86 assert(h >= 0);
87 cx1 = max(x, 0);
88 cx2 = min(x + w - 1, buf_w - 1);
89 cy1 = max(y, 0);
90 cy2 = min(y + h - 1, buf_h - 1);
91 }
93 static void putpixel (int x, int y, byte color) {
94 if (x >= cx1 && x <= cx2 && y >= cy1 && y <= cy2) {
95 buffer[y * pitch + x] = color;
96 }
97 }
99 static byte getpixel (int x, int y) {
100 return x >= cx1 && x <= cx2 && y >= cy1 && y <= cy2 ? buffer[y * pitch + x] : 0;
103 static void mappixel (int x, int y, byte *cmap) {
104 byte c = getpixel(x, y);
105 putpixel(x, y, cmap[c]);
108 void V_center (int f) {
109 if (f) {
110 V_offset(buf_w / 2 - 320 / 2, buf_h / 2 - 200 / 2);
111 } else {
112 V_offset(0, 0);
116 void V_offset (int ox, int oy) {
117 offx = ox;
118 offy = oy;
121 static void draw_spr (short x, short y, vgaimg *i, int d, int c) {
122 if (i==NULL) return;
123 x += offx;
124 y += offy;
125 if (d & 1) x=x-i->w+i->sx; else x-=i->sx;
126 if (d & 2) y=y-i->h+i->sy; else y-=i->sy;
127 if(x+i->w>=cx1 && x<=cx2 && y+i->h>=cy1 && y<=cy2) {
128 int lx, ly;
129 byte *p = (byte*)i + sizeof(vgaimg);
130 for (ly=0; ly<i->h; ly++) {
131 for(lx=0; lx<i->w; lx++) {
132 int rx,ry;
133 rx = (d & 1) ? (i->w-lx-1) : (lx);
134 ry = (d & 2) ? (i->h-ly-1) : (ly);
135 if (*p) {
136 byte t = *p;
137 if (c) if (t>=0x70 && t<=0x7F) t=t-0x70+c;
138 putpixel(x+rx,y+ry,t);
140 p++;
146 void V_rotspr (int x, int y, vgaimg* i, int d) {
147 x+=i->w*((d&1)?1:0);
148 y+=i->h*((d&2)?1:0);
149 draw_spr(x,y,i,d,0);
152 void V_pic (short x, short y, vgaimg *i) {
153 draw_spr(x, y, i, 0, 0);
156 void V_manspr (int x, int y, void *p, unsigned char c) {
157 draw_spr(x, y, p, 0, c);
160 void V_manspr2(int x,int y,void *p, unsigned char c) {
161 draw_spr(x, y, p, 1, c);
164 void V_dot (short x, short y, unsigned char c) {
165 putpixel(x, y, c);
168 void smoke_sprf (int x, int y, byte c) {
169 byte t = getpixel(x,y);
170 c = c + bright[t];
171 c += 0x60;
172 c ^= 0xF;
173 putpixel(x,y,mixmap[c][t]);
176 void flame_sprf (int x, int y, byte c) {
177 byte t = getpixel(x,y);
178 c = c + bright[t];
179 putpixel(x,y,flametab[c]);
182 void V_sprf (short x,short y,vgaimg *i,spr_f *f) {
183 if (i==NULL) return;
184 x-=i->sx;
185 y-=i->sy;
186 int cx, cy;
187 byte *p = (byte*)i;
188 p+=sizeof(vgaimg);
189 for (cy=y; cy<y+i->h; cy++) {
190 for(cx=x; cx<x+i->w; cx++) {
191 if (*p) {
192 (*f)(cx, cy, *p);
194 p++;
199 void V_spr (short x, short y, vgaimg *i) {
200 draw_spr(x, y, i, 0, 0);
203 void V_spr2 (short x, short y, vgaimg *i) {
204 draw_spr(x,y,i,1,0);
207 void V_clr (short x, short w, short y, short h, unsigned char c) {
208 draw_rect(x, y, w, h, c);
211 void V_setscr (void *p) {
212 Y_repaint();
215 void V_copytoscr (short x, short w, short y, short h) {
216 Y_repaint_rect(x, y, w, h);
219 void V_maptoscr (int x, int w, int y, int h, void *cmap) {
220 int cx,cy;
221 for (cx=x; cx<x+w; cx++)
222 for (cy=y; cy<y+h; cy++)
223 mappixel(cx,cy,(byte*)cmap);
224 V_copytoscr(x,w,y,h);
227 void V_remap_rect (int x, int y, int w, int h, byte *cmap) {
228 int cx,cy;
229 for (cx=x; cx<x+w; cx++)
230 for (cy=y; cy<y+h; cy++)
231 mappixel(cx,cy,cmap);