d3dxof: Fix file types checks + tests.
[wine] / dlls / d3dx9_36 / core.c
1 /*
2  *
3  * Copyright 2002 Raphael Junqueira
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include <stdarg.h>
21
22 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
28
29 #include "d3dx9_36_private.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
32
33 /* ID3DXBuffer IUnknown parts follow: */
34 static HRESULT WINAPI ID3DXBufferImpl_QueryInterface(LPD3DXBUFFER iface, REFIID riid, LPVOID* ppobj)
35 {
36     ID3DXBufferImpl *This = (ID3DXBufferImpl *)iface;
37
38     if (IsEqualGUID(riid, &IID_IUnknown)
39       || IsEqualGUID(riid, &IID_ID3DXBuffer))
40     {
41         IUnknown_AddRef(iface);
42         *ppobj = This;
43         return D3D_OK;
44     }
45
46     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
47     return E_NOINTERFACE;
48 }
49
50 static ULONG WINAPI ID3DXBufferImpl_AddRef(LPD3DXBUFFER iface)
51 {
52     ID3DXBufferImpl *This = (ID3DXBufferImpl *)iface;
53     ULONG ref = InterlockedIncrement(&This->ref);
54
55     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
56
57     return ref;
58 }
59
60 static ULONG WINAPI ID3DXBufferImpl_Release(LPD3DXBUFFER iface)
61 {
62     ID3DXBufferImpl *This = (ID3DXBufferImpl *)iface;
63     ULONG ref = InterlockedDecrement(&This->ref);
64
65     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
66
67     if (ref == 0)
68     {
69         HeapFree(GetProcessHeap(), 0, This->buffer);
70         HeapFree(GetProcessHeap(), 0, This);
71     }
72     return ref;
73 }
74
75 /* ID3DXBuffer Interface follow: */
76 static LPVOID WINAPI ID3DXBufferImpl_GetBufferPointer(LPD3DXBUFFER iface)
77 {
78     ID3DXBufferImpl *This = (ID3DXBufferImpl *)iface;
79     return This->buffer;
80 }
81
82 static DWORD WINAPI ID3DXBufferImpl_GetBufferSize(LPD3DXBUFFER iface)
83 {
84     ID3DXBufferImpl *This = (ID3DXBufferImpl *)iface;
85     return This->bufferSize;
86 }
87
88 const ID3DXBufferVtbl D3DXBuffer_Vtbl =
89 {
90     ID3DXBufferImpl_QueryInterface,
91     ID3DXBufferImpl_AddRef,
92     ID3DXBufferImpl_Release,
93     ID3DXBufferImpl_GetBufferPointer,
94     ID3DXBufferImpl_GetBufferSize
95 };
96
97 HRESULT WINAPI D3DXCreateBuffer(DWORD NumBytes, LPD3DXBUFFER* ppBuffer)
98 {
99     ID3DXBufferImpl *object;
100
101     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3DXBufferImpl));
102     if (object == NULL)
103     {
104         *ppBuffer = NULL;
105         return E_OUTOFMEMORY;
106     }
107     object->lpVtbl = &D3DXBuffer_Vtbl;
108     object->ref = 1;
109     object->bufferSize = NumBytes;
110     object->buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, NumBytes);
111     if (object->buffer == NULL)
112     {
113         HeapFree(GetProcessHeap(), 0, object);
114         *ppBuffer = NULL;
115         return E_OUTOFMEMORY;
116     }
117
118     *ppBuffer = (LPD3DXBUFFER)object;
119     return D3D_OK;
120 }