Browse Source

[sdl] add CHR/PAL/PLD loading support

kvm
asie 6 months ago
parent
commit
8c41e96181
8 changed files with 229 additions and 13 deletions
  1. 93
    0
      src/asset_loader.c
  2. 28
    0
      src/asset_loader.h
  3. 77
    4
      src/frontend_posix.c
  4. 1
    0
      src/frontend_sdl.c
  5. 23
    9
      src/zzt.c
  6. 5
    0
      src/zzt.h
  7. 1
    0
      zeta_sdl.sh
  8. 1
    0
      zeta_sdl_mingw.sh

+ 93
- 0
src/asset_loader.c View File

@@ -0,0 +1,93 @@
/**
* Copyright (c) 2018, 2019 Adrian Siekierka
*
* This file is part of Zeta.
*
* Zeta is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Zeta is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Zeta. If not, see <http://www.gnu.org/licenses/>.
*/

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

static u8 pld_to_pal[] = {
0, 1, 2, 3, 4, 5, 20, 7,
56, 57, 58, 59, 60, 61, 62, 63
};

static int zzt_load_chr(void *data, int dlen) {
u8 *data8 = (u8*) data;

if ((dlen & 0xFF) != 0) return -1;
return zzt_load_charset(8, dlen >> 8, data8);
}

static int zzt_load_pal(void *data, int dlen) {
u32 palette[16];
u8 *data8 = (u8*) data;

if (dlen < 48) return -1;

for (int i = 0; i < 16; i++) {
int pos = i * 3;

palette[i] =
(((u32) (data8[pos] & 0x3F) * 255 / 63) << 16)
| (((u32) (data8[pos + 1] & 0x3F) * 255 / 63) << 8)
| (((u32) (data8[pos + 2] & 0x3F) * 255 / 63));
}

return zzt_load_palette(palette);
}

static int zzt_load_pld(void *data, int dlen) {
u32 palette[16];
u8 *data8 = (u8*) data;

if (dlen < 192) return -1;

for (int i = 0; i < 16; i++) {
int pos = (int)(pld_to_pal[i]) * 3;

palette[i] =
(((u32) (data8[pos] & 0x3F) * 255 / 63) << 16)
| (((u32) (data8[pos + 1] & 0x3F) * 255 / 63) << 8)
| (((u32) (data8[pos + 2] & 0x3F) * 255 / 63));
}

return zzt_load_palette(palette);
}

int zzt_load_asset(char *type, void *data, int dlen) {
char category[17];
char *format = strchr(type, ':') + 1;
if ((format - type) > 16) return -1; // overflow protection
strncpy(category, type, format - type - 1);
category[format - type - 1] = '\0';

if (strcmp(category, "charset") == 0) {
if (strcmp(format, "chr") == 0) {
return zzt_load_chr(data, dlen);
}
} else if (strcmp(category, "palette") == 0) {
if (strcmp(format, "pal") == 0) {
return zzt_load_pal(data, dlen);
} else if (strcmp(format, "pld") == 0) {
return zzt_load_pld(data, dlen);
}
}

return -2;
}

+ 28
- 0
src/asset_loader.h View File

@@ -0,0 +1,28 @@
/**
* Copyright (c) 2018, 2019 Adrian Siekierka
*
* This file is part of Zeta.
*
* Zeta is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Zeta is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Zeta. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __ASSET_LOADER_H__
#define __ASSET_LOADER_H__

#include "zzt.h"

USER_FUNCTION
int zzt_load_asset(char *type, void *data, int dlen);

#endif /* __ASSET_LOADER_H__ */

+ 77
- 4
src/frontend_posix.c View File

@@ -24,25 +24,41 @@ static int posix_vfs_exists(const char *filename) {
}

static void posix_zzt_help(int argc, char **argv) {
fprintf(stderr, "Usage: %s [-e command] [-bt] [world file]\n", argv > 0 ? argv[0] : "zeta");
char *owner = (argv > 0 && argv[0] != NULL && strlen(argv[0]) > 0) ? argv[0] : "zeta";

fprintf(stderr, "Usage: %s [arguments] [world file]\n", owner);
fprintf(stderr, "\n");
fprintf(stderr, "Arguments ([] - parameter; * - may specify multiple times):\n");
fprintf(stderr, " -b disable blinking, enable bright backgrounds\n");
fprintf(stderr, " -e [] execute command - repeat to run multiple commands\n");
fprintf(stderr, " by default, runs either ZZT.EXE or SUPERZ.EXE\n");
fprintf(stderr, " *-e [] execute command - repeat to run multiple commands\n");
fprintf(stderr, " by default, ZZT.EXE or SUPERZ.EXE is executed\n");
fprintf(stderr, " -h show help\n");
fprintf(stderr, " *-l [] load asset - in \"type:format:filename\" form or\n");
fprintf(stderr, " \"filename\" form to attempt a guess\n");
fprintf(stderr, " available types/formats: \n");
fprintf(stderr, " - charset:\n");
fprintf(stderr, " - chr (MegaZeux-like; 8x[height], 256 chars)\n");
fprintf(stderr, " - palette:\n");
fprintf(stderr, " - pal (MegaZeux-like; 16 colors ranged 00-3F)\n");
fprintf(stderr, " - pld (Toshiba UPAL; 64 EGA colors ranged 00-3F)\n");
fprintf(stderr, " -t enable world testing mode (skip K, C, ENTER)\n");
fprintf(stderr, "\n");
fprintf(stderr, "See <https://zeta.asie.pl/> for more information.\n");
}

#define IS_EXTENSION(s, e) (strcasecmp((s + strlen((s)) - strlen((e))), (e)) == 0)

static int posix_zzt_init(int argc, char **argv) {
char arg_name[257];
char *execs[16];
char *loads[16];
int exec_count = 0;
int load_count = 0;
int c;
int skip_kc = 0;

#ifdef USE_GETOPT
while ((c = getopt(argc, argv, "be:ht")) >= 0) {
while ((c = getopt(argc, argv, "be:hl:t")) >= 0) {
switch(c) {
case 'b':
video_blink = 0;
@@ -54,6 +70,13 @@ static int posix_zzt_init(int argc, char **argv) {
}
execs[exec_count++] = optarg;
break;
case 'l':
if (load_count > 16) {
fprintf(stderr, "Too many -l commands!\n");
return -1;
}
loads[load_count++] = optarg;
break;
case 'h':
posix_zzt_help(argc, argv);
exit(0);
@@ -95,6 +118,56 @@ static int posix_zzt_init(int argc, char **argv) {
}
}

for (int i = 0; i < load_count; i++) {
char *type, *filename;

if (strrchr(loads[i], ':') == NULL) {
filename = loads[i];
if (IS_EXTENSION(filename, ".chr")) type = "charset:chr";
else if (IS_EXTENSION(filename, ".pal")) type = "palette:pal";
else if (IS_EXTENSION(filename, ".pld")) type = "palette:pld";
else {
fprintf(stderr, "Could not guess type of %s!\n", filename);
continue;
}
} else {
type = loads[i];
filename = strrchr(type, ':') + 1;
type[filename - type - 1] = '\0';
}

FILE *file = fopen(filename, "rb");
if (!file) {
fprintf(stderr, "Could not open %s!\n", filename);
continue;
}

fseek(file, 0, SEEK_END);
long fsize = ftell(file);
fseek(file, 0, SEEK_SET);

if (fsize <= 0 || fsize >= 1048576) {
fprintf(stderr, "Could not read %s!\n", filename);
fclose(file);
continue;
}

u8 *buffer = (u8*) malloc((int) fsize);
if (!fread(buffer, (int) fsize, 1, file)) {
fprintf(stderr, "Could not read %s!\n", filename);
fclose(file);
continue;
}

fclose(file);

if (zzt_load_asset(type, buffer, (int) fsize) < 0) {
fprintf(stderr, "Could not load %s!\n", filename);
}

free(buffer);
}

if (exec_count > 0) {
int exeh = 0;
for (int i = 0; i < exec_count; i++) {

+ 1
- 0
src/frontend_sdl.c View File

@@ -587,6 +587,7 @@ void zeta_update_palette(u32* data) {
SDL_UnlockMutex(render_data_update_mutex);
}

#include "asset_loader.h"
#include "frontend_posix.c"

int main(int argc, char **argv) {

+ 23
- 9
src/zzt.c View File

@@ -956,6 +956,26 @@ void zzt_load_binary(int handle, const char *arg) {
fprintf(stderr, "wrote %d bytes to %d\n", bytes_read, (offset_pars * 16 + 256));
}

int zzt_load_charset(int width, int height, u8 *data) {
if (width != 8 || height <= 0 || height > 16) return -1;

for (int i = 0; i < 256*height; i++) {
zzt.charset[i] = data[i];
}

zeta_update_charset(8, 14, zzt.charset);
return 0;
}

int zzt_load_palette(u32 *colors) {
for (int i = 0; i < 16; i++) {
zzt.palette[i] = colors[i];
}

zeta_update_palette(zzt.palette);
return 0;
}

void zzt_init(void) {
/* for (int i = 0; i < MAX_ALLOC; i++)
seg_allocs[i] = (i < 256) ? (256-i) : 0; */
@@ -1000,16 +1020,10 @@ void zzt_init(void) {
zzt.cpu.func_port_out = cpu_func_port_out_main;
zzt.cpu.func_interrupt = cpu_func_interrupt_main;

for (int i = 0; i < 256*14; i++) {
zzt.charset[i] = res_8x14_bin[i];
}

for (int i = 0; i < 16; i++) {
zzt.palette[i] = def_palette[i];
}
// default assets

zeta_update_charset(8, 14, zzt.charset);
zeta_update_palette(zzt.palette);
zzt_load_charset(8, 14, res_8x14_bin);
zzt_load_palette(def_palette);
}

int zzt_execute(int opcodes) {

+ 5
- 0
src/zzt.h View File

@@ -69,6 +69,11 @@ void zzt_key_set_delay(int ms, int repeat_ms);
USER_FUNCTION
void zzt_set_timer_offset(long ms);

USER_FUNCTION
int zzt_load_charset(int width, int height, u8* data);
USER_FUNCTION
int zzt_load_palette(u32* colors);

IMPLEMENT_FUNCTION
long zeta_time_ms(void);


+ 1
- 0
zeta_sdl.sh View File

@@ -4,4 +4,5 @@ gcc -o build/zeta86 -g -O2 -std=c18 -Wall \
res/8x14.c src/posix_vfs.c src/audio_stream.c \
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

+ 1
- 0
zeta_sdl_mingw.sh View File

@@ -5,4 +5,5 @@ i686-w64-mingw32-windres mingw/resources.rc build/mingw_resources.o
i686-w64-mingw32-gcc -o build/zeta86.exe -g -O2 -std=c18 -Wall -mwindows \
res/8x14.c src/posix_vfs.c src/audio_stream.c src/frontend_sdl.c src/zzt.c src/cpu.c \
src/screenshot_writer.c src/render_software.c build/mingw_resources.o \
src/asset_loader.c \
-lmingw32 -lSDL2main -lSDL2 -lopengl32

Loading…
Cancel
Save