mirror of
https://github.com/love2d/love.git
synced 2026-08-17 11:00:31 +02:00
Finally abstracted constants properly. Changed how the event module works; wasn't really abstract before. Removed libs we're not going to use: native, signal.
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
* Copyright (c) 2006-2009 LOVE Development Team
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
**/
|
||||
|
||||
#ifndef LOVE_ENUM_MAP_H
|
||||
#define LOVE_ENUM_MAP_H
|
||||
|
||||
#include "Exception.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
template<typename T, typename U, unsigned PEAK>
|
||||
class EnumMap
|
||||
{
|
||||
private:
|
||||
|
||||
struct Value
|
||||
{
|
||||
unsigned v;
|
||||
bool set;
|
||||
Value() : set(false) {}
|
||||
};
|
||||
|
||||
Value values_t[PEAK];
|
||||
Value values_u[PEAK];
|
||||
|
||||
public:
|
||||
|
||||
struct Entry
|
||||
{
|
||||
T t;
|
||||
U u;
|
||||
};
|
||||
|
||||
EnumMap(Entry * entries, unsigned size)
|
||||
{
|
||||
unsigned n = size/sizeof(Entry);
|
||||
|
||||
for(unsigned i = 0; i<n; ++i)
|
||||
{
|
||||
unsigned e_t = (unsigned)entries[i].t;
|
||||
unsigned e_u = (unsigned)entries[i].u;
|
||||
|
||||
if(e_t < PEAK)
|
||||
{
|
||||
values_u[e_t].v = e_u;
|
||||
values_u[e_t].set = true;
|
||||
}
|
||||
if(e_u < PEAK)
|
||||
{
|
||||
values_t[e_u].v = e_t;
|
||||
values_t[e_u].set = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool find(T t, U & u)
|
||||
{
|
||||
if((unsigned)t < PEAK && values_u[(unsigned)t].set)
|
||||
{
|
||||
u = (U)values_u[(unsigned)t].v;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool find(U u, T & t)
|
||||
{
|
||||
if((unsigned)u < PEAK && values_t[(unsigned)u].set)
|
||||
{
|
||||
t = (T)values_t[(unsigned)u].v;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}; // EnumMap
|
||||
|
||||
} // love
|
||||
|
||||
#endif // LOVE_ENUM_MAP_H
|
||||
@@ -0,0 +1,164 @@
|
||||
/**
|
||||
* Copyright (c) 2006-2009 LOVE Development Team
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
**/
|
||||
|
||||
#ifndef LOVE_STRING_MAP_H
|
||||
#define LOVE_STRING_MAP_H
|
||||
|
||||
#include "Exception.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
template<typename T, unsigned SIZE>
|
||||
class StringMap
|
||||
{
|
||||
private:
|
||||
|
||||
struct Record
|
||||
{
|
||||
const char * key;
|
||||
T value;
|
||||
bool set;
|
||||
Record() : set(false) {}
|
||||
};
|
||||
|
||||
const static unsigned MAX = (unsigned)((float)SIZE * 1.5f);
|
||||
|
||||
Record records[MAX];
|
||||
const char * reverse[SIZE];
|
||||
|
||||
public:
|
||||
|
||||
struct Entry
|
||||
{
|
||||
const char * key;
|
||||
T value;
|
||||
};
|
||||
|
||||
StringMap(Entry * entries, unsigned num)
|
||||
{
|
||||
|
||||
for(unsigned i = 0; i < SIZE; ++i)
|
||||
reverse[i] = 0;
|
||||
|
||||
unsigned n = num/sizeof(Entry);
|
||||
|
||||
for(unsigned i = 0; i < n; ++i)
|
||||
{
|
||||
add(entries[i].key, entries[i].value);
|
||||
}
|
||||
}
|
||||
|
||||
bool streq(const char * a, const char * b)
|
||||
{
|
||||
while(*a != 0 && *b != 0)
|
||||
{
|
||||
if(*a != *b)
|
||||
return false;
|
||||
++a;
|
||||
++b;
|
||||
}
|
||||
|
||||
return (*a == 0 && *b == 0);
|
||||
}
|
||||
|
||||
bool find(const char * key, T & t)
|
||||
{
|
||||
unsigned str_hash = djb2(key);
|
||||
|
||||
for(unsigned i = 0; i < MAX; ++i)
|
||||
{
|
||||
unsigned str_i = (str_hash + i) % MAX;
|
||||
|
||||
if(records[i].set && streq(records[i].key, key))
|
||||
{
|
||||
t = records[i].value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool find(T key, const char *& str)
|
||||
{
|
||||
unsigned index = (unsigned)key;
|
||||
|
||||
if(index >= SIZE)
|
||||
return false;
|
||||
|
||||
if(reverse[index] != 0)
|
||||
{
|
||||
str = reverse[index];
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool add(const char * key, T value)
|
||||
{
|
||||
unsigned str_hash = djb2(key);
|
||||
bool inserted = false;
|
||||
|
||||
for(unsigned i = 0; i < MAX; ++i)
|
||||
{
|
||||
unsigned str_i = (str_hash + i) % MAX;
|
||||
|
||||
if(!records[str_i].set)
|
||||
{
|
||||
inserted = true;
|
||||
records[str_i].set = true;
|
||||
records[str_i].key = key;
|
||||
records[str_i].value = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned index = (unsigned)value;
|
||||
|
||||
if(index >= SIZE)
|
||||
{
|
||||
printf("\nConstant %s out of bounds with %i!\n", key, index);
|
||||
return false;
|
||||
}
|
||||
|
||||
reverse[index] = key;
|
||||
|
||||
return inserted;
|
||||
}
|
||||
|
||||
unsigned djb2(const char * key)
|
||||
{
|
||||
unsigned hash = 5381;
|
||||
int c;
|
||||
|
||||
while (c = *key++)
|
||||
hash = ((hash << 5) + hash) + c;
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
}; // StringMap
|
||||
|
||||
} // love
|
||||
|
||||
#endif // LOVE_STRING_MAP_H
|
||||
@@ -134,16 +134,6 @@ namespace love
|
||||
// Gets the love table.
|
||||
luax_insistglobal(L, "love");
|
||||
|
||||
// Install constants.
|
||||
if(m.constants != 0)
|
||||
{
|
||||
for(int i = 0; m.constants[i].name != 0; i++)
|
||||
{
|
||||
lua_pushinteger(L, m.constants[i].value);
|
||||
lua_setfield(L, -2, m.constants[i].name);
|
||||
}
|
||||
}
|
||||
|
||||
// Create new table for module.
|
||||
lua_newtable(L);
|
||||
|
||||
|
||||
@@ -65,16 +65,6 @@ namespace love
|
||||
bool own;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Used to store constants in arrays.
|
||||
**/
|
||||
struct Constant
|
||||
{
|
||||
const char * name;
|
||||
int value;
|
||||
};
|
||||
|
||||
/**
|
||||
* A Module with Lua wrapper functions and other data.
|
||||
**/
|
||||
@@ -95,8 +85,6 @@ namespace love
|
||||
// A list of functions which expose the types of the modules (last element 0).
|
||||
const lua_CFunction * types;
|
||||
|
||||
// A list of constants (last element 0).
|
||||
const Constant * constants;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user