amiga_imgv

annotate 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
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@0 3 #include <string.h>
nuclear@0 4 #include <assert.h>
nuclear@0 5 #include "gfx.h"
nuclear@0 6 #include "image.h"
nuclear@0 7 #include "logger.h"
nuclear@0 8
nuclear@0 9 #include "hwregs.h"
nuclear@0 10 #include "copper.h"
nuclear@0 11
nuclear@0 12
nuclear@0 13 static int proc_event(union gfx_event *ev);
nuclear@0 14 static void show_ham_image(struct ham_image *img);
nuclear@0 15
nuclear@0 16 static int win_width, win_height;
nuclear@0 17
nuclear@0 18 int main(int argc, char **argv)
nuclear@0 19 {
nuclear@0 20 struct ham_image *img;
nuclear@0 21
nuclear@0 22 if(argv[1]) {
nuclear@0 23 if(!(img = load_ham_image(argv[1]))) {
nuclear@0 24 fprintf(stderr, "failed to load image: %s\n", argv[1]);
nuclear@0 25 return 1;
nuclear@0 26 }
nuclear@0 27 } else {
nuclear@0 28 printf("generating test image ...\n");
nuclear@0 29 if(!(img = gen_ham_image(320, 256, 6))) {
nuclear@0 30 fprintf(stderr, "failed to generate image\n");
nuclear@0 31 return 1;
nuclear@0 32 }
nuclear@0 33 }
nuclear@0 34
nuclear@0 35 if(gfx_init(6, GFX_HAM) == -1) {
nuclear@0 36 return 1;
nuclear@0 37 }
nuclear@0 38 gfx_wait_vblank();
nuclear@0 39 show_ham_image(img);
nuclear@0 40
nuclear@0 41 for(;;) {
nuclear@0 42 union gfx_event ev;
nuclear@0 43 if(gfx_next_event(&ev, GFX_EVLOOP_BLOCK)) {
nuclear@0 44 if(proc_event(&ev) == -1) {
nuclear@0 45 break;
nuclear@0 46 }
nuclear@0 47 }
nuclear@0 48 }
nuclear@0 49
nuclear@0 50 gfx_shutdown();
nuclear@0 51 return 0;
nuclear@0 52 }
nuclear@0 53
nuclear@0 54 static int proc_event(union gfx_event *ev)
nuclear@0 55 {
nuclear@0 56 switch(ev->type) {
nuclear@0 57 case GFX_EV_QUIT:
nuclear@0 58 return -1;
nuclear@0 59
nuclear@0 60 case GFX_EV_KEY:
nuclear@0 61 switch(ev->key.key) {
nuclear@0 62 case 27:
nuclear@0 63 return -1;
nuclear@0 64 default:
nuclear@0 65 break;
nuclear@0 66 }
nuclear@0 67 break;
nuclear@0 68
nuclear@0 69 default:
nuclear@0 70 break;
nuclear@0 71 }
nuclear@0 72 return 0;
nuclear@0 73 }
nuclear@0 74
nuclear@0 75 #define NUM_BPL 6
nuclear@0 76 static void show_ham_image(struct ham_image *img)
nuclear@0 77 {
nuclear@0 78 int i, j, k, fbwidth, fbheight, ncolors, prev_line;
nuclear@0 79 unsigned char *fbptr = gfx_get_framebuffer();
nuclear@0 80 struct palchange *chg = img->chglist;
nuclear@0 81
nuclear@0 82 fbwidth = gfx_framebuffer_width();
nuclear@0 83 fbheight = gfx_framebuffer_height();
nuclear@0 84
nuclear@0 85 memcpy(fbptr, img->pixels, fbwidth * fbheight / 8 * NUM_BPL);
nuclear@0 86
nuclear@0 87 /* create copper list that handles the palette */
nuclear@0 88 clear_copper();
nuclear@0 89 gfx_begin_copperlist();
nuclear@0 90 /* initial palette at the start of frame */
nuclear@0 91 for(i=0; i<16; i++) {
nuclear@0 92 add_copper(COPPER_MOVE(REGN_COLOR(i), img->palette[i]));
nuclear@0 93 logmsg("copper palette[%d]: %x\n", i, (unsigned int)img->palette[i]);
nuclear@0 94 }
nuclear@0 95 /* add copper instructions for palette changes according to the image changelist */
nuclear@0 96 prev_line = -1;
nuclear@0 97 while(chg) {
nuclear@0 98 assert(chg->line >= prev_line);
nuclear@0 99 if(chg->line != prev_line) {
nuclear@0 100 prev_line = chg->line;
nuclear@0 101 add_copper(COPPER_VWAIT(chg->line));
nuclear@0 102 }
nuclear@0 103 add_copper(COPPER_MOVE(REGN_COLOR(chg->entry >> 12), chg->entry & 0xfff));
nuclear@0 104 }
nuclear@0 105 add_copper(COPPER_END);
nuclear@0 106 }