needs to know about Direct3D headers etc.legacy
| @@ -29,7 +29,7 @@ liblol_a_SOURCES = \ | |||||
| math/vector.cpp math/real.cpp math/half.cpp math/trig.cpp math/trig.h \ | math/vector.cpp math/real.cpp math/half.cpp math/trig.cpp math/trig.h \ | ||||
| \ | \ | ||||
| gpu/shader.cpp gpu/shader.h \ | gpu/shader.cpp gpu/shader.h \ | ||||
| gpu/vbo.cpp gpu/vbo.h \ | |||||
| gpu/indexbuffer.cpp gpu/indexbuffer.h \ | |||||
| gpu/vertexbuffer.cpp gpu/vertexbuffer.h \ | gpu/vertexbuffer.cpp gpu/vertexbuffer.h \ | ||||
| \ | \ | ||||
| image/image.cpp image/image.h image/image-private.h \ | image/image.cpp image/image.h image/image-private.h \ | ||||
| @@ -100,8 +100,8 @@ static inline int isnan(float f) | |||||
| #include "map.h" | #include "map.h" | ||||
| #include "layer.h" | #include "layer.h" | ||||
| #include "gpu/shader.h" | #include "gpu/shader.h" | ||||
| #include "gpu/indexbuffer.h" | |||||
| #include "gpu/vertexbuffer.h" | #include "gpu/vertexbuffer.h" | ||||
| #include "gpu/vbo.h" | |||||
| #include "image/image.h" | #include "image/image.h" | ||||
| #include "application/application.h" | #include "application/application.h" | ||||
| @@ -0,0 +1,134 @@ | |||||
| // | |||||
| // Lol Engine | |||||
| // | |||||
| // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net> | |||||
| // This program is free software; you can redistribute it and/or | |||||
| // modify it under the terms of the Do What The Fuck You Want To | |||||
| // Public License, Version 2, as published by Sam Hocevar. See | |||||
| // http://sam.zoy.org/projects/COPYING.WTFPL for more details. | |||||
| // | |||||
| #if defined HAVE_CONFIG_H | |||||
| # include "config.h" | |||||
| #endif | |||||
| #include "core.h" | |||||
| #include "lolgl.h" | |||||
| #if defined _WIN32 && defined USE_D3D9 | |||||
| # define FAR | |||||
| # define NEAR | |||||
| # include <d3d9.h> | |||||
| #endif | |||||
| using namespace std; | |||||
| #if defined USE_D3D9 | |||||
| extern IDirect3DDevice9 *g_d3ddevice; | |||||
| #elif defined _XBOX | |||||
| extern D3DDevice *g_d3ddevice; | |||||
| #endif | |||||
| namespace lol | |||||
| { | |||||
| // | |||||
| // The IndexBufferData class | |||||
| // -------------------------- | |||||
| // | |||||
| class IndexBufferData | |||||
| { | |||||
| friend class IndexBuffer; | |||||
| #if defined USE_D3D9 | |||||
| IDirect3DIndexBuffer9 *m_ibo; | |||||
| #elif defined _XBOX | |||||
| D3DIndexBuffer *m_ibo; | |||||
| #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ | |||||
| GLuint m_ibo; | |||||
| uint8_t *m_memory; | |||||
| size_t m_size; | |||||
| #endif | |||||
| }; | |||||
| // | |||||
| // The IndexBuffer class | |||||
| // ---------------------- | |||||
| // | |||||
| IndexBuffer::IndexBuffer(size_t size) | |||||
| : m_data(new IndexBufferData) | |||||
| { | |||||
| #if defined USE_D3D9 || defined _XBOX | |||||
| if (FAILED(g_d3ddevice->CreateIndexBuffer(size, D3DUSAGE_WRITEONLY, | |||||
| D3DFMT_INDEX16, D3DPOOL_MANAGED, | |||||
| &m_data->m_ibo, NULL))) | |||||
| Abort(); | |||||
| #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ | |||||
| glGenBuffers(1, &m_data->m_ibo); | |||||
| m_data->m_memory = new uint8_t[size]; | |||||
| m_data->m_size = size; | |||||
| #endif | |||||
| } | |||||
| IndexBuffer::~IndexBuffer() | |||||
| { | |||||
| #if defined USE_D3D9 || defined _XBOX | |||||
| if (FAILED(m_data->m_ibo->Release())) | |||||
| Abort(); | |||||
| #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ | |||||
| glDeleteBuffers(1, &m_data->m_ibo); | |||||
| delete[] m_data->m_memory; | |||||
| #endif | |||||
| } | |||||
| void *IndexBuffer::Lock(size_t offset, size_t size) | |||||
| { | |||||
| #if defined USE_D3D9 || defined _XBOX | |||||
| void *ret; | |||||
| if (FAILED(m_data->m_ibo->Lock(offset, size, (void **)&ret, 0))) | |||||
| Abort(); | |||||
| return ret; | |||||
| #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ | |||||
| return m_data->m_memory + offset; | |||||
| #endif | |||||
| } | |||||
| void IndexBuffer::Unlock() | |||||
| { | |||||
| #if defined USE_D3D9 || defined _XBOX | |||||
| if (FAILED(m_data->m_ibo->Unlock())) | |||||
| Abort(); | |||||
| #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ | |||||
| glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_data->m_ibo); | |||||
| glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_data->m_size, m_data->m_memory, | |||||
| GL_STATIC_DRAW); | |||||
| #endif | |||||
| } | |||||
| void IndexBuffer::Bind() | |||||
| { | |||||
| #if defined USE_D3D9 || defined _XBOX | |||||
| if (FAILED(g_d3ddevice->SetIndices(m_data->m_ibo))) | |||||
| Abort(); | |||||
| #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ | |||||
| glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_data->m_ibo); | |||||
| /* XXX: not necessary because we kept track of the size */ | |||||
| //int size; | |||||
| //glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size); | |||||
| #endif | |||||
| } | |||||
| void IndexBuffer::Unbind() | |||||
| { | |||||
| #if defined USE_D3D9 || defined _XBOX | |||||
| if (FAILED(g_d3ddevice->SetIndices(NULL))) | |||||
| Abort(); | |||||
| #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ | |||||
| glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | |||||
| #endif | |||||
| } | |||||
| } /* namespace lol */ | |||||
| @@ -1,7 +1,7 @@ | |||||
| // | // | ||||
| // Lol Engine | // Lol Engine | ||||
| // | // | ||||
| // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> | |||||
| // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net> | |||||
| // This program is free software; you can redistribute it and/or | // This program is free software; you can redistribute it and/or | ||||
| // modify it under the terms of the Do What The Fuck You Want To | // modify it under the terms of the Do What The Fuck You Want To | ||||
| // Public License, Version 2, as published by Sam Hocevar. See | // Public License, Version 2, as published by Sam Hocevar. See | ||||
| @@ -9,37 +9,35 @@ | |||||
| // | // | ||||
| // | // | ||||
| // The GpuVbo class | |||||
| // ---------------- | |||||
| // The IndexBuffer class | |||||
| // --------------------- | |||||
| // | // | ||||
| #if !defined __LOL_VBO_H__ | |||||
| #define __LOL_VBO_H__ | |||||
| #if !defined __LOL_INDEXBUFFER_H__ | |||||
| #define __LOL_INDEXBUFFER_H__ | |||||
| #include <cstring> | |||||
| namespace lol | namespace lol | ||||
| { | { | ||||
| class GpuVboData; | |||||
| class GpuVbo | |||||
| class IndexBuffer | |||||
| { | { | ||||
| public: | public: | ||||
| GpuVbo(); | |||||
| ~GpuVbo(); | |||||
| IndexBuffer(size_t size); | |||||
| ~IndexBuffer(); | |||||
| void SetSize(size_t elemsize, size_t elemcount); | |||||
| size_t GetSize(); | |||||
| uint8_t *GetData(); | |||||
| uint8_t const *GetData() const; | |||||
| void *Lock(size_t offset, size_t size); | |||||
| void Unlock(); | |||||
| void Bind(); | void Bind(); | ||||
| void Unbind(); | void Unbind(); | ||||
| private: | private: | ||||
| GpuVboData *data; | |||||
| class IndexBufferData *m_data; | |||||
| }; | }; | ||||
| } /* namespace lol */ | } /* namespace lol */ | ||||
| #endif // __LOL_VBO_H__ | |||||
| #endif // __LOL_INDEXBUFFER_H__ | |||||
| @@ -1,100 +0,0 @@ | |||||
| // | |||||
| // Lol Engine | |||||
| // | |||||
| // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> | |||||
| // This program is free software; you can redistribute it and/or | |||||
| // modify it under the terms of the Do What The Fuck You Want To | |||||
| // Public License, Version 2, as published by Sam Hocevar. See | |||||
| // http://sam.zoy.org/projects/COPYING.WTFPL for more details. | |||||
| // | |||||
| #if defined HAVE_CONFIG_H | |||||
| # include "config.h" | |||||
| #endif | |||||
| #include "core.h" | |||||
| #include "lolgl.h" | |||||
| #include "gpu/vbo.h" | |||||
| using namespace std; | |||||
| namespace lol | |||||
| { | |||||
| /* | |||||
| * GpuVbo implementation class | |||||
| */ | |||||
| class GpuVboData | |||||
| { | |||||
| friend class GpuVbo; | |||||
| size_t elemsize, elemcount; | |||||
| uint8_t *alloc_buffer; | |||||
| static size_t const GPU_ALIGN = 128; | |||||
| }; | |||||
| /* | |||||
| * Public GpuVbo class | |||||
| */ | |||||
| GpuVbo::GpuVbo() | |||||
| : data(new GpuVboData()) | |||||
| { | |||||
| data->elemsize = 0; | |||||
| data->elemcount = 0; | |||||
| data->alloc_buffer = 0; | |||||
| } | |||||
| void GpuVbo::SetSize(size_t elemsize, size_t elemcount) | |||||
| { | |||||
| size_t oldsize = data->elemsize * data->elemcount; | |||||
| size_t newsize = elemsize * elemcount; | |||||
| if (newsize == oldsize) | |||||
| return; | |||||
| if (oldsize) | |||||
| delete[] data->alloc_buffer; | |||||
| data->alloc_buffer = NULL; | |||||
| if (newsize) | |||||
| data->alloc_buffer = new uint8_t[newsize + GpuVboData::GPU_ALIGN - 1]; | |||||
| data->elemsize = elemsize; | |||||
| data->elemcount = elemcount; | |||||
| } | |||||
| size_t GpuVbo::GetSize() | |||||
| { | |||||
| return data->elemsize * data->elemcount; | |||||
| } | |||||
| uint8_t *GpuVbo::GetData() | |||||
| { | |||||
| return (uint8_t *)(((uintptr_t)data->alloc_buffer) | |||||
| & ~((uintptr_t)GpuVboData::GPU_ALIGN - 1)); | |||||
| } | |||||
| uint8_t const *GpuVbo::GetData() const | |||||
| { | |||||
| return (uint8_t const *)(((uintptr_t)data->alloc_buffer) | |||||
| & ~((uintptr_t)GpuVboData::GPU_ALIGN - 1)); | |||||
| } | |||||
| void GpuVbo::Bind() | |||||
| { | |||||
| } | |||||
| void GpuVbo::Unbind() | |||||
| { | |||||
| } | |||||
| GpuVbo::~GpuVbo() | |||||
| { | |||||
| delete[] data->alloc_buffer; | |||||
| delete data; | |||||
| } | |||||
| } /* namespace lol */ | |||||
| @@ -142,6 +142,31 @@ void VertexDeclaration::DrawElements(MeshPrimitive type, int skip, int count) | |||||
| #endif | #endif | ||||
| } | } | ||||
| void VertexDeclaration::DrawIndexedElements(MeshPrimitive type, int vbase, | |||||
| int vskip, int vcount, | |||||
| int skip, int count) | |||||
| { | |||||
| #if defined _XBOX || defined USE_D3D9 | |||||
| if (FAILED(g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW))) | |||||
| Abort(); | |||||
| switch (type) | |||||
| { | |||||
| case MeshPrimitive::Triangles: | |||||
| if (FAILED(g_d3ddevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, vbase, vskip, vcount, skip, count))) | |||||
| Abort(); | |||||
| break; | |||||
| } | |||||
| #else | |||||
| switch (type) | |||||
| { | |||||
| case MeshPrimitive::Triangles: | |||||
| /* FIXME: ignores most of the arguments! */ | |||||
| glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_SHORT, 0); | |||||
| break; | |||||
| } | |||||
| #endif | |||||
| } | |||||
| void VertexDeclaration::Unbind() | void VertexDeclaration::Unbind() | ||||
| { | { | ||||
| #if defined _XBOX || defined USE_D3D9 | #if defined _XBOX || defined USE_D3D9 | ||||
| @@ -177,6 +177,8 @@ public: | |||||
| void Bind(); | void Bind(); | ||||
| void DrawElements(MeshPrimitive type, int skip, int count); | void DrawElements(MeshPrimitive type, int skip, int count); | ||||
| void DrawIndexedElements(MeshPrimitive type, int vbase, int vskip, | |||||
| int vcount, int skip, int count); | |||||
| void Unbind(); | void Unbind(); | ||||
| void SetStream(VertexBuffer *vb, ShaderAttrib attr1, | void SetStream(VertexBuffer *vb, ShaderAttrib attr1, | ||||
| ShaderAttrib attr2 = ShaderAttrib(), | ShaderAttrib attr2 = ShaderAttrib(), | ||||
| @@ -13,18 +13,11 @@ | |||||
| #endif | #endif | ||||
| #include "core.h" | #include "core.h" | ||||
| #include "lolgl.h" | |||||
| #include "loldebug.h" | #include "loldebug.h" | ||||
| using namespace std; | using namespace std; | ||||
| using namespace lol; | using namespace lol; | ||||
| #if defined _WIN32 && defined USE_D3D9 | |||||
| # define FAR | |||||
| # define NEAR | |||||
| # include <d3d9.h> | |||||
| #endif | |||||
| #if USE_SDL && defined __APPLE__ | #if USE_SDL && defined __APPLE__ | ||||
| # include <SDL_main.h> | # include <SDL_main.h> | ||||
| #endif | #endif | ||||
| @@ -34,12 +27,6 @@ using namespace lol; | |||||
| # include <direct.h> | # include <direct.h> | ||||
| #endif | #endif | ||||
| #if defined USE_D3D9 | |||||
| extern IDirect3DDevice9 *g_d3ddevice; | |||||
| #elif defined _XBOX | |||||
| extern D3DDevice *g_d3ddevice; | |||||
| #endif | |||||
| class Cube : public WorldEntity | class Cube : public WorldEntity | ||||
| { | { | ||||
| public: | public: | ||||
| @@ -144,23 +131,10 @@ public: | |||||
| memcpy(mesh, &m_mesh[0], m_mesh.Bytes()); | memcpy(mesh, &m_mesh[0], m_mesh.Bytes()); | ||||
| m_vbo->Unlock(); | m_vbo->Unlock(); | ||||
| #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ && !defined _XBOX && !defined USE_D3D9 | |||||
| /* Method 1: store vertex buffer on the GPU memory */ | |||||
| glGenBuffers(1, &m_ibo); | |||||
| glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo); | |||||
| glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indices.Bytes(), | |||||
| &m_indices[0], GL_STATIC_DRAW); | |||||
| #elif defined _XBOX || defined USE_D3D9 | |||||
| int16_t *indices; | |||||
| if (FAILED(g_d3ddevice->CreateIndexBuffer(m_indices.Bytes(), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_ibo, NULL))) | |||||
| Abort(); | |||||
| if (FAILED(m_ibo->Lock(0, 0, (void **)&indices, 0))) | |||||
| Abort(); | |||||
| m_ibo = new IndexBuffer(m_indices.Bytes()); | |||||
| void *indices = m_ibo->Lock(0, 0); | |||||
| memcpy(indices, &m_indices[0], m_indices.Bytes()); | memcpy(indices, &m_indices[0], m_indices.Bytes()); | ||||
| m_ibo->Unlock(); | m_ibo->Unlock(); | ||||
| #else | |||||
| /* TODO */ | |||||
| #endif | |||||
| /* FIXME: this object never cleans up */ | /* FIXME: this object never cleans up */ | ||||
| m_ready = true; | m_ready = true; | ||||
| @@ -172,29 +146,10 @@ public: | |||||
| m_shader->SetUniform(m_mvp, m_matrix); | m_shader->SetUniform(m_mvp, m_matrix); | ||||
| m_vdecl->SetStream(m_vbo, m_coord, m_color); | m_vdecl->SetStream(m_vbo, m_coord, m_color); | ||||
| m_vdecl->Bind(); | m_vdecl->Bind(); | ||||
| #if defined _XBOX || defined USE_D3D9 | |||||
| if (FAILED(g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW))) | |||||
| Abort(); | |||||
| if (FAILED(g_d3ddevice->SetIndices(m_ibo))) | |||||
| Abort(); | |||||
| if (FAILED(g_d3ddevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, m_indices.Count()))) | |||||
| Abort(); | |||||
| #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ | |||||
| glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo); | |||||
| int size; | |||||
| glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size); | |||||
| glDrawElements(GL_TRIANGLES, size / sizeof(uint16_t), GL_UNSIGNED_SHORT, 0); | |||||
| glBindBuffer(GL_ARRAY_BUFFER, 0); | |||||
| #else | |||||
| /* FIXME */ | |||||
| glEnableClientState(GL_VERTEX_ARRAY); | |||||
| glVertexPointer(3, GL_FLOAT, 0, m_vertices); | |||||
| glDisableClientState(GL_VERTEX_ARRAY); | |||||
| #endif | |||||
| m_ibo->Bind(); | |||||
| m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0, 8, 0, | |||||
| m_indices.Count()); | |||||
| m_ibo->Unbind(); | |||||
| m_vdecl->Unbind(); | m_vdecl->Unbind(); | ||||
| } | } | ||||
| @@ -208,15 +163,7 @@ private: | |||||
| ShaderUniform m_mvp; | ShaderUniform m_mvp; | ||||
| VertexDeclaration *m_vdecl; | VertexDeclaration *m_vdecl; | ||||
| VertexBuffer *m_vbo; | VertexBuffer *m_vbo; | ||||
| #if defined USE_D3D9 | |||||
| IDirect3DIndexBuffer9 *m_ibo; | |||||
| #elif defined _XBOX | |||||
| D3DIndexBuffer *m_ibo; | |||||
| #else | |||||
| #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ | |||||
| GLuint m_ibo; | |||||
| #endif | |||||
| #endif | |||||
| IndexBuffer *m_ibo; | |||||
| bool m_ready; | bool m_ready; | ||||
| }; | }; | ||||
| @@ -86,7 +86,6 @@ | |||||
| <ClCompile Include="..\src\font.cpp" /> | <ClCompile Include="..\src\font.cpp" /> | ||||
| <ClCompile Include="..\src\forge.cpp" /> | <ClCompile Include="..\src\forge.cpp" /> | ||||
| <ClCompile Include="..\src\gpu\shader.cpp" /> | <ClCompile Include="..\src\gpu\shader.cpp" /> | ||||
| <ClCompile Include="..\src\gpu\vbo.cpp" /> | |||||
| <ClCompile Include="..\src\gpu\vertexbuffer.cpp" /> | <ClCompile Include="..\src\gpu\vertexbuffer.cpp" /> | ||||
| <ClCompile Include="..\src\gradient.cpp" /> | <ClCompile Include="..\src\gradient.cpp" /> | ||||
| <ClCompile Include="..\src\hash.cpp" /> | <ClCompile Include="..\src\hash.cpp" /> | ||||
| @@ -140,7 +139,6 @@ | |||||
| <ClInclude Include="..\src\font.h" /> | <ClInclude Include="..\src\font.h" /> | ||||
| <ClInclude Include="..\src\forge.h" /> | <ClInclude Include="..\src\forge.h" /> | ||||
| <ClInclude Include="..\src\gpu\shader.h" /> | <ClInclude Include="..\src\gpu\shader.h" /> | ||||
| <ClInclude Include="..\src\gpu\vbo.h" /> | |||||
| <ClInclude Include="..\src\gpu\vertexbuffer.h" /> | <ClInclude Include="..\src\gpu\vertexbuffer.h" /> | ||||
| <ClInclude Include="..\src\gradient.h" /> | <ClInclude Include="..\src\gradient.h" /> | ||||
| <ClInclude Include="..\src\hash.h" /> | <ClInclude Include="..\src\hash.h" /> | ||||
| @@ -160,10 +160,10 @@ | |||||
| <ClCompile Include="..\src\worldentity.cpp"> | <ClCompile Include="..\src\worldentity.cpp"> | ||||
| <Filter>src</Filter> | <Filter>src</Filter> | ||||
| </ClCompile> | </ClCompile> | ||||
| <ClCompile Include="..\src\gpu\shader.cpp"> | |||||
| <ClCompile Include="..\src\gpu\indexbuffer.cpp"> | |||||
| <Filter>src\gpu</Filter> | <Filter>src\gpu</Filter> | ||||
| </ClCompile> | </ClCompile> | ||||
| <ClCompile Include="..\src\gpu\vbo.cpp"> | |||||
| <ClCompile Include="..\src\gpu\shader.cpp"> | |||||
| <Filter>src\gpu</Filter> | <Filter>src\gpu</Filter> | ||||
| </ClCompile> | </ClCompile> | ||||
| <ClCompile Include="..\src\gpu\vertexbuffer.cpp"> | <ClCompile Include="..\src\gpu\vertexbuffer.cpp"> | ||||
| @@ -336,10 +336,10 @@ | |||||
| <ClInclude Include="..\src\lol\unit.h"> | <ClInclude Include="..\src\lol\unit.h"> | ||||
| <Filter>src\lol</Filter> | <Filter>src\lol</Filter> | ||||
| </ClInclude> | </ClInclude> | ||||
| <ClInclude Include="..\src\gpu\shader.h"> | |||||
| <ClInclude Include="..\src\gpu\indexbuffer.h"> | |||||
| <Filter>src\gpu</Filter> | <Filter>src\gpu</Filter> | ||||
| </ClInclude> | </ClInclude> | ||||
| <ClInclude Include="..\src\gpu\vbo.h"> | |||||
| <ClInclude Include="..\src\gpu\shader.h"> | |||||
| <Filter>src\gpu</Filter> | <Filter>src\gpu</Filter> | ||||
| </ClInclude> | </ClInclude> | ||||
| <ClInclude Include="..\src\gpu\vertexbuffer.h"> | <ClInclude Include="..\src\gpu\vertexbuffer.h"> | ||||