1 /* 2 DSFML - The Simple and Fast Multimedia Library for D 3 4 Copyright (c) 2013 - 2015 Jeremy DeHaan (dehaan.jeremiah@gmail.com) 5 6 This software is provided 'as-is', without any express or implied warranty. 7 In no event will the authors be held liable for any damages arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, including commercial applications, 10 and to alter it and redistribute it freely, subject to the following restrictions: 11 12 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. 13 If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 14 15 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 16 17 3. This notice may not be removed or altered from any source distribution 18 */ 19 20 module dsfml.graphics.renderwindow; 21 22 import dsfml.graphics.color; 23 import dsfml.graphics.image; 24 import dsfml.graphics.rect; 25 26 import dsfml.graphics.drawable; 27 import dsfml.graphics.primitivetype; 28 import dsfml.graphics.renderstates; 29 import dsfml.graphics.rendertarget; 30 import dsfml.graphics.shader; 31 import dsfml.graphics.text; 32 import dsfml.graphics.texture; 33 import dsfml.graphics.view; 34 import dsfml.graphics.vertex; 35 36 37 import dsfml.window.contextsettings; 38 import dsfml.window.windowhandle; 39 import dsfml.window.event; 40 import dsfml.window.window; 41 import dsfml.window.videomode; 42 43 import dsfml.system.err; 44 import dsfml.system.vector2; 45 46 /++ 47 + Window that can serve as a target for 2D drawing. 48 + 49 + RenderWindow is the main class of the Graphics package. 50 + 51 + It defines an OS window that can be painted using the other classes of the graphics module. 52 + 53 + RenderWindow is derived from Window, thus it inherits all its features: events, window management, OpenGL rendering, etc. See the documentation of Window for a more complete description of all these features, as well as code examples. 54 + 55 + On top of that, RenderWindow adds more features related to 2D drawing with the graphics module (see its base class RenderTarget for more details). 56 + 57 + Like Window, RenderWindow is still able to render direct OpenGL stuff. It is even possible to mix together OpenGL calls and regular SFML drawing commands. 58 + 59 + Authors: Laurent Gomila, Jeremy DeHaan 60 + See_Also: http://sfml-dev.org/documentation/2.0/classsf_1_1RenderWindow.php#details 61 +/ 62 class RenderWindow : Window, RenderTarget 63 { 64 package sfRenderWindow* sfPtr; 65 private View m_currentView, m_defaultView; 66 67 68 this() 69 { 70 sfPtr = sfRenderWindow_construct(); 71 m_currentView = new View(); 72 m_defaultView = new View(); 73 super(0); 74 } 75 76 //in order to envoke this constructor when using string literals, be sure to use the d suffix, i.e. "素晴らしい !"d 77 this(T)(VideoMode mode, immutable(T)[] title, Style style = Style.DefaultStyle, ref const(ContextSettings) settings = ContextSettings.Default) 78 if (is(T == dchar)||is(T == wchar)||is(T == char)) 79 { 80 81 this(); 82 create(mode, title, style, settings); 83 } 84 85 this(WindowHandle handle, ref const(ContextSettings) settings = ContextSettings.Default) 86 { 87 this(); 88 create(handle, settings); 89 } 90 91 ~this() 92 { 93 import dsfml.system.config; 94 mixin(destructorOutput); 95 sfRenderWindow_destroy(sfPtr); 96 } 97 98 /** 99 * Change the position of the window on screen. 100 * 101 * This property only works for top-level windows (i.e. it will be ignored for windows created from the handle of a child window/control). 102 */ 103 @property 104 { 105 override Vector2i position(Vector2i newPosition) 106 { 107 sfRenderWindow_setPosition(sfPtr,newPosition.x, newPosition.y); 108 return newPosition; 109 } 110 111 override Vector2i position() 112 { 113 Vector2i temp; 114 sfRenderWindow_getPosition(sfPtr,&temp.x, &temp.y); 115 return temp; 116 } 117 } 118 119 /** 120 * The size of the rendering region of the window. 121 */ 122 @property 123 { 124 override Vector2u size(Vector2u newSize) 125 { 126 sfRenderWindow_setSize(sfPtr, newSize.x, newSize.y); 127 return newSize; 128 } 129 override Vector2u size() 130 { 131 Vector2u temp; 132 sfRenderWindow_getSize(sfPtr,&temp.x, &temp.y); 133 return temp; 134 } 135 } 136 137 /** 138 * Change the current active view. 139 * 140 * The view is like a 2D camera, it controls which part of the 2D scene is visible, and how it is viewed in the render-target. The new view will affect everything that is drawn, until another view is set. 141 * 142 * The render target keeps its own copy of the view object, so it is not necessary to keep the original one alive after calling this function. To restore the original view of the target, you can pass the result of getDefaultView() to this function. 143 */ 144 @property 145 { 146 const(View) view(const(View) newView) 147 { 148 sfRenderWindow_setView(sfPtr, newView.sfPtr); 149 m_currentView = new View(sfRenderWindow_getView(sfPtr)); 150 return m_currentView; 151 } 152 const(View) view() const 153 { 154 return m_currentView; 155 } 156 } 157 158 /** 159 * Get the default view of the render target. 160 * 161 * The default view has the initial size of the render target, and never changes after the target has been created. 162 * 163 * Returns: The default view of the render target. 164 */ 165 const(View) getDefaultView() const // note: if refactored, change documentation of view property above 166 { 167 return m_defaultView; 168 } 169 170 /** 171 * Get the settings of the OpenGL context of the window. 172 * 173 * Note that these settings may be different from what was passed to the constructor or the create() function, if one or more settings were not supported. In this case, SFML chose the closest match. 174 * 175 * Returns: Structure containing the OpenGL context settings 176 */ 177 override ContextSettings getSettings() const 178 { 179 ContextSettings temp; 180 sfRenderWindow_getSettings(sfPtr,&temp.depthBits, &temp.stencilBits, &temp.antialiasingLevel, &temp.majorVersion, &temp.minorVersion); 181 return temp; 182 } 183 184 //this is a duplicate with the size property. Need to look into that.(Inherited from RenderTarget) 185 /** 186 * Return the size of the rendering region of the target. 187 * 188 * Returns: Size in pixels 189 */ 190 Vector2u getSize() const 191 { 192 Vector2u temp; 193 194 sfRenderWindow_getSize(sfPtr, &temp.x, &temp.y); 195 196 return temp; 197 } 198 199 /** 200 * Get the OS-specific handle of the window. 201 * 202 * The type of the returned handle is WindowHandle, which is a typedef to the handle type defined by the OS. You shouldn't need to use this function, unless you have very specific stuff to implement that SFML doesn't support, or implement a temporary workaround until a bug is fixed. 203 * 204 * Returns: System handle of the window 205 */ 206 override WindowHandle getSystemHandle() const 207 { 208 return sfRenderWindow_getSystemHandle(sfPtr); 209 } 210 211 /** 212 * Get the viewport of a view, applied to this render target. 213 * 214 * The viewport is defined in the view as a ratio, this function simply applies this ratio to the current dimensions of the render target to calculate the pixels rectangle that the viewport actually covers in the target. 215 * 216 * Params: 217 * view = The view for which we want to compute the viewport 218 * 219 * Returns: Viewport rectangle, expressed in pixels 220 */ 221 IntRect getViewport(const(View) view) const 222 { 223 IntRect temp; 224 225 sfRenderWindow_getViewport(sfPtr, view.sfPtr, &temp.left, &temp.top, &temp.width, &temp.height); 226 227 return temp; 228 } 229 230 /** 231 * Get the viewport of a view, applied to this render target. 232 * 233 * A window is active only on the current thread, if you want to make it active on another thread you have to deactivate it on the previous thread first if it was active. Only one window can be active on a thread at a time, thus the window previously active (if any) automatically gets deactivated. 234 * 235 * Params: 236 * active = True to activate, false to deactivate 237 * 238 * Returns: True if operation was successful, false otherwise 239 */ 240 override bool setActive(bool active) 241 { 242 import dsfml.system..string; 243 bool toReturn = sfRenderWindow_setActive(sfPtr, active); 244 err.write(dsfml.system..string.toString(sfErr_getOutput())); 245 return toReturn; 246 } 247 248 /** 249 * Limit the framerate to a maximum fixed frequency. 250 * 251 * If a limit is set, the window will use a small delay after each call to display() to ensure that the current frame lasted long enough to match the framerate limit. 252 * 253 * SFML will try to match the given limit as much as it can, but since it internally uses sf::sleep, whose precision depends on the underlying OS, the results may be a little unprecise as well (for example, you can get 65 FPS when requesting 60). 254 * 255 * Params: 256 * limit = Framerate limit, in frames per seconds (use 0 to disable limit) 257 */ 258 override void setFramerateLimit(uint limit) 259 { 260 sfRenderWindow_setFramerateLimit(sfPtr, limit); 261 } 262 263 /** 264 * Change the window's icon. 265 * 266 * pixels must be an array of width x height pixels in 32-bits RGBA format. 267 * 268 * The OS default icon is used by default. 269 * 270 * Params: 271 * width = Icon's width, in pixels 272 * height = Icon's height, in pixels 273 * pixels = Icon pixel array to load from 274 */ 275 override void setIcon(uint width, uint height, const(ubyte[]) pixels) 276 { 277 sfRenderWindow_setIcon(sfPtr,width, height, pixels.ptr); 278 } 279 280 /** 281 * Change the joystick threshold. 282 * 283 * The joystick threshold is the value below which no JoystickMoved event will be generated. 284 * 285 * The threshold value is 0.1 by default. 286 * 287 * Params: 288 * threshold = New threshold, in the range [0, 100] 289 */ 290 override void setJoystickThreshhold(float threshhold) 291 { 292 sfRenderWindow_setJoystickThreshold(sfPtr, threshhold); 293 } 294 295 /** 296 * Enable or disable automatic key-repeat. 297 * 298 * If key repeat is enabled, you will receive repeated KeyPressed events while keeping a key pressed. If it is disabled, you will only get a single event when the key is pressed. 299 * 300 * Key repeat is enabled by default. 301 * 302 * Params: 303 * enabled = True to enable, false to disable 304 */ 305 override void setKeyRepeatEnabled(bool enabled) 306 { 307 enabled ? sfRenderWindow_setKeyRepeatEnabled(sfPtr,true):sfRenderWindow_setKeyRepeatEnabled(sfPtr,false); 308 } 309 310 /** 311 * Show or hide the mouse cursor. 312 * 313 * The mouse cursor is visible by default. 314 * 315 * Params: 316 * enabled = True show the mouse cursor, false to hide it 317 */ 318 override void setMouseCursorVisible(bool visible) 319 { 320 visible ? sfRenderWindow_setMouseCursorVisible(sfPtr,true): sfRenderWindow_setMouseCursorVisible(sfPtr,false); 321 } 322 323 //Cannot use templates here as template member functions cannot be virtual. 324 325 /** 326 * Change the title of the window 327 * 328 * Params: 329 * title = New title 330 */ 331 override void setTitle(string newTitle) 332 { 333 import dsfml.system..string; 334 sfRenderWindow_setUnicodeTitle(sfPtr, toStringz(stringConvert!(char, dchar)(newTitle))); 335 } 336 /** 337 * Change the title of the window 338 * 339 * Params: 340 * title = New title 341 */ 342 override void setTitle(wstring newTitle) 343 { 344 import dsfml.system..string; 345 sfRenderWindow_setUnicodeTitle(sfPtr, toStringz(stringConvert!(wchar, dchar)(newTitle))); 346 } 347 /** 348 * Change the title of the window 349 * 350 * Params: 351 * title = New title 352 */ 353 override void setTitle(dstring newTitle) 354 { 355 import dsfml.system..string; 356 sfRenderWindow_setUnicodeTitle(sfPtr, toStringz(newTitle)); 357 } 358 359 /** 360 * Enable or disable vertical synchronization. 361 * 362 * Activating vertical synchronization will limit the number of frames displayed to the refresh rate of the monitor. This can avoid some visual artifacts, and limit the framerate to a good value (but not constant across different computers). 363 * 364 * Vertical synchronization is disabled by default. 365 * 366 * Params: 367 * enabled = True to enable v-sync, false to deactivate it 368 */ 369 override void setVerticalSyncEnabled(bool enabled) 370 { 371 enabled ? sfRenderWindow_setVerticalSyncEnabled(sfPtr, true): sfRenderWindow_setVerticalSyncEnabled(sfPtr, false); 372 } 373 374 /** 375 * Show or hide the window. 376 * 377 * The window is shown by default. 378 * 379 * Params: 380 * visible = True to show the window, false to hide it 381 */ 382 override void setVisible(bool visible) 383 { 384 sfRenderWindow_setVisible(sfPtr,visible); 385 } 386 387 /** 388 * Clear the entire target with a single color. 389 * 390 * This function is usually called once every frame, to clear the previous contents of the target. 391 * 392 * Params: 393 * color = Fill color to use to clear the render target 394 */ 395 void clear(Color color = Color.Black) 396 { 397 sfRenderWindow_clear(sfPtr, color.r,color.g, color.b, color.a); 398 } 399 400 /** 401 * Close the window and destroy all the attached resources. 402 * 403 * After calling this function, the Window instance remains valid and you can call create() to recreate the window. All other functions such as pollEvent() or display() will still work (i.e. you don't have to test isOpen() every time), and will have no effect on closed windows. 404 */ 405 override void close() 406 { 407 sfRenderWindow_close(sfPtr); 408 } 409 410 //Cannot use templates here as template member functions cannot be virtual. 411 412 ///Create (or recreate) the window. 413 /// 414 ///If the window was already created, it closes it first. If style contains Style::Fullscreen, then mode must be a valid video mode. 415 /// 416 ///The fourth parameter is an optional structure specifying advanced OpenGL context settings such as antialiasing, depth-buffer bits, etc. 417 override void create(VideoMode mode, string title, Style style = Style.DefaultStyle, ref const(ContextSettings) settings = ContextSettings.Default) 418 { 419 import dsfml.system..string; 420 421 sfRenderWindow_createFromSettings(sfPtr, mode.width, mode.height, mode.bitsPerPixel, toStringz(stringConvert!(char,dchar)(title)), style, settings.depthBits, settings.stencilBits, settings.antialiasingLevel, settings.majorVersion, settings.minorVersion); 422 err.write(dsfml.system..string.toString(sfErr_getOutput())); 423 424 //get view 425 m_currentView = new View(sfRenderWindow_getView(sfPtr)); 426 427 //get default view 428 m_defaultView = new View(sfRenderWindow_getDefaultView(sfPtr)); 429 430 } 431 ///Create (or recreate) the window. 432 /// 433 ///If the window was already created, it closes it first. If style contains Style::Fullscreen, then mode must be a valid video mode. 434 /// 435 ///The fourth parameter is an optional structure specifying advanced OpenGL context settings such as antialiasing, depth-buffer bits, etc. 436 override void create(VideoMode mode, wstring title, Style style = Style.DefaultStyle, ref const(ContextSettings) settings = ContextSettings.Default) 437 { 438 import dsfml.system..string; 439 440 sfRenderWindow_createFromSettings(sfPtr, mode.width, mode.height, mode.bitsPerPixel, toStringz(stringConvert!(wchar,dchar)(title)), style, settings.depthBits, settings.stencilBits, settings.antialiasingLevel, settings.majorVersion, settings.minorVersion); 441 err.write(dsfml.system..string.toString(sfErr_getOutput())); 442 443 //get view 444 m_currentView = new View(sfRenderWindow_getView(sfPtr)); 445 446 //get default view 447 m_defaultView = new View(sfRenderWindow_getDefaultView(sfPtr)); 448 449 } 450 ///Create (or recreate) the window. 451 /// 452 ///If the window was already created, it closes it first. If style contains Style::Fullscreen, then mode must be a valid video mode. 453 /// 454 ///The fourth parameter is an optional structure specifying advanced OpenGL context settings such as antialiasing, depth-buffer bits, etc. 455 override void create(VideoMode mode, dstring title, Style style = Style.DefaultStyle, ref const(ContextSettings) settings = ContextSettings.Default) 456 { 457 import dsfml.system..string; 458 459 sfRenderWindow_createFromSettings(sfPtr, mode.width, mode.height, mode.bitsPerPixel, toStringz(title), style, settings.depthBits, settings.stencilBits, settings.antialiasingLevel, settings.majorVersion, settings.minorVersion); 460 err.write(dsfml.system..string.toString(sfErr_getOutput())); 461 462 //get view 463 m_currentView = new View(sfRenderWindow_getView(sfPtr)); 464 465 //get default view 466 m_defaultView = new View(sfRenderWindow_getDefaultView(sfPtr)); 467 468 } 469 470 ///Create (or recreate) the window from an existing control. 471 /// 472 ///Use this function if you want to create an OpenGL rendering area into an already existing control. If the window was already created, it closes it first. 473 /// 474 ///The second parameter is an optional structure specifying advanced OpenGL context settings such as antialiasing, depth-buffer bits, etc. 475 override void create(WindowHandle handle, ref const(ContextSettings) settings = ContextSettings.Default) 476 { 477 import dsfml.system..string; 478 sfRenderWindow_createFromHandle(sfPtr, handle, settings.depthBits,settings.stencilBits, settings.antialiasingLevel, settings.majorVersion, settings.minorVersion); 479 err.write(dsfml.system..string.toString(sfErr_getOutput())); 480 //get view 481 m_currentView = new View(sfRenderWindow_getView(sfPtr)); 482 483 //get default view 484 m_defaultView = new View(sfRenderWindow_getDefaultView(sfPtr)); 485 } 486 487 /** 488 * Display on screen what has been rendered to the window so far. 489 * 490 * This function is typically called after all OpenGL rendering has been done for the current frame, in order to show it on screen. 491 */ 492 override void display() 493 { 494 sfRenderWindow_display(sfPtr); 495 } 496 497 /** 498 * Draw a drawable object to the render target. 499 * 500 * Params: 501 * drawable = Object to draw 502 * states = Render states to use for drawing 503 */ 504 void draw(Drawable drawable, RenderStates states = RenderStates.Default) 505 { 506 //Confirms that even a blank render states struct won't break anything during drawing 507 if(states.texture is null) 508 { 509 states.texture = RenderStates.emptyTexture; 510 } 511 if(states.shader is null) 512 { 513 states.shader = RenderStates.emptyShader; 514 } 515 516 drawable.draw(this,states); 517 } 518 519 /** 520 * Draw primitives defined by an array of vertices. 521 * 522 * Params: 523 * vertices = Array of vertices to draw 524 * type = Type of primitives to draw 525 * states = Render states to use for drawing 526 */ 527 void draw(const(Vertex)[] vertices, PrimitiveType type, RenderStates states = RenderStates.Default) 528 { 529 import std.algorithm; 530 //Confirms that even a blank render states struct won't break anything during drawing 531 if(states.texture is null) 532 { 533 states.texture = RenderStates.emptyTexture; 534 } 535 if(states.shader is null) 536 { 537 states.shader = RenderStates.emptyShader; 538 } 539 540 sfRenderWindow_drawPrimitives(sfPtr, vertices.ptr, cast(uint)min(uint.max, vertices.length), type,states.blendMode, states.transform.m_matrix.ptr,states.texture.sfPtr,states.shader.sfPtr); 541 } 542 543 /** 544 * Tell whether or not the window is open. 545 * 546 * This function returns whether or not the window exists. Note that a hidden window (setVisible(false)) is open (therefore this function would return true). 547 * 548 * Returns: True if the window is open, false if it has been closed 549 */ 550 override bool isOpen() 551 { 552 return (sfRenderWindow_isOpen(sfPtr)); 553 } 554 555 /** 556 * Convert a point fom target coordinates to world coordinates, using the current view. 557 * 558 * This function is an overload of the mapPixelToCoords function that implicitely uses the current view. 559 * 560 * Params: 561 * point = Pixel to convert 562 * 563 * Returns: The converted point, in "world" coordinates. 564 */ 565 Vector2f mapPixelToCoords(Vector2i point) const 566 { 567 Vector2f temp; 568 569 sfRenderWindow_mapPixelToCoords(sfPtr,point.x, point.y, &temp.x, &temp.y,null); 570 571 return temp; 572 } 573 574 /** 575 * Convert a point from target coordinates to world coordinates. 576 * 577 * This function finds the 2D position that matches the given pixel of the render-target. In other words, it does the inverse of what the graphics card does, to find the initial position of a rendered pixel. 578 * 579 * Initially, both coordinate systems (world units and target pixels) match perfectly. But if you define a custom view or resize your render-target, this assertion is not true anymore, ie. a point located at (10, 50) in your render-target may map to the point (150, 75) in your 2D world – if the view is translated by (140, 25). 580 * 581 * For render-windows, this function is typically used to find which point (or object) is located below the mouse cursor. 582 * 583 * This version uses a custom view for calculations, see the other overload of the function if you want to use the current view of the render-target. 584 * 585 * Params: 586 * point = Pixel to convert 587 * view = The view to use for converting the point 588 * 589 * Returns: The converted point, in "world" coordinates. 590 */ 591 Vector2f mapPixelToCoords(Vector2i point, const(View) view) const 592 { 593 Vector2f temp; 594 595 sfRenderWindow_mapPixelToCoords(sfPtr,point.x, point.y, &temp.x, &temp.y,view.sfPtr); 596 597 return temp; 598 } 599 600 /** 601 * Convert a point from target coordinates to world coordinates, using the current view. 602 * 603 * This function is an overload of the mapPixelToCoords function that implicitely uses the current view. 604 * 605 * Params: 606 * point = Point to convert 607 * 608 * The converted point, in "world" coordinates 609 */ 610 Vector2i mapCoordsToPixel(Vector2f point) const 611 { 612 Vector2i temp; 613 614 sfRenderWindow_mapCoordsToPixel(sfPtr,point.x, point.y, &temp.x, &temp.y,null); 615 616 return temp; 617 } 618 619 /** 620 * Convert a point from world coordinates to target coordinates. 621 * 622 * This function finds the pixel of the render-target that matches the given 2D point. In other words, it goes through the same process as the graphics card, to compute the final position of a rendered point. 623 * 624 * Initially, both coordinate systems (world units and target pixels) match perfectly. But if you define a custom view or resize your render-target, this assertion is not true anymore, ie. a point located at (150, 75) in your 2D world may map to the pixel (10, 50) of your render-target – if the view is translated by (140, 25). 625 * 626 * This version uses a custom view for calculations, see the other overload of the function if you want to use the current view of the render-target. 627 * 628 * Params: 629 * point = Point to convert 630 * view = The view to use for converting the point 631 * 632 * Returns: The converted point, in target coordinates (pixels) 633 */ 634 Vector2i mapCoordsToPixel(Vector2f point, const(View) view) const 635 { 636 Vector2i temp; 637 638 sfRenderWindow_mapCoordsToPixel(sfPtr,point.x, point.y, &temp.x, &temp.y,view.sfPtr); 639 640 return temp; 641 } 642 643 /** 644 * Restore the previously saved OpenGL render states and matrices. 645 * 646 * See the description of pushGLStates to get a detailed description of these functions. 647 */ 648 void popGLStates() 649 { 650 sfRenderWindow_popGLStates(sfPtr); 651 } 652 653 /** 654 * Save the current OpenGL render states and matrices. 655 * 656 * This function can be used when you mix SFML drawing and direct OpenGL rendering. Combined with PopGLStates, it ensures that: 657 * - SFML's internal states are not messed up by your OpenGL code 658 * - your OpenGL states are not modified by a call to an SFML function 659 * 660 * More specifically, it must be used around the code that calls Draw functions. 661 * 662 * Note that this function is quite expensive: it saves all the possible OpenGL states and matrices, even the ones you don't care about. Therefore it should be used wisely. It is provided for convenience, but the best results will be achieved if you handle OpenGL states yourself (because you know which states have really changed, and need to be saved and restored). Take a look at the ResetGLStates function if you do so. 663 */ 664 void pushGLStates() 665 { 666 import dsfml.system..string; 667 sfRenderWindow_pushGLStates(sfPtr); 668 err.write(dsfml.system..string.toString(sfErr_getOutput())); 669 } 670 671 /** 672 * Reset the internal OpenGL states so that the target is ready for drawing. 673 * 674 * This function can be used when you mix SFML drawing and direct OpenGL rendering, if you choose not to use pushGLStates/popGLStates. It makes sure that all OpenGL states needed by SFML are set, so that subsequent draw() calls will work as expected. 675 */ 676 void resetGLStates() 677 { 678 sfRenderWindow_resetGLStates(sfPtr); 679 } 680 681 /** 682 * Pop the event on top of the event queue, if any, and return it. 683 * 684 * This function is not blocking: if there's no pending event then it will return false and leave event unmodified. Note that more than one event may be present in the event queue, thus you should always call this function in a loop to make sure that you process every pending event. 685 * 686 * Params: 687 * event = Event to be returned 688 * 689 * Returns: True if an event was returned, or false if the event queue was empty 690 */ 691 override bool pollEvent(ref Event event) 692 { 693 return (sfRenderWindow_pollEvent(sfPtr, &event)); 694 } 695 696 /** 697 * Wait for an event and return it. 698 * 699 * This function is blocking: if there's no pending event then it will wait until an event is received. After this function returns (and no error occured), the event object is always valid and filled properly. This function is typically used when you have a thread that is dedicated to events handling: you want to make this thread sleep as long as no new event is received. 700 * 701 * Params: 702 * event = Event to be returned 703 * 704 * Returns: False if any error occurred 705 */ 706 override bool waitEvent(ref Event event) 707 { 708 return (sfRenderWindow_waitEvent(sfPtr, &event)); 709 } 710 711 //TODO: Consider adding these methods. 712 //void onCreate 713 //void onResize 714 715 override protected Vector2i getMousePosition()const 716 { 717 Vector2i temp; 718 sfMouse_getPositionRenderWindow(sfPtr, &temp.x, &temp.y); 719 return temp; 720 } 721 722 //TODO: Fix these names or something. 723 override protected void setMousePosition(Vector2i pos) const 724 { 725 sfMouse_setPositionRenderWindow(pos.x, pos.y, sfPtr); 726 } 727 728 //Provides the static windowPointer method a way to get the pointer 729 //Window.getWindowPointer is protected, so the static method cannot call it directly 730 private void* getWindowPtr(Window window) 731 { 732 return getWindowPointer(window); 733 } 734 735 //let's Texture have a way to get the sfPtr of a regular window. 736 package static void* windowPointer(Window window) 737 { 738 scope RenderWindow temp = new RenderWindow(); 739 740 return temp.getWindowPtr(window); 741 } 742 743 } 744 745 unittest 746 { 747 version(DSFML_Unittest_Graphics) 748 { 749 import std.stdio; 750 import dsfml.graphics.image; 751 import dsfml.system.clock; 752 import dsfml.graphics.sprite; 753 754 writeln("Unit test for RenderWindow"); 755 756 //constructor 757 auto window = new RenderWindow(VideoMode(800,600),"Test Window"); 758 759 //perform each window call 760 Vector2u windowSize = window.size; 761 762 windowSize.x = 1000; 763 windowSize.y = 1000; 764 765 window.size = windowSize; 766 767 Vector2i windowPosition = window.position; 768 769 windowPosition.x = 100; 770 windowPosition.y = 100; 771 772 window.position = windowPosition; 773 774 window.setTitle("thing");//uses the first set title 775 776 window.setTitle("素晴らしい !"d);//forces the dstring override and uses unicode 777 778 window.setActive(true); 779 780 window.setJoystickThreshhold(1); 781 782 window.setVisible(true); 783 784 window.setFramerateLimit(60); 785 786 window.setMouseCursorVisible(true); 787 788 window.setVerticalSyncEnabled(true); 789 790 auto settings = window.getSettings(); 791 792 auto image = new Image(); 793 image.loadFromFile("res/TestImage.png"); 794 795 window.setIcon(image.getSize().x,image.getSize().x,image.getPixelArray()); 796 797 auto texture = new Texture(); 798 799 texture.loadFromImage(image); 800 801 auto sprite = new Sprite(texture); 802 803 auto clock = new Clock(); 804 805 while(window.isOpen()) 806 { 807 Event event; 808 if(window.pollEvent(event)) 809 { 810 //no events 811 } 812 813 if(clock.getElapsedTime().total!"seconds" > 1) 814 { 815 window.close(); 816 } 817 818 window.clear(); 819 820 window.draw(sprite); 821 822 window.display(); 823 824 } 825 826 827 writeln(); 828 } 829 } 830 831 package extern(C) struct sfRenderWindow; 832 833 private extern(C): 834 835 //Construct a new render window 836 sfRenderWindow* sfRenderWindow_construct(); 837 838 //Construct a new render window from settings 839 sfRenderWindow* sfRenderWindow_constructFromSettings(uint width, uint height, uint bitsPerPixel, const(dchar)* title, int style, uint depthBits, uint stencilBits, uint antialiasingLevel, uint majorVersion, uint minorVersion); 840 841 //Construct a render window from an existing control 842 sfRenderWindow* sfRenderWindow_constructFromHandle(WindowHandle handle, uint depthBits, uint stencilBits, uint antialiasingLevel, uint majorVersion, uint minorVersion); 843 844 //Create(or recreate) a new render window from settings 845 void sfRenderWindow_createFromSettings(sfRenderWindow* renderWindow, uint width, uint height, uint bitsPerPixel, const(dchar)* title, int style, uint depthBits, uint stencilBits, uint antialiasingLevel, uint majorVersion, uint minorVersion); 846 847 //Create(or recreate) a render window from an existing control 848 void sfRenderWindow_createFromHandle(sfRenderWindow* renderWindow, WindowHandle handle, uint depthBits, uint stencilBits, uint antialiasingLevel, uint majorVersion, uint minorVersion); 849 850 //Destroy an existing render window 851 void sfRenderWindow_destroy(sfRenderWindow* renderWindow); 852 853 //Close a render window (but doesn't destroy the internal data) 854 void sfRenderWindow_close(sfRenderWindow* renderWindow); 855 856 //Tell whether or not a render window is opened 857 bool sfRenderWindow_isOpen(const sfRenderWindow* renderWindow); 858 859 //Get the creation settings of a render window 860 void sfRenderWindow_getSettings(const sfRenderWindow* renderWindow, uint* depthBits, uint* stencilBits, uint* antialiasingLevel, uint* majorVersion, uint* minorVersion); 861 862 //Get the event on top of event queue of a render window, if any, and pop it 863 bool sfRenderWindow_pollEvent(sfRenderWindow* renderWindow, Event* event); 864 865 //Wait for an event and return it 866 bool sfRenderWindow_waitEvent(sfRenderWindow* renderWindow, Event* event); 867 868 //Get the position of a render window 869 void sfRenderWindow_getPosition(const sfRenderWindow* renderWindow, int* x, int* y); 870 871 //Change the position of a render window on screen 872 void sfRenderWindow_setPosition(sfRenderWindow* renderWindow, int x, int y); 873 874 //Get the size of the rendering region of a render window 875 void sfRenderWindow_getSize(const sfRenderWindow* renderWindow, uint* width, uint* height); 876 877 //Change the size of the rendering region of a render window 878 void sfRenderWindow_setSize(sfRenderWindow* renderWindow, int width, int height); 879 880 //Change the title of a render window 881 void sfRenderWindow_setTitle(sfRenderWindow* renderWindow, const(char)* title); 882 883 //Change the title of a render window (with a UTF-32 string) 884 void sfRenderWindow_setUnicodeTitle(sfRenderWindow* renderWindow, const(dchar)* title); 885 886 //Change a render window's icon 887 void sfRenderWindow_setIcon(sfRenderWindow* renderWindow, uint width, uint height, const ubyte* pixels); 888 889 //Show or hide a render window 890 void sfRenderWindow_setVisible(sfRenderWindow* renderWindow, bool visible); 891 892 //Show or hide the mouse cursor on a render window 893 void sfRenderWindow_setMouseCursorVisible(sfRenderWindow* renderWindow, bool show); 894 895 //Enable / disable vertical synchronization on a render window 896 void sfRenderWindow_setVerticalSyncEnabled(sfRenderWindow* renderWindow, bool enabled); 897 898 //Enable or disable automatic key-repeat for keydown events 899 void sfRenderWindow_setKeyRepeatEnabled(sfRenderWindow* renderWindow, bool enabled); 900 901 //Activate or deactivate a render window as the current target for rendering 902 bool sfRenderWindow_setActive(sfRenderWindow* renderWindow, bool active); 903 904 //Display a render window on screen 905 void sfRenderWindow_display(sfRenderWindow* renderWindow); 906 907 //Limit the framerate to a maximum fixed frequency for a render window 908 void sfRenderWindow_setFramerateLimit(sfRenderWindow* renderWindow, uint limit); 909 910 //Change the joystick threshold, ie. the value below which no move event will be generated 911 void sfRenderWindow_setJoystickThreshold(sfRenderWindow* renderWindow, float threshold); 912 913 //Retrieve the OS-specific handle of a render window 914 WindowHandle sfRenderWindow_getSystemHandle(const sfRenderWindow* renderWindow); 915 916 //Clear a render window with the given color 917 void sfRenderWindow_clear(sfRenderWindow* renderWindow, ubyte r, ubyte g, ubyte b, ubyte a); 918 919 //Change the current active view of a render window 920 void sfRenderWindow_setView(sfRenderWindow* renderWindow, const sfView* view); 921 922 //Get the current active view of a render window 923 sfView* sfRenderWindow_getView(const sfRenderWindow* renderWindow); 924 925 //Get the default view of a render window 926 sfView* sfRenderWindow_getDefaultView(const sfRenderWindow* renderWindow); 927 928 //Get the viewport of a view applied to this target 929 void sfRenderWindow_getViewport(const sfRenderWindow* renderWindow, const sfView* view, int* left, int* top, int* width, int* height); 930 931 //Convert a point from window coordinates to world coordinates 932 void sfRenderWindow_mapPixelToCoords(const sfRenderWindow* renderWindow, int xIn, int yIn, float* xOut, float* yOut, const sfView* targetView); 933 934 //Convert a point from world coordinates to window coordinates 935 void sfRenderWindow_mapCoordsToPixel(const sfRenderWindow* renderWindow, float xIn, float yIn, int* xOut, int* yOut, const sfView* targetView); 936 937 //Draw a drawable object to the render-target 938 //void sfRenderWindow_drawText(sfRenderWindow* renderWindow, const sfText* object, int blendMode,const float* transform, const sfTexture* texture, const sfShader* shader); 939 940 941 //Draw primitives defined by an array of vertices to a render window 942 void sfRenderWindow_drawPrimitives(sfRenderWindow* renderWindow,const (void)* vertices, uint vertexCount, int type, int blendMode,const(float)* transform, const(sfTexture)* texture, const(sfShader)* shader); 943 944 //Save the current OpenGL render states and matrices 945 void sfRenderWindow_pushGLStates(sfRenderWindow* renderWindow); 946 947 //Restore the previously saved OpenGL render states and matrices 948 void sfRenderWindow_popGLStates(sfRenderWindow* renderWindow); 949 950 //Reset the internal OpenGL states so that the target is ready for drawing 951 void sfRenderWindow_resetGLStates(sfRenderWindow* renderWindow); 952 953 //Copy the current contents of a render window to an image 954 sfImage* sfRenderWindow_capture(const sfRenderWindow* renderWindow); 955 956 //Get the current position of the mouse relatively to a render-window 957 void sfMouse_getPositionRenderWindow(const sfRenderWindow* relativeTo, int* x, int* y); 958 959 //Set the current position of the mouse relatively to a render-window 960 void sfMouse_setPositionRenderWindow(int x, int y, const sfRenderWindow* relativeTo); 961 962 const(char)* sfErr_getOutput();