Format code

This commit is contained in:
Maschell 2020-07-05 13:34:13 +02:00
parent 39ce864c70
commit 349de9b582
37 changed files with 887 additions and 939 deletions

View File

@ -33,13 +33,7 @@ bool Application::exitApplication = false;
bool Application::quitRequest = false; bool Application::quitRequest = false;
Application::Application() Application::Application()
: CThread(CThread::eAttributeAffCore1 | CThread::eAttributePinnedAff, 0, 0x20000) : CThread(CThread::eAttributeAffCore1 | CThread::eAttributePinnedAff, 0, 0x20000), bgMusic(NULL), video(NULL), mainWindow(NULL), fontSystem(NULL), exitCode(0) {
, bgMusic(NULL)
, video(NULL)
, mainWindow(NULL)
, fontSystem(NULL)
, exitCode(0)
{
controller[0] = new VPadController(GuiTrigger::CHANNEL_1); controller[0] = new VPadController(GuiTrigger::CHANNEL_1);
controller[1] = new WPadController(GuiTrigger::CHANNEL_2); controller[1] = new WPadController(GuiTrigger::CHANNEL_2);
controller[2] = new WPadController(GuiTrigger::CHANNEL_3); controller[2] = new WPadController(GuiTrigger::CHANNEL_3);
@ -52,87 +46,82 @@ Application::Application()
bgMusic->Play(); bgMusic->Play();
bgMusic->SetVolume(50); bgMusic->SetVolume(50);
exitApplication = false; exitApplication = false;
ProcUIInit(OSSavesDone_ReadyToRelease); ProcUIInit(OSSavesDone_ReadyToRelease);
} }
Application::~Application() Application::~Application() {
{
log_printf("Destroy music\n"); log_printf("Destroy music\n");
delete bgMusic; delete bgMusic;
log_printf("Destroy controller\n"); log_printf("Destroy controller\n");
for(int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
delete controller[i]; delete controller[i];
log_printf("Destroy async deleter\n"); log_printf("Destroy async deleter\n");
AsyncDeleter::destroyInstance(); AsyncDeleter::destroyInstance();
log_printf("Clear resources\n"); log_printf("Clear resources\n");
Resources::Clear(); Resources::Clear();
log_printf("Stop sound handler\n"); log_printf("Stop sound handler\n");
SoundHandler::DestroyInstance(); SoundHandler::DestroyInstance();
ProcUIShutdown(); ProcUIShutdown();
} }
int Application::exec() int Application::exec() {
{
//! start main GX2 thread //! start main GX2 thread
resumeThread(); resumeThread();
//! now wait for thread to finish //! now wait for thread to finish
shutdownThread(); shutdownThread();
return exitCode; return exitCode;
} }
void Application::quit(int code) void Application::quit(int code) {
{
exitCode = code; exitCode = code;
exitApplication = true; exitApplication = true;
quitRequest = true; quitRequest = true;
} }
void Application::fadeOut() void Application::fadeOut() {
{ GuiImage fadeOut(video->getTvWidth(), video->getTvHeight(), (GX2Color) {0, 0, 0, 255});
GuiImage fadeOut(video->getTvWidth(), video->getTvHeight(), (GX2Color){ 0, 0, 0, 255 });
for(int i = 0; i < 255; i += 10) for (int i = 0; i < 255; i += 10) {
{ if (i > 255)
if(i > 255)
i = 255; i = 255;
fadeOut.setAlpha(i / 255.0f); fadeOut.setAlpha(i / 255.0f);
//! start rendering DRC //! start rendering DRC
video->prepareDrcRendering(); video->prepareDrcRendering();
mainWindow->drawDrc(video); mainWindow->drawDrc(video);
GX2SetDepthOnlyControl(GX2_DISABLE, GX2_DISABLE, GX2_COMPARE_FUNC_ALWAYS); GX2SetDepthOnlyControl(GX2_DISABLE, GX2_DISABLE, GX2_COMPARE_FUNC_ALWAYS);
fadeOut.draw(video); fadeOut.draw(video);
GX2SetDepthOnlyControl(GX2_ENABLE, GX2_ENABLE, GX2_COMPARE_FUNC_LEQUAL); GX2SetDepthOnlyControl(GX2_ENABLE, GX2_ENABLE, GX2_COMPARE_FUNC_LEQUAL);
video->drcDrawDone(); video->drcDrawDone();
//! start rendering TV //! start rendering TV
video->prepareTvRendering(); video->prepareTvRendering();
mainWindow->drawTv(video); mainWindow->drawTv(video);
GX2SetDepthOnlyControl(GX2_DISABLE, GX2_DISABLE, GX2_COMPARE_FUNC_ALWAYS); GX2SetDepthOnlyControl(GX2_DISABLE, GX2_DISABLE, GX2_COMPARE_FUNC_ALWAYS);
fadeOut.draw(video); fadeOut.draw(video);
GX2SetDepthOnlyControl(GX2_ENABLE, GX2_ENABLE, GX2_COMPARE_FUNC_LEQUAL); GX2SetDepthOnlyControl(GX2_ENABLE, GX2_ENABLE, GX2_COMPARE_FUNC_LEQUAL);
video->tvDrawDone(); video->tvDrawDone();
//! as last point update the effects as it can drop elements //! as last point update the effects as it can drop elements
mainWindow->updateEffects(); mainWindow->updateEffects();
video->waitForVSync(); video->waitForVSync();
} }
//! one last cleared black screen //! one last cleared black screen
@ -145,98 +134,84 @@ void Application::fadeOut()
video->drcEnable(false); video->drcEnable(false);
} }
bool Application::procUI(void) bool Application::procUI(void) {
{
bool executeProcess = false; bool executeProcess = false;
switch(ProcUIProcessMessages(true)) switch (ProcUIProcessMessages(true)) {
{ case PROCUI_STATUS_EXITING: {
case PROCUI_STATUS_EXITING: log_printf("PROCUI_STATUS_EXITING\n");
{ exitCode = EXIT_SUCCESS;
log_printf("PROCUI_STATUS_EXITING\n"); exitApplication = true;
exitCode = EXIT_SUCCESS; break;
exitApplication = true;
break;
}
case PROCUI_STATUS_RELEASE_FOREGROUND:
{
log_printf("PROCUI_STATUS_RELEASE_FOREGROUND\n");
if(video != NULL)
{
// we can turn of the screen but we don't need to and it will display the last image
video->tvEnable(true);
video->drcEnable(true);
log_printf("delete fontSystem\n");
delete fontSystem;
fontSystem = NULL;
log_printf("delete video\n");
delete video;
video = NULL;
log_printf("deinitialze memory\n");
memoryRelease();
ProcUIDrawDoneRelease();
} }
else case PROCUI_STATUS_RELEASE_FOREGROUND: {
{ log_printf("PROCUI_STATUS_RELEASE_FOREGROUND\n");
ProcUIDrawDoneRelease(); if (video != NULL) {
} // we can turn of the screen but we don't need to and it will display the last image
break; video->tvEnable(true);
} video->drcEnable(true);
case PROCUI_STATUS_IN_FOREGROUND:
{
if(!quitRequest)
{
if(video == NULL)
{
log_printf("PROCUI_STATUS_IN_FOREGROUND\n");
log_printf("initialze memory\n");
memoryInitialize();
log_printf("Initialize video\n"); log_printf("delete fontSystem\n");
video = new CVideo(GX2_TV_SCAN_MODE_720P, GX2_DRC_RENDER_MODE_SINGLE); delete fontSystem;
log_printf("Video size %i x %i\n", video->getTvWidth(), video->getTvHeight()); fontSystem = NULL;
//! setup default Font log_printf("delete video\n");
log_printf("Initialize main font system\n"); delete video;
FreeTypeGX *fontSystem = new FreeTypeGX(Resources::GetFile("font.ttf"), Resources::GetFileSize("font.ttf"), true); video = NULL;
GuiText::setPresetFont(fontSystem);
if(mainWindow == NULL)
{
log_printf("Initialize main window\n");
mainWindow = new MainWindow(video->getTvWidth(), video->getTvHeight());
}
log_printf("deinitialze memory\n");
memoryRelease();
ProcUIDrawDoneRelease();
} else {
ProcUIDrawDoneRelease();
} }
executeProcess = true; break;
} }
break; case PROCUI_STATUS_IN_FOREGROUND: {
} if (!quitRequest) {
case PROCUI_STATUS_IN_BACKGROUND: if (video == NULL) {
default: log_printf("PROCUI_STATUS_IN_FOREGROUND\n");
break; log_printf("initialze memory\n");
memoryInitialize();
log_printf("Initialize video\n");
video = new CVideo(GX2_TV_SCAN_MODE_720P, GX2_DRC_RENDER_MODE_SINGLE);
log_printf("Video size %i x %i\n", video->getTvWidth(), video->getTvHeight());
//! setup default Font
log_printf("Initialize main font system\n");
FreeTypeGX *fontSystem = new FreeTypeGX(Resources::GetFile("font.ttf"), Resources::GetFileSize("font.ttf"), true);
GuiText::setPresetFont(fontSystem);
if (mainWindow == NULL) {
log_printf("Initialize main window\n");
mainWindow = new MainWindow(video->getTvWidth(), video->getTvHeight());
}
}
executeProcess = true;
}
break;
}
case PROCUI_STATUS_IN_BACKGROUND:
default:
break;
} }
return executeProcess; return executeProcess;
} }
void Application::executeThread(void) void Application::executeThread(void) {
{
log_printf("Entering main loop\n"); log_printf("Entering main loop\n");
//! main GX2 loop (60 Hz cycle with max priority on core 1) //! main GX2 loop (60 Hz cycle with max priority on core 1)
while(!exitApplication) while (!exitApplication) {
{ if (procUI() == false)
if(procUI() == false) continue;
continue;
//! Read out inputs //! Read out inputs
for(int i = 0; i < 5; i++) for (int i = 0; i < 5; i++) {
{ if (controller[i]->update(video->getTvWidth(), video->getTvHeight()) == false)
if(controller[i]->update(video->getTvWidth(), video->getTvHeight()) == false)
continue; continue;
//! update controller states //! update controller states
@ -246,35 +221,34 @@ void Application::executeThread(void)
mainWindow->process(); mainWindow->process();
//! start rendering DRC //! start rendering DRC
video->prepareDrcRendering(); video->prepareDrcRendering();
mainWindow->drawDrc(video); mainWindow->drawDrc(video);
video->drcDrawDone(); video->drcDrawDone();
//! start rendering TV //! start rendering TV
video->prepareTvRendering(); video->prepareTvRendering();
mainWindow->drawTv(video); mainWindow->drawTv(video);
video->tvDrawDone(); video->tvDrawDone();
//! enable screen after first frame render //! enable screen after first frame render
if(video->getFrameCount() == 0) { if (video->getFrameCount() == 0) {
video->tvEnable(true); video->tvEnable(true);
video->drcEnable(true); video->drcEnable(true);
} }
//! as last point update the effects as it can drop elements //! as last point update the effects as it can drop elements
mainWindow->updateEffects(); mainWindow->updateEffects();
video->waitForVSync(); video->waitForVSync();
//! transfer elements to real delete list here after all processes are finished //! transfer elements to real delete list here after all processes are finished
//! the elements are transfered to another list to delete the elements in a separate thread //! the elements are transfered to another list to delete the elements in a separate thread
//! and avoid blocking the GUI thread //! and avoid blocking the GUI thread
AsyncDeleter::triggerDeleteProcess(); AsyncDeleter::triggerDeleteProcess();
} }
//! in case we exit to a homebrew let's smoothly fade out //! in case we exit to a homebrew let's smoothly fade out
if(video) if (video) {
{
fadeOut(); fadeOut();
} }

View File

@ -24,16 +24,16 @@
// forward declaration // forward declaration
class FreeTypeGX; class FreeTypeGX;
class Application : public CThread class Application : public CThread {
{
public: public:
static Application * instance() { static Application *instance() {
if(!applicationInstance) if (!applicationInstance)
applicationInstance = new Application(); applicationInstance = new Application();
return applicationInstance; return applicationInstance;
} }
static void destroyInstance() { static void destroyInstance() {
if(applicationInstance) { if (applicationInstance) {
delete applicationInstance; delete applicationInstance;
applicationInstance = NULL; applicationInstance = NULL;
} }
@ -42,6 +42,7 @@ public:
CVideo *getVideo(void) const { CVideo *getVideo(void) const {
return video; return video;
} }
MainWindow *getMainWindow(void) const { MainWindow *getMainWindow(void) const {
return mainWindow; return mainWindow;
} }
@ -51,12 +52,14 @@ public:
} }
int exec(void); int exec(void);
void fadeOut(void); void fadeOut(void);
void quit(int code); void quit(int code);
private: private:
Application(); Application();
virtual ~Application(); virtual ~Application();
bool procUI(void); bool procUI(void);

View File

@ -2,10 +2,9 @@
#include <sysapp/launch.h> #include <sysapp/launch.h>
#include "main.h" #include "main.h"
int main(int argc, char **argv) int main(int argc, char **argv) {
{
//! ******************************************************************* //! *******************************************************************
//! * Jump to our application * //! * Jump to our application *
//! ******************************************************************* //! *******************************************************************
return Menu_Main(); return Menu_Main();
} }

View File

@ -11,12 +11,12 @@ CFile::CFile() {
pos = 0; pos = 0;
} }
CFile::CFile(const std::string & filepath, eOpenTypes mode) { CFile::CFile(const std::string &filepath, eOpenTypes mode) {
iFd = -1; iFd = -1;
this->open(filepath, mode); this->open(filepath, mode);
} }
CFile::CFile(const uint8_t * mem, int32_t size) { CFile::CFile(const uint8_t *mem, int32_t size) {
iFd = -1; iFd = -1;
this->open(mem, size); this->open(mem, size);
} }
@ -25,25 +25,25 @@ CFile::~CFile() {
this->close(); this->close();
} }
int32_t CFile::open(const std::string & filepath, eOpenTypes mode) { int32_t CFile::open(const std::string &filepath, eOpenTypes mode) {
this->close(); this->close();
int32_t openMode = 0; int32_t openMode = 0;
switch(mode) { switch (mode) {
default: default:
case ReadOnly: case ReadOnly:
openMode = O_RDONLY; openMode = O_RDONLY;
break; break;
case WriteOnly: case WriteOnly:
openMode = O_WRONLY; openMode = O_WRONLY;
break; break;
case ReadWrite: case ReadWrite:
openMode = O_RDWR; openMode = O_RDWR;
break; break;
case Append: case Append:
openMode = O_APPEND | O_WRONLY; openMode = O_APPEND | O_WRONLY;
break; break;
} }
//! Using fopen works only on the first launch as expected //! Using fopen works only on the first launch as expected
@ -51,7 +51,7 @@ int32_t CFile::open(const std::string & filepath, eOpenTypes mode) {
//! the .data sections which is needed for a normal application to re-init //! the .data sections which is needed for a normal application to re-init
//! this will be added with launching as RPX //! this will be added with launching as RPX
iFd = ::open(filepath.c_str(), openMode); iFd = ::open(filepath.c_str(), openMode);
if(iFd < 0) if (iFd < 0)
return iFd; return iFd;
@ -61,7 +61,7 @@ int32_t CFile::open(const std::string & filepath, eOpenTypes mode) {
return 0; return 0;
} }
int32_t CFile::open(const uint8_t * mem, int32_t size) { int32_t CFile::open(const uint8_t *mem, int32_t size) {
this->close(); this->close();
mem_file = mem; mem_file = mem;
@ -71,7 +71,7 @@ int32_t CFile::open(const uint8_t * mem, int32_t size) {
} }
void CFile::close() { void CFile::close() {
if(iFd >= 0) if (iFd >= 0)
::close(iFd); ::close(iFd);
iFd = -1; iFd = -1;
@ -80,24 +80,24 @@ void CFile::close() {
pos = 0; pos = 0;
} }
int32_t CFile::read(uint8_t * ptr, size_t size) { int32_t CFile::read(uint8_t *ptr, size_t size) {
if(iFd >= 0) { if (iFd >= 0) {
int32_t ret = ::read(iFd, ptr,size); int32_t ret = ::read(iFd, ptr, size);
if(ret > 0) if (ret > 0)
pos += ret; pos += ret;
return ret; return ret;
} }
int32_t readsize = size; int32_t readsize = size;
if(readsize > (int64_t) (filesize-pos)) if (readsize > (int64_t) (filesize - pos))
readsize = filesize-pos; readsize = filesize - pos;
if(readsize <= 0) if (readsize <= 0)
return readsize; return readsize;
if(mem_file != NULL) { if (mem_file != NULL) {
memcpy(ptr, mem_file+pos, readsize); memcpy(ptr, mem_file + pos, readsize);
pos += readsize; pos += readsize;
return readsize; return readsize;
} }
@ -105,12 +105,12 @@ int32_t CFile::read(uint8_t * ptr, size_t size) {
return -1; return -1;
} }
int32_t CFile::write(const uint8_t * ptr, size_t size) { int32_t CFile::write(const uint8_t *ptr, size_t size) {
if(iFd >= 0) { if (iFd >= 0) {
size_t done = 0; size_t done = 0;
while(done < size) { while (done < size) {
int32_t ret = ::write(iFd, ptr, size - done); int32_t ret = ::write(iFd, ptr, size - done);
if(ret <= 0) if (ret <= 0)
return ret; return ret;
ptr += ret; ptr += ret;
@ -127,25 +127,25 @@ int32_t CFile::seek(long int offset, int32_t origin) {
int32_t ret = 0; int32_t ret = 0;
int64_t newPos = pos; int64_t newPos = pos;
if(origin == SEEK_SET) { if (origin == SEEK_SET) {
newPos = offset; newPos = offset;
} else if(origin == SEEK_CUR) { } else if (origin == SEEK_CUR) {
newPos += offset; newPos += offset;
} else if(origin == SEEK_END) { } else if (origin == SEEK_END) {
newPos = filesize+offset; newPos = filesize + offset;
} }
if(newPos < 0) { if (newPos < 0) {
pos = 0; pos = 0;
} else { } else {
pos = newPos; pos = newPos;
} }
if(iFd >= 0) if (iFd >= 0)
ret = ::lseek(iFd, pos, SEEK_SET); ret = ::lseek(iFd, pos, SEEK_SET);
if(mem_file != NULL) { if (mem_file != NULL) {
if(pos > filesize) { if (pos > filesize) {
pos = filesize; pos = filesize;
} }
} }
@ -160,8 +160,8 @@ int32_t CFile::fwrite(const char *format, ...) {
va_list va; va_list va;
va_start(va, format); va_start(va, format);
if((vsprintf(tmp, format, va) >= 0)) { if ((vsprintf(tmp, format, va) >= 0)) {
result = this->write((uint8_t *)tmp, strlen(tmp)); result = this->write((uint8_t *) tmp, strlen(tmp));
} }
va_end(va); va_end(va);

View File

@ -18,18 +18,22 @@ public:
}; };
CFile(); CFile();
CFile(const std::string & filepath, eOpenTypes mode);
CFile(const uint8_t * memory, int32_t memsize); CFile(const std::string &filepath, eOpenTypes mode);
CFile(const uint8_t *memory, int32_t memsize);
virtual ~CFile(); virtual ~CFile();
int32_t open(const std::string & filepath, eOpenTypes mode); int32_t open(const std::string &filepath, eOpenTypes mode);
int32_t open(const uint8_t * memory, int32_t memsize);
int32_t open(const uint8_t *memory, int32_t memsize);
BOOL isOpen() const { BOOL isOpen() const {
if(iFd >= 0) if (iFd >= 0)
return true; return true;
if(mem_file) if (mem_file)
return true; return true;
return false; return false;
@ -37,23 +41,29 @@ public:
void close(); void close();
int32_t read(uint8_t * ptr, size_t size); int32_t read(uint8_t *ptr, size_t size);
int32_t write(const uint8_t * ptr, size_t size);
int32_t write(const uint8_t *ptr, size_t size);
int32_t fwrite(const char *format, ...); int32_t fwrite(const char *format, ...);
int32_t seek(long int offset, int32_t origin); int32_t seek(long int offset, int32_t origin);
uint64_t tell() { uint64_t tell() {
return pos; return pos;
}; };
uint64_t size() { uint64_t size() {
return filesize; return filesize;
}; };
void rewind() { void rewind() {
this->seek(0, SEEK_SET); this->seek(0, SEEK_SET);
}; };
protected: protected:
int32_t iFd; int32_t iFd;
const uint8_t * mem_file; const uint8_t *mem_file;
uint64_t filesize; uint64_t filesize;
uint64_t pos; uint64_t pos;
}; };

View File

@ -42,7 +42,7 @@ DirList::DirList() {
Depth = 0; Depth = 0;
} }
DirList::DirList(const std::string & path, const char *filter, uint32_t flags, uint32_t maxDepth) { DirList::DirList(const std::string &path, const char *filter, uint32_t flags, uint32_t maxDepth) {
this->LoadPath(path, filter, flags, maxDepth); this->LoadPath(path, filter, flags, maxDepth);
this->SortList(); this->SortList();
} }
@ -51,8 +51,8 @@ DirList::~DirList() {
ClearList(); ClearList();
} }
BOOL DirList::LoadPath(const std::string & folder, const char *filter, uint32_t flags, uint32_t maxDepth) { BOOL DirList::LoadPath(const std::string &folder, const char *filter, uint32_t flags, uint32_t maxDepth) {
if(folder.empty()) return false; if (folder.empty()) return false;
Flags = flags; Flags = flags;
Filter = filter; Filter = filter;
@ -65,11 +65,11 @@ BOOL DirList::LoadPath(const std::string & folder, const char *filter, uint32_t
StringTools::RemoveDoubleSlashs(folderpath); StringTools::RemoveDoubleSlashs(folderpath);
//! remove last slash if exists //! remove last slash if exists
if(length > 0 && folderpath[length-1] == '/') if (length > 0 && folderpath[length - 1] == '/')
folderpath.erase(length-1); folderpath.erase(length - 1);
//! add root slash if missing //! add root slash if missing
if(folderpath.find('/') == std::string::npos) { if (folderpath.find('/') == std::string::npos) {
folderpath += '/'; folderpath += '/';
} }
@ -77,7 +77,7 @@ BOOL DirList::LoadPath(const std::string & folder, const char *filter, uint32_t
} }
BOOL DirList::InternalLoadPath(std::string &folderpath) { BOOL DirList::InternalLoadPath(std::string &folderpath) {
if(folderpath.size() < 3) if (folderpath.size() < 3)
return false; return false;
struct dirent *dirent = NULL; struct dirent *dirent = NULL;
@ -91,13 +91,13 @@ BOOL DirList::InternalLoadPath(std::string &folderpath) {
BOOL isDir = dirent->d_type & DT_DIR; BOOL isDir = dirent->d_type & DT_DIR;
const char *filename = dirent->d_name; const char *filename = dirent->d_name;
if(isDir) { if (isDir) {
if(strcmp(filename,".") == 0 || strcmp(filename,"..") == 0) if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0)
continue; continue;
if((Flags & CheckSubfolders) && (Depth > 0)) { if ((Flags & CheckSubfolders) && (Depth > 0)) {
int32_t length = folderpath.size(); int32_t length = folderpath.size();
if(length > 2 && folderpath[length-1] != '/') { if (length > 2 && folderpath[length - 1] != '/') {
folderpath += '/'; folderpath += '/';
} }
folderpath += filename; folderpath += filename;
@ -108,18 +108,18 @@ BOOL DirList::InternalLoadPath(std::string &folderpath) {
Depth++; Depth++;
} }
if(!(Flags & Dirs)) if (!(Flags & Dirs))
continue; continue;
} else if(!(Flags & Files)) { } else if (!(Flags & Files)) {
continue; continue;
} }
if(Filter) { if (Filter) {
char * fileext = strrchr(filename, '.'); char *fileext = strrchr(filename, '.');
if(!fileext) if (!fileext)
continue; continue;
if(StringTools::strtokcmp(fileext, Filter, ",") == 0) if (StringTools::strtokcmp(fileext, Filter, ",") == 0)
AddEntrie(folderpath, filename, isDir); AddEntrie(folderpath, filename, isDir);
} else { } else {
AddEntrie(folderpath, filename, isDir); AddEntrie(folderpath, filename, isDir);
@ -130,16 +130,16 @@ BOOL DirList::InternalLoadPath(std::string &folderpath) {
return true; return true;
} }
void DirList::AddEntrie(const std::string &filepath, const char * filename, BOOL isDir) { void DirList::AddEntrie(const std::string &filepath, const char *filename, BOOL isDir) {
if(!filename) if (!filename)
return; return;
int32_t pos = FileInfo.size(); int32_t pos = FileInfo.size();
FileInfo.resize(pos+1); FileInfo.resize(pos + 1);
FileInfo[pos].FilePath = (char *) malloc(filepath.size()+strlen(filename)+2); FileInfo[pos].FilePath = (char *) malloc(filepath.size() + strlen(filename) + 2);
if(!FileInfo[pos].FilePath) { if (!FileInfo[pos].FilePath) {
FileInfo.resize(pos); FileInfo.resize(pos);
return; return;
} }
@ -149,8 +149,8 @@ void DirList::AddEntrie(const std::string &filepath, const char * filename, BOOL
} }
void DirList::ClearList() { void DirList::ClearList() {
for(uint32_t i = 0; i < FileInfo.size(); ++i) { for (uint32_t i = 0; i < FileInfo.size(); ++i) {
if(FileInfo[i].FilePath) { if (FileInfo[i].FilePath) {
free(FileInfo[i].FilePath); free(FileInfo[i].FilePath);
FileInfo[i].FilePath = NULL; FileInfo[i].FilePath = NULL;
} }
@ -160,33 +160,33 @@ void DirList::ClearList() {
std::vector<DirEntry>().swap(FileInfo); std::vector<DirEntry>().swap(FileInfo);
} }
const char * DirList::GetFilename(int32_t ind) const { const char *DirList::GetFilename(int32_t ind) const {
if (!valid(ind)) if (!valid(ind))
return ""; return "";
return StringTools::FullpathToFilename(FileInfo[ind].FilePath); return StringTools::FullpathToFilename(FileInfo[ind].FilePath);
} }
static BOOL SortCallback(const DirEntry & f1, const DirEntry & f2) { static BOOL SortCallback(const DirEntry &f1, const DirEntry &f2) {
if(f1.isDir && !(f2.isDir)) return true; if (f1.isDir && !(f2.isDir)) return true;
if(!(f1.isDir) && f2.isDir) return false; if (!(f1.isDir) && f2.isDir) return false;
if(f1.FilePath && !f2.FilePath) return true; if (f1.FilePath && !f2.FilePath) return true;
if(!f1.FilePath) return false; if (!f1.FilePath) return false;
if(strcasecmp(f1.FilePath, f2.FilePath) > 0) if (strcasecmp(f1.FilePath, f2.FilePath) > 0)
return false; return false;
return true; return true;
} }
void DirList::SortList() { void DirList::SortList() {
if(FileInfo.size() > 1) if (FileInfo.size() > 1)
std::sort(FileInfo.begin(), FileInfo.end(), SortCallback); std::sort(FileInfo.begin(), FileInfo.end(), SortCallback);
} }
void DirList::SortList(BOOL (*SortFunc)(const DirEntry &a, const DirEntry &b)) { void DirList::SortList(BOOL (*SortFunc)(const DirEntry &a, const DirEntry &b)) {
if(FileInfo.size() > 1) if (FileInfo.size() > 1)
std::sort(FileInfo.begin(), FileInfo.end(), SortFunc); std::sort(FileInfo.begin(), FileInfo.end(), SortFunc);
} }
@ -194,14 +194,14 @@ uint64_t DirList::GetFilesize(int32_t index) const {
struct stat st; struct stat st;
const char *path = GetFilepath(index); const char *path = GetFilepath(index);
if(!path || stat(path, &st) != 0) if (!path || stat(path, &st) != 0)
return 0; return 0;
return st.st_size; return st.st_size;
} }
int32_t DirList::GetFileIndex(const char *filename) const { int32_t DirList::GetFileIndex(const char *filename) const {
if(!filename) if (!filename)
return -1; return -1;
for (uint32_t i = 0; i < FileInfo.size(); ++i) { for (uint32_t i = 0; i < FileInfo.size(); ++i) {

View File

@ -32,7 +32,7 @@
#include <wut_types.h> #include <wut_types.h>
typedef struct { typedef struct {
char * FilePath; char *FilePath;
BOOL isDir; BOOL isDir;
} DirEntry; } DirEntry;
@ -40,42 +40,54 @@ class DirList {
public: public:
//!Constructor //!Constructor
DirList(void); DirList(void);
//!\param path Path from where to load the filelist of all files //!\param path Path from where to load the filelist of all files
//!\param filter A fileext that needs to be filtered //!\param filter A fileext that needs to be filtered
//!\param flags search/filter flags from the enum //!\param flags search/filter flags from the enum
DirList(const std::string & path, const char *filter = NULL, uint32_t flags = Files | Dirs, uint32_t maxDepth = 0xffffffff); DirList(const std::string &path, const char *filter = NULL, uint32_t flags = Files | Dirs, uint32_t maxDepth = 0xffffffff);
//!Destructor //!Destructor
virtual ~DirList(); virtual ~DirList();
//! Load all the files from a directory //! Load all the files from a directory
BOOL LoadPath(const std::string & path, const char *filter = NULL, uint32_t flags = Files | Dirs, uint32_t maxDepth = 0xffffffff); BOOL LoadPath(const std::string &path, const char *filter = NULL, uint32_t flags = Files | Dirs, uint32_t maxDepth = 0xffffffff);
//! Get a filename of the list //! Get a filename of the list
//!\param list index //!\param list index
const char * GetFilename(int32_t index) const; const char *GetFilename(int32_t index) const;
//! Get the a filepath of the list //! Get the a filepath of the list
//!\param list index //!\param list index
const char *GetFilepath(int32_t index) const { const char *GetFilepath(int32_t index) const {
if (!valid(index)) return ""; if (!valid(index)) return "";
else return FileInfo[index].FilePath; else return FileInfo[index].FilePath;
} }
//! Get the a filesize of the list //! Get the a filesize of the list
//!\param list index //!\param list index
uint64_t GetFilesize(int32_t index) const; uint64_t GetFilesize(int32_t index) const;
//! Is index a dir or a file //! Is index a dir or a file
//!\param list index //!\param list index
BOOL IsDir(int32_t index) const { BOOL IsDir(int32_t index) const {
if(!valid(index)) return false; if (!valid(index)) return false;
return FileInfo[index].isDir; return FileInfo[index].isDir;
}; };
//! Get the filecount of the whole list //! Get the filecount of the whole list
int32_t GetFilecount() const { int32_t GetFilecount() const {
return FileInfo.size(); return FileInfo.size();
}; };
//! Sort list by filepath //! Sort list by filepath
void SortList(); void SortList();
//! Custom sort command for custom sort functions definitions //! Custom sort command for custom sort functions definitions
void SortList(BOOL (*SortFunc)(const DirEntry &a, const DirEntry &b)); void SortList(BOOL (*SortFunc)(const DirEntry &a, const DirEntry &b));
//! Get the index of the specified filename //! Get the index of the specified filename
int32_t GetFileIndex(const char *filename) const; int32_t GetFileIndex(const char *filename) const;
//! Enum for search/filter flags //! Enum for search/filter flags
enum { enum {
Files = 0x01, Files = 0x01,
@ -85,10 +97,13 @@ public:
protected: protected:
// Internal parser // Internal parser
BOOL InternalLoadPath(std::string &path); BOOL InternalLoadPath(std::string &path);
//!Add a list entrie //!Add a list entrie
void AddEntrie(const std::string &filepath, const char * filename, BOOL isDir); void AddEntrie(const std::string &filepath, const char *filename, BOOL isDir);
//! Clear the list //! Clear the list
void ClearList(); void ClearList();
//! Check if valid pos is requested //! Check if valid pos is requested
inline BOOL valid(uint32_t pos) const { inline BOOL valid(uint32_t pos) const {
return (pos < FileInfo.size()); return (pos < FileInfo.size());

View File

@ -10,7 +10,7 @@
int32_t FSUtils::LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_t *size) { int32_t FSUtils::LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_t *size) {
//! always initialze input //! always initialze input
*inbuffer = NULL; *inbuffer = NULL;
if(size) if (size)
*size = 0; *size = 0;
int32_t iFd = open(filepath, O_RDONLY); int32_t iFd = open(filepath, O_RDONLY);
@ -30,12 +30,12 @@ int32_t FSUtils::LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_
uint32_t done = 0; uint32_t done = 0;
int32_t readBytes = 0; int32_t readBytes = 0;
while(done < filesize) { while (done < filesize) {
if(done + blocksize > filesize) { if (done + blocksize > filesize) {
blocksize = filesize - done; blocksize = filesize - done;
} }
readBytes = read(iFd, buffer + done, blocksize); readBytes = read(iFd, buffer + done, blocksize);
if(readBytes <= 0) if (readBytes <= 0)
break; break;
done += readBytes; done += readBytes;
} }
@ -51,27 +51,27 @@ int32_t FSUtils::LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_
*inbuffer = buffer; *inbuffer = buffer;
//! sign is optional input //! sign is optional input
if(size) { if (size) {
*size = filesize; *size = filesize;
} }
return filesize; return filesize;
} }
int32_t FSUtils::CheckFile(const char * filepath) { int32_t FSUtils::CheckFile(const char *filepath) {
if(!filepath) if (!filepath)
return 0; return 0;
struct stat filestat; struct stat filestat;
char dirnoslash[strlen(filepath)+2]; char dirnoslash[strlen(filepath) + 2];
snprintf(dirnoslash, sizeof(dirnoslash), "%s", filepath); snprintf(dirnoslash, sizeof(dirnoslash), "%s", filepath);
while(dirnoslash[strlen(dirnoslash)-1] == '/') while (dirnoslash[strlen(dirnoslash) - 1] == '/')
dirnoslash[strlen(dirnoslash)-1] = '\0'; dirnoslash[strlen(dirnoslash) - 1] = '\0';
char * notRoot = strrchr(dirnoslash, '/'); char *notRoot = strrchr(dirnoslash, '/');
if(!notRoot) { if (!notRoot) {
strcat(dirnoslash, "/"); strcat(dirnoslash, "/");
} }
@ -81,29 +81,29 @@ int32_t FSUtils::CheckFile(const char * filepath) {
return 0; return 0;
} }
int32_t FSUtils::CreateSubfolder(const char * fullpath) { int32_t FSUtils::CreateSubfolder(const char *fullpath) {
if(!fullpath) if (!fullpath)
return 0; return 0;
int32_t result = 0; int32_t result = 0;
char dirnoslash[strlen(fullpath)+1]; char dirnoslash[strlen(fullpath) + 1];
strcpy(dirnoslash, fullpath); strcpy(dirnoslash, fullpath);
int32_t pos = strlen(dirnoslash)-1; int32_t pos = strlen(dirnoslash) - 1;
while(dirnoslash[pos] == '/') { while (dirnoslash[pos] == '/') {
dirnoslash[pos] = '\0'; dirnoslash[pos] = '\0';
pos--; pos--;
} }
if(CheckFile(dirnoslash)) { if (CheckFile(dirnoslash)) {
return 1; return 1;
} else { } else {
char parentpath[strlen(dirnoslash)+2]; char parentpath[strlen(dirnoslash) + 2];
strcpy(parentpath, dirnoslash); strcpy(parentpath, dirnoslash);
char * ptr = strrchr(parentpath, '/'); char *ptr = strrchr(parentpath, '/');
if(!ptr) { if (!ptr) {
//!Device root directory (must be with '/') //!Device root directory (must be with '/')
strcat(parentpath, "/"); strcat(parentpath, "/");
struct stat filestat; struct stat filestat;
@ -119,7 +119,7 @@ int32_t FSUtils::CreateSubfolder(const char * fullpath) {
result = CreateSubfolder(parentpath); result = CreateSubfolder(parentpath);
} }
if(!result) if (!result)
return 0; return 0;
if (mkdir(dirnoslash, 0777) == -1) { if (mkdir(dirnoslash, 0777) == -1) {
@ -129,15 +129,15 @@ int32_t FSUtils::CreateSubfolder(const char * fullpath) {
return 1; return 1;
} }
BOOL FSUtils::saveBufferToFile(const char * path, void * buffer, uint32_t size) { BOOL FSUtils::saveBufferToFile(const char *path, void *buffer, uint32_t size) {
int32_t res = open(path, O_CREAT | O_TRUNC | O_WRONLY); int32_t res = open(path, O_CREAT | O_TRUNC | O_WRONLY);
close(res); close(res);
CFile file(path, CFile::WriteOnly); CFile file(path, CFile::WriteOnly);
if (!file.isOpen()) { if (!file.isOpen()) {
DEBUG_FUNCTION_LINE("Failed to open %s\n",path); DEBUG_FUNCTION_LINE("Failed to open %s\n", path);
return false; return false;
} }
file.write((const uint8_t*) buffer,size); file.write((const uint8_t *) buffer, size);
file.close(); file.close();
return true; return true;
} }

View File

@ -8,9 +8,11 @@ public:
static int32_t LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_t *size); static int32_t LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_t *size);
//! todo: C++ class //! todo: C++ class
static int32_t CreateSubfolder(const char * fullpath); static int32_t CreateSubfolder(const char *fullpath);
static int32_t CheckFile(const char * filepath);
static BOOL saveBufferToFile(const char * path, void * buffer, uint32_t size); static int32_t CheckFile(const char *filepath);
static BOOL saveBufferToFile(const char *path, void *buffer, uint32_t size);
}; };
#endif // __FS_UTILS_H_ #endif // __FS_UTILS_H_

View File

@ -23,9 +23,8 @@
* Constructor for the GuiButton class. * Constructor for the GuiButton class.
*/ */
Background::Background(char *picture, float scroll_speed) Background::Background(char *picture, float scroll_speed) {
{ loc_x = 0.0f;
loc_x=0.0f;
bgImg = Resources::GetImageData(picture); bgImg = Resources::GetImageData(picture);
@ -35,48 +34,44 @@ Background::Background(char *picture, float scroll_speed)
bg->setPosition(0, 0); bg->setPosition(0, 0);
bgs->setPosition(-1280, 0); bgs->setPosition(-1280, 0);
speed=scroll_speed; speed = scroll_speed;
} }
/** /**
* Destructor for the GuiButton class. * Destructor for the GuiButton class.
*/ */
Background::~Background() Background::~Background() {
{ speed = 0.0f;
speed=0.0f; loc_x = 0.0f;
loc_x=0.0f; delete (bgImg);
delete(bgImg); delete (bg);
delete(bg); delete (bgs);
delete(bgs);
} }
void Background::setScrollSpeed(float scroll_speed) void Background::setScrollSpeed(float scroll_speed) {
{ speed = scroll_speed;
speed=scroll_speed;
} }
/** /**
* Draw the button on screen * Draw the button on screen
*/ */
void Background::draw(CVideo *v) void Background::draw(CVideo *v) {
{ if (speed > 0.0f) {
if(speed>0.0f) { loc_x -= speed;
loc_x-=speed; if (loc_x < 0.0f) loc_x = 1280.0f;
if(loc_x<0.0f) loc_x=1280.0f;
bg->setPosition((int)loc_x, 0); bg->setPosition((int) loc_x, 0);
bgs->setPosition((int)loc_x-1280, 0); bgs->setPosition((int) loc_x - 1280, 0);
} }
if(!this->isVisible()) if (!this->isVisible())
return; return;
// draw images // draw images
bg->draw(v); bg->draw(v);
bgs->draw(v); bgs->draw(v);
} }
void Background::update(GuiController * c) void Background::update(GuiController *c) {
{
} }

View File

@ -25,20 +25,24 @@
#include "gui/GuiTrigger.h" #include "gui/GuiTrigger.h"
//!Display, manage, and manipulate buttons in the GUI. Buttons can have images, icons, text, and sound set (all of which are optional) //!Display, manage, and manipulate buttons in the GUI. Buttons can have images, icons, text, and sound set (all of which are optional)
class Background : public GuiElement class Background : public GuiElement {
{ public:
public: Background(char *picture, float scroll_speed);
Background(char *picture, float scroll_speed);
virtual ~Background(); virtual ~Background();
void setScrollSpeed(float scroll_speed);
void draw(CVideo *video); void setScrollSpeed(float scroll_speed);
void update(GuiController * c);
protected: void draw(CVideo *video);
GuiImageData* bgImg;
GuiImage * bg; void update(GuiController *c);
GuiImage * bgs;
float speed; protected:
float loc_x; GuiImageData *bgImg;
GuiImage *bg;
GuiImage *bgs;
float speed;
float loc_x;
}; };
#endif #endif

View File

@ -24,10 +24,9 @@
* Constructor for the GuiButton class. * Constructor for the GuiButton class.
*/ */
Pipe::Pipe(float x) Pipe::Pipe(float x) {
{
srand(OSGetTime()); srand(OSGetTime());
loc_y=rand() % 250 - 125; loc_y = rand() % 250 - 125;
loc_x = x; loc_x = x;
pipeBottomImg = Resources::GetImageData("pipe_down.png"); pipeBottomImg = Resources::GetImageData("pipe_down.png");
pipeTopImg = Resources::GetImageData("pipe_up.png"); pipeTopImg = Resources::GetImageData("pipe_up.png");
@ -37,48 +36,45 @@ Pipe::Pipe(float x)
pipebottom->setAlignment(ALIGN_CENTER | ALIGN_CENTER); pipebottom->setAlignment(ALIGN_CENTER | ALIGN_CENTER);
pipetop->setAlignment(ALIGN_CENTER | ALIGN_CENTER); pipetop->setAlignment(ALIGN_CENTER | ALIGN_CENTER);
pipebottom->setPosition((int)loc_x,-410-loc_y); pipebottom->setPosition((int) loc_x, -410 - loc_y);
pipetop->setPosition((int)loc_x,410-loc_y); pipetop->setPosition((int) loc_x, 410 - loc_y);
scroll_state=false; scroll_state = false;
} }
/** /**
* Destructor for the GuiButton class. * Destructor for the GuiButton class.
*/ */
Pipe::~Pipe() Pipe::~Pipe() {
{ loc_x = 0.0f;
loc_x=0.0f; loc_y = 0.0f;
loc_y=0.0f; scroll_state = 0;
scroll_state=0; delete (pipetop);
delete(pipetop); delete (pipebottom);
delete(pipebottom); delete (pipeTopImg);
delete(pipeTopImg); delete (pipeBottomImg);
delete(pipeBottomImg);
} }
void Pipe::setPosX(float x) void Pipe::setPosX(float x) {
{ loc_x = x;
loc_x=x; pipebottom->setPosition((int) loc_x, -410 - loc_y);
pipebottom->setPosition((int)loc_x,-410-loc_y); pipetop->setPosition((int) loc_x, 410 - loc_y);
pipetop->setPosition((int)loc_x,410-loc_y);
} }
void Pipe::setScroll(bool state) void Pipe::setScroll(bool state) {
{ scroll_state = state;
scroll_state=state;
} }
int Pipe::checkCollision(GuiImage* check) int Pipe::checkCollision(GuiImage *check) {
{ if ((pipetop->getLeft() - pipetop->getWidth() / 4.0f) < (check->getLeft() + check->getWidth() / 2.0f) &&
if((pipetop->getLeft()-pipetop->getWidth()/4.0f)<(check->getLeft()+check->getWidth()/2.0f) && (pipetop->getLeft()+pipetop->getWidth()/2)>(check->getLeft()-check->getWidth()/2.0f)) { //How can that thing work? No, really... (pipetop->getLeft() + pipetop->getWidth() / 2) > (check->getLeft() - check->getWidth() / 2.0f)) { //How can that thing work? No, really...
poss_collision=1; poss_collision = 1;
if((check->getTop()+check->getHeight()/2.0f)>(pipetop->getTop()-720.0f) || ((check->getTop()-check->getHeight()/2.0f))<pipebottom->getTop()) { if ((check->getTop() + check->getHeight() / 2.0f) > (pipetop->getTop() - 720.0f) || ((check->getTop() - check->getHeight() / 2.0f)) < pipebottom->getTop()) {
poss_collision=0; //we already collided poss_collision = 0; //we already collided
return 1; return 1;
} }
} else if (poss_collision==1) { //We just surpassed the problem } else if (poss_collision == 1) { //We just surpassed the problem
poss_collision=0; poss_collision = 0;
return -1; //Add point return -1; //Add point
} }
return 0; return 0;
@ -88,27 +84,25 @@ int Pipe::checkCollision(GuiImage* check)
/** /**
* Draw the button on screen * Draw the button on screen
*/ */
void Pipe::draw(CVideo *v) void Pipe::draw(CVideo *v) {
{ if (scroll_state == true) {
if(scroll_state==true) { loc_x -= 1.5f;
loc_x-=1.5f; if (loc_x < -640.0f) {
if (loc_x<-640.0f) { loc_x = 700.0f;
loc_x=700.0f; loc_y = rand() % 250 - 125;
loc_y=rand() % 250 - 125;
}
pipebottom->setPosition((int)loc_x,-410-loc_y);
pipetop->setPosition((int)loc_x,410-loc_y);
} }
pipebottom->setPosition((int) loc_x, -410 - loc_y);
pipetop->setPosition((int) loc_x, 410 - loc_y);
}
if(!this->isVisible()) if (!this->isVisible())
return; return;
// draw images // draw images
pipebottom->draw(v); pipebottom->draw(v);
pipetop->draw(v); pipetop->draw(v);
} }
void Pipe::update(GuiController * c) void Pipe::update(GuiController *c) {
{
} }

View File

@ -25,25 +25,31 @@
#include "gui/GuiTrigger.h" #include "gui/GuiTrigger.h"
//!Display, manage, and manipulate buttons in the GUI. Buttons can have images, icons, text, and sound set (all of which are optional) //!Display, manage, and manipulate buttons in the GUI. Buttons can have images, icons, text, and sound set (all of which are optional)
class Pipe : public GuiElement class Pipe : public GuiElement {
{ public:
public: Pipe(float x);
Pipe(float x);
virtual ~Pipe(); virtual ~Pipe();
void setPosX(float x);
void setScroll(bool state); void setPosX(float x);
int checkCollision(GuiImage* check);
void draw(CVideo *video); void setScroll(bool state);
void update(GuiController * c);
protected: int checkCollision(GuiImage *check);
GuiImageData* pipeBottomImg;
GuiImageData* pipeTopImg; void draw(CVideo *video);
GuiImage * pipebottom;
GuiImage * pipetop; void update(GuiController *c);
float loc_x;
int loc_y; protected:
int poss_collision=0; GuiImageData *pipeBottomImg;
bool scroll_state; GuiImageData *pipeTopImg;
GuiImage *pipebottom;
GuiImage *pipetop;
float loc_x;
int loc_y;
int poss_collision = 0;
bool scroll_state;
}; };
#endif #endif

View File

@ -23,8 +23,7 @@
* Constructor for the GuiButton class. * Constructor for the GuiButton class.
*/ */
ScoreImage::ScoreImage(int x, int y) ScoreImage::ScoreImage(int x, int y) {
{
digitsImagesData[0] = Resources::GetImageData("font_big_0.png"); digitsImagesData[0] = Resources::GetImageData("font_big_0.png");
digitsImagesData[1] = Resources::GetImageData("font_big_1.png"); digitsImagesData[1] = Resources::GetImageData("font_big_1.png");
digitsImagesData[2] = Resources::GetImageData("font_big_2.png"); digitsImagesData[2] = Resources::GetImageData("font_big_2.png");
@ -37,10 +36,10 @@ ScoreImage::ScoreImage(int x, int y)
digitsImagesData[9] = Resources::GetImageData("font_big_9.png"); digitsImagesData[9] = Resources::GetImageData("font_big_9.png");
digitsImagesData[10] = Resources::GetImageData("font_big_null.png"); digitsImagesData[10] = Resources::GetImageData("font_big_null.png");
for (int i=0;i<3;i++) { for (int i = 0; i < 3; i++) {
digitsImages[i] = new GuiImage(digitsImagesData[10]); digitsImages[i] = new GuiImage(digitsImagesData[10]);
digitsImages[i]->setAlignment(ALIGN_CENTER | ALIGN_CENTER); digitsImages[i]->setAlignment(ALIGN_CENTER | ALIGN_CENTER);
digitsImages[i]->setPosition(x+(i*36),y); digitsImages[i]->setPosition(x + (i * 36), y);
} }
digitsImages[2]->setImageData(digitsImagesData[0]); digitsImages[2]->setImageData(digitsImagesData[0]);
@ -49,40 +48,36 @@ ScoreImage::ScoreImage(int x, int y)
/** /**
* Destructor for the GuiButton class. * Destructor for the GuiButton class.
*/ */
ScoreImage::~ScoreImage() ScoreImage::~ScoreImage() {
{
//for (int i=0;i<11;i++) delete(digitsImagesData[i]); //for (int i=0;i<11;i++) delete(digitsImagesData[i]);
} }
void ScoreImage::setScore(int score) void ScoreImage::setScore(int score) {
{
//Yeah, that's hacky and there are unusefull functions :P //Yeah, that's hacky and there are unusefull functions :P
if (score>999) score=999; //That's unlikey but... if (score > 999) score = 999; //That's unlikey but...
for(int a=0;a<3;a++) digits[a]=10; //Initialize digits to 10 (=don't draw) for (int a = 0; a < 3; a++) digits[a] = 10; //Initialize digits to 10 (=don't draw)
digits[0]=(int)(score/pow(10, 0))%10; digits[0] = (int) (score / pow(10, 0)) % 10;
if (score>9) digits[1]=(int)(score/pow(10, 1))%10; if (score > 9) digits[1] = (int) (score / pow(10, 1)) % 10;
if (score>99) digits[2]=(int)(score/pow(10, 2))%10; if (score > 99) digits[2] = (int) (score / pow(10, 2)) % 10;
for(int a=0; a<3; a++) digitsImages[a]->setImageData(digitsImagesData[digits[2-a]]); for (int a = 0; a < 3; a++) digitsImages[a]->setImageData(digitsImagesData[digits[2 - a]]);
} }
/** /**
* Draw the button on screen * Draw the button on screen
*/ */
void ScoreImage::draw(CVideo *v) void ScoreImage::draw(CVideo *v) {
{ if (!this->isVisible())
if(!this->isVisible()) return;
return;
// draw images // draw images
digitsImages[0]->draw(v); digitsImages[0]->draw(v);
digitsImages[1]->draw(v); digitsImages[1]->draw(v);
digitsImages[2]->draw(v); digitsImages[2]->draw(v);
} }
void ScoreImage::update(GuiController * c) void ScoreImage::update(GuiController *c) {
{
} }

View File

@ -25,18 +25,22 @@
#include "gui/GuiTrigger.h" #include "gui/GuiTrigger.h"
//!Display, manage, and manipulate buttons in the GUI. Buttons can have images, icons, text, and sound set (all of which are optional) //!Display, manage, and manipulate buttons in the GUI. Buttons can have images, icons, text, and sound set (all of which are optional)
class ScoreImage : public GuiElement class ScoreImage : public GuiElement {
{ public:
public: ScoreImage(int x, int y);
ScoreImage(int x, int y);
virtual ~ScoreImage(); virtual ~ScoreImage();
void setScore(int score);
void draw(CVideo *video); void setScore(int score);
void update(GuiController * c);
protected: void draw(CVideo *video);
GuiImageData* digitsImagesData[11]; //Our 10 numers + 1 null digit :P
GuiImage * digitsImages[3]; void update(GuiController *c);
int digits[3];
protected:
GuiImageData *digitsImagesData[11]; //Our 10 numers + 1 null digit :P
GuiImage *digitsImages[3];
int digits[3];
}; };
#endif #endif

View File

@ -22,8 +22,7 @@
* Constructor for the GuiButton class. * Constructor for the GuiButton class.
*/ */
SplashScreen::SplashScreen(GuiImageData * img) SplashScreen::SplashScreen(GuiImageData *img) {
{
img_real = new GuiImage(img); img_real = new GuiImage(img);
img_real->setAlignment(ALIGN_CENTER | ALIGN_CENTER); img_real->setAlignment(ALIGN_CENTER | ALIGN_CENTER);
img_real->setPosition(0, 0); img_real->setPosition(0, 0);
@ -32,61 +31,56 @@ SplashScreen::SplashScreen(GuiImageData * img)
/** /**
* Destructor for the GuiButton class. * Destructor for the GuiButton class.
*/ */
SplashScreen::~SplashScreen() SplashScreen::~SplashScreen() {
{ state = 0;
state=0; delete (img_real);
delete(img_real);
} }
void SplashScreen::setSplashImageData(GuiImageData * img) void SplashScreen::setSplashImageData(GuiImageData *img) {
{
img_real->setImageData(img); img_real->setImageData(img);
} }
void SplashScreen::FadeExit() void SplashScreen::FadeExit() {
{ trasparency = 1.0f;
trasparency=1.0f; state = 1;
state=1; img_real->setAlpha(trasparency);
img_real->setAlpha(trasparency);
} }
void SplashScreen::FadeEnter()
{ void SplashScreen::FadeEnter() {
trasparency=0.0f; trasparency = 0.0f;
state=2; state = 2;
img_real->setAlpha(trasparency); img_real->setAlpha(trasparency);
} }
/** /**
* Draw the button on screen * Draw the button on screen
*/ */
void SplashScreen::draw(CVideo *v) void SplashScreen::draw(CVideo *v) {
{ if (state != 0) {
if(state!=0) { switch (state) {
switch(state) { case 1:
case 1: if (trasparency > 0.0f) {
if (trasparency>0.0f) { trasparency = trasparency - 0.05f;
trasparency=trasparency-0.05f; img_real->setAlpha(trasparency);
img_real->setAlpha(trasparency); } else state = 0;
} else state=0; break;
break; case 2:
case 2: if (trasparency < 1.0f) {
if (trasparency<1.0f) { trasparency = trasparency + 0.05f;
trasparency=trasparency+0.05f; img_real->setAlpha(trasparency);
img_real->setAlpha(trasparency); } else state = 0;
} else state=0; break;
break;
}
} }
}
if(!this->isVisible()) if (!this->isVisible())
return; return;
// draw images // draw images
img_real->draw(v); img_real->draw(v);
} }
void SplashScreen::update(GuiController * c) void SplashScreen::update(GuiController *c) {
{
} }

View File

@ -25,21 +25,27 @@
#include "gui/GuiTrigger.h" #include "gui/GuiTrigger.h"
//!Display, manage, and manipulate buttons in the GUI. Buttons can have images, icons, text, and sound set (all of which are optional) //!Display, manage, and manipulate buttons in the GUI. Buttons can have images, icons, text, and sound set (all of which are optional)
class SplashScreen : public GuiElement class SplashScreen : public GuiElement {
{ public:
public: SplashScreen(GuiImageData *img);
SplashScreen(GuiImageData * img);
virtual ~SplashScreen(); virtual ~SplashScreen();
void setSplashImageData(GuiImageData * img);
void FadeExit(); void setSplashImageData(GuiImageData *img);
void FadeEnter();
void draw(CVideo *video); void FadeExit();
void update(GuiController * c);
protected: void FadeEnter();
GuiImage * img_real;
int state; void draw(CVideo *video);
float trasparency=1.0f;
int tmp_xpos; void update(GuiController *c);
protected:
GuiImage *img_real;
int state;
float trasparency = 1.0f;
int tmp_xpos;
}; };
#endif #endif

View File

@ -19,10 +19,10 @@
#include <gui/Gui.h> #include <gui/Gui.h>
class GuiMainWindowScreen : public GuiFrame class GuiMainWindowScreen : public GuiFrame {
{
public: public:
GuiMainWindowScreen(int w, int h) : GuiFrame(w, h) {} GuiMainWindowScreen(int w, int h) : GuiFrame(w, h) {}
virtual ~GuiMainWindowScreen() {} virtual ~GuiMainWindowScreen() {}
}; };

View File

@ -6,8 +6,7 @@
#include "utils/utils.h" #include "utils/utils.h"
/* Entry point */ /* Entry point */
extern "C" int Menu_Main(void) extern "C" int Menu_Main(void) {
{
//!******************************************************************* //!*******************************************************************
//! Initialize function pointers * //! Initialize function pointers *
//!******************************************************************* //!*******************************************************************

View File

@ -24,12 +24,9 @@
#include "system/AsyncDeleter.h" #include "system/AsyncDeleter.h"
MainWindow::MainWindow(int w, int h) MainWindow::MainWindow(int w, int h)
: width(w) : width(w), height(h) {
, height(h) for (int i = 0; i < 4; i++) {
{ std::string filename = StringTools::strfmt("player%i_point.png", i + 1);
for(int i = 0; i < 4; i++)
{
std::string filename = StringTools::strfmt("player%i_point.png", i+1);
pointerImgData[i] = Resources::GetImageData(filename.c_str()); pointerImgData[i] = Resources::GetImageData(filename.c_str());
pointerImg[i] = new GuiImage(pointerImgData[i]); pointerImg[i] = new GuiImage(pointerImgData[i]);
pointerImg[i]->setScale(1.5f); pointerImg[i]->setScale(1.5f);
@ -39,99 +36,80 @@ MainWindow::MainWindow(int w, int h)
SetupMainView(); SetupMainView();
} }
MainWindow::~MainWindow() MainWindow::~MainWindow() {
{
while(!tvElements.empty()) while (!tvElements.empty()) {
{
delete tvElements[0]; delete tvElements[0];
remove(tvElements[0]); remove(tvElements[0]);
} }
while(!drcElements.empty()) while (!drcElements.empty()) {
{
delete drcElements[0]; delete drcElements[0];
remove(drcElements[0]); remove(drcElements[0]);
} }
for(int i = 0; i < 4; i++) for (int i = 0; i < 4; i++) {
{
delete pointerImg[i]; delete pointerImg[i];
Resources::RemoveImageData(pointerImgData[i]); Resources::RemoveImageData(pointerImgData[i]);
} }
} }
void MainWindow::updateEffects() void MainWindow::updateEffects() {
{
//! dont read behind the initial elements in case one was added //! dont read behind the initial elements in case one was added
uint32_t tvSize = tvElements.size(); uint32_t tvSize = tvElements.size();
uint32_t drcSize = drcElements.size(); uint32_t drcSize = drcElements.size();
for(uint32_t i = 0; (i < drcSize) && (i < drcElements.size()); ++i) for (uint32_t i = 0; (i < drcSize) && (i < drcElements.size()); ++i) {
{
drcElements[i]->updateEffects(); drcElements[i]->updateEffects();
} }
//! only update TV elements that are not updated yet because they are on DRC //! only update TV elements that are not updated yet because they are on DRC
for(uint32_t i = 0; (i < tvSize) && (i < tvElements.size()); ++i) for (uint32_t i = 0; (i < tvSize) && (i < tvElements.size()); ++i) {
{
uint32_t n; uint32_t n;
for(n = 0; (n < drcSize) && (n < drcElements.size()); n++) for (n = 0; (n < drcSize) && (n < drcElements.size()); n++) {
{ if (tvElements[i] == drcElements[n])
if(tvElements[i] == drcElements[n])
break; break;
} }
if(n == drcElements.size()) if (n == drcElements.size()) {
{
tvElements[i]->updateEffects(); tvElements[i]->updateEffects();
} }
} }
} }
void MainWindow::process(){ void MainWindow::process() {
//! dont read behind the initial elements in case one was added //! dont read behind the initial elements in case one was added
uint32_t tvSize = tvElements.size(); uint32_t tvSize = tvElements.size();
uint32_t drcSize = drcElements.size(); uint32_t drcSize = drcElements.size();
for(uint32_t i = 0; (i < drcSize) && (i < drcElements.size()); ++i) for (uint32_t i = 0; (i < drcSize) && (i < drcElements.size()); ++i) {
{
drcElements[i]->process(); drcElements[i]->process();
} }
//! only update TV elements that are not updated yet because they are on DRC //! only update TV elements that are not updated yet because they are on DRC
for(uint32_t i = 0; (i < tvSize) && (i < tvElements.size()); ++i) for (uint32_t i = 0; (i < tvSize) && (i < tvElements.size()); ++i) {
{
uint32_t n; uint32_t n;
for(n = 0; (n < drcSize) && (n < drcElements.size()); n++) for (n = 0; (n < drcSize) && (n < drcElements.size()); n++) {
{ if (tvElements[i] == drcElements[n])
if(tvElements[i] == drcElements[n])
break; break;
} }
if(n == drcElements.size()) if (n == drcElements.size()) {
{
tvElements[i]->process(); tvElements[i]->process();
} }
} }
} }
void MainWindow::update(GuiController *controller) void MainWindow::update(GuiController *controller) {
{
//! dont read behind the initial elements in case one was added //! dont read behind the initial elements in case one was added
//uint32_t tvSize = tvElements.size(); //uint32_t tvSize = tvElements.size();
if(controller->chan & GuiTrigger::CHANNEL_1) if (controller->chan & GuiTrigger::CHANNEL_1) {
{
uint32_t drcSize = drcElements.size(); uint32_t drcSize = drcElements.size();
for(uint32_t i = 0; (i < drcSize) && (i < drcElements.size()); ++i) for (uint32_t i = 0; (i < drcSize) && (i < drcElements.size()); ++i) {
{
drcElements[i]->update(controller); drcElements[i]->update(controller);
} }
} } else {
else
{
uint32_t tvSize = tvElements.size(); uint32_t tvSize = tvElements.size();
for(uint32_t i = 0; (i < tvSize) && (i < tvElements.size()); ++i) for (uint32_t i = 0; (i < tvSize) && (i < tvElements.size()); ++i) {
{
tvElements[i]->update(controller); tvElements[i]->update(controller);
} }
} }
@ -151,8 +129,7 @@ void MainWindow::update(GuiController *controller)
// } // }
// } // }
if(controller->chanIdx >= 1 && controller->chanIdx <= 4 && controller->data.validPointer) if (controller->chanIdx >= 1 && controller->chanIdx <= 4 && controller->data.validPointer) {
{
int wpadIdx = controller->chanIdx - 1; int wpadIdx = controller->chanIdx - 1;
float posX = controller->data.x; float posX = controller->data.x;
float posY = controller->data.y; float posY = controller->data.y;
@ -162,17 +139,13 @@ void MainWindow::update(GuiController *controller)
} }
} }
void MainWindow::drawDrc(CVideo *video) void MainWindow::drawDrc(CVideo *video) {
{ for (uint32_t i = 0; i < drcElements.size(); ++i) {
for(uint32_t i = 0; i < drcElements.size(); ++i)
{
drcElements[i]->draw(video); drcElements[i]->draw(video);
} }
for(int i = 0; i < 4; i++) for (int i = 0; i < 4; i++) {
{ if (pointerValid[i]) {
if(pointerValid[i])
{
pointerImg[i]->setAlpha(0.5f); pointerImg[i]->setAlpha(0.5f);
pointerImg[i]->draw(video); pointerImg[i]->draw(video);
pointerImg[i]->setAlpha(1.0f); pointerImg[i]->setAlpha(1.0f);
@ -180,42 +153,35 @@ void MainWindow::drawDrc(CVideo *video)
} }
} }
void MainWindow::drawTv(CVideo *video) void MainWindow::drawTv(CVideo *video) {
{ for (uint32_t i = 0; i < tvElements.size(); ++i) {
for(uint32_t i = 0; i < tvElements.size(); ++i)
{
tvElements[i]->draw(video); tvElements[i]->draw(video);
} }
for(int i = 0; i < 4; i++) for (int i = 0; i < 4; i++) {
{ if (pointerValid[i]) {
if(pointerValid[i])
{
pointerImg[i]->draw(video); pointerImg[i]->draw(video);
pointerValid[i] = false; pointerValid[i] = false;
} }
} }
} }
void MainWindow::SetupMainView() void MainWindow::SetupMainView() {
{
//DrcFrame = new MainWindowDRC(width,height); //DrcFrame = new MainWindowDRC(width,height);
TvFrame = new MainWindowTV(width,height); TvFrame = new MainWindowTV(width, height);
DrcFrame = TvFrame; DrcFrame = TvFrame;
appendTv(TvFrame); appendTv(TvFrame);
appendDrc(DrcFrame); appendDrc(DrcFrame);
} }
void MainWindow::OnOpenEffectFinish(GuiElement *element) void MainWindow::OnOpenEffectFinish(GuiElement *element) {
{
//! once the menu is open reset its state and allow it to be "clicked/hold" //! once the menu is open reset its state and allow it to be "clicked/hold"
element->effectFinished.disconnect(this); element->effectFinished.disconnect(this);
element->clearState(GuiElement::STATE_DISABLED); element->clearState(GuiElement::STATE_DISABLED);
} }
void MainWindow::OnCloseEffectFinish(GuiElement *element) void MainWindow::OnCloseEffectFinish(GuiElement *element) {
{
//! remove element from draw list and push to delete queue //! remove element from draw list and push to delete queue
remove(element); remove(element);
AsyncDeleter::pushForDelete(element); AsyncDeleter::pushForDelete(element);

View File

@ -26,105 +26,97 @@
class CVideo; class CVideo;
class MainWindow : public sigslot::has_slots<> class MainWindow : public sigslot::has_slots<> {
{
public: public:
MainWindow(int w, int h); MainWindow(int w, int h);
virtual ~MainWindow(); virtual ~MainWindow();
void appendTv(GuiElement *e) void appendTv(GuiElement *e) {
{ if (!e)
if(!e)
return; return;
removeTv(e); removeTv(e);
tvElements.push_back(e); tvElements.push_back(e);
} }
void appendDrc(GuiElement *e)
{ void appendDrc(GuiElement *e) {
if(!e) if (!e)
return; return;
removeDrc(e); removeDrc(e);
drcElements.push_back(e); drcElements.push_back(e);
} }
void append(GuiElement *e) void append(GuiElement *e) {
{
appendTv(e); appendTv(e);
appendDrc(e); appendDrc(e);
} }
void insertTv(uint32_t pos, GuiElement *e) void insertTv(uint32_t pos, GuiElement *e) {
{ if (!e)
if(!e)
return; return;
removeTv(e); removeTv(e);
tvElements.insert(tvElements.begin() + pos, e); tvElements.insert(tvElements.begin() + pos, e);
} }
void insertDrc(uint32_t pos, GuiElement *e)
{ void insertDrc(uint32_t pos, GuiElement *e) {
if(!e) if (!e)
return; return;
removeDrc(e); removeDrc(e);
drcElements.insert(drcElements.begin() + pos, e); drcElements.insert(drcElements.begin() + pos, e);
} }
void insert(uint32_t pos, GuiElement *e) void insert(uint32_t pos, GuiElement *e) {
{
insertTv(pos, e); insertTv(pos, e);
insertDrc(pos, e); insertDrc(pos, e);
} }
void removeTv(GuiElement *e) void removeTv(GuiElement *e) {
{ for (uint32_t i = 0; i < tvElements.size(); ++i) {
for(uint32_t i = 0; i < tvElements.size(); ++i) if (e == tvElements[i]) {
{
if(e == tvElements[i])
{
tvElements.erase(tvElements.begin() + i); tvElements.erase(tvElements.begin() + i);
break; break;
} }
} }
} }
void removeDrc(GuiElement *e)
{ void removeDrc(GuiElement *e) {
for(uint32_t i = 0; i < drcElements.size(); ++i) for (uint32_t i = 0; i < drcElements.size(); ++i) {
{ if (e == drcElements[i]) {
if(e == drcElements[i])
{
drcElements.erase(drcElements.begin() + i); drcElements.erase(drcElements.begin() + i);
break; break;
} }
} }
} }
void remove(GuiElement *e) void remove(GuiElement *e) {
{
removeTv(e); removeTv(e);
removeDrc(e); removeDrc(e);
} }
void removeAll()
{ void removeAll() {
tvElements.clear(); tvElements.clear();
drcElements.clear(); drcElements.clear();
} }
void drawDrc(CVideo *video); void drawDrc(CVideo *video);
void drawTv(CVideo *video); void drawTv(CVideo *video);
void update(GuiController *controller); void update(GuiController *controller);
void updateEffects(); void updateEffects();
void process(); void process();
void lockGUI() void lockGUI() {
{
guiMutex.lock(); guiMutex.lock();
} }
void unlockGUI()
{ void unlockGUI() {
guiMutex.unlock(); guiMutex.unlock();
} }
@ -132,6 +124,7 @@ private:
void SetupMainView(void); void SetupMainView(void);
void OnOpenEffectFinish(GuiElement *element); void OnOpenEffectFinish(GuiElement *element);
void OnCloseEffectFinish(GuiElement *element); void OnCloseEffectFinish(GuiElement *element);
int width, height; int width, height;
@ -140,8 +133,8 @@ private:
GuiSound *ClickSound; GuiSound *ClickSound;
GuiMainWindowScreen * TvFrame; GuiMainWindowScreen *TvFrame;
GuiMainWindowScreen * DrcFrame; GuiMainWindowScreen *DrcFrame;
GuiImageData *pointerImgData[4]; GuiImageData *pointerImgData[4];
GuiImage *pointerImg[4]; GuiImage *pointerImg[4];

View File

@ -20,20 +20,14 @@
#include "resources/Resources.h" #include "resources/Resources.h"
MainWindowTV::MainWindowTV(int w, int h) MainWindowTV::MainWindowTV(int w, int h)
: GuiMainWindowScreen(w, h) : GuiMainWindowScreen(w, h), width(w), height(h), bgImageColor(w, h, (GX2Color) {0, 0, 0, 0}) {
, width(w) bgImageColor.setImageColor((GX2Color) {248, 248, 249, 255}, 0);
, height(h) bgImageColor.setImageColor((GX2Color) {248, 248, 249, 255}, 1);
,bgImageColor(w, h, (GX2Color){ 0, 0, 0, 0 }) bgImageColor.setImageColor((GX2Color) {248, 248, 249, 255}, 2);
{ bgImageColor.setImageColor((GX2Color) {248, 248, 249, 255}, 3);
bgImageColor.setImageColor((GX2Color){ 248, 248, 249, 255 }, 0);
bgImageColor.setImageColor((GX2Color){ 248, 248, 249, 255 }, 1);
bgImageColor.setImageColor((GX2Color){ 248, 248, 249, 255 }, 2);
bgImageColor.setImageColor((GX2Color){ 248, 248, 249, 255 }, 3);
append(&bgImageColor); append(&bgImageColor);
wingSound = new GuiSound(Resources::GetFile("sfx_wing.ogg"), Resources::GetFileSize("sfx_wing.ogg")); wingSound = new GuiSound(Resources::GetFile("sfx_wing.ogg"), Resources::GetFileSize("sfx_wing.ogg"));
pointSound = new GuiSound(Resources::GetFile("sfx_point.ogg"), Resources::GetFileSize("sfx_point.ogg")); pointSound = new GuiSound(Resources::GetFile("sfx_point.ogg"), Resources::GetFileSize("sfx_point.ogg"));
swooshSound = new GuiSound(Resources::GetFile("sfx_swooshing.ogg"), Resources::GetFileSize("sfx_swooshing.ogg")); swooshSound = new GuiSound(Resources::GetFile("sfx_swooshing.ogg"), Resources::GetFileSize("sfx_swooshing.ogg"));
@ -53,10 +47,10 @@ MainWindowTV::MainWindowTV(int w, int h)
//splash = new GuiImage(splashImgData); //splash = new GuiImage(splashImgData);
flappy_bird = new GuiImage(flappy_bird_frm[0]); flappy_bird = new GuiImage(flappy_bird_frm[0]);
for (int i=0;i<4;i++) pipes[i] = new Pipe(150.0f+((float)i*320.0f)); for (int i = 0; i < 4; i++) pipes[i] = new Pipe(150.0f + ((float) i * 320.0f));
bg = new Background("bg_back.png",0); bg = new Background("bg_back.png", 0);
fg = new Background("bg_front.png",0); fg = new Background("bg_front.png", 0);
score_img = new ScoreImage(-54, 200); score_img = new ScoreImage(-54, 200);
@ -70,7 +64,7 @@ MainWindowTV::MainWindowTV(int w, int h)
append(bg); append(bg);
append(flappy_bird); append(flappy_bird);
for (int i=0;i<4;i++) append(pipes[i]); for (int i = 0; i < 4; i++) append(pipes[i]);
append(fg); append(fg);
append(splash); append(splash);
@ -84,69 +78,68 @@ MainWindowTV::MainWindowTV(int w, int h)
//append(hello); //append(hello);
} }
MainWindowTV::~MainWindowTV() MainWindowTV::~MainWindowTV() {
{
remove(&bgImageColor); remove(&bgImageColor);
delete(wingSound); delete (wingSound);
delete(swooshSound); delete (swooshSound);
delete(dieSound); delete (dieSound);
//delete(bg); //delete(bg);
//for (int i=0;i<4;i++) delete pipes[i]; //for (int i=0;i<4;i++) delete pipes[i];
//pipes = NULL; //pipes = NULL;
//delete(fg); //delete(fg);
delete(splash); delete (splash);
delete(flappy_bird); delete (flappy_bird);
} }
void MainWindowTV::draw(CVideo *v){ void MainWindowTV::draw(CVideo *v) {
GuiMainWindowScreen::draw(v); GuiMainWindowScreen::draw(v);
//gets called on every frame for drawing //gets called on every frame for drawing
} }
void MainWindowTV::update(GuiController * c){ void MainWindowTV::update(GuiController *c) {
GuiMainWindowScreen::update(c); GuiMainWindowScreen::update(c);
//CONTROLLER UPDATE!!! //CONTROLLER UPDATE!!!
switch(game_state) { switch (game_state) {
case state_splash: //Start screen case state_splash: //Start screen
if(c->data.buttons_d & VPAD_BUTTON_A || c->data.buttons_d & VPAD_BUTTON_B) { if (c->data.buttons_d & VPAD_BUTTON_A || c->data.buttons_d & VPAD_BUTTON_B) {
splash->FadeExit(); splash->FadeExit();
append(score_img); append(score_img);
for(int i=0;i<4;i++) pipes[i]->setScroll(true); for (int i = 0; i < 4; i++) pipes[i]->setScroll(true);
bg->setScrollSpeed(0.5f); bg->setScrollSpeed(0.5f);
fg->setScrollSpeed(1.5f); fg->setScrollSpeed(1.5f);
flappy_bird_velocity = jump; //Jump after start flappy_bird_velocity = jump; //Jump after start
wingSound->Stop(); wingSound->Stop();
wingSound->Play(); wingSound->Play();
game_state=state_ingame; game_state = state_ingame;
} }
break; break;
case state_ingame: case state_ingame:
if(c->data.buttons_d & VPAD_BUTTON_A || c->data.buttons_d & VPAD_BUTTON_B) { if (c->data.buttons_d & VPAD_BUTTON_A || c->data.buttons_d & VPAD_BUTTON_B) {
flappy_bird_velocity = jump; flappy_bird_velocity = jump;
wingSound->Stop(); wingSound->Stop();
wingSound->Play(); wingSound->Play();
} }
break; break;
case state_gameover: case state_gameover:
if(dieSound->IsPlaying() == false && (c->data.buttons_d & VPAD_BUTTON_A || c->data.buttons_d & VPAD_BUTTON_B)) { if (dieSound->IsPlaying() == false && (c->data.buttons_d & VPAD_BUTTON_A || c->data.buttons_d & VPAD_BUTTON_B)) {
flappy_bird_position=-5; flappy_bird_position = -5;
flappy_bird_rotation = 0.0f; flappy_bird_rotation = 0.0f;
flappy_bird_velocity = 0.0f; flappy_bird_velocity = 0.0f;
flappy_bird->setPosition(-150, -5); flappy_bird->setPosition(-150, -5);
flappy_bird->setAngle(0.0f); flappy_bird->setAngle(0.0f);
score=0; score = 0;
score_img->setScore(score); score_img->setScore(score);
for (int i=0;i<4;i++) pipes[i]->setPosX(150.0f+((float)i*320.0f)); for (int i = 0; i < 4; i++) pipes[i]->setPosX(150.0f + ((float) i * 320.0f));
splash->setSplashImageData(splashImgData); splash->setSplashImageData(splashImgData);
splash->FadeEnter(); splash->FadeEnter();
swooshSound->Stop(); swooshSound->Stop();
swooshSound->Play(); swooshSound->Play();
game_state=state_splash; game_state = state_splash;
} }
break; break;
default: default:
@ -155,81 +148,81 @@ void MainWindowTV::update(GuiController * c){
} }
void MainWindowTV::process(){ void MainWindowTV::process() {
GuiMainWindowScreen::process(); GuiMainWindowScreen::process();
if (game_state!=2) { //If not game over if (game_state != 2) { //If not game over
if (++flappy_bird_change_skip==6) { //Animate picture if (++flappy_bird_change_skip == 6) { //Animate picture
flappy_bird_change_skip=0; flappy_bird_change_skip = 0;
if (++flappy_bird_curr_frame==4) flappy_bird_curr_frame=0; if (++flappy_bird_curr_frame == 4) flappy_bird_curr_frame = 0;
flappy_bird->setImageData(flappy_bird_frm[flappy_bird_curr_frame]); flappy_bird->setImageData(flappy_bird_frm[flappy_bird_curr_frame]);
} }
} }
switch(game_state) { switch (game_state) {
case state_ingame: case state_ingame:
flappy_bird_velocity += gravity; flappy_bird_velocity += gravity;
flappy_bird_position -= flappy_bird_velocity; flappy_bird_position -= flappy_bird_velocity;
if(flappy_bird_position<-224) flappy_bird_position=-224; if (flappy_bird_position < -224) flappy_bird_position = -224;
if(flappy_bird_position>320) flappy_bird_position=320; if (flappy_bird_position > 320) flappy_bird_position = 320;
flappy_bird->setPosition(-150, (int)flappy_bird_position); flappy_bird->setPosition(-150, (int) flappy_bird_position);
if ((((float)flappy_bird_velocity / 10.0f) * 90.0f)<90.0f) flappy_bird_rotation = (((float)flappy_bird_velocity / 10.0f) * 90.0f); if ((((float) flappy_bird_velocity / 10.0f) * 90.0f) < 90.0f) flappy_bird_rotation = (((float) flappy_bird_velocity / 10.0f) * 90.0f);
else flappy_bird_rotation = 90.0f; else flappy_bird_rotation = 90.0f;
flappy_bird->setAngle(flappy_bird_rotation); flappy_bird->setAngle(flappy_bird_rotation);
for (int i=0;i<4;i++) { for (int i = 0; i < 4; i++) {
collision_state[i]=pipes[i]->checkCollision(flappy_bird); collision_state[i] = pipes[i]->checkCollision(flappy_bird);
if(collision_state[i]!=0) { if (collision_state[i] != 0) {
if(collision_state[i]==-1) { if (collision_state[i] == -1) {
//Add point //Add point
score_img->setScore(++score); score_img->setScore(++score);
pointSound->Stop(); pointSound->Stop();
pointSound->Play(); pointSound->Play();
} }
if(collision_state[i]==1) { if (collision_state[i] == 1) {
bg->setScrollSpeed(0.0f); bg->setScrollSpeed(0.0f);
fg->setScrollSpeed(0.0f); fg->setScrollSpeed(0.0f);
for(int i=0;i<4;i++) pipes[i]->setScroll(false); for (int i = 0; i < 4; i++) pipes[i]->setScroll(false);
dieSound->Stop(); dieSound->Stop();
dieSound->Play(); dieSound->Play();
game_state=state_collided; game_state = state_collided;
} }
} }
} }
if (flappy_bird_position<-220) game_state=state_die; if (flappy_bird_position < -220) game_state = state_die;
break; break;
case state_collided: //is dying from a pipe: go down by gravity case state_collided: //is dying from a pipe: go down by gravity
flappy_bird_velocity += gravity; flappy_bird_velocity += gravity;
flappy_bird_position -= flappy_bird_velocity; flappy_bird_position -= flappy_bird_velocity;
if(flappy_bird_position<-224) flappy_bird_position=-224; if (flappy_bird_position < -224) flappy_bird_position = -224;
if(flappy_bird_position>320) flappy_bird_position=320; if (flappy_bird_position > 320) flappy_bird_position = 320;
flappy_bird->setPosition(-150, (int)flappy_bird_position); flappy_bird->setPosition(-150, (int) flappy_bird_position);
if ((((float)flappy_bird_velocity / 10.0f) * 90.0f)<90.0f) flappy_bird_rotation = (((float)flappy_bird_velocity / 10.0f) * 90.0f); if ((((float) flappy_bird_velocity / 10.0f) * 90.0f) < 90.0f) flappy_bird_rotation = (((float) flappy_bird_velocity / 10.0f) * 90.0f);
else flappy_bird_rotation = 90.0f; else flappy_bird_rotation = 90.0f;
flappy_bird->setAngle(flappy_bird_rotation); flappy_bird->setAngle(flappy_bird_rotation);
if (flappy_bird_position<-220) game_state=state_show_gameover; if (flappy_bird_position < -220) game_state = state_show_gameover;
break; break;
case state_show_gameover: //IsDying case state_show_gameover: //IsDying
if(dieSound->IsPlaying() == false) { if (dieSound->IsPlaying() == false) {
swooshSound->Stop(); swooshSound->Stop();
swooshSound->Play(); swooshSound->Play();
splash->setSplashImageData(looseImgData); splash->setSplashImageData(looseImgData);
splash->FadeEnter(); splash->FadeEnter();
remove(score_img); remove(score_img);
game_state=state_gameover; game_state = state_gameover;
} }
break; break;
case state_die: //Die case state_die: //Die
bg->setScrollSpeed(0.0f); bg->setScrollSpeed(0.0f);
fg->setScrollSpeed(0.0f); fg->setScrollSpeed(0.0f);
for(int i=0;i<4;i++) pipes[i]->setScroll(false); for (int i = 0; i < 4; i++) pipes[i]->setScroll(false);
dieSound->Stop(); dieSound->Stop();
dieSound->Play(); dieSound->Play();
game_state=state_show_gameover; game_state = state_show_gameover;
break; break;
default: default:
break; break;

View File

@ -35,10 +35,12 @@
class CVideo; class CVideo;
class MainWindowTV : public GuiMainWindowScreen{ class MainWindowTV : public GuiMainWindowScreen {
public: public:
MainWindowTV(int w, int h); MainWindowTV(int w, int h);
virtual ~MainWindowTV(); virtual ~MainWindowTV();
private: private:
int width, height; int width, height;
@ -49,45 +51,47 @@ private:
GuiSound *swooshSound; GuiSound *swooshSound;
GuiSound *dieSound; GuiSound *dieSound;
GuiImageData* bgImgData; GuiImageData *bgImgData;
GuiImageData* fgImgData; GuiImageData *fgImgData;
GuiImageData* splashImgData; GuiImageData *splashImgData;
GuiImageData* looseImgData; GuiImageData *looseImgData;
Background* bg; Background *bg;
Background* fg; Background *fg;
//GuiImage* splash; //Splash screen //GuiImage* splash; //Splash screen
SplashScreen* splash; SplashScreen *splash;
ScoreImage *score_img; ScoreImage *score_img;
int score=0; int score = 0;
Pipe* pipes[4]; Pipe *pipes[4];
int collision_state[4]; int collision_state[4];
int bg_offset=0; int bg_offset = 0;
int fg_offset=0; int fg_offset = 0;
GuiImageData *flappy_bird_frm[4]; GuiImageData *flappy_bird_frm[4];
GuiImage* flappy_bird; GuiImage *flappy_bird;
int flappy_bird_change_skip=0; int flappy_bird_change_skip = 0;
int flappy_bird_curr_frame=0; int flappy_bird_curr_frame = 0;
int game_state=0; //0=start, 1=in-game, 2=game over int game_state = 0; //0=start, 1=in-game, 2=game over
int set_gameover=0; int set_gameover = 0;
#define gravity 0.25f #define gravity 0.25f
#define jump -4.6f #define jump -4.6f
float flappy_bird_position=-5.0f; float flappy_bird_position = -5.0f;
float flappy_bird_velocity = 0.0f; float flappy_bird_velocity = 0.0f;
float flappy_bird_rotation = 0.0f; float flappy_bird_rotation = 0.0f;
void draw(CVideo *v); void draw(CVideo *v);
void update(GuiController * c);
void update(GuiController *c);
void process(); void process();
}; };

View File

@ -7,43 +7,38 @@
#include "gui/GuiImageAsync.h" #include "gui/GuiImageAsync.h"
#include "gui/GuiSound.h" #include "gui/GuiSound.h"
Resources * Resources::instance = NULL; Resources *Resources::instance = NULL;
void Resources::Clear() void Resources::Clear() {
{ for (int i = 0; RecourceList[i].filename != NULL; ++i) {
for(int i = 0; RecourceList[i].filename != NULL; ++i) if (RecourceList[i].CustomFile) {
{ free(RecourceList[i].CustomFile);
if(RecourceList[i].CustomFile) RecourceList[i].CustomFile = NULL;
{ }
free(RecourceList[i].CustomFile);
RecourceList[i].CustomFile = NULL;
}
if(RecourceList[i].CustomFileSize != 0) if (RecourceList[i].CustomFileSize != 0)
RecourceList[i].CustomFileSize = 0; RecourceList[i].CustomFileSize = 0;
} }
if(instance) if (instance)
delete instance; delete instance;
instance = NULL; instance = NULL;
} }
bool Resources::LoadFiles(const char * path) bool Resources::LoadFiles(const char *path) {
{ if (!path)
if(!path) return false;
return false;
bool result = false; bool result = false;
Clear(); Clear();
for(int i = 0; RecourceList[i].filename != NULL; ++i) for (int i = 0; RecourceList[i].filename != NULL; ++i) {
{
std::string fullpath(path); std::string fullpath(path);
fullpath += "/"; fullpath += "/";
fullpath += RecourceList[i].filename; fullpath += RecourceList[i].filename;
uint8_t * buffer = NULL; uint8_t *buffer = NULL;
uint32_t filesize = 0; uint32_t filesize = 0;
FSUtils::LoadFileToMem(fullpath.c_str(), &buffer, &filesize); FSUtils::LoadFileToMem(fullpath.c_str(), &buffer, &filesize);
@ -51,82 +46,68 @@ bool Resources::LoadFiles(const char * path)
RecourceList[i].CustomFile = buffer; RecourceList[i].CustomFile = buffer;
RecourceList[i].CustomFileSize = (uint32_t) filesize; RecourceList[i].CustomFileSize = (uint32_t) filesize;
result |= (buffer != 0); result |= (buffer != 0);
} }
return result; return result;
} }
const uint8_t * Resources::GetFile(const char * filename) const uint8_t *Resources::GetFile(const char *filename) {
{ for (int i = 0; RecourceList[i].filename != NULL; ++i) {
for(int i = 0; RecourceList[i].filename != NULL; ++i) if (strcasecmp(filename, RecourceList[i].filename) == 0) {
{ return (RecourceList[i].CustomFile ? RecourceList[i].CustomFile : RecourceList[i].DefaultFile);
if(strcasecmp(filename, RecourceList[i].filename) == 0) }
{ }
return (RecourceList[i].CustomFile ? RecourceList[i].CustomFile : RecourceList[i].DefaultFile);
}
}
return NULL; return NULL;
} }
uint32_t Resources::GetFileSize(const char * filename) uint32_t Resources::GetFileSize(const char *filename) {
{ for (int i = 0; RecourceList[i].filename != NULL; ++i) {
for(int i = 0; RecourceList[i].filename != NULL; ++i) if (strcasecmp(filename, RecourceList[i].filename) == 0) {
{ return (RecourceList[i].CustomFile ? RecourceList[i].CustomFileSize : RecourceList[i].DefaultFileSize);
if(strcasecmp(filename, RecourceList[i].filename) == 0) }
{ }
return (RecourceList[i].CustomFile ? RecourceList[i].CustomFileSize : RecourceList[i].DefaultFileSize); return 0;
}
}
return 0;
} }
GuiImageData * Resources::GetImageData(const char * filename) GuiImageData *Resources::GetImageData(const char *filename) {
{ if (!instance)
if(!instance)
instance = new Resources; instance = new Resources;
std::map<std::string, std::pair<unsigned int, GuiImageData *> >::iterator itr = instance->imageDataMap.find(std::string(filename)); std::map<std::string, std::pair<unsigned int, GuiImageData *> >::iterator itr = instance->imageDataMap.find(std::string(filename));
if(itr != instance->imageDataMap.end()) if (itr != instance->imageDataMap.end()) {
{
itr->second.first++; itr->second.first++;
return itr->second.second; return itr->second.second;
} }
for(int i = 0; RecourceList[i].filename != NULL; ++i) for (int i = 0; RecourceList[i].filename != NULL; ++i) {
{ if (strcasecmp(filename, RecourceList[i].filename) == 0) {
if(strcasecmp(filename, RecourceList[i].filename) == 0) const uint8_t *buff = RecourceList[i].CustomFile ? RecourceList[i].CustomFile : RecourceList[i].DefaultFile;
{ const uint32_t size = RecourceList[i].CustomFile ? RecourceList[i].CustomFileSize : RecourceList[i].DefaultFileSize;
const uint8_t * buff = RecourceList[i].CustomFile ? RecourceList[i].CustomFile : RecourceList[i].DefaultFile;
const uint32_t size = RecourceList[i].CustomFile ? RecourceList[i].CustomFileSize : RecourceList[i].DefaultFileSize;
if(buff == NULL) if (buff == NULL)
return NULL; return NULL;
GuiImageData * image = new GuiImageData(buff, size); GuiImageData *image = new GuiImageData(buff, size);
instance->imageDataMap[std::string(filename)].first = 1; instance->imageDataMap[std::string(filename)].first = 1;
instance->imageDataMap[std::string(filename)].second = image; instance->imageDataMap[std::string(filename)].second = image;
return image; return image;
} }
} }
return NULL; return NULL;
} }
void Resources::RemoveImageData(GuiImageData * image) void Resources::RemoveImageData(GuiImageData *image) {
{
std::map<std::string, std::pair<unsigned int, GuiImageData *> >::iterator itr; std::map<std::string, std::pair<unsigned int, GuiImageData *> >::iterator itr;
for(itr = instance->imageDataMap.begin(); itr != instance->imageDataMap.end(); itr++) for (itr = instance->imageDataMap.begin(); itr != instance->imageDataMap.end(); itr++) {
{ if (itr->second.second == image) {
if(itr->second.second == image)
{
itr->second.first--; itr->second.first--;
if(itr->second.first == 0) if (itr->second.first == 0) {
{ AsyncDeleter::pushForDelete(itr->second.second);
AsyncDeleter::pushForDelete( itr->second.second );
instance->imageDataMap.erase(itr); instance->imageDataMap.erase(itr);
} }
break; break;
@ -134,52 +115,44 @@ void Resources::RemoveImageData(GuiImageData * image)
} }
} }
GuiSound * Resources::GetSound(const char * filename) GuiSound *Resources::GetSound(const char *filename) {
{ if (!instance)
if(!instance)
instance = new Resources; instance = new Resources;
std::map<std::string, std::pair<unsigned int, GuiSound *> >::iterator itr = instance->soundDataMap.find(std::string(filename)); std::map<std::string, std::pair<unsigned int, GuiSound *> >::iterator itr = instance->soundDataMap.find(std::string(filename));
if(itr != instance->soundDataMap.end()) if (itr != instance->soundDataMap.end()) {
{
itr->second.first++; itr->second.first++;
return itr->second.second; return itr->second.second;
} }
for(int i = 0; RecourceList[i].filename != NULL; ++i) for (int i = 0; RecourceList[i].filename != NULL; ++i) {
{ if (strcasecmp(filename, RecourceList[i].filename) == 0) {
if(strcasecmp(filename, RecourceList[i].filename) == 0) const uint8_t *buff = RecourceList[i].CustomFile ? RecourceList[i].CustomFile : RecourceList[i].DefaultFile;
{ const uint32_t size = RecourceList[i].CustomFile ? RecourceList[i].CustomFileSize : RecourceList[i].DefaultFileSize;
const uint8_t * buff = RecourceList[i].CustomFile ? RecourceList[i].CustomFile : RecourceList[i].DefaultFile;
const uint32_t size = RecourceList[i].CustomFile ? RecourceList[i].CustomFileSize : RecourceList[i].DefaultFileSize;
if(buff == NULL) if (buff == NULL)
return NULL; return NULL;
GuiSound * sound = new GuiSound(buff, size); GuiSound *sound = new GuiSound(buff, size);
instance->soundDataMap[std::string(filename)].first = 1; instance->soundDataMap[std::string(filename)].first = 1;
instance->soundDataMap[std::string(filename)].second = sound; instance->soundDataMap[std::string(filename)].second = sound;
return sound; return sound;
} }
} }
return NULL; return NULL;
} }
void Resources::RemoveSound(GuiSound * sound) void Resources::RemoveSound(GuiSound *sound) {
{
std::map<std::string, std::pair<unsigned int, GuiSound *> >::iterator itr; std::map<std::string, std::pair<unsigned int, GuiSound *> >::iterator itr;
for(itr = instance->soundDataMap.begin(); itr != instance->soundDataMap.end(); itr++) for (itr = instance->soundDataMap.begin(); itr != instance->soundDataMap.end(); itr++) {
{ if (itr->second.second == sound) {
if(itr->second.second == sound)
{
itr->second.first--; itr->second.first--;
if(itr->second.first == 0) if (itr->second.first == 0) {
{ AsyncDeleter::pushForDelete(itr->second.second);
AsyncDeleter::pushForDelete( itr->second.second );
instance->soundDataMap.erase(itr); instance->soundDataMap.erase(itr);
} }
break; break;

View File

@ -5,25 +5,32 @@
//! forward declaration //! forward declaration
class GuiImageData; class GuiImageData;
class GuiSound; class GuiSound;
class Resources class Resources {
{
public: public:
static void Clear(); static void Clear();
static bool LoadFiles(const char * path);
static const uint8_t * GetFile(const char * filename);
static uint32_t GetFileSize(const char * filename);
static GuiImageData * GetImageData(const char * filename); static bool LoadFiles(const char *path);
static void RemoveImageData(GuiImageData * image);
static const uint8_t *GetFile(const char *filename);
static uint32_t GetFileSize(const char *filename);
static GuiImageData *GetImageData(const char *filename);
static void RemoveImageData(GuiImageData *image);
static GuiSound *GetSound(const char *filename);
static void RemoveSound(GuiSound *sound);
static GuiSound * GetSound(const char * filename);
static void RemoveSound(GuiSound * sound);
private: private:
static Resources *instance; static Resources *instance;
Resources() {} Resources() {}
~Resources() {} ~Resources() {}
std::map<std::string, std::pair<unsigned int, GuiImageData *> > imageDataMap; std::map<std::string, std::pair<unsigned int, GuiImageData *> > imageDataMap;

View File

@ -16,11 +16,10 @@
****************************************************************************/ ****************************************************************************/
#include "AsyncDeleter.h" #include "AsyncDeleter.h"
AsyncDeleter * AsyncDeleter::deleterInstance = NULL; AsyncDeleter *AsyncDeleter::deleterInstance = NULL;
AsyncDeleter::AsyncDeleter() AsyncDeleter::AsyncDeleter()
: CThread(CThread::eAttributeAffCore1 | CThread::eAttributePinnedAff) : CThread(CThread::eAttributeAffCore1 | CThread::eAttributePinnedAff), exitApplication(false) {
, exitApplication(false) {
} }
AsyncDeleter::~AsyncDeleter() { AsyncDeleter::~AsyncDeleter() {
@ -28,14 +27,14 @@ AsyncDeleter::~AsyncDeleter() {
} }
void AsyncDeleter::triggerDeleteProcess(void) { void AsyncDeleter::triggerDeleteProcess(void) {
if(!deleterInstance) if (!deleterInstance)
deleterInstance = new AsyncDeleter; deleterInstance = new AsyncDeleter;
//! to trigger the event after GUI process is finished execution //! to trigger the event after GUI process is finished execution
//! this function is used to swap elements from one to next array //! this function is used to swap elements from one to next array
if(!deleterInstance->deleteElements.empty()) { if (!deleterInstance->deleteElements.empty()) {
deleterInstance->deleteMutex.lock(); deleterInstance->deleteMutex.lock();
while(!deleterInstance->deleteElements.empty()) { while (!deleterInstance->deleteElements.empty()) {
deleterInstance->realDeleteElements.push(deleterInstance->deleteElements.front()); deleterInstance->realDeleteElements.push(deleterInstance->deleteElements.front());
deleterInstance->deleteElements.pop(); deleterInstance->deleteElements.pop();
} }
@ -45,12 +44,12 @@ void AsyncDeleter::triggerDeleteProcess(void) {
} }
void AsyncDeleter::executeThread(void) { void AsyncDeleter::executeThread(void) {
while(!exitApplication) { while (!exitApplication) {
suspendThread(); suspendThread();
//! delete elements that require post process deleting //! delete elements that require post process deleting
//! because otherwise they would block or do invalid access on GUI thread //! because otherwise they would block or do invalid access on GUI thread
while(!realDeleteElements.empty()) { while (!realDeleteElements.empty()) {
deleteMutex.lock(); deleteMutex.lock();
GuiElement *element = realDeleteElements.front(); GuiElement *element = realDeleteElements.front();
realDeleteElements.pop(); realDeleteElements.pop();

View File

@ -32,11 +32,12 @@ public:
class Element { class Element {
public: public:
Element() {} Element() {}
virtual ~Element() {} virtual ~Element() {}
}; };
static void pushForDelete(GuiElement *e) { static void pushForDelete(GuiElement *e) {
if(!deleterInstance) if (!deleterInstance)
deleterInstance = new AsyncDeleter; deleterInstance = new AsyncDeleter;
deleterInstance->deleteElements.push(e); deleterInstance->deleteElements.push(e);
@ -46,6 +47,7 @@ public:
private: private:
AsyncDeleter(); AsyncDeleter();
virtual ~AsyncDeleter(); virtual ~AsyncDeleter();
static AsyncDeleter *deleterInstance; static AsyncDeleter *deleterInstance;

View File

@ -20,48 +20,52 @@
#include <malloc.h> #include <malloc.h>
#include <coreinit/mutex.h> #include <coreinit/mutex.h>
class CMutex class CMutex {
{
public: public:
CMutex() { CMutex() {
pMutex = (OSMutex*) malloc(sizeof(OSMutex)); pMutex = (OSMutex *) malloc(sizeof(OSMutex));
if(!pMutex) if (!pMutex)
return; return;
OSInitMutex(pMutex); OSInitMutex(pMutex);
} }
virtual ~CMutex() { virtual ~CMutex() {
if(pMutex) if (pMutex)
free(pMutex); free(pMutex);
} }
void lock(void) { void lock(void) {
if(pMutex) if (pMutex)
OSLockMutex(pMutex); OSLockMutex(pMutex);
} }
void unlock(void) { void unlock(void) {
if(pMutex) if (pMutex)
OSUnlockMutex(pMutex); OSUnlockMutex(pMutex);
} }
BOOL tryLock(void) { BOOL tryLock(void) {
if(!pMutex) if (!pMutex)
return false; return false;
return (OSTryLockMutex(pMutex) != 0); return (OSTryLockMutex(pMutex) != 0);
} }
private: private:
OSMutex *pMutex; OSMutex *pMutex;
}; };
class CMutexLock class CMutexLock {
{
public: public:
CMutexLock() { CMutexLock() {
mutex.lock(); mutex.lock();
} }
virtual ~CMutexLock() { virtual ~CMutexLock() {
mutex.unlock(); mutex.unlock();
} }
private: private:
CMutex mutex; CMutex mutex;
}; };

View File

@ -25,23 +25,20 @@
class CThread { class CThread {
public: public:
typedef void (* Callback)(CThread *thread, void *arg); typedef void (*Callback)(CThread *thread, void *arg);
//! constructor //! constructor
CThread(int32_t iAttr, int32_t iPriority = 16, int32_t iStackSize = 0x8000, CThread::Callback callback = NULL, void *callbackArg = NULL) CThread(int32_t iAttr, int32_t iPriority = 16, int32_t iStackSize = 0x8000, CThread::Callback callback = NULL, void *callbackArg = NULL)
: pThread(NULL) : pThread(NULL), pThreadStack(NULL), pCallback(callback), pCallbackArg(callbackArg) {
, pThreadStack(NULL)
, pCallback(callback)
, pCallbackArg(callbackArg) {
//! save attribute assignment //! save attribute assignment
iAttributes = iAttr; iAttributes = iAttr;
//! allocate the thread //! allocate the thread
pThread = (OSThread*)memalign(8, sizeof(OSThread)); pThread = (OSThread *) memalign(8, sizeof(OSThread));
//! allocate the stack //! allocate the stack
pThreadStack = (uint8_t *) memalign(0x20, iStackSize); pThreadStack = (uint8_t *) memalign(0x20, iStackSize);
//! create the thread //! create the thread
if(pThread && pThreadStack) if (pThread && pThreadStack)
OSCreateThread(pThread, &CThread::threadCallback, 1, (char*)this, pThreadStack+iStackSize, iStackSize, iPriority, iAttributes); OSCreateThread(pThread, &CThread::threadCallback, 1, (char *) this, pThreadStack + iStackSize, iStackSize, iPriority, iAttributes);
} }
//! destructor //! destructor
@ -50,72 +47,81 @@ public:
} }
static CThread *create(CThread::Callback callback, void *callbackArg, int32_t iAttr = eAttributeNone, int32_t iPriority = 16, int32_t iStackSize = 0x8000) { static CThread *create(CThread::Callback callback, void *callbackArg, int32_t iAttr = eAttributeNone, int32_t iPriority = 16, int32_t iStackSize = 0x8000) {
return ( new CThread(iAttr, iPriority, iStackSize, callback, callbackArg) ); return (new CThread(iAttr, iPriority, iStackSize, callback, callbackArg));
} }
//! Get thread ID //! Get thread ID
virtual void* getThread() const { virtual void *getThread() const {
return pThread; return pThread;
} }
//! Thread entry function //! Thread entry function
virtual void executeThread(void) { virtual void executeThread(void) {
if(pCallback) if (pCallback)
pCallback(this, pCallbackArg); pCallback(this, pCallbackArg);
} }
//! Suspend thread //! Suspend thread
virtual void suspendThread(void) { virtual void suspendThread(void) {
if(isThreadSuspended()) return; if (isThreadSuspended()) return;
if(pThread) OSSuspendThread(pThread); if (pThread) OSSuspendThread(pThread);
} }
//! Resume thread //! Resume thread
virtual void resumeThread(void) { virtual void resumeThread(void) {
if(!isThreadSuspended()) return; if (!isThreadSuspended()) return;
if(pThread) OSResumeThread(pThread); if (pThread) OSResumeThread(pThread);
} }
//! Set thread priority //! Set thread priority
virtual void setThreadPriority(int prio) { virtual void setThreadPriority(int prio) {
if(pThread) OSSetThreadPriority(pThread, prio); if (pThread) OSSetThreadPriority(pThread, prio);
} }
//! Check if thread is suspended //! Check if thread is suspended
virtual BOOL isThreadSuspended(void) const { virtual BOOL isThreadSuspended(void) const {
if(pThread) return OSIsThreadSuspended(pThread); if (pThread) return OSIsThreadSuspended(pThread);
return false; return false;
} }
//! Check if thread is terminated //! Check if thread is terminated
virtual BOOL isThreadTerminated(void) const { virtual BOOL isThreadTerminated(void) const {
if(pThread) return OSIsThreadTerminated(pThread); if (pThread) return OSIsThreadTerminated(pThread);
return false; return false;
} }
//! Check if thread is running //! Check if thread is running
virtual BOOL isThreadRunning(void) const { virtual BOOL isThreadRunning(void) const {
return !isThreadSuspended() && !isThreadRunning(); return !isThreadSuspended() && !isThreadRunning();
} }
//! Shutdown thread //! Shutdown thread
virtual void shutdownThread(void) { virtual void shutdownThread(void) {
//! wait for thread to finish //! wait for thread to finish
if(pThread && !(iAttributes & eAttributeDetach)) { if (pThread && !(iAttributes & eAttributeDetach)) {
if(isThreadSuspended()) if (isThreadSuspended())
resumeThread(); resumeThread();
OSJoinThread(pThread, NULL); OSJoinThread(pThread, NULL);
} }
//! free the thread stack buffer //! free the thread stack buffer
if(pThreadStack) if (pThreadStack)
free(pThreadStack); free(pThreadStack);
if(pThread) if (pThread)
free(pThread); free(pThread);
pThread = NULL; pThread = NULL;
pThreadStack = NULL; pThreadStack = NULL;
} }
//! Thread attributes //! Thread attributes
enum eCThreadAttributes { enum eCThreadAttributes {
eAttributeNone = 0x07, eAttributeNone = 0x07,
eAttributeAffCore0 = 0x01, eAttributeAffCore0 = 0x01,
eAttributeAffCore1 = 0x02, eAttributeAffCore1 = 0x02,
eAttributeAffCore2 = 0x04, eAttributeAffCore2 = 0x04,
eAttributeDetach = 0x08, eAttributeDetach = 0x08,
eAttributePinnedAff = 0x10 eAttributePinnedAff = 0x10
}; };
private: private:
static int threadCallback(int argc, const char **argv) { static int threadCallback(int argc, const char **argv) {
@ -123,6 +129,7 @@ private:
((CThread *) argv)->executeThread(); ((CThread *) argv)->executeThread();
return 0; return 0;
} }
int iAttributes; int iAttributes;
OSThread *pThread; OSThread *pThread;
uint8_t *pThreadStack; uint8_t *pThreadStack;

View File

@ -38,37 +38,31 @@
static MEMHeapHandle mem1_heap = NULL; static MEMHeapHandle mem1_heap = NULL;
static MEMHeapHandle bucket_heap = NULL; static MEMHeapHandle bucket_heap = NULL;
void memoryInitialize(void) void memoryInitialize(void) {
{ if (!mem1_heap) {
if(!mem1_heap)
{
MEMHeapHandle mem1_heap_handle = MEMGetBaseHeapHandle(MEMORY_ARENA_1); MEMHeapHandle mem1_heap_handle = MEMGetBaseHeapHandle(MEMORY_ARENA_1);
unsigned int mem1_allocatable_size = MEMGetAllocatableSizeForFrmHeapEx(mem1_heap_handle, 4); unsigned int mem1_allocatable_size = MEMGetAllocatableSizeForFrmHeapEx(mem1_heap_handle, 4);
void *mem1_memory = MEMAllocFromFrmHeapEx(mem1_heap_handle, mem1_allocatable_size, 4); void *mem1_memory = MEMAllocFromFrmHeapEx(mem1_heap_handle, mem1_allocatable_size, 4);
if(mem1_memory) if (mem1_memory)
mem1_heap = MEMCreateExpHeapEx(mem1_memory, mem1_allocatable_size, 0); mem1_heap = MEMCreateExpHeapEx(mem1_memory, mem1_allocatable_size, 0);
} }
if(!bucket_heap) if (!bucket_heap) {
{
MEMHeapHandle bucket_heap_handle = MEMGetBaseHeapHandle(MEMORY_ARENA_FG_BUCKET); MEMHeapHandle bucket_heap_handle = MEMGetBaseHeapHandle(MEMORY_ARENA_FG_BUCKET);
unsigned int bucket_allocatable_size = MEMGetAllocatableSizeForFrmHeapEx(bucket_heap_handle, 4); unsigned int bucket_allocatable_size = MEMGetAllocatableSizeForFrmHeapEx(bucket_heap_handle, 4);
void *bucket_memory = MEMAllocFromFrmHeapEx(bucket_heap_handle, bucket_allocatable_size, 4); void *bucket_memory = MEMAllocFromFrmHeapEx(bucket_heap_handle, bucket_allocatable_size, 4);
if(bucket_memory) if (bucket_memory)
bucket_heap = MEMCreateExpHeapEx(bucket_memory, bucket_allocatable_size, 0); bucket_heap = MEMCreateExpHeapEx(bucket_memory, bucket_allocatable_size, 0);
} }
} }
void memoryRelease(void) void memoryRelease(void) {
{ if (mem1_heap) {
if(mem1_heap)
{
MEMDestroyExpHeap(mem1_heap); MEMDestroyExpHeap(mem1_heap);
MEMFreeToFrmHeap(MEMGetBaseHeapHandle(MEMORY_ARENA_1), 3); MEMFreeToFrmHeap(MEMGetBaseHeapHandle(MEMORY_ARENA_1), 3);
mem1_heap = NULL; mem1_heap = NULL;
} }
if(bucket_heap) if (bucket_heap) {
{
MEMDestroyExpHeap(bucket_heap); MEMDestroyExpHeap(bucket_heap);
MEMFreeToFrmHeap(MEMGetBaseHeapHandle(MEMORY_ARENA_FG_BUCKET), 3); MEMFreeToFrmHeap(MEMGetBaseHeapHandle(MEMORY_ARENA_FG_BUCKET), 3);
bucket_heap = NULL; bucket_heap = NULL;
@ -162,36 +156,30 @@ void *__wrap__realloc_r(struct _reent *r, void *p, size_t size)
//!------------------------------------------------------------------------------------------- //!-------------------------------------------------------------------------------------------
//! some wrappers //! some wrappers
//!------------------------------------------------------------------------------------------- //!-------------------------------------------------------------------------------------------
void * MEM2_alloc(unsigned int size, unsigned int align) void *MEM2_alloc(unsigned int size, unsigned int align) {
{
return memalign(align, size); return memalign(align, size);
} }
void MEM2_free(void *ptr) void MEM2_free(void *ptr) {
{
free(ptr); free(ptr);
} }
void * MEM1_alloc(unsigned int size, unsigned int align) void *MEM1_alloc(unsigned int size, unsigned int align) {
{
if (align < 4) if (align < 4)
align = 4; align = 4;
return MEMAllocFromExpHeapEx(mem1_heap, size, align); return MEMAllocFromExpHeapEx(mem1_heap, size, align);
} }
void MEM1_free(void *ptr) void MEM1_free(void *ptr) {
{
MEMFreeToExpHeap(mem1_heap, ptr); MEMFreeToExpHeap(mem1_heap, ptr);
} }
void * MEMBucket_alloc(unsigned int size, unsigned int align) void *MEMBucket_alloc(unsigned int size, unsigned int align) {
{
if (align < 4) if (align < 4)
align = 4; align = 4;
return MEMAllocFromExpHeapEx(bucket_heap, size, align); return MEMAllocFromExpHeapEx(bucket_heap, size, align);
} }
void MEMBucket_free(void *ptr) void MEMBucket_free(void *ptr) {
{
MEMFreeToExpHeap(bucket_heap, ptr); MEMFreeToExpHeap(bucket_heap, ptr);
} }

View File

@ -24,15 +24,19 @@ extern "C" {
#include <malloc.h> #include <malloc.h>
void memoryInitialize(void); void memoryInitialize(void);
void memoryRelease(void); void memoryRelease(void);
void * MEM2_alloc(unsigned int size, unsigned int align); void *MEM2_alloc(unsigned int size, unsigned int align);
void MEM2_free(void *ptr); void MEM2_free(void *ptr);
void * MEM1_alloc(unsigned int size, unsigned int align); void *MEM1_alloc(unsigned int size, unsigned int align);
void MEM1_free(void *ptr); void MEM1_free(void *ptr);
void * MEMBucket_alloc(unsigned int size, unsigned int align); void *MEMBucket_alloc(unsigned int size, unsigned int align);
void MEMBucket_free(void *ptr); void MEMBucket_free(void *ptr);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -36,12 +36,12 @@
#include <utils/StringTools.h> #include <utils/StringTools.h>
BOOL StringTools::EndsWith(const std::string& a, const std::string& b) { BOOL StringTools::EndsWith(const std::string &a, const std::string &b) {
if (b.size() > a.size()) return false; if (b.size() > a.size()) return false;
return std::equal(a.begin() + a.size() - b.size(), a.end(), b.begin()); return std::equal(a.begin() + a.size() - b.size(), a.end(), b.begin());
} }
const char * StringTools::byte_to_binary(int32_t x) { const char *StringTools::byte_to_binary(int32_t x) {
static char b[9]; static char b[9];
b[0] = '\0'; b[0] = '\0';
@ -53,25 +53,25 @@ const char * StringTools::byte_to_binary(int32_t x) {
return b; return b;
} }
std::string StringTools::removeCharFromString(std::string& input,char toBeRemoved) { std::string StringTools::removeCharFromString(std::string &input, char toBeRemoved) {
std::string output = input; std::string output = input;
size_t position; size_t position;
while(1) { while (1) {
position = output.find(toBeRemoved); position = output.find(toBeRemoved);
if(position == std::string::npos) if (position == std::string::npos)
break; break;
output.erase(position, 1); output.erase(position, 1);
} }
return output; return output;
} }
const char * StringTools::fmt(const char * format, ...) { const char *StringTools::fmt(const char *format, ...) {
static char strChar[512]; static char strChar[512];
strChar[0] = 0; strChar[0] = 0;
va_list va; va_list va;
va_start(va, format); va_start(va, format);
if((vsprintf(strChar, format, va) >= 0)) { if ((vsprintf(strChar, format, va) >= 0)) {
va_end(va); va_end(va);
return (const char *) strChar; return (const char *) strChar;
} }
@ -80,26 +80,26 @@ const char * StringTools::fmt(const char * format, ...) {
return NULL; return NULL;
} }
const wchar_t * StringTools::wfmt(const char * format, ...) { const wchar_t *StringTools::wfmt(const char *format, ...) {
static char tmp[512]; static char tmp[512];
static wchar_t strWChar[512]; static wchar_t strWChar[512];
strWChar[0] = 0; strWChar[0] = 0;
tmp[0] = 0; tmp[0] = 0;
if(!format) if (!format)
return (const wchar_t *) strWChar; return (const wchar_t *) strWChar;
if(strcmp(format, "") == 0) if (strcmp(format, "") == 0)
return (const wchar_t *) strWChar; return (const wchar_t *) strWChar;
va_list va; va_list va;
va_start(va, format); va_start(va, format);
if((vsprintf(tmp, format, va) >= 0)) { if ((vsprintf(tmp, format, va) >= 0)) {
int bt; int bt;
int32_t strlength = strlen(tmp); int32_t strlength = strlen(tmp);
bt = mbstowcs(strWChar, tmp, (strlength < 512) ? strlength : 512 ); bt = mbstowcs(strWChar, tmp, (strlength < 512) ? strlength : 512);
if(bt > 0) { if (bt > 0) {
strWChar[bt] = 0; strWChar[bt] = 0;
return (const wchar_t *) strWChar; return (const wchar_t *) strWChar;
} }
@ -109,14 +109,14 @@ const wchar_t * StringTools::wfmt(const char * format, ...) {
return NULL; return NULL;
} }
int32_t StringTools::strprintf(std::string &str, const char * format, ...) { int32_t StringTools::strprintf(std::string &str, const char *format, ...) {
static char tmp[512]; static char tmp[512];
tmp[0] = 0; tmp[0] = 0;
int32_t result = 0; int32_t result = 0;
va_list va; va_list va;
va_start(va, format); va_start(va, format);
if((vsprintf(tmp, format, va) >= 0)) { if ((vsprintf(tmp, format, va) >= 0)) {
str = tmp; str = tmp;
result = str.size(); result = str.size();
} }
@ -125,14 +125,14 @@ int32_t StringTools::strprintf(std::string &str, const char * format, ...) {
return result; return result;
} }
std::string StringTools::strfmt(const char * format, ...) { std::string StringTools::strfmt(const char *format, ...) {
std::string str; std::string str;
static char tmp[512]; static char tmp[512];
tmp[0] = 0; tmp[0] = 0;
va_list va; va_list va;
va_start(va, format); va_start(va, format);
if((vsprintf(tmp, format, va) >= 0)) { if ((vsprintf(tmp, format, va) >= 0)) {
str = tmp; str = tmp;
} }
va_end(va); va_end(va);
@ -140,11 +140,11 @@ std::string StringTools::strfmt(const char * format, ...) {
return str; return str;
} }
BOOL StringTools::char2wchar_t(const char * strChar, wchar_t * dest) { BOOL StringTools::char2wchar_t(const char *strChar, wchar_t *dest) {
if(!strChar || !dest) if (!strChar || !dest)
return false; return false;
int bt; int bt;
bt = mbstowcs(dest, strChar, strlen(strChar)); bt = mbstowcs(dest, strChar, strlen(strChar));
if (bt > 0) { if (bt > 0) {
dest[bt] = 0; dest[bt] = 0;
@ -154,39 +154,39 @@ BOOL StringTools::char2wchar_t(const char * strChar, wchar_t * dest) {
return false; return false;
} }
int32_t StringTools::strtokcmp(const char * string, const char * compare, const char * separator) { int32_t StringTools::strtokcmp(const char *string, const char *compare, const char *separator) {
if(!string || !compare) if (!string || !compare)
return -1; return -1;
char TokCopy[512]; char TokCopy[512];
strncpy(TokCopy, compare, sizeof(TokCopy)); strncpy(TokCopy, compare, sizeof(TokCopy));
TokCopy[511] = '\0'; TokCopy[511] = '\0';
char * strTok = strtok(TokCopy, separator); char *strTok = strtok(TokCopy, separator);
while (strTok != NULL) { while (strTok != NULL) {
if (strcasecmp(string, strTok) == 0) { if (strcasecmp(string, strTok) == 0) {
return 0; return 0;
} }
strTok = strtok(NULL,separator); strTok = strtok(NULL, separator);
} }
return -1; return -1;
} }
int32_t StringTools::strextcmp(const char * string, const char * extension, char seperator) { int32_t StringTools::strextcmp(const char *string, const char *extension, char seperator) {
if(!string || !extension) if (!string || !extension)
return -1; return -1;
char *ptr = strrchr(string, seperator); char *ptr = strrchr(string, seperator);
if(!ptr) if (!ptr)
return -1; return -1;
return strcasecmp(ptr + 1, extension); return strcasecmp(ptr + 1, extension);
} }
std::vector<std::string> StringTools::stringSplit(const std::string & inValue, const std::string & splitter) { std::vector<std::string> StringTools::stringSplit(const std::string &inValue, const std::string &splitter) {
std::string value = inValue; std::string value = inValue;
std::vector<std::string> result; std::vector<std::string> result;
while (true) { while (true) {
@ -201,7 +201,7 @@ std::vector<std::string> StringTools::stringSplit(const std::string & inValue, c
result.push_back(""); result.push_back("");
break; break;
} }
if(index + splitter.size() > value.length()) { if (index + splitter.size() > value.length()) {
break; break;
} }
value = value.substr(index + splitter.size(), value.length()); value = value.substr(index + splitter.size(), value.length());

View File

@ -30,52 +30,58 @@
#include <string> #include <string>
#include <wut_types.h> #include <wut_types.h>
class StringTools{ class StringTools {
public: public:
static BOOL EndsWith(const std::string& a, const std::string& b); static BOOL EndsWith(const std::string &a, const std::string &b);
static const char * byte_to_binary(int32_t x);
static std::string removeCharFromString(std::string& input,char toBeRemoved);
static const char * fmt(const char * format, ...);
static const wchar_t * wfmt(const char * format, ...);
static int32_t strprintf(std::string &str, const char * format, ...);
static std::string strfmt(const char * format, ...);
static BOOL char2wchar_t(const char * src, wchar_t * dest);
static int32_t strtokcmp(const char * string, const char * compare, const char * separator);
static int32_t strextcmp(const char * string, const char * extension, char seperator);
static const char * FullpathToFilename(const char *path){ static const char *byte_to_binary(int32_t x);
if(!path) return path;
const char * ptr = path; static std::string removeCharFromString(std::string &input, char toBeRemoved);
const char * Filename = ptr;
while(*ptr != '\0') static const char *fmt(const char *format, ...);
{
if(ptr[0] == '/' && ptr[1] != '\0')
Filename = ptr+1;
++ptr; static const wchar_t *wfmt(const char *format, ...);
}
return Filename; static int32_t strprintf(std::string &str, const char *format, ...);
static std::string strfmt(const char *format, ...);
static BOOL char2wchar_t(const char *src, wchar_t *dest);
static int32_t strtokcmp(const char *string, const char *compare, const char *separator);
static int32_t strextcmp(const char *string, const char *extension, char seperator);
static const char *FullpathToFilename(const char *path) {
if (!path) return path;
const char *ptr = path;
const char *Filename = ptr;
while (*ptr != '\0') {
if (ptr[0] == '/' && ptr[1] != '\0')
Filename = ptr + 1;
++ptr;
} }
static void RemoveDoubleSlashs(std::string &str){ return Filename;
uint32_t length = str.size(); }
//! clear path of double slashes static void RemoveDoubleSlashs(std::string &str) {
for(uint32_t i = 1; i < length; ++i) uint32_t length = str.size();
{
if(str[i-1] == '/' && str[i] == '/') //! clear path of double slashes
{ for (uint32_t i = 1; i < length; ++i) {
str.erase(i, 1); if (str[i - 1] == '/' && str[i] == '/') {
i--; str.erase(i, 1);
length--; i--;
} length--;
} }
} }
}
static std::vector<std::string> stringSplit(const std::string & value, const std::string & splitter); static std::vector<std::string> stringSplit(const std::string &value, const std::string &splitter);
}; };
#endif /* __STRING_TOOLS_H */ #endif /* __STRING_TOOLS_H */

View File

@ -10,7 +10,7 @@
#include <coreinit/systeminfo.h> #include <coreinit/systeminfo.h>
#include <coreinit/thread.h> #include <coreinit/thread.h>
static int log_socket __attribute__((section(".data")))= -1; static int log_socket __attribute__((section(".data"))) = -1;
static struct sockaddr_in connect_addr __attribute__((section(".data"))); static struct sockaddr_in connect_addr __attribute__((section(".data")));
static volatile int log_lock __attribute__((section(".data"))) = 0; static volatile int log_lock __attribute__((section(".data"))) = 0;
@ -30,20 +30,20 @@ void log_init_() {
void log_print_(const char *str) { void log_print_(const char *str) {
// socket is always 0 initially as it is in the BSS // socket is always 0 initially as it is in the BSS
if(log_socket < 0) { if (log_socket < 0) {
return; return;
} }
while(log_lock) while (log_lock)
OSSleepTicks(OSMicrosecondsToTicks(1000)); OSSleepTicks(OSMicrosecondsToTicks(1000));
log_lock = 1; log_lock = 1;
int len = strlen(str); int len = strlen(str);
int ret; int ret;
while (len > 0) { while (len > 0) {
int block = len < 1400 ? len : 1400; // take max 1400 bytes per UDP packet int block = len < 1400 ? len : 1400; // take max 1400 bytes per UDP packet
ret = sendto(log_socket, str, block, 0, (struct sockaddr *)&connect_addr, sizeof(struct sockaddr_in)); ret = sendto(log_socket, str, block, 0, (struct sockaddr *) &connect_addr, sizeof(struct sockaddr_in));
if(ret < 0) if (ret < 0)
break; break;
len -= ret; len -= ret;
@ -54,18 +54,18 @@ void log_print_(const char *str) {
} }
void OSFatal_printf(const char *format, ...) { void OSFatal_printf(const char *format, ...) {
char tmp[512]; char tmp[512];
tmp[0] = 0; tmp[0] = 0;
va_list va; va_list va;
va_start(va, format); va_start(va, format);
if((vsprintf(tmp, format, va) >= 0)) { if ((vsprintf(tmp, format, va) >= 0)) {
OSFatal(tmp); OSFatal(tmp);
} }
va_end(va); va_end(va);
} }
void log_printf_(const char *format, ...) { void log_printf_(const char *format, ...) {
if(log_socket < 0) { if (log_socket < 0) {
return; return;
} }
@ -74,7 +74,7 @@ void log_printf_(const char *format, ...) {
va_list va; va_list va;
va_start(va, format); va_start(va, format);
if((vsprintf(tmp, format, va) >= 0)) { if ((vsprintf(tmp, format, va) >= 0)) {
log_print_(tmp); log_print_(tmp);
} }
va_end(va); va_end(va);

View File

@ -8,9 +8,12 @@ extern "C" {
#include <string.h> #include <string.h>
void log_init_(); void log_init_();
//void log_deinit_(void); //void log_deinit_(void);
void log_print_(const char *str); void log_print_(const char *str);
void log_printf_(const char *format, ...); void log_printf_(const char *format, ...);
void OSFatal_printf(const char *format, ...); void OSFatal_printf(const char *format, ...);
#define __FILENAME_X__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) #define __FILENAME_X__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
@ -31,7 +34,6 @@ void OSFatal_printf(const char *format, ...);
} while (0) } while (0)
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -7,31 +7,31 @@
#include <utils/logger.h> #include <utils/logger.h>
// https://gist.github.com/ccbrown/9722406 // https://gist.github.com/ccbrown/9722406
void dumpHex(const void* data, size_t size) { void dumpHex(const void *data, size_t size) {
char ascii[17]; char ascii[17];
size_t i, j; size_t i, j;
ascii[16] = '\0'; ascii[16] = '\0';
DEBUG_FUNCTION_LINE("0x%08X (0x0000): ", data); DEBUG_FUNCTION_LINE("0x%08X (0x0000): ", data);
for (i = 0; i < size; ++i) { for (i = 0; i < size; ++i) {
log_printf("%02X ", ((unsigned char*)data)[i]); log_printf("%02X ", ((unsigned char *) data)[i]);
if (((unsigned char*)data)[i] >= ' ' && ((unsigned char*)data)[i] <= '~') { if (((unsigned char *) data)[i] >= ' ' && ((unsigned char *) data)[i] <= '~') {
ascii[i % 16] = ((unsigned char*)data)[i]; ascii[i % 16] = ((unsigned char *) data)[i];
} else { } else {
ascii[i % 16] = '.'; ascii[i % 16] = '.';
} }
if ((i+1) % 8 == 0 || i+1 == size) { if ((i + 1) % 8 == 0 || i + 1 == size) {
log_printf(" "); log_printf(" ");
if ((i+1) % 16 == 0) { if ((i + 1) % 16 == 0) {
log_printf("| %s \n", ascii); log_printf("| %s \n", ascii);
if(i + 1 < size) { if (i + 1 < size) {
DEBUG_FUNCTION_LINE("0x%08X (0x%04X); ", data + i + 1,i+1); DEBUG_FUNCTION_LINE("0x%08X (0x%04X); ", data + i + 1, i + 1);
} }
} else if (i+1 == size) { } else if (i + 1 == size) {
ascii[(i+1) % 16] = '\0'; ascii[(i + 1) % 16] = '\0';
if ((i+1) % 16 <= 8) { if ((i + 1) % 16 <= 8) {
log_printf(" "); log_printf(" ");
} }
for (j = (i+1) % 16; j < 16; ++j) { for (j = (i + 1) % 16; j < 16; ++j) {
log_printf(" "); log_printf(" ");
} }
log_printf("| %s \n", ascii); log_printf("| %s \n", ascii);

View File

@ -7,12 +7,12 @@
extern "C" { extern "C" {
#endif #endif
#define LIMIT(x, min, max) \ #define LIMIT(x, min, max) \
({ \ ({ \
typeof( x ) _x = x; \ typeof( x ) _x = x; \
typeof( min ) _min = min; \ typeof( min ) _min = min; \
typeof( max ) _max = max; \ typeof( max ) _max = max; \
( ( ( _x ) < ( _min ) ) ? ( _min ) : ( ( _x ) > ( _max ) ) ? ( _max) : ( _x ) ); \ ( ( ( _x ) < ( _min ) ) ? ( _min ) : ( ( _x ) > ( _max ) ) ? ( _max) : ( _x ) ); \
}) })
#define DegToRad(a) ( (a) * 0.01745329252f ) #define DegToRad(a) ( (a) * 0.01745329252f )
@ -26,7 +26,7 @@ extern "C" {
#define le64(i) ((((uint64_t)le32((i) & 0xFFFFFFFFLL)) << 32) | ((uint64_t)le32(((i) & 0xFFFFFFFF00000000LL) >> 32))) #define le64(i) ((((uint64_t)le32((i) & 0xFFFFFFFFLL)) << 32) | ((uint64_t)le32(((i) & 0xFFFFFFFF00000000LL) >> 32)))
//Needs to have log_init() called beforehand. //Needs to have log_init() called beforehand.
void dumpHex(const void* data, size_t size); void dumpHex(const void *data, size_t size);
#ifdef __cplusplus #ifdef __cplusplus
} }