aboutsummaryrefslogtreecommitdiffstats
path: root/pugl/detail/implementation.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-07-22 16:30:53 +0200
committerDavid Robillard <d@drobilla.net>2019-09-03 08:32:16 +0200
commite83c2b421d140244a6b9edb051b3e0d4aacda332 (patch)
tree398e49f43c96f4602496874fa7b3680236138720 /pugl/detail/implementation.c
parent5081d49f9f08596c07a8ed32430a4fa3e1baf352 (diff)
downloadpugl-e83c2b421d140244a6b9edb051b3e0d4aacda332.tar.gz
pugl-e83c2b421d140244a6b9edb051b3e0d4aacda332.tar.bz2
pugl-e83c2b421d140244a6b9edb051b3e0d4aacda332.zip
Add PuglWorld
The old API was broken for programs that manage multiple views, since it was impossible to wait for events on any view. There are also several functions in the API which are not actually associated with views at all, so those can now be moved to the more appropriate PuglWorld to make this more clear. The old puglInit() and puglDestroy() functions are preserved for compatibility, but marked as deprecated.
Diffstat (limited to 'pugl/detail/implementation.c')
-rw-r--r--pugl/detail/implementation.c70
1 files changed, 65 insertions, 5 deletions
diff --git a/pugl/detail/implementation.c b/pugl/detail/implementation.c
index 04b36d3..29daf15 100644
--- a/pugl/detail/implementation.c
+++ b/pugl/detail/implementation.c
@@ -46,27 +46,87 @@ puglSetDefaultHints(PuglHints hints)
PuglView*
puglInit(int* PUGL_UNUSED(pargc), char** PUGL_UNUSED(argv))
{
- PuglView* view = (PuglView*)calloc(1, sizeof(PuglView));
- if (!view) {
+ return puglNewView(puglNewWorld());
+}
+
+void
+puglDestroy(PuglView* const view)
+{
+ PuglWorld* const world = view->world;
+
+ puglFreeView(view);
+ puglFreeWorld(world);
+}
+
+PuglWorld*
+puglNewWorld(void)
+{
+ PuglWorld* world = (PuglWorld*)calloc(1, sizeof(PuglWorld));
+ if (!world || !(world->impl = puglInitWorldInternals())) {
+ free(world);
return NULL;
}
- PuglInternals* impl = puglInitInternals();
- if (!impl) {
+ return world;
+}
+
+void
+puglFreeWorld(PuglWorld* const world)
+{
+ puglFreeWorldInternals(world);
+ free(world->views);
+ free(world);
+}
+
+PuglView*
+puglNewView(PuglWorld* const world)
+{
+ PuglView* view = (PuglView*)calloc(1, sizeof(PuglView));
+ if (!view || !(view->impl = puglInitViewInternals())) {
free(view);
return NULL;
}
- view->impl = impl;
+ view->world = world;
view->width = 640;
view->height = 480;
view->start_time = puglGetTime(view);
puglSetDefaultHints(view->hints);
+
+ // Add to world view list
+ ++world->numViews;
+ world->views = (PuglView**)realloc(world->views,
+ world->numViews * sizeof(PuglView*));
+ world->views[world->numViews - 1] = view;
+
return view;
}
void
+puglFreeView(PuglView* view)
+{
+ // Remove from world view list
+ PuglWorld* world = view->world;
+ for (size_t i = 0; i < world->numViews; ++i) {
+ if (world->views[i] == view) {
+ if (i == world->numViews - 1) {
+ world->views[i] = NULL;
+ } else {
+ memmove(world->views + i, world->views + i + 1,
+ sizeof(PuglView*) * (world->numViews - i - 1));
+ world->views[world->numViews - 1] = NULL;
+ }
+ --world->numViews;
+ }
+ }
+
+ puglFreeViewInternals(view);
+ free(view->windowClass);
+ free(view);
+}
+
+void
puglInitWindowHint(PuglView* view, PuglWindowHint hint, int value)
{
if (hint < PUGL_NUM_WINDOW_HINTS) {