Переглянути джерело

[sdl] libpng screenshot support

master
asie 1 місяць тому
джерело
коміт
f29db691b9
6 змінених файлів з 51 додано та 8 видалено
  1. 2
    1
      Makefile
  2. 1
    0
      src/config.h
  3. 7
    1
      src/frontend_sdl.c
  4. 38
    5
      src/screenshot_writer.c
  5. 2
    0
      src/screenshot_writer.h
  6. 1
    1
      zeta_sdl.sh

+ 2
- 1
Makefile Переглянути файл

@@ -6,13 +6,14 @@ BUILDDIR = build
CC = gcc
CFLAGS = -g -O2 -flto -std=c11 -Wall
LDFLAGS = -g -O2
LIBS = -lGL -lSDL2 -lSDL2main
LIBS = -lGL -lSDL2 -lSDL2main -lpng
TARGET = $(BUILDDIR)/zeta86

OBJS = $(OBJDIR)/8x14.o \
\
$(OBJDIR)/cpu.o \
$(OBJDIR)/zzt.o \
$(OBJDIR)/asset_loader.o \
$(OBJDIR)/audio_stream.o \
\
$(OBJDIR)/posix_vfs.o \

+ 1
- 0
src/config.h Переглянути файл

@@ -31,6 +31,7 @@
#define BLINK_TOGGLE_DURATION_MS 267
#define ENABLE_SCREENSHOTS
#define USE_GETOPT
#define USE_LIBPNG
#define USE_OPENGL
// #define USE_OPENGL_ES /* GLES 1.1, not production ready */


+ 7
- 1
src/frontend_sdl.c Переглянути файл

@@ -760,7 +760,13 @@ int main(int argc, char **argv) {
else if (sdl_is_blink_phase(zeta_time_ms())) sflags |= RENDER_BLINK_PHASE;

while ((++i) <= 9999) {
#ifdef USE_LIBPNG
int stype = SCREENSHOT_TYPE_PNG;
snprintf(filename, 23, "screen%d.png", i);
#else
int stype = SCREENSHOT_TYPE_BMP;
snprintf(filename, 23, "screen%d.bmp", i);
#endif
file = fopen(filename, "rb");
if (!file) {
file = fopen(filename, "wb");
@@ -769,7 +775,7 @@ int main(int argc, char **argv) {
break;
}
if (write_screenshot(
file, SCREENSHOT_TYPE_BMP,
file, stype,
swidth, sflags,
zzt_get_ram(), charset_update_data,
8, charset_char_height,

+ 38
- 5
src/screenshot_writer.c Переглянути файл

@@ -19,6 +19,7 @@

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "screenshot_writer.h"

#define POS_MUL (scr_width <= 40 ? 2 : 1)
@@ -35,6 +36,30 @@ static void fput32le(FILE *output, int i) {
fputc(((i >> 24) & 0xFF), output);
}

#ifdef USE_LIBPNG
#include <png.h>

static int write_screenshot_png(FILE *output, u8 *buffer, u32 *palette, int flags, int width, int height) {
png_image image;
u8 colormap[48];

memset(&image, 0, sizeof(image));
image.version = PNG_IMAGE_VERSION;
image.width = width;
image.height = height;
image.format = PNG_FORMAT_RGB_COLORMAP;
image.colormap_entries = 16;

for (int i = 0; i < 16; i++) {
colormap[i*3 + 0] = palette[i] >> 16;
colormap[i*3 + 1] = palette[i] >> 8;
colormap[i*3 + 2] = palette[i] >> 0;
}

return png_image_write_to_stdio(&image, output, 0, buffer, 0, colormap) > 0;
}
#endif

static int write_screenshot_bmp(FILE *output, u8 *buffer, u32 *palette, int flags, int width, int height) {
int bmp_size = 54 + (4*16) + (width * height / 2);

@@ -82,7 +107,7 @@ static int write_screenshot_bmp(FILE *output, u8 *buffer, u32 *palette, int flag
int write_screenshot(FILE *output, int type, int scr_width, int flags, u8 *ram, u8 *charset, int char_width, int char_height, u32 *palette) {
void *buffer;

int paletted = (type == SCREENSHOT_TYPE_BMP);
int paletted = (type == SCREENSHOT_TYPE_BMP || type == SCREENSHOT_TYPE_PNG);

buffer = malloc(char_width * char_height * scr_width * POS_MUL * 25 * (paletted ? sizeof(u8) : sizeof(u32)));
if (buffer == NULL) {
@@ -97,10 +122,18 @@ int write_screenshot(FILE *output, int type, int scr_width, int flags, u8 *ram,

int result;

if (type == SCREENSHOT_TYPE_BMP) {
result = write_screenshot_bmp(output, (u8*) buffer, palette, flags, char_width * scr_width * POS_MUL, char_height * 25);
} else {
result = -1;
switch (type) {
case SCREENSHOT_TYPE_BMP:
result = write_screenshot_bmp(output, (u8*) buffer, palette, flags, char_width * scr_width * POS_MUL, char_height * 25);
break;
#ifdef USE_LIBPNG
case SCREENSHOT_TYPE_PNG:
result = write_screenshot_png(output, (u8*) buffer, palette, flags, char_width * scr_width * POS_MUL, char_height * 25);
break;
#endif
default:
result = -1;
break;
}

free(buffer);

+ 2
- 0
src/screenshot_writer.h Переглянути файл

@@ -21,9 +21,11 @@
#define __SCREENSHOT_RENDER_H__

#include "types.h"
#include "config.h"
#include "render_software.h"

#define SCREENSHOT_TYPE_BMP 0
#define SCREENSHOT_TYPE_PNG 1

USER_FUNCTION
int write_screenshot(FILE *output, int type, int scr_width, int flags, u8 *ram, u8 *charset, int char_width, int char_height, u32 *palette);

+ 1
- 1
zeta_sdl.sh Переглянути файл

@@ -5,4 +5,4 @@ gcc -o build/zeta86 -g -O2 -std=c18 -Wall \
src/frontend_sdl.c src/zzt.c src/cpu.c \
src/screenshot_writer.c src/render_software.c \
src/asset_loader.c \
-lGL -lSDL2 -lSDL2main
-lGL -lSDL2 -lSDL2main -lpng

Завантаження…
Відмінити
Зберегти