loaders: Make use of edhanced debug messages.
[gfxprim.git] / libs / backends / GP_BackendVirtual.c
blobb28c3cbb600147018974d2e3e039763a549d94d1
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
23 #include <string.h>
25 #include "core/GP_Context.h"
26 #include "core/GP_Blit.h"
27 #include "core/GP_Debug.h"
29 #include "GP_BackendVirtual.h"
31 struct virt_priv {
32 /* Original backend */
33 GP_Backend *backend;
35 int flags;
38 static void virt_flip(GP_Backend *self)
40 struct virt_priv *virt = GP_BACKEND_PRIV(self);
42 /* Convert and copy the buffer */
43 GP_Blit(self->context, 0, 0, self->context->w, self->context->h,
44 virt->backend->context, 0, 0);
46 /* Call blit on original backend */
47 virt->backend->Flip(virt->backend);
50 static void virt_update_rect(GP_Backend *self, GP_Coord x0, GP_Coord y0,
51 GP_Coord x1, GP_Coord y1)
53 struct virt_priv *virt = GP_BACKEND_PRIV(self);
55 /* Convert and copy the buffer */
56 GP_BlitXYXY(self->context, x0, y0, x1, y1,
57 virt->backend->context, x0, y0);
59 /* Call blit on original backend */
60 virt->backend->UpdateRect(virt->backend, x0, y0, x1, y1);
63 static int virt_set_attributes(struct GP_Backend *self,
64 uint32_t w, uint32_t h,
65 const char *caption)
67 struct virt_priv *virt = GP_BACKEND_PRIV(self);
68 int ret;
70 ret = virt->backend->SetAttributes(virt->backend, w, h, caption);
72 if (ret)
73 return ret;
75 /* If backend was resized, update our buffer as well */
76 if (h != 0 && w != 0)
77 GP_ContextResize(self->context, w, h);
79 return 0;
82 static void virt_poll(GP_Backend *self)
84 struct virt_priv *virt = GP_BACKEND_PRIV(self);
86 virt->backend->Poll(virt->backend);
89 static void virt_exit(GP_Backend *self)
91 struct virt_priv *virt = GP_BACKEND_PRIV(self);
93 GP_ContextFree(self->context);
95 if (virt->flags & GP_BACKEND_CALL_EXIT)
96 virt->backend->Exit(virt->backend);
98 free(self);
101 GP_Backend *GP_BackendVirtualInit(GP_Backend *backend,
102 GP_PixelType pixel_type, int flags)
104 GP_Backend *self;
105 struct virt_priv *virt;
107 self = malloc(sizeof(GP_Backend) +
108 sizeof(struct virt_priv));
110 if (self == NULL) {
111 GP_DEBUG(1, "Malloc failed :(");
112 return NULL;
115 memset(self, 0, sizeof(GP_Backend));
117 /* Create new buffer with different context type */
118 self->context = GP_ContextAlloc(backend->context->w, backend->context->h,
119 pixel_type);
121 if (self->context == NULL)
122 goto err0;
124 virt = GP_BACKEND_PRIV(self);
125 virt->backend = backend;
126 virt->flags = flags;
128 /* Initalize new backend */
129 self->name = "Virtual Backend";
130 self->Flip = virt_flip;
131 self->UpdateRect = virt_update_rect;
132 self->Exit = virt_exit;
134 if (backend->Poll)
135 self->Poll = virt_poll;
137 if (backend->SetAttributes)
138 self->SetAttributes = virt_set_attributes;
140 return self;
142 err0:
143 free(self);
144 return NULL;