Skip to content

Commit

Permalink
Add cart loading, step 2
Browse files Browse the repository at this point in the history
  • Loading branch information
MineRobber9000 committed Mar 13, 2024
1 parent d890e6b commit 6035be4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 38 deletions.
1 change: 1 addition & 0 deletions projects/VS2022/raylib_game/raylib_game.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\..\src\cart.c" />
<ClCompile Include="..\..\..\src\eightbitcolor.c" />
<ClCompile Include="..\..\..\src\lua\lapi.c" />
<ClCompile Include="..\..\..\src\lua\lauxlib.c" />
Expand Down
47 changes: 47 additions & 0 deletions src/cart.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "raylib.h"
#include "cart.h"
#include "riff.h"
#include <string.h>

FourCC _CODE = {'C','O','D','E'};

void CartChunkWalker(Cart *cart, RIFF_Chunk *chunk)
{
if (riff_is_container(chunk->type)) {
RIFF_ChunkListItem *walker = chunk->contains.chunks;
while (walker!=NULL) {
CartChunkWalker(cart,walker->chunk);
walker = walker->next;
}
} else {
if (riff_fourcc_equals(chunk->type,_CODE)) {
if (cart->code_size==0) {
cart->code = MemAlloc(chunk->size);
memcpy(cart->code,chunk->contains.data,chunk->size);
cart->code_size = chunk->size;
} else {
cart->code = MemRealloc(cart->code, cart->code_size+chunk->size);
memcpy(cart->code+cart->code_size,chunk->contains.data,chunk->size);
cart->code_size += chunk->size;
}
}
}
}

Cart *LoadCart(char * filename)
{
Cart *ret = MemAlloc(sizeof(Cart));
int len;
unsigned char *data = LoadFileData(filename,&len);
TraceLog(LOG_INFO,"CART: Loaded cart file, %d bytes",len);
size_t offset = 0;
RIFF_Chunk *chunk = riff_parse_chunk_from_data(data,(size_t)len,&offset);
if (chunk==NULL) {
TraceLog(LOG_ERROR, "CART: Error loading cart: %s", riff_get_error());
ret->code = "function doframe() cls(7) print('error loading cart') end";
ret->code_size = strlen(ret->code);
} else {
CartChunkWalker(ret,chunk);
}
return ret;
}
9 changes: 9 additions & 0 deletions src/cart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdint.h>
#include <string.h>

typedef struct {
unsigned char *code;
size_t code_size;
} Cart;

Cart *LoadCart(char * filename);
43 changes: 5 additions & 38 deletions src/nexus.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
#include "eightbitcolor.h"
#include "lua_api.h"
#include "nexus.h"
#include "riff.h"
#include <string.h>
#include "cart.h"

#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
Expand All @@ -36,14 +35,11 @@ static int ShouldDrawFPS = 0;

static RenderTexture2D framebuffer;

static FourCC _CODE = {'C','O','D','E'};

//----------------------------------------------------------------------------------
// Local Functions Declaration
//----------------------------------------------------------------------------------
static void UpdateDrawFrame(void); // Update and draw one frame
static void _DrawFPS(void); // Draw FPS
static void LoadCart(RIFF_Chunk *chunk); // Load cart from RIFF chunks

//----------------------------------------------------------------------------------
// Main entry point
Expand All @@ -70,19 +66,9 @@ int main(void)
InitLua();

// Load nogameloaded.rom and load the code into the VM
int len;
unsigned char *data = LoadFileData("resources/nogameloaded.rom",&len);
TraceLog(LOG_INFO,"CART: Loaded cart file, %d bytes",len);
size_t offset = 0;
RIFF_Chunk *cart = riff_parse_chunk_from_data(data,(size_t)len,&offset);
if (cart==NULL) {
TraceLog(LOG_ERROR, "CART: Error loading cart: %s", riff_get_error());
char *error = "function doframe() cls(7) print('error loading cart') end";
LoadString(error,strlen(error));
DoCall(0,0);
} else {
LoadCart(cart);
}
Cart *cart = LoadCart("resources/nogameloaded.rom");
LoadString(cart->code,cart->code_size);
DoCall(0,0);

#if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 60, 1);
Expand Down Expand Up @@ -162,23 +148,4 @@ static void _DrawFPS(void)
else if (fps < 15) color = RED; // Low FPS

DrawTextEx(font, TextFormat("FPS: %2i", fps), (Vector2){1, 1}, 45, 0, color);
}

//----------------------------------------------------------------------------------
// Load cart from RIFF chunks
//----------------------------------------------------------------------------------
static void LoadCart(RIFF_Chunk *chunk)
{
if (riff_is_container(chunk->type)) {
RIFF_ChunkListItem *walker = chunk->contains.chunks;
while (walker!=NULL) {
LoadCart(walker->chunk);
walker=walker->next;
}
} else {
if (riff_fourcc_equals(chunk->type,_CODE)) {
LoadString(chunk->contains.data,chunk->size);
DoCall(0,0);
}
}
}
}

0 comments on commit 6035be4

Please sign in to comment.