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

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

gpu: abstraction class for index buffers; the cube tutorial no longer
needs to know about Direct3D headers etc.

  • Property svn:keywords set to Id
File size: 3.2 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://sam.zoy.org/projects/COPYING.WTFPL 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#if defined USE_D3D9
45    IDirect3DIndexBuffer9 *m_ibo;
46#elif defined _XBOX
47    D3DIndexBuffer *m_ibo;
48#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
49    GLuint m_ibo;
50    uint8_t *m_memory;
51    size_t m_size;
52#endif
53};
54
55//
56// The IndexBuffer class
57// ----------------------
58//
59
60IndexBuffer::IndexBuffer(size_t size)
61  : m_data(new IndexBufferData)
62{
63#if defined USE_D3D9 || defined _XBOX
64    if (FAILED(g_d3ddevice->CreateIndexBuffer(size, D3DUSAGE_WRITEONLY,
65                                              D3DFMT_INDEX16, D3DPOOL_MANAGED,
66                                              &m_data->m_ibo, NULL)))
67        Abort();
68#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
69    glGenBuffers(1, &m_data->m_ibo);
70    m_data->m_memory = new uint8_t[size];
71    m_data->m_size = size;
72#endif
73}
74
75IndexBuffer::~IndexBuffer()
76{
77#if defined USE_D3D9 || defined _XBOX
78    if (FAILED(m_data->m_ibo->Release()))
79        Abort();
80#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
81    glDeleteBuffers(1, &m_data->m_ibo);
82    delete[] m_data->m_memory;
83#endif
84}
85
86void *IndexBuffer::Lock(size_t offset, size_t size)
87{
88#if defined USE_D3D9 || defined _XBOX
89    void *ret;
90    if (FAILED(m_data->m_ibo->Lock(offset, size, (void **)&ret, 0)))
91        Abort();
92    return ret;
93#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
94    return m_data->m_memory + offset;
95#endif
96}
97
98void IndexBuffer::Unlock()
99{
100#if defined USE_D3D9 || defined _XBOX
101    if (FAILED(m_data->m_ibo->Unlock()))
102        Abort();
103#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
104    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_data->m_ibo);
105    glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_data->m_size, m_data->m_memory,
106                 GL_STATIC_DRAW);
107#endif
108}
109
110void IndexBuffer::Bind()
111{
112#if defined USE_D3D9 || defined _XBOX
113    if (FAILED(g_d3ddevice->SetIndices(m_data->m_ibo)))
114        Abort();
115#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
116    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_data->m_ibo);
117    /* XXX: not necessary because we kept track of the size */
118    //int size;
119    //glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
120#endif
121}
122
123void IndexBuffer::Unbind()
124{
125#if defined USE_D3D9 || defined _XBOX
126    if (FAILED(g_d3ddevice->SetIndices(NULL)))
127        Abort();
128#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
129    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
130#endif
131}
132
133} /* namespace lol */
134
Note: See TracBrowser for help on using the repository browser.