[1247] | 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 | |
---|
| 24 | using namespace std; |
---|
| 25 | |
---|
| 26 | #if defined USE_D3D9 |
---|
| 27 | extern IDirect3DDevice9 *g_d3ddevice; |
---|
| 28 | #elif defined _XBOX |
---|
| 29 | extern D3DDevice *g_d3ddevice; |
---|
| 30 | #endif |
---|
| 31 | |
---|
| 32 | namespace lol |
---|
| 33 | { |
---|
| 34 | |
---|
| 35 | // |
---|
| 36 | // The IndexBufferData class |
---|
[1455] | 37 | // ------------------------- |
---|
[1247] | 38 | // |
---|
| 39 | |
---|
| 40 | class IndexBufferData |
---|
| 41 | { |
---|
| 42 | friend class IndexBuffer; |
---|
| 43 | |
---|
[1443] | 44 | size_t m_size; |
---|
| 45 | |
---|
[1247] | 46 | #if defined USE_D3D9 |
---|
| 47 | IDirect3DIndexBuffer9 *m_ibo; |
---|
| 48 | #elif defined _XBOX |
---|
| 49 | D3DIndexBuffer *m_ibo; |
---|
| 50 | #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ |
---|
| 51 | GLuint m_ibo; |
---|
| 52 | uint8_t *m_memory; |
---|
| 53 | #endif |
---|
| 54 | }; |
---|
| 55 | |
---|
| 56 | // |
---|
| 57 | // The IndexBuffer class |
---|
| 58 | // ---------------------- |
---|
| 59 | // |
---|
| 60 | |
---|
| 61 | IndexBuffer::IndexBuffer(size_t size) |
---|
| 62 | : m_data(new IndexBufferData) |
---|
| 63 | { |
---|
[1443] | 64 | m_data->m_size = size; |
---|
| 65 | if (!size) |
---|
| 66 | return; |
---|
[1247] | 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 | #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ |
---|
| 73 | glGenBuffers(1, &m_data->m_ibo); |
---|
| 74 | m_data->m_memory = new uint8_t[size]; |
---|
| 75 | #endif |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | IndexBuffer::~IndexBuffer() |
---|
| 79 | { |
---|
[1443] | 80 | if (m_data->m_size) |
---|
| 81 | { |
---|
[1247] | 82 | #if defined USE_D3D9 || defined _XBOX |
---|
[1443] | 83 | if (FAILED(m_data->m_ibo->Release())) |
---|
| 84 | Abort(); |
---|
[1247] | 85 | #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ |
---|
[1443] | 86 | glDeleteBuffers(1, &m_data->m_ibo); |
---|
| 87 | delete[] m_data->m_memory; |
---|
[1247] | 88 | #endif |
---|
[1443] | 89 | } |
---|
[1324] | 90 | delete m_data; |
---|
[1247] | 91 | } |
---|
| 92 | |
---|
| 93 | void *IndexBuffer::Lock(size_t offset, size_t size) |
---|
| 94 | { |
---|
[1443] | 95 | if (!m_data->m_size) |
---|
| 96 | return NULL; |
---|
| 97 | |
---|
[1247] | 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 | #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ |
---|
| 104 | return m_data->m_memory + offset; |
---|
| 105 | #endif |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | void IndexBuffer::Unlock() |
---|
| 109 | { |
---|
[1443] | 110 | if (!m_data->m_size) |
---|
| 111 | return; |
---|
| 112 | |
---|
[1247] | 113 | #if defined USE_D3D9 || defined _XBOX |
---|
| 114 | if (FAILED(m_data->m_ibo->Unlock())) |
---|
| 115 | Abort(); |
---|
| 116 | #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ |
---|
| 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 | |
---|
| 123 | void IndexBuffer::Bind() |
---|
| 124 | { |
---|
[1443] | 125 | if (!m_data->m_size) |
---|
| 126 | return; |
---|
| 127 | |
---|
[1247] | 128 | #if defined USE_D3D9 || defined _XBOX |
---|
| 129 | if (FAILED(g_d3ddevice->SetIndices(m_data->m_ibo))) |
---|
| 130 | Abort(); |
---|
| 131 | #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ |
---|
| 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 | |
---|
| 139 | void IndexBuffer::Unbind() |
---|
| 140 | { |
---|
[1443] | 141 | if (!m_data->m_size) |
---|
| 142 | return; |
---|
| 143 | |
---|
[1247] | 144 | #if defined USE_D3D9 || defined _XBOX |
---|
| 145 | if (FAILED(g_d3ddevice->SetIndices(NULL))) |
---|
| 146 | Abort(); |
---|
| 147 | #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ |
---|
| 148 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
---|
| 149 | #endif |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | } /* namespace lol */ |
---|
| 153 | |
---|