line.c 9.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright 2010 Christian Costa
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 *
 */

#include "wine/debug.h"
#include "d3dx9_36_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(d3dx);

static const struct ID3DXLineVtbl ID3DXLine_Vtbl;

typedef struct ID3DXLineImpl {
28
    ID3DXLine ID3DXLine_iface;
29
    LONG ref;
30 31

    IDirect3DDevice9 *device;
32
    IDirect3DStateBlock9 *state;
33 34
} ID3DXLineImpl;

35 36 37 38 39
static inline ID3DXLineImpl *impl_from_ID3DXLine(ID3DXLine *iface)
{
    return CONTAINING_RECORD(iface, ID3DXLineImpl, ID3DXLine_iface);
}

40 41 42
/*** IUnknown methods ***/
static HRESULT WINAPI ID3DXLineImpl_QueryInterface(ID3DXLine* iface, REFIID riid, LPVOID* object)
{
43
    TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), object);
44 45 46 47 48

    if (IsEqualGUID(riid, &IID_IUnknown) ||
        IsEqualGUID(riid, &IID_ID3DXLine))
    {
        ID3DXLine_AddRef(iface);
49
        *object = iface;
50 51 52 53 54 55 56 57 58 59
        return S_OK;
    }

    ERR("Interface %s not found\n", debugstr_guid(riid));

    return E_NOINTERFACE;
}

static ULONG WINAPI ID3DXLineImpl_AddRef(ID3DXLine* iface)
{
60
    ID3DXLineImpl *This = impl_from_ID3DXLine(iface);
61 62 63 64 65 66 67 68

    TRACE("(%p)->(): AddRef from %u\n", This, This->ref);

    return InterlockedIncrement(&This->ref);
}

static ULONG WINAPI ID3DXLineImpl_Release(ID3DXLine* iface)
{
69
    ID3DXLineImpl *This = impl_from_ID3DXLine(iface);
70 71 72 73 74
    ULONG ref = InterlockedDecrement(&This->ref);

    TRACE("(%p)->(): Release from %u\n", This, ref + 1);

    if (!ref)
75 76
    {
        IDirect3DDevice9_Release(This->device);
77
        HeapFree(GetProcessHeap(), 0, This);
78
    }
79 80 81 82 83 84 85

    return ref;
}

/*** ID3DXLine methods ***/
static HRESULT WINAPI ID3DXLineImpl_GetDevice(ID3DXLine* iface, LPDIRECT3DDEVICE9* device)
{
86
    ID3DXLineImpl *This = impl_from_ID3DXLine(iface);
87

88
    TRACE ("(%p)->(%p): relay\n", This, device);
89

90 91 92 93 94 95
    if (device == NULL) return D3DERR_INVALIDCALL;

    *device = This->device;
    IDirect3DDevice9_AddRef(This->device);

    return D3D_OK;
96 97 98 99
}

static HRESULT WINAPI ID3DXLineImpl_Begin(ID3DXLine* iface)
{
100
    ID3DXLineImpl *This = impl_from_ID3DXLine(iface);
101

102 103 104
    HRESULT hr;
    D3DXMATRIX identity, projection;
    D3DVIEWPORT9 vp;
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    TRACE ("(%p)->()\n", This);

    if (This->state != NULL) /* We already began. Return error. */
        return D3DERR_INVALIDCALL;

    hr = IDirect3DDevice9_CreateStateBlock(This->device, D3DSBT_ALL, &This->state);
    if (FAILED(hr)) return D3DXERR_INVALIDDATA;

    hr = IDirect3DDevice9_GetViewport(This->device, &vp);
    if (FAILED(hr)) goto failed;

    D3DXMatrixIdentity(&identity);
    D3DXMatrixOrthoOffCenterLH(&projection,
                               0.0, vp.Width, /* Min and max x */
                               vp.Height, 0.0, /* Min and max y. Screen y is on top so this is inverted */
                               0.0, 1.0); /* Min and max z */

    hr = IDirect3DDevice9_SetTransform(This->device, D3DTS_WORLD, &identity);
    if (FAILED(hr)) goto failed;

    hr = IDirect3DDevice9_SetTransform(This->device, D3DTS_VIEW, &identity);
    if (FAILED(hr)) goto failed;

    hr = IDirect3DDevice9_SetTransform(This->device, D3DTS_PROJECTION, &projection);
    if (FAILED(hr)) goto failed;

    /* Windows sets similar states so we do the same */

    hr = IDirect3DDevice9_SetRenderState(This->device, D3DRS_LIGHTING, FALSE);
    if (FAILED(hr)) goto failed;

    hr = IDirect3DDevice9_SetRenderState(This->device, D3DRS_FOGENABLE, FALSE);
    if (FAILED(hr)) goto failed;

    hr = IDirect3DDevice9_SetRenderState(This->device, D3DRS_SHADEMODE, D3DSHADE_FLAT);
    if (FAILED(hr)) goto failed;

    hr = IDirect3DDevice9_SetRenderState(This->device, D3DRS_ALPHABLENDENABLE, TRUE);
    if (FAILED(hr)) goto failed;

    hr = IDirect3DDevice9_SetRenderState(This->device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
    if (FAILED(hr)) goto failed;

    hr = IDirect3DDevice9_SetRenderState(This->device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
    if (FAILED(hr)) goto failed;

    return D3D_OK;
failed:
    IDirect3DStateBlock9_Apply(This->state);
    IDirect3DStateBlock9_Release(This->state);
    This->state = NULL;
    return D3DXERR_INVALIDDATA;
158 159 160 161
}

static HRESULT WINAPI ID3DXLineImpl_Draw(ID3DXLine* iface, CONST D3DXVECTOR2* vertexlist, DWORD vertexlistcount, D3DCOLOR color)
{
162
    ID3DXLineImpl *This = impl_from_ID3DXLine(iface);
163 164 165 166 167 168 169 170 171

    FIXME("(%p)->(%p, %u, %#x): stub\n", This, vertexlist, vertexlistcount, color);

    return E_NOTIMPL;
}

static HRESULT WINAPI ID3DXLineImpl_DrawTransform(ID3DXLine* iface, CONST D3DXVECTOR3* vertexlist, DWORD vertexlistcount,
                                                  CONST D3DXMATRIX* transform, D3DCOLOR color)
{
172
    ID3DXLineImpl *This = impl_from_ID3DXLine(iface);
173 174 175 176 177 178 179 180

    FIXME("(%p)->(%p, %u, %p, %#x): stub\n", This, vertexlist, vertexlistcount, transform, color);

    return E_NOTIMPL;
}

static HRESULT WINAPI ID3DXLineImpl_SetPattern(ID3DXLine* iface, DWORD pattern)
{
181
    ID3DXLineImpl *This = impl_from_ID3DXLine(iface);
182 183 184 185 186 187 188 189

    FIXME("(%p)->(%#x): stub\n", This, pattern);

    return E_NOTIMPL;
}

static DWORD WINAPI ID3DXLineImpl_GetPattern(ID3DXLine* iface)
{
190
    ID3DXLineImpl *This = impl_from_ID3DXLine(iface);
191 192 193 194 195 196 197 198

    FIXME("(%p)->(): stub\n", This);

    return 0xFFFFFFFF;
}

static HRESULT WINAPI ID3DXLineImpl_SetPatternScale(ID3DXLine* iface, FLOAT scale)
{
199
    ID3DXLineImpl *This = impl_from_ID3DXLine(iface);
200 201 202 203 204 205 206 207

    FIXME("(%p)->(%f): stub\n", This, scale);

    return E_NOTIMPL;
}

static FLOAT WINAPI ID3DXLineImpl_GetPatternScale(ID3DXLine* iface)
{
208
    ID3DXLineImpl *This = impl_from_ID3DXLine(iface);
209 210 211 212 213 214 215 216

    FIXME("(%p)->(): stub\n", This);

    return 1.0f;
}

static HRESULT WINAPI ID3DXLineImpl_SetWidth(ID3DXLine* iface, FLOAT width)
{
217
    ID3DXLineImpl *This = impl_from_ID3DXLine(iface);
218 219 220 221 222 223 224 225

    FIXME("(%p)->(%f): stub\n", This, width);

    return E_NOTIMPL;
}

static FLOAT WINAPI ID3DXLineImpl_GetWidth(ID3DXLine* iface)
{
226
    ID3DXLineImpl *This = impl_from_ID3DXLine(iface);
227 228 229 230 231 232 233 234

    FIXME("(%p)->(): stub\n", This);

    return 1.0f;
}

static HRESULT WINAPI ID3DXLineImpl_SetAntialias(ID3DXLine* iface, BOOL antialias)
{
235
    ID3DXLineImpl *This = impl_from_ID3DXLine(iface);
236 237 238 239 240 241 242 243

    FIXME("(%p)->(%u): stub\n", This, antialias);

    return E_NOTIMPL;
}

static BOOL WINAPI ID3DXLineImpl_GetAntialias(ID3DXLine* iface)
{
244
    ID3DXLineImpl *This = impl_from_ID3DXLine(iface);
245 246 247 248 249 250 251 252

    FIXME("(%p)->(): stub\n", This);

    return FALSE;
}

static HRESULT WINAPI ID3DXLineImpl_SetGLLines(ID3DXLine* iface, BOOL gl_lines)
{
253
    ID3DXLineImpl *This = impl_from_ID3DXLine(iface);
254 255 256 257 258 259 260 261

    FIXME("(%p)->(%u): stub\n", This, gl_lines);

    return E_NOTIMPL;
}

static BOOL WINAPI ID3DXLineImpl_GetGLLines(ID3DXLine* iface)
{
262
    ID3DXLineImpl *This = impl_from_ID3DXLine(iface);
263 264 265 266 267 268 269 270

    FIXME("(%p)->(): stub\n", This);

    return FALSE;
}

static HRESULT WINAPI ID3DXLineImpl_End(ID3DXLine* iface)
{
271
    ID3DXLineImpl *This = impl_from_ID3DXLine(iface);
272

273
    HRESULT hr;
274

275 276 277 278 279 280 281 282 283 284 285
    TRACE ("(%p)->()\n", This);

    if (This->state == NULL) /* We haven't begun yet. */
        return D3DERR_INVALIDCALL;

    hr = IDirect3DStateBlock9_Apply(This->state);
    IDirect3DStateBlock9_Release(This->state);
    This->state = NULL;

    if (FAILED(hr)) return D3DXERR_INVALIDDATA;
    else return D3D_OK;
286 287 288 289
}

static HRESULT WINAPI ID3DXLineImpl_OnLostDevice(ID3DXLine* iface)
{
290
    ID3DXLineImpl *This = impl_from_ID3DXLine(iface);
291 292 293 294 295 296 297

    FIXME("(%p)->(): stub\n", This);

    return E_NOTIMPL;
}
static HRESULT WINAPI ID3DXLineImpl_OnResetDevice(ID3DXLine* iface)
{
298
    ID3DXLineImpl *This = impl_from_ID3DXLine(iface);
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

    FIXME("(%p)->(): stub\n", This);

    return E_NOTIMPL;
}

static const struct ID3DXLineVtbl ID3DXLine_Vtbl =
{
    /*** IUnknown methods ***/
    ID3DXLineImpl_QueryInterface,
    ID3DXLineImpl_AddRef,
    ID3DXLineImpl_Release,
    /*** ID3DXLine methods ***/
    ID3DXLineImpl_GetDevice,
    ID3DXLineImpl_Begin,
    ID3DXLineImpl_Draw,
    ID3DXLineImpl_DrawTransform,
    ID3DXLineImpl_SetPattern,
    ID3DXLineImpl_GetPattern,
    ID3DXLineImpl_SetPatternScale,
    ID3DXLineImpl_GetPatternScale,
    ID3DXLineImpl_SetWidth,
    ID3DXLineImpl_GetWidth,
    ID3DXLineImpl_SetAntialias,
    ID3DXLineImpl_GetAntialias,
    ID3DXLineImpl_SetGLLines,
    ID3DXLineImpl_GetGLLines,
    ID3DXLineImpl_End,
    ID3DXLineImpl_OnLostDevice,
    ID3DXLineImpl_OnResetDevice
};

HRESULT WINAPI D3DXCreateLine(LPDIRECT3DDEVICE9 device, LPD3DXLINE* line)
{
    ID3DXLineImpl* object;

    TRACE("(%p, %p)\n", device, line);

    if (!device || !line)
        return D3DERR_INVALIDCALL;

    object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3DXLineImpl));
    if (!object)
    {
        ERR("Out of memory\n");
        return E_OUTOFMEMORY;
    }

347
    object->ID3DXLine_iface.lpVtbl = &ID3DXLine_Vtbl;
348
    object->ref = 1;
349
    object->device = device;
350
    object->state = NULL; /* We only initialize it on Begin */
351
    IDirect3DDevice9_AddRef(device);
352

353
    *line = &object->ID3DXLine_iface;
354 355 356

    return D3D_OK;
}