forked from cloudwu/lua-bgfx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
277 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
local bgfx = require "bgfx" | ||
|
||
local common = {} | ||
|
||
local debug | ||
|
||
function common:keypress_cb(key, press) | ||
if press == 0 then | ||
return | ||
end | ||
if key == iup.K_F1 then | ||
debug = not debug | ||
bgfx.set_debug(debug and "S" or "") | ||
elseif key == iup.K_F12 then | ||
bgfx.request_screenshot() | ||
end | ||
end | ||
|
||
local function save_ppm(filename, data, width, height) | ||
local f = assert(io.open(filename, "wb")) | ||
f:write(string.format("P3\n%d %d\n255\n",width, height)) | ||
local offset = 1 | ||
local line = 0 | ||
for i = 1, width*height do | ||
local r,g,b,a,off = string.unpack("BBBB",data,offset) | ||
f:write(r," ",g," ",b," ") | ||
offset = off | ||
line = line + 1 | ||
if line > 8 then | ||
f:write "\n" | ||
line = 0 | ||
end | ||
end | ||
f:close() | ||
end | ||
|
||
function common.save_screenshot(filename) | ||
local name , width, height, data = bgfx.get_screenshot() | ||
if name then | ||
local size = #data | ||
if size ~= width * height * 4 then | ||
-- not RGBA | ||
return | ||
end | ||
print("Save screenshot to ", filename) | ||
save_ppm(filename, data, width, height) | ||
end | ||
end | ||
|
||
return common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#ifndef SIMPLE_LOCK_H | ||
#define SIMPLE_LOCK_H | ||
|
||
#ifdef _MSC_VER | ||
|
||
#include <windows.h> | ||
#define inline __inline | ||
|
||
#define atom_cas_long(ptr, oval, nval) (InterlockedCompareExchange((LONG volatile *)ptr, nval, oval) == oval) | ||
#define atom_cas_pointer(ptr, oval, nval) (InterlockedCompareExchangePointer((PVOID volatile *)ptr, nval, oval) == oval) | ||
#define atom_inc(ptr) InterlockedIncrement((LONG volatile *)ptr) | ||
#define atom_dec(ptr) InterlockedDecrement((LONG volatile *)ptr) | ||
#define atom_add(ptr, n) InterlockedAdd((LONG volatile *)ptr, n) | ||
#define atom_sync() MemoryBarrier() | ||
#define atom_spinlock(ptr) while (InterlockedExchange((LONG volatile *)ptr , 1)) {} | ||
#define atom_spintrylock(ptr) (InterlockedExchange((LONG volatile *)ptr , 1) == 0) | ||
#define atom_spinunlock(ptr) InterlockedExchange((LONG volatile *)ptr, 0) | ||
|
||
#else | ||
|
||
#define atom_cas_long(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval) | ||
#define atom_cas_pointer(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval) | ||
#define atom_inc(ptr) __sync_add_and_fetch(ptr, 1) | ||
#define atom_dec(ptr) __sync_sub_and_fetch(ptr, 1) | ||
#define atom_add(ptr, n) __sync_add_and_fetch(ptr, n) | ||
#define atom_sync() __sync_synchronize() | ||
#define atom_spinlock(ptr) while (__sync_lock_test_and_set(ptr,1)) {} | ||
#define atom_spintrylock(ptr) (__sync_lock_test_and_set(ptr,1) == 0) | ||
#define atom_spinunlock(ptr) __sync_lock_release(ptr) | ||
|
||
#endif | ||
|
||
typedef int spinlock_t; | ||
|
||
/* spin lock */ | ||
#define spin_lock_init(Q) (Q)->lock = 0 | ||
#define spin_lock_destory(Q) | ||
#define spin_lock(Q) atom_spinlock(&(Q)->lock) | ||
#define spin_unlock(Q) atom_spinunlock(&(Q)->lock) | ||
#define spin_trylock(Q) atom_spintrylock(&(Q)->lock) | ||
|
||
/* read write lock */ | ||
|
||
struct rwlock { | ||
int write; | ||
int read; | ||
}; | ||
|
||
static inline void | ||
rwlock_init(struct rwlock *lock) { | ||
lock->write = 0; | ||
lock->read = 0; | ||
} | ||
|
||
static inline void | ||
rwlock_destory(struct rwlock *lock) { | ||
(void)lock; | ||
// to nothing | ||
} | ||
|
||
static inline void | ||
rwlock_rlock(struct rwlock *lock) { | ||
for (;;) { | ||
while(lock->write) { | ||
atom_sync(); | ||
} | ||
atom_inc(&lock->read); | ||
if (lock->write) { | ||
atom_dec(&lock->read); | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
static inline void | ||
rwlock_wlock(struct rwlock *lock) { | ||
atom_spinlock(&lock->write); | ||
while(lock->read) { | ||
atom_sync(); | ||
} | ||
} | ||
|
||
static inline void | ||
rwlock_wunlock(struct rwlock *lock) { | ||
atom_spinunlock(&lock->write); | ||
} | ||
|
||
static inline void | ||
rwlock_runlock(struct rwlock *lock) { | ||
atom_dec(&lock->read); | ||
} | ||
|
||
#endif |