code style

顯示具有 Work 標籤的文章。 顯示所有文章
顯示具有 Work 標籤的文章。 顯示所有文章

1/12/2015

也來寫卡牌遊戲好了(二)

沒想到我也學富堅一樣拖稿拖超久!


咳! 好Der~ 今天我們來繼續談談怎麼寫卡牌遊戲。

什麼?! 你說卡牌遊戲已經過時了?!?! 現在都是什麼刀什麼塔的!

咳! 好Der~ 今天我們就來談談中國手遊什麼的如何砸錢買廣告。(喂)


呃....那今天要說什麼? (翻翻筆記、抓抓頭)

我覺得是這樣啦,我相信台北市民的智慧,應該是不會在乎要講什麼,這一路走來,不管面對再多的劣勢,我們始終堅持打一場正面的遊戲開發。(喂!!!!)



咳! 好Der~ 今天我們來討論做遊戲的時候要用什麼工具來管理數據資料。 對!就是SQLite、SQLite、SQLite,很重要,所以要講三次。


SQLite就是專門用於獨立單機架構的資料庫系統。輕便短小、沒有負擔、讓人幾乎忘了它的存在。你還在用csv檔案來儲存角色數值嗎?你還在為了資料排序而寫了一堆sorting algorithm嗎?


用了SQLite,馬上就能解決您過去的困擾 (然後多了更多要如何好好利用SQLite的煩惱)(登愣)

首先,效率問題!SQLite是將資料儲存在檔案系統,所以File IO可能會是個問題。幸好,SQLite有提供memory function,可以將SQLite搬到RAM上面做操作,因為現在記憶體夠大,小小的遊戲數值應該是佔不了多少空間。可以參考這邊的作法 link

通常遊戲中的動態數據,例如玩家資料、關卡進度、道具數量,是從遊戲server傳回client儲存。而靜態數據,例如卡片設定、關卡設定、道具設定...等等,往往變動幅度不大,如果每次玩家進行遊戲都得重複下載這些數據,就有點浪費大家的時間與頻寬。比較偷懶的作法就是在更新數據的時候讓server自動產生靜態數據db,然後再通知client下載這個db。
這邊提供一個mysql轉換成sqlite的script。

因為靜態數據db會存在client,相信很多人不想讓別人看到你db裡面放了什麼鬼東西,所以我們就得加密db。免費的SQLite雖然有提供加密的界面,但是並沒有實作;官方的實作版本得花錢購買。幸好,wxSQLite3有提供加密的功能,省去我們自己DIY的功夫。

SQLite CLI的加密方式如下
$~/sqlite3secure source.sqlite
.backup /tmp/temp.txt
attach database "encrypted.sqlite" as x key "password";
.restore x /tmp/temp.txt
.quit

好了,到這邊SQLite的基礎建設差不多都搞定了。接下來就是讓開發人員開開心心地在client裡面下SQL指令去查詢遊戲數據囉~ 可喜可賀!
(真的有這麼輕鬆寫意嗎?  噓~~~ 不要傷了皇「城之內」的和氣)

至於用ORM方便物件導向操作什麼的,我只能說「真男人都是直接下SQL指令抓raw data來用Der」
(其實是我沒空研究啦)


時間也差不多了, 咱們下回再見 (天線寶寶說掰掰)

6/25/2013

SquareEnix 三國志亂舞 程式架構分析

三國志亂舞

SE社很貼心地在遊戲中放了版權宣告

cocos2d-iphone  BJ4

FBEncryptor  AES加密工具

stig-json-framework  json工具

ziparchive  iOS zip utility
 
css-refresh  CSS開發工具

cubiq-iscroll  javascript touch event tool

jQuery  BJ4

brian-c/phantom-limb  在PC上模擬touch event

sizzle  CSS selector engine
 
SWFObject  javascript flash player

所以整個遊戲應該是用cocos2d-iphone + html5建構出來的

1/08/2012

Create Sprite Sheets with TexturePacker


2D game為了提高效能,通常會將一個個sprite集合成一張大圖,也就是所謂的sprite sheet。3D game也是如此,只不過換了個名詞:sprite換成texture、sheet換成atlas。
Picture 1. Sprite Sheet Example

拼圖的演算法有數個,這裡舉個簡單例子:
每插入一個新的sprite,該區塊將會產生兩個更小的區塊,依此類推。如果sprite都無法插入每個小區塊,則表示sheet不夠大,必須將sheet放大重新來過。
Algorithm Example


如果你想在iOS上寫2D game的話,cocos2d是一套相當不錯的開源軟體,當然它也提供了sprite sheet的功能。那麼問題就在於要如何產生sprite sheet?其實市面上已經有一堆現成工具讓使用者能夠方便快速地製作sprite sheet。這裡我會介紹TexturePacker這套跨平台免費軟體(高階功能需要付費)。

工商服務時間




主要支援框架


支援貼圖格式

  • Flash (.swf) – consisting of single frames
  • PNG
  • JPG
  • TIFF
  • BMP

支援輸出格式

  • PNG – Portable Network Graphics
  • PVR – iPhone’s native image format
  • PVR.CCZ – zlib compressed version of PVR for cocos2d
  • PVR.GZ – gnu zip compressed PVR files
  • JPG – JPEG


TexturePacker提供兩種排版的演算法:

Basic
這是最基本的演算法,簡單無腦地依序插入sprite,直到填滿為止。免費版本只提供Basic排版。

MaxRects
MaxRects是一種快速、高密度的排版演算法。開啟sprite旋轉與修剪的功能可以得到更好的結果。TexturePackerPro/CLI版本可以使用這個演算法(付費版本)

如果對這些演算法有興趣,可以參考這個網頁(link)。

Dithering
  • RGBA8888, BGRA8888 – 32bit, 256 colors per channel, 16777216 colors, 256 transparency values
  • RGBA5555
  • RGBA5551 – 16bit, 32 colors per channel, 32768 colors, 1 “hard” transparency value
  • RGBA4444 – 16bit, 16 colors per channel, 4096 colors, 16 transparency values
  • RGB565 – 16bit, 65536 colors, no transparency
  • PVRTC2 – lossy compression for iPhone(付費版本)
  • PVRTC4 – lossy compression for iPhone(付費版本)

Trimming
修剪功能可以移除sprite周圍的透明像素,可以縮減貼圖大小、提高繪圖效能。2.3.5版本以前是免費,之後就改成付費版本了。

除此之外,TexturePacker支援command line,可以讓開發者在建置專案的過程中,同時把需要的sprite sheets給自動產生出來。因此也不需要版本控管這些sheets啦。

Tips
  • 使用PVR時可以補上–premultiply-alpha,當然也要記得作相對應的設定:[CCTexture2D PVRImagesHavePremultipliedAlpha:YES]
  • 使用inner-padding可以避免sprite吃到其他相鄰srpite的像素,造成黑色邊緣。



11/21/2011

Survey Perforce

最近在試用Perforce

據悉Google, Sony, NVidia, Pixar, Epic都在使用Perforce作為SCM工具


安裝真的挺方便的,不過我沒做過網管,沒有說服力

身為遊戲開發人員,SCM工具除了能夠version control以外

最好還要有能夠預覽所管理的psd、3ds max、maya等圖形檔案

Perforce很神奇地提供P4Thumb這個工具

P4Thumb可以針對Server上的檔案自動產生thumbnail
包含 * PSD (Photoshop) * MB (Maya) * MAX (3ds Max)

其他Client就可以取得預覽縮圖啦!

1/18/2011

msysgit with CopSSH for windows

 1. install GitExtensions (includes msysgit)
* by default, Git installs to C:\Program Files\Git\

2. install CopSSH
* by default, CopSSH installs to C:\Program Files\ICW\

3. control panel > user accounts > create new account (for example, git)

4. copssh control panel > users > add activated account, i.e. git
* choose "Linux shell and Sftp" for access type to enable shell command execution

5. in C:\Program Files\ICW\home\gi, edit .bashrc and append below script
gitpath=/cygdrive/c/Program\ Files/Git/bin
gitcore=/cygdrive/c/Program\ Files/Git/libexec/git-core
export PATH=$gitpath:$gitcore:$PATH

6. create a remote repository
$ssh git@localhost
$git init test
$cd test; touch abc
$git add .; git commit -m 'test'

7. clone from remote
$git clone git@localhost:test mytest

8. all done :)

If you want to use public keys to access git account, collect public keys and put them into home/git/.ssh/authorized_keys. Check this to get more information.

6/05/2010

笑傲江湖 online

雨狗寫了一篇文章講 完美時空 笑傲江湖online 的記者發表會

從影片可以看出大陸遊戲公司對於市場行銷手法的敏銳度

並且十分有創意地將真人武術與3D立體投影結合起來

雖然記者會的演出十分成功  相信絕對是120%的加分

但是在下得了不google就會死的病  所以就花了幾分鐘找找網路新聞
好奇的是它到底是一家技術導向還是行銷導向的公司?

為甚麼昱泉在大陸會淪落到被併購的下場?

另外一提, 它好像也是一家很精實的遊戲公司呀~ XD

5/22/2010

3DS預測

老任六月會在E3發表NDS後繼機3DS

我也來預測一下3DS的規格好了

CPU: ARM11 266-400MHz

RAM: 32-64MB

3D: iPhone 3GS > 3DS >= PSP

upper screen: autostereoscopic LCD 352x198 3.53" 800x240

lower screen: touch screen LCD 256x192 3.02" 320x240

accelerometer, gyro sensor

slide Pad

depth dual camera

802.11b/g/n

7/29/2009

iPhone 3D engine survey

[open source]
SIO2
indie certifiate $99
Blender as the tool chain
Bullet as the physics engine
Lua script integrated
rich tutorials

Oolong, author's blog
ARM VFP math library

Irrlicht
OGRE

note: SIO2, Oolong mainly focus on iPhone.

[commercial]
Torgue
2D: indie $750, commercial $2,250
3D: indie $650, commercial $1,749

ShiVa
unlimited: €169,00
advanced: €1499,00

unity3D
foundation: indie $199, pro $1499
iPhone: basic $399, pro $1499

12/07/2008

wget & curl

因為工作上需要, 想要自動化上傳/下載檔案到web server上

所以就survey了wget與curl 這裡有兩者的比較 wget vs curl

不過在https上遇到了一些問題 因為以前沒有研究過這方面的東西

總之wget/curl要連線到https/ssl的server上必須要有ca憑證 cacert.pem
wget --ca-certificate=cacert.pem url
curl --cacert cacert.pem url

接下來就是自動login的方法了

web server是用form post method讓使用者登入
wget --post-data="name=hello&pass=world" url
curl -d "name=hello&pass=world" url

由於登入資訊是用cookie紀錄 所以要將cookie儲存下來供爾後使用
[SAVE]
wget --keep-session-cookies --save-cookies file url
curl -c file url
[LOAD]
wget --load-cookies cookiefile url
curl -b cookiefile url

最後則是上傳檔案的關鍵了
wget只提供了plaintext的post method
還好curl對於multipart/form-data有特別支援
prefix@代表所要上傳的檔案
curl -F "name=hello" -F "passl=world" -F "file=@filename" url

附註一提: curl是一個snapshot url的工具
沒有辦法像wget一樣parse網頁內容並下載相關檔案

8/02/2007

Simone

取自電影「Simone」,全名是Simulator One。這個工具的主要目標是利用script來編輯FSM,並且產生FSM示意圖以供參考。除此之外,透過即時編譯執行,我們可以立刻模擬FSM的行為,同時驗證script的正確性。此工具使用的腳本語言為Lua,產生FSM示意圖的函式庫為LuaGraph,建立GUI視窗的函式庫為wxLua。


AI.lua是我所設計的lua extension。其主要用途是將LuaGraph與FSM結合在一起。在FSM中,每一個state都會註冊一個node至graph之中。設定State與State之間的轉換關係的同時也會將node與node之間的edge註冊至graph之中。程式人員或企畫人員在編輯FSM腳本時,可以利用lua物件導向的方式,override掉之前的函式,創造出不一樣的功能。利用AI module,程式人員或企畫人員可以自己編寫AI script。由於lua非常彈性,我們可以針對特定的state改寫其中的member function。甚至我們也可以任意制訂object的屬性,讓企畫人員馬上實現他們的想法。

載入AI script之後,Simone會產生新的child window並且畫出FSM圖。在Simone下方有一個文字輸入欄位,這欄位可即時輸入script指令來改變FSM的狀態。此外,Simone preference可以設定PNG檔案輸出位置與State之顏色等。

script sample
local AI = require 'AI'
function CreateAI()
local o = AI.NewObject('basic')

-- create the attribute of the object for display
o.attr={
hp={name='HP',value=100},
-- add new attribute for you wanted
}

-- create states
local idle = AI.NewState(o,'idle')
local patrol = AI.NewState(o,'patrol')
local normal = AI.NewState(o,'normal')

-- add variables to state
idle.timer = 0
patrol.timer = 0

-- override function
function idle:OnEnter(previous)
idle.timer = 0
end

-- state event
local function onAttack(state)
local hp = state:GetObject().attr.hp
hp.value = hp.value - 10
state:Transit(normal)
end
idle:AddEvent(normal, onAttack, 'is attacked', 'attack')
idle:AddEvent(normal, onAttack, 'is alerted', 'alert')

-- condition
local function gotoPatrol(state, deltaTime)
state.timer = state.timer+deltaTime
if (state.timer>3000) then
return true
end
return false
end
idle:Connect(patrol, gotoPatrol, 'idle>3sec')

-- override function
function patrol:OnEnter(previous)
patrol.timer = 0
end

-- condition
local function gotoIdle (state, deltaTime)
state.timer = state.timer+deltaTime
if (state.timer>5000) then
return true
end
return false
end
patrol:Connect(idle, gotoIdle, 'patrol>5sec')

local function pass() return true end
normal:Connect(idle, pass)

o:SetState(idle)
return o
end

7/13/2007

UI of Games

What About Flash? Can We Really Make Games With It?

Mini-postmortem on the use of gameswf

gameswf

gnash

Scaleform GFx

3/30/2007

Irradiance Environment Map

Radiance is the emitted energy per unit time in a given direction from a unit area of an emitting surface. We can capture radiance at a point for all directions by rendering a cube map, A.K.A. radiance distribution function. The radiance of a purely diffuse surface is defined in terms of surface's irradiance.

The irradiance distribution function at a point can be stored using a diffuse cube map, also called irradiance environment map. At SIGGRAPH 2001, Ramamoorthi and Hanrahan proposed a technique to represent irradiance environment map by Spherical Harmonics. This reduces both the storage and bandwidth costs.



DirectX SDK provides functions for projecting a cube map into spherical harmonic coefficients as well as other SH operation functions. SH is a kind of frequency space transformation likes Fourier transformation, but on spherical space. Ramamoorthi found 3rd order SH effectively represents the irradiance distribution function. 3rd order SH only needs 9 floating point coefficients per channel, i.e. R, G, B.



Reference

3/16/2007

Using DirectX with wxWidget

wxWidgets A.K.A. wxWindow is a cross-platform toolkit for creating window application. There is a class called wxGLCanvas for OpenGL, but no information for DirectX. Then, I try to integrate DirectX with wxWidgets. I found flick will happen when I use paint event to draw d3d frame. To avoid from flicking, I use erase event to clear the background of window and redraw d3d frame. Here is the pseudo code:

class MyApp : public wxApp {
virtual bool OnInit();
virtual int OnExit();
};
class MyViewport : public wxPanel {
public:
MyViewport(wxWindow* parent);
void OnIdle(wxIdleEvent& event);
void OnDraw(wxEraseEvent& event);
private:
DECLARE_EVENT_TABLE()
};

class MyFrame: public wxFrame {
MyViewport *mpViewport;
public:
MyFrame(const wxString& title);
private:
DECLARE_EVENT_TABLE()
};

// the event tables connect the wxWidgets events with the functions (event
// handlers) which process them. It can be also done at run-time, but for the
// simple menu events like this the static method is much simpler.
BEGIN_EVENT_TABLE(MyViewport, wxPanel)
EVT_ERASE_BACKGROUND (MyViewport::OnDraw)
EVT_IDLE (MyViewport::OnIdle)
END_EVENT_TABLE()

// Create a new application object: this macro will allow wxWidgets to create
// the application object during program execution (it's better than using a
// static object for many reasons) and also implements the accessor function
// wxGetApp() which will return the reference of the right type (i.e. MyApp and not wxApp)
IMPLEMENT_APP(MyApp)

// 'Main program' equivalent: the program execution "starts" here
bool MyApp::OnInit() {
// create the main application window
MyFrame *frame = new MyFrame( _T("D3D sample") );

create D3D device here

frame->Show(true);
SetTopWindow(frame);
return true;
}

int MyApp::OnExit() {
release D3D device here

return wxApp::OnExit();
}

// frame constructor
MyFrame::MyFrame(const wxString& title) :
wxFrame(NULL, -1, title, wxPoint(0,0)), mpViewport(NULL), mpTreeCtrl(NULL)
{
mpViewport = new MyViewport(this);
}

MyViewport::MyViewport(wxWindow* parent) : wxPanel(parent) {
}

void MyViewport::OnIdle(wxIdleEvent& event) {
update your d3d objects here

// call Refresh() to send a wxEraseEvent to redraw a frame
Refresh();
}

void MyViewport::OnDraw(wxEraseEvent& event) {
g_pd3dDevice->BeginScene();

draw your d3d objects here

g_pd3dDevice->EndScene();
g_pd3dDevice->Present(NULL,NULL, this->GetHWND() ,NULL);
}

3/09/2007

How to write a directx shader for 3dsmax


Since programmable GPU was appeared, many and many game engines have to support this feature to create more and more visual effects. And also, 3D modeling softwares need to adapt this new feature.

3dsmax provides a material called "DirectX shader". You can write your directX shader (a.k.a HLSL) and use it as a material in 3dsmax. but there are some rules and restrictions(see DirectX 9 Shader Material-Semantics and Annotations). There are some sample FX files in map\fx\ directory. Let us open the default.fx file.

// This is used by 3dsmax to load the correct parser
string ParamID = "0x0";

//DxMaterial specific
// light direction (view space)
float3 lightDir : Direction < uiname = "Light Direction" object = "TargetLight"> = {-0.577, -0.577, 0.577};

// light intensity
float4 I_a = { 0.1f, 0.1f, 0.1f, 1.0f }; // ambient
float4 I_d = { 1.0f, 1.0f, 1.0f, 1.0f }; // diffuse
float4 I_s = { 1.0f, 1.0f, 1.0f, 1.0f }; // specular

// material reflectivity
float4 k_a < uiname = "Ambient"> = float4( 0.47f, 0.47f, 0.47f, 1.0f ); // ambient
float4 k_d < uiname = "Diffuse"> = float4( 0.47f, 0.47f, 0.47f, 1.0f ); // diffuse
float4 k_s < uiname = "Specular"> = float4( 1.0f, 1.0f, 1.0f, 1.0f ); // diffuse // specular
int n< uiname = "Specular Power" uitype = "IntSpinner" uimin =" 0.0f;" uimax =" 50.0f;"> = 15;

// transformations
float4x4 World : WORLD;
float4x4 View : VIEW;
float4x4 Projection : PROJECTION;
float4x4 WorldViewProj : WORLDVIEWPROJ;
float4x4 WorldView : WORLDVIEW;

struct VS_OUTPUT
{
float4 Pos : POSITION;
float4 col : COLOR0;
};

VS_OUTPUT VS(
float3 Pos : POSITION,
float3 col : COLOR,
float3 Norm : NORMAL,
float2 Tex : TEXCOORD0)
{
VS_OUTPUT Out = (VS_OUTPUT)0;
float3 L = lightDir;
float3 P = mul(float4(Pos, 1),(float4x4)World); // position (view space)
float3 N = normalize(mul(Norm,(float3x3)World)); // normal (view space)
float3 R = normalize(2 * dot(N, L) * N - L); // reflection vector (view space)
float3 V = normalize(P); // view direction (view space)
Out.Pos = mul(float4(Pos,1),WorldViewProj); // position (projected)
float4 Diff = I_a * k_a + I_d * k_d * max(0, dot(N, L)); // diffuse + ambient
float4 Spec = I_s * k_s * pow(max(0, dot(R, V)), n/4); // specular
Out.col = Diff + Spec;
return Out;
}

float4 PS(
float4 Diff : COLOR0,
float4 Spec : COLOR1,
float2 Tex : TEXCOORD0,
float2 Tex1 : TEXCOORD1 ) : COLOR
{
float4 color = Diff + Spec;
return color ;
}

technique DefaultTechnique
{
pass P0
{
// shaders
CullMode = None;
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_1_1 PS();
}
}


DirectX shader material parses the shader file. These variables with 3dsmax specified annotation will create UI components as you can see in "Default.dx Parameters" rollout.
And 3dsmax will pass values to these variables with specified semantic and annotation.
3dsmax use "Direction" as semantic to specify lighting direction. when there is no light in the scene, this parameter use default light's value i.e. current view direction.

The most trick part is we have to assign all mapping channels to vertex shader inputs if we want to use other coordinates such as vertex color, vertex alpha, second texture coordinate...etc.
Else these input value will be disordered and can not get the correct result.