amiga_imgv

diff src/main.c @ 0:a4788c959918

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 25 Oct 2017 19:34:53 +0300
parents
children 663471a80c21
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main.c	Wed Oct 25 19:34:53 2017 +0300
     1.3 @@ -0,0 +1,106 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <string.h>
     1.7 +#include <assert.h>
     1.8 +#include "gfx.h"
     1.9 +#include "image.h"
    1.10 +#include "logger.h"
    1.11 +
    1.12 +#include "hwregs.h"
    1.13 +#include "copper.h"
    1.14 +
    1.15 +
    1.16 +static int proc_event(union gfx_event *ev);
    1.17 +static void show_ham_image(struct ham_image *img);
    1.18 +
    1.19 +static int win_width, win_height;
    1.20 +
    1.21 +int main(int argc, char **argv)
    1.22 +{
    1.23 +	struct ham_image *img;
    1.24 +
    1.25 +	if(argv[1]) {
    1.26 +		if(!(img = load_ham_image(argv[1]))) {
    1.27 +			fprintf(stderr, "failed to load image: %s\n", argv[1]);
    1.28 +			return 1;
    1.29 +		}
    1.30 +	} else {
    1.31 +		printf("generating test image ...\n");
    1.32 +		if(!(img = gen_ham_image(320, 256, 6))) {
    1.33 +			fprintf(stderr, "failed to generate image\n");
    1.34 +			return 1;
    1.35 +		}
    1.36 +	}
    1.37 +
    1.38 +	if(gfx_init(6, GFX_HAM) == -1) {
    1.39 +		return 1;
    1.40 +	}
    1.41 +	gfx_wait_vblank();
    1.42 +	show_ham_image(img);
    1.43 +
    1.44 +	for(;;) {
    1.45 +		union gfx_event ev;
    1.46 +		if(gfx_next_event(&ev, GFX_EVLOOP_BLOCK)) {
    1.47 +			if(proc_event(&ev) == -1) {
    1.48 +				break;
    1.49 +			}
    1.50 +		}
    1.51 +	}
    1.52 +
    1.53 +	gfx_shutdown();
    1.54 +	return 0;
    1.55 +}
    1.56 +
    1.57 +static int proc_event(union gfx_event *ev)
    1.58 +{
    1.59 +	switch(ev->type) {
    1.60 +	case GFX_EV_QUIT:
    1.61 +		return -1;
    1.62 +
    1.63 +	case GFX_EV_KEY:
    1.64 +		switch(ev->key.key) {
    1.65 +		case 27:
    1.66 +			return -1;
    1.67 +		default:
    1.68 +			break;
    1.69 +		}
    1.70 +		break;
    1.71 +
    1.72 +	default:
    1.73 +		break;
    1.74 +	}
    1.75 +	return 0;
    1.76 +}
    1.77 +
    1.78 +#define NUM_BPL		6
    1.79 +static void show_ham_image(struct ham_image *img)
    1.80 +{
    1.81 +	int i, j, k, fbwidth, fbheight, ncolors, prev_line;
    1.82 +	unsigned char *fbptr = gfx_get_framebuffer();
    1.83 +	struct palchange *chg = img->chglist;
    1.84 +
    1.85 +	fbwidth = gfx_framebuffer_width();
    1.86 +	fbheight = gfx_framebuffer_height();
    1.87 +
    1.88 +	memcpy(fbptr, img->pixels, fbwidth * fbheight / 8 * NUM_BPL);
    1.89 +
    1.90 +	/* create copper list that handles the palette */
    1.91 +	clear_copper();
    1.92 +	gfx_begin_copperlist();
    1.93 +	/* initial palette at the start of frame */
    1.94 +	for(i=0; i<16; i++) {
    1.95 +		add_copper(COPPER_MOVE(REGN_COLOR(i), img->palette[i]));
    1.96 +		logmsg("copper palette[%d]: %x\n", i, (unsigned int)img->palette[i]);
    1.97 +	}
    1.98 +	/* add copper instructions for palette changes according to the image changelist */
    1.99 +	prev_line = -1;
   1.100 +	while(chg) {
   1.101 +		assert(chg->line >= prev_line);
   1.102 +		if(chg->line != prev_line) {
   1.103 +			prev_line = chg->line;
   1.104 +			add_copper(COPPER_VWAIT(chg->line));
   1.105 +		}
   1.106 +		add_copper(COPPER_MOVE(REGN_COLOR(chg->entry >> 12), chg->entry & 0xfff));
   1.107 +	}
   1.108 +	add_copper(COPPER_END);
   1.109 +}