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

Last change on this file since 2183 was 2183, checked in by sam, 7 years ago

build: fix the WTFPL site URL in all code comments.

  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 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
26#if defined USE_D3D9
27extern IDirect3DDevice9 *g_d3ddevice;
28#elif defined _XBOX
29extern D3DDevice *g_d3ddevice;
30#endif
31
32namespace lol
33{
34
35//
36// The IndexBufferData class
37// -------------------------
38//
39
40class IndexBufferData
41{
42    friend class IndexBuffer;
43
44    size_t m_size;
45
46#if defined USE_D3D9
47    IDirect3DIndexBuffer9 *m_ibo;
48#elif defined _XBOX
49    D3DIndexBuffer *m_ibo;
50#else
51    GLuint m_ibo;
52    uint8_t *m_memory;
53#endif
54};
55
56//
57// The IndexBuffer class
58// ----------------------
59//
60
61IndexBuffer::IndexBuffer(size_t size)
62  : m_data(new IndexBufferData)
63{
64    m_data->m_size = size;
65    if (!size)
66        return;
67#if defined USE_D3D9 || defined _XBOX
68    if (FAILED(g_d3ddevice->CreateIndexBuffer(size, D3DUSAGE_WRITEONLY,
69                                              D3DFMT_INDEX16, D3DPOOL_MANAGED,
70                                              &m_data->m_ibo, NULL)))
71        Abort();
72#else
73    glGenBuffers(1, &m_data->m_ibo);
74    m_data->m_memory = new uint8_t[size];
75#endif
76}
77
78IndexBuffer::~IndexBuffer()
79{
80    if (m_data->m_size)
81    {
82#if defined USE_D3D9 || defined _XBOX
83        if (FAILED(m_data->m_ibo->Release()))
84            Abort();
85#else
86        glDeleteBuffers(1, &m_data->m_ibo);
87        delete[] m_data->m_memory;
88#endif
89    }
90    delete m_data;
91}
92
93void *IndexBuffer::Lock(size_t offset, size_t size)
94{
95    if (!m_data->m_size)
96        return NULL;
97
98#if defined USE_D3D9 || defined _XBOX
99    void *ret;
100    if (FAILED(m_data->m_ibo->Lock(offset, size, (void **)&ret, 0)))
101        Abort();
102    return ret;
103#else
104    return m_data->m_memory + offset;
105#endif
106}
107
108void IndexBuffer::Unlock()
109{
110    if (!m_data->m_size)
111        return;
112
113#if defined USE_D3D9 || defined _XBOX
114    if (FAILED(m_data->m_ibo->Unlock()))
115        Abort();
116#else
117    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_data->m_ibo);
118    glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_data->m_size, m_data->m_memory,
119                 GL_STATIC_DRAW);
120#endif
121}
122
123void IndexBuffer::Bind()
124{
125    if (!m_data->m_size)
126        return;
127
128#if defined USE_D3D9 || defined _XBOX
129    if (FAILED(g_d3ddevice->SetIndices(m_data->m_ibo)))
130        Abort();
131#else
132    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_data->m_ibo);
133    /* XXX: not necessary because we kept track of the size */
134    //int size;
135    //glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
136#endif
137}
138
139void IndexBuffer::Unbind()
140{
141    if (!m_data->m_size)
142        return;
143
144#if defined USE_D3D9 || defined _XBOX
145    if (FAILED(g_d3ddevice->SetIndices(NULL)))
146        Abort();
147#else
148    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
149#endif
150}
151
152} /* namespace lol */
153
Note: See TracBrowser for help on using the repository browser.