Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
RobLoach committed Jan 4, 2025
1 parent 4b17ee7 commit 4773c55
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 89 deletions.
18 changes: 6 additions & 12 deletions src/ChaiLove.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "ChaiLove.h"
#include "LibretroLog.h"
#include <libretro.h>
#include <string>
#include "ChaiLove.h"

ChaiLove* ChaiLove::m_instance = NULL;
retro_input_state_t ChaiLove::input_state_cb = NULL;
Expand Down Expand Up @@ -55,20 +54,19 @@ void ChaiLove::quit(void) {
window.unload();
}

bool ChaiLove::load(const std::string& file, const void* data) {
bool ChaiLove::load(const std::string& file, const void* data, unsigned int dataSize) {
// Display a welcome message from ChaiLove.
#ifndef GIT_VERSION
#define GIT_VERSION ""
#endif
#ifndef GIT_VERSION
#define GIT_VERSION ""
#endif
std::string version = CHAILOVE_VERSION_STRING GIT_VERSION;
//LibretroLog::log(RETRO_LOG_INFO) << "[ChaiLove] ChaiLove " << version.c_str() << std::endl;

// Iniitalize some of the initial subsystems.
sound.load(app);

// Initalize the file system.
bool loaded = filesystem.init(file, data);
if (!loaded) {
if (!filesystem.init(file, data, dataSize)) {
//LibretroLog::log(RETRO_LOG_ERROR) << "[ChaiLove] [filesystem] Error loading " << file << std::endl;
return false;
}
Expand Down Expand Up @@ -131,10 +129,6 @@ void ChaiLove::reset() {
* Render the ChaiLove.
*/
void ChaiLove::draw() {
if (event.m_shouldclose) {
return;
}

// Clear the screen.
graphics.clear();

Expand Down
4 changes: 3 additions & 1 deletion src/ChaiLove.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
#define CHAILOVE_VERSION_PATCH 0
#define CHAILOVE_VERSION_STRING "2.0.0-pre"

#include <string>

#include "pntr_app.h"

#include "libretro.h"
Expand Down Expand Up @@ -101,7 +103,7 @@ class ChaiLove {

~ChaiLove();
void quit(void);
bool load(const std::string& file, const void* data);
bool load(const std::string& file, const void* data, unsigned int dataSize);
void update();
void draw();
void reset();
Expand Down
1 change: 0 additions & 1 deletion src/love/Types/Audio/SoundData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <stdlib.h>

#include "../../../ChaiLove.h"
#include "../../../LibretroLog.h"

namespace love {
namespace Types {
Expand Down
1 change: 0 additions & 1 deletion src/love/Types/Graphics/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "pntr_app.h"
#include <string>
#include "../../../ChaiLove.h"
#include "../../../LibretroLog.h"
#include "Image.h"

namespace love {
Expand Down
1 change: 0 additions & 1 deletion src/love/Types/Graphics/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <string>
#include "../../../ChaiLove.h"
#include "../../../LibretroLog.h"

namespace love {
namespace Types {
Expand Down
1 change: 0 additions & 1 deletion src/love/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "TinySHA1.hpp"
#include <cppcodec/base64_default_rfc4648.hpp>
#include <cppcodec/hex_default_lower.hpp>
#include "../LibretroLog.h"

namespace love {

Expand Down
24 changes: 10 additions & 14 deletions src/love/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "physfs.h"
#include "filesystem.h"
#include "../ChaiLove.h"
#include "../LibretroLog.h"
#include "Types/FileSystem/FileInfo.h"

using love::Types::FileSystem::FileInfo;
Expand All @@ -15,15 +14,7 @@ namespace love {
/**
* Initialize the file system.
*/
bool filesystem::init(const std::string& file, const void* data) {
// Initialize PhysFS
if (PHYSFS_isInit() == 0) {
if (PHYSFS_init((const char*)ChaiLove::getInstance()->environ_cb) == 0) {
//LibretroLog::log(RETRO_LOG_ERROR) << "[ChaiLove] [filesystem] Error loading PhysFS - " << getLastError() << std::endl;
return false;
}
}

bool filesystem::init(const std::string& file, const void* data, unsigned int dataSize) {
// Check if we are simply running ChaiLove.
if (file.empty()) {
return mount(".", "/", false);
Expand All @@ -34,7 +25,7 @@ bool filesystem::init(const std::string& file, const void* data) {

// Allow loading from an Archive.
if (extension == "chaigame" || extension == "chailove" || extension == "zip") {
return mount(file.c_str(), "/", false);
return mount(data, dataSize, "/");
}

// If we are just running the core, load the base path.
Expand Down Expand Up @@ -143,9 +134,6 @@ PHYSFS_sint64 filesystem::getSize(PHYSFS_File* file) {
}

bool filesystem::unload() {
if (PHYSFS_isInit() != 0) {
PHYSFS_deinit();
}
return true;
}

Expand Down Expand Up @@ -262,6 +250,14 @@ bool filesystem::mount(const std::string& archive, const std::string& mountpoint
return mount(archive, mountpoint, true);
}

bool filesystem::mount(const void *data, unsigned int size, const std::string& mountpoint) {
// TODO: Do we need to register del() callback?
if (PHYSFS_mountMemory(data, (PHYSFS_uint64)size, NULL, "chailove.zip", mountpoint.c_str(), 1) == 0) {
return false;
}
return true;
}

bool filesystem::mount(const std::string& archive, const std::string& mountpoint, bool appendToPath) {
// Protect against empty archive/mount points.
int append = appendToPath ? 1 : 0;
Expand Down
3 changes: 2 additions & 1 deletion src/love/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class filesystem {
bool load(const std::string& file);
void mountlibretro();

bool init(const std::string& file, const void* data);
bool init(const std::string& file, const void* data, unsigned int dataSize);
bool unload();
void* openRW(const std::string& filename);
char* readChar(const std::string& filename);
Expand Down Expand Up @@ -141,6 +141,7 @@ class filesystem {
bool mount(const std::string& archive, const std::string& mountpoint, bool appendToPath);
bool mount(const std::string& archive, const std::string& mountpoint);
bool mount(const char *archive, const std::string& mountpoint);
bool mount(const void *data, unsigned int size, const std::string& mountpoint);

/**
* Gets the path to the designated save directory.
Expand Down
1 change: 0 additions & 1 deletion src/love/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "pntr_app.h"
#include "font.h"
#include "Types/Graphics/Font.h"
#include "../LibretroLog.h"

namespace love {

Expand Down
1 change: 0 additions & 1 deletion src/love/graphics.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "graphics.h"

#include "../ChaiLove.h"
#include "../LibretroLog.h"
#include "Types/Graphics/Image.h"
#include "Types/Graphics/Font.h"
#include "Types/Graphics/Color.h"
Expand Down
4 changes: 4 additions & 0 deletions src/love/math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ double math::random(double min, double max) {
return (double)pntr_app_random_float(app, (float)min, (float)max);
}

double math::random(double max) {
return (double)pntr_app_random_float(app, 0.0f, (float)max);
}

math& math::setRandomSeed(int min, int max) {
return setRandomSeed(random(min, max));
}
Expand Down
1 change: 1 addition & 0 deletions src/love/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class math {
int random(int min, int max);
int random(int max);
double random(double min, double max);
double random(double max);

/**
* Sets the random seed to a random seed between the given min and max values.
Expand Down
3 changes: 1 addition & 2 deletions src/love/script.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "script.h"
#include "../ChaiLove.h"
#include "../LibretroLog.h"
#include <algorithm>

#ifdef __HAVE_CHAISCRIPT__
Expand Down Expand Up @@ -395,8 +394,8 @@ script::script(const std::string& file) {
chai.add(fun<int, math, int, int>(&math::random), "random");
chai.add(fun<double, math, double>(&math::random), "random");
chai.add(fun<double, math, double, double>(&math::random), "random");
chai.add(fun<love::math&, math, int, int>(&math::setRandomSeed), "setRandomSeed");
chai.add(fun<love::math&, math, int>(&math::setRandomSeed), "setRandomSeed");
chai.add(fun<love::math&, math, int, int>(&math::setRandomSeed), "setRandomSeed");
chai.add(fun(&math::getRandomSeed), "getRandomSeed");

// Data
Expand Down
1 change: 0 additions & 1 deletion src/love/system.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "system.h"
#include "../ChaiLove.h"
#include "../LibretroLog.h"

#include <string>
#include <cstdlib>
Expand Down
1 change: 0 additions & 1 deletion src/love/window.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "window.h"
#include <string>
#include "../ChaiLove.h"
#include "../LibretroLog.h"

#include "pntr_app.h"
#include "config.h"
Expand Down
81 changes: 30 additions & 51 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,91 +1,70 @@
#include <string>
#include <iostream> // TODO: Remove this

#define PNTR_PHYSFS_IMPLEMENTATION
#include "pntr_physfs.h"

#define PNTR_APP_IMPLEMENTATION
#define PNTR_ENABLE_DEFAULT_FONT
#define PNTR_ENABLE_TTF
//#define PNTR_ENABLE_VARGS
#define PNTR_ENABLE_MATH
#define PNTR_NO_STDIO
#define PNTR_NO_SAVE_IMAGE
#include "pntr_app.h"

#include "ChaiLove.h"

typedef struct AppData {
pntr_image* image;
ChaiLove* chailove;
} AppData;

bool Init(pntr_app* app) {
// Set up the initial app data.
AppData* appData = (AppData*)pntr_load_memory(sizeof(AppData));
pntr_app_set_userdata(app, appData);


ChaiLove::environ_cb = pntr_app_libretro_environ_cb(app);

// Initialize PhysFS
if (PHYSFS_init((const char*)pntr_app_libretro_environ_cb(app)) == 0) {
if (PHYSFS_init((const char*)ChaiLove::environ_cb) == 0) {
pntr_app_log(PNTR_APP_LOG_ERROR, "PHYSFS_init() failed");
return false;
}

// if (PHYSFS_mount("examples", "ex", 1) == 0) {
// pntr_app_log(PNTR_APP_LOG_ERROR, "PHYSFS_mount() failed");
// return false;
// };

// Load the given file.
unsigned int size;
void* fileData = pntr_app_load_arg_file(app, &size);
if (fileData == NULL) {
pntr_app_log(PNTR_APP_LOG_ERROR, "Failed to load file");
// Set up the chailove instance.
ChaiLove* chailove = ChaiLove::getInstance();
if (chailove == NULL) {
PHYSFS_deinit();
return false;
}

if (PHYSFS_mountMemory((const char*)fileData, (PHYSFS_uint64)size, &pntr_unload_memory, "chailove.zip", NULL, 1) == 0) {

pntr_unload_memory(fileData);
}
if (fileData) {
appData->image = pntr_load_image_from_memory(PNTR_IMAGE_TYPE_PNG, (const unsigned char*)fileData, size);
}
else {
pntr_app_log(PNTR_APP_LOG_ERROR, "Failed to load file");
// Load
std::string argFile = app->argFile == NULL ? "" : app->argFile;
if (!chailove->load(argFile, app->argFileData, app->argFileDataSize)) {
ChaiLove::destroy();
PHYSFS_deinit();
return false;
}
// Find the game path.
// std::string gamePath(info ? info->path : "");
// if (gamePath == ".") {
// gamePath = "main.chai";
// }
// void* data = NULL;
// if (info != NULL) {
// data = (void*)info->data;
// }
pntr_app_set_userdata(app, (void*)chailove);

return true;
}

bool Update(pntr_app* app, pntr_image* screen) {
AppData* appData = (AppData*)pntr_app_userdata(app);
ChaiLove* chailove = (ChaiLove*)pntr_app_userdata(app);
chailove->screen = screen;
if (chailove == NULL) {
return false;
}

// Clear the screen
pntr_clear_background(screen, PNTR_RAYWHITE);
// Update the game.
chailove->update();

pntr_draw_image(screen, appData->image, 0, 0);
if (chailove->event.m_shouldclose) {
return false;
}

// Draw some text
//pntr_draw_text(screen, appData->font, "Congrats! You created your first pntr_app!", 35, screen->height - 30, PNTR_DARKGRAY);
// Render the game.
chailove->draw();

return true;
}

void Close(pntr_app* app) {
AppData* appData = (AppData*)pntr_app_userdata(app);



ChaiLove::destroy();
PHYSFS_deinit();
}

pntr_app Main(int argc, char* argv[]) {
Expand All @@ -94,7 +73,7 @@ pntr_app Main(int argc, char* argv[]) {
return (pntr_app) {
.width = 640,
.height = 480,
.title = "libretro_chailove",
.title = "ChaiLove",
.init = Init,
.update = Update,
.close = Close,
Expand Down

0 comments on commit 4773c55

Please sign in to comment.