[AD] public state store/restore API

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


We briefly talked about this in #allegro-dev, so I made a patch. It's
simply two functions:

void al_store_state(ALLEGRO_STATE *state, int flags);
void al_restore_state(ALLEGRO_STATE *state, int flags);

The intended use would be something like for example:

void my_blended_draw(...)
{
    ALLEGRO_STATE state;
    al_store_state(&state, ALLEGRO_STATE_BLENDER);

    al_set_blender(...);

    al_restore_state(&state, ALLEGRO_STATE_BLENDER);
}

But then I found that only one single example currently would have use
for it - so not sure now we really need it. And also currently, the
above would look like:

void my_blended_draw(...)
{
    int src, dst;
    ALLEGRO_COLOR color;
    al_get_blender(&src, &dst, &color);

    al_set_blender(...);

    al_set_blender(src, dst, color);
}

So not terribly long either. What does everyone think?

-- 
Elias Pschernig <elias@xxxxxxxxxx>
Index: src/tls.c
===================================================================
--- src/tls.c	(revision 10665)
+++ src/tls.c	(working copy)
@@ -592,6 +592,88 @@
 
 
 
+/* Function: al_store_state
+ * 
+ * Stores part of the state of the current thread in the given state object.
+ */
+#define _STORE(x) state->x = tls->x;
+void al_store_state(ALLEGRO_STATE *state, int flags)
+{
+   thread_local_state *tls;
+   if ((tls = tls_get()) == NULL)
+      return;
+
+   if (flags & ALLEGRO_STATE_NEW_DISPLAY_PARAMETERS) {
+      _STORE(new_display_format);
+      _STORE(new_display_refresh_rate);
+      _STORE(new_display_flags);
+   }
+   
+   if (flags & ALLEGRO_STATE_NEW_BITMAP_PARAMETERS) {
+      _STORE(new_bitmap_format);
+      _STORE(new_bitmap_flags);
+   }
+   
+   if (flags & ALLEGRO_STATE_DISPLAY) {
+      _STORE(current_display);
+   }
+   
+   if (flags & ALLEGRO_STATE_TARGET_BITMAP) {
+      _STORE(target_bitmap);
+   }
+   
+   if (flags & ALLEGRO_STATE_BLENDER) {
+      _STORE(blend_source);
+      _STORE(blend_dest);
+      _STORE(blend_color);
+      _STORE(memory_blender);
+   }
+};
+#undef _STORE
+
+
+
+/* Function: al_restore_state
+ * 
+ * Restores part of the state of the current thread from the given state object.
+ */
+#define _STORE(x) tls->x = state->x;
+void al_restore_state(ALLEGRO_STATE const *state, int flags)
+{
+   thread_local_state *tls;
+   if ((tls = tls_get()) == NULL)
+      return;
+
+   if (flags & ALLEGRO_STATE_NEW_DISPLAY_PARAMETERS) {
+      _STORE(new_display_format);
+      _STORE(new_display_refresh_rate);
+      _STORE(new_display_flags);
+   }
+   
+   if (flags & ALLEGRO_STATE_NEW_BITMAP_PARAMETERS) {
+      _STORE(new_bitmap_format);
+      _STORE(new_bitmap_flags);
+   }
+   
+   if (flags & ALLEGRO_STATE_DISPLAY) {
+      _STORE(current_display);
+   }
+   
+   if (flags & ALLEGRO_STATE_TARGET_BITMAP) {
+      _STORE(target_bitmap);
+   }
+   
+   if (flags & ALLEGRO_STATE_BLENDER) {
+      _STORE(blend_source);
+      _STORE(blend_dest);
+      _STORE(blend_color);
+      _STORE(memory_blender);
+   }
+};
+#undef _STORE
+
+
+
 /* Function: al_set_blender
  *
  * Sets the function to use for blending for the current thread.
Index: include/allegro5/base.h
===================================================================
--- include/allegro5/base.h	(revision 10665)
+++ include/allegro5/base.h	(working copy)
@@ -97,8 +97,6 @@
    int autodetect;                  /* set to allow autodetection */
 } _DRIVER_INFO;
 
-       
-
 AL_END_EXTERN_C
 
 #endif          /* ifndef ALLEGRO_BASE_H */
Index: include/allegro5/allegro.h
===================================================================
--- include/allegro5/allegro.h	(revision 10665)
+++ include/allegro5/allegro.h	(working copy)
@@ -48,6 +48,8 @@
 #include "allegro5/bitmap_new.h"
 #include "allegro5/color.h"
 
+#include "allegro5/tls.h"
+
 #include "allegro5/file.h"
 #include "allegro5/lzss.h"
 
Index: include/allegro5/tls.h
===================================================================
--- include/allegro5/tls.h	(revision 0)
+++ include/allegro5/tls.h	(revision 0)
@@ -0,0 +1,70 @@
+/*         ______   ___    ___
+ *        /\  _  \ /\_ \  /\_ \
+ *        \ \ \L\ \\//\ \ \//\ \      __     __   _ __   ___
+ *         \ \  __ \ \ \ \  \ \ \   /'__`\ /'_ `\/\`'__\/ __`\
+ *          \ \ \/\ \ \_\ \_ \_\ \_/\  __//\ \L\ \ \ \//\ \L\ \
+ *           \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
+ *            \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
+ *                                           /\____/
+ *                                           \_/__/
+ *
+ *      Timer routines.
+ *
+ *      See readme.txt for copyright information.
+ */
+
+/* Title: Timer routines
+ */
+
+#ifndef ALLEGRO_TLS_H
+#define ALLEGRO_TLS_H
+
+#include "allegro5/base.h"
+
+AL_BEGIN_EXTERN_C
+
+/* Enum: ALLEGRO_STATE
+ * 
+ * Flags which can be passed to al_store_state/al_restore_state.
+ * 
+ * ALLEGRO_STATE_NEW_DISPLAY_PARAMETERS - new_display_format,
+ *                                        new_display_refresh_rate,
+ *                                        new_display_flags
+ * ALLEGRO_STATE_NEW_BITMAP_PARAMETERS  - new_bitmap_format,
+ *                                        new_bitmap_flags
+ * ALLEGRO_STATE_DISPLAY                - current_display
+ * ALLEGRO_STATE_TARGET_BITMAP          - target_bitmap 
+ * ALLEGRO_STATE_BLENDER                - blender
+ */
+#define ALLEGRO_STATE_NEW_DISPLAY_PARAMETERS 0x0001
+#define ALLEGRO_STATE_NEW_BITMAP_PARAMETERS  0x0002
+#define ALLEGRO_STATE_DISPLAY                0x0004
+#define ALLEGRO_STATE_TARGET_BITMAP          0x0008
+#define ALLEGRO_STATE_BLENDER                0x0010
+
+/* Type: ALLEGRO_STATE
+ * 
+ * Opaque type which is passed to al_store_state/al_restore_state.
+ */
+/* FIXME: This should all be internal - what's a good way to do that in C? */
+typedef struct ALLEGRO_STATE
+{
+   int new_display_format;
+   int new_display_refresh_rate;
+   int new_display_flags;
+   int new_bitmap_format;
+   int new_bitmap_flags;
+   ALLEGRO_DISPLAY *current_display;
+   ALLEGRO_BITMAP *target_bitmap;
+   int blend_source;
+   int blend_dest;
+   ALLEGRO_COLOR blend_color;
+   void *memory_blender;
+} ALLEGRO_STATE;
+
+AL_FUNC(void, al_store_state, (ALLEGRO_STATE *state, int flags));
+AL_FUNC(void, al_restore_state, (ALLEGRO_STATE const *state, int flags));
+
+AL_END_EXTERN_C
+
+#endif /* ifndef ALLEGRO_TLS_H */


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/