Skip to content

Commit

Permalink
Introduced data cache for faster loading
Browse files Browse the repository at this point in the history
  • Loading branch information
esoteric-programmer committed Mar 5, 2017
1 parent bfff7f6 commit a77bd93
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Version 0.6.2
- Data for cia version can be loaded from /3ds/lemmings now
- Introduced cache to reduce start-up time
- Bugfix

Version 0.6.1
- Bugfixes (note: multiplayer mode is not compatible with v0.6)

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ RUN_FLAGS :=

VERSION_MAJOR := 0
VERSION_MINOR := 6
VERSION_MICRO := 1
VERSION_MICRO := 2

# 3DS CONFIGURATION #

Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ To avoid duplicates, you may only fill these folders if you own all Lemmings gam
orig, ohno, xmas91, xmas92, holi94
For multiplayer mode, fill the subfolders of /lemmings/2p of the host system.

If you modify level files later, you should delete the CACHE_xx.DAT files in the lemmings folder afterwards.

The game uses the NDSP service to play audio. Thus you need a DSP firm dump to have audio.
You may want to use your own sound and/or music files.
Put your wave files into lemmings/audio folder.
See README.txt in that folder for more details.

\* See README.txt in these folders.

These files are not included (and will never be included),
because they are protected by copyright law.

For game control, see CONTROL.txt.

\* See README.txt in these folders.
9 changes: 9 additions & 0 deletions include/data_cache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef DAtA_CACHE_H
#define DAtA_CACHE_H
#define CACHE_FILE_VERSION 0
#include <3ds.h>

void update_data_cache(u8 game, u8 multiplayer, const char* level_names);
u8 read_data_cache(u8 game, u8 multiplayer, char* level_names);

#endif
57 changes: 57 additions & 0 deletions src/data_cache.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <stdio.h>
#include "data_cache.h"
#include "gamespecific.h"
#include "settings.h"

void update_data_cache(u8 game, u8 multiplayer, const char* level_names) {
u8 version = CACHE_FILE_VERSION;
char cachefilename[64];
u16 num_of_levels = 0;
if (!level_names) {
return;
}
if (!multiplayer) {
num_of_levels = (u16)import[game].num_of_difficulties
* (u16)import[game].num_of_level_per_difficulty;
}else{
return;
}
sprintf(cachefilename, "%s/CACHE_%02X.DAT",PATH_ROOT, game + (multiplayer?0x80:0));
FILE* cachefile = fopen(cachefilename, "wb");
if (!cachefile) {
return;
}
fwrite(&version, 1, 1, cachefile);
fwrite(level_names, 1, 33*num_of_levels, cachefile);
fclose(cachefile);
}

u8 read_data_cache(u8 game, u8 multiplayer, char* level_names) {
u8 version = 0;
char cachefilename[64];
u16 num_of_levels = 0;
if (!level_names) {
return 0;
}
if (!multiplayer) {
num_of_levels = (u16)import[game].num_of_difficulties
* (u16)import[game].num_of_level_per_difficulty;
}else{
return 0;
}
sprintf(cachefilename, "%s/CACHE_%02X.DAT",PATH_ROOT, game + (multiplayer?0x80:0));
FILE* cachefile = fopen(cachefilename, "rb");
if (!cachefile) {
return 0;
}
fread(&version, 1, 1, cachefile);
if (version > CACHE_FILE_VERSION) {
return 0;
}
u16 size = fread(level_names, 1, 33*num_of_levels, cachefile);
fclose(cachefile);
if (size != 33*num_of_levels) {
return 0;
}
return 1;
}
19 changes: 16 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "main.h"
#include "settings_menu.h"
#include "network_menu.h"
#include "data_cache.h"

const char* LEMMINGS_DIRS[] = {"/lemmings", "/3ds/lemmings", 0};
const char* PATH_ROOT = 0;
Expand Down Expand Up @@ -160,7 +161,13 @@ int main() {
}
u16 offset = 0;
for (i=0;i<LEMMING_GAMES;i++) {
games[i] = read_level_names(i, level_names + offset);
games[i] = read_data_cache(i, 0, level_names + offset);
if (!games[i]) {
games[i] = read_level_names(i, level_names + offset);
if (games[i]) {
update_data_cache(i, 0, level_names + offset);
}
}
offset += 33*
(u16)import[i].num_of_difficulties *
(u16)import[i].num_of_level_per_difficulty;
Expand Down Expand Up @@ -240,12 +247,18 @@ int main() {
if (num_2p_level[0] || num_2p_level[1]) {
name_2p_level = (char*)malloc(33*(u16)(num_2p_level[0]+num_2p_level[1]));
if (name_2p_level[0]) {
if (!read_level_names_from_path(import_2p[0].level_path, &num_2p_level[0], name_2p_level)) {
if (!read_level_names_from_path(
import_2p[0].level_path,
&num_2p_level[0],
name_2p_level)) {
name_2p_level[0] = 0;
}
}
if (name_2p_level[1]) {
if (!read_level_names_from_path(import_2p[1].level_path, &num_2p_level[1], &name_2p_level[33*(u16)num_2p_level[0]])) {
if (!read_level_names_from_path(
import_2p[1].level_path,
&num_2p_level[1],
&name_2p_level[33*(u16)num_2p_level[0]])) {
name_2p_level[1] = 0;
}
}
Expand Down

0 comments on commit a77bd93

Please sign in to comment.