| 250 | } |
| 251 | |
| 252 | int CompileScript(asIScriptEngine *engine) |
| 253 | { |
| 254 | int r; |
| 255 | |
| 256 | // This script prints a char approximately 10 times per second |
| 257 | const char *scriptMain = |
| 258 | "string char = \"-\"; " |
| 259 | "bool doQuit = false; " |
| 260 | "void main() " |
| 261 | "{ " |
| 262 | " uint time = GetSystemTime(); " |
| 263 | " while( !doQuit ) " |
| 264 | " { " |
| 265 | " uint t = GetSystemTime(); " |
| 266 | " if( t - time > 100 ) " |
| 267 | " { " |
| 268 | " time = t; " |
| 269 | " Print(char); " |
| 270 | " } " |
| 271 | " } " |
| 272 | "} "; |
| 273 | |
| 274 | const char *scriptEvents = |
| 275 | "void OnKeyPress() " |
| 276 | "{ " |
| 277 | " Print(\"A key was pressed\\n\"); " |
| 278 | " if( char == \"-\" ) " |
| 279 | " char = \"+\"; " |
| 280 | " else " |
| 281 | " char = \"-\"; " |
| 282 | "} " |
| 283 | "void OnQuit() " |
| 284 | "{ " |
| 285 | " doQuit = true; " |
| 286 | "} "; |
| 287 | |
| 288 | // Add the script sections that will be compiled into executable code |
| 289 | asIScriptModule *mod = engine->GetModule(0, asGM_ALWAYS_CREATE); |
| 290 | r = mod->AddScriptSection("scriptMain", scriptMain, strlen(scriptMain)); |
| 291 | if( r < 0 ) |
| 292 | { |
| 293 | cout << "AddScriptSection() failed" << endl; |
| 294 | return -1; |
| 295 | } |
| 296 | |
| 297 | r = mod->AddScriptSection("scriptEvents", scriptEvents, strlen(scriptEvents)); |
| 298 | if( r < 0 ) |
| 299 | { |
| 300 | cout << "AddScriptSection() failed" << endl; |
| 301 | return -1; |
| 302 | } |
| 303 | |
| 304 | // Compile the script |
| 305 | r = mod->Build(); |
| 306 | if( r < 0 ) |
| 307 | { |
| 308 | cout << "Build() failed" << endl; |
| 309 | return -1; |
no test coverage detected