Refactored util to be more lib-like, so its functionality can be used by the game too. This way the game can load and use the resources from the uncompressed DAVE.EXE without needing export.

This commit is contained in:
Josh K 2018-07-05 20:05:32 -04:00
parent 86354692fb
commit 952b7099fb
7 changed files with 80 additions and 58 deletions

View File

@ -25,6 +25,7 @@ LDFLAGS_g := -s
OBJDIR_g := obj
SRCS := $(wildcard *.c)
SRCS += $(wildcard util/*.c)
OBJS_t := $(SRCS:.c=.o)
# include dirs

View File

@ -9,9 +9,10 @@ game_assets_t* g_assets = NULL;
void init_game() {
gs->quit = 1;
gs->current_level = 1;
gs->levels = GetLevels();
}
// initialize assets
// initialize game assets
void init_assets( SDL_Renderer* r ) {
}
@ -31,6 +32,10 @@ void render( SDL_Renderer* r ) {
const uint8_t R_SCALE = 3;
int main( int argc, char** argv ) {
// initialize resources from DAVE.EXE
LoadTiles();
LoadLevels();
gs = malloc( sizeof(game_state_t) );
init_game();
@ -40,6 +45,8 @@ int main( int argc, char** argv ) {
g_assets = malloc( sizeof(game_assets_t) );
init_assets( renderer );
// tile surfaces should be converted as textures inside g_assets now
FreeTileSurfaces();
// main loop
while ( !gs->quit ) {

View File

@ -3,16 +3,8 @@
#include <SDL.h>
// level structure
// byte[256] path, two signed 8bit relative movement, 0xea 0xea for end
// byte[100x10] tile index data, 100 by 10, so more than one level could fit in a chunk
// byte[24] unsed padding
// note: player start and monster starts are hardcoded
typedef struct {
int8_t path[256];
uint8_t tiles[1000];
uint8_t pad[24];
} level_t;
// for dealing with resources in EXE
#include "util/util.h"
// global game state
typedef struct {
@ -20,12 +12,13 @@ typedef struct {
uint8_t current_level;
uint8_t view_x, view_y;
level_t levels[10];
level_t* levels; // grabbed from util
} game_state_t;
// game assets
typedef struct {
SDL_Texture* tile_tx[158];
// tiles as textures converted from util's tile surfaces
SDL_Texture* tile_tx[NUM_EXE_TILES];
} game_assets_t;
#endif

View File

@ -28,7 +28,7 @@ OBJDIR_g := obj
OBJS_t := tiles.o levels.o
# include dirs
CFLAGS_g +=
CFLAGS_g += -DUTIL_BIN
# libs
ifdef ISWIN
@ -40,9 +40,9 @@ endif
# binary target
ifdef ISWIN
TARG_t := tiles.exe
TARG_t := util.exe
else
TARG_t := tiles
TARG_t := util
endif
utils: $(TARG_t)

View File

@ -1,30 +1,22 @@
// extract level data from uncompressed dave.exe
// named as levelxx.dat
#include <stdio.h>
#include <SDL.h>
//extern SDL_RWops* ddexe;
extern SDL_Surface* tile_sfc[];
// level structure
// byte[256] path, two signed 8bit relative movement, 0xea 0xea for end
// byte[100x10] tile index data, 100 by 10, so more than one level could fit in a chunk
// byte[24] unsed padding
// note: player start and monster starts are hardcoded
typedef struct {
int8_t path[256];
uint8_t tiles[1000];
uint8_t pad[24];
} level_t;
#include "util.h"
// all levels in the exe
level_t levels[10];
static level_t levels[NUM_EXE_LEVELS];
// return level structure for external use
level_t* GetLevels() {
return levels;
}
// export all levels to seperate .dat file
void SaveLevels() {
for ( int l = 0; l < 10; ++l ) {
for ( int l = 0; l < NUM_EXE_LEVELS; ++l ) {
char fname[1024];
snprintf( fname, 1024, "../levels/level%02u.dat", l );
snprintf( fname, 1024, "./levels/level%02u.dat", l );
printf( "Saving level %u to '%s'\n", l, fname );
SDL_RWops* lvlfile = SDL_RWFromFile( fname, "wb" );
@ -48,7 +40,7 @@ void SaveLevels() {
void LoadLevels() {
const uint32_t lvl_dat_addr = 0x26e0a;
SDL_RWops* ddexe = SDL_RWFromFile( "../DAVE.EXE", "rb" );
SDL_RWops* ddexe = SDL_RWFromFile( "DAVE.EXE", "rb" );
if ( ddexe == NULL ) { fprintf( stderr, "Error opening DAVE.EXE for levels.\n" ); return; }
// 10 levels @ 1280 bytes each
@ -61,7 +53,7 @@ void LoadLevels() {
memset( levels, 0, sizeof(levels) );
// read each level into array
for ( int l = 0; l < 10; ++l ) {
for ( int l = 0; l < NUM_EXE_LEVELS; ++l ) {
// read path data
for ( int p = 0; p < 256; ++p ) {
SDL_RWread( ddexe, &levels[l].path[p], 1, 1 );
@ -83,7 +75,7 @@ void CreateWorldMap() {
// create big empty surface for containing entire world map
SDL_Surface* map = SDL_CreateRGBSurface( 0, 1600, 1600, 32, 0, 0, 0, 0 );
// level, row, column
for ( int l = 0; l < 10; ++l ) {
for ( int l = 0; l < NUM_EXE_LEVELS; ++l ) {
for ( int y = 0; y < 10; ++y ) {
for ( int x = 0; x < 100; ++x ) {
uint8_t til = levels[l].tiles[y * 100 + x];
@ -91,6 +83,7 @@ void CreateWorldMap() {
dst.x = x * 16;
dst.y = l * 160 + y * 16;
dst.w = 16; dst.h = 16;
SDL_Surface** tile_sfc = GetTileSurfaces();
SDL_BlitSurface( tile_sfc[til], NULL, map, &dst );
// hardcoded player and monster starts

View File

@ -2,44 +2,36 @@
#include <stdint.h>
#include <inttypes.h>
#include <SDL.h>
#include "util.h"
// all tiles in the exe
SDL_Surface* tile_sfc[158];
//SDL_Texture* tile_tx[158];
// level info
void LoadLevels();
void SaveLevels();
void CreateWorldMap();
static SDL_Surface* tile_sfc[NUM_EXE_TILES];
// export all tiles to bmp
void SaveTiles() {
// Save out the all tile surfaces
for ( int curtil = 0; curtil < 158; ++curtil ) {
for ( int curtil = 0; curtil < NUM_EXE_TILES; ++curtil ) {
SDL_Surface* sfc = tile_sfc[curtil];
printf( "Saving tile%03d.bmp (%ux%u)...\n", curtil, sfc->w, sfc->h );
char fname[1024];
snprintf( fname, 1024, "../tiles/tile%03d.bmp", curtil );
snprintf( fname, 1024, "./tiles/tile%03d.bmp", curtil );
SDL_SaveBMP( sfc, fname );
}
}
// convert all loaded tile surfaces to textures
/*void ConvertTiles( SDL_Renderer* r ) {
for ( int i = 0; i < 158; ++i ) {
tile_tx[i] = SDL_CreateTextureFromSurface( r, tile_sfc[i] );
SDL_FreeSurface( tile_sfc[i] );
}
}*/
// return tile surface array for external use
SDL_Surface** GetTileSurfaces() {
return tile_sfc;
}
// fill global tile array with tiles from exe
void LoadTiles() {
const uint32_t vga_data_addr = 0x120f0;
const uint32_t vga_pal_addr = 0x26b0a;
// exe assumed uncompressed
SDL_RWops* ddexe = SDL_RWFromFile( "../DAVE.EXE", "rb" );
SDL_RWops* ddexe = SDL_RWFromFile( "DAVE.EXE", "rb" );
if ( ddexe != NULL ) printf( "SUCCESS! (%"PRIi64" bytes)\n", SDL_RWsize( ddexe ) );
else { fprintf( stderr, "Error opening DAVE.EXE for tiles!\n" ); exit( EXIT_FAILURE ); }
// tileset
SDL_RWseek( ddexe, vga_data_addr, RW_SEEK_SET );
@ -152,11 +144,12 @@ void LoadTiles() {
// free all loaded exe's tile surfaces
void FreeTileSurfaces() {
for ( int i = 0; i < 158; ++i ) {
for ( int i = 0; i < NUM_EXE_TILES; ++i ) {
SDL_FreeSurface( tile_sfc[i] );
}
}
#ifdef UTIL_BIN
int main( int argc, char** argv ) {
// high scores
SDL_RWops* dscor = SDL_RWFromFile( "../DSCORES.DAV", "rb" );
@ -168,16 +161,16 @@ int main( int argc, char** argv ) {
SDL_RWclose( dscor );
LoadTiles();
//SaveTiles();
//ConvertTiles();
SaveTiles();
// LEVELS
LoadLevels();
//SaveLevels();
SaveLevels();
CreateWorldMap();
FreeTileSurfaces();
return 0;
}
#endif

35
util/util.h Normal file
View File

@ -0,0 +1,35 @@
// utility functions for dealing with resources in the dave.exe file
#ifndef _UTIL_H
#define _UTIL_H
#include <SDL.h>
// constant exe resource counts
#define NUM_EXE_TILES 158
#define NUM_EXE_LEVELS 10
// tiles
void LoadTiles();
SDL_Surface** GetTileSurfaces();
void SaveTiles();
void FreeTileSurfaces();
// level structure
// byte[256] path, two signed 8bit relative movement, 0xea 0xea for end
// byte[100x10] tile index data, 100 by 10, so more than one level could fit in a chunk
// byte[24] unsed padding
// note: player start and monster starts are hardcoded
typedef struct {
int8_t path[256];
uint8_t tiles[1000];
uint8_t pad[24];
} level_t;
// levels
void LoadLevels();
level_t* GetLevels();
void SaveLevels();
void CreateWorldMap();
#endif