D
Dとは
C言語の次、らしい。Dリファレンス日本語版
インストール
こちらからdmdとdmcを落としてくる。 で、たとえば両者を
c:\dmd
に展開して
c:\dmd\dm c:\dmd\dmd
というフォルダ構成にしたら、環境変数設定。
LIB c:\dmd\dmd\lib,c:\dmd\dm\lib PATHにc:\dmd\dmd\binを追加。
SDLなどのライブラリはDerelictを使うのが簡単。チュートリアルに従えばOk。build.exeも入れとこう。お告げに従って2.0.9を入れる。
基礎
printfも通るけどwriteflnのほうが便利。lnは改行付き。
import std.stdio; void main(char[][] args) { writefln("Hello,World"); }
char[]はふつうの文字列。連想配列は配列の宣言で[char[]]すれば良い。
import std.stdio; void main(char[][] args) { char[][char[]] testhash; testhash["hoge"]="ho_gee"; testhash["hage"]="ha_gee"; writefln(testhash["hoge"]); }
ファイル開いて各行処理。
import std.stdio; import std.stream; void main(char[][] args) { char[] hoge; Stream file = new BufferedFile("hoge.txt"); foreach(ulong n,char[] line;file){ writefln("%d: %s",n,line); } file.close(); }
正規表現で置換。"g"を付けるとgsub。
sub(line,"h.*e","Hoge","g")
SDL
import derelict.sdl.sdl;
して
DerelictSDL.load();
すればSDLのAPIを叩けるようになる。ビルドは
build filename -full -clean -Ic:\dmd\dmd\src\ext gdi32.lib
など。
SDL_Image
指定したファイルを表示するだけの例。
import std.stdio; import derelict.sdl.sdl; import derelict.sdl.image; void init() { DerelictSDL.load(); DerelictSDLImage.load(); SDL_Init(SDL_INIT_VIDEO ); SDL_WM_SetCaption("test", null); } void main(char[][] args) { int width; if (args.length < 2) { writefln("input filename"); return; } init(); SDL_Surface* image =IMG_Load(args[1]); if (image == null) { writefln("couldn't load " ~ args[1]); return; } if(image.w<=200){ width=200; } else{ width=image.w; } SDL_Surface* scr = SDL_SetVideoMode(width, image.h, 16, SDL_SWSURFACE); SDL_BlitSurface(image, cast(SDL_Rect*)0, scr, cast(SDL_Rect*)0); SDL_UpdateRect(scr, 0,0,0,0); mainLoop: while (true) { SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: break mainLoop; default: break; } } } }