Added 'res' dir from which resources are loaded. Level data loaded from util is now as a copy, so original data from EXE is preserved. Input events polled in a loop. Fixed start_level so jumping is completely reset. Z key can be used for jumping as well. Added rest of hardcoded player starts.
This commit is contained in:
parent
13ec0a5067
commit
6f6a429163
68
lmdave.c
68
lmdave.c
|
@ -13,8 +13,9 @@ void init_game() {
|
|||
// clean dave state
|
||||
memset( &gs->ds, 0, sizeof(dave_state_t) );
|
||||
|
||||
// returns array of NUM_EXE_LEVELS that was loaded by the util lib
|
||||
gs->levels = GetLevels();
|
||||
// copy all levels loaded from exe by util
|
||||
for ( int i = 0; i < NUM_EXE_LEVELS; ++i )
|
||||
GetLevel( i, &gs->levels[i] );
|
||||
}
|
||||
|
||||
// initialize game assets
|
||||
|
@ -40,13 +41,16 @@ void start_level() {
|
|||
|
||||
gs->ds.jump_timer = 0;
|
||||
gs->ds.on_ground = 0;
|
||||
gs->ds.do_jump = 0;
|
||||
|
||||
// hardcoded player starts
|
||||
switch ( gs->current_level ) {
|
||||
case 0: gs->ds.tx = 2; gs->ds.ty = 8; break;
|
||||
case 0: case 4: case 5: case 7: case 9: gs->ds.tx = 2; gs->ds.ty = 8; break;
|
||||
case 1: gs->ds.tx = 1; gs->ds.ty = 8; break;
|
||||
case 2: gs->ds.tx = 2; gs->ds.ty = 5; break;
|
||||
case 3: gs->ds.tx = 1; gs->ds.ty = 5; break;
|
||||
case 6: gs->ds.tx = 1; gs->ds.ty = 2; break;
|
||||
case 8: gs->ds.tx = 6; gs->ds.ty = 1; break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
|
@ -62,7 +66,27 @@ void start_level() {
|
|||
// poll input
|
||||
void check_input() {
|
||||
SDL_Event ev;
|
||||
SDL_PollEvent( &ev );
|
||||
|
||||
while ( SDL_PollEvent( &ev ) ) {
|
||||
|
||||
// events
|
||||
switch ( ev.type ) {
|
||||
|
||||
case SDL_QUIT: gs->quit = 1; break;
|
||||
|
||||
case SDL_KEYDOWN: {
|
||||
if ( ev.key.repeat ) break;
|
||||
SDL_Keycode key = ev.key.keysym.sym;
|
||||
// android back
|
||||
if ( key == SDLK_AC_BACK ) { gs->quit = 1; }
|
||||
// jump event
|
||||
if ( key == SDLK_UP || key == SDLK_z ) { gs->ds.try_jump = 1; }
|
||||
} break;
|
||||
|
||||
default: break;
|
||||
} // switch
|
||||
}
|
||||
|
||||
// real-time keystate
|
||||
const uint8_t* keys = SDL_GetKeyboardState( NULL );
|
||||
|
||||
|
@ -70,22 +94,6 @@ void check_input() {
|
|||
if ( keys[SDL_SCANCODE_RIGHT] ) gs->ds.try_right = 1;
|
||||
if ( keys[SDL_SCANCODE_LEFT] ) gs->ds.try_left = 1;
|
||||
//if ( keys[SDL_SCANCODE_UP] ) gs->ds.try_jump = 1;
|
||||
|
||||
// events
|
||||
switch ( ev.type ) {
|
||||
|
||||
case SDL_QUIT: gs->quit = 1; break;
|
||||
|
||||
case SDL_KEYDOWN: {
|
||||
if ( ev.key.repeat ) break;
|
||||
SDL_Keycode key = ev.key.keysym.sym;
|
||||
// android back
|
||||
if ( key == SDLK_AC_BACK ) { gs->quit = 1; }
|
||||
// jump event
|
||||
if ( key == SDLK_UP ) { gs->ds.try_jump = 1; }
|
||||
} break;
|
||||
default: break;
|
||||
} // switch
|
||||
}
|
||||
|
||||
// clear all try input flags at end of frame
|
||||
|
@ -275,10 +283,6 @@ void scroll_screen() {
|
|||
|
||||
// update game logic
|
||||
void update_game() {
|
||||
/*if ( gs->current_level == 0xff ) gs->current_level = 0;
|
||||
if ( gs->current_level > NUM_EXE_LEVELS - 1 )
|
||||
gs->current_level = NUM_EXE_LEVELS - 1;*/
|
||||
|
||||
// update collision point flags
|
||||
check_collision();
|
||||
// pickups
|
||||
|
@ -289,6 +293,7 @@ void update_game() {
|
|||
move_dave();
|
||||
// game view scrolling
|
||||
scroll_screen();
|
||||
// dave gravity
|
||||
apply_gravity();
|
||||
// update level-wide state
|
||||
update_level();
|
||||
|
@ -357,14 +362,10 @@ int main( int argc, char** argv ) {
|
|||
if ( SDL_Init( SDL_INIT_VIDEO ) )
|
||||
SDL_Log( "SDL Init error: %s\n", SDL_GetError() );
|
||||
|
||||
// SDL_Window* window = NULL;
|
||||
// SDL_Renderer* renderer = NULL;
|
||||
// if ( SDL_CreateWindowAndRenderer( 960, 720, SDL_WINDOW_RESIZABLE, &window, &renderer ) )
|
||||
// SDL_Log( "SDL WindowAndRenderer error: %s\n", SDL_GetError() );
|
||||
|
||||
// create window and renderer
|
||||
int winw = 1280, winh = 720;
|
||||
SDL_Window* window = SDL_CreateWindow( "lmdave", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
||||
1280, 720, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL );
|
||||
winw, winh, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL );
|
||||
if ( window == NULL ) {
|
||||
SDL_Log( "SDL_CreateWindow error: %s\n", SDL_GetError() );
|
||||
SDL_Quit(); return EXIT_FAILURE;
|
||||
|
@ -380,7 +381,6 @@ int main( int argc, char** argv ) {
|
|||
|
||||
// set internal rendering size
|
||||
SDL_RenderSetLogicalSize( renderer, 320, 200 );
|
||||
//SDL_RenderSetScale( renderer, R_SCALE, R_SCALE );
|
||||
// scaling hint
|
||||
//SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "linear" );
|
||||
|
||||
|
@ -397,7 +397,7 @@ int main( int argc, char** argv ) {
|
|||
start_level();
|
||||
|
||||
// main loop
|
||||
while ( !gs->quit ) {
|
||||
while ( gs->quit == 0 ) {
|
||||
// fixed timestep
|
||||
uint32_t st = SDL_GetTicks();
|
||||
|
||||
|
@ -405,7 +405,6 @@ int main( int argc, char** argv ) {
|
|||
update_game();
|
||||
render( renderer );
|
||||
|
||||
//uint32_t et = SDL_GetTicks();
|
||||
uint32_t delay = FRAME_DELAY - (SDL_GetTicks() - st);
|
||||
delay = delay > FRAME_DELAY ? 0 : delay;
|
||||
SDL_Delay( delay );
|
||||
|
@ -423,9 +422,10 @@ int main( int argc, char** argv ) {
|
|||
SDL_Quit();
|
||||
|
||||
// an explicit exit for android
|
||||
#ifdef __ANDROID__
|
||||
#if defined( __ANDROID__ ) || defined( ANDROID )
|
||||
exit( EXIT_SUCCESS );
|
||||
#endif
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
|
3
lmdave.h
3
lmdave.h
|
@ -54,7 +54,7 @@ typedef struct {
|
|||
// dave player state
|
||||
dave_state_t ds;
|
||||
|
||||
level_t* levels; // grabbed from util, NUM_EXE_LEVELS count
|
||||
level_t levels[NUM_EXE_LEVELS]; // copied from exe util's GetLevel
|
||||
} game_state_t;
|
||||
|
||||
// game assets
|
||||
|
@ -69,3 +69,4 @@ const uint8_t TILE_SIZE = 16;
|
|||
const uint8_t FRAME_DELAY = 33;
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -42,9 +42,9 @@ endif
|
|||
|
||||
# binary target
|
||||
ifdef ISWIN
|
||||
TARG_t := util.exe
|
||||
TARG_t := ../build/util.exe
|
||||
else
|
||||
TARG_t := util
|
||||
TARG_t := ../build/util
|
||||
endif
|
||||
|
||||
utils: $(TARG_t)
|
||||
|
|
|
@ -7,9 +7,10 @@
|
|||
// all levels in the exe
|
||||
static level_t levels[NUM_EXE_LEVELS];
|
||||
|
||||
// return level structure for external use
|
||||
level_t* GetLevels() {
|
||||
return levels;
|
||||
// copy a single level structure for external use
|
||||
void GetLevel( unsigned n, level_t* lvl ) {
|
||||
if ( n > NUM_EXE_LEVELS - 1 ) n = 0;
|
||||
memcpy( lvl, &levels[n], sizeof(level_t) );
|
||||
}
|
||||
|
||||
// export all levels to seperate .dat file
|
||||
|
@ -40,7 +41,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( "res/DAVE.EXE", "rb" );
|
||||
if ( ddexe == NULL ) { fprintf( stderr, "Error opening DAVE.EXE for levels.\n" ); return; }
|
||||
|
||||
// 10 levels @ 1280 bytes each
|
||||
|
|
|
@ -30,7 +30,7 @@ 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( "res/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 ); }
|
||||
|
||||
|
@ -153,13 +153,13 @@ void FreeTileSurfaces() {
|
|||
#ifdef UTIL_BIN
|
||||
int main( int argc, char** argv ) {
|
||||
// high scores
|
||||
SDL_RWops* dscor = SDL_RWFromFile( "../DSCORES.DAV", "rb" );
|
||||
/*SDL_RWops* dscor = SDL_RWFromFile( "res/DSCORES.DAV", "rb" );
|
||||
uint8_t lscor[9];
|
||||
while ( SDL_RWread( dscor, lscor, 9, 1 ) ) {
|
||||
printf( "LVL %u - SCORE: %u%u%u%u%u - NAM: %.3s\n", lscor[0],
|
||||
lscor[1], lscor[2], lscor[3], lscor[4], lscor[5], (char*)&lscor[6] );
|
||||
}
|
||||
SDL_RWclose( dscor );
|
||||
SDL_RWclose( dscor );*/
|
||||
|
||||
LoadTiles();
|
||||
SaveTiles();
|
||||
|
|
|
@ -27,7 +27,7 @@ typedef struct {
|
|||
|
||||
// levels
|
||||
void LoadLevels();
|
||||
level_t* GetLevels();
|
||||
void GetLevel( unsigned n, level_t* lvl );
|
||||
void SaveLevels();
|
||||
void CreateWorldMap();
|
||||
|
||||
|
|
Loading…
Reference in New Issue