aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/3rdparty/buildvm/buildvm.exebin62976 -> 123392 bytes
-rw-r--r--src/3rdparty/minilua/minilua.exebin112640 -> 219136 bytes
-rw-r--r--src/jin/main.cpp25
-rw-r--r--src/libjin/Common/je_pool.hpp168
-rw-r--r--src/libjin/Graphics/animations/je_animation.cpp14
-rw-r--r--src/libjin/Graphics/animations/je_animator.cpp14
-rw-r--r--src/libjin/Graphics/animations/je_animator.h5
-rw-r--r--src/libjin/Graphics/je_color.cpp5
-rw-r--r--src/libjin/Graphics/je_color.h5
-rw-r--r--src/libjin/Graphics/je_graphics.h1
-rw-r--r--src/libjin/Graphics/je_window.cpp22
-rw-r--r--src/libjin/Graphics/je_window.h11
-rw-r--r--src/libjin/Graphics/particles/je_particle.cpp14
-rw-r--r--src/libjin/Graphics/particles/je_particle.h21
-rw-r--r--src/libjin/Graphics/particles/je_particle_pool.cpp0
-rw-r--r--src/libjin/Graphics/particles/je_particle_pool.h36
-rw-r--r--src/libjin/Graphics/particles/je_particle_system.h5
-rw-r--r--src/libjin/Time/je_timer.cpp78
-rw-r--r--src/libjin/Time/je_timer.h73
-rw-r--r--src/libjin/ai/je_behavior_tree.h2
-rw-r--r--src/libjin/ai/je_state_machine.cpp96
-rw-r--r--src/libjin/ai/je_state_machine.h38
-rw-r--r--src/lua/embed/boot.lua.h8
-rw-r--r--src/lua/modules/graphics/je_lua_graphics.cpp21
24 files changed, 484 insertions, 178 deletions
diff --git a/src/3rdparty/buildvm/buildvm.exe b/src/3rdparty/buildvm/buildvm.exe
index 17459f4..c1e069e 100644
--- a/src/3rdparty/buildvm/buildvm.exe
+++ b/src/3rdparty/buildvm/buildvm.exe
Binary files differ
diff --git a/src/3rdparty/minilua/minilua.exe b/src/3rdparty/minilua/minilua.exe
index e6cd530..09b1820 100644
--- a/src/3rdparty/minilua/minilua.exe
+++ b/src/3rdparty/minilua/minilua.exe
Binary files differ
diff --git a/src/jin/main.cpp b/src/jin/main.cpp
index 2f0f9b3..da5b5e7 100644
--- a/src/jin/main.cpp
+++ b/src/jin/main.cpp
@@ -4,13 +4,13 @@
#ifdef _WIN32
#include <Windows.h>
- #include <SDL2/SDL_Main.h>
#include <direct.h>
#include <shlobj.h>
#include <wchar.h>
#endif
#define EXECUTABLE_DIR "./"
+#define DIALOG_TITLE L"Open existing game"
using namespace std;
using namespace JinEngine::Filesystem;
@@ -58,16 +58,17 @@ std::string wstrtostr(const std::wstring &wstr)
std::string BrowseFolder()
{
- string path = EXECUTABLE_DIR;
#ifdef _WIN32
- IFileDialog *pfd = NULL;
+ string path = EXECUTABLE_DIR;
+ IFileDialog* pfd = NULL;
DWORD dwFlags;
- IShellItem *pShellItem = NULL;
+ IShellItem* pItem = NULL;
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr)) goto End;
hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
IID_IFileOpenDialog, reinterpret_cast<void**>(&pfd));
if (FAILED(hr)) goto End;
+ pfd->SetTitle(DIALOG_TITLE);
hr = pfd->GetOptions(&dwFlags);
if (FAILED(hr)) goto End;
hr = pfd->SetOptions(dwFlags | FOS_PICKFOLDERS);
@@ -75,19 +76,19 @@ std::string BrowseFolder()
hr = pfd->Show(NULL);
if (hr == HRESULT_FROM_WIN32(ERROR_CANCELLED))
goto End;
- hr = pfd->GetResult(&pShellItem);
+ hr = pfd->GetResult(&pItem);
if (FAILED(hr)) goto End;
LPWSTR filePath;
- hr = pShellItem->GetDisplayName(SIGDN_FILESYSPATH, &filePath);
- if (FAILED(hr))
- {
- goto End;
- }
+ hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &filePath);
+ if (FAILED(hr)) goto End;
path = wstrtostr(filePath);
CoTaskMemFree(filePath);
-#endif
+ pItem->Release();
+ pfd->Release();
+ CoUninitialize();
End:
return path;
+#endif
}
int main(int argc, char* args[])
@@ -95,9 +96,7 @@ int main(int argc, char* args[])
string cwd = EXECUTABLE_DIR;
#ifdef _WIN32
if (argc > 1)
- {
cwd = args[1];
- }
#endif
else
cwd = BrowseFolder();
diff --git a/src/libjin/Common/je_pool.hpp b/src/libjin/Common/je_pool.hpp
index 1c74eaf..cb96c5b 100644
--- a/src/libjin/Common/je_pool.hpp
+++ b/src/libjin/Common/je_pool.hpp
@@ -1,11 +1,177 @@
+///
+/// An O(1) Object Pool. Original from https://www.codeproject.com/Articles/746630/O-Object-Pool-in-Cplusplus
+///
#ifndef __JE_POOL_H__
#define __JE_POOL_H__
+#include <stdlib.h>
+#include <iostream>
+
+#include "je_types.h"
+
namespace JinEngine
{
+ class DefaultMemoryAllocator
+ {
+ public:
+ static inline void *Allocate(size_t size)
+ {
+ return ::operator new(size, ::std::nothrow);
+ }
+ static inline void Deallocate(void *pointer, size_t size)
+ {
+ ::operator delete(pointer);
+ }
+ };
+
+ template<typename T, class TMemoryAllocator = DefaultMemoryAllocator>
+ class Pool
+ {
+ private:
+ struct _Node
+ {
+ void *_memory;
+ size_t _capacity;
+ _Node *_nextNode;
+
+ _Node(size_t capacity)
+ {
+ if (capacity < 1)
+ throw std::invalid_argument("capacity must be at least 1.");
+
+ _memory = TMemoryAllocator::Allocate(_itemSize * capacity);
+ if (_memory == NULL)
+ throw std::bad_alloc();
+
+ _capacity = capacity;
+ _nextNode = NULL;
+ }
+ ~_Node()
+ {
+ TMemoryAllocator::Deallocate(_memory, _itemSize * _capacity);
+ }
+ };
+
+ void *_nodeMemory;
+ T *_firstDeleted;
+ size_t _countInNode;
+ size_t _nodeCapacity;
+ _Node _firstNode;
+ _Node *_lastNode;
+ size_t _maxBlockLength;
+
+ static const size_t _itemSize;
+
+ Pool(const Pool<T, TMemoryAllocator> &source);
+ void operator = (const Pool<T, TMemoryAllocator> &source);
+
+ void _AllocateNewNode()
+ {
+ size_t size = _countInNode;
+ if (size >= _maxBlockLength)
+ size = _maxBlockLength;
+ else
+ {
+ size *= 2;
+
+ if (size < _countInNode)
+ throw std::overflow_error("size became too big.");
+
+ if (size >= _maxBlockLength)
+ size = _maxBlockLength;
+ }
+
+ _Node *newNode = new _Node(size);
+ _lastNode->_nextNode = newNode;
+ _lastNode = newNode;
+ _nodeMemory = newNode->_memory;
+ _countInNode = 0;
+ _nodeCapacity = size;
+ }
+
+ public:
+ explicit Pool(size_t initialCapacity = 32, size_t maxBlockLength = 1000000)
+ : _firstDeleted(NULL)
+ , _countInNode(0)
+ , _nodeCapacity(initialCapacity)
+ , _firstNode(initialCapacity)
+ , _maxBlockLength(maxBlockLength)
+ {
+ if (maxBlockLength < 1)
+ throw std::invalid_argument("maxBlockLength must be at least 1.");
+
+ _nodeMemory = _firstNode._memory;
+ _lastNode = &_firstNode;
+ }
+ ~Pool()
+ {
+ _Node *node = _firstNode._nextNode;
+ while (node)
+ {
+ _Node *nextNode = node->_nextNode;
+ delete node;
+ node = nextNode;
+ }
+ }
+
+ T *New()
+ {
+ if (_firstDeleted)
+ {
+ T *result = _firstDeleted;
+ _firstDeleted = *((T **)_firstDeleted);
+ new(result) T();
+ return result;
+ }
+
+ if (_countInNode >= _nodeCapacity)
+ _AllocateNewNode();
+
+ char *address = (char *)_nodeMemory;
+ address += _countInNode * _itemSize;
+ T *result = new(address) T();
+ _countInNode++;
+ return result;
+ }
+
+ // This method is useful if you want to call a non-default constructor.
+ // It should be used like this:
+ // new (pool.GetNextWithoutInitializing()) ObjectType(... parameters ...);
+ T *GetNextWithoutInitializing()
+ {
+ if (_firstDeleted)
+ {
+ T *result = (T *)_firstDeleted;
+ _firstDeleted = *((T **)_firstDeleted);
+ return result;
+ }
+
+ if (_countInNode >= _nodeCapacity)
+ _AllocateNewNode();
+
+ char *address = (char *)_nodeMemory;
+ address += _countInNode * _itemSize;
+ _countInNode++;
+ return (T *)address;
+ }
+ void Delete(T *content)
+ {
+ content->~T();
+
+ *((T **)content) = _firstDeleted;
+ _firstDeleted = content;
+ }
+ void DeleteWithoutDestroying(T *content)
+ {
+ *((T **)content) = _firstDeleted;
+ _firstDeleted = content;
+ }
+ };
+ template<typename T, class TMemoryAllocator>
+ const size_t Pool<T, TMemoryAllocator>::_itemSize = ((sizeof(T) + sizeof(void *) - 1) / sizeof(void *)) * sizeof(void *);
-}
+} // namespace JinEngine
#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/animations/je_animation.cpp b/src/libjin/Graphics/animations/je_animation.cpp
index e69de29..4fe673a 100644
--- a/src/libjin/Graphics/animations/je_animation.cpp
+++ b/src/libjin/Graphics/animations/je_animation.cpp
@@ -0,0 +1,14 @@
+#include "je_animation.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Animations
+ {
+
+
+
+ }
+ }
+} \ No newline at end of file
diff --git a/src/libjin/Graphics/animations/je_animator.cpp b/src/libjin/Graphics/animations/je_animator.cpp
index e69de29..360bd5d 100644
--- a/src/libjin/Graphics/animations/je_animator.cpp
+++ b/src/libjin/Graphics/animations/je_animator.cpp
@@ -0,0 +1,14 @@
+#include "je_animator.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Animations
+ {
+
+
+
+ }
+ }
+} \ No newline at end of file
diff --git a/src/libjin/Graphics/animations/je_animator.h b/src/libjin/Graphics/animations/je_animator.h
index 84b0385..6510a7d 100644
--- a/src/libjin/Graphics/animations/je_animator.h
+++ b/src/libjin/Graphics/animations/je_animator.h
@@ -24,8 +24,7 @@ namespace JinEngine
bool hasKey(const std::string& key);
void play();
- void switchAnimationByKey(const std::string& key);
- void switchAnimation(const Animation* clip);
+ void switchAnimation(const std::string& key);
///
/// Control clips.
@@ -44,7 +43,7 @@ namespace JinEngine
};
- }
+ } // namespace Animations
} // namespace Graphics
} // namespace JinEngine
diff --git a/src/libjin/Graphics/je_color.cpp b/src/libjin/Graphics/je_color.cpp
index da48162..c939a1d 100644
--- a/src/libjin/Graphics/je_color.cpp
+++ b/src/libjin/Graphics/je_color.cpp
@@ -13,5 +13,10 @@ namespace JinEngine
const Color Color::MAGENTA = Color(255, 0, 255);
const Color Color::YELLOW = Color(255, 255, 0);
+ const uint32 Color::RMASK = 0x000000ff;
+ const uint32 Color::GMASK = 0x0000ff00;
+ const uint32 Color::BMASK = 0x00ff0000;
+ const uint32 Color::AMASK = 0xff000000;
+
}
} \ No newline at end of file
diff --git a/src/libjin/Graphics/je_color.h b/src/libjin/Graphics/je_color.h
index 9195e48..06b8f61 100644
--- a/src/libjin/Graphics/je_color.h
+++ b/src/libjin/Graphics/je_color.h
@@ -30,6 +30,11 @@ namespace JinEngine
static const Color MAGENTA;
static const Color YELLOW;
+ static const uint32 RMASK;
+ static const uint32 GMASK;
+ static const uint32 BMASK;
+ static const uint32 AMASK;
+
///
/// Get lerp color with given factor.
///
diff --git a/src/libjin/Graphics/je_graphics.h b/src/libjin/Graphics/je_graphics.h
index d9e0c0a..52bfbda 100644
--- a/src/libjin/Graphics/je_graphics.h
+++ b/src/libjin/Graphics/je_graphics.h
@@ -32,6 +32,5 @@
// int64 textureMemory;
//};
-
#endif // defined(jin_graphics)
#endif // __JE_GRAPHICS_H__ \ No newline at end of file
diff --git a/src/libjin/Graphics/je_window.cpp b/src/libjin/Graphics/je_window.cpp
index 86ccd29..c14d290 100644
--- a/src/libjin/Graphics/je_window.cpp
+++ b/src/libjin/Graphics/je_window.cpp
@@ -3,6 +3,7 @@
#include <iostream>
+#include "../common/je_exception.h"
#include "../utils/je_utils.h"
#include "../audio/sdl/je_sdl_audio.h"
#include "../utils/je_log.h"
@@ -30,8 +31,9 @@ namespace JinEngine
mSize.w = setting->width;
mSize.h = setting->height;
mFps = setting->fps;
- bool vsync = setting->vsync;
- const char* title = setting->title;
+ bool vsync = setting->vsync;
+ const char* title = setting->title;
+ const char* icon = setting->icon;
if (mWnd)
{
@@ -58,13 +60,27 @@ namespace JinEngine
int wx = SDL_WINDOWPOS_UNDEFINED,
wy = SDL_WINDOWPOS_UNDEFINED;
- int flag = SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL;
+ int flag = SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL;
if (setting->fullscreen) flag |= SDL_WINDOW_FULLSCREEN;
if (setting->resizable) flag |= SDL_WINDOW_RESIZABLE;
mWnd = SDL_CreateWindow(title, wx, wy, mSize.w, mSize.h, flag);
if (mWnd == NULL)
return false;
+
+ // Set window icon
+ try
+ {
+ Bitmap* bitmap = Bitmap::createBitmap(icon);
+ SDL_Surface *surface;
+ Color* pixels = const_cast<Color*>(bitmap->getPixels());
+ uint w = bitmap->getWidth(), h = bitmap->getHeight();
+ surface = SDL_CreateRGBSurfaceFrom(
+ pixels, w, h, 32, w * 4, Color::RMASK, Color::GMASK, Color::BMASK, Color::AMASK);
+ SDL_SetWindowIcon(mWnd, surface);
+ SDL_FreeSurface(surface);
+ } catch (...) {}
+
ctx = SDL_GL_CreateContext(mWnd);
if (ctx == NULL)
return false;
diff --git a/src/libjin/Graphics/je_window.h b/src/libjin/Graphics/je_window.h
index 7ca1e5e..436fd24 100644
--- a/src/libjin/Graphics/je_window.h
+++ b/src/libjin/Graphics/je_window.h
@@ -26,6 +26,7 @@ namespace JinEngine
{
public:
const char* title; ///< window title
+ const char* icon; ///< window icon
bool fullscreen; ///< full screen
int width, height; ///< window size
bool vsync; ///< vsync
@@ -58,6 +59,16 @@ namespace JinEngine
///
void swapBuffers();
+ ///
+ ///
+ ///
+ inline void hide() { SDL_HideWindow(mWnd); };
+
+ ///
+ ///
+ ///
+ void show() { SDL_ShowWindow(mWnd); };
+
private:
// declare a singleton
diff --git a/src/libjin/Graphics/particles/je_particle.cpp b/src/libjin/Graphics/particles/je_particle.cpp
index e69de29..9179093 100644
--- a/src/libjin/Graphics/particles/je_particle.cpp
+++ b/src/libjin/Graphics/particles/je_particle.cpp
@@ -0,0 +1,14 @@
+#include "je_particle.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Particles
+ {
+
+
+
+ }
+ }
+} \ No newline at end of file
diff --git a/src/libjin/Graphics/particles/je_particle.h b/src/libjin/Graphics/particles/je_particle.h
index 3dd480e..73803e8 100644
--- a/src/libjin/Graphics/particles/je_particle.h
+++ b/src/libjin/Graphics/particles/je_particle.h
@@ -2,6 +2,7 @@
#define __JE_PARTICLE_H__
#include "../../math/je_vector2.hpp"
+#include "../je_color.h"
namespace JinEngine
{
@@ -12,6 +13,9 @@ namespace JinEngine
class ParticleEmitter;
+ ///
+ ///
+ ///
struct LifeTimeDef
{
bool enableRandom = false;
@@ -25,11 +29,17 @@ namespace JinEngine
} life;
};
+ ///
+ ///
+ ///
struct LinearAccelaration
{
};
+ ///
+ ///
+ ///
struct SpeedOverTimeDef
{
bool enable = false;
@@ -100,7 +110,16 @@ namespace JinEngine
///
struct Particle
{
- Particle(const ParticleDef& particleDef);
+ ///
+ /// Default constructor.
+ ///
+ Particle();
+
+ ///
+ /// Initialize with given definition.
+ ///
+ void initialize(const ParticleDef& particleDef);
+
///
/// Whole life time.
///
diff --git a/src/libjin/Graphics/particles/je_particle_pool.cpp b/src/libjin/Graphics/particles/je_particle_pool.cpp
deleted file mode 100644
index e69de29..0000000
--- a/src/libjin/Graphics/particles/je_particle_pool.cpp
+++ /dev/null
diff --git a/src/libjin/Graphics/particles/je_particle_pool.h b/src/libjin/Graphics/particles/je_particle_pool.h
index 6bd41e0..46cd73a 100644
--- a/src/libjin/Graphics/particles/je_particle_pool.h
+++ b/src/libjin/Graphics/particles/je_particle_pool.h
@@ -1,7 +1,7 @@
#ifndef __JE_PARTICLE_BATCH_H__
#define __JE_PARTICLE_BATCH_H__
-#include <list>
+#include "../../common/je_pool.hpp"
#include "je_particle.h"
@@ -15,39 +15,7 @@ namespace JinEngine
///
/// Particle pool for reducing memory fragmentation.
///
- class ParticlePool
- {
- public:
-
- ///
- /// Particle pool constructor.
- ///
- /// @param count Max count of particles.
- ///
- ParticlePool(uint count);
-
- ///
- /// Particle pool destructor.
- ///
- ~ParticlePool();
-
- ///
- /// Claim a particle if available.
- ///
- Particle* claim();
-
- ///
- /// Recycle particle if the particle is no more alive.
- ///
- void recycle(Particle* particle);
-
- private:
- ///
- /// All particles include available and inavailable particles.
- ///
- std::list<Particle> particles;
-
- };
+ typedef Pool<Particle> ParticlePool;
} // namespace Particles
} // namespace Graphics
diff --git a/src/libjin/Graphics/particles/je_particle_system.h b/src/libjin/Graphics/particles/je_particle_system.h
index 32123a6..fa050f7 100644
--- a/src/libjin/Graphics/particles/je_particle_system.h
+++ b/src/libjin/Graphics/particles/je_particle_system.h
@@ -63,7 +63,10 @@ namespace JinEngine
///
/// Release particle and make it available in particle pool.
///
- void releaseParticle();
+ void releaseParticle()
+ {
+ Particle*p = mParticlePool.New();
+ }
private:
// Disable default constructor.
diff --git a/src/libjin/Time/je_timer.cpp b/src/libjin/Time/je_timer.cpp
index a2f2486..94ab747 100644
--- a/src/libjin/Time/je_timer.cpp
+++ b/src/libjin/Time/je_timer.cpp
@@ -9,52 +9,55 @@ namespace JinEngine
{
- Timers::Timers()
- : timers()
+ Timer::Timer()
+ : mHandlers()
{
}
- Timers::~Timers()
+ Timer::~Timer()
{
- for (int i = 0; i < timers.size(); ++i)
- delete timers[i];
+ for (int i = 0; i < mHandlers.size(); ++i)
+ delete mHandlers[i];
}
- void Timers::update(int ms)
+ void Timer::update(int ms)
{
- std::vector<Timer*>::iterator it = timers.begin();
- for (; it != timers.end(); )
- {
- if (!(*it)->process(ms))
- {
- Timer* t = *it;
- timers.erase(it);
- delete t;
- return;
- }
- ++it;
- }
+ // Process handler.
+ std::vector<Handler*>::iterator it = mHandlers.begin();
+ for (; it != mHandlers.end(); ++it)
+ (*it)->process(ms);
+ // Erase canceled handler.
+ for (it = mHandlers.begin(); it != mHandlers.end();)
+ {
+ if ((*it)->canceled)
+ it = mHandlers.erase(it);
+ else
+ ++it;
+ }
}
- void Timers::every(int ms, timer_callback callback, void* p)
+ Timer::Handler* Timer::every(int ms, TimerCallback callback, void* p)
{
- Timer* t = new Timer(Timer::EVERY, ms, 0, callback, p);
- timers.push_back(t);
+ Handler* t = new Handler(Handler::EVERY, ms, 0, callback, p);
+ mHandlers.push_back(t);
+ return t;
}
- void Timers::after(int ms, timer_callback callback, void* p)
+ Timer::Handler* Timer::after(int ms, TimerCallback callback, void* p)
{
- Timer* t = new Timer(Timer::AFTER, ms, 0, callback, p);
- timers.push_back(t);
+ Handler* t = new Handler(Handler::AFTER, ms, 0, callback, p);
+ mHandlers.push_back(t);
+ return t;
}
- void Timers::repeat(int ms, int count, timer_callback callback, void* p)
+ Timer::Handler* Timer::repeat(int ms, int count, TimerCallback callback, void* p)
{
- Timer* t = new Timer(Timer::REPEAT, ms, count, callback, p);
- timers.push_back(t);
+ Handler* t = new Handler(Handler::REPEAT, ms, count, callback, p);
+ mHandlers.push_back(t);
+ return t;
}
- Timers::Timer::Timer(Type t, int d, int c, timer_callback f, void* p)
+ Timer::Handler::Handler(Type t, int d, int c, TimerCallback f, void* p)
: type(t)
, duration(d)
, count(c)
@@ -62,14 +65,15 @@ namespace JinEngine
, countdown(c)
, callback(f)
, paramters(p)
+ , canceled(false)
{
}
- Timers::Timer::~Timer()
+ Timer::Handler::~Handler()
{
}
- bool Timers::Timer::process(int ms)
+ void Timer::Handler::process(int ms)
{
tickdown -= ms;
if (tickdown <= 0)
@@ -82,19 +86,23 @@ namespace JinEngine
}
else if (type == AFTER)
{
- return false;
+ canceled = true;
}
else if (type == REPEAT)
{
--countdown;
- if (countdown <= 0)
- return false;
+ if (countdown <= 0)
+ canceled = true;
}
}
- return true;
}
+ void Timer::cancel(Handler* handler)
+ {
+ handler->canceled = true;
+ }
+
} // namespace Time
} // namespace JinEngine
-#endif // defined(jin_time)
+#endif // defined(jin_time) \ No newline at end of file
diff --git a/src/libjin/Time/je_timer.h b/src/libjin/Time/je_timer.h
index b558a55..d0e5513 100644
--- a/src/libjin/Time/je_timer.h
+++ b/src/libjin/Time/je_timer.h
@@ -14,68 +14,77 @@ namespace JinEngine
///
///
///
- class Timers
+ class Timer
{
public:
- typedef void(*timer_callback)(void* prameters);
+
+ typedef void(*TimerCallback)(void* prameters);
///
///
///
- Timers();
+ class Handler
+ {
+ private:
+ friend class Timer;
+ enum Type
+ {
+ EVERY,
+ AFTER,
+ REPEAT,
+ };
+ Handler(Type type, int duration, int count = 0, TimerCallback callback = nullptr, void* paramters = nullptr);
+ virtual ~Handler();
+ void process(int ms);
+
+ int duration;
+ int count;
+ int tickdown;
+ int countdown;
+ Type type;
+ TimerCallback callback;
+ void* paramters;
+ bool canceled;
+ };
///
///
///
- ~Timers();
+ Timer();
///
///
///
- void update(int ms);
+ ~Timer();
///
///
///
- void every(int ms, timer_callback callback, void* paramters);
+ void update(int ms);
///
///
///
- void after(int ms, timer_callback callback, void* paramters);
+ Handler* every(int ms, TimerCallback callback, void* paramters);
///
///
///
- void repeat(int ms, int count, timer_callback callback, void* paramters);
+ Handler* after(int ms, TimerCallback callback, void* paramters);
- private:
+ ///
+ ///
+ ///
+ Handler* repeat(int ms, int count, TimerCallback callback, void* paramters);
///
///
///
- class Timer
- {
- public:
- enum Type
- {
- EVERY,
- AFTER,
- REPEAT,
- };
- Timer(Type type, int duration, int count = 0, timer_callback callback = nullptr, void* paramters = nullptr);
- virtual ~Timer();
- bool process(int ms);
- private:
- int duration;
- int count;
- int tickdown;
- int countdown;
- Type type;
- timer_callback callback;
- void* paramters;
- };
- std::vector<Timer*> timers;
+ void cancel(Handler* handler = nullptr);
+
+ private:
+
+ std::vector<Handler*> mHandlers;
};
@@ -114,4 +123,4 @@ namespace JinEngine
#endif // defined(jin_time)
-#endif // __JE_TIMER_H__
+#endif // __JE_TIMER_H__ \ No newline at end of file
diff --git a/src/libjin/ai/je_behavior_tree.h b/src/libjin/ai/je_behavior_tree.h
index cb518f0..8621696 100644
--- a/src/libjin/ai/je_behavior_tree.h
+++ b/src/libjin/ai/je_behavior_tree.h
@@ -9,8 +9,6 @@ namespace JinEngine
namespace AI
{
-
-
///
///
///
diff --git a/src/libjin/ai/je_state_machine.cpp b/src/libjin/ai/je_state_machine.cpp
index 8f8bd81..9b766b5 100644
--- a/src/libjin/ai/je_state_machine.cpp
+++ b/src/libjin/ai/je_state_machine.cpp
@@ -88,7 +88,6 @@ namespace JinEngine
mMode = mode;
}
- //ģʽ״̬
void StateMachine::update()
{
switch (mMode)
@@ -109,10 +108,42 @@ namespace JinEngine
Parameter& p = it->second;
switch (p.type)
{
- case ParameterType::Int: return p.value._int == condition.value._int;
- case ParameterType::Float: return p.value._float == condition.value._float;
- case ParameterType::Bool: return p.value._bool == condition.value._bool;
- case ParameterType::Trigger: return p.value._trigger == true;
+ case ParameterType::Int:
+ {
+ int value = p.value._int;
+ int cvalue = condition.value._int;
+ bool is = false;
+ is |= ((condition.expression & ParameterExpression::INT_BIGGER) ? value > cvalue : false);
+ is |= ((condition.expression & ParameterExpression::INT_EQUAL) ? value == cvalue : false);
+ is |= ((condition.expression & ParameterExpression::INT_SMALLER) ? value < cvalue : false);
+ return is;
+ }
+ case ParameterType::Float:
+ {
+ float value = p.value._float;
+ float cvalue = condition.value._float;
+ bool is = false;
+ is |= ((condition.expression & ParameterExpression::FLOAT_BIGGER) ? value > cvalue : false);
+ is |= ((condition.expression & ParameterExpression::FLOAT_EQUAL) ? value == cvalue : false);
+ is |= ((condition.expression & ParameterExpression::FLOAT_SMALLER) ? value < cvalue : false);
+ return is;
+ }
+ case ParameterType::Bool:
+ {
+ bool value = p.value._bool;
+ bool cvalue = condition.value._bool;
+ bool is = false;
+ is |= ((condition.expression & ParameterExpression::BOOL_IS) ? value == cvalue : false);
+ is |= ((condition.expression & ParameterExpression::BOOL_NOT) ? value != cvalue : false);
+ return is;
+ }
+ case ParameterType::Trigger:
+ {
+ bool is = p.value._trigger;
+ // Close trigger.
+ p.value._trigger = false;
+ return is;
+ }
}
return false;
}
@@ -197,7 +228,7 @@ namespace JinEngine
}
}
- void StateMachine::addTransitioni(const std::string& stateFrom, const std::string& stateTo, const std::string& name, int value)
+ void StateMachine::addTransitioni(const std::string& stateFrom, const std::string& stateTo, const std::string& name, ParameterExpression expression, int value)
{
map<string, State>::iterator it;
it = mStates.find(stateFrom);
@@ -224,17 +255,18 @@ namespace JinEngine
Parameter& parameter = itp->second;
if (parameter.type != ParameterType::Int)
{
- jin_log_error("The type of parameter called %s is %s, but the transition gives a int value.", name, parameterTypeString(parameter.type));
+ jin_log_error("The type of parameter %s is %s, but the transition gives a int value.", name, parameterTypeString(parameter.type));
return;
}
- Transition trasition;
- trasition.condition.parameter = name;
- trasition.condition.value._int = value;
- trasition.state = stateTo;
- from.transitions.push_back(trasition);
+ Transition transition;
+ transition.condition.parameter = name;
+ transition.condition.expression = expression;
+ transition.condition.value._int = value;
+ transition.state = stateTo;
+ from.transitions.push_back(transition);
}
- void StateMachine::addTransitionf(const std::string& stateFrom, const std::string& stateTo, const std::string& name, float value)
+ void StateMachine::addTransitionf(const std::string& stateFrom, const std::string& stateTo, const std::string& name, ParameterExpression expression, float value)
{
map<string, State>::iterator it;
it = mStates.find(stateFrom);
@@ -261,17 +293,18 @@ namespace JinEngine
Parameter& parameter = itp->second;
if (parameter.type != ParameterType::Float)
{
- jin_log_error("The type of parameter called %s is %s, but the transition gives a float value.", name, parameterTypeString(parameter.type));
+ jin_log_error("The type of parameter %s is %s, but the transition gives a float value.", name, parameterTypeString(parameter.type));
return;
}
- Transition trasition;
- trasition.condition.parameter = name;
- trasition.condition.value._float = value;
- trasition.state = stateTo;
- from.transitions.push_back(trasition);
+ Transition transition;
+ transition.condition.parameter = name;
+ transition.condition.expression = expression;
+ transition.condition.value._float = value;
+ transition.state = stateTo;
+ from.transitions.push_back(transition);
}
- void StateMachine::addTransitionb(const std::string& stateFrom, const std::string& stateTo, const std::string& name, bool value)
+ void StateMachine::addTransitionb(const std::string& stateFrom, const std::string& stateTo, const std::string& name, ParameterExpression expression, bool value)
{
map<string, State>::iterator it;
it = mStates.find(stateFrom);
@@ -298,14 +331,15 @@ namespace JinEngine
Parameter& parameter = itp->second;
if (parameter.type != ParameterType::Bool)
{
- jin_log_error("The type of parameter called %s is %s, but the transition gives a bool value.", name, parameterTypeString(parameter.type));
+ jin_log_error("The type of parameter %s is %s, but the transition gives a bool value.", name, parameterTypeString(parameter.type));
return;
}
- Transition trasition;
- trasition.condition.parameter = name;
- trasition.condition.value._bool = value;
- trasition.state = stateTo;
- from.transitions.push_back(trasition);
+ Transition transition;
+ transition.condition.parameter = name;
+ transition.condition.expression = expression;
+ transition.condition.value._bool = value;
+ transition.state = stateTo;
+ from.transitions.push_back(transition);
}
void StateMachine::addTransitiont(const std::string& stateFrom, const std::string& stateTo, const std::string& name)
@@ -335,7 +369,7 @@ namespace JinEngine
Parameter& parameter = itp->second;
if (parameter.type != ParameterType::Trigger)
{
- jin_log_error("The type of parameter called %s is %s, but the transition gives a trigger value.", name, parameterTypeString(parameter.type));
+ jin_log_error("The type of parameter %s is %s, but the transition gives a trigger value.", name, parameterTypeString(parameter.type));
return;
}
Transition trasition;
@@ -356,7 +390,7 @@ namespace JinEngine
Parameter& p = it->second;
if (p.type != ParameterType::Int)
{
- jin_log_error("The type of parameter called %s is %s, but try to assign a int value to it", name, parameterTypeString(p.type));
+ jin_log_error("The type of parameter %s is %s, but try to assign a int value to it", name, parameterTypeString(p.type));
return;
}
p.value._int = value;
@@ -373,7 +407,7 @@ namespace JinEngine
Parameter& p = it->second;
if (p.type != ParameterType::Float)
{
- jin_log_error("The type of parameter called %s is %s, but try to assign a float value to it", name, parameterTypeString(p.type));
+ jin_log_error("The type of parameter %s is %s, but try to assign a float value to it", name, parameterTypeString(p.type));
return;
}
p.value._float = value;
@@ -390,7 +424,7 @@ namespace JinEngine
Parameter& p = it->second;
if (p.type != ParameterType::Bool)
{
- jin_log_error("The type of parameter called %s is %s, but try to assign a bool value to it", name, parameterTypeString(p.type));
+ jin_log_error("The type of parameter %s is %s, but try to assign a bool value to it", name, parameterTypeString(p.type));
return;
}
p.value._bool = value;
@@ -407,7 +441,7 @@ namespace JinEngine
Parameter& p = it->second;
if (p.type != ParameterType::Trigger)
{
- jin_log_error("The type of parameter called %s is %s, but try to assign a trigger value to it", name, parameterTypeString(p.type));
+ jin_log_error("The type of parameter %s is %s, but try to assign a trigger value to it", name, parameterTypeString(p.type));
return;
}
p.value._trigger = true;
diff --git a/src/libjin/ai/je_state_machine.h b/src/libjin/ai/je_state_machine.h
index 7d638c4..1765e97 100644
--- a/src/libjin/ai/je_state_machine.h
+++ b/src/libjin/ai/je_state_machine.h
@@ -34,6 +34,23 @@ namespace JinEngine
///
///
///
+ enum ParameterExpression
+ {
+ INT_BIGGER = 0x02,
+ INT_SMALLER = 0x04,
+ INT_EQUAL = 0x08,
+
+ FLOAT_BIGGER = 0x10,
+ FLOAT_SMALLER = 0x20,
+ FLOAT_EQUAL = 0x40,
+
+ BOOL_IS = 0x80,
+ BOOL_NOT = 0x100,
+ };
+
+ ///
+ ///
+ ///
typedef void(StateChangeCallback)(void* userdata);
///
@@ -104,17 +121,17 @@ namespace JinEngine
///
///
///
- void addTransitioni(const std::string& stateFrom, const std::string& stateTo, const std::string& name, int value);
+ void addTransitioni(const std::string& stateFrom, const std::string& stateTo, const std::string& name, ParameterExpression condition, int value);
///
///
///
- void addTransitionf(const std::string& stateFrom, const std::string& stateTo, const std::string& name, float value);
+ void addTransitionf(const std::string& stateFrom, const std::string& stateTo, const std::string& name, ParameterExpression condition, float value);
///
///
///
- void addTransitionb(const std::string& stateFrom, const std::string& stateTo, const std::string& name, bool value);
+ void addTransitionb(const std::string& stateFrom, const std::string& stateTo, const std::string& name, ParameterExpression condition, bool value);
///
///
@@ -200,21 +217,6 @@ namespace JinEngine
ParameterValue value;
};
- enum ParameterExpression
- {
- //
- INT_BIGGER = 0x02,
- INT_SMALLER = 0x04,
- INT_EQUAL = 0x08,
- //
- FLOAT_BIGGER = 0x10,
- FLOAT_SMALLER = 0x20,
- FLOAT_EQUAL = 0x40,
- //
- BOOL_IS = 0x80,
- BOOL_NOT = 0x100,
- };
-
///
/// Traslation's condition.
///
diff --git a/src/lua/embed/boot.lua.h b/src/lua/embed/boot.lua.h
index 2369b0f..0884188 100644
--- a/src/lua/embed/boot.lua.h
+++ b/src/lua/embed/boot.lua.h
@@ -19,6 +19,7 @@ jin.config.title = jin.config.title or ("jin v" .. jin.version)
jin.config.resizable = jin.config.resizable or false
jin.config.fullscreen = jin.config.fullscreen or false
jin.config.fps = jin.config.fps or 60
+jin.config.icon = jin.config.icon or ""
-------------------------------------------------------------------------
-- Default game loop
@@ -62,6 +63,7 @@ end
-- Display error message.
local function onError(msg)
+ jin.graphics.showWindow()
local err = "Error:\n" .. msg .. "\n" .. debug.traceback()
jin.graphics.reset()
jin.graphics.setClearColor(100, 100, 100, 255)
@@ -80,7 +82,7 @@ end
-- No game screen.
local function noGame()
- jin.graphics.reset()
+ jin.graphics.showWindow()
jin.graphics.reset()
jin.graphics.setClearColor(100, 100, 100, 255)
jin.graphics.clear()
@@ -114,6 +116,10 @@ end
jin.audio.init()
jin.graphics.init(jin.config)
+-------------------------------------------------------------------------
+-- Boot game
+-------------------------------------------------------------------------
+
xpcall(boot, onError)
-------------------------------------------------------------------------
diff --git a/src/lua/modules/graphics/je_lua_graphics.cpp b/src/lua/modules/graphics/je_lua_graphics.cpp
index 7efb2e7..c45a939 100644
--- a/src/lua/modules/graphics/je_lua_graphics.cpp
+++ b/src/lua/modules/graphics/je_lua_graphics.cpp
@@ -42,6 +42,7 @@ namespace JinEngine
setting.width = luax_getfieldinteger(L, 1, "width");
setting.height = luax_getfieldinteger(L, 1, "height");
setting.title = luax_getfieldstring(L, 1, "title");
+ setting.icon = luax_getfieldstring(L, 1, "icon");
setting.vsync = luax_getfieldbool(L, 1, "vsync");
setting.fullscreen = luax_getfieldbool(L, 1, "fullscreen");
setting.resizable = luax_getfieldbool(L, 1, "resizable");
@@ -77,7 +78,21 @@ namespace JinEngine
wnd->quit();
return 0;
}
-
+
+ LUA_IMPLEMENT int l_showWindow(lua_State* L)
+ {
+ Window* wnd = Window::get();
+ wnd->show();
+ return 0;
+ }
+
+ LUA_IMPLEMENT int l_hideWindow(lua_State* L)
+ {
+ Window* wnd = Window::get();
+ wnd->hide();
+ return 0;
+ }
+
LUA_IMPLEMENT int l_getSize(lua_State* L)
{
Window* wnd = Window::get();
@@ -742,7 +757,9 @@ namespace JinEngine
{ "getSize", l_getSize },
{ "getWidth", l_getWidth },
{ "getHeight", l_getHeight },
- { "destroy", l_destroy },
+ { "destroy", l_destroy },
+ { "hideWindow", l_hideWindow },
+ { "showWindow", l_showWindow },
/* creators */
{ "newBitmap", l_newBitmap },
{ "newTexture", l_newTexture },