Changeset 50
- Timestamp:
- 02/12/08 14:22:08 (4 years ago)
- Files:
-
- trunk/GNUmakefile (modified) (1 diff)
- trunk/derelict/sdl/audio.d (modified) (2 diffs)
- trunk/derelict/sdl/video.d (modified) (2 diffs)
- trunk/derelict/util/exception.d (modified) (9 diffs)
- trunk/derelict/util/loader.d (modified) (10 diffs)
- trunk/derelict/util/wrapper.d (modified) (4 diffs)
- trunk/src/GNUmakefile (modified) (1 diff)
- trunk/src/GNUmakefile.dmd (modified) (1 diff)
- trunk/src/color.d (modified) (11 diffs)
- trunk/src/config.d (modified) (6 diffs)
- trunk/src/display.d (modified) (10 diffs)
- trunk/src/dl_gfx.d (modified) (2 diffs)
- trunk/src/events.d (modified) (5 diffs)
- trunk/src/list.d (modified) (1 diff)
- trunk/src/main.d (modified) (2 diffs)
- trunk/src/math.d (modified) (2 diffs)
- trunk/src/menu.d (modified) (9 diffs)
- trunk/src/messages.d (modified) (2 diffs)
- trunk/src/net.d (modified) (9 diffs)
- trunk/src/objects.d (modified) (14 diffs)
- trunk/src/player.d (modified) (6 diffs)
- trunk/src/ui.d (modified) (19 diffs)
- trunk/src/visual.d (modified) (1 diff)
- trunk/src/world.d (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/GNUmakefile
r11 r50 10 10 11 11 backup: 12 @base=qonkd-src.$(shell date +%Y%m%d); n=1; \12 dir=$(shell basename `pwd`); base=qonkd-src.$(shell date +%Y%m%d); n=1; \ 13 13 while true; do true; tgzname=$$base-$$n.tgz; test -s $$tgzname || break; let n=n+1; done; \ 14 tar -C .. -czf $$tgzname `basename $(PWD)`\14 tar -C .. -czf $$tgzname $$dir \ 15 15 --exclude .svn \ 16 16 --exclude $(BUILD_DIR) \ 17 17 --exclude 'qonkd-src.*' \ 18 --exclude $(TARGET) \18 --exclude ./$(TARGET) \ 19 19 --exclude core --exclude 'core.*' \ 20 20 --exclude '*.ttf'; \ trunk/derelict/sdl/audio.d
r44 r50 32 32 module derelict.sdl.audio; 33 33 34 private import std.string; 34 35 private import derelict.sdl.types; 35 36 private import derelict.sdl.rwops; … … 110 111 SDL_AudioSpec* SDL_LoadWAV(char *file, SDL_AudioSpec *spec, Uint8 **buf, Uint32 *len) 111 112 { 112 return SDL_LoadWAV_RW(SDL_RWFromFile(file, "rb"), 1, spec, buf, len);113 return SDL_LoadWAV_RW(SDL_RWFromFile(file, std.string.toStringz("rb")), 1, spec, buf, len); 113 114 } 114 115 trunk/derelict/sdl/video.d
r44 r50 32 32 module derelict.sdl.video; 33 33 34 private import std.string; 34 35 private import derelict.sdl.types; 35 36 private import derelict.sdl.mutex; … … 203 204 SDL_Surface* SDL_LoadBMP(char *file) 204 205 { 205 return SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1);206 return SDL_LoadBMP_RW(SDL_RWFromFile(file, std.string.toStringz("rb")), 1); 206 207 } 207 208 208 209 int SDL_SaveBMP(SDL_Surface *surfave, char *file) 209 210 { 210 return SDL_SaveBMP_RW(surfave, SDL_RWFromFile(file, "wb"), 1);211 return SDL_SaveBMP_RW(surfave, SDL_RWFromFile(file,std.string.toStringz("wb")), 1); 211 212 } 212 213 trunk/derelict/util/exception.d
r5 r50 38 38 { 39 39 public: 40 this( char[]msg)40 this(string msg) 41 41 { 42 42 super(msg); … … 52 52 public: 53 53 54 this( char[]sharedLibName)54 this(string sharedLibName) 55 55 { 56 56 super("Failed to load shared library " ~ sharedLibName); 57 _sharedLibName = sharedLibName .dup;57 _sharedLibName = sharedLibName; 58 58 } 59 59 60 char[]sharedLibName()60 string sharedLibName() 61 61 { 62 62 return _sharedLibName; … … 64 64 65 65 private: 66 char[]_sharedLibName;66 string _sharedLibName; 67 67 } 68 68 … … 75 75 { 76 76 public: 77 this( char[]msg)77 this(string msg) 78 78 { 79 79 super(msg); … … 81 81 } 82 82 83 this( char[] sharedLibName, char[]procName)83 this(string sharedLibName, string procName) 84 84 { 85 85 super("Failed to load proc " ~ procName ~ " from shared library " ~ sharedLibName); 86 _procName = procName .dup;86 _procName = procName; 87 87 } 88 88 89 char[]procName()89 string procName() 90 90 { 91 91 return _procName; … … 93 93 94 94 private: 95 char[]_procName;95 string _procName; 96 96 } 97 97 … … 104 104 public: 105 105 106 this( char[]sharedLibName)106 this(string sharedLibName) 107 107 { 108 108 super("Attempted to use handle to unloaded shared library " ~ sharedLibName); 109 _sharedLibName = sharedLibName .dup;109 _sharedLibName = sharedLibName; 110 110 } 111 111 112 char[]sharedLibName()112 string sharedLibName() 113 113 { 114 114 return _sharedLibName; … … 116 116 117 117 private: 118 char[]_sharedLibName;118 string _sharedLibName; 119 119 } 120 120 … … 128 128 * to be thrown. 129 129 */ 130 typedef bool function( char[] libName, char[]procName) MissingProcCallback;130 typedef bool function(string libName, string procName) MissingProcCallback; 131 131 132 132 private MissingProcCallback missingProcCallback; 133 133 134 public void Derelict_HandleMissingProc( char[] libName, char[]procName)134 public void Derelict_HandleMissingProc(string libName, string procName) 135 135 { 136 136 bool result = false; trunk/derelict/util/loader.d
r44 r50 1 1 /* 2 * Copyright (c) 200 5-2006Derelict Developers2 * Copyright (c) 2004-2007 Derelict Developers 3 3 * All rights reserved. 4 4 * … … 51 51 } 52 52 53 version (DerelictUseStdLoader) { 53 54 private alias void* SharedLibHandle; 55 56 //============================================================================== 57 class SharedLib 58 { 59 public: 60 string name() 61 { 62 return _name; 63 } 64 65 private: 66 SharedLibHandle _handle; 67 string _name; 68 69 this(SharedLibHandle handle, string name) 70 { 71 _handle = handle; 72 _name = name; 73 } 74 } 75 //============================================================================== 76 SharedLib Derelict_LoadSharedLib(string libName) 77 in 78 { 79 assert(libName !is null); 80 } 81 body 82 { 83 return Platform_LoadSharedLib(libName); 84 } 85 86 //============================================================================== 87 SharedLib Derelict_LoadSharedLib(string[] libNames) 88 in 89 { 90 assert(libNames !is null); 91 } 92 body 93 { 94 SharedLibLoadException exception = null; 95 SharedLib lib = null; 96 97 foreach(string libName; libNames) 98 { 99 try 100 { 101 lib = Derelict_LoadSharedLib(libName); 102 if(lib !is null) break; 103 } 104 catch(SharedLibLoadException slle) 105 { 106 exception = slle; 107 } 108 } 109 if(lib is null) 110 throw exception; 111 112 return lib; 113 } 114 115 //============================================================================== 116 void Derelict_UnloadSharedLib(SharedLib lib) 117 { 118 if(lib !is null && lib._handle !is null) 119 Platform_UnloadSharedLib(lib); 120 } 121 //============================================================================== 122 void* Derelict_GetProc(SharedLib lib, string procName) 123 in 124 { 125 assert(lib !is null); 126 assert(procName !is null); 127 } 128 body 129 { 130 if(lib._handle is null) 131 throw new InvalidSharedLibHandleException(lib._name); 132 return Platform_GetProc(lib, procName); 133 } 134 //============================================================================== 135 version(Windows) 136 { 137 private import derelict.util.wintypes; 138 139 SharedLib Platform_LoadSharedLib(string libName) 140 { 141 HMODULE hlib = LoadLibraryA(toCString(libName)); 142 if(null is hlib) 143 throw new SharedLibLoadException(libName); 144 145 return new SharedLib(hlib, libName); 146 } 147 148 void Platform_UnloadSharedLib(SharedLib lib) 149 { 150 FreeLibrary(cast(HMODULE)lib._handle); 151 lib._handle = null; 152 } 153 154 void* Platform_GetProc(SharedLib lib, string procName) 155 { 156 void* proc = GetProcAddress(cast(HMODULE)lib._handle, toCString(procName)); 157 if(null is proc) 158 Derelict_HandleMissingProc(lib._name, procName); 159 160 return proc; 161 } 162 163 } 164 else version(Nix) 165 { 54 166 version(Tango) 55 167 { 56 static assert(0, "Cannot use std.loader with Tango"); 57 } 58 59 import std.loader; 60 61 //============================================================================== 62 class SharedLib 63 { 64 public: 65 char[] name() 168 private import tango.sys.Common; 169 } 170 else version(linux) 171 { 172 private import std.c.linux.linux; 173 } 174 else 175 { 176 extern(C) 66 177 { 67 return _name; 68 } 69 70 private: 71 char[] _name; 72 73 void* delegate(in char[] symbolName) getSymbolDg; 74 Object theLib; 75 76 private void unload() { 77 if (theLib) { 78 delete theLib; 79 theLib = null; 80 getSymbolDg = null; 81 } 82 } 83 84 85 void* getSymbol(char[] symName) { 86 if (theLib !is null) return getSymbolDg(symName); 87 else return null; 88 } 89 90 91 this(Object theLib, void* delegate(in char[]) getSymbolDg, char[] name) 92 in { 93 assert (theLib !is null); 94 assert (getSymbolDg !is null); 95 } 96 body { 97 this.theLib = theLib; 98 this.getSymbolDg = getSymbolDg; 99 this._name = name; 100 } 101 } 102 //============================================================================== 103 SharedLib Derelict_LoadSharedLib(char[] libName) 104 in 105 { 106 assert(libName !is null); 107 } 108 body 109 { 110 try { 111 auto ExeModule exeMod = new ExeModule(libName); 112 auto res = new SharedLib(exeMod, &exeMod.getSymbol, libName); 113 exeMod = null; // fool the auto 114 return res; 115 } 116 catch (ExeModuleException exc) { 117 throw new SharedLibLoadException(libName); 118 } 119 } 120 121 //============================================================================== 122 SharedLib Derelict_LoadSharedLib(char[][] libNames) 123 in 124 { 125 assert(libNames !is null); 126 } 127 body 128 { 129 SharedLibLoadException exception = null; 130 SharedLib lib = null; 131 132 foreach(char[] libName; libNames) 133 { 134 try 135 { 136 lib = Derelict_LoadSharedLib(libName); 137 } 138 catch(SharedLibLoadException slle) 139 { 140 exception = slle; 141 } 142 } 143 if(lib is null) 144 throw exception; 145 146 return lib; 147 } 148 149 //============================================================================== 150 void Derelict_UnloadSharedLib(SharedLib lib) 151 { 152 if(lib !is null) lib.unload(); 153 } 154 //============================================================================== 155 void* Derelict_GetProc(SharedLib lib, char[] procName) 156 in 157 { 158 assert(lib !is null); 159 assert(procName !is null); 160 } 161 body 162 { 163 void* proc = lib.getSymbol(procName); 164 if (proc is null) { 178 /* From <dlfcn.h> 179 * See http://www.opengroup.org/onlinepubs/007908799/xsh/dlsym.html 180 */ 181 182 const int RTLD_NOW = 2; 183 184 void *dlopen(char* file, int mode); 185 int dlclose(void* handle); 186 void *dlsym(void* handle, char* name); 187 char* dlerror(); 188 } 189 } 190 191 SharedLib Platform_LoadSharedLib(string libName) 192 { 193 void* hlib = dlopen(toCString(libName), RTLD_NOW); 194 if(null is hlib) 195 throw new SharedLibLoadException("Failed to load shared library " ~ libName); 196 197 return new SharedLib(hlib, libName); 198 } 199 200 void Platform_UnloadSharedLib(SharedLib lib) 201 { 202 dlclose(lib._handle); 203 lib._handle = null; 204 } 205 206 void* Platform_GetProc(SharedLib lib, string procName) 207 { 208 void* proc = dlsym(lib._handle, toCString(procName)); 209 if(null is proc) 165 210 Derelict_HandleMissingProc(lib._name, procName); 166 } 211 167 212 return proc; 168 213 } 169 //============================================================================== 170 } 171 //============================================================================== 172 else { 173 private alias void* SharedLibHandle; 174 175 //============================================================================== 176 class SharedLib 177 { 178 public: 179 char[] name() 180 { 181 return _name; 182 } 183 184 private: 185 SharedLibHandle _handle; 186 char[] _name; 187 188 this(SharedLibHandle handle, char[] name) 189 { 190 _handle = handle; 191 _name = name; 192 } 193 } 194 //============================================================================== 195 SharedLib Derelict_LoadSharedLib(char[] libName) 196 in 197 { 198 assert(libName !is null); 199 } 200 body 201 { 202 return Platform_LoadSharedLib(libName); 203 } 204 205 //============================================================================== 206 SharedLib Derelict_LoadSharedLib(char[][] libNames) 207 in 208 { 209 assert(libNames !is null); 210 } 211 body 212 { 213 SharedLibLoadException exception = null; 214 SharedLib lib = null; 215 216 foreach(char[] libName; libNames) 217 { 218 try 219 { 220 lib = Derelict_LoadSharedLib(libName); 221 } 222 catch(SharedLibLoadException slle) 223 { 224 exception = slle; 225 } 226 } 227 if(lib is null) 228 throw exception; 229 230 return lib; 231 } 232 233 //============================================================================== 234 void Derelict_UnloadSharedLib(SharedLib lib) 235 { 236 if(lib !is null && lib._handle !is null) 237 Platform_UnloadSharedLib(lib); 238 } 239 //============================================================================== 240 void* Derelict_GetProc(SharedLib lib, char[] procName) 241 in 242 { 243 assert(lib !is null); 244 assert(procName !is null); 245 } 246 body 247 { 248 if(lib._handle is null) 249 throw new InvalidSharedLibHandleException(lib._name); 250 return Platform_GetProc(lib, procName); 251 } 252 //============================================================================== 253 version(Windows) 254 { 255 private import derelict.util.wintypes; 256 257 SharedLib Platform_LoadSharedLib(char[] libName) 258 { 259 HMODULE hlib = LoadLibraryA(toCString(libName)); 260 if(null is hlib) 261 throw new SharedLibLoadException(libName); 262 263 return new SharedLib(hlib, libName); 264 } 265 266 void Platform_UnloadSharedLib(SharedLib lib) 267 { 268 FreeLibrary(cast(HMODULE)lib._handle); 269 lib._handle = null; 270 } 271 272 void* Platform_GetProc(SharedLib lib, char[] procName) 273 { 274 void* proc = GetProcAddress(cast(HMODULE)lib._handle, toCString(procName)); 275 if(null is proc) 276 Derelict_HandleMissingProc(lib._name, procName); 277 278 return proc; 279 } 280 281 } 282 else version(Nix) 283 { 284 version(Tango) 285 { 286 private import tango.sys.Common; 287 } 288 else version(linux) 289 { 290 private import std.c.linux.linux; 291 } 292 else 293 { 294 extern(C) 295 { 296 /* From <dlfcn.h> 297 * See http://www.opengroup.org/onlinepubs/007908799/xsh/dlsym.html 298 */ 299 300 const int RTLD_NOW = 2; 301 302 void *dlopen(char* file, int mode); 303 int dlclose(void* handle); 304 void *dlsym(void* handle, char* name); 305 char* dlerror(); 306 } 307 } 308 309 SharedLib Platform_LoadSharedLib(char[] libName) 310 { 311 void* hlib = dlopen(toCString(libName), RTLD_NOW); 312 if(null is hlib) 313 throw new SharedLibLoadException("Failed to load shared library " ~ libName); 314 315 return new SharedLib(hlib, libName); 316 } 317 318 void Platform_UnloadSharedLib(SharedLib lib) 319 { 320 dlclose(lib._handle); 321 lib._handle = null; 322 } 323 324 void* Platform_GetProc(SharedLib lib, char[] procName) 325 { 326 void* proc = dlsym(lib._handle, toCString(procName)); 327 if(null is proc) 328 Derelict_HandleMissingProc(lib._name, procName); 329 330 return proc; 331 } 332 } 333 else 334 { 335 static assert(0); 336 } 214 } 215 else 216 { 217 static assert(0); 337 218 } 338 219 … … 340 221 341 222 struct GenericLoader { 342 void setup( char[] winLibs, char[] linLibs, char[] macLibs, void function(SharedLib) userLoad, char[]versionStr = "") {223 void setup(string winLibs, string linLibs, string macLibs, void function(SharedLib) userLoad, string versionStr = "") { 343 224 assert (userLoad !is null); 344 225 this.winLibs = winLibs; … … 349 230 } 350 231 351 void load( char[]libNameString = null)232 void load(string libNameString = null) 352 233 { 353 234 if (myLib !is null) { … … 367 248 } 368 249 else version(darwin) { 369 libNameString = macLibs;250 libNameString = macLibs; 370 251 } 371 252 372 253 if(libNameString is null || libNameString == "") 373 254 { 374 throw new DerelictException("Invalid library name");255 throw new DerelictException("Invalid library name"); 375 256 } 376 257 } 377 258 378 char[][] libNames = libNameString.splitStr(",");379 foreach (inout char[]l; libNames) {259 string[] libNames = libNameString.splitStr(","); 260 foreach (inout string l; libNames) { 380 261 l = l.stripWhiteSpace(); 381 262 } 382 263 264 load(libNames); 265 } 266 267 void load(string[] libNames) 268 { 383 269 myLib = Derelict_LoadSharedLib(libNames); 270 271 if(userLoad is null) 272 { 273 // this should never, ever, happen 274 throw new DerelictException("Something is horribly wrong -- internal load function not configured"); 275 } 384 276 userLoad(myLib); 385 277 } 386 278 387 char[]versionString()279 string versionString() 388 280 { 389 281 return versionStr; … … 398 290 } 399 291 292 bool loaded() 293 { 294 return (myLib !is null); 295 } 296 297 string libName() 298 { 299 return loaded ? myLib.name : null; 300 } 301 400 302 static ~this() 401 303 { … … 409 311 410 312 SharedLib myLib; 411 char[]winLibs;412 char[]linLibs;413 char[]macLibs;414 char[]versionStr = "";313 string winLibs; 314 string linLibs; 315 string macLibs; 316 string versionStr = ""; 415 317 416 318 void function(SharedLib) userLoad; … … 429 331 } 430 332 431 private SharedLib myLib() {432 return dependence.myLib;433 }434 435 333 void load() 436 334 { 437 assert ( myLib !is null);438 userLoad( myLib);439 } 440 441 char[]versionString()335 assert (dependence.loaded); 336 userLoad(dependence.myLib); 337 } 338 339 string versionString() 442 340 { 443 341 return dependence.versionString; … … 446 344 void unload() 447 345 { 346 } 347 348 bool loaded() 349 { 350 return dependence.loaded; 351 } 352 353 string libName() 354 { 355 return dependence.libName; 448 356 } 449 357 … … 456 364 //============================================================================== 457 365 458 struct GenericStaticLoader {459 void setup(void function() userLoad = null, char[] versionStr = "") {460 this.userLoad = userLoad;461 this.versionStr = versionStr;462 }463 464 void load()465 {466 if (userLoad !is null) {467 userLoad();468 }469 }470 471 char[] versionString()472 {473 return versionString;474 }475 476 void unload()477 {478 }479 480 private {481 char[] versionStr;482 void function() userLoad;483 }484 }485 486 487 //==============================================================================488 489 366 package struct Binder(T) { 490 void opCall( char[]n, SharedLib lib) {367 void opCall(string n, SharedLib lib) { 491 368 *fptr = Derelict_GetProc(lib, n); 492 369 } trunk/derelict/util/wrapper.d
r45 r50 51 51 } 52 52 53 char* toCString( char[]str)53 char* toCString(string str) 54 54 { 55 55 version(Tango) … … 63 63 } 64 64 65 char[]toDString(char* cstr)65 string toDString(char* cstr) 66 66 { 67 67 version(Tango) … … 75 75 } 76 76 77 int findStr( char[] str, char[]match)77 int findStr(string str, string match) 78 78 { 79 79 version(Tango) … … 88 88 } 89 89 90 char[][] splitStr(char[] str, char[]delim)90 string[] splitStr(string str, string delim) 91 91 { 92 92 return split(str, delim); 93 93 } 94 94 95 char[] stripWhiteSpace(char[]str)95 string stripWhiteSpace(string str) 96 96 { 97 97 version(Tango) trunk/src/GNUmakefile
r39 r50 1 link GNUmakefile. gdc1 link GNUmakefile.dmd trunk/src/GNUmakefile.dmd
r48 r50 87 87 88 88 $(THE_TARGET): $(OBJS) 89 gcc -m32 -o $(THE_TARGET) $(OBJS) $(DMD_DIR)/lib/libphobos .a -lm -lpthread -ldl89 gcc -m32 -o $(THE_TARGET) $(OBJS) $(DMD_DIR)/lib/libphobos2.a -lm -lpthread -ldl 90 90 91 91 $(BUILD_DIR)/%.o : %.d trunk/src/color.d
r28 r50 1 /*{ {{1 /*{// 2 2 3 3 QonkD - A simple space conquest strategy game … … 19 19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 20 21 } }}*/21 }//*/ 22 22 23 23 module color; 24 24 25 25 import std.string; 26 27 class Color //{{{ 28 { 29 real R, G, B, A; 26 import std.stdio; 27 import std.c.stdlib; 28 29 class Color 30 {// 31 float R, G, B, A; 32 float H, S, V; 30 33 ubyte r, g, b, a; 31 34 uint c; 32 35 33 static Color opCall( uint _c ) //{{{ 34 { 36 //{// static constructors 37 38 static Color opCall( uint _c ) 39 {// 35 40 Color color = new Color; 36 41 with( color ) { … … 38 43 c = _c; 39 44 40 r = c>>24; 41 g = (c>>16)&0xff; 42 b = (c>>8)&0xff; 43 a = c&0xff; 44 45 R = r/255.; 46 G = g/255.; 47 B = b/255.; 48 A = a/255.; 45 r = cast(ubyte)(c>>24); 46 g = cast(ubyte)((c>>16)&0xff); 47 b = cast(ubyte)((c>>8)&0xff); 48 a = cast(ubyte)(c&0xff); 49 50 set_reals(); 49 51 } 50 52 return color; 51 53 52 } //}}}53 54 static Color opCall( ubyte _r, ubyte _g, ubyte _b, ubyte _a = 0xff ) //{{{55 { 54 }// 55 56 static Color opCall( ubyte _r, ubyte _g, ubyte _b, ubyte _a = 0xff ) 57 {// 56 58 Color color = new Color; 57 59 with( color ) { … … 64 66 c = r<<24 | g<<16 | b<<8 | a; 65 67 66 R = r/255.; 67 G = g/255.; 68 B = b/255.; 69 A = a/255.; 68 set_reals(); 70 69 } 71 70 return color; 72 } //}}}73 74 static Color opCall( real rr, real rg, real rb, real ra = 1. ) //{{{75 { 71 }// 72 73 static Color opCall( float rr, float rg, float rb, float ra = 1. ) 74 {// 76 75 Color color = new Color; 77 76 with( color ) { … … 81 80 B = rb > 1. ? 1. : rb; 82 81 A = ra > 1. ? 1. : ra; 82 rgb2hsv( R, G, B, H, S, V ); 83 83 84 84 r = cast(ubyte)(R*255); … … 90 90 } 91 91 return color; 92 } //}}}93 94 static Color opCall( char[] str ) //{{{95 { 96 uint h[ char[]];92 }// 93 94 static Color opCall( string str ) 95 {// 96 uint h[string]; 97 97 h["black"] = 0x000000ff; 98 98 h["white"] = 0xffffffff; … … 105 105 h["gray"] = 0x7f7f7fff; 106 106 107 if(!( str in h )) 108 str = "magenta"; 109 110 return Color( h[str] ); 111 112 } //}}} 113 114 static Color hsv( real h, real s = 1., real v = 1., real a = 1. ) //{{{ 115 { 116 real r, g, b; 107 long hex2long( string str ) { 108 version(stdlib) { 109 char* end; 110 char* ptr = toStringz(str); 111 long i = std.c.stdlib.strtol( ptr, &end, 16 ); 112 if( end == ptr || *end != '\0' ) 113 throw new Exception( format("invalid hex format \"%s\"", str) ); 114 debug writefln("h2l: %s=>%s=>%x, ptr=%d, end=%d",str,std.string.toString(ptr),i,cast(long)ptr,cast(long)end); 115 return i; 116 } 117 else { 118 long i = 0; 119 str = tolower(str); 120 foreach( c; str ) { 121 i = i << 4; 122 if( c >= '0' && c <= '9' ) 123 i |= c - '0'; 124 else if( c >= 'a' && c <= 'f' ) 125 i |= c - 'a' + 10; 126 else 127 throw new Exception( format("invalid hex format \"%s\"", str) ); 128 } 129 return i; 130 } 131 } 132 133 if( str in h ) { 134 return Color( h[str] ); 135 } 136 else { 137 long c = 0x1deadbeef; 138 if( str[0] == '#' ) { 139 str = str[1 .. str.length]; 140 141 if( str.length == 3 ) 142 str = "" ~ str[0] ~ str[0] ~ str[1] ~ str[1] ~ str[2] ~ str[2]; 143 144 if( str.length == 6 ) 145 str ~= "FF"; 146 147 if( str.length == 8 ) 148 c = hex2long(str); 149 } 150 151 if( c == 0x1deadbeef ) 152 throw new Exception( format("error parsing color string \"%s\"", str) ); 153 154 return Color(cast(uint)c); 155 } 156 157 }// 158 159 static Color hsv( float h, float s = 1., float v = 1., float a = 1. ) 160 {// 161 float r, g, b; 117 162 118 163 if( s == 0 ) { … … 124 169 h /= 60; // sector 0 to 5 125 170 int i = cast(int)h; 126 realf = h - i; // factorial part of h127 realp = v * ( 1 - s );128 realq = v * ( 1 - s * f );129 realt = v * ( 1 - s * ( 1 - f ) );171 float f = h - i; // factorial part of h 172 float p = v * ( 1 - s ); 173 float q = v * ( 1 - s * f ); 174 float t = v * ( 1 - s * ( 1 - f ) ); 130 175 131 176 switch( i ) { … … 165 210 return Color( r, g, b, a ); 166 211 167 } //}}} 168 169 char[] toString() //{{{ 170 { 171 return format( "#%08x", c ); 172 } //}}} 212 }// 213 214 //}// 215 216 static void rgb2hsv( float r, float g, float b, out float h, out float s, out float v ) 217 in {// 218 assert( r>=0. && r<=1. && g>=0. && g<=1. && b>=0. && b<=1. ); 219 } 220 out { 221 //assert( s>=0. && s<=1. && v>=0. && v<=1. && h>=0. && h<360. ); 222 if(!( s>=0. && s<=1. && v>=0. && v<=1. && h>=0. && h<360. )) { 223 writefln("rgb(%f,%f,%f) -> hsv(%f,%f,%f)", r,g,b, h,s,v ); 224 assert(false); 225 } 226 } 227 body { 228 // r,g,b values are from 0 to 1 229 // h = [0,360], s = [0,1], v = [0,1] 230 // if s == 0, then h = -1 (undefined) 231 232 v = r<g?(g<b?b:g):(r<b?b:r); // max(r,g,b) 233 234 if( v == 0 ) { 235 s = 0; 236 h = 0; 237 return; 238 } 239 240 float delta = v - (r>g?(g>b?b:g):(r>b?b:r)); // max - min 241 s = delta / v; 242 243 if( delta == 0 ) // gray 244 h = 0; 245 else if( r == v ) 246 h = ( g - b ) / delta; // between yellow & magenta 247 else if( g == v ) 248 h = 2 + ( b - r ) / delta; // between cyan & yellow 249 else 250 h = 4 + ( r - g ) / delta; // between magenta & cyan 251 252 h *= 60; // degrees 253 if( h < 0 ) 254 h += 360; 255 }// 256 257 static void rgb2hsv( ubyte r, ubyte g, ubyte b, out float h, out float s, out float v ) 258 {// 259 rgb2hsv( r/255., g/255., b/255., h, s, v ); 260 }// 261 262 string toString() 263 {// 264 return format ("#%08x", c); 265 }// 173 266 174 267 // operators: 175 268 176 Color opMul( real x ) //{{{177 { 269 Color opMul( float x ) 270 {// 178 271 //return Color( R*x, G*x, B*x, A ); 179 272 return Color( R, G, B, A*x ); 180 } //}}} 181 182 } //}}} 183 184 class HueFactory //{{{ provides sequencialy hue-distributed colors 185 { 273 }// 274 275 private: 276 277 void set_reals() 278 {// 279 R = cast(float) r / ubyte.max; 280 G = cast(float) g / ubyte.max; 281 B = cast(float) b / ubyte.max; 282 A = cast(float) a / ubyte.max; 283 rgb2hsv( R, G, B, H, S, V ); 284 }// 285 286 }// 287 288 class HueFactory 289 {// provides sequencialy hue-distributed colors 290 186 291 uint idx = 0; 187 292 … … 189 294 190 295 Color next() { 191 realhue, value;296 float hue, value; 192 297 uint i = idx++; 193 298 if( i < 8 ) { … … 211 316 } 212 317 213 } //}}}214 215 // vim600:f dm=marker:318 }// 319 320 // vim600:foldmethod=marker:foldmarker={//,}//: trunk/src/config.d
r48 r50 29 29 class Config 30 30 { 31 void load_from_args( char[][] args )31 void load_from_args( string[] args ) 32 32 {// 33 33 // BUG: how will this work on a external repository? 34 char[]ver = "$Revision$ $Date$";35 char[]help_arg;36 char[]version_arg;34 string ver = "$Revision$ $Date$"; 35 string help_arg; 36 string version_arg; 37 37 version(Windows) { 38 38 help_arg = "/?"; … … 60 60 }// 61 61 62 void load_from_arg( char[]arg )62 void load_from_arg( string arg ) 63 63 {// 64 64 int p = find( arg, '=' ); 65 char[]key;66 char[]val;65 string key; 66 string val; 67 67 if( p > 0 ) { 68 68 key = arg[0 .. p]; … … 88 88 }// 89 89 90 void load_from_file( char[]filename )90 void load_from_file( string filename ) 91 91 {// 92 throw Error( "not implemented" );92 throw new Error( "not implemented" ); 93 93 }// 94 94 95 void save_to_file( char[]filename )95 void save_to_file( string filename ) 96 96 {// 97 throw Error( "not implemented" );97 throw new Error( "not implemented" ); 98 98 }// 99 99 100 char[] opIndex( char[]key )100 string opIndex( string key ) 101 101 {// 102 102 try { … … 109 109 }// 110 110 111 char[] opIndexAssign( char[] value, char[]key )111 string opIndexAssign( string value, string key ) 112 112 {// 113 113 debug(config) writefln( "values[%s] := \"%s\"", key, value ); … … 115 115 }// 116 116 117 pr otected:118 char[] [char[]] values;117 private: 118 string[string] values; 119 119 } 120 120 … … 123 123 protected: 124 124 125 char[]default_value;126 char[]help;125 string default_value; 126 string help; 127 127 /* TODO 128 128 - know the type of the item; 129 129 - constraints (min/max for numbers, list of possible values for strings (along with help for each one)); 130 130 */ 131 char[]value;131 string value; 132 132 }// 133 133 trunk/src/display.d
r49 r50 40 40 class Display 41 41 { 42 this( char[]title, Config cfg = null ) {// init video device, load font42 this( string title, Config cfg = null ) {// init video device, load font 43 43 44 44 Derelict_SetMissingProcCallback( &missing_proc_handler ); // support older SDL library versions … … 87 87 88 88 // font loading 89 char[] filename = (cfg is null) ? "" : cfg["font_filename"]; 90 if( filename != "" ) 91 { 92 DerelictSDLttf.load(); 93 if( TTF_Init() < 0 ) 94 throw new Exception( format( 95 "TTF_Init: %s", 96 std.string.toString(TTF_GetError()) ) ); 97 98 uint ptsize = cast(uint)atoi(cfg["font_size"]); 99 if( ptsize <= 0 ) 100 ptsize = 12; 101 font = TTF_OpenFont( std.string.toStringz(filename), ptsize ); 102 if( font is null ) { 103 TTF_Quit(); 104 throw new Exception( format( 105 "Unable to open font %s at size %d", 106 filename, ptsize ) ); 107 } 89 string filename = (cfg is null) ? "" : cfg["font_filename"]; 90 if( filename == "" ) { 91 //TODO: find system font 92 filename = "font.ttf"; 108 93 } 109 94 95 DerelictSDLttf.load(); 96 if( TTF_Init() < 0 ) 97 throw new Exception( format( 98 "TTF_Init: %s", 99 std.string.toString(TTF_GetError()) ) ); 100 101 uint ptsize = cast(uint)atoi(cfg["font_size"]); 102 if( ptsize <= 0 ) 103 ptsize = 12; 104 font = TTF_OpenFont( std.string.toStringz(filename), ptsize ); 105 if( font is null ) { 106 TTF_Quit(); 107 throw new Exception( format( 108 "Unable to open font %s at size %d", 109 filename, ptsize ) ); 110 } 111 110 112 int h_width; 111 TTF_SizeText( font, "H", &h_width, &text_height ) ; 113 TTF_SizeText( font, std.string.toStringz("H"), &h_width, &text_height ) ; 114 112 115 }// 113 116 … … 322 325 //}// 323 326 324 TextImage text_to_image( char[]text, Color color ) {//327 TextImage text_to_image( string text, Color color ) {// 325 328 326 329 if( font is null ) … … 328 331 329 332 // cache of text images 330 char[]key = color.toString ~ ":" ~ text;333 string key = color.toString ~ ":" ~ text; 331 334 if( key in text_cache ) 332 335 return text_cache[key]; … … 345 348 //}// 346 349 347 void draw_text( char[]text, int align_, short i, short j, Color color, out int w, out int h ) {//350 void draw_text( string text, int align_, short i, short j, Color color, out int w, out int h ) {// 348 351 349 352 TextImage img = text_to_image( text, color ); … … 367 370 } 368 371 369 void draw_text( char[]text, int align_, short i, short j, Color color )372 void draw_text( string text, int align_, short i, short j, Color color ) 370 373 { 371 374 int w, h; … … 373 376 } 374 377 375 void draw_text( char[]text, short i, short j, Color color, out int w, out int h )378 void draw_text( string text, short i, short j, Color color, out int w, out int h ) 376 379 { 377 380 draw_text( text, 0, i, j, color, w, h ); … … 416 419 417 420 TTF_Font* font; 418 TextImage[ char[]] text_cache;421 TextImage[string] text_cache; 419 422 420 423 float cx, cy, sz; // viewport … … 422 425 //}// 423 426 424 static bool missing_proc_handler( char[] libName, char[]procName ) {//427 static bool missing_proc_handler( string libName, string procName ) {// 425 428 switch( procName ) { 426 429 case "SDL_GetKeyRepeat": … … 446 449 uint last_used; 447 450 448 this( char[]text, TTF_Font* font, Color color )451 this( string text, TTF_Font* font, Color color ) 449 452 { 450 453 debug(text_cache) trunk/src/dl_gfx.d
r5 r50 257 257 private SharedLib libSDLGfxPrimitives; 258 258 259 private void* getProc( char[]procName )259 private void* getProc( string procName ) 260 260 { 261 261 return Derelict_GetProc( libSDLGfxPrimitives, procName ); … … 316 316 } 317 317 318 public void DerelictSDLGfx_Load( char[]libName )318 public void DerelictSDLGfx_Load( string libName ) 319 319 { 320 320 if( libSDLGfxPrimitives !is null ) trunk/src/events.d
r48 r50 27 27 import player; 28 28 import objects; 29 private import std.string; 29 30 30 31 //}// … … 36 37 abstract class GameEvent 37 38 {// 38 char[] serialize(); 39 static GameEvent deserialize( char[] ); 39 string serialize(); 40 41 static GameEvent deserialize (string str) 42 {// 43 if (std.string.find (str, "attack:") == 0) 44 return GameEvent_Attack.deserialize (str); 45 else if (std.string.find (str, "sendship:") == 0) 46 return GameEvent_SendShip.deserialize (str); 47 else if (std.string.find (str, "spawnship:") == 0) 48 return GameEvent_SpawnShip.deserialize (str); 49 else 50 throw new Error ("unknown game event \"" ~ str ~ "\""); 51 }// 52 40 53 }// 41 54 … … 50 63 } 51 64 52 /* 53 char[] serialize() 65 string serialize() 54 66 { 55 char[] str = "attack:"; 67 version(none) { 68 string str = "attack:"; 56 69 str ~= planet.id; 57 70 if( previous_owner !is null ) 58 71 str ~= ":" ~ previous_owner.id; 59 72 return str; 73 } else return null; 60 74 } 61 75 62 GameEvent_Attack deserialize( char[]str )76 static GameEvent_Attack deserialize( string str ) 63 77 { 64 78 return null; //TODO 65 79 } 66 */67 80 }// 68 81 … … 77 90 } 78 91 79 /* 80 char[] serialize() 92 string serialize() 81 93 { 82 return format( "sendship:%d:%d", from.id, to.id ); 94 version(none) { 95 return std.string.format( "sendship:%d:%d", from.id, to.id ); 96 } else { return null; } 83 97 } 84 98 85 void deserialize( char[]str )99 static GameEvent deserialize( string str ) 86 100 { 101 return null; 87 102 } 88 */89 103 }// 90 104 … … 92 106 {// 93 107 Planet planet; 108 109 string serialize() 110 { 111 return null; 112 } 113 114 static GameEvent deserialize (string str) 115 { 116 return null; 117 } 94 118 }// 95 119 trunk/src/list.d
r48 r50 172 172 //}}} 173 173 174 invariant { // {{{ consistency checking174 invariant() { // {{{ consistency checking 175 175 assert( item_count == 0 || ( head_ !is null && tail_ !is null ) ); 176 176 } //}}} trunk/src/main.d
r48 r50 29 29 30 30 import derelict.sdl.sdl; 31 import std.stdio; 31 32 32 33 //}// … … 37 38 this() { 38 39 // game 39 values[ "game_planets" ] = "10";40 values[ "game_bots" ] = "2";41 values[ "game_players" ] = "1";40 this[ "game_planets" ] = "10"; 41 this[ "game_bots" ] = "2"; 42 this[ "game_players" ] = "1"; 42 43 // networking 43 values[ "net_start" ] = "0";44 values[ "net_local_port" ] = "9666";45 values[ "net_remote_port" ] = "9666";46 values[ "net_remote_addr" ] = "";44 this[ "net_start" ] = "0"; 45 this[ "net_local_port" ] = "9666"; 46 this[ "net_remote_port" ] = "9666"; 47 this[ "net_remote_addr" ] = ""; 47 48 // video 48 values[ "display_width" ] = "1024";49 values[ "display_height" ] = "768";50 values[ "display_bpp" ] = "0";51 values[ "display_fullscreen" ] = "0";49 this[ "display_width" ] = "1024"; 50 this[ "display_height" ] = "768"; 51 this[ "display_bpp" ] = "0"; 52 this[ "display_fullscreen" ] = "0"; 52 53 // misc 53 values[ "coredump" ] = "0";54 this[ "coredump" ] = "0"; 54 55 } 55 56 }// 56 57 57 int main ( char[][] args)58 int main (string[] args) 58 59 { 59 QonkD_Config cfg = new QonkD_Config; 60 cfg.load_from_args( args[1 .. args.length] ); 60 try { 61 QonkD_Config cfg = new QonkD_Config; 62 cfg.load_from_args (args[1 .. args.length]); 61 63 62 Display display = new Display( args[0], cfg ); 63 UI ui = new UI( cfg ); 64 Visual visual = new Visual( display, ui ); 64 Display display = new Display (args[0], cfg); 65 65 66 //{// key-to-function mapping 66 UI ui = new UI (cfg); 67 Visual visual = new Visual (display, ui); 67 68 68 char[][SDLKey] sdlkey_to_function; 69 sdlkey_to_function[SDLK_ESCAPE] = "quit"; 70 sdlkey_to_function[SDLK_b] = "addbot"; 71 sdlkey_to_function[SDLK_F1] = "addship"; 72 sdlkey_to_function[SDLK_SPACE] = "send_mode"; //TODO: split-screen 69 //{// key-to-function mapping 73 70 74 sdlkey_to_function[SDLK_w] = "up";75 sdlkey_to_function[SDLK_a] = "left";76 sdlkey_to_function[SDLK_s] = "down";77 sdlkey_to_function[SDLK_d] = "right";78 sdlkey_to_function[SDLK_LCTRL] = "activate";71 string[SDLKey] sdlkey_to_function; 72 sdlkey_to_function[SDLK_ESCAPE] = "quit"; 73 sdlkey_to_function[SDLK_b] = "addbot"; 74 sdlkey_to_function[SDLK_F1] = "addship"; 75 sdlkey_to_function[SDLK_SPACE] = "send_mode"; //TODO: split-screen 79 76 80 sdlkey_to_function[SDLK_UP]= "up";81 sdlkey_to_function[SDLK_DOWN] = "down";82 sdlkey_to_function[SDLK_LEFT] = "left";83 sdlkey_to_function[SDLK_RIGHT]= "right";84 sdlkey_to_function[SDLK_RCTRL] = "activate";77 sdlkey_to_function[SDLK_w] = "up"; 78 sdlkey_to_function[SDLK_a] = "left"; 79 sdlkey_to_function[SDLK_s] = "down"; 80 sdlkey_to_function[SDLK_d] = "right"; 81 sdlkey_to_function[SDLK_LCTRL] = "activate"; 85 82 86 sdlkey_to_function[SDLK_KP8]= "up";87 sdlkey_to_function[SDLK_KP4] = "left";88 sdlkey_to_function[SDLK_KP2] = "down";89 sdlkey_to_function[SDLK_KP6]= "right";90 sdlkey_to_function[SDLK_KP5]= "activate";83 sdlkey_to_function[SDLK_UP] = "up"; 84 sdlkey_to_function[SDLK_DOWN] = "down"; 85 sdlkey_to_function[SDLK_LEFT] = "left"; 86 sdlkey_to_function[SDLK_RIGHT] = "right"; 87 sdlkey_to_function[SDLK_RCTRL] = "activate"; 91 88 92 sdlkey_to_function[SDLK_u] = "min_defense++"; 93 sdlkey_to_function[SDLK_j] = "min_defense--"; 94 sdlkey_to_function[SDLK_i] = "min_attack++"; 95 sdlkey_to_function[SDLK_k] = "min_attack--"; 96 sdlkey_to_function[SDLK_o] = "max_attack++"; 97 sdlkey_to_function[SDLK_l] = "max_attack--"; 89 sdlkey_to_function[SDLK_KP8] = "up"; 90 sdlkey_to_function[SDLK_KP4] = "left"; 91 sdlkey_to_function[SDLK_KP2] = "down"; 92 sdlkey_to_function[SDLK_KP6] = "right"; 93 sdlkey_to_function[SDLK_KP5] = "activate"; 98 94 99 int[SDLKey] sdlkey_to_player;100 sdlkey_to_player[SDLK_w] = 0;101 sdlkey_to_player[SDLK_a] = 0;102 sdlkey_to_player[SDLK_s] = 0;103 sdlkey_to_player[SDLK_d] = 0;104 sdlkey_to_player[SDLK_LCTRL] = 0;95 sdlkey_to_function[SDLK_u] = "min_defense++"; 96 sdlkey_to_function[SDLK_j] = "min_defense--"; 97 sdlkey_to_function[SDLK_i] = "min_attack++"; 98 sdlkey_to_function[SDLK_k] = "min_attack--"; 99 sdlkey_to_function[SDLK_o] = "max_attack++"; 100 sdlkey_to_function[SDLK_l] = "max_attack--"; 105 101 106 sdlkey_to_player[SDLK_UP] = 1; 107 sdlkey_to_player[SDLK_DOWN] = 1; 108 sdlkey_to_player[SDLK_LEFT] = 1; 109 sdlkey_to_player[SDLK_RIGHT] = 1; 110 sdlkey_to_player[SDLK_RCTRL] = 1; 102 int[SDLKey] sdlkey_to_player; 103 sdlkey_to_player[SDLK_w] = 0; 104 sdlkey_to_player[SDLK_a] = 0; 105 sdlkey_to_player[SDLK_s] = 0; 106 sdlkey_to_player[SDLK_d] = 0; 107 sdlkey_to_player[SDLK_LCTRL] = 0; 111 108 112 sdlkey_to_player[SDLK_KP8] = 2;113 sdlkey_to_player[SDLK_KP4] = 2;114 sdlkey_to_player[SDLK_KP2] = 2;115 sdlkey_to_player[SDLK_KP6] = 2;116 sdlkey_to_player[SDLK_KP5] = 2;109 sdlkey_to_player[SDLK_UP] = 1; 110 sdlkey_to_player[SDLK_DOWN] = 1; 111 sdlkey_to_player[SDLK_LEFT] = 1; 112 sdlkey_to_player[SDLK_RIGHT] = 1; 113 sdlkey_to_player[SDLK_RCTRL] = 1; 117 114 118 //}// 119 120 while( ui.is_running ) 121 {// main event loop 122 123 SDL_Event sdl_event; 124 while( SDL_PollEvent(&sdl_event) ) 125 {// process ui events 126 Event ui_event; 127 ui_event.now = SDL_GetTicks()/1000.0; 128 129 switch(sdl_event.type) 130 { 131 case SDL_KEYDOWN: 132 case SDL_KEYUP: 133 SDLKey key = sdl_event.key.keysym.sym; 134 if( key in sdlkey_to_function ) { 135 ui_event.func = sdlkey_to_function[key]; 136 ui_event.name = ( sdl_event.type == SDL_KEYUP ? "up" : "down" ); 137 } 138 if( key in sdlkey_to_player ) 139 ui_event.player = sdlkey_to_player[key]; 140 break; 141 142 case SDL_QUIT: 143 ui_event.name = "down"; 144 ui_event.func = "quit"; 145 break; 146 147 case SDL_MOUSEMOTION: 148 ui_event.name = "move"; 149 ui_event.func = ""; 150 ui_event.x = display.i2x( sdl_event.motion.x ); 151 ui_event.y = display.j2y( sdl_event.motion.y ); 152 break; 153 154 case SDL_MOUSEBUTTONDOWN: 155 case SDL_MOUSEBUTTONUP: 156 ui_event.name = ( sdl_event.type == SDL_MOUSEBUTTONUP ? "up" : "down" ); 157 if( sdl_event.button.button == SDL_BUTTON_LEFT ) 158 ui_event.func = "select"; 159 else if( sdl_event.button.button == SDL_BUTTON_RIGHT ) 160 ui_event.func = "activate"; 161 ui_event.x = display.i2x( sdl_event.motion.x ); 162 ui_event.y = display.j2y( sdl_event.motion.y ); 163 break; 164 165 default: 166 break; 167 } 168 169 ui.process_ui_event( ui_event ); 170 }// 171 172 //{// process game tick and network events 173 174 ui.process_tick( SDL_GetTicks()/1000.0 ); 175 176 visual.render(); 177 178 if( ui.world !is null ) 179 ui.world.events = []; 115 sdlkey_to_player[SDLK_KP8] = 2; 116 sdlkey_to_player[SDLK_KP4] = 2; 117 sdlkey_to_player[SDLK_KP2] = 2; 118 sdlkey_to_player[SDLK_KP6] = 2; 119 sdlkey_to_player[SDLK_KP5] = 2; 180 120 181 121 //}// 182 122 183 SDL_Delay(10); //TODO: proper fps handling 123 while (ui.is_running) 124 {// main event loop 184 125 185 }// 126 SDL_Event sdl_event; 127 while (SDL_PollEvent (&sdl_event)) 128 {// process ui events 129 Event ui_event; 130 ui_event.now = SDL_GetTicks()/1000.0; 131 132 switch (sdl_event.type) 133 { 134 case SDL_KEYDOWN: 135 case SDL_KEYUP: 136 SDLKey key = sdl_event.key.keysym.sym; 137 if (key in sdlkey_to_function) { 138 ui_event.func = sdlkey_to_function[key]; 139 ui_event.name = (sdl_event.type == SDL_KEYUP ? "up" : "down"); 140 } 141 if (key in sdlkey_to_player) 142 ui_event.player = sdlkey_to_player[key]; 143 break; 144 145 case SDL_QUIT: 146 ui_event.name = "down"; 147 ui_event.func = "quit"; 148 break; 149 150 case SDL_MOUSEMOTION: 151 ui_event.name = "move"; 152 ui_event.func = ""; 153 ui_event.x = display.i2x (sdl_event.motion.x); 154 ui_event.y = display.j2y (sdl_event.motion.y); 155 break; 156 157 case SDL_MOUSEBUTTONDOWN: 158 case SDL_MOUSEBUTTONUP: 159 ui_event.name = (sdl_event.type == SDL_MOUSEBUTTONUP ? "up" : "down"); 160 if (sdl_event.button.button == SDL_BUTTON_LEFT) 161 ui_event.func = "select"; 162 else if (sdl_event.button.button == SDL_BUTTON_RIGHT) 163 ui_event.func = "activate"; 164 ui_event.x = display.i2x (sdl_event.motion.x); 165 ui_event.y = display.j2y (sdl_event.motion.y); 166 break; 167 168 default: 169 break; 170 } 171 172 ui.process_ui_event (ui_event); 173 }// 174 175 //{// process game tick and network events 176 177 ui.process_tick (SDL_GetTicks()/1000.0); 178 179 visual.render(); 180 181 if (ui.world !is null) 182 ui.world.events = []; 183 184 //}// 185 186 SDL_Delay(10); //TODO: proper fps handling 187 188 }// 189 } 190 catch (Exception e) { 191 writefln ("exception(%s)", e); 192 } 186 193 187 194 return 0; trunk/src/math.d
r16 r50 36 36 //{{{ random number generation functions 37 37 38 real rand( real min, realmax )38 float rand( float min, float max ) 39 39 in { 40 40 assert( min < max ); … … 88 88 } 89 89 90 /+ 91 92 T max(T) ( T[] a ) 93 { 94 T m = a[0]; 95 for( uint i = 1; i < a.length; ++i ) { 96 if( a[i] > m ) 97 m = a[i]; 98 } 99 return m; 100 } 101 102 T min(T) ( T[] a ) 103 { 104 T m = a[0]; 105 for( uint i = 1; i < a.length; ++i ) { 106 if( a[i] < m ) 107 m = a[i]; 108 } 109 return m; 110 } 111 112 +/ 113 90 114 //}}} 91 115 trunk/src/menu.d
r48 r50 29 29 abstract class MenuItem //{{{ 30 30 { 31 char[]text;31 string text; 32 32 void activate( bool ); 33 invariant {33 invariant() { 34 34 assert( text !is null ); 35 35 } … … 40 40 public: 41 41 42 this( char[]t, void delegate() f )42 this( string t, void delegate() f ) 43 43 { 44 44 text = t; … … 63 63 void delegate() func; 64 64 65 invariant {65 invariant() { 66 66 assert( func !is null ); 67 67 } … … 73 73 public: 74 74 75 this( char[]t, int _min, int _max, int _val, int delegate(int) f ) //{{{75 this( string t, int _min, int _max, int _val, int delegate(int) f ) //{{{ 76 76 in { 77 77 assert( _min <= _max ); … … 128 128 129 129 int min, max, value; 130 char[]text_format;130 string text_format; 131 131 int delegate(int) func; 132 132 133 invariant {133 invariant() { 134 134 assert( value >= min ); 135 135 assert( value <= max ); … … 169 169 } 170 170 171 char[]opIndex( uint i ) {171 string opIndex( uint i ) { 172 172 return items[i].text; 173 173 } 174 174 175 char[] opIndexAssign( char[]t, uint i ) {175 string opIndexAssign( string t, uint i ) { 176 176 return ( items[i].text = t ); 177 177 } … … 181 181 } 182 182 183 /* char[][] opCall() {183 /*string[] opCall() { 184 184 return items; 185 185 }*/ … … 187 187 //}}} 188 188 189 void command( char[]cmd ) //{{{189 void command( string cmd ) //{{{ 190 190 { 191 191 debug(menu) … … 251 251 menu ~= new MI_Action( "exit", { running = false; } ); 252 252 253 char[][char] k2f;253 string[char] k2f; 254 254 k2f['h'] = "left"; 255 255 k2f['l'] = "right"; trunk/src/messages.d
r48 r50 40 40 } //}}} 41 41 42 void add( char[]text, Color c, uint display_time = 10000 ) //{{{42 void add( string text, Color c, uint display_time = 10000 ) //{{{ 43 43 { 44 44 lines.insert( new MessageLine( text, c, display_time ) ); … … 73 73 private class MessageLine //{{{ 74 74 { 75 char[]text;75 string text; 76 76 uint display_time; 77 77 uint display_start; 78 78 Color color; 79 79 80 this( char[]_text, Color _color, uint time )80 this( string _text, Color _color, uint time ) 81 81 { 82 82 text = _text; trunk/src/net.d
r33 r50 1 /*{ {{1 /*{// 2 2 3 3 QonkD - A simple space conquest strategy game … … 19 19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 20 21 } }}*/22 23 // {{{imports21 }//*/ 22 23 //{// imports 24 24 25 25 import world; 26 import events; 26 27 import list; 27 28 import std.string; … … 31 32 import derelict.sdl.sdl; 32 33 33 //} }}34 35 class Net 36 { 37 TCPsocket incoming_socket; 38 TCPsocket server_socket; 39 TCPsocket[] peer_sockets; 40 SDLNet_SocketSet peer_set; 34 //}// 35 36 static bool init = false; 37 38 abstract class Net 39 {// 40 public: 41 41 42 uint max_peers = 16; 42 43 43 this( uint port ) 44 { 44 this() 45 {// 46 assert( !net.init ); 47 net.init = true; 45 48 DerelictSDLNet.load(); 46 49 if( SDLNet_Init() < 0 ) 47 50 throw new Exception( format( "SDLNet_Init: %s", SDLNet_GetError() ) ); 48 49 IPaddress ip; 50 resolve_host( &ip, null, port ); 51 incoming_socket = tcp_open( &ip ); 52 peer_set = new_socket_set( max_peers ); 53 } 51 }// 54 52 55 53 ~this() 56 { 54 {// 57 55 //TODO: send quit message 58 56 SDLNet_Quit(); 59 } 60 61 bool connect( char[] addr, uint port ) 62 { 63 IPaddress remote_ip; 64 resolve_host( &remote_ip, addr, port ); 65 TCPsocket remote_socket = tcp_open( &remote_ip ); 66 peer_sockets ~= remote_socket; 67 server_socket = remote_socket; 68 return true; 69 } 70 71 void get_peers() 72 { 73 write_line( server_socket, "get_peers" ); 74 } 75 76 World get_world() 77 { 78 write_line( server_socket, "get_world" ); 79 80 while(true) { 81 while( !SDLNet_SocketReady( server_socket ) ) { 82 SDL_Delay( 10 ); //XXX: ugly 83 } 84 85 char[] line = read_line( server_socket ); 86 if( line is null || line == "" ) 87 return null; 88 char[][] tokens = split( line ); 89 if( tokens[0] != "world" ) 90 continue; 91 } 92 return null; 93 } 94 95 void send_events( List!(GameEvent) events ) 96 { 97 } 98 99 char[] socket_to_string( TCPsocket socket ) 100 { 101 IPaddress* p_ip = SDLNet_TCP_GetPeerAddress( socket ); 102 assert( p_ip !is null ); 103 return format( "%s %s", p_ip.host, p_ip.port ); 104 } 105 106 /* 107 TCPsocket string_to_socket( char[] str ) 108 { 109 char[][] a = std.string.split( str, ":" ); 110 } 111 */ 112 113 void write_line( TCPsocket socket, ... ) //{{{ 114 { 115 char[] buf; 57 }// 58 59 void send_events( GameEvent[] events ); 60 61 GameEvent[] receive_events(); 62 63 GameEvent[] receive_events_from_socket( TCPsocket socket ) 64 {// 65 GameEvent[] events; 66 while( SDLNet_SocketReady(socket) ) { 67 string line = read_line(socket); 68 events ~= GameEvent.deserialize( line ); 69 } 70 return events; 71 }// 72 73 private: 74 void write_line( TCPsocket socket, ... ) 75 {// 76 string buf; 116 77 std.format.doFormat( delegate( dchar c ) { buf ~= c; }, _arguments, _argptr ); 117 78 debug writefln("write_line(%s)", buf); 118 79 buf ~= "\n"; 119 80 SDLNet_TCP_Send( socket, toStringz(buf), buf.length ); 120 } //}}}121 122 char[] read_line( TCPsocket socket ) //{{{123 { 124 char[]line = "";81 }// 82 83 string read_line( TCPsocket socket ) 84 {// 85 string line = ""; 125 86 while(true) { 126 87 char c; … … 133 94 line ~= c; 134 95 } 135 } //}}} 96 }// 97 98 //{// Safe wraper for SDLNet functions (throws exception on error). 99 100 void resolve_host( IPaddress* ip, string addr, ushort port ) 101 { 102 if( SDLNet_ResolveHost( ip, std.string.toStringz(addr), port ) != 0 ) 103 throw new Exception( format( "SDLNet_ResolveHost(%s, %s): %s", addr ? addr : "<null>", port, std.string.toString(SDLNet_GetError()) ) ); 104 } 105 106 TCPsocket tcp_open( IPaddress* ip ) 107 { 108 TCPsocket socket = SDLNet_TCP_Open( ip ); 109 if( !socket ) 110 throw new Exception( format( "SDLNet_TCP_Open: %s", std.string.toString(SDLNet_GetError()) ) ); 111 return socket; 112 } 113 114 SDLNet_SocketSet new_socket_set( uint max_sockets ) 115 { 116 SDLNet_SocketSet set = SDLNet_AllocSocketSet( max_sockets ); 117 if( !set ) 118 throw new Exception( format( "SDLNet_AllocSocketSet: %s", std.string.toString(SDLNet_GetError()) ) ); 119 return set; 120 } 121 122 //}// 123 }// 124 125 class Net_Server : Net 126 {// 127 public: 128 this( int port ) 129 {// 130 IPaddress ip; 131 resolve_host( &ip, null, port ); 132 incoming_socket = tcp_open( &ip ); 133 peer_set = new_socket_set( max_peers ); 134 }// 135 136 void send_events( GameEvent[] events ) 137 {// 138 foreach( peer_socket; peer_sockets ) { 139 foreach( event; events ) { 140 auto ev = cast(GameEvent_SendShip) event; 141 if( ev is null ) 142 write_line( peer_socket, event.serialize() ); 143 } 144 } 145 }// 146 147 GameEvent[] receive_events() 148 {// 149 GameEvent[] events; 150 foreach( socket; peer_sockets ) 151 events ~= receive_events_from_socket( socket ); 152 return events; 153 }// 154 155 private: 156 TCPsocket incoming_socket; 157 TCPsocket[] peer_sockets; 158 SDLNet_SocketSet peer_set; 159 }// 160 161 class Net_Client : Net 162 {// 163 public: 164 this( string server_addr, int port ) 165 {// 166 IPaddress server_ip; 167 resolve_host( &server_ip, server_addr, port ); 168 server_socket = tcp_open( &server_ip ); 169 }// 170 171 void send_events( GameEvent[] events ) 172 {// 173 foreach( event; events ) { 174 auto ev = cast(GameEvent_SendShip) event; 175 if( ev !is null ) 176 write_line( server_socket, event.serialize() ); 177 } 178 }// 179 180 GameEvent[] receive_events() 181 {// 182 return receive_events_from_socket( server_socket ); 183 }// 184 185 private: 186 TCPsocket server_socket; 187 }// 188 189 /+ 190 class Net_Peer 191 {// deprecated 192 193 void get_peers() 194 { 195 write_line( server_socket, "get_peers" ); 196 } 197 198 World get_world() 199 { 200 write_line( server_socket, "get_world" ); 201 202 while(true) { 203 while( !SDLNet_SocketReady( server_socket ) ) { 204 SDL_Delay( 10 ); //XXX: ugly 205 } 206 207 string line = read_line( server_socket ); 208 if( line is null || line == "" ) 209 return null; 210 string[] tokens = split( line ); 211 if( tokens[0] != "world" ) 212 continue; 213 } 214 return null; 215 } 216 217 string socket_to_string( TCPsocket socket ) 218 { 219 IPaddress* p_ip = SDLNet_TCP_GetPeerAddress( socket ); 220 assert( p_ip !is null ); 221 return format( "%s %s", p_ip.host, p_ip.port ); 222 } 223 224 /* 225 TCPsocket string_to_socket( string str ) 226 { 227 string[] a = std.string.split( str, ":" ); 228 } 229 */ 136 230 137 231 void send_peer_list( TCPsocket remote ) 138 232 { 139 foreach( TCPsocketsocket; peer_sockets )233 foreach( socket; peer_sockets ) 140 234 write_line( remote, "peer_in %s", socket_to_string(socket) ); 141 235 } … … 146 240 } 147 241 148 void process_line( char[] line ) //{{{149 { 150 char[][] tokens = split( line );242 void process_line( string line ) 243 {// 244 string[] tokens = split( line ); 151 245 switch( tokens[0] ) { 152 246 /* … … 166 260 */ 167 261 } 168 } //}}}169 170 void listen() //{{{171 { 262 }// 263 264 void listen() 265 {// 172 266 TCPsocket new_socket = SDLNet_TCP_Accept( incoming_socket ); 173 267 if( new_socket ) { … … 185 279 186 280 if( SDLNet_CheckSockets( peer_set, 0 ) > 0 ) { 187 foreach( uint i, TCPsocketsocket; peer_sockets ) {281 foreach( i, socket; peer_sockets ) { 188 282 if( SDLNet_SocketReady( socket ) ) { 189 char[]line = read_line(socket);283 string line = read_line(socket); 190 284 if( line !is null && line != "" ) 191 285 process_line( line ); … … 195 289 } 196 290 } 197 } //}}}198 199 void remove_peer( uint i ) //{{{200 { 291 }// 292 293 void remove_peer( uint i ) 294 {// 201 295 debug writefln( "peer disconencted" ); 202 296 … … 214 308 peer_sockets ~= peer_sockets[i+1..last]; 215 309 } 216 } //}}} 217 218 // {{{ Safe wraper for SDLNet functions (throws exception on error). 219 220 void resolve_host( IPaddress* ip, char[] addr, ushort port ) 221 { 222 if( SDLNet_ResolveHost( ip, std.string.toStringz(addr), port ) != 0 ) 223 throw new Exception( format( "SDLNet_ResolveHost(%s, %s): %s", addr ? addr : "<null>", port, std.string.toString(SDLNet_GetError()) ) ); 224 } 225 226 TCPsocket tcp_open( IPaddress* ip ) 227 { 228 TCPsocket socket = SDLNet_TCP_Open( ip ); 229 if( !socket ) 230 throw new Exception( format( "SDLNet_TCP_Open: %s", std.string.toString(SDLNet_GetError()) ) ); 231 return socket; 232 } 233 234 SDLNet_SocketSet new_socket_set( uint max_sockets ) 235 { 236 SDLNet_SocketSet set = SDLNet_AllocSocketSet( max_sockets ); 237 if( !set ) 238 throw new Exception( format( "SDLNet_AllocSocketSet: %s", std.string.toString(SDLNet_GetError()) ) ); 239 return set; 240 } 241 242 //}}} 243 } 244 245 // vim600:fdm=marker: 310 }// 311 }// 312 +/ 313 314 // vim600:foldmethod=marker:foldmarker={//,}//: trunk/src/objects.d
r48 r50 1 /*{ {{1 /*{// 2 2 3 3 QonkD - A simple space conquest strategy game … … 19 19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 20 21 } }}*/21 }//*/ 22 22 23 23 module objects; 24 24 25 // {{{ imports 25 //{// imports 26 26 27 import world; 27 28 import player; … … 30 31 import std.math; 31 32 import std.stdio; 32 //}}} 33 34 class FlyingShip : GameObject //{{{ 35 { 36 const Planet home; // planet that generated this ship 37 const Planet target; // planet the ship is headed to 33 34 //}// 35 36 class FlyingShip : GameObject 37 {// 38 Planet home; // planet that generated this ship 39 Planet target; // planet the ship is headed to 38 40 float speed; 39 41 40 invariant { //{{{ 42 invariant() 43 {// 41 44 assert( home !is null ); 42 45 assert( target !is null ); 43 46 assert( speed == speed ); 44 } //}}} 45 46 this( Planet _home, Planet _target ) { //{{{ 47 }// 48 49 this( Planet _home, Planet _target ) 50 {// 47 51 home = _home; 48 52 target = _target; … … 50 54 super( home.x, home.y, World.ship_size, home.owner ); 51 55 owner.flying_ships.append( this ); 52 } //}}}53 54 void update( float now ) //{{{55 { 56 }// 57 58 void update( float now ) 59 {// 56 60 float dx = target.x - x; 57 61 float dy = target.y - y; … … 71 75 } 72 76 73 } //}}}74 75 } //}}}76 77 class Planet : GameObject //{{{78 { 79 // {{{data members77 }// 78 79 }// 80 81 class Planet : GameObject 82 {// 83 //{// data members 80 84 81 85 float orbit_distance; … … 88 92 float new_ship_interval; 89 93 90 //}}} 91 92 // {{{ constructor 93 94 this( Player owner_, float size_, float distance_, float speed_ ) { 94 //}// 95 96 this( Player owner_, float size_, float distance_, float speed_ ) 97 {// 95 98 super( 0, 0, size_, owner_ ); 96 99 orbit_distance = distance_; … … 114 117 } 115 118 } 116 } 117 118 this( Player owner ) { 119 }// 120 121 this( Player owner ) 122 {// 119 123 this( owner, 120 124 rand( World.min_planet_size, World.max_planet_size ), … … 122 126 rand( -16., 16. ) 123 127 ); 124 } 125 126 //}}} 127 128 void send_ship( Planet to, uint count = 1 ) //{{{ 129 in { 128 }// 129 130 void send_ship( Planet to, uint count = 1 ) 131 in {// 130 132 //assert( nships >= count ); 131 133 assert( to !is null ); … … 136 138 new FlyingShip( this, to ); 137 139 } 138 } //}}} 139 140 void update( float now ) { //{{{ 140 }// 141 142 void update( float now ) 143 {// 141 144 142 145 //TODO: speed should be linear, not circular … … 156 159 } 157 160 158 } //}}}159 160 void receive_ship( Player ship_owner ) //{{{161 { 161 }// 162 163 void receive_ship( Player ship_owner ) 164 {// 162 165 if( owner is ship_owner.world.god ) { 163 166 // burn at the sun … … 191 194 } 192 195 } 193 } //}}} 194 195 int opCmp( Planet b ) { //{{{ 196 }// 197 198 int opCmp( Planet b ) 199 {// 196 200 if( size > b.size) 197 201 return -1; … … 200 204 else 201 205 return 0; 202 } //}}} 203 204 } //}}} 205 206 class Moon : Planet //{{{ 207 { 208 this( Planet planet_ ) { //{{{ 206 }// 207 208 }// 209 210 class Moon : Planet 211 {// 212 this( Planet planet_ ) 213 {// 209 214 parent = planet_; 210 215 super( parent.owner, … … 214 219 ); 215 220 update( 0 ); // to compute initial position 216 } //}}} 217 218 void update( float now ) { //{{{ 221 }// 222 223 void update( float now ) 224 {// 219 225 super.update( now ); 220 226 x += parent.x; 221 227 y += parent.y; 222 } //}}}223 224 } //}}}225 226 abstract class GameObject // {{{base class for physical entities in the game227 { 228 }// 229 230 }// 231 232 abstract class GameObject // base class for physical entities in the game 233 {// 228 234 float x; 229 235 float y; … … 231 237 Player owner; 232 238 233 invariant { 239 invariant() 240 {// 234 241 assert( x == x ); 235 242 assert( y == y ); 236 243 assert( size > 0 ); 237 244 assert( owner !is null ); 238 } 239 240 this( float _x, float _y, float _size, Player _owner ) { 245 }// 246 247 this( float _x, float _y, float _size, Player _owner ) 248 {// 241 249 x = _x; 242 250 y = _y; 243 251 size = _size; 244 252 owner = _owner; 245 } 246 247 float dist2( float px, float py ) { 253 }// 254 255 float dist2( float px, float py ) 256 {// 248 257 float dx = x - px; 249 258 float dy = y - py; 250 259 return dx*dx + dy*dy; 251 } 252 253 float dist2( GameObject a ) { 260 }// 261 262 float dist2( GameObject a ) 263 {// 254 264 return dist2( a.x, a.y ); 255 } 256 257 } //}}}258 259 // vim600:fdm=marker: 265 }// 266 267 }// 268 269 // vim600:fdm=marker:fmr={//,}//: trunk/src/player.d
r48 r50 38 38 39 39 World world; 40 char[]name;40 string name; 41 41 Color color; 42 42 List!(Planet) planets; … … 51 51 //}// 52 52 53 invariant 53 invariant() 54 54 {// 55 55 assert( world !is null ); … … 61 61 //{// constructors 62 62 63 this( World _world, char[] _name, Color _color ) { 63 this( World _world, string _name, Color _color ) 64 { 64 65 world = _world; 65 66 name = _name; … … 70 71 } 71 72 72 this( World _world, char[] _name ) { 73 this( World _world, string _name ) 74 { 73 75 this( _world, _name, _world.hue_factory.next() ); 74 76 } … … 137 139 class Human_Player : Player 138 140 {// 139 this( World _world, char[]_name, Color _color ) {141 this( World _world, string _name, Color _color ) { 140 142 super( _world, _name, _color ); 141 143 } … … 160 162 class Neutral_Player : Player 161 163 {// 162 this( World _world, char[]_name, Color _color ) {164 this( World _world, string _name, Color _color ) { 163 165 super( _world, _name, _color ); 164 166 update_score(); trunk/src/ui.d
r47 r50 1 /*{ {{1 /*{// 2 2 3 3 QonkD - A simple space conquest strategy game … … 19 19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 20 21 } }}*/21 }//*/ 22 22 23 23 module ui; 24 24 25 // {{{ imports 25 //{// imports 26 import world; 27 import events; 26 28 import color; 27 29 import config; … … 31 33 import messages; 32 34 import net; 33 import world;34 35 import std.math; 35 36 import std.string; 36 37 debug import std.stdio; 37 //} }}38 //}// 38 39 39 40 static bool running = true; 40 41 41 42 class UI 42 { 43 {// 43 44 public: 44 45 45 //{ {{class data46 //{// class data 46 47 47 48 // game data … … 63 64 } 64 65 65 //} }}66 67 this( Config cfg ) //{{{68 { 66 //}// 67 68 this( Config cfg ) 69 {// 69 70 messages = new Messages; 70 71 drag = new Drag; … … 73 74 version(menu) { 74 75 main_menu = new Menu; 75 main_menu ~= new MI_Action( "start game", &menu_start);76 main_menu ~= new MI_Action( "start singleplayer game", &menu_singleplayer ); 76 77 main_menu ~= new MI_Int( "planets = %d", 6, 600, atoi(config["game_planets"]), &menu_planets ); 77 78 main_menu ~= new MI_Int( "bots = %d", 1, 600-1, atoi(config["game_bots"]), &menu_bots ); 79 main_menu ~= new MI_Separator(); 80 version(network) { 81 main_menu ~= new MI_Action( "host network game", &menu_server ); 82 main_menu ~= new MI_Action( "join network game", &menu_client ); 83 main_menu ~= new MI_Separator(); 84 } 85 main_menu ~= new MI_Action( "start splitscreen multiplayer game", &menu_splitscreen ); 86 main_menu ~= new MI_Int( "players = %d", 1, 3, atoi(config["game_players"]), &menu_players ); 87 main_menu ~= new MI_Separator(); 78 88 main_menu ~= new MI_Action( "exit", &menu_exit ); 79 89 … … 88 98 is_running = true; 89 99 90 } //}}}91 92 void start_singleplayer() //{{{93 { 100 }// 101 102 void start_singleplayer() 103 {// 94 104 uint planet_count = atoi(config["game_planets"]); 95 105 assert( world is null ); … … 118 128 } 119 129 } 120 } //}}} 121 122 void start_multiplayer() //{{{ 123 { 124 uint local_port = atoi( config["net_local_port"] ); 125 uint remote_port = atoi( config["net_remote_port"] ); 126 net = new Net( local_port ); 130 }// 131 132 /* 133 void start_multiplayer() 134 {// 127 135 if( config["net_remote_addr"] != "" ) { 128 assert( net.connect( config["net_remote_addr"], remote_port ) ); 129 world = net.get_world(); 136 // join 137 net = new Net_Client( config["net_remote_addr"], atoi(config["net_remote_port"]) ); 138 //world = new World(); 130 139 } 131 140 else { 132 uint planet_count = atoi( config["game_planets"] ); 133 world = new World( planet_count ); 141 // host 142 net = new Net_Server( atoi(config["net_local_port"]) ); 143 //world = new World( atoi(config["game_planets"]) ); 134 144 } 135 145 … … 137 147 control_count = 1; 138 148 controls[0] = new Control( 0, world, "me", Color("white") ); 139 world.player_join( controls[0] ); 140 } //}}} 141 142 //{{{ menu callbacks 149 //world.player_join( controls[0] ); 150 }// 151 */ 152 153 //{// menu callbacks 143 154 144 155 version(menu) { 145 void menu_start() { 156 157 void menu_singleplayer() { 158 active_menu = null; 159 config["game_players"] = "1"; 160 start_singleplayer(); 161 } 162 163 void menu_splitscreen() { 146 164 active_menu = null; 147 165 start_singleplayer(); 148 166 } 149 167 150 void menu_exit() { 151 is_running = false; 168 void menu_server() { 169 } 170 171 void menu_client() { 152 172 } 153 173 … … 163 183 return n; 164 184 } 165 } 166 167 //}}} 168 169 void select( Control player, bool all = false ) //{{{ 170 { 185 186 int menu_players( int n ) { 187 config["game_players"] = std.string.toString(n); 188 return n; 189 } 190 191 void menu_exit() { 192 is_running = false; 193 } 194 } 195 196 //}// 197 198 void select( Control player, bool all = false ) 199 {// 171 200 if( player !is null ) { 172 201 player.selected_planets.remove_all(); 173 foreach( Planetp; player.planets ) {202 foreach( p; player.planets ) { 174 203 if( all || drag.is_inside( p.x, p.y ) ) 175 204 player.selected_planets.append( p ); 176 205 } 177 206 } 178 } //}}}179 180 void process_ event( Event e ) //{{{181 { 207 }// 208 209 void process_ui_event( Event e ) 210 {// 182 211 version(menu) { 183 212 if( active_menu is null ) 184 process_game_ event( e );213 process_game_ui_event( e ); 185 214 else { 186 215 if( e.name == "down" ) { … … 192 221 } 193 222 } else { 194 process_game_ event( e );195 } 196 } //}}}197 198 void process_game_ event( Event e ) //{{{199 { 223 process_game_ui_event( e ); 224 } 225 }// 226 227 void process_game_ui_event( Event e ) 228 {// 200 229 assert( world !is null ); 201 230 const bool select_while_dragging = true; // move to preferences? 202 const realdrag_threshold = 2*world.ship_size;231 const float drag_threshold = 2*world.ship_size; 203 232 uint player_idx = ( control_count > 1 ) ? e.player : 0; 204 233 Control player = controls[player_idx]; … … 319 348 } 320 349 321 } //}}} 322 323 void process_tick( real now ) //{{{ 324 { 325 void do_process_tick( real now ) { 350 }// 351 352 void process_tick( float now ) 353 {// 354 void do_process_tick( float now ) { 355 356 if( net ) { 357 net.send_events( world.events ); 358 world.events ~= net.receive_events(); 359 } 360 326 361 for( uint i = 0; i < control_count; i++ ) { 327 362 if( controls[i].activate_repeat.update( now ) ) 328 363 controls[i].game_activate(); 329 364 } 365 330 366 world.update( now ); 331 332 if( net ) {333 net.send_events( world.events);334 net.listen();335 }336 367 337 368 process_game_events(); … … 345 376 } 346 377 347 } //}}}348 349 void process_game_events() //{{{350 { 351 foreach( GameEvente; world.events ) {352 GameEvent_SendShipev = cast(GameEvent_SendShip)e;378 }// 379 380 void process_game_events() 381 {// 382 foreach( e; world.events ) { 383 auto ev = cast(GameEvent_SendShip)e; 353 384 if( ev !is null ) 354 385 ev.from.send_ship( ev.to ); 355 386 } 356 } //}}}357 358 void update_player_count( uint n_players_alive ) { //{{{359 387 }// 388 389 void update_player_count( uint n_players_alive ) 390 {// 360 391 Player new_leader = world.players.head(); 361 392 Control me = (control_count == 1) ? controls[0] : null; … … 383 414 current_leader = new_leader; 384 415 } 385 } //}}}386 387 } 388 389 class Control : Human_Player //{{{390 { 416 }// 417 418 }// 419 420 class Control : Human_Player 421 {// 391 422 AutoRepeat activate_repeat; 392 423 Planet cursor_planet = null; 393 424 List!(Planet) selected_planets; 394 char[]send_mode;425 string send_mode; 395 426 uint idx; 396 427 397 this( uint idx_, World world_, char[] name_, Color color_ ) //{{{398 { 428 this( uint idx_, World world_, string name_, Color color_ ) 429 {// 399 430 super( world_, name_, color_ ); 400 431 idx = idx_; … … 402 433 activate_repeat = new AutoRepeat; 403 434 send_mode = "nearest"; 404 } //}}}405 406 void game_activate() //{{{407 { 435 }// 436 437 void game_activate() 438 {// 408 439 if( cursor_planet ) { 409 440 410 441 Planet find_richest( List!(Planet) list ) { 411 442 Planet richest; 412 realrichest_dist2;413 foreach( Planetp; list ) {443 float richest_dist2; 444 foreach( p; list ) { 414 445 if( p !is cursor_planet && p.owner is this && p.nships > 0 ) { 415 realdist2 = cursor_planet.dist2( p );446 float dist2 = cursor_planet.dist2( p ); 416 447 if( richest is null || 417 448 p.nships > richest.nships || … … 444 475 445 476 if( from ) { 446 world.events .append( new GameEvent_SendShip( from, cursor_planet ));447 } 448 } 449 } //}}}450 451 void move_cursor( char[] dir ) //{{{452 { 477 world.events ~= new GameEvent_SendShip( from, cursor_planet ); 478 } 479 } 480 }// 481 482 void move_cursor( string dir ) 483 {// 453 484 if( cursor_planet is null ) { 454 485 cursor_planet = planets.head(); … … 480 511 } 481 512 482 } //}}}483 484 } //}}}485 486 class Drag //{{{487 { 513 }// 514 515 }// 516 517 class Drag 518 {// 488 519 public: 489 520 490 521 bool pressed; 491 realx0, y0, x1, y1;492 493 void down( real x, realy ) {522 float x0, y0, x1, y1; 523 524 void down( float x, float y ) { 494 525 pressed = true; 495 526 x0 = x1 = x; … … 497 528 } 498 529 499 bool move( real x, real y, realthreshold = 4 ) {530 bool move( float x, float y, float threshold = 4 ) { 500 531 if( pressed ) { 501 532 if( x0 != x1 || ( abs(x-x0) > threshold ) ) { … … 517 548 } 518 549 519 bool is_inside( real x, realy ) {520 reala0 = x0, b0 = y0, a1 = x1, b1 = y1;550 bool is_inside( float x, float y ) { 551 float a0 = x0, b0 = y0, a1 = x1, b1 = y1; 521 552 sort2( a0, a1 ); 522 553 sort2( b0, b1 ); 523 554 return( x >= a0 && x <= a1 && y >= b0 && y <= b1 ); 524 555 } 525 } //}}}526 527 class AutoRepeat //{{{528 { 556 }// 557 558 class AutoRepeat 559 {// 529 560 private: 530 561 … … 560 591 } 561 592 562 } //}}}563 564 struct Event //{{{565 { 593 }// 594 595 struct Event 596 {// 566 597 float now; 567 598 float x, y; 568 char[]name;569 char[]func;599 string name; 600 string func; 570 601 int player; 571 } //}}}572 573 // vim600:f dm=marker:602 }// 603 604 // vim600:foldmethod=marker:foldmarker={//,}//: trunk/src/visual.d
r48 r50 52 52 } 53 53 54 invariant 54 invariant() 55 55 { 56 56 assert( dpy !is null ); trunk/src/world.d
r37 r50 1 /*{ {{1 /*{// 2 2 3 3 QonkD - A simple space conquest strategy game … … 19 19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 20 21 } }}*/21 }//*/ 22 22 23 23 module world; 24 24 25 // {{{imports25 //{// imports 26 26 27 27 public import objects; 28 28 public import player; 29 import events; 29 30 30 31 import color; 31 32 import list; 32 33 33 //} }}34 //}// 34 35 35 36 class World … … 37 38 public: 38 39 39 this ( uint planet_count ) { //{{{40 40 this (uint planet_count) 41 {// 41 42 players = new List!(Player); 42 events = new List!(GameEvent);43 43 hue_factory = new HueFactory; 44 44 … … 60 60 players.sort_me(); 61 61 62 } //}}}62 }// 63 63 64 64 // this( Net_World ) { TODO; } 65 65 66 bool player_join( Player player ) { //{{{ 66 bool player_join( Player player ) 67 {// 67 68 68 69 // takes one planet from neutral player … … 82 83 83 84 return true; 84 } //}}}85 }// 85 86 86 Planet find_closest_planet( real x, real y, //{{{87 Planet find_closest_planet( float x, float y, 87 88 bool delegate(Player a) player_cond = null, 88 bool delegate(Planet b) planet_cond = null 89 ) {89 bool delegate(Planet b) planet_cond = null ) 90 {// 90 91 Planet closest_planet; 91 realclosest_distance;92 float closest_distance; 92 93 93 foreach( Playerplayer; players ) {94 foreach( player; players ) { 94 95 95 96 if( player is god ) … … 99 100 continue; 100 101 101 foreach( Planetplanet; player.planets ) {102 foreach( planet; player.planets ) { 102 103 103 104 if( planet_cond && !planet_cond( planet ) ) … … 105 106 106 107 if( closest_planet ) { 107 reald = planet.dist2( x, y );108 float d = planet.dist2( x, y ); 108 109 if( d < closest_distance ) { 109 110 closest_planet = planet; … … 119 120 120 121 return closest_planet; 121 } 122 }// 122 123 123 //}}} 124 125 void update( real now ) { //{{{ 124 void update( float now ) 125 {// 126 126 127 127 if( last_update ) … … 131 131 last_update = now; 132 132 133 foreach( Playerplayer; players ) {133 foreach( player; players ) { 134 134 135 foreach( Planetplanet; player.planets )135 foreach( planet; player.planets ) 136 136 planet.update( now ); 137 137 138 foreach( FlyingShipship; player.flying_ships )138 foreach( ship; player.flying_ships ) 139 139 ship.update( now ); 140 140 141 141 player.update(); 142 142 } 143 } //}}}143 }// 144 144 145 //{{{ class data 145 Player get_player( string name ) 146 {// 147 foreach( player; players ) { 148 if( player.name == name ) 149 return player; 150 } 151 152 return null; 153 }// 154 155 //{// class data 146 156 147 157 List!(Player) players; 148 const realship_production_rate = 60; //120;149 const realship_size = 0.8;150 const realmin_moon_size = 2.;151 const realmax_moon_size = 2.5;152 const realmin_planet_size = 3.;153 const realmax_planet_size = 4.;154 const realmin_ship_speed = 60;155 const realmax_ship_speed = 90;156 const realradius = 300;158 const float ship_production_rate = 60; //120; 159 const float ship_size = 0.8; 160 const float min_moon_size = 2.; 161 const float max_moon_size = 2.5; 162 const float min_planet_size = 3.; 163 const float max_planet_size = 4.; 164 const float min_ship_speed = 60; 165 const float max_ship_speed = 90; 166 const float radius = 300; 157 167 158 168 bool started; … … 160 170 int initial_ship_count = 3; 161 171 162 reallast_update = 0;163 realdt;172 float last_update = 0; 173 float dt; 164 174 165 List!(GameEvent)events;175 GameEvent[] events; 166 176 HueFactory hue_factory; 167 177 … … 170 180 Player god; 171 181 172 //} }}182 //}// 173 183 174 invariant { //{{{ 184 invariant() 185 {// 175 186 assert( players !is null ); 176 187 assert( hue_factory !is null ); … … 178 189 assert( god !is null ); 179 190 assert( radius > 0 ); 180 } //}}}191 }// 181 192 } 182 193 183 // {{{ game events 184 185 // send_ship ( planet, planet ) 186 // attack ( planet, prev_owner ) 187 // spawn ship ( planet ) 188 189 abstract class GameEvent 190 { 191 //char[] serialize(); 192 //void deserialize( char[] ); 193 } 194 195 class GameEvent_Attack : GameEvent 196 { 197 Planet planet; 198 Player previous_owner; 199 200 this( Planet p, Player o = null ) { 201 planet = p; 202 previous_owner = o; 203 } 204 } 205 206 class GameEvent_SendShip : GameEvent 207 { 208 Planet from, to; 209 210 this( Planet from_, Planet to_ ) 211 { 212 from = from_; 213 to = to_; 214 } 215 216 /* 217 char[] serialize() 218 { 219 return format( "sendship:%d:%d", from.id, to.id ); 220 } 221 222 void deserialize( char[] str ) 223 { 224 } 225 */ 226 } 227 228 class GameEvent_SpawnShip : GameEvent 229 { 230 Planet planet; 231 } 232 233 //}}} 234 235 // vim600:fdm=marker: 194 // vim600:fdm=marker:fmr={//,}//
