1 /* Project 16 Source Code~
\r
2 * Copyright (C) 2012-2022 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover
\r
4 * This file is part of Project 16.
\r
6 * Project 16 is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Project 16 is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with this program. If not, see <http://www.gnu.org/licenses/>, or
\r
18 * write to the Free Software Foundation, Inc., 51 Franklin Street,
\r
19 * Fifth Floor, Boston, MA 02110-1301 USA.
\r
23 * Functions for handling modex and doing other basic graphics stuff.
\r
28 #include "src/lib/16_head.h"
\r
29 #include "src/lib/16_tail.h"
\r
30 //#include "src/lib/planar.h"
\r
31 //#include "src/lib/modex16/16planar.h"
\r
32 #include "src/lib/16text.h"
\r
33 ////#include "src/lib/modex16/320x240.h"
\r
34 // #include "src/lib/modex16/320x200.h"
\r
35 // #include "src/lib/modex16/256x192.h"
\r
36 // #include "src/lib/modex16/192x144_.h"
\r
37 // #include "src/lib/modex16/160x120.h"
\r
44 #define TILEWHD TILEWH*2
\r
45 #define QUADWH TILEWH/2
\r
47 //#define OLDBGPRESERVE
\r
49 /* -========================== Types & Macros ==========================- */
\r
50 #define PAGE_OFFSET(x,y) (((y)<<6)+((y)<<4)+((x)>>2))
\r
51 #define PLANE(x) (1 << ((x) & 3))
\r
52 #define SELECT_ALL_PLANES() outpw(0x03c4, 0xff02)
\r
54 // clips for rectangles not on 4s
\r
56 static byte lclip[4] = {0x0f, 0x0e, 0x0c, 0x08}; \
\r
57 static byte rclip[4] = {0x00, 0x01, 0x03, 0x07};
\r
60 static byte pclip[4] = {1,2,4,8};
\r
69 /* -======================= Constants & Vars ==========================- */
\r
70 extern byte far* VGA; /* The VGA Memory */
\r
71 extern byte far gamepal;
\r
72 extern byte far corepal; //core palette used universally
\r
73 #define SCREEN_SEG 0xa000
\r
74 #define VIDEO_INT 0x10
\r
75 #define SET_MODE 0x00
\r
76 #define VGA_256_COLOR_MODE 0x13
\r
77 #define TEXT_MODE 0x03
\r
79 #define SC_INDEX 0x03c4
\r
82 #define SC_MAPMASK 2
\r
83 #define SC_CHARMAP 3
\r
84 #define SC_MEMMODE 4
\r
86 #define CRTC_INDEX 0x3D4
\r
87 #define CRTC_H_TOTAL 0
\r
88 #define CRTC_H_DISPEND 1
\r
89 #define CRTC_H_BLANK 2
\r
90 #define CRTC_H_ENDBLANK 3
\r
91 #define CRTC_H_RETRACE 4
\r
92 #define CRTC_H_ENDRETRACE 5
\r
93 #define CRTC_V_TOTAL 6
\r
94 #define CRTC_OVERFLOW 7
\r
95 #define CRTC_ROWSCAN 8
\r
96 #define CRTC_MAXSCANLINE 9
\r
97 #define CRTC_CURSORSTART 10
\r
98 #define CRTC_CURSOREND 11
\r
99 #define CRTC_STARTHIGH 12
\r
100 #define CRTC_STARTLOW 13
\r
101 #define CRTC_CURSORHIGH 14
\r
102 #define CRTC_CURSORLOW 15
\r
103 #define CRTC_V_RETRACE 16
\r
104 #define CRTC_V_ENDRETRACE 17
\r
105 #define CRTC_V_DISPEND 18
\r
106 #define CRTC_OFFSET 19
\r
107 #define CRTC_UNDERLINE 20
\r
108 #define CRTC_V_BLANK 21
\r
109 #define CRTC_V_ENDBLANK 22
\r
110 #define CRTC_MODE 23
\r
111 #define CRTC_LINECOMPARE 24
\r
113 #define GC_INDEX 0x03ce
\r
114 #define GC_SETRESET 0
\r
115 #define GC_ENABLESETRESET 1
\r
116 #define GC_COLORCOMPARE 2
\r
117 #define GC_DATAROTATE 3
\r
118 #define GC_READMAP 4
\r
120 #define GC_MISCELLANEOUS 6
\r
121 #define GC_COLORDONTCARE 7
\r
122 #define GC_BITMASK 8
\r
124 #define AC_INDEX 0x03c0
\r
125 #define SC_DATA 0x03c5
\r
126 #define CRTC_DATA 0x03d5
\r
127 #define MISC_OUTPUT 0x03c2
\r
128 #define HIGH_ADDRESS 0x0C
\r
129 #define LOW_ADDRESS 0x0D
\r
130 #define VRETRACE 0x08
\r
131 //#define INPUT _STATUS_1 defined in 16_head
\r
132 //#define STATUS_REGISTER_1 0x03da
\r
134 #define STATUS_REGISTER_1 0x3da
\r
136 #define ATR_INDEX AC_INDEX
\r
137 #define ATR_OVERSCAN 17
\r
138 #define DISPLAY_ENABLE 0x01
\r
139 //#define MAP_MASK 0x02
\r
140 #define PAL_READ_REG 0x03C7 /* Color register, read address */
\r
141 #define PAL_WRITE_REG 0x03C8 /* Color register, write address */
\r
142 #define PAL_DATA_REG 0x03C9 /* Color register, data port */
\r
143 #define PAL_SIZE (256 * 3)
\r
145 //===========================================================================
\r
147 #define SCREENSEG 0xa000
\r
149 //#define SCREENWIDTH 80 // default screen width in bytes
\r
150 //#define MAXSCANLINES 240 // size of ylookup table
\r
152 //#define CHARWIDTH 2
\r
153 //#define TILEWIDTH 4
\r
155 //===========================================================================
\r
158 // VGA hardware routines
\r
160 void VGAWRITEMODE(byte x),
\r
161 VGAMAPMASK(byte x),
\r
162 VGAREADMAP(byte x),
\r
163 VGABITMASK(byte x);
\r
165 #define VW_Hlin(x,z,y,c,q) VL_Hlin(x,y,(z)-(x)+1,c,q)
\r
166 #define VW_Vlin(y,z,x,c,q) VL_Vlin(x,y,(z)-(y)+1,c,q)
\r
167 #define PALPROGRAMSNEWPAGE gvar.video.page[0] = modexDefaultPage(&gvar.video.page[0], &gvar);// modexFadeOn(4, &gvar.video.palette);
\r
169 /* -============================ Functions =============================- */
\r
170 /* mode switching, page, and plane functions */
\r
171 void VL_Startup (global_game_variables_t *gvar),
\r
172 VL_Shutdown (global_game_variables_t *gvar),
\r
173 VL_SetVGAPlaneMode (global_game_variables_t *gvar),
\r
174 VL_ClearVideo (byte color),
\r
176 VGAmodeX(sword vq, boolean cmem, global_game_variables_t *gv),
\r
177 modexEnter(sword vq, boolean cmem, global_game_variables_t *gv);
\r
178 //void VL_vgaSetMode(byte mode);
\r
179 byte VL_vgaGetMode(void);
\r
180 extern void TL_VidInit(global_game_variables_t *gvar);
\r
181 void modexLeave(void);
\r
182 void modexsetBaseXMode();
\r
183 page_t modexDefaultPage(page_t *p, global_game_variables_t *gvar);
\r
184 page_t modexNextPage(page_t *p);
\r
185 page_t modexNextPageFlexibleSize(page_t *p, word x, word y);
\r
186 void modexCalcVmemRemain(video_t *video);
\r
187 void VL_Initofs(video_t *video);
\r
188 void modexHiganbanaPageSetup(global_game_variables_t *gvar);
\r
189 void modexShowPage(page_t *page);//for 0croll.exe
\r
190 void VL_ShowPage(page_t *page, boolean vsync, boolean sr);
\r
191 void modexPanPage(page_t *page, int dx, int dy);
\r
192 void modexSelectPlane(byte plane);
\r
193 void modexClearRegion(page_t *page, int x, int y, int w, int h, byte color);
\r
194 /* moved to src/lib/modex16/16render.c */
\r
195 void modexCopyPageRegion(page_t *dest, page_t *src, word sx, word sy, word dx, word dy, word width, word height);
\r
197 /* Palette fade and flash effects */
\r
198 void VL_FadeIn (int start, int end, byte far *palette, int steps, video_t *v);
\r
199 void modexFadeOn(word fade, byte *palette);
\r
200 void modexFadeOff(word fade, byte *palette);
\r
201 void modexFlashOn(word fade, byte *palette);
\r
202 void modexFlashOff(word fade, byte *palette);
\r
204 /* palette loading and saving */
\r
205 void VL_SetPalette (byte far *palette, video_t *v);
\r
206 void modexPalSave(byte *palette);
\r
207 byte *modexNewPal();
\r
208 void modexLoadPalFile(char *filename, byte *palette);
\r
209 static void loadPcxStage1(FILE *file, bitmap_t *result);
\r
210 void loadPcxPalette(FILE *file, bitmap_t *result);
\r
211 bitmap_t bitmapLoadPcx(char *filename);
\r
212 tileset_t bitmapLoadPcxTiles(char *filename, word twidth, word theight);
\r
213 void oldDrawBmp(byte far* page, int x, int y, bitmap_t *bmp, byte sprite);
\r
214 void modexDrawBmp(page_t *page, int x, int y, bitmap_t *bmp);
\r
215 void modexDrawBmpRegion(page_t *page, int x, int y, int rx, int ry, int rw, int rh, bitmap_t *bmp);
\r
216 void modexDrawSprite(page_t *page, int x, int y, bitmap_t *bmp);
\r
217 void modexDrawSpriteRegion(page_t *page, int x, int y, int rx, int ry, int rw, int rh, bitmap_t *bmp);
\r
218 void modexDrawBmpPBuf(page_t *page, int x, int y, planar_buf_t *bmp); /*pbuf version*/
\r
219 void modexDrawBmpPBufRegion(page_t *page, int x, int y, int rx, int ry, int rw, int rh, planar_buf_t *bmp);
\r
220 void modexDrawSpritePBuf(page_t *page, int x, int y, planar_buf_t *bmp);
\r
221 void modexDrawSpritePBufRegion(page_t *page, int x, int y, int rx, int ry, int rw, int rh, planar_buf_t *bmp);
\r
222 void VL_LoadPalFile(const char *filename, byte *palette, global_game_variables_t *gvar);
\r
223 void VL_LoadPalFileCore(byte *palette, global_game_variables_t *gvar);
\r
224 void VL_SetCorePal(global_game_variables_t *gvar);
\r
225 void VLL_LoadPalFilewithoffset(const char *filename, byte *palette, word o, word palsize, global_game_variables_t *gvar);
\r
226 void VL_UpdatePaletteWrite(byte *palette, word o, word palsize, global_game_variables_t *gvar);
\r
227 void VL_PaletteSync(global_game_variables_t *gvar);
\r
228 void modexSavePalFile(char *filename, byte *palette);
\r
229 #define MenuFadeIn() VL_FadeIn(0,255,&gamepal,10)
\r
231 /* fixed palette functions */
\r
232 void modexPalBlack();
\r
233 void modexPalWhite();
\r
235 /* utility functions */
\r
236 void modexPalUpdate(byte *p);
\r
237 void VL_modexPalScramble(byte *p);
\r
238 word VL_modexPalOverscan(byte *p, word col);
\r
239 void VL_ColorBorder (int color, video_t *v);
\r
240 void VL_Plot (int x, int y, int color, global_game_variables_t *gvar);
\r
241 void VL_Hlin (unsigned x, unsigned y, unsigned width, unsigned color, global_game_variables_t *gvar);
\r
242 void VL_Vlin (int x, int y, int height, int color, global_game_variables_t *gvar);
\r
243 void VL_Bar (int x, int y, int width, int height, int color, global_game_variables_t *gvar);
\r
244 void VL_MemToScreen (byte far *source, int width, int height, int x, int y, global_game_variables_t *gvar);
\r
245 void modexputPixel(page_t *page, int x, int y, byte color);
\r
246 byte modexgetPixel(page_t *page, int x, int y);
\r
248 #if 0 // not needed anymore. maybe good for reference purposes though.
\r
249 static inline void modexwritepixel(page_t *page, int x, int y, word addr, byte color)
\r
251 /* Each address accesses four neighboring pixels, so set
\r
252 Write Plane Enable according to which pixel we want
\r
253 to modify. The plane is determined by the two least
\r
254 significant bits of the x-coordinate: */
\r
255 modexSelectPlane(PLANE(x));
\r
256 //outp(SC_INDEX, 0x02);
\r
257 //outp(SC_DATA, 0x01 << (x & 3));
\r
259 /* The offset of the pixel into the video segment is
\r
260 offset = (width * y + x) / 4, and write the given
\r
261 color to the plane we selected above. Heed the active
\r
262 page start selection. */
\r
263 vga_state.vga_graphics_ram[addr] = color;
\r
265 static inline byte modexreadPixel(page_t *page, int x, int y, word addr)
\r
267 /* Select the plane from which we must read the pixel color: */
\r
268 outpw(GC_INDEX, 0x04);
\r
269 outpw(GC_INDEX+1, x & 3);
\r
270 return vga_state.vga_graphics_ram[addr];
\r
274 void modexDrawChar(page_t *page, int x/*for planar selection only*/, word t, word col, word bgcol, word addr);
\r
275 void modexprint(page_t *page, sword x, sword y, word t, boolean tlsw, word color, word bgcolor, boolean vidsw, const byte *str);
\r
276 void modexprintbig(page_t *page, word x, word y, word t, word col, word bgcol, const byte *str);
\r
277 void VL_modexPrintTextBox(global_game_variables_t *gvar);
\r
278 void VL_print(const byte *str, nibble pagenum, global_game_variables_t *gvar);
\r
279 void modexpdump(nibble pagenum, global_game_variables_t *gvar);
\r
280 void VL_PatternDraw(video_t *video, word pn, boolean sw, boolean allsw);
\r
281 void modexWaitBorder();
\r
282 void modexWaitBorder_start();
\r
283 void modexWaitBorder_end();
\r
284 void VL_WaitVBL(word num);
\r
285 void VL_SetLineWidth (unsigned width, global_game_variables_t *gvar);
\r
286 void VL_PrintmodexmemInfo(video_t *v);
\r