Browse Source

core: add a simple Array template class.

legacy
Sam Hocevar sam 13 years ago
parent
commit
02e31f5c41
4 changed files with 124 additions and 36 deletions
  1. +87
    -0
      src/array.h
  2. +1
    -0
      src/core.h
  3. +6
    -6
      test/tutorial/01_triangle.cpp
  4. +30
    -30
      test/tutorial/02_cube.cpp

+ 87
- 0
src/array.h View File

@@ -0,0 +1,87 @@
//
// 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.
//

//
// The Array class
// ---------------
// A very simple Array class not unlike the std::vector.
//

#if !defined __LOL_ARRAY_H__
#define __LOL_ARRAY_H__

#include <stdint.h>

namespace lol
{

template<typename T> class Array
{
public:
inline Array() : m_data(0), m_count(0), m_reserved(0) {}
inline ~Array() { delete[] m_data; }

inline T& operator[](int n)
{
return m_data[n];
}

inline T const& operator[](int n) const
{
return m_data[n];
}

inline Array<T> const& operator+=(T const &x)
{
if (m_count >= m_reserved)
Reserve(m_count * 13 / 8 + 8);
m_data[m_count++] = x;
return *this;
}

void Remove(int pos)
{
memmove(m_data + pos, m_data + pos + 1, m_count - pos - 1);
m_count--;
}

void Remove(int pos, int count)
{
memmove(m_data + pos, m_data + pos + count, m_count - pos - count);
m_count -= count;
}

void Reserve(int count)
{
if (count <= (int)m_reserved)
return;

T *tmp = new T[count];
if (m_data)
{
memcpy(tmp, m_data, m_count * sizeof(T));
delete[] m_data;
}
m_data = tmp;
m_reserved = count;
}

inline size_t Count() const { return m_count; }
inline size_t Bytes() const { return m_count * sizeof(T); }

private:
T *m_data;
size_t m_count, m_reserved;
};

} /* namespace lol */

#endif // __LOL_ARRAY_H__


+ 1
- 0
src/core.h View File

@@ -70,6 +70,7 @@ static inline int isnan(float f)
#include "numeric.h"
#include "timer.h"
#include "thread/thread.h"
#include "array.h"

// Static classes
#include "log.h"


+ 6
- 6
test/tutorial/01_triangle.cpp View File

@@ -36,9 +36,9 @@ class Triangle : public WorldEntity
public:
Triangle()
{
m_vertices[0] = vec2( 0.0, 0.8);
m_vertices[1] = vec2(-0.8, -0.8);
m_vertices[2] = vec2( 0.8, -0.8);
m_vertices += vec2( 0.0, 0.8);
m_vertices += vec2(-0.8, -0.8);
m_vertices += vec2( 0.8, -0.8);
m_ready = false;
}

@@ -75,9 +75,9 @@ public:

m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));

m_vbo = new VertexBuffer(sizeof(m_vertices));
m_vbo = new VertexBuffer(m_vertices.Bytes());
void *vertices = m_vbo->Lock(0, 0);
memcpy(vertices, m_vertices, sizeof(m_vertices));
memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
m_vbo->Unlock();

m_ready = true;
@@ -93,7 +93,7 @@ public:
}

private:
vec2 m_vertices[3];
Array<vec2> m_vertices;
Shader *m_shader;
ShaderAttrib m_coord;
VertexDeclaration *m_vdecl;


+ 30
- 30
test/tutorial/02_cube.cpp View File

@@ -48,28 +48,28 @@ public:
m_angle = 0;

/* Front vertices/colors */
m_mesh[0] = vec3(-1.0, -1.0, 1.0); m_mesh[1] = vec3(1.0, 0.0, 1.0);
m_mesh[2] = vec3( 1.0, -1.0, 1.0); m_mesh[3] = vec3(0.0, 1.0, 0.0);
m_mesh[4] = vec3( 1.0, 1.0, 1.0); m_mesh[5] = vec3(1.0, 0.5, 0.0);
m_mesh[6] = vec3(-1.0, 1.0, 1.0); m_mesh[7] = vec3(1.0, 1.0, 0.0);
m_mesh += vec3(-1.0, -1.0, 1.0); m_mesh += vec3(1.0, 0.0, 1.0);
m_mesh += vec3( 1.0, -1.0, 1.0); m_mesh += vec3(0.0, 1.0, 0.0);
m_mesh += vec3( 1.0, 1.0, 1.0); m_mesh += vec3(1.0, 0.5, 0.0);
m_mesh += vec3(-1.0, 1.0, 1.0); m_mesh += vec3(1.0, 1.0, 0.0);
/* Back */
m_mesh[8] = vec3(-1.0, -1.0, -1.0); m_mesh[9] = vec3(1.0, 0.0, 0.0);
m_mesh[10] = vec3( 1.0, -1.0, -1.0); m_mesh[11] = vec3(0.0, 0.5, 0.0);
m_mesh[12] = vec3( 1.0, 1.0, -1.0); m_mesh[13] = vec3(0.0, 0.5, 1.0);
m_mesh[14] = vec3(-1.0, 1.0, -1.0); m_mesh[15] = vec3(0.0, 0.0, 1.0);
m_indices[0] = i16vec3(0, 1, 2);
m_indices[1] = i16vec3(2, 3, 0);
m_indices[2] = i16vec3(1, 5, 6);
m_indices[3] = i16vec3(6, 2, 1);
m_indices[4] = i16vec3(7, 6, 5);
m_indices[5] = i16vec3(5, 4, 7);
m_indices[6] = i16vec3(4, 0, 3);
m_indices[7] = i16vec3(3, 7, 4);
m_indices[8] = i16vec3(4, 5, 1);
m_indices[9] = i16vec3(1, 0, 4);
m_indices[10] = i16vec3(3, 2, 6);
m_indices[11] = i16vec3(6, 7, 3);
m_mesh += vec3(-1.0, -1.0, -1.0); m_mesh += vec3(1.0, 0.0, 0.0);
m_mesh += vec3( 1.0, -1.0, -1.0); m_mesh += vec3(0.0, 0.5, 0.0);
m_mesh += vec3( 1.0, 1.0, -1.0); m_mesh += vec3(0.0, 0.5, 1.0);
m_mesh += vec3(-1.0, 1.0, -1.0); m_mesh += vec3(0.0, 0.0, 1.0);
m_indices += i16vec3(0, 1, 2);
m_indices += i16vec3(2, 3, 0);
m_indices += i16vec3(1, 5, 6);
m_indices += i16vec3(6, 2, 1);
m_indices += i16vec3(7, 6, 5);
m_indices += i16vec3(5, 4, 7);
m_indices += i16vec3(4, 0, 3);
m_indices += i16vec3(3, 7, 4);
m_indices += i16vec3(4, 5, 1);
m_indices += i16vec3(1, 0, 4);
m_indices += i16vec3(3, 2, 6);
m_indices += i16vec3(6, 7, 3);

m_ready = false;
}
@@ -139,24 +139,24 @@ public:
new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position,
VertexUsage::Color));

m_vbo = new VertexBuffer(sizeof(m_mesh));
m_vbo = new VertexBuffer(m_mesh.Bytes());
void *mesh = m_vbo->Lock(0, 0);
memcpy(mesh, m_mesh, sizeof(m_mesh));
memcpy(mesh, &m_mesh[0], m_mesh.Bytes());
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, sizeof(m_indices), m_indices,
GL_STATIC_DRAW);
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(sizeof(m_indices), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_ibo, NULL)))
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();
memcpy(indices, m_indices, sizeof(m_indices));
memcpy(indices, &m_indices[0], m_indices.Bytes());
m_ibo->Unlock();
#else
/* TODO */
@@ -178,7 +178,7 @@ public:
Abort();
if (FAILED(g_d3ddevice->SetIndices(m_ibo)))
Abort();
if (FAILED(g_d3ddevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, sizeof(m_indices) / sizeof(*m_indices))))
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);
@@ -201,8 +201,8 @@ public:
private:
float m_angle;
mat4 m_matrix;
vec3 m_mesh[16];
i16vec3 m_indices[12];
Array<vec3> m_mesh;
Array<i16vec3> m_indices;
Shader *m_shader;
ShaderAttrib m_coord, m_color;
ShaderUniform m_mvp;


Loading…
Cancel
Save