1 /*
2  * DSFML - The Simple and Fast Multimedia Library for D
3  *
4  * Copyright (c) 2013 - 2018 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  * DSFML is based on SFML (Copyright Laurent Gomila)
26  */
27 
28 /**
29  * If you need to make OpenGL calls without having an active window (like in a
30  * thread), you can use an instance of this class to get a valid context.
31  *
32  * Having a valid context is necessary for $(I every) OpenGL call.
33  *
34  * Note that a context is only active in its current thread, if you create a new
35  * thread it will have no valid context by default.
36  *
37  * To use a $(U Context) instance, just construct it and let it live as long as you
38  * need a valid context. No explicit activation is needed, all it has to do is
39  * to exist. Its destructor will take care of deactivating and freeing all the
40  * attached resources.
41  *
42  * Example:
43  * ---
44  * void threadFunction()
45  * {
46  *    Context context = new Context();
47  *    // from now on, you have a valid context
48  *
49  *    // you can make OpenGL calls
50  *    glClear(GL_DEPTH_BUFFER_BIT);
51  * }
52  * // the context is automatically deactivated and destroyed by the
53  * // Context destructor when the class is collected by the GC
54  * ---
55  */
56 module dsfml.window.context;
57 
58 alias GlFunctionPointer = void*;
59 
60 /**
61  * Class holding a valid drawing context.
62  */
63 class Context
64 {
65     package sfContext* sfPtr;
66 
67     /**
68      * Default constructor.
69      *
70      * The constructor creates and activates the context.
71      */
72     this()
73     {
74         sfPtr = sfContext_create();
75     }
76 
77     /// Destructor.
78     ~this()
79     {
80         import dsfml.system.config;
81         mixin(destructorOutput);
82         sfContext_destroy(sfPtr);
83     }
84 
85     /**
86      * Activate or deactivate explicitely the context.
87      *
88      * Params:
89      * 	active = true to activate, false to deactivate
90      *
91      * Returns: true on success, false on failure.
92      */
93     void setActive(bool active)
94     {
95         sfContext_setActive(sfPtr,active);
96     }
97 }
98 
99 package extern(C)
100 {
101     struct sfContext;
102 }
103 private extern(C)
104 {
105     sfContext* sfContext_create();
106     void sfContext_destroy(sfContext* context);
107     void sfContext_setActive(sfContext* context, bool active);
108 }