Verified Commit 2c4803d9 authored by RobLoach's avatar RobLoach
Browse files

feat: Add love.system.getUsername()

parent a8e37401
......@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- No Game demo when ChaiLove is loaded without content
- Version mismatch warning when content's ChaiLove version doesn't match core's
- `love.data.hash()` `sha1` support
- `love.system.getUsername()`
## 0.19.0 - 2018-04-13
### Added
......
......@@ -124,18 +124,8 @@ std::string ChaiLove::demo() {
}
bool ChaiLove::update() {
// Poll all the inputs.
ChaiLove::input_poll_cb();
// Poll all SDL events.
while (SDL_PollEvent(&sdlEvent)) {
switch (sdlEvent.type) {
case SDL_QUIT:
event.quit();
return !event.m_shouldclose;
break;
}
}
// Update and poll all the events.
event.update();
// Update the input systems.
mouse.update();
......@@ -145,7 +135,7 @@ bool ChaiLove::update() {
// Step forward the timer, and update the game.
script->update(timer.getDelta());
return !event.m_shouldclose;
return event.m_shouldclose;
}
/**
......@@ -161,21 +151,23 @@ void ChaiLove::reset() {
* Render the ChaiLove.
*/
void ChaiLove::draw() {
if (!event.m_shouldclose) {
// Clear the screen.
graphics.clear();
if (event.m_shouldclose) {
return;
}
// Render the game.
script->draw();
// Clear the screen.
graphics.clear();
// Render the in-game console.
console.draw();
// Render the game.
script->draw();
// Flip the buffer.
if (SDL_Flip(screen) == -1) {
std::string out("[ChaiLove] Failed to swap the buffers: ");
std::cout << out << SDL_GetError() << std::endl;
}
// Render the in-game console.
console.draw();
// Flip the buffer.
if (SDL_Flip(screen) == -1) {
std::string out("[ChaiLove] Failed to swap the buffers: ");
std::cout << out << SDL_GetError() << std::endl;
}
}
......
......@@ -100,7 +100,6 @@ class ChaiLove {
uint32_t *videoBuffer;
SDL_Surface* screen;
SDL_Event sdlEvent;
};
#endif // SRC_CHAILOVE_H_
......@@ -10,7 +10,7 @@ const char *retro_save_directory;
const char *retro_system_directory;
const char *retro_content_directory;
static bool use_audio_cb;
int16_t audio_buffer[1470]; // 2 * (44100 / 60)
int16_t audio_buffer[2 * (44100 / 60)];
static retro_video_refresh_t video_cb;
retro_audio_sample_t audio_cb;
static retro_audio_sample_batch_t audio_batch_cb;
......@@ -38,7 +38,7 @@ void retro_set_input_state(retro_input_state_t cb) {
static void emit_audio(void) {
ChaiLove* app = ChaiLove::getInstance();
app->audio.mixer_render(audio_buffer);
audio_batch_cb(audio_buffer, 735); // 44100 / 60
audio_batch_cb(audio_buffer, 44100 / 60);
}
static void audio_set_state(bool enable) {
......@@ -463,7 +463,7 @@ void retro_run(void) {
}
// Update the game.
if (!app->update()) {
if (app->update()) {
ChaiLove::environ_cb(RETRO_ENVIRONMENT_SHUTDOWN, 0);
return;
}
......
#include "event.h"
#include "../ChaiLove.h"
#include <SDL.h>
namespace love {
void event::quit() {
m_shouldclose = true;
}
void event::update() {
// Poll all the inputs.
ChaiLove::input_poll_cb();
// Poll all SDL events.
while (SDL_PollEvent(&sdlEvent)) {
switch (sdlEvent.type) {
case SDL_QUIT:
quit();
break;
}
}
}
} // namespace love
#ifndef SRC_LOVE_EVENT_H_
#define SRC_LOVE_EVENT_H_
#include <SDL.h>
namespace love {
/**
* Manages events, like keypresses.
......@@ -11,7 +13,13 @@ class event {
* Exits the ChaiLove application.
*/
void quit();
void update();
bool m_shouldclose = false;
private:
SDL_Event sdlEvent;
};
} // namespace love
......
......@@ -19,7 +19,14 @@ class filesystem {
/**
* Loads and runs a .chai file.
*
* @param file The name (and path) of the file.
* @param file The name (and path) of the file. Having the .chai extension at the end is optional.
*
* ### Example
*
* @code
* love.filesystem.load("myscript.chai")
* love.filesystem.load("myotherscript")
* @endcode
*
* @return Whether or not the file was loaded and ran properly.
*/
......@@ -66,11 +73,15 @@ class filesystem {
/**
* Unmounts a zip file or folder previously mounted with filesystem::unmount.
*
* @return bool True, when unmounting was a success.
*/
bool unmount(const std::string& archive);
/**
* Mounts a zip file or folder in the game's save directory for reading.
*
* @return bool True, when mounting was a success.
*/
bool mount(const std::string& archive, const std::string& mountpoint);
......
......@@ -278,6 +278,7 @@ script::script(const std::string& file) {
chai.add(fun(&system::getOS), "getOS");
chai.add(fun(&system::getVersion), "getVersion");
chai.add(fun(&system::getVersionString), "getVersionString");
chai.add(fun(&system::getUsername), "getUsername");
// Mouse
chai.add(fun(&mouse::getX), "getX");
......
......@@ -2,11 +2,17 @@
#include "../ChaiLove.h"
#include <string>
#include <cstring>
#include <iostream>
#include "semver.h"
#include "libretro.h"
namespace love {
system::system() {
m_username = "";
}
std::string system::getOS() {
// TODO(RobLoach): Replace Macros with something more complex?
#if (defined __HAIKU__)
......@@ -105,4 +111,15 @@ bool system::load(config& t) {
return true;
}
std::string system::getUsername() {
if (!m_usernameInitialized) {
m_usernameInitialized = true;
const char *username = NULL;
if (ChaiLove::environ_cb(RETRO_ENVIRONMENT_GET_USERNAME, &username) && username) {
m_username = std::string(username);
}
}
return m_username;
}
} // namespace love
......@@ -58,6 +58,8 @@ namespace love {
*/
class system {
public:
system();
/**
* Gets the current operating system.
*
......@@ -83,6 +85,16 @@ class system {
*/
std::string getVersionString();
/**
* Gets the current user's name, if available. When not available, will return an empty string.
*
* @return The username of the current user, or an empty string otherwise.
*/
std::string getUsername();
std::string m_username;
bool m_usernameInitialized = false;
bool load(config& t);
};
......
......@@ -9,7 +9,6 @@
using ::ChaiLove;
using love::Types::Graphics::Point;
using std::string;
namespace love {
......@@ -65,13 +64,14 @@ bool window::unload() {
return true;
}
string window::getTitle() {
std::string window::getTitle() {
char* titleChar;
SDL_WM_GetCaption(&titleChar, NULL);
return std::string(titleChar);
}
window& window::setTitle(string title) {
window& window::setTitle(const std::string& title) {
ChaiLove::getInstance()->config.window.title = title;
SDL_WM_SetCaption(title.c_str(), 0);
return *this;
}
......
......@@ -33,7 +33,7 @@ class window {
*
* @see love.window.getTitle
*/
window& setTitle(std::string title);
window& setTitle(const std::string& title);
/**
* Display a simple message for a certain amount of frames.
......
......@@ -44,6 +44,8 @@ def draw() {
love.graphics.setColor(255, 255, 255)
love.graphics.print("Test " + to_string(currenttest + 1) + "/" + to_string(tests.size()) + ": " + tests[currenttest].name, 5, 5);
love.graphics.print("FPS " + to_string(love.timer.getFPS()), 5, 20);
love.graphics.print("Username: " + love.system.getUsername(), 5, 35)
}
def update(delta) {
......
......@@ -6,3 +6,6 @@ assert_equal(version[1], '.', "love.system.getVersionString()")
var versionNumber = love.system.getVersion()
assert_equal(versionNumber[0], 0, "love.system.getVersion()")
var username = love.system.getUsername()
assert(true, "love.system.getUsername() == '" + username + "'")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment