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

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

build: fix the X360 port.

  • Property svn:keywords set to Id
File size: 3.3 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#elif defined _XBOX
23#   include <xtl.h>
24#   undef near /* Fuck Microsoft */
25#   undef far /* Fuck Microsoft again */
26#endif
27
28using namespace std;
29
30namespace lol
31{
32
33//
34// The IndexBufferData class
35// -------------------------
36//
37
38class IndexBufferData
39{
40    friend class IndexBuffer;
41
42    size_t m_size;
43
44#if defined USE_D3D9
45    IDirect3DDevice9 *m_dev;
46    IDirect3DIndexBuffer9 *m_ibo;
47#elif defined _XBOX
48    D3DDevice *m_dev;
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 defined USE_D3D9
69    m_data->m_dev = (IDirect3DDevice9 *)g_renderer->GetDevice();
70#   elif defined _XBOX
71    m_data->m_dev = (D3DDevice *)g_renderer->GetDevice();
72#   endif
73
74    if (FAILED(m_data->m_dev->CreateIndexBuffer(size, D3DUSAGE_WRITEONLY,
75                                              D3DFMT_INDEX16, D3DPOOL_MANAGED,
76                                              &m_data->m_ibo, nullptr)))
77        Abort();
78#else
79    glGenBuffers(1, &m_data->m_ibo);
80    m_data->m_memory = new uint8_t[size];
81#endif
82}
83
84IndexBuffer::~IndexBuffer()
85{
86    if (m_data->m_size)
87    {
88#if defined USE_D3D9 || defined _XBOX
89        if (FAILED(m_data->m_ibo->Release()))
90            Abort();
91#else
92        glDeleteBuffers(1, &m_data->m_ibo);
93        delete[] m_data->m_memory;
94#endif
95    }
96    delete m_data;
97}
98
99void *IndexBuffer::Lock(size_t offset, size_t size)
100{
101    if (!m_data->m_size)
102        return nullptr;
103
104#if defined USE_D3D9 || defined _XBOX
105    void *ret;
106    if (FAILED(m_data->m_ibo->Lock(offset, size, (void **)&ret, 0)))
107        Abort();
108    return ret;
109#else
110    return m_data->m_memory + offset;
111#endif
112}
113
114void IndexBuffer::Unlock()
115{
116    if (!m_data->m_size)
117        return;
118
119#if defined USE_D3D9 || defined _XBOX
120    if (FAILED(m_data->m_ibo->Unlock()))
121        Abort();
122#else
123    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_data->m_ibo);
124    glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_data->m_size, m_data->m_memory,
125                 GL_STATIC_DRAW);
126#endif
127}
128
129void IndexBuffer::Bind()
130{
131    if (!m_data->m_size)
132        return;
133
134#if defined USE_D3D9 || defined _XBOX
135    if (FAILED(m_data->m_dev->SetIndices(m_data->m_ibo)))
136        Abort();
137#else
138    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_data->m_ibo);
139    /* XXX: not necessary because we kept track of the size */
140    //int size;
141    //glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
142#endif
143}
144
145void IndexBuffer::Unbind()
146{
147    if (!m_data->m_size)
148        return;
149
150#if defined USE_D3D9 || defined _XBOX
151    if (FAILED(m_data->m_dev->SetIndices(nullptr)))
152        Abort();
153#else
154    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
155#endif
156}
157
158} /* namespace lol */
159
Note: See TracBrowser for help on using the repository browser.