the final executable, as objcopy was problematic with some OS linkers.
-# $Id: GNUmakefile,v 1.3 2014/06/21 17:16:32 mmondor Exp $
+# $Id: GNUmakefile,v 1.4 2014/06/25 07:47:28 mmondor Exp $
CC := cc
RM := rm
UNAME := uname
-TOUCH := touch
-OBJDUMP := objdump
-OBJCOPY := objcopy
-GREP := grep
-AWK := awk
DATE := date
STRIP := strip
$(addprefix ../common/,sha1.o rmd160.o hmac_sha1.o hmac_rmd160.o \
mmenc.o)
BIN := tms-client
-RAWOBJS := $(addsuffix .enc.o,$(addprefix bmp/,RomDD.bmp FedCA.bmp) \
+RAWFILES := \
+ $(addprefix bmp/,RomDD.bmp FedCA.bmp) \
$(addprefix wav/,nt_cloaked.wav nt_uncloak.wav nt_shield_up.wav \
nt_shield_down.wav nt_fire_torp_other.wav nt_plasma_hit.wav \
nt_explosion_other.wav) \
- $(addprefix fnt/,7x13.fnt))
+ $(addprefix fnt/,7x13.fnt)
+
+RAWFILES_ENC := $(addsuffix .enc, $(RAWFILES))
+RAWFILES_C := $(addsuffix .c, $(RAWFILES_ENC))
+RAWFILES_H := $(addsuffix .h, $(RAWFILES_ENC))
+RAWFILES_O := $(addsuffix .o, $(RAWFILES_ENC))
SDL_CFLAGS := $(shell sdl-config --cflags)
SDL_LDFLAGS := $(shell sdl-config --libs)
# GL_LDFLAGS := -lGL -lGLU
endif
-# Determine target of compiled objects so that we may convert binaries
-# to compatible objects using objcopy and then link them like other modules.
-OBJTARGET := $(shell $(TOUCH) $(TMPDIR)/obj.c && \
- $(CC) $(CFLAGS) -c -o $(TMPDIR)/obj.o $(TMPDIR)/obj.c && \
- $(OBJDUMP) -t $(TMPDIR)/obj.o | \
- $(GREP) 'file format' | $(AWK) '{print $$4}' \
- && $(RM) $(TMPDIR)/obj.o $(TMPDIR)/obj.c)
-#OBJARCH := $(shell echo $(OBJTARGET) | $(AWK) -F '-' '{print $$2}')
-OBJARCH := i386
-SEED := $(shell date +%s)
+SEED := $(shell $(DATE) +%s)
# Architecture independent settings follow
CFLAGS += -DBIG_ENDIAN=4321 -DLITTLE_ENDIAN=1234
all: $(BIN)
-%.o: %.c
+%.o: %.c $(RAWFILES_O)
$(CC) -c $(CFLAGS) -I. -o $@ $<
encode:
$(CC) -o src/encode src/encode.c
-$(RAWOBJS): encode
+filetoc:
+ $(CC) -o src/file-to-c src/file-to-c.c
+
+$(RAWFILES_O): encode filetoc
src/encode $(basename $(basename $@)) $(basename $@) $(SEED)
- $(OBJCOPY) -I binary -B $(OBJARCH) -O $(OBJTARGET) $(basename $@) $@
- $(RM) -f $(basename $@)
+ src/file-to-c $(basename $@)
+ $(CC) -c -o $@ $(basename $@).c
-$(BIN): $(OBJS) $(RAWOBJS)
- $(CC) -o $@ $(OBJS) $(RAWOBJS) $(LDFLAGS)
-# $(CC) -o $@ -static $(OBJS) $(RAWOBJS) $(LDFLAGS) -lc -lm -lossaudio -L/usr/X11R7/lib/X11/locale/lib/common/ -lxlibi18n -lximcp -lxlcDef -lxlcUTF8Load -lxlocale -lxomGeneric -lX11 -lxcb -lXau -lXdmcp -lXext -lXrandr -lXrender -lusbhid -lpthread -lSDL -lSDL_image -lSDL_mixer -lSDL_net -lSDL_gfx -lz
+$(BIN): $(OBJS) $(RAWFILES_O)
+ $(CC) -o $@ $(OBJS) $(RAWFILES_O) $(LDFLAGS)
$(STRIP) -s -w -R .comment -R .ident -R .debug* $@*
clean:
- $(RM) -f $(BIN) $(BIN).exe $(OBJS) $(RAWOBJS) \
- src/encode src/encode.exe stdout.txt stderr.txt
+ $(RM) -f $(BIN) $(BIN).exe $(OBJS) \
+ $(RAWFILES_ENC) $(RAWFILES_C) $(RAWFILES_H) $(RAWFILES_O) \
+ src/encode src/encode.exe src/file-to-c src/file-to-c.exe \
+ stdout.txt stderr.txt
+
-/* $Id: decode.c,v 1.1 2006/12/31 08:32:39 mmondor Exp $ */
+/* $Id: decode.c,v 1.2 2014/06/25 07:47:28 mmondor Exp $ */
/*
* Copyright (c) 2006, Matthew Mondor
#include <stdio.h>
#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
#include <decode.h>
void
-decode(void **ndata, size_t *nsize, void *data, size_t size)
+decode(uint8_t **ndata, size_t *nsize, const uint8_t *data, size_t size)
{
- uint8_t *ptr, *tptr, *key;
+ uint8_t *ptr, *tptr, *key, *copy;
- ptr = (uint8_t *)data;
+ /* XXX No error checking done */
+ copy = malloc(size);
+ (void) memcpy(copy, data, size);
+
+ ptr = copy;
*ndata = &ptr[4];
*nsize = size - 4;
-/* $Id: decode.h,v 1.1 2006/12/31 08:32:39 mmondor Exp $ */
+/* $Id: decode.h,v 1.2 2014/06/25 07:47:28 mmondor Exp $ */
/*
* Copyright (c) 2006, Matthew Mondor
-void decode(void **, size_t *, void *, size_t);
+#include <stdint.h>
+
+
+void decode(uint8_t **, size_t *, const uint8_t *, size_t);
--- /dev/null
+/* $Id: file-to-c.c,v 1.1 2014/06/25 07:47:28 mmondor Exp $ */
+
+/*
+ * Copyright (c) 2014, Matthew Mondor
+ * ALL RIGHTS RESERVED.
+ */
+
+/*
+ * Simple program to convert a file to a C program which can then be compiled
+ * and linked with an application. On some operating systems, using binutils
+ * objcopy to do this is problematic.
+ */
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+int main(int, char **);
+static const char *basename(const char *);
+static char *symbolname(const char *);
+static long filesize(const char *);
+static char *strtoupper(const char *);
+
+
+int
+main(int argc, char **argv)
+{
+ char *source, *target_c, *target_h, *symbol, *symbol2;
+ int i;
+ long size, count;
+ FILE *fhs, *fht;
+
+ if (argc != 2) {
+ (void) fprintf(stderr, "Usage: %s <file>\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+ source = argv[1];
+
+ symbol = symbolname(basename(source));
+ symbol2 = strtoupper(symbol);
+ i = strlen(source);
+ if ((target_c = malloc(i + 3)) == NULL) {
+ (void) fprintf(stderr, "malloc(%d) - %s\n", i + 3,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ (void) memcpy(target_c, source, i);
+ target_c[i++] = '.';
+ target_c[i] = 'c';
+ target_c[i + 1] = '\0';
+ if ((target_h = strdup(target_c)) == NULL) {
+ (void) fprintf(stderr, "strdup(%s) - %s\n", target_c,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ target_h[i] = 'h';
+
+ size = filesize(source);
+ if ((fht = fopen(target_c, "w")) == NULL) {
+ (void) fprintf(stderr, "fopen(%s) - %s\n", target_c,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ if ((fhs = fopen(source, "r")) == NULL) {
+ (void) fprintf(stderr, "fopen(%s) - %s\n", source,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ (void) fprintf(fht,
+ "\n/* Autogenerated by file-to-c, do not edit */\n\n\
+#include <stddef.h>\n\
+#include <stdint.h>\n\
+\n\
+const uint8_t\tstatic_%s_data[%ld] = {\n\
+ ", symbol, size);
+ for (i = 0, count = 1; ; i++, count++) {
+ uint8_t b;
+
+ if (i == 8) {
+ (void) fprintf(fht, "\n\t");
+ i = 0;
+ }
+ if (fread(&b, 1, 1, fhs) != 1)
+ break;
+ (void) fprintf(fht, "0x%02X%s", b,
+ (count == size ? "" : ", "));
+ }
+ (void) fclose(fhs);
+ (void) fprintf(fht, "\n};\n\n\
+const size_t\tstatic_%s_size = %ld;\n\n", symbol, size);
+ (void) fclose(fht);
+
+ if ((fht = fopen(target_h, "w")) == NULL) {
+ (void) fprintf(stderr, "fopen(%s) - %s\n", target_h,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ (void) fprintf(fht,
+ "\n/* Autogenerated by file-to-c, do not edit */\n\n\
+#ifndef _STATIC_%s_\n\
+#define _STATIC_%s_\n\
+\n\
+extern const uint8_t\tstatic_%s_data[%ld];\n\
+extern const size_t\tstatic_%s_size;\n\
+\n\
+#endif\n\n", symbol2, symbol2, symbol, size, symbol);
+ (void) fclose(fht);
+
+ exit(EXIT_SUCCESS);
+}
+
+/* Not available on all systems; minimal implementation */
+static const char *
+basename(const char *path)
+{
+ const char *cptr;
+
+ for (;;) {
+ for (cptr = path; *cptr != '\0' && *cptr != '/'; cptr++) ;
+ if (*cptr == '/') {
+ path = ++cptr;
+ continue;
+ }
+ break;
+ }
+
+ return path;
+}
+
+static char *
+symbolname(const char *string)
+{
+ char *symbol, *cptr;
+
+ if ((symbol = strdup(string)) == NULL) {
+ (void) fprintf(stderr, "strdup(%s) - %s\n", string,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ for (cptr = symbol; *cptr != '\0'; cptr++) {
+ if (!isalnum((int)*cptr))
+ *cptr = '_';
+ }
+
+ return symbol;
+}
+
+/* stat(2)/fstat(2) not available on all systems */
+static long
+filesize(const char *path)
+{
+ FILE *fh;
+ long size;
+
+ if ((fh = fopen(path, "rb")) == NULL) {
+ (void) fprintf(stderr, "fopen(%s) - %s\n", path,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ if (fseek(fh, 0, SEEK_END) == -1) {
+ (void) fprintf(stderr, "fseek(SEEK_END) - %s\n",
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ if ((size = ftell(fh)) == -1) {
+ (void) fprintf(stderr, "ftell() - %s\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ (void) fclose(fh);
+
+ return size;
+}
+
+static char *
+strtoupper(const char *string)
+{
+ char *str, *cptr;
+
+ if ((str = strdup(string)) == NULL) {
+ (void) fprintf(stderr, "strdup(%s) - %s\n", string,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ for (cptr = str; *cptr != '\0'; cptr++)
+ *cptr = toupper((int)*cptr);
+
+ return str;
+}
+
-/* $Id: main.c,v 1.4 2014/06/21 18:24:41 mmondor Exp $ */
+/* $Id: main.c,v 1.5 2014/06/25 07:47:28 mmondor Exp $ */
/*
* Copyright (c) 2006, Matthew Mondor
static void handle_uevent(SDL_Event *);
static void send_event(int, int);
-static SDL_Surface *bmp_load_key(void *, size_t);
+static SDL_Surface *bmp_load_key(const uint8_t *, size_t);
static int surface_blit_angle(SDL_Surface *, int, int, int);
-static Mix_Chunk *sample_load(void *, size_t);
+static Mix_Chunk *sample_load(const uint8_t *, size_t);
-static struct font *font_load(void *, size_t, int, int);
+static struct font *font_load(const uint8_t *, size_t, int, int);
static void font_blit_string(struct font *, int, int,
const char *, uint8_t, uint8_t, uint8_t, uint8_t);
Mix_GetError());
exit(EXIT_FAILURE);
}
- snd_cloak = sample_load((void *)&_binary_wav_nt_cloaked_wav_enc_start,
- (size_t)&_binary_wav_nt_cloaked_wav_enc_size);
- snd_uncloak = sample_load(
- (void *)&_binary_wav_nt_uncloak_wav_enc_start,
- (size_t)&_binary_wav_nt_uncloak_wav_enc_size);
- snd_shield = sample_load(
- (void *)&_binary_wav_nt_shield_up_wav_enc_start,
- (size_t)&_binary_wav_nt_shield_up_wav_enc_size);
- snd_unshield = sample_load(
- (void *)&_binary_wav_nt_shield_down_wav_enc_start,
- (size_t)&_binary_wav_nt_shield_down_wav_enc_size);
- snd_torp = sample_load(
- (void *)&_binary_wav_nt_fire_torp_other_wav_enc_start,
- (size_t)&_binary_wav_nt_fire_torp_other_wav_enc_size);
- snd_hit = sample_load((void *)&_binary_wav_nt_plasma_hit_wav_enc_start,
- (size_t)&_binary_wav_nt_plasma_hit_wav_enc_size);
- snd_explode = sample_load(
- (void *)&_binary_wav_nt_explosion_other_wav_enc_start,
- (size_t)&_binary_wav_nt_explosion_other_wav_enc_size);
+ snd_cloak = sample_load(static_nt_cloaked_wav_enc_data,
+ static_nt_cloaked_wav_enc_size);
+ snd_uncloak = sample_load(static_nt_uncloak_wav_enc_data,
+ static_nt_uncloak_wav_enc_size);
+ snd_shield = sample_load(static_nt_shield_up_wav_enc_data,
+ static_nt_shield_up_wav_enc_size);
+ snd_unshield = sample_load(static_nt_shield_down_wav_enc_data,
+ static_nt_shield_down_wav_enc_size);
+ snd_torp = sample_load(static_nt_fire_torp_other_wav_enc_data,
+ static_nt_fire_torp_other_wav_enc_size);
+ snd_hit = sample_load(static_nt_plasma_hit_wav_enc_data,
+ static_nt_plasma_hit_wav_enc_size);
+ snd_explode = sample_load(static_nt_explosion_other_wav_enc_data,
+ static_nt_explosion_other_wav_enc_size);
(void) Mix_AllocateChannels(16);
(void) Mix_ReserveChannels(2);
if ((mus = Mix_LoadMUS("ogg/1.ogg")) == NULL) {
/*
* Bitmap graphics
*/
- rship = bmp_load_key((void *)&_binary_bmp_RomDD_bmp_enc_start,
- (size_t)&_binary_bmp_RomDD_bmp_enc_size);
- fship = bmp_load_key((void *)&_binary_bmp_FedCA_bmp_enc_start,
- (size_t)&_binary_bmp_FedCA_bmp_enc_size);
+ rship = bmp_load_key(static_RomDD_bmp_enc_data,
+ static_RomDD_bmp_enc_size);
+ fship = bmp_load_key(static_FedCA_bmp_enc_data,
+ static_FedCA_bmp_enc_size);
/*
* Bitmap fonts
*/
- font = font_load((void *)&_binary_fnt_7x13_fnt_enc_start,
- (size_t)&_binary_fnt_7x13_fnt_enc_size, 7, 13);
+ font = font_load(static_7x13_fnt_enc_data,
+ static_7x13_fnt_enc_size, 7, 13);
/*
* We're already the main thread.
}
static SDL_Surface *
-bmp_load_key(void *data, size_t size)
+bmp_load_key(const uint8_t *data, size_t size)
{
SDL_RWops *rwo;
SDL_Surface *s;
Uint32 c;
- void *ndata;
+ uint8_t *ndata;
size_t nsize;
decode(&ndata, &nsize, data, size);
}
static Mix_Chunk *
-sample_load(void *data, size_t size)
+sample_load(const uint8_t *data, size_t size)
{
SDL_RWops *rwo;
Mix_Chunk *c;
- void *ndata;
+ uint8_t *ndata;
size_t nsize;
decode(&ndata, &nsize, data, size);
}
static struct font *
-font_load(void *data, size_t size, int width, int height)
+font_load(const uint8_t *data, size_t size, int width, int height)
{
struct font *font;
- void *ndata;
+ uint8_t *ndata;
size_t nsize;
decode(&ndata, &nsize, data, size);
-/* $Id: rawobjs.h,v 1.1 2006/12/31 08:32:39 mmondor Exp $ */
+/* $Id: rawobjs.h,v 1.2 2014/06/25 07:47:28 mmondor Exp $ */
/*
- * Copyright (c) 2006, Matthew Mondor
+ * Copyright (c) 2006, 2014, Matthew Mondor
* ALL RIGHTS RESERVED.
*/
/* XXX This file should probably be auto-generated */
-
-
#ifndef RAWOBJS_H
#define RAWOBJS_H
+#include "wav/nt_cloaked.wav.enc.h"
+#include "wav/nt_explosion_other.wav.enc.h"
+#include "wav/nt_fire_torp_other.wav.enc.h"
+#include "wav/nt_plasma_hit.wav.enc.h"
+#include "wav/nt_shield_down.wav.enc.h"
+#include "wav/nt_shield_up.wav.enc.h"
+#include "wav/nt_uncloak.wav.enc.h"
+#include "bmp/FedCA.bmp.enc.h"
+#include "bmp/RomDD.bmp.enc.h"
-/*
- * On win32, binary objects must be referenced without the underscore prefix.
- * Let's then define a few aliases.
- */
-#ifdef WIN32
-
-#define _binary_bmp_FedCA_bmp_enc_size binary_bmp_FedCA_bmp_enc_size
-#define _binary_bmp_FedCA_bmp_enc_start binary_bmp_FedCA_bmp_enc_start
-#define _binary_bmp_RomDD_bmp_enc_size binary_bmp_RomDD_bmp_enc_size
-#define _binary_bmp_RomDD_bmp_enc_start binary_bmp_RomDD_bmp_enc_start
-
-#define _binary_wav_nt_cloaked_wav_enc_size binary_wav_nt_cloaked_wav_enc_size
-#define _binary_wav_nt_cloaked_wav_enc_start binary_wav_nt_cloaked_wav_enc_start
-#define _binary_wav_nt_explosion_other_wav_enc_size binary_wav_nt_explosion_other_wav_enc_size
-#define _binary_wav_nt_explosion_other_wav_enc_start binary_wav_nt_explosion_other_wav_enc_start
-#define _binary_wav_nt_fire_torp_other_wav_enc_size binary_wav_nt_fire_torp_other_wav_enc_size
-#define _binary_wav_nt_fire_torp_other_wav_enc_start binary_wav_nt_fire_torp_other_wav_enc_start
-#define _binary_wav_nt_plasma_hit_wav_enc_size binary_wav_nt_plasma_hit_wav_enc_size
-#define _binary_wav_nt_plasma_hit_wav_enc_start binary_wav_nt_plasma_hit_wav_enc_start
-#define _binary_wav_nt_shield_down_wav_enc_size binary_wav_nt_shield_down_wav_enc_size
-#define _binary_wav_nt_shield_down_wav_enc_start binary_wav_nt_shield_down_wav_enc_start
-#define _binary_wav_nt_shield_up_wav_enc_size binary_wav_nt_shield_up_wav_enc_size
-#define _binary_wav_nt_shield_up_wav_enc_start binary_wav_nt_shield_up_wav_enc_start
-#define _binary_wav_nt_uncloak_wav_enc_size binary_wav_nt_uncloak_wav_enc_size
-#define _binary_wav_nt_uncloak_wav_enc_start binary_wav_nt_uncloak_wav_enc_start
-
-#define _binary_fnt_7x13_fnt_enc_start binary_fnt_7x13_fnt_enc_start
-#define _binary_fnt_7x13_fnt_enc_size binary_fnt_7x13_fnt_enc_size
-
-#endif
-
-
-
-/*
- * And export symbols to other modules
- */
-
-extern void *_binary_bmp_FedCA_bmp_enc_size;
-extern void *_binary_bmp_FedCA_bmp_enc_start;
-extern void *_binary_bmp_RomDD_bmp_enc_size;
-extern void *_binary_bmp_RomDD_bmp_enc_start;
-
-extern void *_binary_wav_nt_cloaked_wav_enc_size;
-extern void *_binary_wav_nt_cloaked_wav_enc_start;
-extern void *_binary_wav_nt_explosion_other_wav_enc_size;
-extern void *_binary_wav_nt_explosion_other_wav_enc_start;
-extern void *_binary_wav_nt_fire_torp_other_wav_enc_size;
-extern void *_binary_wav_nt_fire_torp_other_wav_enc_start;
-extern void *_binary_wav_nt_plasma_hit_wav_enc_size;
-extern void *_binary_wav_nt_plasma_hit_wav_enc_start;
-extern void *_binary_wav_nt_shield_down_wav_enc_size;
-extern void *_binary_wav_nt_shield_down_wav_enc_start;
-extern void *_binary_wav_nt_shield_up_wav_enc_size;
-extern void *_binary_wav_nt_shield_up_wav_enc_start;
-extern void *_binary_wav_nt_uncloak_wav_enc_size;
-extern void *_binary_wav_nt_uncloak_wav_enc_start;
-
-extern void *_binary_fnt_7x13_fnt_enc_start;
-extern void *_binary_fnt_7x13_fnt_enc_size;
-
-
+#include "fnt/7x13.fnt.enc.h"
#endif