source: trunk/src/gpu/indexbuffer.cpp @ 2770

Last change on this file since 2770 was 2770, checked in by sam, 9 years ago

gpu: get rid of the global D3D device pointer and move D3D initialisation
code from the Video to the Renderer class.

  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "core.h"
16#include "lolgl.h"
17
18#if defined _WIN32 && defined USE_D3D9
19#   define FAR
20#   define NEAR
21#   include <d3d9.h>
22#endif
23
24using namespace std;
25
26namespace lol
27{
28
29//
30// The IndexBufferData class
31// -------------------------
32//
33
34class IndexBufferData
35{
36    friend class IndexBuffer;
37
38    size_t m_size;
39
40#if defined USE_D3D9
41    IDirect3DDevice9 *m_dev;
42    IDirect3DIndexBuffer9 *m_ibo;
43#elif defined _XBOX
44    D3DDevice9 *m_dev;
45    D3DIndexBuffer *m_ibo;
46#else
47    GLuint m_ibo;
48    uint8_t *m_memory;
49#endif
50};
51
52//
53// The IndexBuffer class
54// ----------------------
55//
56
57IndexBuffer::IndexBuffer(size_t size)
58  : m_data(new IndexBufferData)
59{
60    m_data->m_size = size;
61    if (!size)
62        return;
63#if defined USE_D3D9 || defined _XBOX
64#   if defined USE_D3D9
65    m_data->m_dev = (IDirect3DDevice9 *)g_renderer->GetDevice();
66#   elif defined _XBOX
67    m_data->m_dev = (D3DDevice9 *)g_renderer->GetDevice();
68#   endif
69
70    if (FAILED(m_data->m_dev->CreateIndexBuffer(size, D3DUSAGE_WRITEONLY,
71                                              D3DFMT_INDEX16, D3DPOOL_MANAGED,
72                                              &m_data->m_ibo, nullptr)))
73        Abort();
74#else
75    glGenBuffers(1, &m_data->m_ibo);
76    m_data->m_memory = new uint8_t[size];
77#endif
78}
79
80IndexBuffer::~IndexBuffer()
81{
82    if (m_data->m_size)
83    {
84#if defined USE_D3D9 || defined _XBOX
85        if (FAILED(m_data->m_ibo->Release()))
86            Abort();
87#else
88        glDeleteBuffers(1, &m_data->m_ibo);
89        delete[] m_data->m_memory;
90#endif
91    }
92    delete m_data;
93}
94
95void *IndexBuffer::Lock(size_t offset, size_t size)
96{
97    if (!m_data->m_size)
98        return nullptr;
99
100#if defined USE_D3D9 || defined _XBOX
101    void *ret;
102    if (FAILED(m_data->m_ibo->Lock(offset, size, (void **)&ret, 0)))
103        Abort();
104    return ret;
105#else
106    return m_data->m_memory + offset;
107#endif
108}
109
110void IndexBuffer::Unlock()
111{
112    if (!m_data->m_size)
113        return;
114
115#if defined USE_D3D9 || defined _XBOX
116    if (FAILED(m_data->m_ibo->Unlock()))
117        Abort();
118#else
119    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_data->m_ibo);
120    glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_data->m_size, m_data->m_memory,
121                 GL_STATIC_DRAW);
122#endif
123}
124
125void IndexBuffer::Bind()
126{
127    if (!m_data->m_size)
128        return;
129
130#if defined USE_D3D9 || defined _XBOX
131    if (FAILED(m_data->m_dev->SetIndices(m_data->m_ibo)))
132        Abort();
133#else
134    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_data->m_ibo);
135    /* XXX: not necessary because we kept track of the size */
136    //int size;
137    //glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
138#endif
139}
140
141void IndexBuffer::Unbind()
142{
143    if (!m_data->m_size)
144        return;
145
146#if defined USE_D3D9 || defined _XBOX
147    if (FAILED(m_data->m_dev->SetIndices(nullptr)))
148        Abort();
149#else
150    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
151#endif
152}
153
154} /* namespace lol */
155
Note: See TracBrowser for help on using the repository browser.