1 /*
2  * DSFML - The Simple and Fast Multimedia Library for D
3  *
4  * Copyright (c) 2013 - 2017 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
8  * use of this software.
9  *
10  * Permission is granted to anyone to use this software for any purpose,
11  * including commercial applications, and to alter it and redistribute it
12  * freely, subject to the following restrictions:
13  *
14  * 1. The origin of this software must not be misrepresented; you must not claim
15  * that you wrote the original software. If you use this software in a product,
16  * an acknowledgment in the product documentation would be appreciated but is
17  * not required.
18  *
19  * 2. Altered source versions must be plainly marked as such, and must not be
20  * misrepresented as being the original software.
21  *
22  * 3. This notice may not be removed or altered from any source distribution
23  */
24 
25 /**
26  * If you need to make OpenGL calls without having an active window (like in a
27  * thread), you can use an instance of this class to get a valid context.
28  *
29  * Having a valid context is necessary for $(I every) OpenGL call.
30  *
31  * Note that a context is only active in its current thread, if you create a new
32  * thread it will have no valid context by default.
33  *
34  * To use a $(U Context) instance, just construct it and let it live as long as you
35  * need a valid context. No explicit activation is needed, all it has to do is
36  * to exist. Its destructor will take care of deactivating and freeing all the
37  * attached resources.
38  *
39  * Example:
40  * ---
41  * void threadFunction()
42  * {
43  *    Context context = new Context();
44  *    // from now on, you have a valid context
45  *
46  *    // you can make OpenGL calls
47  *    glClear(GL_DEPTH_BUFFER_BIT);
48  * }
49  * // the context is automatically deactivated and destroyed by the
50  * // Context destructor when the class is collected by the GC
51  * ---
52  */
53 module dsfml.window.context;
54 
55 alias GlFunctionPointer = void*;
56 
57 /**
58  * Class holding a valid drawing context.
59  */
60 class Context
61 {
62     package sfContext* sfPtr;
63 
64     /**
65      * Default constructor.
66      *
67      * The constructor creates and activates the context.
68      */
69     this()
70     {
71         sfPtr = sfContext_create();
72     }
73 
74     /// Destructor.
75     ~this()
76     {
77         import dsfml.system.config;
78         mixin(destructorOutput);
79         sfContext_destroy(sfPtr);
80     }
81 
82     /**
83      * Activate or deactivate explicitely the context.
84      *
85      * Params:
86      * 	active = true to activate, false to deactivate
87      *
88      * Returns: true on success, false on failure.
89      */
90     void setActive(bool active)
91     {
92         sfContext_setActive(sfPtr,active);
93     }
94 }
95 
96 package extern(C)
97 {
98     struct sfContext;
99 }
100 private extern(C)
101 {
102     sfContext* sfContext_create();
103     void sfContext_destroy(sfContext* context);
104     void sfContext_setActive(sfContext* context, bool active);
105 }