D語言學習筆記

 
D語言學習筆記我在sina的博客上已經寫了本文,不過這裏關注的人更多,因此轉貼)
1,配置環境(2008-7-15)
需要如下的軟件(只是基於我的學習路線,其他的還沒有空閒時間研究):
D語言編譯器下載
http://ftp.digitalmars.com/dmd.2.014.zip
D語言連接器及工具下載
http://ftp.digitalmars.com/dmc.zip
D語言IDE下載
http://svn.dsource.org/projects/dwin/downloads/scite4d/scite4dsetup.exe
D語言編譯工具下載
http://svn.dsource.org/projects/dsss/downloads/0.75/dsss-0.75-dmd-win.zip
D語言基本庫下載
http://downloads.dsource.org/projects/tango/0.99.6/tango-0.99.6-bin-win32-dmd.1.029.zip
D語言Windows下GUI庫下載
http://www.dprogramming.com/dfl09701.zip
有了以上的軟件,我們開始配置D語言開發環境。
首先,需要配置編譯器環境,假定我們的工作環境在D:/D-Language文件夾下,將dmd,dmc解壓後放在該
目錄,其中dmd會產生兩個子目錄dm和dmd,而dmc只產生一個dm目錄,將它們放在D:/D-Language目錄下

接着設置環境變量,將D:/D-Language/dm/bin和D:/D-Language/dmd/bin加入Path路徑中。
這樣編譯器環境已經設置好了,打開一個命令行窗口,輸入dmd,如果出現一堆參數提示,表明成功。
再次,設置編譯工具。將dsss解壓,進入其bin所在的上層目錄(也就是能看到bin的目錄),將所有的
包含bin目錄的子目錄覆蓋到D:/D-Language/dmd目錄下(會提示是否覆蓋bin目錄,選擇是,我們僅僅是
想將dsss的路徑和dmd的路徑放在一起而已)。
在命令行窗口,輸入dsss,如果出現一堆參數提示,表明成功。
然後,設置IDE環境。雙擊scite4dsetup.exe安裝sci軟件,安裝完後打開sci安裝路徑下的
SciTEGlobal.properties文件(一般來說在C:/Program Files/SciTE4D目錄下),修改第一行,將它改
成具體的路徑(D:/D-Language/dmd)。
tango安裝:
一,同dsss安裝,解壓後將bin目錄以及其兄弟目錄全部拷貝到D:/D-Language/dmd目錄下。
二,打開D:/D-Language/dmd/bin/sc.ini文件用下面的覆蓋:
[Version]
version=7.51 Build 020
[Environment]
LIB="[email=%@P%/../lib;%@P%/../../dm/lib]%@P%/../lib;%@P%/../../dm/lib[/email]"
DFLAGS="[email=-I%@P%/../import]-I%@P%/../import[/email]" -version=Tango -defaultlib=tango-base-dmd.lib -debuglib=tango-
base-dmd.lib -L+tango-user-dmd.lib
[email=LINKCMD=%@P%/link.exe]LINKCMD=%@P%/link.exe[/email]
安裝完畢。
DFL安裝(使用dsss安裝該庫):
一,解壓後將bin目錄以及其兄弟目錄全部拷貝到D:/D-Language/dmd目錄下。
二,打開sci,併到D:/D-Language/dmd/import/dfl下打開all.d文件。
三,在D:/D-Language/dmd/import/目錄下創建dsss.conf文件,內容如下:
[dfl]
target=dfl
type=library
postbuild=copy /y dfl.lib ../../lib/dfl.lib
四,在sci中F7 Build。
五,安裝成功。
2,使用學習:
tango網絡編程實例:
打開sci,
將以下代碼拷貝到文檔中,並保存成server.d:
private import  tango.core.Thread;
private import  tango.io.Console;
private import  tango.net.ServerSocket;
void main()
{
        const int port = 8080;

        // thread body for socket-listener
        void run()
        {      
            auto server = new ServerSocket (new InternetAddress(port));
               
            while(true)
            {
                // wait for requests
                auto request = server.accept;
                // write a response
                request.output.write ("server replies 'hello'");
                Cout("Socket accept!/n");
            }
        }
        // start server in a separate thread, and wait for it to start
        (new Thread (&run)).start;

}
在保存的server.d同目錄下創建dsss.conf文件,文件內容如下:
[server.d]
在sci中F7 Build,F5 run。
可以看到如今一個網絡server端已經寫好。
我們現在需要寫一個client端連接服務端,並打印出服務端反饋的信息。
client.d如下:

private import  tango.io.Console;
private import  tango.net.SocketConduit,
                tango.net.InternetAddress;
void main()
{
        // make a connection request to the server
        auto request = new SocketConduit;
        request.connect (new InternetAddress ("localhost", 8080));
        request.output.write ("hello/n");
        // wait for response (there is an optional timeout supported)
        char[64] response;
        auto size = request.input.read (response);
        // close socket
        request.close;
        // display server response
        Cout (response[0..size]).newline;
}
如果不想再繁瑣的使用dsss.conf進行編譯,直接在sci中Ctrl+F5直接就可以運行,看結果可以看到,本程序已經連接服務器,並打印了服務器的反饋字符串。
------------------------------
DFL界面編程:
簡單的界面直接如下代碼(form.d):
import dfl.all;
int main()
{
    Form myForm;
    Label myLabel;
    myForm = new Form;
    myForm.text = "hello, D Language China";
    myForm.width = 600;
    myForm.height = 300;
    myLabel = new Label;
    myLabel.font = new Font("Verdana", 14f);
    char[] str =
    " http://www.d-programming-language-china.org/ /n"
    " http://bbs.d-programming-language-china.org/ /n"
    " http://scite4d.d-programming-language-china.org/ ";
    myLabel.text = str;
    myLabel.location = Point(15, 15);
    myLabel.autoSize = true;
    myLabel.parent = myForm;
    Application.run(myForm);
    return 0;
}
如果需要命令行編譯的話:dmd form.d dfl.lib即可。這裏需要連接dfl.lib。

如果直接使用dfl.exe編譯的話可以如此(記住如果找不到dfl.exe,到dfl的bin目錄下將dfl.exe拷貝到dmd/bin目錄下):dfl -release form.d -gui即可(-gui選項去除exe執行背後的命令行窗口)。

如果有resource文件(比如.ico,.rc,.res),則使用 dfl -release form.d form.res -gui就可以將這些資源文件link到程序中。

--end of 2008-7-15

--start of 2008-7-17
今天,學習一下DFL的GDI畫圖功能,我們使用DFL開啓一個MDI工程,在其子窗口中,增加一個按鈕,當點擊該按鈕時,將會在該窗口下畫出一個圖形,多次點擊依序畫出圖形,可以根據這個demo程序開發一個畫圖程序(比如UML類圖...)。


private import dfl.all;
class DrawForm: Form
{
    private int count;
this()
{
  text = "draw ellipse!";
        count = 0;
}

    public void add()
    {
        count++;
        invalidate(false);
    }

protected override void onPaint(PaintEventArgs ea)
{
  super.onPaint(ea);
  
  auto Pen p = new Pen(Color(0xFF, 0, 0), 4);
      
        for(int i = 0;i < count;i++)
        {
            ea.graphics.drawEllipse(p, (i % 6) * 50, (i / 6) * 50, 50, 50);
        }
}
}

class MainForm: Form
{
uint docnum = 0;

this()
{
  text = "Draw Studio";
  startPosition = FormStartPosition.WINDOWS_DEFAULT_BOUNDS;
  
  MenuItem mi;
  menu = new MainMenu;
  
  with(mi = new MenuItem)
  {
   text = "New";
   
   click ~= &menubar_click;
  }
      
  menu.menuItems.add(mi);
  
  isMdiContainer = true;
  
  addChild();
}


private void mdiChildHiClick(Object sender, EventArgs ea)
{
  Button btn;
  btn = cast(Button)sender;
  assert(btn);
  
  DrawForm f;
  f = cast(DrawForm)btn.parent;
  assert(f);
  
        f.add();
}


final void addChild()
{
  Form f;
  with(f = new DrawForm)
  {
   text = "Studio:";
   mdiParent = this;
   
   with(new Button)
   {
    text = "&Add";
    location = Point(42, 42);
    parent = f;
    click ~= &mdiChildHiClick;
   }
   show();
  }
}

private void menubar_click(Object sender, EventArgs ea)
{
  addChild();
}
}

int main()
{
int result = 0;

try
{
  Application.run(new MainForm);
}
catch(Object o)
{
  msgBox(o.toString(), "Fatal Error", MsgBoxButtons.OK, MsgBoxIcon.ERROR);
  
  result = 1;
}

return result;
}


--end of 2008-7-17

--start of 2008-7-18
D語言遊戲開發包Derelict安裝使用:

首先到http://www.dsource.org/projects/derelict/changeset/head/trunk?old_path=%2F&format=zip
下載Derelict開發包。

再次下載SDL的動態庫http://www.libsdl.org/release/SDL-1.2.13-win32.zip文件。

SDL動態庫安裝:

解壓後將sdl.dll放在windows/system32目錄下(其它系統目錄也可)。

Derelict安裝:

解壓後,使用命令行進入Derelict目錄(該目錄下有dsss.conf文件),輸入dsss build,也可以進入子目
錄build 相應的模塊(比如DerelictSDL)。
產生的lib文件都在Derelict目錄下的lib目錄中,將它們都拷貝到dmd/lib目錄下,並且將每個
DerelictXXX(比如DerelictSDL)目錄下的derelict目錄整個拷貝到dmd/import目錄下(記住該目錄在
以前的編譯器配置中,已經在dmd/bin/sc.ini中指定爲第三方lib的指定目錄)。這些DerelictXXX下的
子模塊是可以互相獨立的(除了DerelictUtil中的Util模塊,這是所有的其它模塊都依賴的模塊,必須
拷貝過去)。
既然這些模塊依賴於DerelictUtil,那麼如果我們現在進行SDL遊戲編程,除了依賴DerelictSDL模塊外
,還需依賴DerelictUtil模塊。所以以下的demo程序編譯時需要使用如下的命令行指令:
  dmd sdl_ex1.d  DerelictUtil.lib DerelictSDL.lib

以下是demo程序,需要SDL庫支持。

文件名:

sdl_ex1.d

文件內容:

module sdl_ex1;
import derelict.sdl.sdl;
version(Tango)
{
import tango.stdc.stdio;
}
SDL_Surface* screen;  // pointer to the screen surface object

void render()
{
if(SDL_MUSTLOCK(screen))
  if(SDL_LockSurface(screen) < 0)
   return;
   
int tick = SDL_GetTicks();

// track the current yoffset when looping throught the pixels
int yoff = 0;

uint* buff = cast(uint*)screen.pixels;

for(int i=0; i<600; ++i)
{
  for(int j=0, off = yoff; j<800; ++j, ++off)
  {
   buff[off] = i * i*4 + j * j*4 ;//you can add + tick for flash
window
  }
  
  // add the width of the screen (the surface pitch / bytes per pixel)
  // to the yoffset in order to move to the next row
  yoff += screen.pitch/4;
}

// unlock the screen if we had to lock it
if(SDL_MUSTLOCK(screen))
  SDL_UnlockSurface(screen);
  
// tell SDL to update the whole screen
SDL_UpdateRect(screen, 0, 0, 800, 600);
}

int main()
{
// load the SDL shared library
DerelictSDL.load();

// Initialize SDL
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
  printf("Unable to init SDL: %s/n", SDL_GetError());
  SDL_Quit();
  return 1;
}

// Create the screen surface (window)
screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
if(screen is null)
{
  printf("Unable to set 800x600 video: %s/n", SDL_GetError());
  SDL_Quit();
  return 1;
}

// main loop flag
bool running = true;

// main loop
while(running)
{
  // draw to the screen
  render();
  
  // process events
  SDL_Event event;
  while(SDL_PollEvent(&event))
  {
   switch(event.type)
   {
    // exit if SDLK or the window close button are pressed
    case SDL_KEYUP:
     if(event.key.keysym.sym == SDLK_ESCAPE)
      running = false;
     break;
    case SDL_QUIT:
     running = false;
     break;
    default:
     break;
   }
  }
  
  // yield the rest of the timeslice
  SDL_Delay(0);
}
// clean up SDL
SDL_Quit();
return 0;
}

運行後的快照如下:


DWin安裝使用:
下載安裝包:http://svn.dsource.org/projects/dwin/downloads/dwin/dwin.exe
雙擊執行。
將安裝目錄下的lib目錄下的所有lib拷貝到dmd/lib目錄下。
將該目錄下的其它子文件夾拷貝到dmd/import/dwin目錄下(dwin目錄不存在請創建,而上層import父目
錄則在以前的安裝過程中已經指明是第三方的D語言庫源文件存放目錄)。
安裝完畢。
記住使用dwin.lib時,根據我的經驗,pcre.lib和dfl.lib需要包含,否則不能運行成功。
以下的代碼示例顯示如何打開一個IE窗口,並自動導航到google頁面,並在其中輸入"D language",自
動點擊搜索按鈕,並顯示查詢結果。希望以此作爲指導,顯示一個自動化IE測試如何進行。
程序如果一下子沒有顯示相應的自動化,請耐心等待,因爲程序中使用了Sleep函數。
文件名:test.d
執行命令行:dmd test.d(注意沒有添加一起link的lib文件是因爲程序中已經pragma需要的lib文件了

程序:
module test;
import tango.io.Stdout;
import dwin.sys.win32.ie.IE;
pragma(lib, "tango-base-dmd.lib");
pragma(lib, "tango-user-dmd.lib");
pragma(lib, "dwin.lib");
pragma(lib, "dfl.lib");
pragma(lib, "pcre.lib");
void main(char[][] args)
{
    auto ie = ieCreate(null, "Width",800, "Height",600, "ToolBar",false, "MenuBar",false,
"Navigate", "http://www.google.cn");
   
    Sleep(8000);
    //waitId(ie, "postform"); //wait postform node load
   
    auto text = getElementsByName(ie, "q");
    putInnerText(text[0],"D Language");
   
    Sleep(1000);
   
    auto result = getElementsByName(ie, "btnG");   
   
    result[0].click();
   
    Stdout( getInnerText(result[0]) ).newline;
   
    Sleep(8000);
    ie.Quit();
}
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章