diff options
Diffstat (limited to 'test.c')
| -rw-r--r-- | test.c | 96 | 
1 files changed, 96 insertions, 0 deletions
@@ -0,0 +1,96 @@ +#include <stdint.h> +#include <stdbool.h> +#include <unistd.h> +#include <stdlib.h> +#include <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <string.h> +#include "lodepng.h" +#include "gb.h" + +#define NUM_FRAMES 600 + +static uint32_t +file_size( +  const char * const path +) { +  struct stat st; + +  if (stat(path, &st)) { +    perror("stat():"); +    exit(EXIT_FAILURE); +  } + +  return st.st_size; +} + +static const uint8_t * +load( +  const char * const path, +  uint32_t * const r_file_size +) { +  const uint32_t rom_size = file_size(path); +  uint8_t *rom_data = malloc(rom_size); +  if (!rom_data) { +    perror("malloc():"); +    exit(EXIT_FAILURE); +  } + +  FILE *fh = fopen(path, "rb"); +  if (!fh) { +    perror("fopen():"); +    exit(EXIT_FAILURE); +  } + +  if (!fread(rom_data, rom_size, 1, fh)) { +    perror("fread():"); +    exit(EXIT_FAILURE); +  } + +  fclose(fh); + +  if (r_file_size) { +    *r_file_size = (uint32_t) rom_size; +  } +   +  return rom_data; +} + +// buffer for rendered frames +static uint8_t frames[NUM_FRAMES * GB_FRAME_SIZE]; + +int main(int argc, char *argv[]) { +  for (int i = 1; i < argc; i++) { +    uint32_t rom_size = 0; +    const uint8_t *rom_data = load(argv[i], &rom_size); +    fprintf(stderr, "loaded \"%s\" (%u bytes)\n", argv[i], rom_size); + +    gb_t ctx; +    gb_init(&ctx, rom_data, rom_size); +    fprintf(stderr, "gb context initialized\n"); + +    // render several frames +    for (size_t j = 0; j < NUM_FRAMES; j++) { +      // render frame +      gb_frame(&ctx); + +      // copy frame to buffer +      memcpy(frames + GB_FRAME_SIZE * j, gb_get_rgb_frame(&ctx), GB_FRAME_SIZE); +    } + +    // save png +    if (lodepng_encode24_file("out.png", frames, 160, 144 * NUM_FRAMES)) { +      fprintf(stderr, "lode_png_encode24_file() failed\n"); +      return EXIT_FAILURE; +    } + +    fprintf(stderr, "%d frames rendered\n", NUM_FRAMES); + +    // free rom data +    free((void*) rom_data); +  } + +  // return success +  return 0; +}  | 
