From cbeff523d3c01471fcddfbf713b5d8d9e36dc1a9 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Tue, 30 May 2023 23:32:52 +0100 Subject: pngpal tool and first stab at embedding data on the EXE WIP; the embedded data has an "environ" symbol that will cause issues when we embed more than one piece of data. --- Makefile | 4 ---- README.md | 1 + TODO.md | 2 +- data/sprites.png | Bin 0 -> 345 bytes src/Makefile | 5 ++++- src/main.c | 12 +++++++++-- src/vga.c | 8 ++++++++ src/vga.h | 3 +++ tools/pngpal.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 data/sprites.png create mode 100755 tools/pngpal.py diff --git a/Makefile b/Makefile index 0c23670..8fbd78e 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,13 @@ all: - #make -C tools - #make -C data make -j -C src all run: all dosbox -config dosbox.conf clean: - #make -C data clean make -C src clean cleanall: make -C src clean - #make -C tools clean .PHONY: all clean run diff --git a/README.md b/README.md index bdf738e..8a07d71 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ Development: - GNU Make - DJGPP C (using GCC 12.1.0; other may work) - DOSBOX +- tools: Python 3 and PIL (or Pillow) library For DJGPP I recommend cross-compilation using [build-djgpp](https://github.com/andrewwutw/build-djgpp/). diff --git a/TODO.md b/TODO.md index b7d7aad..b105713 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,6 @@ # TODO - drawing - - set palette - sw blitter - time - vga vsync @@ -25,3 +24,4 @@ # REVIEW +- embeded data: remove the environ symbol! diff --git a/data/sprites.png b/data/sprites.png new file mode 100644 index 0000000..bfe73d4 Binary files /dev/null and b/data/sprites.png differ diff --git a/src/Makefile b/src/Makefile index 94b83eb..a53c697 100644 --- a/src/Makefile +++ b/src/Makefile @@ -4,7 +4,7 @@ CFLAGS := -I. -c -Wall -pedantic -O2 -march=i386 LDFLAGS := -s SRCS := $(wildcard *.c) -OBJS := $(SRCS:.c=.o) +OBJS := $(SRCS:.c=.o) palette.o all: $(BIN) @@ -14,6 +14,9 @@ $(BIN): $(OBJS) .c.o: $(CC) $(CFLAGS) $< -o $@ +palette.o: ../data/sprites.png + ../tools/pngpal.py $< $@ + clean: rm -f $(BIN) *.o Makefile.deps diff --git a/src/main.c b/src/main.c index 5b315df..dd3cffa 100644 --- a/src/main.c +++ b/src/main.c @@ -7,11 +7,16 @@ #include "vga.h" +/* embedded data */ +extern const uint8_t binary_palette_start[]; + int main(int argc, char *argv[]) { /* set VGA 320x200, 256 col */ set_mode(0x13); + set_palette(binary_palette_start); + uint8_t *screen = open_framebuffer(); if (!screen) { @@ -19,8 +24,11 @@ int main(int argc, char *argv[]) return 1; } - for (int i = 0; i < 320 * 200 * 10; i++) - screen[rand() % (320 * 200)] = rand() % 255; + for (int i = 0; i < 16; i++) + { + memset(screen, i, 320 * 200); + getch(); + } printf("Hello DOS!\n"); diff --git a/src/vga.c b/src/vga.c index 5badd82..b3ee0a2 100644 --- a/src/vga.c +++ b/src/vga.c @@ -2,6 +2,7 @@ #include #include #include +#include uint8_t *open_framebuffer() { @@ -22,3 +23,10 @@ void set_mode(uint8_t mode) regs.x.ax = mode; __dpmi_int(0x10, ®s); } + +void set_palette(const uint8_t *palette) +{ + outportb(0x3c8, 0); + for (int i = 0; i < 768; i++) + outportb(0x3c9, (palette[i] * 63) / 255); +} diff --git a/src/vga.h b/src/vga.h index f35e6dd..a9c46c6 100644 --- a/src/vga.h +++ b/src/vga.h @@ -6,4 +6,7 @@ void close_framebuffer(); void set_mode(uint8_t mode); +/* the palette is expected to be 8 bit per color, and will be converted to VGA's 6 bit per color */ +void set_palette(const uint8_t *palette); + #endif // _VGA_H diff --git a/tools/pngpal.py b/tools/pngpal.py new file mode 100755 index 0000000..7db451d --- /dev/null +++ b/tools/pngpal.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 + +from argparse import ArgumentParser +import subprocess +import os +from PIL import Image + +__version__ = "1.0" + +ld = os.environ.get("LD", "i586-pc-msdosdjgpp-ld") + + +def main(): + parser = ArgumentParser( + description="PNG to palette .o", + epilog="Copyright (C) 2023 Juan J Martinez ", + ) + + parser.add_argument( + "--version", action="version", version="%(prog)s " + __version__ + ) + parser.add_argument("image", help="image to convert") + parser.add_argument("output", help="objtect name for the palette") + + args = parser.parse_args() + + try: + image = Image.open(args.image) + except IOError: + parser.error("failed to open the image") + + if image.mode != "P": + parser.error("not an indexed image (no palette)") + + palette = image.getpalette() + if not palette: + parser.error("failed to extract the palette") + + tmp = args.output.rstrip(".o") + with open(tmp, "wb") as fd: + fd.write(bytearray(palette)) + fd.flush() + rc = subprocess.call( + [ + ld, + "-r", + "-b", + "binary", + "-o", + args.output, + tmp, + ] + ) + os.unlink(tmp) + if rc != 0: + parser.error("Failed to run %s" % ld) + + +if __name__ == "__main__": + main() -- cgit v1.2.3