Compare commits

...

2 Commits

Author SHA1 Message Date
2402567330 Added generic button
Some checks reported errors
continuous-integration/drone/push Build encountered an error
2022-06-21 14:53:10 +02:00
a3a1e900b5 Filesystem library 2022-06-21 14:50:39 +02:00
11 changed files with 557 additions and 4 deletions

View File

@ -22,12 +22,15 @@ list(APPEND CommonFiles
blocks/simpleblocks.cpp
mario.cpp
blocks.cpp
gui/gui.cpp
)
list(APPEND SDLLibs
sdlpp)
if(WIN32)
list(APPEND CommonFiles
filesystem/windows/filesystem.cpp)
list(APPEND CommonFiles
../sdlpp/SDL2/SDL2_framerate.c
../sdlpp/SDL2/SDL2_gfxPrimitives.c
@ -51,6 +54,8 @@ if(WIN32)
SDL2_ttf_m
)
else()
list(APPEND CommonFiles
filesystem/unix/filesystem.cpp)
add_executable(mario)
add_executable(editor)
list(APPEND SDLLibs

View File

@ -1,4 +1,5 @@
#include "../sdlpp/sdlpp.hpp"
#include "gui/gui.hpp"
#include "sprites.hpp"
#ifdef _WIN32
#include "../sdlpp/SDL2/SDL2_framerate.h"
@ -56,6 +57,7 @@ struct MouseInfo {
SDLPP::Vec2D<int> edit_box;
SDLPP::Vec2D<int> tool_box;
ToolType::Value tool_type{};
const Button *cur_button{};
};
struct MapInfo {
@ -491,6 +493,7 @@ void getMousePositionFlags(SDLPP::Scene &scene) {
MouseVisitor visitor;
scene.visitCollisions(*mouse, visitor);
global_vars.mouse.cur_button = visitor.getCurButton();
global_vars.mouse.cur_flags = visitor.getFlags();
// + 1 because the left map arrow is on position 0
global_vars.mouse.edit_box =
@ -556,6 +559,9 @@ void mouseUpAction(uint64_t flags, SDLPP::Scene &scene) {
updateToolSelection(global_vars.tool.cur_page_characters - 1,
ToolType::CHARACTER);
}
if(MouseVisitor::button(flags)) {
global_vars.mouse.cur_button->performFunction();
}
}
SDLPP::Vec2D<int> getSelectedObjectIndexes() {
@ -948,12 +954,18 @@ void checkArrowsEnabled(uint64_t cur_page, uint64_t max_page,
}
}
void testButtonFunc(void *input) {
auto actual = static_cast<int*>(input);
std::cout << "NUM: " << *actual << std::endl;
}
#ifdef _WIN32
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PWSTR szCmdLine, int nCmdShow) {
#else
int main() {
#endif
int num = 0;
SDLPP::init();
SDLPP::Window w("Mario editor!");
w.setResizable(true);
@ -1125,6 +1137,10 @@ int main() {
updateToolSelection(0, ToolType::CHARACTER);
setToolColor();
auto button = createButton(0, 0, 0.2, 0.2, renderer, "CLICK ME", "#FFFFFF", "#000000", "#FF0088", testButtonFunc, &num);
button->setStatic();
scene->addObject(button);
auto base = SDL_GetTicks();
int frames = 0;
std::thread inputThread(doInput, scene);
@ -1146,6 +1162,7 @@ int main() {
setFlag(UPDATE_FLAG);
while (!getFlag(QUIT_FLAG)) {
num = (num + 1) % 256;
SDL_framerateDelay(&gFPS);
SDL_PumpEvents();
std::lock_guard<std::mutex> lock(destruction_mutex);

View File

@ -4,6 +4,7 @@
#include "objectids.hpp"
#include "edit_box.hpp"
#include "tool_box.hpp"
#include <memory>
#define SELECTED_LEFT_MAP 0x00000001
#define SELECTED_RIGHT_MAP 0x00000002
@ -15,6 +16,7 @@
#define SELECTED_LEFT_MOD 0x00000080
#define SELECTED_RIGHT_CHARACTER 0x00000100
#define SELECTED_LEFT_CHARACTER 0x00000200
#define SELECTED_BUTTON 0x00000400
void MouseVisitor::visit(const SDLPP::RenderObject &obj) {
auto id = obj.getId();
@ -52,6 +54,10 @@ void MouseVisitor::visit(const SDLPP::RenderObject &obj) {
tool_box_location = dynamic_cast<const ToolBox &>(obj).getIndexes();
tool_box_type = dynamic_cast<const ToolBox &>(obj).getType();
break;
case BUTTON_ID:
select_flags |= SELECTED_BUTTON;
cur_button = reinterpret_cast<const Button*>(&obj);
break;
default:
break;
}
@ -88,6 +94,9 @@ bool MouseVisitor::moveCharactersLeft(uint64_t flags) {
bool MouseVisitor::moveCharactersRight(uint64_t flags) {
return flags & SELECTED_RIGHT_CHARACTER;
}
bool MouseVisitor::button(uint64_t flags) {
return flags & SELECTED_BUTTON;
}
void ToolVisitor::visit(const SDLPP::RenderObject &obj) {
auto id = obj.getCollisions()[0]->getId();

View File

@ -4,6 +4,7 @@
#include "../sdlpp/sdlpp_visitor.hpp"
#include "../sdlpp/sdlpp_geometry.hpp"
#include "blocks.hpp"
#include "gui/gui.hpp"
#include <memory>
struct VisitorType {
@ -46,6 +47,9 @@ public:
uint64_t getToolType() const {
return tool_box_type;
}
const Button *getCurButton() {
return cur_button;
}
static bool moveMapLeft(uint64_t flags);
static bool moveMapRight(uint64_t flags);
@ -55,6 +59,7 @@ public:
static bool moveModsRight(uint64_t flags);
static bool moveCharactersLeft(uint64_t flags);
static bool moveCharactersRight(uint64_t flags);
static bool button(uint64_t flags);
private:
uint64_t select_flags = 0;
@ -64,6 +69,7 @@ private:
SDLPP::Vec2D<int> tool_box_location = { -1, -1 };
uint64_t _type{};
uint64_t tool_box_type = 0;
const Button *cur_button;
};
class ToolVisitor : public SDLPP::Visitor {

127
mario/filesystem.hpp Normal file
View File

@ -0,0 +1,127 @@
#ifndef FSLIB_H
#define FSLIB_H
#include <string>
#ifdef _WIN32
#include <Windows.h>
using string = std::wstring;
using char_t = wchar_t;
#else
#include <dirent.h>
using string = std::string;
using char_t = char;
#endif
// windows version stolen from
// http://www.martinbroadhurst.com/list-the-files-in-a-directory-in-c.html
namespace FSLib {
bool exists( const string &path );
bool isDirectory( const string &path );
bool rename( const string &file_a, const string &file_b );
bool deleteFile( const string &file );
string canonical( const string &path );
bool createDirectoryFull( const string &path );
string getContainingDirectory( const string &path );
string getFileName( const string &path );
string getFileExtension( const string &path );
extern char dir_divisor;
class Directory {
public:
Directory() = delete;
explicit Directory( const string &path_ );
explicit Directory( const Directory &d ) = default;
explicit Directory( Directory &&d ) = default;
class Iterator {
public:
explicit Iterator( const Directory &d_ );
~Iterator();
#ifdef _WIN32
explicit Iterator( bool ended_ );
#else
Iterator( const Directory &d_, const struct dirent *current_entry_ );
#endif
Iterator() = delete;
Iterator( const Iterator &i ) = default;
Iterator( Iterator &&i ) = default;
char_t const *operator*() const;
Iterator &operator++();
bool operator==( const Iterator &i_other ) const;
Iterator operator++( int ) {
Iterator ret( *this );
operator++();
return ret;
}
bool operator!=( const Iterator &i_other ) const {
return !( i_other == *this );
}
private:
#ifndef _WIN32
DIR *d{};
const struct dirent *current_entry{};
#else
HANDLE hFind{};
WIN32_FIND_DATA data{};
bool ended{ false };
#endif
};
using iterator = Iterator;
using const_iterator = Iterator;
iterator end();
const_iterator end() const;
iterator begin() {
return Iterator( *this );
}
const_iterator begin() const {
return Iterator( *this );
}
const_iterator cbegin() const {
return begin();
}
const_iterator cend() const {
return end();
}
const char_t *path() const {
return dir_path.c_str();
}
#ifdef _WIN32
const char_t *validPath() const {
return dir_path.substr( 0, dir_path.length() - 2 ).c_str();
}
#endif
private:
string dir_path;
};
} // namespace FSLib
#endif

View File

@ -0,0 +1,174 @@
#include "../../filesystem.hpp"
#include <cstring>
#include <sys/stat.h>
#include <unistd.h>
#ifdef __APPLE__
#include <sys/syslimits.h>
#endif
#include <stdexcept>
#include <iostream>
char FSLib::dir_divisor = '/';
FSLib::Directory::Directory( const string &path_ ) : dir_path( path_ ) {}
FSLib::Directory::Iterator::Iterator( const Directory &d_ )
: d( opendir( d_.path() ) ) {
if ( !exists( d_.path() ) || !isDirectory( d_.path() ) ) {
throw std::runtime_error(
std::string( "Directory " ) + d_.path() +
" either doesn't exist or isn't a directory" );
}
current_entry = readdir( d );
// skip "." and ".."
if ( current_entry != nullptr &&
( !strcmp( current_entry->d_name, "." ) ||
!strcmp( current_entry->d_name, ".." ) ) )
++( *this );
}
FSLib::Directory::Iterator::Iterator( const Directory &d_,
const struct dirent *current_entry_ )
: d( opendir( d_.path() ) ), current_entry( current_entry_ ) {}
FSLib::Directory::Iterator::~Iterator() {
if ( d )
closedir( d );
}
bool FSLib::exists( const string &path ) {
struct stat path_stat;
return stat( path.c_str(), &path_stat ) == 0;
}
string FSLib::canonical( const string &path ) {
char_t *canonical_path = new char_t[PATH_MAX];
auto failed = realpath( path.c_str(), canonical_path ) == nullptr;
if ( failed ) {
delete[] canonical_path;
return string();
}
string canonical_string{ canonical_path };
delete[] canonical_path;
return canonical_string;
}
bool FSLib::isDirectory( const string &path ) {
struct stat path_stat;
if ( stat( path.c_str(), &path_stat ) != 0 )
return false;
return S_ISDIR( path_stat.st_mode );
}
bool FSLib::rename( const string &file_a, const string &file_b ) {
// TODO log
std::cout << file_a << " -> " << file_b << std::endl;
return ::rename( file_a.c_str(), file_b.c_str() ) == 0;
}
// TODO do windows version
bool deleteRecursive(const string &dir) {
for(const auto &file : FSLib::Directory(dir)) {
auto path = dir + "/" + file;
if(FSLib::isDirectory(path)) {
if(!deleteRecursive(path)) {
return false;
}
} else if(unlink(path.c_str()) != 0) {
return false;
}
}
return rmdir(dir.c_str()) == 0;
}
bool FSLib::deleteFile( const string &file ) {
// TODO log
auto canon = canonical( file );
if(canon.empty()) {
return false;
}
if(isDirectory(canon)) {
return deleteRecursive(canon);
}
return unlink(canon.c_str()) == 0;
}
bool FSLib::createDirectoryFull( const string &path ) {
uint64_t pos{};
// go through all directories leading to the last one
// and create them if they don't exist
do {
// get partial directory path
pos = path.find_first_of( "/", pos );
if ( pos > 0 ) {
auto dirname = path.substr( 0, pos );
// create it if it doesn't exist
if ( !FSLib::exists( dirname ) ) {
if ( mkdir( dirname.c_str(),
S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH ) != 0 )
return false;
}
}
pos++;
} while ( pos < path.length() && pos != 0 );
return true;
}
FSLib::Directory::iterator FSLib::Directory::end() {
return Iterator( *this, nullptr );
}
FSLib::Directory::const_iterator FSLib::Directory::end() const {
return Iterator( *this, nullptr );
}
char_t const *FSLib::Directory::Iterator::operator*() const {
return current_entry->d_name;
}
FSLib::Directory::Iterator &FSLib::Directory::Iterator::operator++() {
if ( current_entry == nullptr )
return *this;
current_entry = readdir( d );
// skip . and ..
if ( current_entry != nullptr &&
( !strcmp( current_entry->d_name, "." ) ||
!strcmp( current_entry->d_name, ".." ) ) )
return operator++();
return *this;
}
bool FSLib::Directory::Iterator::operator==( const Iterator &i_other ) const {
return i_other.current_entry == current_entry;
}
string FSLib::getContainingDirectory( const string &path ) {
auto pos = path.find_last_of('/');
if(pos == string::npos) {
return ".";
}
return path.substr(0, pos);
}
string FSLib::getFileName( const string &path ) {
auto pos = path.find_last_of('/');
if(pos == string::npos) {
return path;
}
return path.substr(pos+1);
}
string FSLib::getFileExtension( const string &path ) {
auto pos = path.find_last_of('.');
if(pos == string::npos) {
return "";
}
return path.substr(pos + 1);
}

View File

@ -0,0 +1,156 @@
#include "../../filesystem.hpp"
#include <Shlwapi.h>
#include <cstring>
#include <stdexcept>
#include <windows.h>
char FSLib::dir_divisor = '\\';
FSLib::Directory::Directory( const string &path_ ) : dir_path( path_ ) {
// need to append \\* for windows to search files in directory
dir_path.append( L"\\*" );
}
FSLib::Directory::Iterator::Iterator( const Directory &d_ ) {
if ( !exists( d_.validPath() ) || !isDirectory( d_.validPath() ) ) {
throw std::runtime_error(
"Directory either doesn't exist or isn't a directory" );
}
hFind = FindFirstFileW( d_.path(), &data );
if ( hFind != INVALID_HANDLE_VALUE ) {
if ( !wcscmp( data.cFileName, L"." ) ||
!wcscmp( data.cFileName, L".." ) ) {
++( *this );
}
} else {
ended = true;
}
}
FSLib::Directory::Iterator::~Iterator() {
if ( hFind )
FindClose( hFind );
}
// this is definitely not a good way to create the "end" iterator
// but it was the only way I thought of with my limited knowledge of
// windows.h
FSLib::Directory::Iterator::Iterator( bool ended_ ) : ended( ended_ ) {}
bool FSLib::exists( const string &path ) {
struct _stat path_stat;
return _wstat( path.c_str(), &path_stat ) == 0;
}
string FSLib::canonical( const string &path ) {
char_t *full_path = new char_t[MAX_PATH];
char_t *canonical_path = new char_t[MAX_PATH];
auto failed = !GetFullPathName( path.c_str(), MAX_PATH, full_path, NULL );
if ( failed ) {
delete[] canonical_path;
delete[] full_path;
return string();
}
failed = !PathCanonicalizeW( canonical_path, full_path );
delete[] full_path;
if ( failed ) {
delete[] canonical_path;
return string();
}
string canonical_string{ canonical_path };
delete[] canonical_path;
return canonical_string;
}
bool FSLib::isDirectory( const string &path ) {
struct _stat path_stat;
if ( _wstat( path.c_str(), &path_stat ) != 0 )
return false;
return path_stat.st_mode & _S_IFDIR;
}
bool FSLib::rename( const string &file_a, const string &file_b ) {
return MoveFileExW( file_a.c_str(), file_b.c_str(),
MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING );
}
bool FSLib::createDirectoryFull( const string &path ) {
uint64_t pos = path.find_first_of( L":", 0 ) + 2;
if ( pos == string::npos )
pos = 0;
// go through all directories leading to the last one
// and create them if they don't exist
do {
// get partial directory path
pos = path.find_first_of( L"\\", pos );
auto dirname = path.substr( 0, pos );
// create it if it doesn't exist
if ( !FSLib::exists( dirname ) ) {
if ( !CreateDirectoryW( dirname.c_str(), NULL ) )
return false;
}
pos++;
} while ( pos < path.length() && pos != 0 );
return true;
}
FSLib::Directory::iterator FSLib::Directory::end() {
return Iterator( true );
}
FSLib::Directory::const_iterator FSLib::Directory::end() const {
return Iterator( true );
}
char_t const *FSLib::Directory::Iterator::operator*() const {
return data.cFileName;
}
FSLib::Directory::Iterator &FSLib::Directory::Iterator::operator++() {
if ( ended == true )
return *this;
// skip . and ..
if ( FindNextFileW( hFind, &data ) == 0 ) {
ended = true;
} else if ( !wcscmp( data.cFileName, L"." ) ||
!wcscmp( data.cFileName, L".." ) ) {
return operator++();
}
return *this;
}
bool FSLib::Directory::Iterator::operator==( const Iterator &i_other ) const {
return i_other.ended == ended;
}
string FSLib::getContainingDirectory( const string &path ) {
auto pos = path.find_last_of('\\');
if(pos == string::npos) {
return ".";
}
return path.substr(0, pos);
}
string FSLib::getFileName( const string &path ) {
auto pos = path.find_last_of('\\');
if(pos == string::npos) {
return path;
}
return path.substr(pos+1);
}
string FSLib::getFileExtension( const string &path ) {
auto pos = path.find_last_of('.');
if(pos == string::npos) {
return "";
}
return path.substr(pos + 1);
}

12
mario/gui/gui.cpp Normal file
View File

@ -0,0 +1,12 @@
#include "gui.hpp"
std::shared_ptr<SDLPP::RectangleRender>
createButton(double x, double y, double w, double h,
std::shared_ptr<SDLPP::Renderer> &r, const std::string &text,
const std::string &color, const std::string &outline_color,
const std::string &background_color,
std::function<void(void *)> click_fun, void *input) {
auto button = std::make_shared<Button>(
x, y, w, h, r, text, color, outline_color, background_color, click_fun, input);
return std::static_pointer_cast<SDLPP::RectangleRender>(button);
}

42
mario/gui/gui.hpp Normal file
View File

@ -0,0 +1,42 @@
#ifndef GUIELEMENTS
#define GUIELEMENTS
#include "../../sdlpp/sdlpp.hpp"
#include "../global_vars.hpp"
#include "../objectids.hpp"
class Button : public SDLPP::TextRenderer {
public:
Button() = delete;
Button(double x, double y, double w, double h,
std::shared_ptr<SDLPP::Renderer> &r, const std::string &text,
const std::string &color, const std::string &outline_color,
const std::string &background_color,
std::function<void(void *)> click_fun, void *input)
: TextRenderer(x, y, w, h, r, g_text_config->getFont(), text, color,
outline_color, 0.2),
click_fun(click_fun), func_input(input) {
setColor(background_color);
setId(BUTTON_ID);
addCollision(SDLPP::RectColider(0, 0, 1, 1));
}
void setBackgroundColor(const std::string &color) {
setColor(color);
}
void performFunction() const {
click_fun(func_input);
}
// TODO highlight visit
private:
std::function<void(void *)> click_fun;
void *func_input;
};
std::shared_ptr<SDLPP::RectangleRender>
createButton(double x, double y, double w, double h,
std::shared_ptr<SDLPP::Renderer> &r, const std::string &text,
const std::string &color, const std::string &outline_color,
const std::string &background_color,
std::function<void(void *)> click_fun, void *input);
#endif

View File

@ -6,6 +6,7 @@
#include "blocks.hpp"
#include "objectids.hpp"
#include "global_vars.hpp"
#include "filesystem.hpp"
#define TERRAIN_TYPE_HAS_ADDITIONAL 0x8
#define WIDE_TERRAIN_HAS_ADDITIONAL 0x8000
@ -101,13 +102,14 @@ void loadMap(std::shared_ptr<SDLPP::Scene> &scene,
std::shared_ptr<SDLPP::RenderObject> &mario,
const std::string &file, std::vector<mapColumnType> &objects,
bool editor, size_t editor_width) {
auto renderer = scene->getRendererShared();
std::ifstream map_file;
map_file.open(file, std::ios::in | std::ios::binary);
if(!map_file.is_open()) {
if(!FSLib::exists(file)) {
// create empty array large enough for initial editor window
objects.resize(editor_width);
return;
}
auto renderer = scene->getRendererShared();
std::ifstream map_file;
map_file.open(file, std::ios::in | std::ios::binary);
uint16_t version;
map_file.read((char *)&version, sizeof(uint16_t) / sizeof(char));
switch(version) {

View File

@ -90,6 +90,9 @@
#define NPC_RIGHT_SIDE_DETECT 0x200A
#define NPC_TOP_DETECT 0x200B
// GUI
#define BUTTON_ID 0xA000
#define EDITOR_EDIT_SQUARE 0xF001
#define EDITOR_MOUSE_ID 0xF002
#define EDITOR_LEFT_MAP_ID 0xF003