# HG changeset patch # User John Tsiombikas # Date 1509022196 -10800 # Node ID 663471a80c21e2e35840bcc87a43c00b2166934b # Parent f75893a3323400c7279dfa496bcde16dd72f68ab broken + sdl emu diff -r f75893a33234 -r 663471a80c21 Makefile --- a/Makefile Thu Oct 26 10:04:29 2017 +0300 +++ b/Makefile Thu Oct 26 15:49:56 2017 +0300 @@ -1,10 +1,18 @@ -src = $(wildcard src/*.c) $(wildcard src/amiga/*.c) +src = $(wildcard src/*.c) obj = $(src:.c=.o) bin = imgv CC = vc -CFLAGS = -Isrc -Isrc/amiga -LDFLAGS = -lamiga + +ifeq ($(CC), vc) + CFLAGS = -Isrc -Isrc/amiga + LDFLAGS = -lamiga + src += $(wildcard src/amiga/*.c) +else + CFLAGS = -pedantic -Wall -g -Isrc -Isrc/sdl `sdl-config --cflags` + LDFLAGS = `sdl-config --libs` + src += $(wildcard src/sdl/*.c) +endif $(bin): $(obj) $(CC) -o $@ $(obj) $(LDFLAGS) diff -r f75893a33234 -r 663471a80c21 src/amiga/gfx.c --- a/src/amiga/gfx.c Thu Oct 26 10:04:29 2017 +0300 +++ b/src/amiga/gfx.c Thu Oct 26 15:49:56 2017 +0300 @@ -57,6 +57,7 @@ gfx_begin_copperlist(); add_copper(COPPER_END); + logmsg("starting DMA\n"); REG_DMACON = SETBITS(DMA_BPL | DMA_COPPER | DMA_MASTER); return 0; } @@ -201,3 +202,7 @@ { gfx_wait_vpos(300); } + +void gfx_show_image(struct ham_image *img) +{ +} diff -r f75893a33234 -r 663471a80c21 src/gfx.h --- a/src/gfx.h Thu Oct 26 10:04:29 2017 +0300 +++ b/src/gfx.h Thu Oct 26 15:49:56 2017 +0300 @@ -47,6 +47,8 @@ int width, height; }; +struct ham_image; + int gfx_init(int nbpl, unsigned int flags); void gfx_shutdown(void); @@ -66,4 +68,6 @@ void gfx_wait_vpos(int x); void gfx_wait_vblank(void); +void gfx_show_image(struct ham_image *img); + #endif /* GFX_H_ */ diff -r f75893a33234 -r 663471a80c21 src/image.c --- a/src/image.c Thu Oct 26 10:04:29 2017 +0300 +++ b/src/image.c Thu Oct 26 15:49:56 2017 +0300 @@ -1,10 +1,122 @@ +#include #include #include +#include #include "image.h" +#include "logger.h" + +#ifdef __unix__ +#include +#define HAVE_NTOHS +#else +static int bigendian(void); +static uint16_t swap16(uint16_t x); +static uint16_t nop16(uint16_t x); +static uint16_t (*ntohs)(uint16_t); +#endif + +struct ham_image_header { + char magic[4]; + uint16_t width, height; + unsigned char nbitplanes, padding; + uint16_t nchg; + uint16_t palette[16]; +}; struct ham_image *load_ham_image(const char *fname) { - return 0; /* TODO */ + int i, num; + unsigned long imgsz; + struct ham_image_header hdr; + struct ham_image *img = 0; + struct palchange *tail = 0; + FILE *fp; + +#ifndef HAVE_NTOHS + ntohs = bigendian() ? nop16 : swap16; +#endif + + logmsg("opening file: %s\n", fname); + if(!(fp = fopen(fname, "rb"))) { + logmsg("failed to open %s: %s\n", fname, strerror(errno)); + return 0; + } + + if(fread(&hdr, sizeof hdr, 1, fp) < 1) { + logmsg("unexpected eof while reading image header\n"); + goto err; + } + if(memcmp(hdr.magic, "HAAM", 4) != 0) { + logmsg("failed to load image: %s: unknown file type\n", fname); + goto err; + } + + if(!(img = malloc(sizeof *img))) { + logmsg("failed to allocate image structure\n"); + goto err; + } + img->width = ntohs(hdr.width); + img->height = ntohs(hdr.height); + img->nbitplanes = hdr.nbitplanes; + + imgsz = img->width * img->height / 8 * img->nbitplanes; + + logmsg("header info: %dx%d, %d bpl, size: %lu\n", img->width, img->height, (int)img->nbitplanes, imgsz); + + if(!(img->pixels = malloc(imgsz))) { + logmsg("failed to allocate %dx%d (%d bitplane) image\n", img->width, img->height, img->nbitplanes); + goto err; + } + + for(i=0; i<16; i++) { + img->palette[i] = ntohs(hdr.palette[i]); + } + + num = ntohs(hdr.nchg); + for(i=0; iline, 2, 1, fp) < 1 || fread(&chg->entry, 2, 1, fp) < 1) { + logmsg("unexpected end of file while reading palette changelist\n"); + goto err; + } + chg->line = ntohs(chg->line); + chg->entry = ntohs(chg->entry); + chg->next = 0; + + if(tail) { + tail->next = chg; + tail = chg; + } else { + img->chglist = tail = chg; + } + } + + if(fread(img->pixels, 1, imgsz, fp) < imgsz) { + logmsg("unexpected end of file while reading image pixels\n"); + goto err; + } + + fclose(fp); + return img; + +err: + if(img) { + while(img->chglist) { + void *tmp = img->chglist; + img->chglist = img->chglist->next; + free(tmp); + } + free(img->pixels); + free(img); + } + if(fp) fclose(fp); + return 0; } struct ham_image *gen_ham_image(int w, int h, int nbpl) @@ -53,3 +165,21 @@ return img; } + +#ifndef HAVE_NTOHS +static int bigendian(void) +{ + static const uint16_t x = 0xaabb; + return *(unsigned char*)&x == 0xaa; +} + +static uint16_t swap16(uint16_t x) +{ + return (x << 8) | (x >> 8); +} + +static uint16_t nop16(uint16_t x) +{ + return x; +} +#endif diff -r f75893a33234 -r 663471a80c21 src/main.c --- a/src/main.c Thu Oct 26 10:04:29 2017 +0300 +++ b/src/main.c Thu Oct 26 15:49:56 2017 +0300 @@ -6,14 +6,8 @@ #include "image.h" #include "logger.h" -#include "hwregs.h" -#include "copper.h" - static int proc_event(union gfx_event *ev); -static void show_ham_image(struct ham_image *img); - -static int win_width, win_height; int main(int argc, char **argv) { @@ -36,7 +30,7 @@ return 1; } gfx_wait_vblank(); - show_ham_image(img); + gfx_show_image(img); for(;;) { union gfx_event ev; @@ -71,36 +65,3 @@ } return 0; } - -#define NUM_BPL 6 -static void show_ham_image(struct ham_image *img) -{ - int i, j, k, fbwidth, fbheight, ncolors, prev_line; - unsigned char *fbptr = gfx_get_framebuffer(); - struct palchange *chg = img->chglist; - - fbwidth = gfx_framebuffer_width(); - fbheight = gfx_framebuffer_height(); - - memcpy(fbptr, img->pixels, fbwidth * fbheight / 8 * NUM_BPL); - - /* create copper list that handles the palette */ - clear_copper(); - gfx_begin_copperlist(); - /* initial palette at the start of frame */ - for(i=0; i<16; i++) { - add_copper(COPPER_MOVE(REGN_COLOR(i), img->palette[i])); - logmsg("copper palette[%d]: %x\n", i, (unsigned int)img->palette[i]); - } - /* add copper instructions for palette changes according to the image changelist */ - prev_line = -1; - while(chg) { - assert(chg->line >= prev_line); - if(chg->line != prev_line) { - prev_line = chg->line; - add_copper(COPPER_VWAIT(chg->line)); - } - add_copper(COPPER_MOVE(REGN_COLOR(chg->entry >> 12), chg->entry & 0xfff)); - } - add_copper(COPPER_END); -} diff -r f75893a33234 -r 663471a80c21 src/sdl/gfx.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/gfx.c Thu Oct 26 15:49:56 2017 +0300 @@ -0,0 +1,160 @@ +#include +#include +#include "gfx.h" +#include "image.h" + +static SDL_Surface *fbsurf; +static int scr_width, scr_height; +static int fb_width, fb_height; +static int num_bitplanes; + + +int gfx_init(int nbpl, unsigned int flags) +{ + num_bitplanes = nbpl; + scr_width = fb_width = (flags & GFX_HIRES) ? 640 : 320; + scr_height = fb_height = (flags & GFX_ILACE) ? 512 : 256; + + if(SDL_Init(SDL_INIT_VIDEO) == -1) { + fprintf(stderr, "failed to initialize SDL\n"); + return -1; + } + if(!(fbsurf = SDL_SetVideoMode(scr_width, scr_height, 32, SDL_SWSURFACE))) { + fprintf(stderr, "failed to set video mode %dx%d\n", scr_width, scr_height); + SDL_Quit(); + return -1; + } + + return 0; +} + +void gfx_shutdown(void) +{ + SDL_Quit(); +} + + +int gfx_screen_width(void) +{ + return scr_width; +} + +int gfx_screen_height(void) +{ + return scr_height; +} + + +void *gfx_set_framebuffer(void *fb, int width, int height) +{ + return 0; +} + +void *gfx_get_framebuffer(void) +{ + return 0; +} + + +int get_framebuffer_width(void) +{ + return fb_width; +} + +int get_framebuffer_height(void) +{ + return fb_height; +} + + +void gfx_begin_copperlist(void) +{ +} + + +int gfx_next_event(union gfx_event *ev, int block) +{ + SDL_Event sdlev; + + if(block) { + SDL_WaitEvent(&sdlev); + } else { + if(!SDL_PollEvent(&sdlev)) { + return 0; + } + } + + switch(sdlev.type) { + case SDL_QUIT: + ev->type = GFX_EV_QUIT; + return 1; + + case SDL_KEYDOWN: + case SDL_KEYUP: + ev->type = GFX_EV_KEY; + ev->key.key = sdlev.key.keysym.sym; + ev->key.pressed = sdlev.key.state == SDL_PRESSED; + return 1; + + default: + break; + } + return 0; +} + + +void gfx_wait_vpos(int x) +{ +} + +void gfx_wait_vblank(void) +{ +} + +#define ARED(x) ((((x) & 0xf00) >> 4) | (((x) & 0xf00) >> 8)) +#define AGREEN(x) (((x) & 0xf0) | (((x) & 0xf0) >> 4)) +#define ABLUE(x) ((((x) & 0xf) << 4) | ((x) & 0xf)) + +void gfx_show_image(struct ham_image *img) +{ + int i, j, k; + uint32_t palette[16]; + uint32_t *dest; + unsigned char *src; + + for(i=0; i<16; i++) { + uint16_t pcol = img->palette[i]; + int red = ARED(pcol); + int green = AGREEN(pcol); + int blue = ABLUE(pcol); + palette[i] = (red << fbsurf->format->Rshift) | (green << fbsurf->format->Gshift) | + (blue << fbsurf->format->Bshift); + } + + if(SDL_MUSTLOCK(fbsurf)) { + SDL_LockSurface(fbsurf); + } + + dest = fbsurf->pixels; + src = img->pixels; + for(i=0; iheight; i++) { + for(j=0; jwidth; j++) { + unsigned char idx = 0; + int bit = j & 7; + for(k=0; knbitplanes; k++) { + idx = (idx << 1) | ((*(src + k * img->width / 8) >> bit) & 1); + } + *dest++ = palette[idx]; + if(bit == 7) { + ++src; + } + } + src += img->width / 8 * (img->nbitplanes - 1); + } + + if(SDL_MUSTLOCK(fbsurf)) { + SDL_UnlockSurface(fbsurf); + } + + SDL_Flip(fbsurf); +} diff -r f75893a33234 -r 663471a80c21 tools/convhammer/main.c --- a/tools/convhammer/main.c Thu Oct 26 10:04:29 2017 +0300 +++ b/tools/convhammer/main.c Thu Oct 26 15:49:56 2017 +0300 @@ -167,6 +167,8 @@ } } + free(inpixels); + fclose(fp); return img; err: @@ -215,6 +217,11 @@ num = htons(num); fwrite(&num, 2, 1, fp); + for(i=0; i<16; i++) { + val = htons(img->palette[i]); + fwrite(&val, 2, 1, fp); + } + chg = img->chglist; while(chg) { val = htons(chg->line);