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

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

gpu: fix a memory leak in the VertexBuffer and IndexBuffer classes.

  • 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    delete m_data;
85}
86
87void *IndexBuffer::Lock(size_t offset, size_t size)
88{
89#if defined USE_D3D9 || defined _XBOX
90    void *ret;
91    if (FAILED(m_data->m_ibo->Lock(offset, size, (void **)&ret, 0)))
92        Abort();
93    return ret;
94#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
95    return m_data->m_memory + offset;
96#endif
97}
98
99void IndexBuffer::Unlock()
100{
101#if defined USE_D3D9 || defined _XBOX
102    if (FAILED(m_data->m_ibo->Unlock()))
103        Abort();
104#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
105    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_data->m_ibo);
106    glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_data->m_size, m_data->m_memory,
107                 GL_STATIC_DRAW);
108#endif
109}
110
111void IndexBuffer::Bind()
112{
113#if defined USE_D3D9 || defined _XBOX
114    if (FAILED(g_d3ddevice->SetIndices(m_data->m_ibo)))
115        Abort();
116#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
117    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_data->m_ibo);
118    /* XXX: not necessary because we kept track of the size */
119    //int size;
120    //glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
121#endif
122}
123
124void IndexBuffer::Unbind()
125{
126#if defined USE_D3D9 || defined _XBOX
127    if (FAILED(g_d3ddevice->SetIndices(NULL)))
128        Abort();
129#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
130    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
131#endif
132}
133
134} /* namespace lol */
135
Note: See TracBrowser for help on using the repository browser.