Browse Source

Merged default into minor

--HG--
branch : minor
Alex Szpakowski 10 years ago
parent
commit
ad1fbbf3ea

+ 55 - 82
src/common/Object.h

@@ -68,99 +68,72 @@ public:
 	 **/
 	virtual void release();
 
-	/**
-	 * Meant to be used as a temporary object. Facilitates safer and cleaner
-	 * code to release objects by doing so when the AutoRelease object is
-	 * destroyed (e.g. goes out of scope.)
-	 **/
-	class AutoRelease
+private:
+
+	// The reference count.
+	std::atomic<int> count;
+
+}; // Object
+
+/**
+ * Partial re-implementation + specialization of std::shared_ptr. We can't
+ * use C++11's stdlib yet...
+ **/
+template <typename T>
+class StrongRef
+{
+public:
+
+	StrongRef()
+	: object(nullptr)
 	{
-	public:
+	}
 
-		AutoRelease(Object *obj)
-			: object(obj)
-		{
-		}
+	StrongRef(T *obj)
+	: object(obj)
+	{
+		if (object) object->retain();
+	}
 
-		~AutoRelease()
-		{
-			if (object)
-				object->release();
-		}
+	StrongRef(const StrongRef &other)
+	: object(other.get())
+	{
+		if (object) object->retain();
+	}
 
-	private:
+	~StrongRef()
+	{
+		if (object) object->release();
+	}
 
-		AutoRelease() {}
-		Object *object;
+	StrongRef &operator = (const StrongRef &other)
+	{
+		set(other.get());
+		return *this;
+	}
 
-	}; // AutoRelease
+	T *operator->() const
+	{
+		return object;
+	}
 
-	/**
-	 * Partial re-implementation + specialization of std::shared_ptr. We can't
-	 * use C++11's stdlib yet...
-	 **/
-	template <typename T>
-	class StrongRef
+	void set(T *obj)
 	{
-	public:
-
-		StrongRef()
-			: object(nullptr)
-		{
-		}
-
-		StrongRef(T *obj)
-			: object(obj)
-		{
-			if (object) object->retain();
-		}
-
-		StrongRef(const StrongRef &other)
-			: object(other.get())
-		{
-			if (object) object->retain();
-		}
-
-		~StrongRef()
-		{
-			if (object) object->release();
-		}
-
-		StrongRef &operator = (const StrongRef &other)
-		{
-			set(other.get());
-			return *this;
-		}
-
-		T *operator->() const
-		{
-			return object;
-		}
-
-		void set(T *obj)
-		{
-			if (obj) obj->retain();
-			if (object) object->release();
-			object = obj;
-		}
-
-		T *get() const
-		{
-			return object;
-		}
-
-	private:
-
-		T *object;
-
-	}; // StrongRef
+		if (obj) obj->retain();
+		if (object) object->release();
+		object = obj;
+	}
 
-private:
+	T *get() const
+	{
+		return object;
+	}
 
-	// The reference count.
-	std::atomic<int> count;
+private:
 
-}; // Object
+	T *object;
+	
+}; // StrongRef
 
 } // love
 

+ 2 - 1
src/common/version.h

@@ -25,10 +25,11 @@ namespace love
 {
 
 // Version stuff.
+#define LOVE_VERSION_STRING "0.10.0"
 const int VERSION_MAJOR = 0;
 const int VERSION_MINOR = 10;
 const int VERSION_REV = 0;
-const char *VERSION = "0.10.0";
+const char *VERSION = LOVE_VERSION_STRING;
 const char *VERSION_COMPATIBILITY[] =  { VERSION, 0 };
 const char *VERSION_CODENAME = "";
 

+ 2 - 2
src/love.cpp

@@ -151,10 +151,10 @@ int main(int argc, char **argv)
 	argv = hack_argv;
 #endif // LOVE_LEGENDARY_APP_ARGV_HACK
 
-	if (strcmp(love::VERSION, love_version()) != 0)
+	if (strcmp(LOVE_VERSION_STRING, love_version()) != 0)
 	{
 		printf("Version mismatch detected!\nLOVE binary is version %s\n"
-				"LOVE library is version %s\n", love::VERSION, love_version());
+				"LOVE library is version %s\n", LOVE_VERSION_STRING, love_version());
 		return 1;
 	}
 

+ 2 - 2
src/modules/audio/openal/Source.h

@@ -147,7 +147,7 @@ private:
 	static const unsigned int MAX_BUFFERS = 8;
 	ALuint streamBuffers[MAX_BUFFERS];
 
-	Object::StrongRef<StaticDataBuffer> staticBuffer;
+	StrongRef<StaticDataBuffer> staticBuffer;
 
 	float pitch;
 	float volume;
@@ -176,7 +176,7 @@ private:
 	int sampleRate;
 	int channels;
 
-	Object::StrongRef<love::sound::Decoder> decoder;
+	StrongRef<love::sound::Decoder> decoder;
 
 	unsigned int toLoop;
 

+ 1 - 1
src/modules/font/ImageRasterizer.h

@@ -53,7 +53,7 @@ private:
 	void load();
 
 	// The image data
-	Object::StrongRef<love::image::ImageData> imageData;
+	StrongRef<love::image::ImageData> imageData;
 
 	// The glyphs in the font
 	uint32 *glyphs;

+ 14 - 5
src/modules/font/freetype/TrueTypeRasterizer.cpp

@@ -33,6 +33,9 @@ namespace freetype
 TrueTypeRasterizer::TrueTypeRasterizer(FT_Library library, love::filesystem::FileData *data, int size)
 	: data(data)
 {
+	if (size <= 0)
+		throw love::Exception("Invalid TrueType font size: %d", size);
+
 	FT_Error err = FT_Err_Ok;
 	err = FT_New_Memory_Face(library,
 	                         (const FT_Byte *)data->getData(), /* first byte in memory */
@@ -41,9 +44,12 @@ TrueTypeRasterizer::TrueTypeRasterizer(FT_Library library, love::filesystem::Fil
 	                         &face);
 
 	if (err != FT_Err_Ok)
-		throw love::Exception("TrueType Font Loading error: FT_New_Face failed: 0x%x (problem with font file?)", err);
+		throw love::Exception("TrueType Font loading error: FT_New_Face failed: 0x%x (problem with font file?)", err);
+
+	err = FT_Set_Pixel_Sizes(face, size, size);
 
-	FT_Set_Pixel_Sizes(face, size, size);
+	if (err != FT_Err_Ok)
+		throw love::Exception("TrueType Font loading error: FT_Set_Pixel_Sizes failed: 0x%x (invalid size?)", err);
 
 	// Set global metrics
 	FT_Size_Metrics s = face->size->metrics;
@@ -74,14 +80,17 @@ GlyphData *TrueTypeRasterizer::getGlyphData(uint32 glyph) const
 	err = FT_Load_Glyph(face, FT_Get_Char_Index(face, glyph), FT_LOAD_DEFAULT);
 
 	if (err != FT_Err_Ok)
-		throw love::Exception("TrueType Font Loading error: FT_Load_Glyph failed (0x%x)", err);
+		throw love::Exception("TrueType Font glyph error: FT_Load_Glyph failed (0x%x)", err);
 
 	err = FT_Get_Glyph(face->glyph, &ftglyph);
 
 	if (err != FT_Err_Ok)
-		throw love::Exception("TrueType Font Loading error: FT_Get_Glyph failed (0x%x)", err);
+		throw love::Exception("TrueType Font glyph error: FT_Get_Glyph failed (0x%x)", err);
 
-	FT_Glyph_To_Bitmap(&ftglyph, FT_RENDER_MODE_NORMAL, 0, 1);
+	err = FT_Glyph_To_Bitmap(&ftglyph, FT_RENDER_MODE_NORMAL, 0, 1);
+
+	if (err != FT_Err_Ok)
+		throw love::Exception("TrueType Font glyph error: FT_Glyph_To_Bitmap failed (0x%x)", err);
 
 	FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph) ftglyph;
 	FT_Bitmap &bitmap = bitmap_glyph->bitmap; //just to make things easier

+ 18 - 0
src/modules/graphics/Graphics.h

@@ -137,6 +137,24 @@ public:
 		size_t textureMemory;
 	};
 
+	struct ColorMask
+	{
+		bool r;
+		bool g;
+		bool b;
+		bool a;
+
+		bool operator == (const ColorMask &m) const
+		{
+			return r == m.r && g == m.g && b == m.b && a == m.a;
+		}
+
+		bool operator != (const ColorMask &m) const
+		{
+			return !(operator == (m));
+		}
+	};
+
 	virtual ~Graphics();
 
 	// Implements Module.

+ 1 - 1
src/modules/graphics/opengl/Font.h

@@ -211,7 +211,7 @@ private:
 	const Glyph &findGlyph(uint32 glyph);
 	void printv(const Matrix &t, const std::vector<DrawCommand> &drawcommands, const std::vector<GlyphVertex> &vertices);
 
-	Object::StrongRef<love::font::Rasterizer> rasterizer;
+	StrongRef<love::font::Rasterizer> rasterizer;
 
 	int height;
 	float lineHeight;

+ 16 - 29
src/modules/graphics/opengl/Graphics.cpp

@@ -160,14 +160,8 @@ void Graphics::restoreStateChecked(const DisplayState &s)
 		}
 	}
 
-	for (int i = 0; i < 4; i++)
-	{
-		if (s.colorMask[i] != cur.colorMask[i])
-		{
-			setColorMask(s.colorMask);
-			break;
-		}
-	}
+	if (s.colorMask != cur.colorMask)
+		setColorMask(s.colorMask);
 
 	if (s.wireframe != cur.wireframe)
 		setWireframe(s.wireframe);
@@ -186,7 +180,7 @@ void Graphics::setViewportSize(int width, int height)
 
 	// We want to affect the main screen, not any Canvas that's currently active
 	// (not that any *should* be active when this is called.)
-	std::vector<Object::StrongRef<Canvas>> canvases = states.back().canvases;
+	std::vector<StrongRef<Canvas>> canvases = states.back().canvases;
 	setCanvas();
 
 	// Set the viewport to top-left corner.
@@ -391,7 +385,7 @@ void Graphics::clear(ClearType type)
 void Graphics::present()
 {
 	// Make sure we don't have a canvas active.
-	std::vector<Object::StrongRef<Canvas>> canvases = states.back().canvases;
+	std::vector<StrongRef<Canvas>> canvases = states.back().canvases;
 	setCanvas();
 
 	if (GLAD_ES_VERSION_3_0 || GLAD_EXT_discard_framebuffer)
@@ -761,7 +755,7 @@ void Graphics::setCanvas(Canvas *canvas)
 
 	canvas->startGrab();
 
-	std::vector<Object::StrongRef<Canvas>> canvasref;
+	std::vector<StrongRef<Canvas>> canvasref;
 	canvasref.push_back(canvas);
 
 	std::swap(state.canvases, canvasref);
@@ -779,7 +773,7 @@ void Graphics::setCanvas(const std::vector<Canvas *> &canvases)
 	auto attachments = std::vector<Canvas *>(canvases.begin() + 1, canvases.end());
 	canvases[0]->startGrab(attachments);
 
-	std::vector<Object::StrongRef<Canvas>> canvasrefs;
+	std::vector<StrongRef<Canvas>> canvasrefs;
 	canvasrefs.reserve(canvases.size());
 
 	for (Canvas *c : canvases)
@@ -788,12 +782,12 @@ void Graphics::setCanvas(const std::vector<Canvas *> &canvases)
 	std::swap(state.canvases, canvasrefs);
 }
 
-void Graphics::setCanvas(const std::vector<Object::StrongRef<Canvas>> &canvases)
+void Graphics::setCanvas(const std::vector<StrongRef<Canvas>> &canvases)
 {
 	std::vector<Canvas *> canvaslist;
 	canvaslist.reserve(canvases.size());
 
-	for (const Object::StrongRef<Canvas> &c : canvases)
+	for (const StrongRef<Canvas> &c : canvases)
 		canvaslist.push_back(c.get());
 
 	return setCanvas(canvaslist);
@@ -814,21 +808,18 @@ std::vector<Canvas *> Graphics::getCanvas() const
 	std::vector<Canvas *> canvases;
 	canvases.reserve(states.back().canvases.size());
 
-	for (const Object::StrongRef<Canvas> &c : states.back().canvases)
+	for (const StrongRef<Canvas> &c : states.back().canvases)
 		canvases.push_back(c.get());
 
 	return canvases;
 }
 
-void Graphics::setColorMask(const bool mask[4])
+void Graphics::setColorMask(ColorMask mask)
 {
-	for (int i = 0; i < 4; i++)
-		states.back().colorMask[i] = mask[i];
-
-	glColorMask(mask[0], mask[1], mask[2], mask[3]);
+	glColorMask(mask.r, mask.g, mask.b, mask.a);
 }
 
-const bool *Graphics::getColorMask() const
+Graphics::ColorMask Graphics::getColorMask() const
 {
 	return states.back().colorMask;
 }
@@ -1118,7 +1109,7 @@ love::image::ImageData *Graphics::newScreenshot(love::image::Image *image, bool
 {
 	// Temporarily unbind the currently active canvas (glReadPixels reads the
 	// active framebuffer, not the main one.)
-	std::vector<Object::StrongRef<Canvas>> canvases = states.back().canvases;
+	std::vector<StrongRef<Canvas>> canvases = states.back().canvases;
 	setCanvas();
 
 	int w = getWidth();
@@ -1352,14 +1343,12 @@ Graphics::DisplayState::DisplayState()
 	, stencilInvert(false)
 	, font(nullptr)
 	, shader(nullptr)
+	, colorMask({true, true, true, true})
 	, wireframe(false)
 	, defaultFilter()
 	, defaultMipmapFilter(Texture::FILTER_NEAREST)
 	, defaultMipmapSharpness(0.0f)
 {
-	// We should just directly initialize the array in the initializer list, but
-	// that feature of C++11 is broken in Visual Studio 2013...
-	colorMask[0] = colorMask[1] = colorMask[2] = colorMask[3] = true;
 }
 
 Graphics::DisplayState::DisplayState(const DisplayState &other)
@@ -1377,13 +1366,12 @@ Graphics::DisplayState::DisplayState(const DisplayState &other)
 	, font(other.font)
 	, shader(other.shader)
 	, canvases(other.canvases)
+	, colorMask(other.colorMask)
 	, wireframe(other.wireframe)
 	, defaultFilter(other.defaultFilter)
 	, defaultMipmapFilter(other.defaultMipmapFilter)
 	, defaultMipmapSharpness(other.defaultMipmapSharpness)
 {
-	for (int i = 0; i < 4; i++)
-		colorMask[i] = other.colorMask[i];
 }
 
 Graphics::DisplayState::~DisplayState()
@@ -1408,8 +1396,7 @@ Graphics::DisplayState &Graphics::DisplayState::operator = (const DisplayState &
 	shader = other.shader;
 	canvases = other.canvases;
 
-	for (int i = 0; i < 4; i++)
-		colorMask[i] = other.colorMask[i];
+	colorMask = other.colorMask;
 
 	wireframe = other.wireframe;
 

+ 7 - 16
src/modules/graphics/opengl/Graphics.h

@@ -199,7 +199,7 @@ public:
 
 	void setCanvas(Canvas *canvas);
 	void setCanvas(const std::vector<Canvas *> &canvases);
-	void setCanvas(const std::vector<Object::StrongRef<Canvas>> &canvases);
+	void setCanvas(const std::vector<StrongRef<Canvas>> &canvases);
 	void setCanvas();
 
 	std::vector<Canvas *> getCanvas() const;
@@ -207,13 +207,12 @@ public:
 	/**
 	 * Sets the enabled color components when rendering.
 	 **/
-	void setColorMask(const bool mask[4]);
+	void setColorMask(ColorMask mask);
 
 	/**
 	 * Gets the current color mask.
-	 * Returns an array of 4 booleans representing the mask.
 	 **/
-	const bool *getColorMask() const;
+	ColorMask getColorMask() const;
 
 	/**
 	 * Sets the current blend mode.
@@ -425,22 +424,17 @@ private:
 
 	struct DisplayState
 	{
-		// Colors.
 		Color color;
 		Color backgroundColor;
 
-		// Blend mode.
 		BlendMode blendMode;
 
-		// Line.
 		float lineWidth;
 		LineStyle lineStyle;
 		LineJoin lineJoin;
 
-		// Point.
 		float pointSize;
 
-		// Scissor.
 		bool scissor;
 		OpenGL::Viewport scissorBox;
 
@@ -448,20 +442,17 @@ private:
 		bool stencilTest;
 		bool stencilInvert;
 
-		Object::StrongRef<Font> font;
-		Object::StrongRef<Shader> shader;
+		StrongRef<Font> font;
+		StrongRef<Shader> shader;
 
-		std::vector<Object::StrongRef<Canvas>> canvases;
+		std::vector<StrongRef<Canvas>> canvases;
 
-		// Color mask.
-		bool colorMask[4];
+		ColorMask colorMask;
 
 		bool wireframe;
 
-		// Default filter.
 		Texture::Filter defaultFilter;
 
-		// Default mipmap filter and sharpness.
 		Texture::FilterMode defaultMipmapFilter;
 		float defaultMipmapSharpness;
 

+ 2 - 2
src/modules/graphics/opengl/Image.h

@@ -164,11 +164,11 @@ private:
 
 	// The ImageData from which the texture is created. May be null if
 	// Compressed image data was used to create the texture.
-	Object::StrongRef<love::image::ImageData> data;
+	StrongRef<love::image::ImageData> data;
 
 	// Or the Compressed Image Data from which the texture is created. May be
 	// null if raw ImageData was used to create the texture.
-	Object::StrongRef<love::image::CompressedData> cdata;
+	StrongRef<love::image::CompressedData> cdata;
 
 	// OpenGL texture identifier.
 	GLuint texture;

+ 1 - 1
src/modules/graphics/opengl/Mesh.h

@@ -178,7 +178,7 @@ private:
 	int range_min;
 	int range_max;
 
-	Object::StrongRef<Texture> texture;
+	StrongRef<Texture> texture;
 
 	// Whether the per-vertex colors are used when drawing.
 	bool colors_enabled;

+ 1 - 1
src/modules/graphics/opengl/ParticleSystem.cpp

@@ -742,7 +742,7 @@ std::vector<Quad *> ParticleSystem::getQuads() const
 	std::vector<Quad *> quadlist;
 	quadlist.reserve(quads.size());
 
-	for (const Object::StrongRef<Quad> &q : quads)
+	for (const StrongRef<Quad> &q : quads)
 		quadlist.push_back(q.get());
 
 	return quadlist;

+ 2 - 2
src/modules/graphics/opengl/ParticleSystem.h

@@ -580,7 +580,7 @@ protected:
 	VertexIndex ibo;
 
 	// The texture to be drawn.
-	Object::StrongRef<Texture> texture;
+	StrongRef<Texture> texture;
 
 	// Whether the particle emitter is active.
 	bool active;
@@ -660,7 +660,7 @@ protected:
 	std::vector<Colorf> colors;
 
 	// Quads.
-	std::vector<Object::StrongRef<Quad>> quads;
+	std::vector<StrongRef<Quad>> quads;
 
 	bool relativeRotation;
 

+ 1 - 1
src/modules/graphics/opengl/SpriteBatch.h

@@ -124,7 +124,7 @@ private:
 	 */
 	void setColorv(Vertex *v, const Color &color);
 
-	Object::StrongRef<Texture> texture;
+	StrongRef<Texture> texture;
 
 	// Max number of sprites in the batch.
 	int size;

+ 11 - 7
src/modules/graphics/opengl/wrap_Graphics.cpp

@@ -675,17 +675,19 @@ int w_getFont(lua_State *L)
 
 int w_setColorMask(lua_State *L)
 {
-	bool mask[4];
+	Graphics::ColorMask mask;
 
 	if (lua_gettop(L) <= 1 && lua_isnoneornil(L, 1))
 	{
 		// Enable all color components if no argument is given.
-		mask[0] = mask[1] = mask[2] = mask[3] = true;
+		mask.r = mask.g = mask.b = mask.a = true;
 	}
 	else
 	{
-		for (int i = 0; i < 4; i++)
-			mask[i] = luax_toboolean(L, i + 1);
+		mask.r = luax_toboolean(L, 1);
+		mask.g = luax_toboolean(L, 2);
+		mask.b = luax_toboolean(L, 3);
+		mask.a = luax_toboolean(L, 4);
 	}
 
 	instance()->setColorMask(mask);
@@ -695,10 +697,12 @@ int w_setColorMask(lua_State *L)
 
 int w_getColorMask(lua_State *L)
 {
-	const bool *mask = instance()->getColorMask();
+	Graphics::ColorMask mask = instance()->getColorMask();
 
-	for (int i = 0; i < 4; i++)
-		luax_pushboolean(L, mask[i]);
+	luax_pushboolean(L, mask.r);
+	luax_pushboolean(L, mask.g);
+	luax_pushboolean(L, mask.b);
+	luax_pushboolean(L, mask.a);
 
 	return 4;
 }

+ 3 - 1
src/modules/love/love.cpp

@@ -165,7 +165,9 @@ int w__openConsole(lua_State *L);
 
 const char *love_version()
 {
-	return love::VERSION;
+	// Do not refer to love::VERSION here, the linker
+	// will patch it back up to the executable's one..
+	return LOVE_VERSION_STRING;
 }
 
 const char *love_codename()

+ 1 - 1
src/modules/mouse/sdl/Mouse.h

@@ -69,7 +69,7 @@ public:
 
 private:
 
-	Object::StrongRef<love::mouse::Cursor> curCursor;
+	StrongRef<love::mouse::Cursor> curCursor;
 
 	std::map<Cursor::SystemCursor, Cursor *> systemCursors;
 

+ 1 - 1
src/modules/physics/box2d/Body.h

@@ -441,7 +441,7 @@ private:
 	//
 	// This ensures that a World only can be destroyed
 	// once all bodies have been destroyed too.
-	Object::StrongRef<World> world;
+	StrongRef<World> world;
 
 	bodyudata *udata;
 

+ 1 - 1
src/modules/sound/lullaby/Decoder.h

@@ -53,7 +53,7 @@ protected:
 
 	// The encoded data. This should be replaced with buffered file
 	// reads in the future.
-	Object::StrongRef<Data> data;
+	StrongRef<Data> data;
 
 	// File extension.
 	std::string ext;

+ 1 - 1
src/modules/thread/LuaThread.h

@@ -50,7 +50,7 @@ private:
 
 	void onError();
 
-	Object::StrongRef<love::Data> code;
+	StrongRef<love::Data> code;
 	std::string name;
 	std::string error;
 

+ 1 - 1
src/modules/window/sdl/Window.h

@@ -128,7 +128,7 @@ private:
 		int width  = 800;
 		int height = 600;
 		WindowSettings settings;
-		Object::StrongRef<love::image::ImageData> icon;
+		StrongRef<love::image::ImageData> icon;
 
 	} curMode;