From 81c38e22d094b9d27b48c3fc778c4075e8e7efa9 Mon Sep 17 00:00:00 2001 From: rude Date: Thu, 28 Jun 2012 23:52:33 +0200 Subject: [PATCH] Applied the new style guidelines. Well, sort of. Lot's more to be done, but this is a start. --- src/common/Data.h | 114 +- src/common/EnumMap.h | 199 +- src/common/Exception.cpp | 127 +- src/common/Exception.h | 131 +- src/common/Matrix.cpp | 394 +-- src/common/Matrix.h | 333 +- src/common/Memoizer.cpp | 89 +- src/common/Memoizer.h | 92 +- src/common/Module.h | 110 +- src/common/Object.cpp | 104 +- src/common/Object.h | 152 +- src/common/Reference.cpp | 163 +- src/common/Reference.h | 175 +- src/common/StringMap.h | 335 +- src/common/Variant.cpp | 348 +-- src/common/Variant.h | 149 +- src/common/Vector.cpp | 50 +- src/common/Vector.h | 621 ++-- src/common/b64.cpp | 158 +- src/common/b64.h | 78 +- src/common/config.h | 186 +- src/common/delay.cpp | 83 +- src/common/delay.h | 66 +- src/common/int.h | 129 +- src/common/math.h | 170 +- src/common/runtime.cpp | 961 +++--- src/common/runtime.h | 767 ++--- src/common/types.h | 347 +-- src/common/utf8.cpp | 121 +- src/common/utf8.h | 93 +- src/common/version.h | 73 +- src/common/wrap_Data.cpp | 114 +- src/common/wrap_Data.h | 75 +- src/launcher.cpp | 68 +- src/love.cpp | 726 ++--- src/modules/audio/Audio.cpp | 103 +- src/modules/audio/Audio.h | 473 +-- src/modules/audio/Source.cpp | 147 +- src/modules/audio/Source.h | 238 +- src/modules/audio/null/Audio.cpp | 335 +- src/modules/audio/null/Audio.h | 176 +- src/modules/audio/null/Source.cpp | 410 +-- src/modules/audio/null/Source.h | 187 +- src/modules/audio/openal/Audio.cpp | 635 ++-- src/modules/audio/openal/Audio.h | 282 +- src/modules/audio/openal/Pool.cpp | 585 ++-- src/modules/audio/openal/Pool.h | 250 +- src/modules/audio/openal/Source.cpp | 1422 ++++----- src/modules/audio/openal/Source.h | 301 +- src/modules/audio/wrap_Audio.cpp | 682 +++-- src/modules/audio/wrap_Audio.h | 121 +- src/modules/audio/wrap_Source.cpp | 638 ++-- src/modules/audio/wrap_Source.h | 131 +- src/modules/event/Event.cpp | 592 ++-- src/modules/event/Event.h | 166 +- src/modules/event/sdl/Event.cpp | 627 ++-- src/modules/event/sdl/Event.h | 163 +- src/modules/event/sdl/wrap_Event.cpp | 298 +- src/modules/event/sdl/wrap_Event.h | 95 +- src/modules/filesystem/File.cpp | 105 +- src/modules/filesystem/File.h | 355 +-- src/modules/filesystem/FileData.cpp | 179 +- src/modules/filesystem/FileData.h | 169 +- src/modules/filesystem/physfs/File.cpp | 483 +-- src/modules/filesystem/physfs/File.h | 177 +- src/modules/filesystem/physfs/Filesystem.cpp | 1215 ++++---- src/modules/filesystem/physfs/Filesystem.h | 594 ++-- src/modules/filesystem/physfs/wrap_File.cpp | 452 +-- src/modules/filesystem/physfs/wrap_File.h | 102 +- .../filesystem/physfs/wrap_FileData.cpp | 140 +- src/modules/filesystem/physfs/wrap_FileData.h | 88 +- .../filesystem/physfs/wrap_Filesystem.cpp | 883 +++--- .../filesystem/physfs/wrap_Filesystem.h | 149 +- src/modules/font/Font.h | 108 +- src/modules/font/GlyphData.cpp | 249 +- src/modules/font/GlyphData.h | 267 +- src/modules/font/ImageRasterizer.cpp | 304 +- src/modules/font/ImageRasterizer.h | 141 +- src/modules/font/Rasterizer.cpp | 106 +- src/modules/font/Rasterizer.h | 193 +- src/modules/font/freetype/Font.cpp | 157 +- src/modules/font/freetype/Font.h | 149 +- .../font/freetype/TrueTypeRasterizer.cpp | 240 +- .../font/freetype/TrueTypeRasterizer.h | 137 +- src/modules/font/freetype/wrap_Font.cpp | 223 +- src/modules/font/freetype/wrap_Font.h | 85 +- src/modules/font/wrap_GlyphData.cpp | 90 +- src/modules/font/wrap_GlyphData.h | 81 +- src/modules/font/wrap_Rasterizer.cpp | 90 +- src/modules/font/wrap_Rasterizer.h | 77 +- src/modules/graphics/Color.h | 212 +- src/modules/graphics/DrawQable.cpp | 66 +- src/modules/graphics/DrawQable.h | 129 +- src/modules/graphics/Drawable.cpp | 66 +- src/modules/graphics/Drawable.h | 127 +- src/modules/graphics/Graphics.cpp | 331 +- src/modules/graphics/Graphics.h | 287 +- src/modules/graphics/Image.cpp | 159 +- src/modules/graphics/Image.h | 170 +- src/modules/graphics/Quad.cpp | 73 +- src/modules/graphics/Quad.h | 132 +- src/modules/graphics/Volatile.cpp | 138 +- src/modules/graphics/Volatile.h | 185 +- src/modules/graphics/opengl/Canvas.cpp | 890 +++--- src/modules/graphics/opengl/Canvas.h | 220 +- src/modules/graphics/opengl/Font.cpp | 763 ++--- src/modules/graphics/opengl/Font.h | 365 ++- src/modules/graphics/opengl/Graphics.cpp | 2301 +++++++------- src/modules/graphics/opengl/Graphics.h | 1072 ++++--- src/modules/graphics/opengl/Image.cpp | 844 ++--- src/modules/graphics/opengl/Image.h | 329 +- src/modules/graphics/opengl/OpenGL.cpp | 113 +- src/modules/graphics/opengl/OpenGL.h | 110 +- .../graphics/opengl/ParticleSystem.cpp | 1073 +++---- src/modules/graphics/opengl/ParticleSystem.h | 897 +++--- src/modules/graphics/opengl/PixelEffect.cpp | 648 ++-- src/modules/graphics/opengl/PixelEffect.h | 160 +- src/modules/graphics/opengl/Quad.cpp | 258 +- src/modules/graphics/opengl/Quad.h | 153 +- src/modules/graphics/opengl/SpriteBatch.cpp | 562 ++-- src/modules/graphics/opengl/SpriteBatch.h | 274 +- src/modules/graphics/opengl/VertexBuffer.cpp | 465 +-- src/modules/graphics/opengl/VertexBuffer.h | 658 ++-- src/modules/graphics/opengl/wrap_Canvas.cpp | 408 +-- src/modules/graphics/opengl/wrap_Canvas.h | 103 +- src/modules/graphics/opengl/wrap_Font.cpp | 218 +- src/modules/graphics/opengl/wrap_Font.h | 93 +- src/modules/graphics/opengl/wrap_Graphics.cpp | 2723 +++++++++-------- src/modules/graphics/opengl/wrap_Graphics.h | 239 +- src/modules/graphics/opengl/wrap_Image.cpp | 270 +- src/modules/graphics/opengl/wrap_Image.h | 89 +- .../graphics/opengl/wrap_ParticleSystem.cpp | 830 ++--- .../graphics/opengl/wrap_ParticleSystem.h | 157 +- .../graphics/opengl/wrap_PixelEffect.cpp | 484 +-- .../graphics/opengl/wrap_PixelEffect.h | 88 +- src/modules/graphics/opengl/wrap_Quad.cpp | 160 +- src/modules/graphics/opengl/wrap_Quad.h | 89 +- .../graphics/opengl/wrap_SpriteBatch.cpp | 366 +-- .../graphics/opengl/wrap_SpriteBatch.h | 101 +- src/modules/image/Image.h | 167 +- src/modules/image/ImageData.cpp | 301 +- src/modules/image/ImageData.h | 301 +- src/modules/image/devil/Image.cpp | 147 +- src/modules/image/devil/Image.h | 107 +- src/modules/image/devil/ImageData.cpp | 454 +-- src/modules/image/devil/ImageData.h | 132 +- src/modules/image/wrap_Image.cpp | 269 +- src/modules/image/wrap_Image.h | 79 +- src/modules/image/wrap_ImageData.cpp | 410 +-- src/modules/image/wrap_ImageData.h | 93 +- src/modules/joystick/Joystick.cpp | 116 +- src/modules/joystick/Joystick.h | 131 +- src/modules/joystick/sdl/Joystick.cpp | 551 ++-- src/modules/joystick/sdl/Joystick.h | 157 +- src/modules/joystick/sdl/wrap_Joystick.cpp | 368 +-- src/modules/joystick/sdl/wrap_Joystick.h | 105 +- src/modules/keyboard/Keyboard.cpp | 377 +-- src/modules/keyboard/Keyboard.h | 453 +-- src/modules/keyboard/sdl/Keyboard.cpp | 445 +-- src/modules/keyboard/sdl/Keyboard.h | 119 +- src/modules/keyboard/wrap_Keyboard.cpp | 219 +- src/modules/keyboard/wrap_Keyboard.h | 78 +- src/modules/mouse/Mouse.cpp | 103 +- src/modules/mouse/Mouse.h | 153 +- src/modules/mouse/sdl/Mouse.cpp | 217 +- src/modules/mouse/sdl/Mouse.h | 115 +- src/modules/mouse/wrap_Mouse.cpp | 330 +- src/modules/mouse/wrap_Mouse.h | 97 +- src/modules/physics/Body.cpp | 103 +- src/modules/physics/Body.h | 119 +- src/modules/physics/Joint.cpp | 117 +- src/modules/physics/Joint.h | 135 +- src/modules/physics/Shape.cpp | 105 +- src/modules/physics/Shape.h | 121 +- src/modules/physics/box2d/Body.cpp | 945 +++--- src/modules/physics/box2d/Body.h | 833 +++-- src/modules/physics/box2d/ChainShape.cpp | 227 +- src/modules/physics/box2d/ChainShape.h | 212 +- src/modules/physics/box2d/CircleShape.cpp | 115 +- src/modules/physics/box2d/CircleShape.h | 143 +- src/modules/physics/box2d/Contact.cpp | 233 +- src/modules/physics/box2d/Contact.h | 275 +- src/modules/physics/box2d/DistanceJoint.cpp | 162 +- src/modules/physics/box2d/DistanceJoint.h | 179 +- src/modules/physics/box2d/EdgeShape.cpp | 121 +- src/modules/physics/box2d/EdgeShape.h | 125 +- src/modules/physics/box2d/Fixture.cpp | 648 ++-- src/modules/physics/box2d/Fixture.h | 450 +-- src/modules/physics/box2d/FrictionJoint.cpp | 148 +- src/modules/physics/box2d/FrictionJoint.h | 156 +- src/modules/physics/box2d/GearJoint.cpp | 128 +- src/modules/physics/box2d/GearJoint.h | 151 +- src/modules/physics/box2d/Joint.cpp | 307 +- src/modules/physics/box2d/Joint.h | 276 +- src/modules/physics/box2d/MouseJoint.cpp | 190 +- src/modules/physics/box2d/MouseJoint.h | 214 +- src/modules/physics/box2d/Physics.cpp | 676 ++-- src/modules/physics/box2d/Physics.h | 702 ++--- src/modules/physics/box2d/PolygonShape.cpp | 123 +- src/modules/physics/box2d/PolygonShape.h | 135 +- src/modules/physics/box2d/PrismaticJoint.cpp | 286 +- src/modules/physics/box2d/PrismaticJoint.h | 294 +- src/modules/physics/box2d/PulleyJoint.cpp | 152 +- src/modules/physics/box2d/PulleyJoint.h | 161 +- src/modules/physics/box2d/RevoluteJoint.cpp | 278 +- src/modules/physics/box2d/RevoluteJoint.h | 294 +- src/modules/physics/box2d/RopeJoint.cpp | 128 +- src/modules/physics/box2d/RopeJoint.h | 125 +- src/modules/physics/box2d/Shape.cpp | 307 +- src/modules/physics/box2d/Shape.h | 164 +- src/modules/physics/box2d/WeldJoint.cpp | 146 +- src/modules/physics/box2d/WeldJoint.h | 160 +- src/modules/physics/box2d/WheelJoint.cpp | 236 +- src/modules/physics/box2d/WheelJoint.h | 254 +- src/modules/physics/box2d/World.cpp | 1096 +++---- src/modules/physics/box2d/World.h | 573 ++-- src/modules/physics/box2d/wrap_Body.cpp | 1200 ++++---- src/modules/physics/box2d/wrap_Body.h | 188 +- src/modules/physics/box2d/wrap_ChainShape.cpp | 260 +- src/modules/physics/box2d/wrap_ChainShape.h | 103 +- .../physics/box2d/wrap_CircleShape.cpp | 142 +- src/modules/physics/box2d/wrap_CircleShape.h | 89 +- src/modules/physics/box2d/wrap_Contact.cpp | 271 +- src/modules/physics/box2d/wrap_Contact.h | 105 +- .../physics/box2d/wrap_DistanceJoint.cpp | 214 +- .../physics/box2d/wrap_DistanceJoint.h | 97 +- src/modules/physics/box2d/wrap_EdgeShape.cpp | 124 +- src/modules/physics/box2d/wrap_EdgeShape.h | 87 +- src/modules/physics/box2d/wrap_Fixture.cpp | 615 ++-- src/modules/physics/box2d/wrap_Fixture.h | 137 +- .../physics/box2d/wrap_FrictionJoint.cpp | 180 +- .../physics/box2d/wrap_FrictionJoint.h | 93 +- src/modules/physics/box2d/wrap_GearJoint.cpp | 146 +- src/modules/physics/box2d/wrap_GearJoint.h | 89 +- src/modules/physics/box2d/wrap_Joint.cpp | 218 +- src/modules/physics/box2d/wrap_Joint.h | 95 +- src/modules/physics/box2d/wrap_MouseJoint.cpp | 250 +- src/modules/physics/box2d/wrap_MouseJoint.h | 101 +- src/modules/physics/box2d/wrap_Physics.cpp | 947 +++--- src/modules/physics/box2d/wrap_Physics.h | 127 +- .../physics/box2d/wrap_PolygonShape.cpp | 124 +- src/modules/physics/box2d/wrap_PolygonShape.h | 87 +- .../physics/box2d/wrap_PrismaticJoint.cpp | 402 +-- .../physics/box2d/wrap_PrismaticJoint.h | 119 +- .../physics/box2d/wrap_PulleyJoint.cpp | 176 +- src/modules/physics/box2d/wrap_PulleyJoint.h | 93 +- .../physics/box2d/wrap_RevoluteJoint.cpp | 402 +-- .../physics/box2d/wrap_RevoluteJoint.h | 119 +- src/modules/physics/box2d/wrap_RopeJoint.cpp | 128 +- src/modules/physics/box2d/wrap_RopeJoint.h | 87 +- src/modules/physics/box2d/wrap_Shape.cpp | 228 +- src/modules/physics/box2d/wrap_Shape.h | 103 +- src/modules/physics/box2d/wrap_WeldJoint.cpp | 180 +- src/modules/physics/box2d/wrap_WeldJoint.h | 97 +- src/modules/physics/box2d/wrap_WheelJoint.cpp | 332 +- src/modules/physics/box2d/wrap_WheelJoint.h | 111 +- src/modules/physics/box2d/wrap_World.cpp | 427 +-- src/modules/physics/box2d/wrap_World.h | 125 +- src/modules/sound/Decoder.h | 284 +- src/modules/sound/Sound.cpp | 88 +- src/modules/sound/Sound.h | 166 +- src/modules/sound/SoundData.cpp | 361 +-- src/modules/sound/SoundData.h | 136 +- src/modules/sound/lullaby/Decoder.cpp | 144 +- src/modules/sound/lullaby/Decoder.h | 158 +- src/modules/sound/lullaby/FLACDecoder.cpp | 367 +-- src/modules/sound/lullaby/FLACDecoder.h | 151 +- src/modules/sound/lullaby/ModPlugDecoder.cpp | 291 +- src/modules/sound/lullaby/ModPlugDecoder.h | 130 +- src/modules/sound/lullaby/Mpg123Decoder.cpp | 473 +-- src/modules/sound/lullaby/Mpg123Decoder.h | 161 +- src/modules/sound/lullaby/Sound.cpp | 157 +- src/modules/sound/lullaby/Sound.h | 137 +- src/modules/sound/lullaby/VorbisDecoder.cpp | 514 ++-- src/modules/sound/lullaby/VorbisDecoder.h | 156 +- src/modules/sound/wrap_Decoder.cpp | 134 +- src/modules/sound/wrap_Decoder.h | 83 +- src/modules/sound/wrap_Sound.cpp | 291 +- src/modules/sound/wrap_Sound.h | 83 +- src/modules/sound/wrap_SoundData.cpp | 186 +- src/modules/sound/wrap_SoundData.h | 91 +- src/modules/thread/Thread.cpp | 634 ++-- src/modules/thread/Thread.h | 253 +- src/modules/thread/posix/threads.cpp | 381 +-- src/modules/thread/posix/threads.h | 215 +- src/modules/thread/sdl/threads.cpp | 321 +- src/modules/thread/sdl/threads.h | 212 +- src/modules/thread/threads.cpp | 94 +- src/modules/thread/threads.h | 172 +- src/modules/thread/win32/threads.cpp | 422 +-- src/modules/thread/win32/threads.h | 217 +- src/modules/thread/wrap_Thread.cpp | 575 ++-- src/modules/thread/wrap_Thread.h | 107 +- src/modules/timer/Timer.h | 171 +- src/modules/timer/sdl/Timer.cpp | 271 +- src/modules/timer/sdl/Timer.h | 181 +- src/modules/timer/wrap_Timer.cpp | 222 +- src/modules/timer/wrap_Timer.h | 85 +- src/modules/window/Window.cpp | 82 +- src/modules/window/Window.h | 158 +- src/modules/window/sdl/Window.cpp | 614 ++-- src/modules/window/sdl/Window.h | 166 +- 302 files changed, 42648 insertions(+), 42081 deletions(-) diff --git a/src/common/Data.h b/src/common/Data.h index 36a0b1edd..33da435f8 100644 --- a/src/common/Data.h +++ b/src/common/Data.h @@ -1,56 +1,58 @@ -/** -* Copyright (c) 2006-2012 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_DATA_H -#define LOVE_DATA_H - -// LOVE -#include "config.h" -#include "Object.h" - -namespace love -{ - /** - * This class is a simple abstraction over all objects which contain data. - **/ - class Data : public Object - { - public: - - /** - * Destructor. - **/ - virtual ~Data() {}; - - /** - * Gets a pointer to the data. This pointer will obviously not - * be valid if the Data object is destroyed. - **/ - virtual void * getData() const = 0 ; - - /** - * Gets the size of the Data in bytes. - **/ - virtual int getSize() const = 0; - - }; // Data -} // love - -#endif // LOVE_DATA_H +/** + * Copyright (c) 2006-2012 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_DATA_H +#define LOVE_DATA_H + +// LOVE +#include "config.h" +#include "Object.h" + +namespace love +{ + +/** + * This class is a simple abstraction over all objects which contain data. + **/ +class Data : public Object +{ +public: + + /** + * Destructor. + **/ + virtual ~Data() {}; + + /** + * Gets a pointer to the data. This pointer will obviously not + * be valid if the Data object is destroyed. + **/ + virtual void *getData() const = 0 ; + + /** + * Gets the size of the Data in bytes. + **/ + virtual int getSize() const = 0; + +}; // Data + +} // love + +#endif // LOVE_DATA_H diff --git a/src/common/EnumMap.h b/src/common/EnumMap.h index a1394fc53..8ac652d36 100644 --- a/src/common/EnumMap.h +++ b/src/common/EnumMap.h @@ -1,99 +1,100 @@ -/** -* Copyright (c) 2006-2012 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 - 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 +class EnumMap +{ +public: + + struct Entry + { + T t; + U u; + }; + + EnumMap(Entry *entries, unsigned size) + { + unsigned n = size/sizeof(Entry); + + for (unsigned i = 0; i -#include -using namespace std; - -namespace love -{ - Exception::Exception(const char * fmt, ...) - { - va_list args; - int size_buffer = 256, size_out; - char * buffer; - while (true) - { - buffer = new char[size_buffer]; - memset(buffer, 0, size_buffer); - - va_start(args, fmt); - size_out = vsnprintf(buffer, size_buffer, fmt, args); - va_end(args); - - // see http://perfec.to/vsnprintf/pasprintf.c - // if size_out ... - // == -1 --> output was truncated - // == size_buffer --> output was truncated - // == size_buffer-1 --> ambiguous, /may/ have been truncated - // > size_buffer --> output was truncated, and size_out - // bytes would have been written - if (size_out == size_buffer || size_out == -1 || size_out == size_buffer-1) - size_buffer *= 2; - else if (size_out > size_buffer) - size_buffer = size_out + 2; // to avoid the ambiguous case - else - break; - - delete[] buffer; - } - message = std::string(buffer); - delete[] buffer; - } - -} +/** + * Copyright (c) 2006-2012 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. + **/ + +#include "Exception.h" +#include "common/config.h" + +#include + +using namespace std; + +namespace love +{ + +Exception::Exception(const char *fmt, ...) +{ + va_list args; + int size_buffer = 256, size_out; + char *buffer; + while (true) + { + buffer = new char[size_buffer]; + memset(buffer, 0, size_buffer); + + va_start(args, fmt); + size_out = vsnprintf(buffer, size_buffer, fmt, args); + va_end(args); + + // see http://perfec.to/vsnprintf/pasprintf.c + // if size_out ... + // == -1 --> output was truncated + // == size_buffer --> output was truncated + // == size_buffer-1 --> ambiguous, /may/ have been truncated + // > size_buffer --> output was truncated, and size_out + // bytes would have been written + if (size_out == size_buffer || size_out == -1 || size_out == size_buffer-1) + size_buffer *= 2; + else if (size_out > size_buffer) + size_buffer = size_out + 2; // to avoid the ambiguous case + else + break; + + delete[] buffer; + } + message = std::string(buffer); + delete[] buffer; +} + +} diff --git a/src/common/Exception.h b/src/common/Exception.h index 9098f22f7..34493919f 100644 --- a/src/common/Exception.h +++ b/src/common/Exception.h @@ -1,64 +1,67 @@ -/** -* Copyright (c) 2006-2012 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_EXCEPTION_H -#define LOVE_EXCEPTION_H - -#include -#include // vararg -#include // vsnprintf -#include // strncpy -#include - -namespace love -{ - /** - * A convenient vararg-enabled exception class. - **/ - class Exception : public std::exception - { - private: - - std::string message; - - public: - - /** - * Creates a new Exception according to printf-rules. - * - * See: http://www.cplusplus.com/reference/clibrary/cstdio/printf/ - * - * @param fmt The format string (see printf). - **/ - Exception(const char * fmt, ...); - virtual ~Exception() throw() {} - - /** - * Returns a string containing reason for the exception. - * @return A description of the exception. - **/ - inline virtual const char * what() const throw() - { return message.c_str(); } - - }; // class - -} // love - -#endif // LOVE_EXCEPTION_H +/** + * Copyright (c) 2006-2012 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_EXCEPTION_H +#define LOVE_EXCEPTION_H + +#include +#include // vararg +#include // vsnprintf +#include // strncpy +#include + +namespace love +{ + +/** + * A convenient vararg-enabled exception class. + **/ +class Exception : public std::exception +{ +public: + + /** + * Creates a new Exception according to printf-rules. + * + * See: http://www.cplusplus.com/reference/clibrary/cstdio/printf/ + * + * @param fmt The format string (see printf). + **/ + Exception(const char *fmt, ...); + virtual ~Exception() throw() {} + + /** + * Returns a string containing reason for the exception. + * @return A description of the exception. + **/ + inline virtual const char *what() const throw() + { + return message.c_str(); + } + +private: + + std::string message; + +}; // Exception + +} // love + +#endif // LOVE_EXCEPTION_H diff --git a/src/common/Matrix.cpp b/src/common/Matrix.cpp index 9296540dc..1547ae484 100644 --- a/src/common/Matrix.cpp +++ b/src/common/Matrix.cpp @@ -1,196 +1,198 @@ -/** -* Copyright (c) 2006-2012 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. -**/ - -#include "Matrix.h" - -// STD -#include // memcpy -#include - -namespace love -{ - - // | e0 e4 e8 e12 | - // | e1 e5 e9 e13 | - // | e2 e6 e10 e14 | - // | e3 e7 e11 e15 | - - Matrix::Matrix() - { - setIdentity(); - } - - Matrix::~Matrix() - { - } - - // | e0 e4 e8 e12 | - // | e1 e5 e9 e13 | - // | e2 e6 e10 e14 | - // | e3 e7 e11 e15 | - // | e0 e4 e8 e12 | - // | e1 e5 e9 e13 | - // | e2 e6 e10 e14 | - // | e3 e7 e11 e15 | - - Matrix Matrix::operator * (const Matrix & m) const - { - Matrix t; - - t.e[0] = (e[0]*m.e[0]) + (e[4]*m.e[1]) + (e[8]*m.e[2]) + (e[12]*m.e[3]); - t.e[4] = (e[0]*m.e[4]) + (e[4]*m.e[5]) + (e[8]*m.e[6]) + (e[12]*m.e[7]); - t.e[8] = (e[0]*m.e[8]) + (e[4]*m.e[9]) + (e[8]*m.e[10]) + (e[12]*m.e[11]); - t.e[12] = (e[0]*m.e[12]) + (e[4]*m.e[13]) + (e[8]*m.e[14]) + (e[12]*m.e[15]); - - t.e[1] = (e[1]*m.e[0]) + (e[5]*m.e[1]) + (e[9]*m.e[2]) + (e[13]*m.e[3]); - t.e[5] = (e[1]*m.e[4]) + (e[5]*m.e[5]) + (e[9]*m.e[6]) + (e[13]*m.e[7]); - t.e[9] = (e[1]*m.e[8]) + (e[5]*m.e[9]) + (e[9]*m.e[10]) + (e[13]*m.e[11]); - t.e[13] = (e[1]*m.e[12]) + (e[5]*m.e[13]) + (e[9]*m.e[14]) + (e[13]*m.e[15]); - - t.e[2] = (e[2]*m.e[0]) + (e[6]*m.e[1]) + (e[10]*m.e[2]) + (e[14]*m.e[3]); - t.e[6] = (e[2]*m.e[4]) + (e[6]*m.e[5]) + (e[10]*m.e[6]) + (e[14]*m.e[7]); - t.e[10] = (e[2]*m.e[8]) + (e[6]*m.e[9]) + (e[10]*m.e[10]) + (e[14]*m.e[11]); - t.e[14] = (e[2]*m.e[12]) + (e[6]*m.e[13]) + (e[10]*m.e[14]) + (e[14]*m.e[15]); - - t.e[3] = (e[3]*m.e[0]) + (e[7]*m.e[1]) + (e[11]*m.e[2]) + (e[15]*m.e[3]); - t.e[7] = (e[3]*m.e[4]) + (e[7]*m.e[5]) + (e[11]*m.e[6]) + (e[15]*m.e[7]); - t.e[11] = (e[3]*m.e[8]) + (e[7]*m.e[9]) + (e[11]*m.e[10]) + (e[15]*m.e[11]); - t.e[15] = (e[3]*m.e[12]) + (e[7]*m.e[13]) + (e[11]*m.e[14]) + (e[15]*m.e[15]); - - return t; - } - - void Matrix::operator *= (const Matrix & m) - { - Matrix t = (*this) * m; - memcpy((void*)this->e, (void*)t.e, sizeof(float)*16); - } - - const float * Matrix::getElements() const - { - return e; - } - - void Matrix::setIdentity() - { - memset(e, 0, sizeof(float)*16); - e[0] = e[5] = e[10] = e[15] = 1; - } - - void Matrix::setTranslation(float x, float y) - { - setIdentity(); - e[12] = x; - e[13] = y; - } - - void Matrix::setRotation(float rad) - { - setIdentity(); - float c = cos(rad), s = sin(rad); - e[0] = c; e[4] = -s; - e[1] = s; e[5] = c; - } - - void Matrix::setScale(float sx, float sy) - { - setIdentity(); - e[0] = sx; - e[5] = sy; - } - - void Matrix::setShear(float kx, float ky) - { - setIdentity(); - e[1] = ky; - e[4] = kx; - } - - void Matrix::setTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) - { - memset(e, 0, sizeof(float)*16); // zero out matrix - float c = cos(angle), s = sin(angle); - // matrix multiplication carried out on paper: - // |1 x| |c -s | |sx | | 1 ky | |1 -ox| - // | 1 y| |s c | | sy | |kx 1 | | 1 -oy| - // | 1 | | 1 | | 1 | | 1 | | 1 | - // | 1| | 1| | 1| | 1| | 1 | - // move rotate scale skew origin - e[10] = e[15] = 1.0f; - e[0] = c * sx - ky * s * sy; // = a - e[1] = s * sx + ky * c * sy; // = b - e[4] = kx * c * sx - s * sy; // = c - e[5] = kx * s * sx + c * sy; // = d - e[12] = x - ox * e[0] - oy * e[4]; - e[13] = y - ox * e[1] - oy * e[5]; - } - - void Matrix::translate(float x, float y) - { - Matrix t; - t.setTranslation(x, y); - this->operator *=(t); - } - - void Matrix::rotate(float rad) - { - Matrix t; - t.setRotation(rad); - this->operator *=(t); - } - - void Matrix::scale(float sx, float sy) - { - Matrix t; - t.setScale(sx, sy); - this->operator *=(t); - } - - void Matrix::shear(float kx, float ky) - { - Matrix t; - t.setShear(kx,ky); - this->operator *=(t); - } - - // | x | - // | y | - // | 0 | - // | 1 | - // | e0 e4 e8 e12 | - // | e1 e5 e9 e13 | - // | e2 e6 e10 e14 | - // | e3 e7 e11 e15 | - - void Matrix::transform(vertex * dst, const vertex * src, int size) const - { - for (int i = 0;i // memcpy +#include + +namespace love +{ + +// | e0 e4 e8 e12 | +// | e1 e5 e9 e13 | +// | e2 e6 e10 e14 | +// | e3 e7 e11 e15 | + +Matrix::Matrix() +{ + setIdentity(); +} + +Matrix::~Matrix() +{ +} + +// | e0 e4 e8 e12 | +// | e1 e5 e9 e13 | +// | e2 e6 e10 e14 | +// | e3 e7 e11 e15 | +// | e0 e4 e8 e12 | +// | e1 e5 e9 e13 | +// | e2 e6 e10 e14 | +// | e3 e7 e11 e15 | + +Matrix Matrix::operator * (const Matrix &m) const +{ + Matrix t; + + t.e[0] = (e[0]*m.e[0]) + (e[4]*m.e[1]) + (e[8]*m.e[2]) + (e[12]*m.e[3]); + t.e[4] = (e[0]*m.e[4]) + (e[4]*m.e[5]) + (e[8]*m.e[6]) + (e[12]*m.e[7]); + t.e[8] = (e[0]*m.e[8]) + (e[4]*m.e[9]) + (e[8]*m.e[10]) + (e[12]*m.e[11]); + t.e[12] = (e[0]*m.e[12]) + (e[4]*m.e[13]) + (e[8]*m.e[14]) + (e[12]*m.e[15]); + + t.e[1] = (e[1]*m.e[0]) + (e[5]*m.e[1]) + (e[9]*m.e[2]) + (e[13]*m.e[3]); + t.e[5] = (e[1]*m.e[4]) + (e[5]*m.e[5]) + (e[9]*m.e[6]) + (e[13]*m.e[7]); + t.e[9] = (e[1]*m.e[8]) + (e[5]*m.e[9]) + (e[9]*m.e[10]) + (e[13]*m.e[11]); + t.e[13] = (e[1]*m.e[12]) + (e[5]*m.e[13]) + (e[9]*m.e[14]) + (e[13]*m.e[15]); + + t.e[2] = (e[2]*m.e[0]) + (e[6]*m.e[1]) + (e[10]*m.e[2]) + (e[14]*m.e[3]); + t.e[6] = (e[2]*m.e[4]) + (e[6]*m.e[5]) + (e[10]*m.e[6]) + (e[14]*m.e[7]); + t.e[10] = (e[2]*m.e[8]) + (e[6]*m.e[9]) + (e[10]*m.e[10]) + (e[14]*m.e[11]); + t.e[14] = (e[2]*m.e[12]) + (e[6]*m.e[13]) + (e[10]*m.e[14]) + (e[14]*m.e[15]); + + t.e[3] = (e[3]*m.e[0]) + (e[7]*m.e[1]) + (e[11]*m.e[2]) + (e[15]*m.e[3]); + t.e[7] = (e[3]*m.e[4]) + (e[7]*m.e[5]) + (e[11]*m.e[6]) + (e[15]*m.e[7]); + t.e[11] = (e[3]*m.e[8]) + (e[7]*m.e[9]) + (e[11]*m.e[10]) + (e[15]*m.e[11]); + t.e[15] = (e[3]*m.e[12]) + (e[7]*m.e[13]) + (e[11]*m.e[14]) + (e[15]*m.e[15]); + + return t; +} + +void Matrix::operator *= (const Matrix &m) +{ + Matrix t = (*this) * m; + memcpy((void *)this->e, (void *)t.e, sizeof(float)*16); +} + +const float *Matrix::getElements() const +{ + return e; +} + +void Matrix::setIdentity() +{ + memset(e, 0, sizeof(float)*16); + e[0] = e[5] = e[10] = e[15] = 1; +} + +void Matrix::setTranslation(float x, float y) +{ + setIdentity(); + e[12] = x; + e[13] = y; +} + +void Matrix::setRotation(float rad) +{ + setIdentity(); + float c = cos(rad), s = sin(rad); + e[0] = c; + e[4] = -s; + e[1] = s; + e[5] = c; +} + +void Matrix::setScale(float sx, float sy) +{ + setIdentity(); + e[0] = sx; + e[5] = sy; +} + +void Matrix::setShear(float kx, float ky) +{ + setIdentity(); + e[1] = ky; + e[4] = kx; +} + +void Matrix::setTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) +{ + memset(e, 0, sizeof(float)*16); // zero out matrix + float c = cos(angle), s = sin(angle); + // matrix multiplication carried out on paper: + // |1 x| |c -s | |sx | | 1 ky | |1 -ox| + // | 1 y| |s c | | sy | |kx 1 | | 1 -oy| + // | 1 | | 1 | | 1 | | 1 | | 1 | + // | 1| | 1| | 1| | 1| | 1 | + // move rotate scale skew origin + e[10] = e[15] = 1.0f; + e[0] = c * sx - ky * s * sy; // = a + e[1] = s * sx + ky * c * sy; // = b + e[4] = kx * c * sx - s * sy; // = c + e[5] = kx * s * sx + c * sy; // = d + e[12] = x - ox * e[0] - oy * e[4]; + e[13] = y - ox * e[1] - oy * e[5]; +} + +void Matrix::translate(float x, float y) +{ + Matrix t; + t.setTranslation(x, y); + this->operator *=(t); +} + +void Matrix::rotate(float rad) +{ + Matrix t; + t.setRotation(rad); + this->operator *=(t); +} + +void Matrix::scale(float sx, float sy) +{ + Matrix t; + t.setScale(sx, sy); + this->operator *=(t); +} + +void Matrix::shear(float kx, float ky) +{ + Matrix t; + t.setShear(kx,ky); + this->operator *=(t); +} + +// | x | +// | y | +// | 0 | +// | 1 | +// | e0 e4 e8 e12 | +// | e1 e5 e9 e13 | +// | e2 e6 e10 e14 | +// | e3 e7 e11 e15 | + +void Matrix::transform(vertex *dst, const vertex *src, int size) const +{ + for (int i = 0; i - -namespace love -{ - - std::map Memoizer::objectMap; - - void Memoizer::add(void * key, void * val) - { - objectMap[key] = val; - } - - void Memoizer::remove(void * key) - { - objectMap.erase(key); - } - - void * Memoizer::find(void * key) - { - if (objectMap.count(key)) return objectMap[key]; - return NULL; - } - -} // love +/** + * Copyright (c) 2006-2012 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. + **/ + +#include "Memoizer.h" +#include + +namespace love +{ + +std::map Memoizer::objectMap; + +void Memoizer::add(void *key, void *val) +{ + objectMap[key] = val; +} + +void Memoizer::remove(void *key) +{ + objectMap.erase(key); +} + +void *Memoizer::find(void *key) +{ + if (objectMap.count(key)) return objectMap[key]; + return NULL; +} + +} // love diff --git a/src/common/Memoizer.h b/src/common/Memoizer.h index 167d0ef35..9944431f0 100644 --- a/src/common/Memoizer.h +++ b/src/common/Memoizer.h @@ -1,46 +1,46 @@ -/** -* Copyright (c) 2006-2012 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_MEMOIZER_H -#define LOVE_MEMOIZER_H - -#include - -namespace love -{ - class Memoizer - { - private: - - static std::map objectMap; - - public: - - static void add(void * key, void * val); - - static void remove(void * key); - - static void * find(void * key); - - }; // Memoizer - -} // love - -#endif // LOVE_MEMOIZER_H +/** + * Copyright (c) 2006-2012 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_MEMOIZER_H +#define LOVE_MEMOIZER_H + +#include + +namespace love +{ + +class Memoizer +{ +public: + + static void add(void *key, void *val); + + static void remove(void *key); + + static void *find(void *key); + +private: + + static std::map objectMap; +}; // Memoizer + +} // love + +#endif // LOVE_MEMOIZER_H diff --git a/src/common/Module.h b/src/common/Module.h index 5fa04cc9f..8129cea4d 100644 --- a/src/common/Module.h +++ b/src/common/Module.h @@ -1,55 +1,55 @@ -/** -* Copyright (c) 2006-2012 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_MODULE_H -#define LOVE_MODULE_H - -// LOVE -#include "runtime.h" -#include "Exception.h" -#include "Object.h" - -namespace love -{ - /** - * Abstract superclass for all modules. - **/ - class Module : public Object - { - public: - - /** - * Destructor. - **/ - virtual ~Module(){}; - - /** - * Gets the name of the module. This is used in case of errors - * and other messages. - * - * @return The full name of the module, eg. love.graphics.opengl. - **/ - virtual const char * getName() const = 0; - - }; // Module - -} // love - -#endif // LOVE_MODULE_H +/** + * Copyright (c) 2006-2012 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_MODULE_H +#define LOVE_MODULE_H + +// LOVE +#include "runtime.h" +#include "Exception.h" +#include "Object.h" + +namespace love +{ +/** + * Abstract superclass for all modules. + **/ +class Module : public Object +{ +public: + + /** + * Destructor. + **/ + virtual ~Module() {}; + + /** + * Gets the name of the module. This is used in case of errors + * and other messages. + * + * @return The full name of the module, eg. love.graphics.opengl. + **/ + virtual const char *getName() const = 0; + +}; // Module + +} // love + +#endif // LOVE_MODULE_H diff --git a/src/common/Object.cpp b/src/common/Object.cpp index 8dba1c00a..b71ec096e 100644 --- a/src/common/Object.cpp +++ b/src/common/Object.cpp @@ -1,52 +1,52 @@ -/** -* Copyright (c) 2006-2012 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. -**/ - -// LOVE -#include "Object.h" - -namespace love -{ - - Object::Object() - : count(1) - { - } - - Object::~Object() - { - } - - int Object::getReferenceCount() const - { - return count; - } - - void Object::retain() - { - ++count; - } - - void Object::release() - { - if (--count <= 0) - delete this; - } - -} // love +/** + * Copyright (c) 2006-2012 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. + **/ + +// LOVE +#include "Object.h" + +namespace love +{ + +Object::Object() + : count(1) +{ +} + +Object::~Object() +{ +} + +int Object::getReferenceCount() const +{ + return count; +} + +void Object::retain() +{ + ++count; +} + +void Object::release() +{ + if (--count <= 0) + delete this; +} + +} // love diff --git a/src/common/Object.h b/src/common/Object.h index 839399f43..8bc8672bf 100644 --- a/src/common/Object.h +++ b/src/common/Object.h @@ -1,76 +1,76 @@ -/** -* Copyright (c) 2006-2012 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_OBJECT_H -#define LOVE_OBJECT_H - -namespace love -{ - /** - * Superclass for all object that should be able to cross the Lua/C border - * (this pertains to most objects). - * - * This class is an alternative to using smart pointers; it contains retain/release - * methods, and will delete itself with the reference count hits zero. The wrapper - * code assumes that all userdata inherits from this class. - **/ - class Object - { - private: - - // The reference count. - int count; - - public: - - /** - * Constructor. Sets reference count to one. - **/ - Object(); - - /** - * Destructor. - **/ - virtual ~Object() = 0; - - /** - * Gets the reference count of this Object. - * @returns The reference count. - **/ - int getReferenceCount() const; - - /** - * Retains the Object, i.e. increases the - * reference count by one. - **/ - void retain(); - - /** - * Releases one reference to the Object, i.e. decrements the - * reference count by one, and potentially deletes the Object - * if there are no more references. - **/ - void release(); - - }; // Object - -} // love - -#endif // LOVE_OBJECT_H +/** + * Copyright (c) 2006-2012 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_OBJECT_H +#define LOVE_OBJECT_H + +namespace love +{ + +/** + * Superclass for all object that should be able to cross the Lua/C border + * (this pertains to most objects). + * + * This class is an alternative to using smart pointers; it contains retain/release + * methods, and will delete itself with the reference count hits zero. The wrapper + * code assumes that all userdata inherits from this class. + **/ +class Object +{ +public: + + /** + * Constructor. Sets reference count to one. + **/ + Object(); + + /** + * Destructor. + **/ + virtual ~Object() = 0; + + /** + * Gets the reference count of this Object. + * @returns The reference count. + **/ + int getReferenceCount() const; + + /** + * Retains the Object, i.e. increases the + * reference count by one. + **/ + void retain(); + + /** + * Releases one reference to the Object, i.e. decrements the + * reference count by one, and potentially deletes the Object + * if there are no more references. + **/ + void release(); + +private: + + // The reference count. + int count; +}; // Object + +} // love + +#endif // LOVE_OBJECT_H diff --git a/src/common/Reference.cpp b/src/common/Reference.cpp index 5ba5cd5c7..c4861ab34 100644 --- a/src/common/Reference.cpp +++ b/src/common/Reference.cpp @@ -1,81 +1,82 @@ -/** -* Copyright (c) 2006-2012 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. -**/ - -#include "Reference.h" - -namespace love -{ - const char REFERENCE_TABLE_NAME[] = "love-references"; - - Reference::Reference() - : L(0), idx(LUA_REFNIL) - { - } - - Reference::Reference(lua_State * L) - : L(0), idx(LUA_REFNIL) - { - ref(L); - } - - Reference::~Reference() - { - unref(); - } - - void Reference::ref(lua_State * L) - { - unref(); // Just to be safe. - this->L = L; - luax_insist(L, LUA_REGISTRYINDEX, REFERENCE_TABLE_NAME); - lua_insert(L, -2); // Move reference table behind value. - idx = luaL_ref(L, -2); - lua_pop(L, 1); - } - - void Reference::unref() - { - if (idx != LUA_REFNIL) - { - luax_insist(L, LUA_REGISTRYINDEX, REFERENCE_TABLE_NAME); - luaL_unref(L, -1, idx); - lua_pop(L, 1); - idx = LUA_REFNIL; - } - } - - void Reference::push() - { - if (idx != LUA_REFNIL) - { - luax_insist(L, LUA_REGISTRYINDEX, REFERENCE_TABLE_NAME); - lua_rawgeti(L, -1, idx); - lua_remove(L, -2); - } - else - lua_pushnil(L); - } - - lua_State * Reference::getL() - { - return L; - } - -} // love +/** + * Copyright (c) 2006-2012 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. + **/ + +#include "Reference.h" + +namespace love +{ + +const char REFERENCE_TABLE_NAME[] = "love-references"; + +Reference::Reference() + : L(0), idx(LUA_REFNIL) +{ +} + +Reference::Reference(lua_State *L) + : L(0), idx(LUA_REFNIL) +{ + ref(L); +} + +Reference::~Reference() +{ + unref(); +} + +void Reference::ref(lua_State *L) +{ + unref(); // Just to be safe. + this->L = L; + luax_insist(L, LUA_REGISTRYINDEX, REFERENCE_TABLE_NAME); + lua_insert(L, -2); // Move reference table behind value. + idx = luaL_ref(L, -2); + lua_pop(L, 1); +} + +void Reference::unref() +{ + if (idx != LUA_REFNIL) + { + luax_insist(L, LUA_REGISTRYINDEX, REFERENCE_TABLE_NAME); + luaL_unref(L, -1, idx); + lua_pop(L, 1); + idx = LUA_REFNIL; + } +} + +void Reference::push() +{ + if (idx != LUA_REFNIL) + { + luax_insist(L, LUA_REGISTRYINDEX, REFERENCE_TABLE_NAME); + lua_rawgeti(L, -1, idx); + lua_remove(L, -2); + } + else + lua_pushnil(L); +} + +lua_State *Reference::getL() +{ + return L; +} + +} // love diff --git a/src/common/Reference.h b/src/common/Reference.h index a5dc8befd..e20e67d40 100644 --- a/src/common/Reference.h +++ b/src/common/Reference.h @@ -1,87 +1,88 @@ -/** -* Copyright (c) 2006-2012 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_REFERENCE_H -#define LOVE_REFERENCE_H - -// LOVE -#include "runtime.h" - -namespace love -{ - /** - * This class wraps the reference functionality built into - * Lua, which allows C++ code to refer to Lua variables. - **/ - class Reference - { - private: - - // The Lua state in which the reference resides. - lua_State * L; - - // Index to the Lua reference. - int idx; - - public: - - /** - * Creates the reference object, but does not create - * the actual reference. - **/ - Reference(); - - /** - * Creates the object and a reference to the value - * on the top of the stack. - **/ - Reference(lua_State * L); - - /** - * Deletes the reference, if any. - **/ - virtual ~Reference(); - - /** - * Creates a reference to the value on the - * top of the stack. - **/ - void ref(lua_State * L); - - /** - * Unrefs the reference, if any. - **/ - void unref(); - - /** - * Pushes the referred value onto the stack. - **/ - void push(); - - /** - * Gets the Lua state associated with this - * reference. - **/ - lua_State * getL(); - }; - -} // love - -#endif // LOVE_REFERENCE_H +/** + * Copyright (c) 2006-2012 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_REFERENCE_H +#define LOVE_REFERENCE_H + +// LOVE +#include "runtime.h" + +namespace love +{ + +/** + * This class wraps the reference functionality built into + * Lua, which allows C++ code to refer to Lua variables. + **/ +class Reference +{ +public: + + /** + * Creates the reference object, but does not create + * the actual reference. + **/ + Reference(); + + /** + * Creates the object and a reference to the value + * on the top of the stack. + **/ + Reference(lua_State *L); + + /** + * Deletes the reference, if any. + **/ + virtual ~Reference(); + + /** + * Creates a reference to the value on the + * top of the stack. + **/ + void ref(lua_State *L); + + /** + * Unrefs the reference, if any. + **/ + void unref(); + + /** + * Pushes the referred value onto the stack. + **/ + void push(); + + /** + * Gets the Lua state associated with this + * reference. + **/ + lua_State *getL(); + +private: + + // The Lua state in which the reference resides. + lua_State *L; + + // Index to the Lua reference. + int idx; +}; + +} // love + +#endif // LOVE_REFERENCE_H diff --git a/src/common/StringMap.h b/src/common/StringMap.h index 127e50f57..9d54d430a 100644 --- a/src/common/StringMap.h +++ b/src/common/StringMap.h @@ -1,167 +1,168 @@ -/** -* Copyright (c) 2006-2012 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 - class StringMap - { - private: - - struct Record - { - const char * key; - T value; - bool set; - Record() : set(false) {} - }; - - const static unsigned MAX = SIZE*2; - - 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; //this isn't used, is this intentional? - - if (!records[str_i].set) - return false; - - if (streq(records[str_i].key, key)) - { - t = records[str_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 +/** + * Copyright (c) 2006-2012 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 +class StringMap +{ +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; //this isn't used, is this intentional? + + if (!records[str_i].set) + return false; + + if (streq(records[str_i].key, key)) + { + t = records[str_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; + } + +private: + + struct Record + { + const char *key; + T value; + bool set; + Record() : set(false) {} + }; + + const static unsigned MAX = SIZE*2; + + Record records[MAX]; + const char *reverse[SIZE]; + +}; // StringMap + +} // love + +#endif // LOVE_STRING_MAP_H diff --git a/src/common/Variant.cpp b/src/common/Variant.cpp index ace030766..94dd45dae 100644 --- a/src/common/Variant.cpp +++ b/src/common/Variant.cpp @@ -1,173 +1,175 @@ -/** -* Copyright (c) 2006-2012 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. -**/ - -#include "Variant.h" -#include - -namespace love -{ - extern StringMap types; - - love::Type extractudatatype(lua_State * L, int idx) - { - Type t = INVALID_ID; - if (!lua_isuserdata(L, idx)) - return t; - if (luaL_getmetafield (L, idx, "__tostring") == 0) - return t; - lua_pushvalue(L, idx); - int result = lua_pcall(L, 1, 1, 0); - if (result == 0) - types.find(lua_tostring(L, -1), t); - if (result == 0 || result == LUA_ERRRUN) - lua_pop(L, 1); - return t; - } - - Variant::Variant(bool boolean) - { - type = BOOLEAN; - data.boolean = boolean; - } - - Variant::Variant(double number) - { - type = NUMBER; - data.number = number; - } - - Variant::Variant(const char *string, size_t len) - { - type = STRING; - char *buf = new char[len+1]; - memset(buf, 0, len+1); - memcpy(buf, string, len); - data.string.str = buf; - data.string.len = len; - } - - Variant::Variant(char c) - { - type = CHARACTER; - data.character = c; - } - - Variant::Variant(void *userdata) - { - type = LUSERDATA; - data.userdata = userdata; - } - - Variant::Variant(love::Type udatatype, void *userdata) - { - type = FUSERDATA; - this->udatatype = udatatype; - if (udatatype != INVALID_ID) - { - Proxy *p = (Proxy *) userdata; - flags = p->flags; - data.userdata = p->data; - ((love::Object *) data.userdata)->retain(); - } - else - data.userdata = userdata; - } - - Variant::~Variant() - { - switch(type) - { - case STRING: - delete[] data.string.str; - break; - case FUSERDATA: - ((love::Object *) data.userdata)->release(); - break; - default: - break; - } - } - - Variant *Variant::fromLua(lua_State *L, int n) - { - Variant *v = NULL; - size_t len; - const char *str; - switch(lua_type(L, n)) - { - case LUA_TBOOLEAN: - v = new Variant(luax_toboolean(L, n)); - break; - case LUA_TNUMBER: - v = new Variant(lua_tonumber(L, n)); - break; - case LUA_TSTRING: - str = lua_tolstring(L, n, &len); - v = new Variant(str, len); - break; - case LUA_TLIGHTUSERDATA: - v = new Variant(lua_touserdata(L, n)); - break; - case LUA_TUSERDATA: - v = new Variant(extractudatatype(L, n), lua_touserdata(L, n)); - break; - } - return v; - } - - void Variant::toLua(lua_State *L) - { - switch(type) - { - case BOOLEAN: - lua_pushboolean(L, data.boolean); - break; - case CHARACTER: - lua_pushlstring(L, &data.character, 1); - break; - case NUMBER: - lua_pushnumber(L, data.number); - break; - case STRING: - lua_pushlstring(L, data.string.str, data.string.len); - break; - case LUSERDATA: - lua_pushlightuserdata(L, data.userdata); - break; - case FUSERDATA: - if (udatatype != INVALID_ID) - { - const char *name = NULL; - love::types.find(udatatype, name); - ((love::Object *) data.userdata)->retain(); - luax_newtype(L, name, flags, data.userdata); - } - else - lua_pushlightuserdata(L, data.userdata); - // I know this is not the same - // sadly, however, it's the most - // I can do (at the moment). - break; - default: - lua_pushnil(L); - break; - } - } -} // love +/** + * Copyright (c) 2006-2012 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. + **/ + +#include "Variant.h" +#include "common/StringMap.h" + +namespace love +{ + +extern StringMap types; + +love::Type extractudatatype(lua_State *L, int idx) +{ + Type t = INVALID_ID; + if (!lua_isuserdata(L, idx)) + return t; + if (luaL_getmetafield(L, idx, "__tostring") == 0) + return t; + lua_pushvalue(L, idx); + int result = lua_pcall(L, 1, 1, 0); + if (result == 0) + types.find(lua_tostring(L, -1), t); + if (result == 0 || result == LUA_ERRRUN) + lua_pop(L, 1); + return t; +} + +Variant::Variant(bool boolean) +{ + type = BOOLEAN; + data.boolean = boolean; +} + +Variant::Variant(double number) +{ + type = NUMBER; + data.number = number; +} + +Variant::Variant(const char *string, size_t len) +{ + type = STRING; + char *buf = new char[len+1]; + memset(buf, 0, len+1); + memcpy(buf, string, len); + data.string.str = buf; + data.string.len = len; +} + +Variant::Variant(char c) +{ + type = CHARACTER; + data.character = c; +} + +Variant::Variant(void *userdata) +{ + type = LUSERDATA; + data.userdata = userdata; +} + +Variant::Variant(love::Type udatatype, void *userdata) +{ + type = FUSERDATA; + this->udatatype = udatatype; + if (udatatype != INVALID_ID) + { + Proxy *p = (Proxy *) userdata; + flags = p->flags; + data.userdata = p->data; + ((love::Object *) data.userdata)->retain(); + } + else + data.userdata = userdata; +} + +Variant::~Variant() +{ + switch (type) + { + case STRING: + delete[] data.string.str; + break; + case FUSERDATA: + ((love::Object *) data.userdata)->release(); + break; + default: + break; + } +} + +Variant *Variant::fromLua(lua_State *L, int n) +{ + Variant *v = NULL; + size_t len; + const char *str; + switch (lua_type(L, n)) + { + case LUA_TBOOLEAN: + v = new Variant(luax_toboolean(L, n)); + break; + case LUA_TNUMBER: + v = new Variant(lua_tonumber(L, n)); + break; + case LUA_TSTRING: + str = lua_tolstring(L, n, &len); + v = new Variant(str, len); + break; + case LUA_TLIGHTUSERDATA: + v = new Variant(lua_touserdata(L, n)); + break; + case LUA_TUSERDATA: + v = new Variant(extractudatatype(L, n), lua_touserdata(L, n)); + break; + } + return v; +} + +void Variant::toLua(lua_State *L) +{ + switch (type) + { + case BOOLEAN: + lua_pushboolean(L, data.boolean); + break; + case CHARACTER: + lua_pushlstring(L, &data.character, 1); + break; + case NUMBER: + lua_pushnumber(L, data.number); + break; + case STRING: + lua_pushlstring(L, data.string.str, data.string.len); + break; + case LUSERDATA: + lua_pushlightuserdata(L, data.userdata); + break; + case FUSERDATA: + if (udatatype != INVALID_ID) + { + const char *name = NULL; + love::types.find(udatatype, name); + ((love::Object *) data.userdata)->retain(); + luax_newtype(L, name, flags, data.userdata); + } + else + lua_pushlightuserdata(L, data.userdata); + // I know this is not the same + // sadly, however, it's the most + // I can do (at the moment). + break; + default: + lua_pushnil(L); + break; + } +} + +} // love diff --git a/src/common/Variant.h b/src/common/Variant.h index 0e23029ad..b3f0f42e1 100644 --- a/src/common/Variant.h +++ b/src/common/Variant.h @@ -1,73 +1,76 @@ -/** -* Copyright (c) 2006-2012 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_VARIANT_H -#define LOVE_VARIANT_H - -#include -#include - -#include - -namespace love -{ - class Variant : public love::Object - { - private: - enum Type - { - UNKNOWN = 0, - BOOLEAN, - NUMBER, - CHARACTER, - STRING, - LUSERDATA, - FUSERDATA - } type; - union - { - bool boolean; - char character; - double number; - struct { - const char *str; - size_t len; - } string; - void *userdata; - } data; - love::Type udatatype; - bits flags; - - public: - - Variant(bool boolean); - Variant(double number); - Variant(const char *string, size_t len); - Variant(char c); - Variant(void *userdata); - Variant(love::Type udatatype, void *userdata); - virtual ~Variant(); - - static Variant *fromLua(lua_State *L, int n); - void toLua(lua_State *L); - }; // Variant -} // love - -#endif // LOVE_VARIANT_H +/** + * Copyright (c) 2006-2012 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_VARIANT_H +#define LOVE_VARIANT_H + +#include "common/runtime.h" +#include "common/Object.h" + +#include + +namespace love +{ + +class Variant : public love::Object +{ +public: + + Variant(bool boolean); + Variant(double number); + Variant(const char *string, size_t len); + Variant(char c); + Variant(void *userdata); + Variant(love::Type udatatype, void *userdata); + virtual ~Variant(); + + static Variant *fromLua(lua_State *L, int n); + void toLua(lua_State *L); + +private: + enum Type + { + UNKNOWN = 0, + BOOLEAN, + NUMBER, + CHARACTER, + STRING, + LUSERDATA, + FUSERDATA + } type; + union + { + bool boolean; + char character; + double number; + struct + { + const char *str; + size_t len; + } string; + void *userdata; + } data; + love::Type udatatype; + bits flags; + +}; // Variant +} // love + +#endif // LOVE_VARIANT_H diff --git a/src/common/Vector.cpp b/src/common/Vector.cpp index b378aa129..77ac2e998 100644 --- a/src/common/Vector.cpp +++ b/src/common/Vector.cpp @@ -1,26 +1,26 @@ -/** -* Copyright (c) 2006-2012 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. -**/ - -#include "Vector.h" - -namespace love -{ - // Implementation in header. +/** + * Copyright (c) 2006-2012 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. + **/ + +#include "Vector.h" + +namespace love +{ +// Implementation in header. } \ No newline at end of file diff --git a/src/common/Vector.h b/src/common/Vector.h index aca38fbcc..2aaa77fb2 100644 --- a/src/common/Vector.h +++ b/src/common/Vector.h @@ -1,310 +1,311 @@ -/** -* Copyright (c) 2006-2012 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_VECTOR_H -#define LOVE_VECTOR_H - -// STD -#include - -// LOVE -#include "Matrix.h" - -namespace love -{ - /** - * 2D Vector class. - * - * @author Anders Ruud - * @date 2006-05-13 - **/ - class Vector - { - public: - - // The components. - float x, y; - - /** - * Creates a new (1,1) Vector. - **/ - Vector(); - - /** - * Creates a new Vector. - * @param x The x position/dimension. - * @param y The y position/dimension. - **/ - Vector(float x, float y); - - /** - * Gets the length of the Vector. - * @return The length of the Vector. - * - * This method requires sqrt() and should be used - * carefully. - **/ - float getLength() const; - - /** - * Normalizes the Vector. - * @return The old length of the Vector. - **/ - float normalize(); - - /** - * Gets a normal to the Vector. - * @return A normal to the Vector. - **/ - - Vector getNormal() const; - - /** - * Adds a Vector to this Vector. - * @param v The Vector we want to add to this Vector. - * @return The resulting Vector. - **/ - Vector operator + (const Vector & v) const; - - /** - * Substracts a Vector to this Vector. - * @param v The Vector we want to subtract to this Vector. - * @return The resulting Vector. - **/ - Vector operator - (const Vector & v) const; - - /** - * Resizes a Vector by a scalar. - * @param s The scalar with which to resize the Vector. - * @return The resulting Vector. - **/ - Vector operator * (float s) const; - - /** - * Resizes a Vector by a scalar. - * @param s The scalar with which to resize the Vector. - * @return The resulting Vector. - **/ - Vector operator / (float s) const; - - /** - * Reverses the Vector. - * @return The reversed Vector. - **/ - Vector operator - () const; - - /** - * Adds a Vector to this Vector, and also saves changes in the first Vector. - * @param v The Vector we want to add to this Vector. - **/ - void operator += (const Vector & v); - - /** - * Subtracts a Vector to this Vector, and also saves changes in the first Vector. - * @param v The Vector we want to subtract to this Vector. - **/ - void operator -= (const Vector & v); - - /** - * Resizes the Vector, and also saves changes in the first Vector. - * @param s The scalar by which we want to resize the Vector. - **/ - void operator *= (float s); - - /** - * Resizes the Vector, and also saves changes in the first Vector. - * @param s The scalar by which we want to resize the Vector. - **/ - void operator /= (float s); - - /** - * Calculates the dot product of two Vectors. - * @return The dot product of the two Vectors. - **/ - float operator * (const Vector & v) const; - - /** - * Calculates the cross product of two Vectors. - * @return The cross product of the two Vectors. - **/ - float operator ^ (const Vector & v) const; - - bool operator == (const Vector & v) const; - - bool operator < (const Vector & v) const; - /** - * Gets the x value of the Vector. - * @return The x value of the Vector. - **/ - float getX() const; - - /** - * Gets the x value of the Vector. - * @return The x value of the Vector. - **/ - float getY() const; - - /** - * Sets the x value of the Vector. - * @param The x value of the Vector. - **/ - void setX(float x); - - /** - * Sets the x value of the Vector. - * @param The x value of the Vector. - **/ - void setY(float y); - - }; - - inline float Vector::getLength() const - { - return sqrt(x*x + y*y); - } - - inline Vector Vector::getNormal() const - { - return Vector(-y, x); - } - - inline float Vector::normalize() - { - - float len = getLength(); - - if (len > 0) - (*this) /= len; - - return len; - } - - /** - * Inline methods must have body in header. - **/ - - inline Vector::Vector() - { - x = 1; - y = 1; - } - - inline Vector::Vector(float x, float y) - { - this->x = x; - this->y = y; - } - - inline Vector Vector::operator + (const Vector & v) const - { - return Vector(x + v.x, y + v.y); - } - - inline Vector Vector::operator - (const Vector & v) const - { - return Vector(x - v.getX(), y - v.getY()); - } - - inline Vector Vector::operator * (float s) const - { - return Vector(x*s, y*s); - } - - inline Vector Vector::operator / (float s) const - { - return Vector(x/s, y/s); - } - - inline Vector Vector::operator - () const - { - return Vector(-x, -y); - } - - inline void Vector::operator += (const Vector & v) - { - x += v.getX(); - y += v.getY(); - } - - inline void Vector::operator -= (const Vector & v) - { - x -= v.getX(); - y -= v.getY(); - } - - inline void Vector::operator *= (float s) - { - x *= s; - y *= s; - } - - inline void Vector::operator /= (float s) - { - x /= s; - y /= s; - } - - inline float Vector::operator * (const Vector & v) const - { - return x * v.getX() + y * v.getY(); - } - - inline float Vector::operator ^ (const Vector & v) const - { - return x * v.getY() - y * v.getX(); - } - - inline bool Vector::operator == (const Vector & v) const - { - return getLength() == v.getLength(); - } - - inline bool Vector::operator < (const Vector & v) const - { - return getLength() < v.getLength(); - } - - /** - * Accessor methods - **/ - - inline float Vector::getX() const - { - return x; - } - - inline float Vector::getY() const - { - return y; - } - - inline void Vector::setX(float x) - { - this->x = x; - } - - inline void Vector::setY(float y) - { - this->y = y; - } - -} //love - -#endif// LOVE_VECTOR_H +/** + * Copyright (c) 2006-2012 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_VECTOR_H +#define LOVE_VECTOR_H + +// STD +#include + +// LOVE +#include "Matrix.h" + +namespace love +{ + +/** + * 2D Vector class. + * + * @author Anders Ruud + * @date 2006-05-13 + **/ +class Vector +{ +public: + + // The components. + float x, y; + + /** + * Creates a new (1,1) Vector. + **/ + Vector(); + + /** + * Creates a new Vector. + * @param x The x position/dimension. + * @param y The y position/dimension. + **/ + Vector(float x, float y); + + /** + * Gets the length of the Vector. + * @return The length of the Vector. + * + * This method requires sqrt() and should be used + * carefully. + **/ + float getLength() const; + + /** + * Normalizes the Vector. + * @return The old length of the Vector. + **/ + float normalize(); + + /** + * Gets a normal to the Vector. + * @return A normal to the Vector. + **/ + + Vector getNormal() const; + + /** + * Adds a Vector to this Vector. + * @param v The Vector we want to add to this Vector. + * @return The resulting Vector. + **/ + Vector operator + (const Vector &v) const; + + /** + * Substracts a Vector to this Vector. + * @param v The Vector we want to subtract to this Vector. + * @return The resulting Vector. + **/ + Vector operator - (const Vector &v) const; + + /** + * Resizes a Vector by a scalar. + * @param s The scalar with which to resize the Vector. + * @return The resulting Vector. + **/ + Vector operator * (float s) const; + + /** + * Resizes a Vector by a scalar. + * @param s The scalar with which to resize the Vector. + * @return The resulting Vector. + **/ + Vector operator / (float s) const; + + /** + * Reverses the Vector. + * @return The reversed Vector. + **/ + Vector operator - () const; + + /** + * Adds a Vector to this Vector, and also saves changes in the first Vector. + * @param v The Vector we want to add to this Vector. + **/ + void operator += (const Vector &v); + + /** + * Subtracts a Vector to this Vector, and also saves changes in the first Vector. + * @param v The Vector we want to subtract to this Vector. + **/ + void operator -= (const Vector &v); + + /** + * Resizes the Vector, and also saves changes in the first Vector. + * @param s The scalar by which we want to resize the Vector. + **/ + void operator *= (float s); + + /** + * Resizes the Vector, and also saves changes in the first Vector. + * @param s The scalar by which we want to resize the Vector. + **/ + void operator /= (float s); + + /** + * Calculates the dot product of two Vectors. + * @return The dot product of the two Vectors. + **/ + float operator * (const Vector &v) const; + + /** + * Calculates the cross product of two Vectors. + * @return The cross product of the two Vectors. + **/ + float operator ^ (const Vector &v) const; + + bool operator == (const Vector &v) const; + + bool operator < (const Vector &v) const; + /** + * Gets the x value of the Vector. + * @return The x value of the Vector. + **/ + float getX() const; + + /** + * Gets the x value of the Vector. + * @return The x value of the Vector. + **/ + float getY() const; + + /** + * Sets the x value of the Vector. + * @param The x value of the Vector. + **/ + void setX(float x); + + /** + * Sets the x value of the Vector. + * @param The x value of the Vector. + **/ + void setY(float y); + +}; + +inline float Vector::getLength() const +{ + return sqrt(x*x + y*y); +} + +inline Vector Vector::getNormal() const +{ + return Vector(-y, x); +} + +inline float Vector::normalize() +{ + + float len = getLength(); + + if (len > 0) + (*this) /= len; + + return len; +} + +/** + * Inline methods must have body in header. + **/ + +inline Vector::Vector() +{ + x = 1; + y = 1; +} + +inline Vector::Vector(float x, float y) +{ + this->x = x; + this->y = y; +} + +inline Vector Vector::operator + (const Vector &v) const +{ + return Vector(x + v.x, y + v.y); +} + +inline Vector Vector::operator - (const Vector &v) const +{ + return Vector(x - v.getX(), y - v.getY()); +} + +inline Vector Vector::operator * (float s) const +{ + return Vector(x*s, y*s); +} + +inline Vector Vector::operator / (float s) const +{ + return Vector(x/s, y/s); +} + +inline Vector Vector::operator - () const +{ + return Vector(-x, -y); +} + +inline void Vector::operator += (const Vector &v) +{ + x += v.getX(); + y += v.getY(); +} + +inline void Vector::operator -= (const Vector &v) +{ + x -= v.getX(); + y -= v.getY(); +} + +inline void Vector::operator *= (float s) +{ + x *= s; + y *= s; +} + +inline void Vector::operator /= (float s) +{ + x /= s; + y /= s; +} + +inline float Vector::operator * (const Vector &v) const +{ + return x * v.getX() + y * v.getY(); +} + +inline float Vector::operator ^ (const Vector &v) const +{ + return x * v.getY() - y * v.getX(); +} + +inline bool Vector::operator == (const Vector &v) const +{ + return getLength() == v.getLength(); +} + +inline bool Vector::operator < (const Vector &v) const +{ + return getLength() < v.getLength(); +} + +/** + * Accessor methods + **/ + +inline float Vector::getX() const +{ + return x; +} + +inline float Vector::getY() const +{ + return y; +} + +inline void Vector::setX(float x) +{ + this->x = x; +} + +inline void Vector::setY(float y) +{ + this->y = y; +} + +} //love + +#endif// LOVE_VECTOR_H diff --git a/src/common/b64.cpp b/src/common/b64.cpp index 5da6e3def..f0343f89a 100644 --- a/src/common/b64.cpp +++ b/src/common/b64.cpp @@ -1,78 +1,80 @@ -/** -* Copyright (c) 2006-2012 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. -**/ - -#include "b64.h" - -namespace love -{ - static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq"; - - void b64_decode_block(char in[4], char out[3]) - { - out[0] = (char)(in[0] << 2 | in[1] >> 4); - out[1] = (char)(in[1] << 4 | in[2] >> 2); - out[2] = (char)(((in[2] << 6) & 0xc0) | in[3]); - } - - char * b64_decode(const char * src, int slen, int & size) - { - size = (slen / 4) * 3; - - char * dst = new char[size]; - char * d = dst; - - char in[4], out[3], v; - int i, len, pos = 0; - - while (pos <= slen) - { - for (len = 0, i = 0; i < 4 && pos <= slen; i++ ) - { - v = 0; - - while (pos <= slen && v == 0 ) - { - v = src[pos++]; - v = (char)((v < 43 || v > 122) ? 0 : cd64[v - 43]); - if (v) - v = (char)((v == '$') ? 0 : v - 61); - } - - if (pos <= slen) - { - len++; - if (v) - in[i] = (char)(v - 1); - } - else - in[i] = 0; - } - - if (len) - { - b64_decode_block(in, out); - for (i = 0; i < len - 1; i++) - *(d++) = out[i]; - } - } - - return dst; - } -} // love +/** + * Copyright (c) 2006-2012 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. + **/ + +#include "b64.h" + +namespace love +{ + +static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq"; + +void b64_decode_block(char in[4], char out[3]) +{ + out[0] = (char)(in[0] << 2 | in[1] >> 4); + out[1] = (char)(in[1] << 4 | in[2] >> 2); + out[2] = (char)(((in[2] << 6) & 0xc0) | in[3]); +} + +char *b64_decode(const char *src, int slen, int &size) +{ + size = (slen / 4) * 3; + + char *dst = new char[size]; + char *d = dst; + + char in[4], out[3], v; + int i, len, pos = 0; + + while (pos <= slen) + { + for (len = 0, i = 0; i < 4 && pos <= slen; i++) + { + v = 0; + + while (pos <= slen && v == 0) + { + v = src[pos++]; + v = (char)((v < 43 || v > 122) ? 0 : cd64[v - 43]); + if (v) + v = (char)((v == '$') ? 0 : v - 61); + } + + if (pos <= slen) + { + len++; + if (v) + in[i] = (char)(v - 1); + } + else + in[i] = 0; + } + + if (len) + { + b64_decode_block(in, out); + for (i = 0; i < len - 1; i++) + *(d++) = out[i]; + } + } + + return dst; +} + +} // love diff --git a/src/common/b64.h b/src/common/b64.h index cfb4888bb..fa69bf6a2 100644 --- a/src/common/b64.h +++ b/src/common/b64.h @@ -1,39 +1,41 @@ -/** -* Copyright (c) 2006-2012 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. -**/ - -#include "config.h" - -#ifndef LOVE_B64_H -#define LOVE_B64_H - -namespace love -{ - /** - * Decode base64 encoded data. - * - * @param src The string containing the base64 data. - * @param slen The length of the string. - * @param size The size of the binary data is stored here. - * @return A chunk of memory containing the binary data (allocated with new[]). - */ - char * b64_decode(const char * src, int slen, int & size); -} // love - +/** + * Copyright (c) 2006-2012 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. + **/ + +#include "config.h" + +#ifndef LOVE_B64_H +#define LOVE_B64_H + +namespace love +{ + +/** + * Decode base64 encoded data. + * + * @param src The string containing the base64 data. + * @param slen The length of the string. + * @param size The size of the binary data is stored here. + * @return A chunk of memory containing the binary data (allocated with new[]). + */ +char *b64_decode(const char *src, int slen, int &size); + +} // love + #endif // LOVE_B64_H \ No newline at end of file diff --git a/src/common/config.h b/src/common/config.h index 2b9944a11..2de75ce5b 100644 --- a/src/common/config.h +++ b/src/common/config.h @@ -1,93 +1,93 @@ -/** -* Copyright (c) 2006-2012 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_CONFIG_H -#define LOVE_CONFIG_H - -// Platform stuff. -#if defined(WIN32) || defined(_WIN32) -# define LOVE_WINDOWS 1 -#endif -#if defined(linux) || defined(__linux) || defined(__linux__) -# define LOVE_LINUX 1 -#endif -#if defined(__APPLE__) -# define LOVE_MACOSX 1 -#endif -#if defined(macintosh) -# define LOVE_MACOS 1 -#endif - -// Endianness. -#if defined(__i386__) || defined(__i386) -# define LOVE_LITTLE_ENDIAN 1 -#endif -#if defined(__ppc__) || defined(__ppc) || defined(__powerpc__) || defined(__powerpc) -# define LOVE_BIG_ENDIAN 1 -#endif - -// Warnings. -#ifndef _CRT_SECURE_NO_WARNINGS -# define _CRT_SECURE_NO_WARNINGS -#endif - -// Preferably, and ironically, this macro should go unused. -#ifndef LOVE_UNUSED -# define LOVE_UNUSED(x) (void)sizeof(x) -#endif - -#ifndef LOVE_BUILD -# define LOVE_BUILD -# define LOVE_BUILD_STANDALONE -# define LOVE_BUILD_EXE -//# define LOVE_BUILD_DLL -#endif - -// DLL-stuff. -#ifdef LOVE_WINDOWS -# define LOVE_EXPORT __declspec(dllexport) -#else -# define LOVE_EXPORT -#endif - -#if defined(LOVE_WINDOWS) -# define LOVE_LEGENDARY_UTF8_ARGV_HACK -# define LOVE_LEGENDARY_CONSOLE_IO_HACK -# define NOMINMAX -#endif - -#if defined(LOVE_MACOSX) -# define LOVE_LEGENDARY_LIBSTDCXX_HACK -#endif - -// Autotools config.h -#ifdef HAVE_CONFIG_H -# include <../config.h> -# undef VERSION -# ifdef WORDS_BIGENDIAN -# undef LOVE_LITTLE_ENDIAN -# define LOVE_BIG_ENDIAN 1 -# else -# undef LOVE_BIG_ENDIAN -# define LOVE_LITTLE_ENDIAN 1 -# endif -#endif - -#endif // LOVE_CONFIG_H +/** + * Copyright (c) 2006-2012 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_CONFIG_H +#define LOVE_CONFIG_H + +// Platform stuff. +#if defined(WIN32) || defined(_WIN32) +# define LOVE_WINDOWS 1 +#endif +#if defined(linux) || defined(__linux) || defined(__linux__) +# define LOVE_LINUX 1 +#endif +#if defined(__APPLE__) +# define LOVE_MACOSX 1 +#endif +#if defined(macintosh) +# define LOVE_MACOS 1 +#endif + +// Endianness. +#if defined(__i386__) || defined(__i386) +# define LOVE_LITTLE_ENDIAN 1 +#endif +#if defined(__ppc__) || defined(__ppc) || defined(__powerpc__) || defined(__powerpc) +# define LOVE_BIG_ENDIAN 1 +#endif + +// Warnings. +#ifndef _CRT_SECURE_NO_WARNINGS +# define _CRT_SECURE_NO_WARNINGS +#endif + +// Preferably, and ironically, this macro should go unused. +#ifndef LOVE_UNUSED +# define LOVE_UNUSED(x) (void)sizeof(x) +#endif + +#ifndef LOVE_BUILD +# define LOVE_BUILD +# define LOVE_BUILD_STANDALONE +# define LOVE_BUILD_EXE +//# define LOVE_BUILD_DLL +#endif + +// DLL-stuff. +#ifdef LOVE_WINDOWS +# define LOVE_EXPORT __declspec(dllexport) +#else +# define LOVE_EXPORT +#endif + +#if defined(LOVE_WINDOWS) +# define LOVE_LEGENDARY_UTF8_ARGV_HACK +# define LOVE_LEGENDARY_CONSOLE_IO_HACK +# define NOMINMAX +#endif + +#if defined(LOVE_MACOSX) +# define LOVE_LEGENDARY_LIBSTDCXX_HACK +#endif + +// Autotools config.h +#ifdef HAVE_CONFIG_H +# include <../config.h> +# undef VERSION +# ifdef WORDS_BIGENDIAN +# undef LOVE_LITTLE_ENDIAN +# define LOVE_BIG_ENDIAN 1 +# else +# undef LOVE_BIG_ENDIAN +# define LOVE_LITTLE_ENDIAN 1 +# endif +#endif + +#endif // LOVE_CONFIG_H diff --git a/src/common/delay.cpp b/src/common/delay.cpp index 51eb63d6b..163091794 100644 --- a/src/common/delay.cpp +++ b/src/common/delay.cpp @@ -1,41 +1,42 @@ -/** -* Copyright (c) 2006-2012 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. -**/ - - -#include "delay.h" - -namespace love { - - void delay(unsigned int ms) { -#if LOVE_THREADS == LOVE_THREADS_POSIX - struct timespec ts1, ts2; - - ts1.tv_sec = ms / 1000; - ts1.tv_nsec = (ms % 1000) * 1000000; - // FIXME: handle signals - nanosleep(&ts1, &ts2); -#elif LOVE_THREADS == LOVE_THREADS_WIN32 - Sleep(ms); -#elif LOVE_THREADS == LOVE_THREADS_SDL - SDL_Delay(ms); -#endif - } - -} // namespace love +/** + * Copyright (c) 2006-2012 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. + **/ + +#include "delay.h" + +namespace love +{ + +void delay(unsigned int ms) +{ +#if LOVE_THREADS == LOVE_THREADS_POSIX + struct timespec ts1, ts2; + + ts1.tv_sec = ms / 1000; + ts1.tv_nsec = (ms % 1000) * 1000000; + // FIXME: handle signals + nanosleep(&ts1, &ts2); +#elif LOVE_THREADS == LOVE_THREADS_WIN32 + Sleep(ms); +#elif LOVE_THREADS == LOVE_THREADS_SDL + SDL_Delay(ms); +#endif +} + +} // love diff --git a/src/common/delay.h b/src/common/delay.h index 125d5cdc0..31e4cf127 100644 --- a/src/common/delay.h +++ b/src/common/delay.h @@ -1,33 +1,33 @@ -/** -* Copyright (c) 2006-2012 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 DELAY_H_ -#define DELAY_H_ - -#include - - -namespace love { - - void delay(unsigned int ms); - -}; // namespace love - -#endif /* DELAY_H_ */ +/** + * Copyright (c) 2006-2012 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 DELAY_H_ +#define DELAY_H_ + +#include + +namespace love +{ + +void delay(unsigned int ms); + +}; // namespace love + +#endif // DELAY_H_ diff --git a/src/common/int.h b/src/common/int.h index 6fa93be2d..c9ee9eda9 100644 --- a/src/common/int.h +++ b/src/common/int.h @@ -1,64 +1,65 @@ -/** -* Copyright (c) 2006-2012 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_INT_H -#define LOVE_INT_H - -#include - -#ifndef LOVE_WINDOWS -#include -#endif - -#define LOVE_INT8_MAX 0x7F -#define LOVE_UINT8_MAX 0xFF -#define LOVE_INT16_MAX 0x7FFF -#define LOVE_UINT16_MAX 0xFFFF -#define LOVE_INT32_MAX 0x7FFFFFFF -#define LOVE_UINT32_MAX 0xFFFFFFFF -#define LOVE_INT64_MAX 0x7FFFFFFFFFFFFFFF -#define LOVE_UINT64_MAX 0xFFFFFFFFFFFFFFFF - -namespace love -{ -// Blame Microsoft -#ifdef LOVE_WINDOWS - typedef __int8 int8; - typedef unsigned __int8 uint8; - typedef __int16 int16; - typedef unsigned __int16 uint16; - typedef __int32 int32; - typedef unsigned __int32 uint32; - typedef __int64 int64; - typedef unsigned __int64 uint64; - -#else - typedef int8_t int8; - typedef uint8_t uint8; - typedef int16_t int16; - typedef uint16_t uint16; - typedef int32_t int32; - typedef uint32_t uint32; - typedef int64_t int64; - typedef uint64_t uint64; -#endif -} // love - -#endif // LOVE_INT_H +/** + * Copyright (c) 2006-2012 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_INT_H +#define LOVE_INT_H + +#include "common/config.h" + +#ifndef LOVE_WINDOWS +#include +#endif + +#define LOVE_INT8_MAX 0x7F +#define LOVE_UINT8_MAX 0xFF +#define LOVE_INT16_MAX 0x7FFF +#define LOVE_UINT16_MAX 0xFFFF +#define LOVE_INT32_MAX 0x7FFFFFFF +#define LOVE_UINT32_MAX 0xFFFFFFFF +#define LOVE_INT64_MAX 0x7FFFFFFFFFFFFFFF +#define LOVE_UINT64_MAX 0xFFFFFFFFFFFFFFFF + +namespace love +{ + +// Blame Microsoft +#ifdef LOVE_WINDOWS + typedef __int8 int8; + typedef unsigned __int8 uint8; + typedef __int16 int16; + typedef unsigned __int16 uint16; + typedef __int32 int32; + typedef unsigned __int32 uint32; + typedef __int64 int64; + typedef unsigned __int64 uint64; +#else // LOVE_WINDOWS + typedef int8_t int8; + typedef uint8_t uint8; + typedef int16_t int16; + typedef uint16_t uint16; + typedef int32_t int32; + typedef uint32_t uint32; + typedef int64_t int64; + typedef uint64_t uint64; +#endif // LOVE_WINDOWS + +} // love + +#endif // LOVE_INT_H diff --git a/src/common/math.h b/src/common/math.h index f03af1952..282b1ffc3 100644 --- a/src/common/math.h +++ b/src/common/math.h @@ -1,85 +1,85 @@ -/** -* Copyright (c) 2006-2012 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_MATH_H -#define LOVE_MATH_H - -#include // for CHAR_BIT - -/* Definitions of useful mathematical constants - * M_E - e - * M_LOG2E - log2(e) - * M_LOG10E - log10(e) - * M_LN2 - ln(2) - * M_LN10 - ln(10) - * M_PI - pi - * M_PI_2 - pi/2 - * M_PI_4 - pi/4 - * M_1_PI - 1/pi - * M_2_PI - 2/pi - * M_2_SQRTPI - 2/sqrt(pi) - * M_SQRT2 - sqrt(2) - * M_SQRT1_2 - 1/sqrt(2) - */ - -#define LOVE_M_E 2.71828182845904523536 -#define LOVE_M_LOG2E 1.44269504088896340736 -#define LOVE_M_LOG10E 0.434294481903251827651 -#define LOVE_M_LN2 0.693147180559945309417 -#define LOVE_M_LN10 2.30258509299404568402 -#define LOVE_M_PI 3.14159265358979323846 -#define LOVE_M_PI_2 1.57079632679489661923 -#define LOVE_M_PI_4 0.785398163397448309616 -#define LOVE_M_1_PI 0.318309886183790671538 -#define LOVE_M_2_PI 0.636619772367581343076 -#define LOVE_M_2_SQRTPI 1.12837916709551257390 -#define LOVE_M_SQRT2 1.41421356237309504880 -#define LOVE_M_SQRT1_2 0.707106781186547524401 -#define LOVE_M_TORAD (float)(LOVE_M_PI/180.0) -#define LOVE_M_TODEG (float)(180.0/LOVE_M_PI) -#define LOVE_TORAD(x) (float)(x*LOVE_M_TORAD) -#define LOVE_TODEG(x) (float)(x*LOVE_M_TODEG) - -namespace love -{ - -struct vertex -{ - unsigned char r, g, b, a; - float x, y; - float s, t; -}; - -inline int next_p2(int x) -{ - x += (x == 0); - x--; - for (unsigned int i = 1; i < sizeof(int)*CHAR_BIT; i <<= 1) x |= x >> i; - return ++x; -} - -inline float next_p2(float x) -{ - return static_cast(next_p2(static_cast(x))); -} - -} // love - -#endif // LOVE_MATH_H +/** + * Copyright (c) 2006-2012 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_MATH_H +#define LOVE_MATH_H + +#include // for CHAR_BIT + +/* Definitions of useful mathematical constants + * M_E - e + * M_LOG2E - log2(e) + * M_LOG10E - log10(e) + * M_LN2 - ln(2) + * M_LN10 - ln(10) + * M_PI - pi + * M_PI_2 - pi/2 + * M_PI_4 - pi/4 + * M_1_PI - 1/pi + * M_2_PI - 2/pi + * M_2_SQRTPI - 2/sqrt(pi) + * M_SQRT2 - sqrt(2) + * M_SQRT1_2 - 1/sqrt(2) + */ + +#define LOVE_M_E 2.71828182845904523536 +#define LOVE_M_LOG2E 1.44269504088896340736 +#define LOVE_M_LOG10E 0.434294481903251827651 +#define LOVE_M_LN2 0.693147180559945309417 +#define LOVE_M_LN10 2.30258509299404568402 +#define LOVE_M_PI 3.14159265358979323846 +#define LOVE_M_PI_2 1.57079632679489661923 +#define LOVE_M_PI_4 0.785398163397448309616 +#define LOVE_M_1_PI 0.318309886183790671538 +#define LOVE_M_2_PI 0.636619772367581343076 +#define LOVE_M_2_SQRTPI 1.12837916709551257390 +#define LOVE_M_SQRT2 1.41421356237309504880 +#define LOVE_M_SQRT1_2 0.707106781186547524401 +#define LOVE_M_TORAD (float)(LOVE_M_PI/180.0) +#define LOVE_M_TODEG (float)(180.0/LOVE_M_PI) +#define LOVE_TORAD(x) (float)(x*LOVE_M_TORAD) +#define LOVE_TODEG(x) (float)(x*LOVE_M_TODEG) + +namespace love +{ + +struct vertex +{ + unsigned char r, g, b, a; + float x, y; + float s, t; +}; + +inline int next_p2(int x) +{ + x += (x == 0); + x--; + for (unsigned int i = 1; i < sizeof(int)*CHAR_BIT; i <<= 1) x |= x >> i; + return ++x; +} + +inline float next_p2(float x) +{ + return static_cast(next_p2(static_cast(x))); +} + +} // love + +#endif // LOVE_MATH_H diff --git a/src/common/runtime.cpp b/src/common/runtime.cpp index ccab8862c..f28cb2eda 100644 --- a/src/common/runtime.cpp +++ b/src/common/runtime.cpp @@ -1,480 +1,481 @@ -/** -* Copyright (c) 2006-2012 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. -**/ - -#include "runtime.h" - -// LOVE -#include "Module.h" -#include "Object.h" -#include "Reference.h" -#include "StringMap.h" -#include - -// STD -#include - - -namespace love -{ - static thread::Mutex *gcmutex = 0; - void *_gcmutex = 0; - unsigned int _gcthread = 0; - /** - * Called when an object is collected. The object is released - * once in this function, possibly deleting it. - **/ - static int w__gc(lua_State * L) - { - if (!gcmutex) - { - gcmutex = new thread::Mutex(); - _gcmutex = (void*) gcmutex; - } - Proxy * p = (Proxy *)lua_touserdata(L, 1); - Object * t = (Object *)p->data; - if (p->own) - { - thread::Lock lock(gcmutex); - _gcthread = thread::ThreadBase::threadId(); - t->release(); - } - return 0; - } - - static int w__tostring(lua_State * L) - { - lua_pushvalue(L, lua_upvalueindex(1)); - return 1; - } - - static int w__typeOf(lua_State * L) - { - Proxy * p = (Proxy *)lua_touserdata(L, 1); - Type t = luax_type(L, 2); - luax_pushboolean(L, p->flags[t]); - return 1; - } - - static int w__eq(lua_State * L) - { - Proxy * p1 = (Proxy *)lua_touserdata(L, 1); - Proxy * p2 = (Proxy *)lua_touserdata(L, 2); - luax_pushboolean(L, p1->data == p2->data); - return 1; - } - - Reference * luax_refif(lua_State * L, int type) - { - Reference * r = 0; - - // Create a reference only if the test succeeds. - if (lua_type(L, -1) == type) - r = new Reference(L); - else // Pop the value even if it fails (but also if it succeeds). - lua_pop(L, 1); - - return r; - } - - void luax_printstack(lua_State * L) - { - for (int i = 1;i<=lua_gettop(L);i++) - { - std::cout << i << " - " << luaL_typename(L, i) << std::endl; - } - } - - bool luax_toboolean(lua_State * L, int idx) - { - return (lua_toboolean(L, idx) != 0); - } - - void luax_pushboolean(lua_State * L, bool b) - { - lua_pushboolean(L, b ? 1 : 0); - } - - bool luax_optboolean(lua_State * L, int idx, bool b) - { - if (lua_isboolean(L, idx) == 1) - return (lua_toboolean(L, idx) == 1 ? true : false); - return b; - } - - std::string luax_checkstring(lua_State * L, int idx) - { - size_t len; - const char * str = luaL_checklstring(L, idx, &len); - return std::string(str, len); - } - - void luax_pushstring(lua_State * L, std::string str) - { - lua_pushlstring(L, str.data(), str.size()); - } - - int luax_assert_argc(lua_State * L, int min) - { - int argc = lua_gettop(L); - if ( argc < min ) - return luaL_error(L, "Incorrect number of arguments. Got [%d], expected at least [%d]", argc, min); - return 0; - } - - int luax_assert_argc(lua_State * L, int min, int max) - { - int argc = lua_gettop(L); - if ( argc < min || argc > max) - return luaL_error(L, "Incorrect number of arguments. Got [%d], expected [%d-%d]", argc, min, max); - return 0; - } - - int luax_assert_function(lua_State * L, int n) - { - if (!lua_isfunction(L, n)) - return luaL_error(L, "Argument must be of type \"function\"."); - return 0; - } - - int luax_register_module(lua_State * L, const WrappedModule & m) - { - // Put a reference to the C++ module in Lua. - luax_getregistry(L, REGISTRY_MODULES); - - Proxy * p = (Proxy *)lua_newuserdata(L, sizeof(Proxy)); - p->own = true; - p->data = m.module; - p->flags = m.flags; - - luaL_newmetatable(L, m.module->getName()); - lua_pushvalue(L, -1); - lua_setfield(L, -2, "__index"); - lua_pushcfunction(L, w__gc); - lua_setfield(L, -2, "__gc"); - - lua_setmetatable(L, -2); - lua_setfield(L, -2, m.name); // _modules[name] = proxy - lua_pop(L, 1); - - // Gets the love table. - luax_insistglobal(L, "love"); - - // Create new table for module. - lua_newtable(L); - - // Register all the functions. - luaL_register(L, 0, m.functions); - - // Register types. - if (m.types != 0) - for (const lua_CFunction * t = m.types; *t != 0; t++) - (*t)(L); - - lua_pushvalue(L, -1); - lua_setfield(L, -3, m.name); // love.graphics = table - lua_remove(L, -2); // love - - return 1; - } - - int luax_preload(lua_State * L, lua_CFunction f, const char * name) - { - lua_getglobal(L, "package"); - lua_getfield(L, -1, "preload"); - lua_pushcfunction(L, f); - lua_setfield(L, -2, name); - lua_pop(L, 2); - return 0; - } - - int luax_register_type(lua_State * L, const char * tname, const luaL_Reg * f) - { - luaL_newmetatable(L, tname); - - // m.__index = m - lua_pushvalue(L, -1); - lua_setfield(L, -2, "__index"); - - // setup gc - lua_pushcfunction(L, w__gc); - lua_setfield(L, -2, "__gc"); - - // Add equality - lua_pushcfunction(L, w__eq); - lua_setfield(L, -2, "__eq"); - - // Add tostring function. - lua_pushstring(L, tname); - lua_pushcclosure(L, w__tostring, 1); - lua_setfield(L, -2, "__tostring"); - - // Add tostring to as type() as well. - lua_pushstring(L, tname); - lua_pushcclosure(L, w__tostring, 1); - lua_setfield(L, -2, "type"); - - // Add typeOf - lua_pushcfunction(L, w__typeOf); - lua_setfield(L, -2, "typeOf"); - - if (f != 0) - luaL_register(L, 0, f); - - lua_pop(L, 1); // Pops metatable. - return 0; - } - - int luax_table_insert(lua_State * L, int tindex, int vindex, int pos) - { - if (tindex < 0) - tindex = lua_gettop(L)+1+tindex; - if (vindex < 0) - vindex = lua_gettop(L)+1+vindex; - if (pos == -1) - { - lua_pushvalue(L, vindex); - lua_rawseti(L, tindex, lua_objlen(L, tindex)+1); - return 0; - } - else if (pos < 0) - pos = lua_objlen(L, tindex)+1+pos; - for (int i = lua_objlen(L, tindex)+1; i > pos; i--) - { - lua_rawgeti(L, tindex, i-1); - lua_rawseti(L, tindex, i); - } - lua_pushvalue(L, vindex); - lua_rawseti(L, tindex, pos); - return 0; - } - - int luax_register_searcher(lua_State * L, lua_CFunction f, int pos) - { - // Add the package loader to the package.loaders table. - lua_getglobal(L, "package"); - - if (lua_isnil(L, -1)) - return luaL_error(L, "Can't register searcher: package table does not exist."); - - lua_getfield(L, -1, "loaders"); - - if (lua_isnil(L, -1)) - return luaL_error(L, "Can't register searcher: package.loaders table does not exist."); - - lua_pushcfunction(L, f); - luax_table_insert(L, -2, -1, pos); - lua_pop(L, 3); - return 0; - } - - void luax_newtype(lua_State * L, const char * name, bits flags, void * data, bool own) - { - Proxy * u = (Proxy *)lua_newuserdata(L, sizeof(Proxy)); - - u->data = data; - u->flags = flags; - u->own = own; - - luaL_newmetatable(L, name); - lua_setmetatable(L, -2); - } - - bool luax_istype(lua_State * L, int idx, love::bits type) - { - if (lua_isuserdata(L, idx) == 0) - return false; - - return ((((Proxy *)lua_touserdata(L, idx))->flags & type) == type); - } - - int luax_getfunction(lua_State * L, const char * mod, const char * fn) - { - lua_getglobal(L, "love"); - if (lua_isnil(L, -1)) return luaL_error(L, "Could not find global love!"); - lua_getfield(L, -1, mod); - if (lua_isnil(L, -1)) return luaL_error(L, "Could not find love.%s!", mod); - lua_getfield(L, -1, fn); - if (lua_isnil(L, -1)) return luaL_error(L, "Could not find love.%s.%s!", mod, fn); - - lua_remove(L, -2); // remove mod - lua_remove(L, -2); // remove fn - return 0; - } - - int luax_convobj(lua_State * L, int idx, const char * mod, const char * fn) - { - // Convert string to a file. - luax_getfunction(L, mod, fn); - lua_pushvalue(L, idx); // The initial argument. - lua_call(L, 1, 1); // Call the function, one arg, one return value. - lua_replace(L, idx); // Replace the initial argument with the new object. - return 0; - } - - int luax_convobj(lua_State * L, int idxs[], int n, const char * mod, const char * fn) - { - luax_getfunction(L, mod, fn); - for (int i = 0; i < n; i++) - { - lua_pushvalue(L, idxs[i]); // The arguments. - } - lua_call(L, n, 1); // Call the function, n args, one return value. - lua_replace(L, idxs[0]); // Replace the initial argument with the new object. - return 0; - } - - int luax_strtofile(lua_State * L, int idx) - { - return luax_convobj(L, idx, "filesystem", "newFile"); - } - - int luax_filetodata(lua_State * L, int idx) - { - return luax_convobj(L, idx, "filesystem", "read"); - } - - int luax_insist(lua_State * L, int idx, const char * k) - { - // Convert to absolute index if necessary. - if (idx < 0 && idx > LUA_REGISTRYINDEX) - idx = lua_gettop(L) + ++idx; - - lua_getfield(L, idx, k); - - // Create if necessary. - if (!lua_istable(L, -1)) - { - lua_pop(L, 1); // Pop the non-table. - lua_newtable(L); - lua_pushvalue(L, -1); // Duplicate the table to leave on top. - lua_setfield(L, idx, k); // lua_stack[idx][k] = lua_stack[-1] (table) - } - - return 1; - } - - int luax_insistglobal(lua_State * L, const char * k) - { - lua_getglobal(L, k); - - if (!lua_istable(L, -1)) - { - lua_pop(L, 1); // Pop the non-table. - lua_newtable(L); - lua_pushvalue(L, -1); - lua_setglobal(L, k); - } - - return 1; - } - - int luax_insistlove(lua_State * L, const char * k) - { - luax_insistglobal(L, "love"); - luax_insist(L, -1, k); - - // The love table should be replaced with the top stack - // item. Only the reqested table should remain on the stack. - lua_replace(L, -2); - - return 1; - } - - int luax_getregistry(lua_State * L, Registry r) - { - switch(r) - { - case REGISTRY_GC: - return luax_insistlove(L, "_gc"); - case REGISTRY_MODULES: - return luax_insistlove(L, "_modules"); - default: - return luaL_error(L, "Attempted to use invalid registry."); - } - } - - StringMap::Entry typeEntries[] = - { - {"Invalid", INVALID_ID}, - - {"Object", OBJECT_ID}, - {"Data", DATA_ID}, - {"Module", MODULE_ID}, - - // Filesystem - {"File", FILESYSTEM_FILE_ID}, - {"FileData", FILESYSTEM_FILE_DATA_ID}, - - // Font - {"GlyphData", FONT_GLYPH_DATA_ID}, - {"Rasterizer", FONT_RASTERIZER_ID}, - - // Graphics - {"Drawable", GRAPHICS_DRAWABLE_ID}, - {"Image", GRAPHICS_IMAGE_ID}, - {"Quad", GRAPHICS_QUAD_ID}, - {"Font", GRAPHICS_FONT_ID}, - {"ParticleSystem", GRAPHICS_PARTICLE_SYSTEM_ID}, - {"SpriteBatch", GRAPHICS_SPRITE_BATCH_ID}, - {"Canvas", GRAPHICS_CANVAS_ID}, - - // Image - {"ImageData", IMAGE_IMAGE_DATA_ID}, - - // Audio - {"Source", AUDIO_SOURCE_ID}, - - // Sound - {"SoundData", SOUND_SOUND_DATA_ID}, - {"Decoder", SOUND_DECODER_ID}, - - // Physics - {"World", PHYSICS_WORLD_ID}, - {"Contact", PHYSICS_CONTACT_ID}, - {"Body", PHYSICS_BODY_ID}, - {"Shape", PHYSICS_SHAPE_ID}, - {"CircleShape", PHYSICS_CIRCLE_SHAPE_ID}, - {"PolygonShape", PHYSICS_POLYGON_SHAPE_ID}, - {"Joint", PHYSICS_JOINT_ID}, - {"MouseJoint", PHYSICS_MOUSE_JOINT_ID}, - {"DistanceJoint", PHYSICS_DISTANCE_JOINT_ID}, - {"PrismaticJoint", PHYSICS_PRISMATIC_JOINT_ID}, - {"RevoluteJoint", PHYSICS_REVOLUTE_JOINT_ID}, - {"PulleyJoint", PHYSICS_PULLEY_JOINT_ID}, - {"GearJoint", PHYSICS_GEAR_JOINT_ID}, - - // Thread - {"Thread", THREAD_THREAD_ID}, - - // The modules themselves. Only add abstracted modules here. - {"filesystem", MODULE_FILESYSTEM_ID}, - {"image", MODULE_IMAGE_ID}, - {"sound", MODULE_SOUND_ID}, - }; - - StringMap types(typeEntries, sizeof(typeEntries)); - - Type luax_type(lua_State * L, int idx) - { - Type t = INVALID_ID; - types.find(luaL_checkstring(L, idx), t); - return t; - } -} // love +/** + * Copyright (c) 2006-2012 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. + **/ + +#include "runtime.h" + +// LOVE +#include "Module.h" +#include "Object.h" +#include "Reference.h" +#include "StringMap.h" +#include "thread/threads.h" + +// STD +#include + +namespace love +{ + +static thread::Mutex *gcmutex = 0; +void *_gcmutex = 0; +unsigned int _gcthread = 0; +/** + * Called when an object is collected. The object is released + * once in this function, possibly deleting it. + **/ +static int w__gc(lua_State *L) +{ + if (!gcmutex) + { + gcmutex = new thread::Mutex(); + _gcmutex = (void *) gcmutex; + } + Proxy *p = (Proxy *)lua_touserdata(L, 1); + Object *t = (Object *)p->data; + if (p->own) + { + thread::Lock lock(gcmutex); + _gcthread = thread::ThreadBase::threadId(); + t->release(); + } + return 0; +} + +static int w__tostring(lua_State *L) +{ + lua_pushvalue(L, lua_upvalueindex(1)); + return 1; +} + +static int w__typeOf(lua_State *L) +{ + Proxy *p = (Proxy *)lua_touserdata(L, 1); + Type t = luax_type(L, 2); + luax_pushboolean(L, p->flags[t]); + return 1; +} + +static int w__eq(lua_State *L) +{ + Proxy *p1 = (Proxy *)lua_touserdata(L, 1); + Proxy *p2 = (Proxy *)lua_touserdata(L, 2); + luax_pushboolean(L, p1->data == p2->data); + return 1; +} + +Reference *luax_refif (lua_State *L, int type) +{ + Reference *r = 0; + + // Create a reference only if the test succeeds. + if (lua_type(L, -1) == type) + r = new Reference(L); + else // Pop the value even if it fails (but also if it succeeds). + lua_pop(L, 1); + + return r; +} + +void luax_printstack(lua_State *L) +{ + for (int i = 1; i<=lua_gettop(L); i++) + { + std::cout << i << " - " << luaL_typename(L, i) << std::endl; + } +} + +bool luax_toboolean(lua_State *L, int idx) +{ + return (lua_toboolean(L, idx) != 0); +} + +void luax_pushboolean(lua_State *L, bool b) +{ + lua_pushboolean(L, b ? 1 : 0); +} + +bool luax_optboolean(lua_State *L, int idx, bool b) +{ + if (lua_isboolean(L, idx) == 1) + return (lua_toboolean(L, idx) == 1 ? true : false); + return b; +} + +std::string luax_checkstring(lua_State *L, int idx) +{ + size_t len; + const char *str = luaL_checklstring(L, idx, &len); + return std::string(str, len); +} + +void luax_pushstring(lua_State *L, std::string str) +{ + lua_pushlstring(L, str.data(), str.size()); +} + +int luax_assert_argc(lua_State *L, int min) +{ + int argc = lua_gettop(L); + if (argc < min) + return luaL_error(L, "Incorrect number of arguments. Got [%d], expected at least [%d]", argc, min); + return 0; +} + +int luax_assert_argc(lua_State *L, int min, int max) +{ + int argc = lua_gettop(L); + if (argc < min || argc > max) + return luaL_error(L, "Incorrect number of arguments. Got [%d], expected [%d-%d]", argc, min, max); + return 0; +} + +int luax_assert_function(lua_State *L, int n) +{ + if (!lua_isfunction(L, n)) + return luaL_error(L, "Argument must be of type \"function\"."); + return 0; +} + +int luax_register_module(lua_State *L, const WrappedModule &m) +{ + // Put a reference to the C++ module in Lua. + luax_getregistry(L, REGISTRY_MODULES); + + Proxy *p = (Proxy *)lua_newuserdata(L, sizeof(Proxy)); + p->own = true; + p->data = m.module; + p->flags = m.flags; + + luaL_newmetatable(L, m.module->getName()); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushcfunction(L, w__gc); + lua_setfield(L, -2, "__gc"); + + lua_setmetatable(L, -2); + lua_setfield(L, -2, m.name); // _modules[name] = proxy + lua_pop(L, 1); + + // Gets the love table. + luax_insistglobal(L, "love"); + + // Create new table for module. + lua_newtable(L); + + // Register all the functions. + luaL_register(L, 0, m.functions); + + // Register types. + if (m.types != 0) + for (const lua_CFunction *t = m.types; *t != 0; t++) + (*t)(L); + + lua_pushvalue(L, -1); + lua_setfield(L, -3, m.name); // love.graphics = table + lua_remove(L, -2); // love + + return 1; +} + +int luax_preload(lua_State *L, lua_CFunction f, const char *name) +{ + lua_getglobal(L, "package"); + lua_getfield(L, -1, "preload"); + lua_pushcfunction(L, f); + lua_setfield(L, -2, name); + lua_pop(L, 2); + return 0; +} + +int luax_register_type(lua_State *L, const char *tname, const luaL_Reg *f) +{ + luaL_newmetatable(L, tname); + + // m.__index = m + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + + // setup gc + lua_pushcfunction(L, w__gc); + lua_setfield(L, -2, "__gc"); + + // Add equality + lua_pushcfunction(L, w__eq); + lua_setfield(L, -2, "__eq"); + + // Add tostring function. + lua_pushstring(L, tname); + lua_pushcclosure(L, w__tostring, 1); + lua_setfield(L, -2, "__tostring"); + + // Add tostring to as type() as well. + lua_pushstring(L, tname); + lua_pushcclosure(L, w__tostring, 1); + lua_setfield(L, -2, "type"); + + // Add typeOf + lua_pushcfunction(L, w__typeOf); + lua_setfield(L, -2, "typeOf"); + + if (f != 0) + luaL_register(L, 0, f); + + lua_pop(L, 1); // Pops metatable. + return 0; +} + +int luax_table_insert(lua_State *L, int tindex, int vindex, int pos) +{ + if (tindex < 0) + tindex = lua_gettop(L)+1+tindex; + if (vindex < 0) + vindex = lua_gettop(L)+1+vindex; + if (pos == -1) + { + lua_pushvalue(L, vindex); + lua_rawseti(L, tindex, lua_objlen(L, tindex)+1); + return 0; + } + else if (pos < 0) + pos = lua_objlen(L, tindex)+1+pos; + for (int i = lua_objlen(L, tindex)+1; i > pos; i--) + { + lua_rawgeti(L, tindex, i-1); + lua_rawseti(L, tindex, i); + } + lua_pushvalue(L, vindex); + lua_rawseti(L, tindex, pos); + return 0; +} + +int luax_register_searcher(lua_State *L, lua_CFunction f, int pos) +{ + // Add the package loader to the package.loaders table. + lua_getglobal(L, "package"); + + if (lua_isnil(L, -1)) + return luaL_error(L, "Can't register searcher: package table does not exist."); + + lua_getfield(L, -1, "loaders"); + + if (lua_isnil(L, -1)) + return luaL_error(L, "Can't register searcher: package.loaders table does not exist."); + + lua_pushcfunction(L, f); + luax_table_insert(L, -2, -1, pos); + lua_pop(L, 3); + return 0; +} + +void luax_newtype(lua_State *L, const char *name, bits flags, void *data, bool own) +{ + Proxy *u = (Proxy *)lua_newuserdata(L, sizeof(Proxy)); + + u->data = data; + u->flags = flags; + u->own = own; + + luaL_newmetatable(L, name); + lua_setmetatable(L, -2); +} + +bool luax_istype(lua_State *L, int idx, love::bits type) +{ + if (lua_isuserdata(L, idx) == 0) + return false; + + return ((((Proxy *)lua_touserdata(L, idx))->flags & type) == type); +} + +int luax_getfunction(lua_State *L, const char *mod, const char *fn) +{ + lua_getglobal(L, "love"); + if (lua_isnil(L, -1)) return luaL_error(L, "Could not find global love!"); + lua_getfield(L, -1, mod); + if (lua_isnil(L, -1)) return luaL_error(L, "Could not find love.%s!", mod); + lua_getfield(L, -1, fn); + if (lua_isnil(L, -1)) return luaL_error(L, "Could not find love.%s.%s!", mod, fn); + + lua_remove(L, -2); // remove mod + lua_remove(L, -2); // remove fn + return 0; +} + +int luax_convobj(lua_State *L, int idx, const char *mod, const char *fn) +{ + // Convert string to a file. + luax_getfunction(L, mod, fn); + lua_pushvalue(L, idx); // The initial argument. + lua_call(L, 1, 1); // Call the function, one arg, one return value. + lua_replace(L, idx); // Replace the initial argument with the new object. + return 0; +} + +int luax_convobj(lua_State *L, int idxs[], int n, const char *mod, const char *fn) +{ + luax_getfunction(L, mod, fn); + for (int i = 0; i < n; i++) + { + lua_pushvalue(L, idxs[i]); // The arguments. + } + lua_call(L, n, 1); // Call the function, n args, one return value. + lua_replace(L, idxs[0]); // Replace the initial argument with the new object. + return 0; +} + +int luax_strtofile(lua_State *L, int idx) +{ + return luax_convobj(L, idx, "filesystem", "newFile"); +} + +int luax_filetodata(lua_State *L, int idx) +{ + return luax_convobj(L, idx, "filesystem", "read"); +} + +int luax_insist(lua_State *L, int idx, const char *k) +{ + // Convert to absolute index if necessary. + if (idx < 0 && idx > LUA_REGISTRYINDEX) + idx = lua_gettop(L) + ++idx; + + lua_getfield(L, idx, k); + + // Create if necessary. + if (!lua_istable(L, -1)) + { + lua_pop(L, 1); // Pop the non-table. + lua_newtable(L); + lua_pushvalue(L, -1); // Duplicate the table to leave on top. + lua_setfield(L, idx, k); // lua_stack[idx][k] = lua_stack[-1] (table) + } + + return 1; +} + +int luax_insistglobal(lua_State *L, const char *k) +{ + lua_getglobal(L, k); + + if (!lua_istable(L, -1)) + { + lua_pop(L, 1); // Pop the non-table. + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setglobal(L, k); + } + + return 1; +} + +int luax_insistlove(lua_State *L, const char *k) +{ + luax_insistglobal(L, "love"); + luax_insist(L, -1, k); + + // The love table should be replaced with the top stack + // item. Only the reqested table should remain on the stack. + lua_replace(L, -2); + + return 1; +} + +int luax_getregistry(lua_State *L, Registry r) +{ + switch (r) + { + case REGISTRY_GC: + return luax_insistlove(L, "_gc"); + case REGISTRY_MODULES: + return luax_insistlove(L, "_modules"); + default: + return luaL_error(L, "Attempted to use invalid registry."); + } +} + +StringMap::Entry typeEntries[] = +{ + {"Invalid", INVALID_ID}, + + {"Object", OBJECT_ID}, + {"Data", DATA_ID}, + {"Module", MODULE_ID}, + + // Filesystem + {"File", FILESYSTEM_FILE_ID}, + {"FileData", FILESYSTEM_FILE_DATA_ID}, + + // Font + {"GlyphData", FONT_GLYPH_DATA_ID}, + {"Rasterizer", FONT_RASTERIZER_ID}, + + // Graphics + {"Drawable", GRAPHICS_DRAWABLE_ID}, + {"Image", GRAPHICS_IMAGE_ID}, + {"Quad", GRAPHICS_QUAD_ID}, + {"Font", GRAPHICS_FONT_ID}, + {"ParticleSystem", GRAPHICS_PARTICLE_SYSTEM_ID}, + {"SpriteBatch", GRAPHICS_SPRITE_BATCH_ID}, + {"Canvas", GRAPHICS_CANVAS_ID}, + + // Image + {"ImageData", IMAGE_IMAGE_DATA_ID}, + + // Audio + {"Source", AUDIO_SOURCE_ID}, + + // Sound + {"SoundData", SOUND_SOUND_DATA_ID}, + {"Decoder", SOUND_DECODER_ID}, + + // Physics + {"World", PHYSICS_WORLD_ID}, + {"Contact", PHYSICS_CONTACT_ID}, + {"Body", PHYSICS_BODY_ID}, + {"Shape", PHYSICS_SHAPE_ID}, + {"CircleShape", PHYSICS_CIRCLE_SHAPE_ID}, + {"PolygonShape", PHYSICS_POLYGON_SHAPE_ID}, + {"Joint", PHYSICS_JOINT_ID}, + {"MouseJoint", PHYSICS_MOUSE_JOINT_ID}, + {"DistanceJoint", PHYSICS_DISTANCE_JOINT_ID}, + {"PrismaticJoint", PHYSICS_PRISMATIC_JOINT_ID}, + {"RevoluteJoint", PHYSICS_REVOLUTE_JOINT_ID}, + {"PulleyJoint", PHYSICS_PULLEY_JOINT_ID}, + {"GearJoint", PHYSICS_GEAR_JOINT_ID}, + + // Thread + {"Thread", THREAD_THREAD_ID}, + + // The modules themselves. Only add abstracted modules here. + {"filesystem", MODULE_FILESYSTEM_ID}, + {"image", MODULE_IMAGE_ID}, + {"sound", MODULE_SOUND_ID}, +}; + +StringMap types(typeEntries, sizeof(typeEntries)); + +Type luax_type(lua_State *L, int idx) +{ + Type t = INVALID_ID; + types.find(luaL_checkstring(L, idx), t); + return t; +} + +} // love diff --git a/src/common/runtime.h b/src/common/runtime.h index 74122659d..14a2979ff 100644 --- a/src/common/runtime.h +++ b/src/common/runtime.h @@ -1,383 +1,384 @@ -/** -* Copyright (c) 2006-2012 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_RUNTIME_H -#define LOVE_RUNTIME_H - -// LOVE -#include "types.h" - -// Lua -extern "C" { - #include - #include - #include -} - -namespace love -{ - // Forward declarations. - class Module; - class Reference; - - // Exposed mutex of the GC - extern void *_gcmutex; - extern unsigned int _gcthread; - - /** - * Registries represent special tables which can be accessed with - * luax_getregistry. - **/ - enum Registry - { - REGISTRY_GC = 1, - REGISTRY_MODULES, - }; - - /** - * This structure wraps all Lua-exposed objects. It exists in the - * Lua state as a full userdata (so we can catch __gc "events"), - * though the Object it refers to is light userdata in the sense - * that it is not allocated by the Lua VM. - **/ - struct Proxy - { - // Holds type information (see types.h). - bits flags; - - // The light userdata. - void * data; - - // True if Lua should delete on GC. - bool own; - }; - - /** - * A Module with Lua wrapper functions and other data. - **/ - struct WrappedModule - { - // The module containing the functions. - Module * module; - - // The name for the table to put the functions in, without the 'love'-prefix. - const char * name; - - // The type flags of this module. - love::bits flags; - - // The functions of the module (last element {0,0}). - const luaL_Reg * functions; - - // A list of functions which expose the types of the modules (last element 0). - const lua_CFunction * types; - - }; - - /** - * Returns a reference to the top stack element (-1) if the value - * is of the specified type. If the value is incorrect, zero is returned. - * - * In any case, the top stack element is popped, regardless of its type. - **/ - Reference * luax_refif(lua_State * L, int type); - - /** - * Prints the current contents of the stack. Only useful for debugging. - * @param L The Lua state. - **/ - void luax_printstack(lua_State * L); - - /** - * Converts the value at idx to a bool. It follow the same rules - * as lua_toboolean, but returns a bool instead of an int. - * @param L The Lua state. - * @param idx The index on the Lua stack. - * @return True if the value evaluates to true, false otherwise. - **/ - bool luax_toboolean(lua_State * L, int idx); - - /** - * Pushes a bool onto the stack. It's the same as lua_pushboolean, - * but with bool instead of int. - * @param L The Lua state. - * @param b The bool to push. - **/ - void luax_pushboolean(lua_State * L, bool b); - - /** - * Converts the value at idx to a bool, or if not present, b is returned. - * @param L The Lua state. - * @param idx The index of the Lua stack. - * @param b The value to return if no value exist at the specified index. - * @return True if the value evaluates to true, false otherwise. - **/ - bool luax_optboolean(lua_State * L, int idx, bool b); - - /** - * Converts the value at idx to a std::string. It takes care of the string - * size and possible embedded nulls. - * @param L The Lua state. - * @param idx The index on the Lua stack. - * @return Copy of the string at the specified index. - **/ - std::string luax_checkstring(lua_State * L, int idx); - - /** - * Pushes a std::string onto the stack. It uses the length of the string - * for lua_pushlstring's len argument. - * @param L The Lua state. - * @param str The string to push. - **/ - void luax_pushstring(lua_State * L, std::string str); - - /** - * Require at least 'min' number of items on the stack. - * @param L The Lua state. - * @param min The minimum number of items on the stack. - * @return Zero if conditions are met, otherwise a Lua error (longjmp). - **/ - int luax_assert_argc(lua_State * L, int min); - - /** - * Require at least 'min', but more than 'max' items on the stack. - * @param L The Lua state. - * @param min The minimum number of items on the stack. - * @param max The maximum number of items on the stack. - * @return Zero if conditions are met, otherwise a Lua error (longjmp). - **/ - int luax_assert_argc(lua_State * L, int min, int max); - - /** - * Require that the value at idx is a function. - * @param L The Lua state. - *@param idx The index on the stack. - **/ - int luax_assert_function(lua_State * L, int idx); - - /** - * Register a module in the love table. The love table will be created if it does not exist. - * @param L The Lua state. - **/ - int luax_register_module(lua_State * L, const WrappedModule & m); - - /** - * Inserts a module with 'name' into the package.preloaded table. - * @param f The function to be called when the module is opened. - * @param name The name of the module, with 'love'-prefix, for instance 'love.graphics'. - **/ - int luax_preload(lua_State * L, lua_CFunction f, const char * name); - - /** - * Register a new type. - * @param tname The name of the type. This must not conflict with other type names, - * even from other modules. - * @param f The list of member functions for the type. - **/ - int luax_register_type(lua_State * L, const char * tname, const luaL_Reg * f = 0); - - /** - * Do a table.insert from C - * @param L the state - * @param tindex the stack index of the table - * @param vindex the stack index of the value - * @param pos the position to insert it in - **/ - int luax_table_insert(lua_State * L, int tindex, int vindex, int pos = -1); - - /** - * Register a new searcher function for package.loaders. This can for instance enable - * loading of files through love.filesystem using standard require. - * @param L The Lua state. - * @param f The searcher function. - * @param pos The position to insert the loader in. - **/ - int luax_register_searcher(lua_State * L, lua_CFunction f, int pos = -1); - - /** - * Creates a new Lua-accessible object of the given type, and put it on the stack. - * @param L The Lua state. - * @param name The name of the type. This must match the used earlier with luax_register_type. - * @param flags The type information. - * @param data The pointer to the actual object. - * @own Set this to true (default) if the object should be released upon garbage collection. - **/ - void luax_newtype(lua_State * L, const char * name, bits flags, void * data, bool own = true); - - /** - * Checks whether the value at idx is a certain type. - * @param L The Lua state. - * @param idx The index on the stack. - * @param type The type to check for. - * @return True if the value is Proxy of the specified type, false otherwise. - **/ - bool luax_istype(lua_State * L, int idx, love::bits type); - - /** - * Gets the function love.module.function and puts it on top of the stack (alone). If the - * love table, the module, or the function does not exist, an error is returned. - * @return An error if nonexistent, or 1 if successful. - **/ - int luax_getfunction(lua_State * L, const char * module, const char * function); - - /** - * Converts an object into another object by the specified function love.module.function. - * The conversion function must accept a single object of the relevant type as a parameter, - * and returnone value. If the function does not exist (see luax_getfunction), an error is returned. - * - * Note that the initial object at idx is replaced by the new object. - * - * @param L The Lua state. - * @param idx The index on the stack. - * @param module The module in the love table. - * @param function The function in the module. - **/ - int luax_convobj(lua_State * L, int idx, const char * module, const char * function); - - /** - * Converts an object into another object by the specified function love.module.function. - * The conversion function must accept a single object of the relevant type as its first parameter, - * and return one value. If the function does not exist (see luax_getfunction), an error is returned. - * - * Note that the initial object at idx is replaced by the new object. - * - * @param L The Lua state. - * @param idxs An array of indices on the stack. - * @param n How many arguments are being passed. - * @param module The module in the love table. - * @param function The function in the module. - **/ - int luax_convobj(lua_State * L, int idxs[], int n, const char * module, const char * function); - - /** - * 'Insist' that a table 'k' exists in the table at idx. Insistence involves that the - * table (k) is created if it does not exist in the table at idx. The table at idx must - * pre-exist, however. Also note that if the a non-table value exists at the specified - * location, it will be overwritten with a new table. The insisted table, and only the - * insisted table, will be placed on top of the stack. - * - * @param idx The index on the stack containing a table. - * @param k The name of the table we are insisting exist. - **/ - int luax_insist(lua_State * L, int idx, const char * k); - - /** - * Insist that a global table 'k' exists. See luax_insist. - * @param k The name of the table we are insisting exist. - **/ - int luax_insistglobal(lua_State * L, const char * k); - - /** - * Insists that a table 'k' exists inside the 'love' table. See luax_insist. - * @param k The name of the table we are insisting exist. - **/ - int luax_insistlove(lua_State * L, const char * k); - - /** - * Gets (creates if needed) the specified Registry, and puts it on top - * of the stack. - * @param L The Lua state. - * @param r The Registry to get. - **/ - int luax_getregistry(lua_State * L, Registry r); - - Type luax_type(lua_State * L, int idx); - - /** - * Convert the value at the specified index to an Lua number, and then - * convert to a float. - * - * @param L The Lua state. - * @param idx The index on the stack. - */ - inline float luax_tofloat(lua_State *L, int idx) - { - return static_cast(lua_tonumber(L, idx)); - } - - /** - * Like luax_tofloat, but checks that the value is a number. - * - * @see luax_tofloat - */ - inline float luax_checkfloat(lua_State *L, int idx) - { - return static_cast(luaL_checknumber(L, idx)); - } - - /** - * Converts the value at idx to the specified type without checking that - * this conversion is valid. If the type has been previously verified with - * luax_istype, then this can be safely used. Otherwise, use luax_checktype. - * @param L The Lua state. - * @param idx The index on the stack. - * @param name The name of the type. - * @param type The type bit. - **/ - template - T * luax_totype(lua_State * L, int idx, const char *, love::bits) - { - return (T*)(((Proxy *)lua_touserdata(L, idx))->data); - } - - /** - * Like luax_totype, but causes an error if the value at idx is not Proxy, - * or is not the specified type. - * @param L The Lua state. - * @param idx The index on the stack. - * @param name The name of the type. - * @param type The type bit. - **/ - template - T * luax_checktype(lua_State * L, int idx, const char * name, love::bits type) - { - if (lua_isuserdata(L, idx) == 0) - luaL_error(L, "Incorrect parameter type: expected userdata."); - - Proxy * u = (Proxy *)lua_touserdata(L, idx); - - if ((u->flags & type) != type) - luaL_error(L, "Incorrect parameter type: expected %s", name); - - return (T *)u->data; - } - - template - T * luax_getmodule(lua_State * L, const char * k, love::bits type) - { - luax_getregistry(L, REGISTRY_MODULES); - lua_getfield(L, -1, k); - - if (!lua_isuserdata(L, -1)) - luaL_error(L, "Tried to get nonexisting module %s.", k); - - Proxy * u = (Proxy *)lua_touserdata(L, -1); - - if ((u->flags & type) != type) - luaL_error(L, "Incorrect module %s", k); - - lua_pop(L, 2); - - return (T*)u->data; - } - -} // love - -#endif // LOVE_RUNTIME_H +/** + * Copyright (c) 2006-2012 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_RUNTIME_H +#define LOVE_RUNTIME_H + +// LOVE +#include "types.h" + +// Lua +extern "C" { + #include + #include + #include +} + +namespace love +{ + +// Forward declarations. +class Module; +class Reference; + +// Exposed mutex of the GC +extern void *_gcmutex; +extern unsigned int _gcthread; + +/** + * Registries represent special tables which can be accessed with + * luax_getregistry. + **/ +enum Registry +{ + REGISTRY_GC = 1, + REGISTRY_MODULES, +}; + +/** + * This structure wraps all Lua-exposed objects. It exists in the + * Lua state as a full userdata (so we can catch __gc "events"), + * though the Object it refers to is light userdata in the sense + * that it is not allocated by the Lua VM. + **/ +struct Proxy +{ + // Holds type information (see types.h). + bits flags; + + // The light userdata. + void *data; + + // True if Lua should delete on GC. + bool own; +}; + +/** + * A Module with Lua wrapper functions and other data. + **/ +struct WrappedModule +{ + // The module containing the functions. + Module *module; + + // The name for the table to put the functions in, without the 'love'-prefix. + const char *name; + + // The type flags of this module. + love::bits flags; + + // The functions of the module (last element {0,0}). + const luaL_Reg *functions; + + // A list of functions which expose the types of the modules (last element 0). + const lua_CFunction *types; + +}; + +/** + * Returns a reference to the top stack element (-1) if the value + * is of the specified type. If the value is incorrect, zero is returned. + * + * In any case, the top stack element is popped, regardless of its type. + **/ +Reference *luax_refif (lua_State *L, int type); + +/** + * Prints the current contents of the stack. Only useful for debugging. + * @param L The Lua state. + **/ +void luax_printstack(lua_State *L); + +/** + * Converts the value at idx to a bool. It follow the same rules + * as lua_toboolean, but returns a bool instead of an int. + * @param L The Lua state. + * @param idx The index on the Lua stack. + * @return True if the value evaluates to true, false otherwise. + **/ +bool luax_toboolean(lua_State *L, int idx); + +/** + * Pushes a bool onto the stack. It's the same as lua_pushboolean, + * but with bool instead of int. + * @param L The Lua state. + * @param b The bool to push. + **/ +void luax_pushboolean(lua_State *L, bool b); + +/** + * Converts the value at idx to a bool, or if not present, b is returned. + * @param L The Lua state. + * @param idx The index of the Lua stack. + * @param b The value to return if no value exist at the specified index. + * @return True if the value evaluates to true, false otherwise. + **/ +bool luax_optboolean(lua_State *L, int idx, bool b); + +/** + * Converts the value at idx to a std::string. It takes care of the string + * size and possible embedded nulls. + * @param L The Lua state. + * @param idx The index on the Lua stack. + * @return Copy of the string at the specified index. + **/ +std::string luax_checkstring(lua_State *L, int idx); + +/** + * Pushes a std::string onto the stack. It uses the length of the string + * for lua_pushlstring's len argument. + * @param L The Lua state. + * @param str The string to push. + **/ +void luax_pushstring(lua_State *L, std::string str); + +/** + * Require at least 'min' number of items on the stack. + * @param L The Lua state. + * @param min The minimum number of items on the stack. + * @return Zero if conditions are met, otherwise a Lua error (longjmp). + **/ +int luax_assert_argc(lua_State *L, int min); + +/** + * Require at least 'min', but more than 'max' items on the stack. + * @param L The Lua state. + * @param min The minimum number of items on the stack. + * @param max The maximum number of items on the stack. + * @return Zero if conditions are met, otherwise a Lua error (longjmp). + **/ +int luax_assert_argc(lua_State *L, int min, int max); + +/** + * Require that the value at idx is a function. + * @param L The Lua state. + *@param idx The index on the stack. + **/ +int luax_assert_function(lua_State *L, int idx); + +/** + * Register a module in the love table. The love table will be created if it does not exist. + * @param L The Lua state. + **/ +int luax_register_module(lua_State *L, const WrappedModule &m); + +/** + * Inserts a module with 'name' into the package.preloaded table. + * @param f The function to be called when the module is opened. + * @param name The name of the module, with 'love'-prefix, for instance 'love.graphics'. + **/ +int luax_preload(lua_State *L, lua_CFunction f, const char *name); + +/** + * Register a new type. + * @param tname The name of the type. This must not conflict with other type names, + * even from other modules. + * @param f The list of member functions for the type. + **/ +int luax_register_type(lua_State *L, const char *tname, const luaL_Reg *f = 0); + +/** + * Do a table.insert from C + * @param L the state + * @param tindex the stack index of the table + * @param vindex the stack index of the value + * @param pos the position to insert it in + **/ +int luax_table_insert(lua_State *L, int tindex, int vindex, int pos = -1); + +/** + * Register a new searcher function for package.loaders. This can for instance enable + * loading of files through love.filesystem using standard require. + * @param L The Lua state. + * @param f The searcher function. + * @param pos The position to insert the loader in. + **/ +int luax_register_searcher(lua_State *L, lua_CFunction f, int pos = -1); + +/** + * Creates a new Lua-accessible object of the given type, and put it on the stack. + * @param L The Lua state. + * @param name The name of the type. This must match the used earlier with luax_register_type. + * @param flags The type information. + * @param data The pointer to the actual object. + * @own Set this to true (default) if the object should be released upon garbage collection. + **/ +void luax_newtype(lua_State *L, const char *name, bits flags, void *data, bool own = true); + +/** + * Checks whether the value at idx is a certain type. + * @param L The Lua state. + * @param idx The index on the stack. + * @param type The type to check for. + * @return True if the value is Proxy of the specified type, false otherwise. + **/ +bool luax_istype(lua_State *L, int idx, love::bits type); + +/** + * Gets the function love.module.function and puts it on top of the stack (alone). If the + * love table, the module, or the function does not exist, an error is returned. + * @return An error if nonexistent, or 1 if successful. + **/ +int luax_getfunction(lua_State *L, const char *module, const char *function); + +/** + * Converts an object into another object by the specified function love.module.function. + * The conversion function must accept a single object of the relevant type as a parameter, + * and returnone value. If the function does not exist (see luax_getfunction), an error is returned. + * + * Note that the initial object at idx is replaced by the new object. + * + * @param L The Lua state. + * @param idx The index on the stack. + * @param module The module in the love table. + * @param function The function in the module. + **/ +int luax_convobj(lua_State *L, int idx, const char *module, const char *function); + +/** + * Converts an object into another object by the specified function love.module.function. + * The conversion function must accept a single object of the relevant type as its first parameter, + * and return one value. If the function does not exist (see luax_getfunction), an error is returned. + * + * Note that the initial object at idx is replaced by the new object. + * + * @param L The Lua state. + * @param idxs An array of indices on the stack. + * @param n How many arguments are being passed. + * @param module The module in the love table. + * @param function The function in the module. + **/ +int luax_convobj(lua_State *L, int idxs[], int n, const char *module, const char *function); + +/** + * 'Insist' that a table 'k' exists in the table at idx. Insistence involves that the + * table (k) is created if it does not exist in the table at idx. The table at idx must + * pre-exist, however. Also note that if the a non-table value exists at the specified + * location, it will be overwritten with a new table. The insisted table, and only the + * insisted table, will be placed on top of the stack. + * + * @param idx The index on the stack containing a table. + * @param k The name of the table we are insisting exist. + **/ +int luax_insist(lua_State *L, int idx, const char *k); + +/** + * Insist that a global table 'k' exists. See luax_insist. + * @param k The name of the table we are insisting exist. + **/ +int luax_insistglobal(lua_State *L, const char *k); + +/** + * Insists that a table 'k' exists inside the 'love' table. See luax_insist. + * @param k The name of the table we are insisting exist. + **/ +int luax_insistlove(lua_State *L, const char *k); + +/** + * Gets (creates if needed) the specified Registry, and puts it on top + * of the stack. + * @param L The Lua state. + * @param r The Registry to get. + **/ +int luax_getregistry(lua_State *L, Registry r); + +Type luax_type(lua_State *L, int idx); + +/** + * Convert the value at the specified index to an Lua number, and then + * convert to a float. + * + * @param L The Lua state. + * @param idx The index on the stack. + */ +inline float luax_tofloat(lua_State *L, int idx) +{ + return static_cast(lua_tonumber(L, idx)); +} + +/** + * Like luax_tofloat, but checks that the value is a number. + * + * @see luax_tofloat + */ +inline float luax_checkfloat(lua_State *L, int idx) +{ + return static_cast(luaL_checknumber(L, idx)); +} + +/** + * Converts the value at idx to the specified type without checking that + * this conversion is valid. If the type has been previously verified with + * luax_istype, then this can be safely used. Otherwise, use luax_checktype. + * @param L The Lua state. + * @param idx The index on the stack. + * @param name The name of the type. + * @param type The type bit. + **/ +template +T *luax_totype(lua_State *L, int idx, const char *, love::bits) +{ + return (T *)(((Proxy *)lua_touserdata(L, idx))->data); +} + +/** + * Like luax_totype, but causes an error if the value at idx is not Proxy, + * or is not the specified type. + * @param L The Lua state. + * @param idx The index on the stack. + * @param name The name of the type. + * @param type The type bit. + **/ +template +T *luax_checktype(lua_State *L, int idx, const char *name, love::bits type) +{ + if (lua_isuserdata(L, idx) == 0) + luaL_error(L, "Incorrect parameter type: expected userdata."); + + Proxy *u = (Proxy *)lua_touserdata(L, idx); + + if ((u->flags & type) != type) + luaL_error(L, "Incorrect parameter type: expected %s", name); + + return (T *)u->data; +} + +template +T *luax_getmodule(lua_State *L, const char *k, love::bits type) +{ + luax_getregistry(L, REGISTRY_MODULES); + lua_getfield(L, -1, k); + + if (!lua_isuserdata(L, -1)) + luaL_error(L, "Tried to get nonexisting module %s.", k); + + Proxy *u = (Proxy *)lua_touserdata(L, -1); + + if ((u->flags & type) != type) + luaL_error(L, "Incorrect module %s", k); + + lua_pop(L, 2); + + return (T *)u->data; +} + +} // love + +#endif // LOVE_RUNTIME_H diff --git a/src/common/types.h b/src/common/types.h index 4e915118a..4b0ea2898 100644 --- a/src/common/types.h +++ b/src/common/types.h @@ -1,173 +1,174 @@ -/** -* Copyright (c) 2006-2012 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_TYPES_H -#define LOVE_TYPES_H - -// STD -#include - -namespace love -{ - enum Type - { - INVALID_ID = 0, - // Cross-module types. - OBJECT_ID, - DATA_ID, - MODULE_ID, - - // Filesystem. - FILESYSTEM_FILE_ID, - FILESYSTEM_FILE_DATA_ID, - - // Font - FONT_GLYPH_DATA_ID, - FONT_RASTERIZER_ID, - - // Graphics - GRAPHICS_DRAWABLE_ID, - GRAPHICS_DRAWQABLE_ID, - GRAPHICS_IMAGE_ID, - GRAPHICS_QUAD_ID, - GRAPHICS_FONT_ID, - GRAPHICS_PARTICLE_SYSTEM_ID, - GRAPHICS_SPRITE_BATCH_ID, - GRAPHICS_CANVAS_ID, - GRAPHICS_PIXELEFFECT_ID, - - // Image - IMAGE_IMAGE_DATA_ID, - IMAGE_ENCODED_IMAGE_DATA_ID, - - // Audio - AUDIO_SOURCE_ID, - - // Sound - SOUND_SOUND_DATA_ID, - SOUND_DECODER_ID, - - // Physics - PHYSICS_WORLD_ID, - PHYSICS_CONTACT_ID, - PHYSICS_BODY_ID, - PHYSICS_FIXTURE_ID, - PHYSICS_SHAPE_ID, - PHYSICS_CIRCLE_SHAPE_ID, - PHYSICS_POLYGON_SHAPE_ID, - PHYSICS_EDGE_SHAPE_ID, - PHYSICS_CHAIN_SHAPE_ID, - PHYSICS_JOINT_ID, - PHYSICS_MOUSE_JOINT_ID, - PHYSICS_DISTANCE_JOINT_ID, - PHYSICS_PRISMATIC_JOINT_ID, - PHYSICS_REVOLUTE_JOINT_ID, - PHYSICS_PULLEY_JOINT_ID, - PHYSICS_GEAR_JOINT_ID, - PHYSICS_FRICTION_JOINT_ID, - PHYSICS_WELD_JOINT_ID, - PHYSICS_ROPE_JOINT_ID, - PHYSICS_WHEEL_JOINT_ID, - - // Thread - THREAD_THREAD_ID, - - // The modules themselves. Only add abstracted modules here. - MODULE_FILESYSTEM_ID, - MODULE_IMAGE_ID, - MODULE_SOUND_ID, - - // Count the number of bits needed. - TYPE_MAX_ENUM - }; - - typedef std::bitset bits; - - const bits INVALID_T = bits(1) << INVALID_ID; - - const bits OBJECT_T = bits(1) << OBJECT_ID; - const bits DATA_T = (bits(1) << DATA_ID) | OBJECT_T; - const bits MODULE_T = (bits(1) << MODULE_ID) | OBJECT_T; - - // Filesystem. - const bits FILESYSTEM_FILE_T = (bits(1) << FILESYSTEM_FILE_ID) | OBJECT_T; - const bits FILESYSTEM_FILE_DATA_T = (bits(1) << FILESYSTEM_FILE_DATA_ID) | DATA_T; - - const bits FONT_GLYPH_DATA_T = (bits(1) << FONT_GLYPH_DATA_ID) | DATA_T; - const bits FONT_RASTERIZER_T = (bits(1) << FONT_RASTERIZER_ID) | OBJECT_T; - - // Graphics. - const bits GRAPHICS_DRAWABLE_T = (bits(1) << GRAPHICS_DRAWABLE_ID) | OBJECT_T; - const bits GRAPHICS_DRAWQABLE_T = (bits(1) << GRAPHICS_DRAWQABLE_ID) | GRAPHICS_DRAWABLE_T; - const bits GRAPHICS_IMAGE_T = (bits(1) << GRAPHICS_IMAGE_ID) | GRAPHICS_DRAWQABLE_T; - const bits GRAPHICS_QUAD_T = (bits(1) << GRAPHICS_QUAD_ID) | OBJECT_T; - const bits GRAPHICS_FONT_T = (bits(1) << GRAPHICS_FONT_ID) | OBJECT_T; - const bits GRAPHICS_PARTICLE_SYSTEM_T = (bits(1) << GRAPHICS_PARTICLE_SYSTEM_ID) | GRAPHICS_DRAWABLE_T; - const bits GRAPHICS_SPRITE_BATCH_T = (bits(1) << GRAPHICS_SPRITE_BATCH_ID) | GRAPHICS_DRAWABLE_T; - const bits GRAPHICS_CANVAS_T = (bits(1) << GRAPHICS_CANVAS_ID) | GRAPHICS_DRAWQABLE_T; - const bits GRAPHICS_PIXELEFFECT_T = (bits(1) << GRAPHICS_PIXELEFFECT_ID) | OBJECT_T; - - // Image. - const bits IMAGE_IMAGE_DATA_T = (bits(1) << IMAGE_IMAGE_DATA_ID) | DATA_T; - const bits IMAGE_ENCODED_IMAGE_DATA_T = (bits(1) << IMAGE_ENCODED_IMAGE_DATA_ID) | DATA_T; - - // Audio. - const bits AUDIO_SOURCE_T = (bits(1) << AUDIO_SOURCE_ID) | OBJECT_T; - - // Sound. - const bits SOUND_SOUND_DATA_T = (bits(1) << SOUND_SOUND_DATA_ID) | DATA_T; - const bits SOUND_DECODER_T = bits(1) << SOUND_DECODER_ID; - - // Physics. - const bits PHYSICS_WORLD_T = (bits(1) << PHYSICS_WORLD_ID) | OBJECT_T; - const bits PHYSICS_CONTACT_T = (bits(1) << PHYSICS_CONTACT_ID) | OBJECT_T; - const bits PHYSICS_BODY_T = (bits(1) << PHYSICS_BODY_ID) | OBJECT_T; - const bits PHYSICS_FIXTURE_T = (bits(1) << PHYSICS_FIXTURE_ID) | OBJECT_T; - const bits PHYSICS_SHAPE_T = (bits(1) << PHYSICS_SHAPE_ID) | OBJECT_T; - const bits PHYSICS_CIRCLE_SHAPE_T = (bits(1) << PHYSICS_CIRCLE_SHAPE_ID) | PHYSICS_SHAPE_T; - const bits PHYSICS_POLYGON_SHAPE_T = (bits(1) << PHYSICS_POLYGON_SHAPE_ID) | PHYSICS_SHAPE_T; - const bits PHYSICS_EDGE_SHAPE_T = (bits(1) << PHYSICS_EDGE_SHAPE_ID) | PHYSICS_SHAPE_T; - const bits PHYSICS_CHAIN_SHAPE_T = (bits(1) << PHYSICS_CHAIN_SHAPE_ID) | PHYSICS_SHAPE_T; - const bits PHYSICS_JOINT_T = (bits(1) << PHYSICS_JOINT_ID) | OBJECT_T; - const bits PHYSICS_MOUSE_JOINT_T = (bits(1) << PHYSICS_MOUSE_JOINT_ID) | PHYSICS_JOINT_T; - const bits PHYSICS_DISTANCE_JOINT_T = (bits(1) << PHYSICS_DISTANCE_JOINT_ID) | PHYSICS_JOINT_T; - const bits PHYSICS_PRISMATIC_JOINT_T = (bits(1) << PHYSICS_PRISMATIC_JOINT_ID) | PHYSICS_JOINT_T; - const bits PHYSICS_REVOLUTE_JOINT_T = (bits(1) << PHYSICS_REVOLUTE_JOINT_ID) | PHYSICS_JOINT_T; - const bits PHYSICS_PULLEY_JOINT_T = (bits(1) << PHYSICS_PULLEY_JOINT_ID) | PHYSICS_JOINT_T; - const bits PHYSICS_GEAR_JOINT_T = (bits(1) << PHYSICS_GEAR_JOINT_ID) | PHYSICS_JOINT_T; - const bits PHYSICS_FRICTION_JOINT_T = (bits(1) << PHYSICS_FRICTION_JOINT_ID) | PHYSICS_JOINT_T; - const bits PHYSICS_WELD_JOINT_T = (bits(1) << PHYSICS_WELD_JOINT_ID) | PHYSICS_JOINT_T; - const bits PHYSICS_ROPE_JOINT_T = (bits(1) << PHYSICS_ROPE_JOINT_ID) | PHYSICS_JOINT_T; - const bits PHYSICS_WHEEL_JOINT_T = (bits(1) << PHYSICS_WHEEL_JOINT_ID) | PHYSICS_JOINT_T; - - // Thread. - const bits THREAD_THREAD_T = (bits(1) << THREAD_THREAD_ID) | OBJECT_T; - - // Modules. - const bits MODULE_FILESYSTEM_T = (bits(1) << MODULE_FILESYSTEM_ID) | MODULE_T; - const bits MODULE_IMAGE_T = (bits(1) << MODULE_IMAGE_ID) | MODULE_T; - const bits MODULE_SOUND_T = (bits(1) << MODULE_SOUND_ID) | MODULE_T; - - bool getType(const char * in, Type & out); - bool getType(Type in, const char *& out); - -} // love - -#endif // LOVE_TYPES_H +/** + * Copyright (c) 2006-2012 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_TYPES_H +#define LOVE_TYPES_H + +// STD +#include + +namespace love +{ + +enum Type +{ + INVALID_ID = 0, + // Cross-module types. + OBJECT_ID, + DATA_ID, + MODULE_ID, + + // Filesystem. + FILESYSTEM_FILE_ID, + FILESYSTEM_FILE_DATA_ID, + + // Font + FONT_GLYPH_DATA_ID, + FONT_RASTERIZER_ID, + + // Graphics + GRAPHICS_DRAWABLE_ID, + GRAPHICS_DRAWQABLE_ID, + GRAPHICS_IMAGE_ID, + GRAPHICS_QUAD_ID, + GRAPHICS_FONT_ID, + GRAPHICS_PARTICLE_SYSTEM_ID, + GRAPHICS_SPRITE_BATCH_ID, + GRAPHICS_CANVAS_ID, + GRAPHICS_PIXELEFFECT_ID, + + // Image + IMAGE_IMAGE_DATA_ID, + IMAGE_ENCODED_IMAGE_DATA_ID, + + // Audio + AUDIO_SOURCE_ID, + + // Sound + SOUND_SOUND_DATA_ID, + SOUND_DECODER_ID, + + // Physics + PHYSICS_WORLD_ID, + PHYSICS_CONTACT_ID, + PHYSICS_BODY_ID, + PHYSICS_FIXTURE_ID, + PHYSICS_SHAPE_ID, + PHYSICS_CIRCLE_SHAPE_ID, + PHYSICS_POLYGON_SHAPE_ID, + PHYSICS_EDGE_SHAPE_ID, + PHYSICS_CHAIN_SHAPE_ID, + PHYSICS_JOINT_ID, + PHYSICS_MOUSE_JOINT_ID, + PHYSICS_DISTANCE_JOINT_ID, + PHYSICS_PRISMATIC_JOINT_ID, + PHYSICS_REVOLUTE_JOINT_ID, + PHYSICS_PULLEY_JOINT_ID, + PHYSICS_GEAR_JOINT_ID, + PHYSICS_FRICTION_JOINT_ID, + PHYSICS_WELD_JOINT_ID, + PHYSICS_ROPE_JOINT_ID, + PHYSICS_WHEEL_JOINT_ID, + + // Thread + THREAD_THREAD_ID, + + // The modules themselves. Only add abstracted modules here. + MODULE_FILESYSTEM_ID, + MODULE_IMAGE_ID, + MODULE_SOUND_ID, + + // Count the number of bits needed. + TYPE_MAX_ENUM +}; + +typedef std::bitset bits; + +const bits INVALID_T = bits(1) << INVALID_ID; + +const bits OBJECT_T = bits(1) << OBJECT_ID; +const bits DATA_T = (bits(1) << DATA_ID) | OBJECT_T; +const bits MODULE_T = (bits(1) << MODULE_ID) | OBJECT_T; + +// Filesystem. +const bits FILESYSTEM_FILE_T = (bits(1) << FILESYSTEM_FILE_ID) | OBJECT_T; +const bits FILESYSTEM_FILE_DATA_T = (bits(1) << FILESYSTEM_FILE_DATA_ID) | DATA_T; + +const bits FONT_GLYPH_DATA_T = (bits(1) << FONT_GLYPH_DATA_ID) | DATA_T; +const bits FONT_RASTERIZER_T = (bits(1) << FONT_RASTERIZER_ID) | OBJECT_T; + +// Graphics. +const bits GRAPHICS_DRAWABLE_T = (bits(1) << GRAPHICS_DRAWABLE_ID) | OBJECT_T; +const bits GRAPHICS_DRAWQABLE_T = (bits(1) << GRAPHICS_DRAWQABLE_ID) | GRAPHICS_DRAWABLE_T; +const bits GRAPHICS_IMAGE_T = (bits(1) << GRAPHICS_IMAGE_ID) | GRAPHICS_DRAWQABLE_T; +const bits GRAPHICS_QUAD_T = (bits(1) << GRAPHICS_QUAD_ID) | OBJECT_T; +const bits GRAPHICS_FONT_T = (bits(1) << GRAPHICS_FONT_ID) | OBJECT_T; +const bits GRAPHICS_PARTICLE_SYSTEM_T = (bits(1) << GRAPHICS_PARTICLE_SYSTEM_ID) | GRAPHICS_DRAWABLE_T; +const bits GRAPHICS_SPRITE_BATCH_T = (bits(1) << GRAPHICS_SPRITE_BATCH_ID) | GRAPHICS_DRAWABLE_T; +const bits GRAPHICS_CANVAS_T = (bits(1) << GRAPHICS_CANVAS_ID) | GRAPHICS_DRAWQABLE_T; +const bits GRAPHICS_PIXELEFFECT_T = (bits(1) << GRAPHICS_PIXELEFFECT_ID) | OBJECT_T; + +// Image. +const bits IMAGE_IMAGE_DATA_T = (bits(1) << IMAGE_IMAGE_DATA_ID) | DATA_T; +const bits IMAGE_ENCODED_IMAGE_DATA_T = (bits(1) << IMAGE_ENCODED_IMAGE_DATA_ID) | DATA_T; + +// Audio. +const bits AUDIO_SOURCE_T = (bits(1) << AUDIO_SOURCE_ID) | OBJECT_T; + +// Sound. +const bits SOUND_SOUND_DATA_T = (bits(1) << SOUND_SOUND_DATA_ID) | DATA_T; +const bits SOUND_DECODER_T = bits(1) << SOUND_DECODER_ID; + +// Physics. +const bits PHYSICS_WORLD_T = (bits(1) << PHYSICS_WORLD_ID) | OBJECT_T; +const bits PHYSICS_CONTACT_T = (bits(1) << PHYSICS_CONTACT_ID) | OBJECT_T; +const bits PHYSICS_BODY_T = (bits(1) << PHYSICS_BODY_ID) | OBJECT_T; +const bits PHYSICS_FIXTURE_T = (bits(1) << PHYSICS_FIXTURE_ID) | OBJECT_T; +const bits PHYSICS_SHAPE_T = (bits(1) << PHYSICS_SHAPE_ID) | OBJECT_T; +const bits PHYSICS_CIRCLE_SHAPE_T = (bits(1) << PHYSICS_CIRCLE_SHAPE_ID) | PHYSICS_SHAPE_T; +const bits PHYSICS_POLYGON_SHAPE_T = (bits(1) << PHYSICS_POLYGON_SHAPE_ID) | PHYSICS_SHAPE_T; +const bits PHYSICS_EDGE_SHAPE_T = (bits(1) << PHYSICS_EDGE_SHAPE_ID) | PHYSICS_SHAPE_T; +const bits PHYSICS_CHAIN_SHAPE_T = (bits(1) << PHYSICS_CHAIN_SHAPE_ID) | PHYSICS_SHAPE_T; +const bits PHYSICS_JOINT_T = (bits(1) << PHYSICS_JOINT_ID) | OBJECT_T; +const bits PHYSICS_MOUSE_JOINT_T = (bits(1) << PHYSICS_MOUSE_JOINT_ID) | PHYSICS_JOINT_T; +const bits PHYSICS_DISTANCE_JOINT_T = (bits(1) << PHYSICS_DISTANCE_JOINT_ID) | PHYSICS_JOINT_T; +const bits PHYSICS_PRISMATIC_JOINT_T = (bits(1) << PHYSICS_PRISMATIC_JOINT_ID) | PHYSICS_JOINT_T; +const bits PHYSICS_REVOLUTE_JOINT_T = (bits(1) << PHYSICS_REVOLUTE_JOINT_ID) | PHYSICS_JOINT_T; +const bits PHYSICS_PULLEY_JOINT_T = (bits(1) << PHYSICS_PULLEY_JOINT_ID) | PHYSICS_JOINT_T; +const bits PHYSICS_GEAR_JOINT_T = (bits(1) << PHYSICS_GEAR_JOINT_ID) | PHYSICS_JOINT_T; +const bits PHYSICS_FRICTION_JOINT_T = (bits(1) << PHYSICS_FRICTION_JOINT_ID) | PHYSICS_JOINT_T; +const bits PHYSICS_WELD_JOINT_T = (bits(1) << PHYSICS_WELD_JOINT_ID) | PHYSICS_JOINT_T; +const bits PHYSICS_ROPE_JOINT_T = (bits(1) << PHYSICS_ROPE_JOINT_ID) | PHYSICS_JOINT_T; +const bits PHYSICS_WHEEL_JOINT_T = (bits(1) << PHYSICS_WHEEL_JOINT_ID) | PHYSICS_JOINT_T; + +// Thread. +const bits THREAD_THREAD_T = (bits(1) << THREAD_THREAD_ID) | OBJECT_T; + +// Modules. +const bits MODULE_FILESYSTEM_T = (bits(1) << MODULE_FILESYSTEM_ID) | MODULE_T; +const bits MODULE_IMAGE_T = (bits(1) << MODULE_IMAGE_ID) | MODULE_T; +const bits MODULE_SOUND_T = (bits(1) << MODULE_SOUND_ID) | MODULE_T; + +bool getType(const char *in, Type &out); +bool getType(Type in, const char *&out); + +} // love + +#endif // LOVE_TYPES_H diff --git a/src/common/utf8.cpp b/src/common/utf8.cpp index 00e3b1e1b..96001714d 100644 --- a/src/common/utf8.cpp +++ b/src/common/utf8.cpp @@ -1,60 +1,61 @@ -/** -* Copyright (c) 2006-2012 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. -**/ - -#include "utf8.h" - -#ifdef LOVE_WINDOWS - -namespace love -{ - std::string to_utf8(LPCWSTR wstr) - { - size_t wide_len = wcslen(wstr)+1; - - // Get size in UTF-8. - int utf8_size = WideCharToMultiByte(CP_UTF8, 0, wstr, wide_len, 0, 0, 0, 0); - - char * utf8_str = new char[utf8_size]; - - // Convert to UTF-8. - int ok = WideCharToMultiByte(CP_UTF8, 0, wstr, wide_len, utf8_str, utf8_size, 0, 0); - - std::string ret; - if (ok) - ret = utf8_str; - - delete utf8_str; - return ret; - } - - void replace_char(std::string & str, char find, char replace) - { - int length = str.length(); - - for (int i = 0; i -#include - -namespace love -{ - /** - * Convert the wide string to a UTF-8 encoded string. - * @param wstr The wide-char string. - * @return A UTF-8 string. - **/ - std::string to_utf8(LPCWSTR wstr); - - /** - * Replace all occurences of 'find' with 'replace' in a string. - * @param str The string to modify. - * @param find The character to match. - * @param replace The character to replace matches. - **/ - void replace_char(std::string & str, char find, char replace); - -} // love - +/** + * Copyright (c) 2006-2012 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. + **/ + +#include "config.h" + +#ifdef LOVE_WINDOWS + +#include +#include + +namespace love +{ + +/** + * Convert the wide string to a UTF-8 encoded string. + * @param wstr The wide-char string. + * @return A UTF-8 string. + **/ +std::string to_utf8(LPCWSTR wstr); + +/** + * Replace all occurences of 'find' with 'replace' in a string. + * @param str The string to modify. + * @param find The character to match. + * @param replace The character to replace matches. + **/ +void replace_char(std::string &str, char find, char replace); + +} // love + #endif // LOVE_WINDOWS \ No newline at end of file diff --git a/src/common/version.h b/src/common/version.h index 0ec190336..88ab7c60e 100644 --- a/src/common/version.h +++ b/src/common/version.h @@ -1,36 +1,37 @@ -/** -* Copyright (c) 2006-2012 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_VERSION_H -#define LOVE_VERSION_H - -namespace love -{ - // Version stuff. - const int VERSION_MAJOR = 0; - const int VERSION_MINOR = 8; - const int VERSION_REV = 0; - const char * VERSION = "0.8.0"; - const char * VERSION_COMPATIBILITY[] = { VERSION, 0 }; - const char * VERSION_CODENAME = "Rubber Piggy"; - -} // love - -#endif // LOVE_VERSION_H +/** + * Copyright (c) 2006-2012 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_VERSION_H +#define LOVE_VERSION_H + +namespace love +{ + +// Version stuff. +const int VERSION_MAJOR = 0; +const int VERSION_MINOR = 8; +const int VERSION_REV = 0; +const char *VERSION = "0.8.0"; +const char *VERSION_COMPATIBILITY[] = { VERSION, 0 }; +const char *VERSION_CODENAME = "Rubber Piggy"; + +} // love + +#endif // LOVE_VERSION_H diff --git a/src/common/wrap_Data.cpp b/src/common/wrap_Data.cpp index 4d8c7075d..59e40d93c 100644 --- a/src/common/wrap_Data.cpp +++ b/src/common/wrap_Data.cpp @@ -1,56 +1,58 @@ -/** -* Copyright (c) 2006-2012 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. -**/ - -#include "wrap_Data.h" - -namespace love -{ - Data * luax_checkdata(lua_State * L, int idx) - { - return luax_checktype(L, idx, "Data", DATA_T); - } - - int w_Data_getPointer(lua_State * L) - { - Data * t = luax_checkdata(L, 1); - lua_pushlightuserdata(L, t->getData()); - return 1; - } - - int w_Data_getSize(lua_State * L) - { - Data * t = luax_checkdata(L, 1); - lua_pushinteger(L, t->getSize()); - return 1; - } - - const luaL_Reg w_Data_functions[] = { - { "getPointer", w_Data_getPointer }, - { "getSize", w_Data_getSize }, - { 0, 0 } - }; - - int w_Data_open(lua_State * L) - { - luax_register_type(L, "Data", w_Data_functions); - return 0; - } - -} // love +/** + * Copyright (c) 2006-2012 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. + **/ + +#include "wrap_Data.h" + +namespace love +{ + +Data *luax_checkdata(lua_State *L, int idx) +{ + return luax_checktype(L, idx, "Data", DATA_T); +} + +int w_Data_getPointer(lua_State *L) +{ + Data *t = luax_checkdata(L, 1); + lua_pushlightuserdata(L, t->getData()); + return 1; +} + +int w_Data_getSize(lua_State *L) +{ + Data *t = luax_checkdata(L, 1); + lua_pushinteger(L, t->getSize()); + return 1; +} + +const luaL_Reg w_Data_functions[] = +{ + { "getPointer", w_Data_getPointer }, + { "getSize", w_Data_getSize }, + { 0, 0 } +}; + +int w_Data_open(lua_State *L) +{ + luax_register_type(L, "Data", w_Data_functions); + return 0; +} + +} // love diff --git a/src/common/wrap_Data.h b/src/common/wrap_Data.h index 3ab1c34e7..2d9c7f018 100644 --- a/src/common/wrap_Data.h +++ b/src/common/wrap_Data.h @@ -1,37 +1,38 @@ -/** -* Copyright (c) 2006-2012 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_WRAP_DATA_H -#define LOVE_WRAP_DATA_H - -// LOVE -#include "runtime.h" -#include "Data.h" - -namespace love -{ - Data * luax_checkdata(lua_State * L, int idx); - int w_Data_getPointer(lua_State * L); - int w_Data_getSize(lua_State * L); - int w_Data_open(lua_State * L); - -} // love - -#endif // LOVE_WRAP_DATA_H +/** + * Copyright (c) 2006-2012 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_WRAP_DATA_H +#define LOVE_WRAP_DATA_H + +// LOVE +#include "runtime.h" +#include "Data.h" + +namespace love +{ + +Data *luax_checkdata(lua_State *L, int idx); +int w_Data_getPointer(lua_State *L); +int w_Data_getSize(lua_State *L); +int w_Data_open(lua_State *L); + +} // love + +#endif // LOVE_WRAP_DATA_H diff --git a/src/launcher.cpp b/src/launcher.cpp index fd89dbf8f..ab46367b7 100644 --- a/src/launcher.cpp +++ b/src/launcher.cpp @@ -1,34 +1,34 @@ -/** -* Copyright (c) 2006-2012 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. -**/ - -#include -#include - -extern "C" -{ - extern int lovemain(int argc, char **argv); -} - -#ifdef LOVE_BUILD_EXE -int main(int argc, char **argv) -{ - return lovemain(argc, argv); -} -#endif // LOVE_BUILD_EXE +/** + * Copyright (c) 2006-2012 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. + **/ + +#include "common/config.h" +#include + +extern "C" +{ + extern int lovemain(int argc, char **argv); +} + +#ifdef LOVE_BUILD_EXE +int main(int argc, char **argv) +{ + return lovemain(argc, argv); +} +#endif // LOVE_BUILD_EXE diff --git a/src/love.cpp b/src/love.cpp index 73d2d3f0a..008a1b652 100644 --- a/src/love.cpp +++ b/src/love.cpp @@ -1,363 +1,363 @@ -/** -* Copyright (c) 2006-2012 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. -**/ - -// LOVE -#include -#include -#include - -#ifdef LOVE_WINDOWS -#include -#endif // LOVE_WINDOWS - -#ifdef LOVE_LEGENDARY_CONSOLE_IO_HACK -#include -#include -#include -#include -#endif // LOVE_LEGENDARY_CONSOLE_IO_HACK - -#ifdef LOVE_BUILD_EXE - -// SDL -#include - -// Libraries. -#include "libraries/luasocket/luasocket.h" - -// Scripts -#include "scripts/boot.lua.h" - -#endif // LOVE_BUILD_EXE - -#ifdef LOVE_BUILD_STANDALONE - -// All modules define a c-accessible luaopen -// so let's make use of those, instead -// of addressing implementations directly. -extern "C" -{ - extern int luaopen_love_audio(lua_State*); - extern int luaopen_love_event(lua_State*); - extern int luaopen_love_filesystem(lua_State*); - extern int luaopen_love_font(lua_State*); - extern int luaopen_love_graphics(lua_State*); - extern int luaopen_love_image(lua_State*); - extern int luaopen_love_joystick(lua_State*); - extern int luaopen_love_keyboard(lua_State*); - extern int luaopen_love_mouse(lua_State*); - extern int luaopen_love_physics(lua_State*); - extern int luaopen_love_sound(lua_State*); - extern int luaopen_love_timer(lua_State*); - extern int luaopen_love_thread(lua_State*); - extern int luaopen_love_boot(lua_State*); -} - -static const luaL_Reg modules[] = { - { "love.audio", luaopen_love_audio }, - { "love.event", luaopen_love_event }, - { "love.filesystem", luaopen_love_filesystem }, - { "love.font", luaopen_love_font }, - { "love.graphics", luaopen_love_graphics }, - { "love.image", luaopen_love_image }, - { "love.joystick", luaopen_love_joystick }, - { "love.keyboard", luaopen_love_keyboard }, - { "love.mouse", luaopen_love_mouse }, - { "love.physics", luaopen_love_physics }, - { "love.sound", luaopen_love_sound }, - { "love.timer", luaopen_love_timer }, - { "love.thread", luaopen_love_thread }, - { "love.boot", luaopen_love_boot }, - { 0, 0 } -}; - -#endif // LOVE_BUILD_STANDALONE - -#ifdef LOVE_LEGENDARY_CONSOLE_IO_HACK -int w__openConsole(lua_State * L); -#endif // LOVE_LEGENDARY_CONSOLE_IO_HACK - -extern "C" LOVE_EXPORT int luaopen_love(lua_State * L) -{ - love::luax_insistglobal(L, "love"); - - // Set version information. - lua_pushstring(L, love::VERSION); - lua_setfield(L, -2, "_version"); - - lua_pushnumber(L, love::VERSION_MAJOR); - lua_setfield(L, -2, "_version_major"); - lua_pushnumber(L, love::VERSION_MINOR); - lua_setfield(L, -2, "_version_minor"); - lua_pushnumber(L, love::VERSION_REV); - lua_setfield(L, -2, "_version_revision"); - - lua_pushstring(L, love::VERSION_CODENAME); - lua_setfield(L, -2, "_version_codename"); - -#ifdef LOVE_LEGENDARY_CONSOLE_IO_HACK - lua_pushcfunction(L, w__openConsole); - lua_setfield(L, -2, "_openConsole"); -#endif // LOVE_LEGENDARY_CONSOLE_IO_HACK - - lua_newtable(L); - - for (int i = 0; love::VERSION_COMPATIBILITY[i] != 0; ++i) - { - lua_pushstring(L, love::VERSION_COMPATIBILITY[i]); - lua_rawseti(L, -2, i+1); - } - - lua_setfield(L, -2, "_version_compat"); - -#ifdef LOVE_WINDOWS - lua_pushstring(L, "Windows"); -#elif defined(LOVE_MACOSX) - lua_pushstring(L, "OS X"); -#elif defined(LOVE_LINUX) - lua_pushstring(L, "Linux"); -#else - lua_pushstring(L, "Unknown"); -#endif - lua_setfield(L, -2, "_os"); - -#ifdef LOVE_BUILD_STANDALONE - - // Preload module loaders. - for (int i = 0; modules[i].name != 0; i++) - { - love::luax_preload(L, modules[i].func, modules[i].name); - } - - love::luasocket::__open(L); - -#endif // LOVE_BUILD_STANDALONE - - return 1; -} - -#ifdef LOVE_LEGENDARY_UTF8_ARGV_HACK - -void get_utf8_arguments(int & argc, char **& argv) -{ - LPWSTR cmd = GetCommandLineW(); - - if (!cmd) - return; - - LPWSTR * argv_w = CommandLineToArgvW(cmd, &argc); - - argv = new char*[argc]; - - for (int i = 0; i 1 && strcmp(argv[1],"--version") == 0) - { - printf("LOVE %s (%s)\n", love::VERSION, love::VERSION_CODENAME); - return 0; - } - - // Create the virtual machine. - lua_State * L = lua_open(); - luaL_openlibs(L); - - love::luax_preload(L, luaopen_love, "love"); - - luaopen_love(L); - - // Add command line arguments to global arg (like stand-alone Lua). - { - lua_newtable(L); - - if (argc > 0) - { - lua_pushstring(L, argv[0]); - lua_rawseti(L, -2, -2); - } - - lua_pushstring(L, "embedded boot.lua"); - lua_rawseti(L, -2, -1); - - for (int i = 1; i +#endif // LOVE_WINDOWS + +#ifdef LOVE_LEGENDARY_CONSOLE_IO_HACK +#include +#include +#include +#include +#endif // LOVE_LEGENDARY_CONSOLE_IO_HACK + +#ifdef LOVE_BUILD_EXE + +// SDL +#include + +// Libraries. +#include "libraries/luasocket/luasocket.h" + +// Scripts +#include "scripts/boot.lua.h" + +#endif // LOVE_BUILD_EXE + +#ifdef LOVE_BUILD_STANDALONE + +// All modules define a c-accessible luaopen +// so let's make use of those, instead +// of addressing implementations directly. +extern "C" +{ + extern int luaopen_love_audio(lua_State*); + extern int luaopen_love_event(lua_State*); + extern int luaopen_love_filesystem(lua_State*); + extern int luaopen_love_font(lua_State*); + extern int luaopen_love_graphics(lua_State*); + extern int luaopen_love_image(lua_State*); + extern int luaopen_love_joystick(lua_State*); + extern int luaopen_love_keyboard(lua_State*); + extern int luaopen_love_mouse(lua_State*); + extern int luaopen_love_physics(lua_State*); + extern int luaopen_love_sound(lua_State*); + extern int luaopen_love_timer(lua_State*); + extern int luaopen_love_thread(lua_State*); + extern int luaopen_love_boot(lua_State*); +} + +static const luaL_Reg modules[] = { + { "love.audio", luaopen_love_audio }, + { "love.event", luaopen_love_event }, + { "love.filesystem", luaopen_love_filesystem }, + { "love.font", luaopen_love_font }, + { "love.graphics", luaopen_love_graphics }, + { "love.image", luaopen_love_image }, + { "love.joystick", luaopen_love_joystick }, + { "love.keyboard", luaopen_love_keyboard }, + { "love.mouse", luaopen_love_mouse }, + { "love.physics", luaopen_love_physics }, + { "love.sound", luaopen_love_sound }, + { "love.timer", luaopen_love_timer }, + { "love.thread", luaopen_love_thread }, + { "love.boot", luaopen_love_boot }, + { 0, 0 } +}; + +#endif // LOVE_BUILD_STANDALONE + +#ifdef LOVE_LEGENDARY_CONSOLE_IO_HACK +int w__openConsole(lua_State * L); +#endif // LOVE_LEGENDARY_CONSOLE_IO_HACK + +extern "C" LOVE_EXPORT int luaopen_love(lua_State * L) +{ + love::luax_insistglobal(L, "love"); + + // Set version information. + lua_pushstring(L, love::VERSION); + lua_setfield(L, -2, "_version"); + + lua_pushnumber(L, love::VERSION_MAJOR); + lua_setfield(L, -2, "_version_major"); + lua_pushnumber(L, love::VERSION_MINOR); + lua_setfield(L, -2, "_version_minor"); + lua_pushnumber(L, love::VERSION_REV); + lua_setfield(L, -2, "_version_revision"); + + lua_pushstring(L, love::VERSION_CODENAME); + lua_setfield(L, -2, "_version_codename"); + +#ifdef LOVE_LEGENDARY_CONSOLE_IO_HACK + lua_pushcfunction(L, w__openConsole); + lua_setfield(L, -2, "_openConsole"); +#endif // LOVE_LEGENDARY_CONSOLE_IO_HACK + + lua_newtable(L); + + for (int i = 0; love::VERSION_COMPATIBILITY[i] != 0; ++i) + { + lua_pushstring(L, love::VERSION_COMPATIBILITY[i]); + lua_rawseti(L, -2, i+1); + } + + lua_setfield(L, -2, "_version_compat"); + +#ifdef LOVE_WINDOWS + lua_pushstring(L, "Windows"); +#elif defined(LOVE_MACOSX) + lua_pushstring(L, "OS X"); +#elif defined(LOVE_LINUX) + lua_pushstring(L, "Linux"); +#else + lua_pushstring(L, "Unknown"); +#endif + lua_setfield(L, -2, "_os"); + +#ifdef LOVE_BUILD_STANDALONE + + // Preload module loaders. + for (int i = 0; modules[i].name != 0; i++) + { + love::luax_preload(L, modules[i].func, modules[i].name); + } + + love::luasocket::__open(L); + +#endif // LOVE_BUILD_STANDALONE + + return 1; +} + +#ifdef LOVE_LEGENDARY_UTF8_ARGV_HACK + +void get_utf8_arguments(int & argc, char **& argv) +{ + LPWSTR cmd = GetCommandLineW(); + + if (!cmd) + return; + + LPWSTR * argv_w = CommandLineToArgvW(cmd, &argc); + + argv = new char*[argc]; + + for (int i = 0; i 1 && strcmp(argv[1],"--version") == 0) + { + printf("LOVE %s (%s)\n", love::VERSION, love::VERSION_CODENAME); + return 0; + } + + // Create the virtual machine. + lua_State * L = lua_open(); + luaL_openlibs(L); + + love::luax_preload(L, luaopen_love, "love"); + + luaopen_love(L); + + // Add command line arguments to global arg (like stand-alone Lua). + { + lua_newtable(L); + + if (argc > 0) + { + lua_pushstring(L, argv[0]); + lua_rawseti(L, -2, -2); + } + + lua_pushstring(L, "embedded boot.lua"); + lua_rawseti(L, -2, -1); + + for (int i = 1; i::Entry Audio::distanceModelEntries[] = - { - {"none", Audio::DISTANCE_NONE}, - {"inverse", Audio::DISTANCE_INVERSE}, - {"inverse clamped", Audio::DISTANCE_INVERSE_CLAMPED}, - {"linear", Audio::DISTANCE_LINEAR}, - {"linear clamped", Audio::DISTANCE_LINEAR_CLAMPED}, - {"exponent", Audio::DISTANCE_EXPONENT}, - {"exponent clamped", Audio::DISTANCE_EXPONENT_CLAMPED} - }; - - StringMap Audio::distanceModels(Audio::distanceModelEntries, sizeof(Audio::distanceModelEntries)); - - bool Audio::getConstant(const char * in, DistanceModel & out) - { - return distanceModels.find(in, out); - } - - bool Audio::getConstant(DistanceModel in, const char *& out) - { - return distanceModels.find(in, out); - } - -} // audio -} // love +/** + * Copyright (c) 2006-2012 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 Audio 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 Audio distribution. + **/ + +#include "Audio.h" + +namespace love +{ +namespace audio +{ + +StringMap::Entry Audio::distanceModelEntries[] = +{ + {"none", Audio::DISTANCE_NONE}, + {"inverse", Audio::DISTANCE_INVERSE}, + {"inverse clamped", Audio::DISTANCE_INVERSE_CLAMPED}, + {"linear", Audio::DISTANCE_LINEAR}, + {"linear clamped", Audio::DISTANCE_LINEAR_CLAMPED}, + {"exponent", Audio::DISTANCE_EXPONENT}, + {"exponent clamped", Audio::DISTANCE_EXPONENT_CLAMPED} +}; + +StringMap Audio::distanceModels(Audio::distanceModelEntries, sizeof(Audio::distanceModelEntries)); + +bool Audio::getConstant(const char *in, DistanceModel &out) +{ + return distanceModels.find(in, out); +} + +bool Audio::getConstant(DistanceModel in, const char *&out) +{ + return distanceModels.find(in, out); +} + +} // audio +} // love diff --git a/src/modules/audio/Audio.h b/src/modules/audio/Audio.h index d27d240c3..1e5dfce91 100644 --- a/src/modules/audio/Audio.h +++ b/src/modules/audio/Audio.h @@ -1,234 +1,239 @@ -/** -* Copyright (c) 2006-2012 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 = 0; 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_AUDIO_AUDIO_H -#define LOVE_AUDIO_AUDIO_H - -#include -#include -#include "Source.h" - -namespace love -{ -namespace sound -{ - class Decoder; - class SoundData; -} -namespace audio -{ - /** - * The Audio module is responsible for playing back raw sound samples. - **/ - class Audio : public Module - { - public: - - /** - * Attenuation by distance. - */ - enum DistanceModel - { - DISTANCE_NONE = 1, - DISTANCE_INVERSE, - DISTANCE_INVERSE_CLAMPED, - DISTANCE_LINEAR, - DISTANCE_LINEAR_CLAMPED, - DISTANCE_EXPONENT, - DISTANCE_EXPONENT_CLAMPED, - DISTANCE_MAX_ENUM - }; - - static bool getConstant(const char * in, DistanceModel & out); - static bool getConstant(DistanceModel in, const char *& out); - - /** - * Destructor. - **/ - virtual ~Audio(){}; - - virtual Source * newSource(love::sound::Decoder * decoder) = 0; - virtual Source * newSource(love::sound::SoundData * soundData) = 0; - - /** - * Gets the current number of simultaneous playing sources. - * @return The current number of simultaneous playing sources. - **/ - virtual int getNumSources() const = 0; - - /** - * Gets the maximum supported number of simultaneous playing sources. - * @return The maximum supported number of simultaneous playing sources. - **/ - virtual int getMaxSources() const = 0; - - /** - * Play the specified Source. - * @param source The Source to play. - **/ - virtual void play(Source * source) = 0; - - /** - * Stops playback on the specified source. - * @param source The source on which to stop the playback. - **/ - virtual void stop(Source * source) = 0; - - /** - * Stops all playing audio. - **/ - virtual void stop() = 0; - - /** - * Pauses playback on the specified source. - * @param source The source on which to pause the playback. - **/ - virtual void pause(Source * source) = 0; - - /** - * Pauses all audio. - **/ - virtual void pause() = 0; - - /** - * Resumes playback on the specified source. - * @param source The source on which to resume the playback. - **/ - virtual void resume(Source * source) = 0; - - /** - * Resumes all audio. - **/ - virtual void resume() = 0; - - /** - * Rewinds the specified source. Whatever is playing on this - * source gets rewound to the start. - * @param source The source to rewind. - **/ - virtual void rewind(Source * source) = 0; - - /** - * Rewinds all playing audio. - **/ - virtual void rewind() = 0; - - /** - * Sets the master volume, where 0.0f is min (off) and 1.0f is max. - * @param volume The new master volume. - **/ - virtual void setVolume(float volume) = 0; - - /** - * Gets the master volume. - * @return The current master volume. - **/ - virtual float getVolume() const = 0; - - /** - * Gets the position of the listener. - * @param v A float array of size 3 containing (x,y,z) in that order. - **/ - virtual void getPosition(float * v) const = 0; - - /** - * Sets the position of the listener. - * @param v A float array of size 3 containing [x,y,z] in that order. - **/ - virtual void setPosition(float * v) = 0; - - /** - * Gets the orientation of the listener. - * @param v A float array of size 6 containing [x,y,z] for the forward - * vector, followed by [x,y,z] for the up vector. - **/ - virtual void getOrientation(float * v) const = 0; - - /** - * Sets the orientation of the listener. - * @param v A float array of size 6 containing [x,y,z] for the forward - * vector, followed by [x,y,z] for the up vector. - **/ - virtual void setOrientation(float * v) = 0; - - /** - * Gets the velocity of the listener. - * @param v A float array of size 3 containing [x,y,z] in that order. - **/ - virtual void getVelocity(float * v) const = 0; - - /** - * Sets the velocity of the listener. - * @param v A float array of size 3 containing [x,y,z] in that order. - **/ - virtual void setVelocity(float * v) = 0; - - /** - * Begins recording audio input from the microphone. - **/ - virtual void record() = 0; - - /** - * Gets a section of recorded audio. - * Per OpenAL, the measurement begins from the start of the - * audio data in memory, which is after the last time this function - * was called. If this function has not been called yet this recording - * session, it just grabs from the beginning. - * @return All the recorded SoundData thus far. - **/ - virtual love::sound::SoundData * getRecordedData() = 0; - - /** - * Stops recording and, if passed true, returns all the recorded audio - * not already gotten by getRecordedData. - * @param returnData Whether to return recorded audio. - * @return if returnData, all the recorded audio yet to be gotten, - * otherwise NULL. - **/ - virtual love::sound::SoundData * stopRecording(bool returnData) = 0; - - /** - * Checks whether LOVE is able to record audio input. - * @return hasMic Whether LOVE has a microphone enabled. - **/ - virtual bool canRecord() = 0; - - /** - * Gets the distance model used for attenuation. - * @return Distance model. - */ - virtual DistanceModel getDistanceModel() const = 0; - - /** - * Sets the distance model used for attenuation. - * @param distanceModel Distance model. - */ - virtual void setDistanceModel(DistanceModel distanceModel) = 0; - - private: - - static StringMap::Entry distanceModelEntries[]; - static StringMap distanceModels; - }; // Audio - -} // audio -} // love - -#endif // LOVE_AUDIO_AUDIO_H +/** + * Copyright (c) 2006-2012 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 = 0; 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_AUDIO_AUDIO_H +#define LOVE_AUDIO_AUDIO_H + +#include "common/Module.h" +#include "common/StringMap.h" +#include "Source.h" + +namespace love +{ + +namespace sound +{ + +class Decoder; +class SoundData; + +} // sound + +namespace audio +{ + +/** + * The Audio module is responsible for playing back raw sound samples. + **/ +class Audio : public Module +{ +public: + + /** + * Attenuation by distance. + */ + enum DistanceModel + { + DISTANCE_NONE = 1, + DISTANCE_INVERSE, + DISTANCE_INVERSE_CLAMPED, + DISTANCE_LINEAR, + DISTANCE_LINEAR_CLAMPED, + DISTANCE_EXPONENT, + DISTANCE_EXPONENT_CLAMPED, + DISTANCE_MAX_ENUM + }; + + static bool getConstant(const char *in, DistanceModel &out); + static bool getConstant(DistanceModel in, const char *&out); + + /** + * Destructor. + **/ + virtual ~Audio() {}; + + virtual Source *newSource(love::sound::Decoder *decoder) = 0; + virtual Source *newSource(love::sound::SoundData *soundData) = 0; + + /** + * Gets the current number of simultaneous playing sources. + * @return The current number of simultaneous playing sources. + **/ + virtual int getNumSources() const = 0; + + /** + * Gets the maximum supported number of simultaneous playing sources. + * @return The maximum supported number of simultaneous playing sources. + **/ + virtual int getMaxSources() const = 0; + + /** + * Play the specified Source. + * @param source The Source to play. + **/ + virtual void play(Source *source) = 0; + + /** + * Stops playback on the specified source. + * @param source The source on which to stop the playback. + **/ + virtual void stop(Source *source) = 0; + + /** + * Stops all playing audio. + **/ + virtual void stop() = 0; + + /** + * Pauses playback on the specified source. + * @param source The source on which to pause the playback. + **/ + virtual void pause(Source *source) = 0; + + /** + * Pauses all audio. + **/ + virtual void pause() = 0; + + /** + * Resumes playback on the specified source. + * @param source The source on which to resume the playback. + **/ + virtual void resume(Source *source) = 0; + + /** + * Resumes all audio. + **/ + virtual void resume() = 0; + + /** + * Rewinds the specified source. Whatever is playing on this + * source gets rewound to the start. + * @param source The source to rewind. + **/ + virtual void rewind(Source *source) = 0; + + /** + * Rewinds all playing audio. + **/ + virtual void rewind() = 0; + + /** + * Sets the master volume, where 0.0f is min (off) and 1.0f is max. + * @param volume The new master volume. + **/ + virtual void setVolume(float volume) = 0; + + /** + * Gets the master volume. + * @return The current master volume. + **/ + virtual float getVolume() const = 0; + + /** + * Gets the position of the listener. + * @param v A float array of size 3 containing (x,y,z) in that order. + **/ + virtual void getPosition(float *v) const = 0; + + /** + * Sets the position of the listener. + * @param v A float array of size 3 containing [x,y,z] in that order. + **/ + virtual void setPosition(float *v) = 0; + + /** + * Gets the orientation of the listener. + * @param v A float array of size 6 containing [x,y,z] for the forward + * vector, followed by [x,y,z] for the up vector. + **/ + virtual void getOrientation(float *v) const = 0; + + /** + * Sets the orientation of the listener. + * @param v A float array of size 6 containing [x,y,z] for the forward + * vector, followed by [x,y,z] for the up vector. + **/ + virtual void setOrientation(float *v) = 0; + + /** + * Gets the velocity of the listener. + * @param v A float array of size 3 containing [x,y,z] in that order. + **/ + virtual void getVelocity(float *v) const = 0; + + /** + * Sets the velocity of the listener. + * @param v A float array of size 3 containing [x,y,z] in that order. + **/ + virtual void setVelocity(float *v) = 0; + + /** + * Begins recording audio input from the microphone. + **/ + virtual void record() = 0; + + /** + * Gets a section of recorded audio. + * Per OpenAL, the measurement begins from the start of the + * audio data in memory, which is after the last time this function + * was called. If this function has not been called yet this recording + * session, it just grabs from the beginning. + * @return All the recorded SoundData thus far. + **/ + virtual love::sound::SoundData *getRecordedData() = 0; + + /** + * Stops recording and, if passed true, returns all the recorded audio + * not already gotten by getRecordedData. + * @param returnData Whether to return recorded audio. + * @return if returnData, all the recorded audio yet to be gotten, + * otherwise NULL. + **/ + virtual love::sound::SoundData *stopRecording(bool returnData) = 0; + + /** + * Checks whether LOVE is able to record audio input. + * @return hasMic Whether LOVE has a microphone enabled. + **/ + virtual bool canRecord() = 0; + + /** + * Gets the distance model used for attenuation. + * @return Distance model. + */ + virtual DistanceModel getDistanceModel() const = 0; + + /** + * Sets the distance model used for attenuation. + * @param distanceModel Distance model. + */ + virtual void setDistanceModel(DistanceModel distanceModel) = 0; + +private: + + static StringMap::Entry distanceModelEntries[]; + static StringMap distanceModels; +}; // Audio + +} // audio +} // love + +#endif // LOVE_AUDIO_AUDIO_H diff --git a/src/modules/audio/Source.cpp b/src/modules/audio/Source.cpp index cbce73534..ba325808e 100644 --- a/src/modules/audio/Source.cpp +++ b/src/modules/audio/Source.cpp @@ -1,73 +1,74 @@ -/** -* Copyright (c) 2006-2012 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. -**/ - -#include "Source.h" - -namespace love -{ -namespace audio -{ - Source::Source(Type type) - : type(type) - { - } - - Source::~Source() - { - } - - bool Source::getConstant(const char * in, Type & out) - { - return types.find(in, out); - } - - bool Source::getConstant(Type in, const char *& out) - { - return types.find(in, out); - } - - bool Source::getConstant(const char * in, Unit & out) - { - return units.find(in, out); - } - - bool Source::getConstant(Unit in, const char *& out) - { - return units.find(in, out); - } - - StringMap::Entry Source::typeEntries[] = - { - {"static", Source::TYPE_STATIC}, - {"stream", Source::TYPE_STREAM}, - }; - - StringMap Source::types(Source::typeEntries, sizeof(Source::typeEntries)); - - StringMap::Entry Source::unitEntries[] = - { - {"seconds", Source::UNIT_SECONDS}, - {"samples", Source::UNIT_SAMPLES}, - }; - - StringMap Source::units(Source::unitEntries, sizeof(Source::unitEntries)); - -} // audio -} // love +/** + * Copyright (c) 2006-2012 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. + **/ + +#include "Source.h" + +namespace love +{ +namespace audio +{ + +Source::Source(Type type) + : type(type) +{ +} + +Source::~Source() +{ +} + +bool Source::getConstant(const char *in, Type &out) +{ + return types.find(in, out); +} + +bool Source::getConstant(Type in, const char *&out) +{ + return types.find(in, out); +} + +bool Source::getConstant(const char *in, Unit &out) +{ + return units.find(in, out); +} + +bool Source::getConstant(Unit in, const char *&out) +{ + return units.find(in, out); +} + +StringMap::Entry Source::typeEntries[] = +{ + {"static", Source::TYPE_STATIC}, + {"stream", Source::TYPE_STREAM}, +}; + +StringMap Source::types(Source::typeEntries, sizeof(Source::typeEntries)); + +StringMap::Entry Source::unitEntries[] = +{ + {"seconds", Source::UNIT_SECONDS}, + {"samples", Source::UNIT_SAMPLES}, +}; + +StringMap Source::units(Source::unitEntries, sizeof(Source::unitEntries)); + +} // audio +} // love diff --git a/src/modules/audio/Source.h b/src/modules/audio/Source.h index 70b431829..54f62dad7 100644 --- a/src/modules/audio/Source.h +++ b/src/modules/audio/Source.h @@ -1,119 +1,119 @@ -/** -* Copyright (c) 2006-2012 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_AUDIO_SOURCE_H -#define LOVE_AUDIO_SOURCE_H - -// LOVE -#include -#include - -namespace love -{ -namespace audio -{ - class Source : public Object - { - public: - - enum Type - { - TYPE_STATIC = 1, - TYPE_STREAM, - TYPE_MAX_ENUM - }; // Type - - enum Unit - { - UNIT_SECONDS = 1, - UNIT_SAMPLES, - UNIT_MAX_ENUM - }; - - protected: - Type type; - public: - - Source(Type type); - virtual ~Source(); - - virtual Source * copy() = 0; - - virtual void play() = 0; - virtual void stop() = 0; - virtual void pause() = 0; - virtual void resume() = 0; - virtual void rewind() = 0; - virtual bool isStopped() const = 0; - virtual bool isPaused() const = 0; - virtual bool isFinished() const = 0; - virtual bool update() = 0; - - virtual void setPitch(float pitch) = 0; - virtual float getPitch() const = 0; - - virtual void setVolume(float volume) = 0; - virtual float getVolume() const = 0; - - virtual void seek(float offset, Unit unit) = 0; - virtual float tell(Unit unit) = 0; - - // all float * v must be of size 3 - virtual void setPosition(float * v) = 0; - virtual void getPosition(float * v) const = 0; - virtual void setVelocity(float * v) = 0; - virtual void getVelocity(float * v) const = 0; - virtual void setDirection(float * v) = 0; - virtual void getDirection(float * v) const = 0; - - virtual void setLooping(bool looping) = 0; - virtual bool isLooping() const = 0; - virtual bool isStatic() const = 0; - - virtual void setMinVolume(float volume) = 0; - virtual float getMinVolume() const = 0; - virtual void setMaxVolume(float volume) = 0; - virtual float getMaxVolume() const = 0; - - virtual void setReferenceDistance(float distance) = 0; - virtual float getReferenceDistance() const = 0; - virtual void setRolloffFactor(float factor) = 0; - virtual float getRolloffFactor() const = 0; - virtual void setMaxDistance(float distance) = 0; - virtual float getMaxDistance() const = 0; - - static bool getConstant(const char * in, Type & out); - static bool getConstant(Type in, const char *& out); - static bool getConstant(const char * in, Unit & out); - static bool getConstant(Unit in, const char *& out); - - private: - - static StringMap::Entry typeEntries[]; - static StringMap types; - static StringMap::Entry unitEntries[]; - static StringMap units; - - }; // Source - -} // audio -} // love - -#endif // LOVE_AUDIO_SOURCE_H +/** + * Copyright (c) 2006-2012 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_AUDIO_SOURCE_H +#define LOVE_AUDIO_SOURCE_H + +// LOVE +#include "common/Object.h" +#include "common/StringMap.h" + +namespace love +{ +namespace audio +{ + +class Source : public Object +{ +public: + + enum Type + { + TYPE_STATIC = 1, + TYPE_STREAM, + TYPE_MAX_ENUM + }; // Type + + enum Unit + { + UNIT_SECONDS = 1, + UNIT_SAMPLES, + UNIT_MAX_ENUM + }; + + Source(Type type); + virtual ~Source(); + + virtual Source *copy() = 0; + + virtual void play() = 0; + virtual void stop() = 0; + virtual void pause() = 0; + virtual void resume() = 0; + virtual void rewind() = 0; + virtual bool isStopped() const = 0; + virtual bool isPaused() const = 0; + virtual bool isFinished() const = 0; + virtual bool update() = 0; + + virtual void setPitch(float pitch) = 0; + virtual float getPitch() const = 0; + + virtual void setVolume(float volume) = 0; + virtual float getVolume() const = 0; + + virtual void seek(float offset, Unit unit) = 0; + virtual float tell(Unit unit) = 0; + + // all float * v must be of size 3 + virtual void setPosition(float *v) = 0; + virtual void getPosition(float *v) const = 0; + virtual void setVelocity(float *v) = 0; + virtual void getVelocity(float *v) const = 0; + virtual void setDirection(float *v) = 0; + virtual void getDirection(float *v) const = 0; + + virtual void setLooping(bool looping) = 0; + virtual bool isLooping() const = 0; + virtual bool isStatic() const = 0; + + virtual void setMinVolume(float volume) = 0; + virtual float getMinVolume() const = 0; + virtual void setMaxVolume(float volume) = 0; + virtual float getMaxVolume() const = 0; + + virtual void setReferenceDistance(float distance) = 0; + virtual float getReferenceDistance() const = 0; + virtual void setRolloffFactor(float factor) = 0; + virtual float getRolloffFactor() const = 0; + virtual void setMaxDistance(float distance) = 0; + virtual float getMaxDistance() const = 0; + + static bool getConstant(const char *in, Type &out); + static bool getConstant(Type in, const char *&out); + static bool getConstant(const char *in, Unit &out); + static bool getConstant(Unit in, const char *&out); + +protected: + Type type; + +private: + + static StringMap::Entry typeEntries[]; + static StringMap types; + static StringMap::Entry unitEntries[]; + static StringMap units; + +}; // Source + +} // audio +} // love + +#endif // LOVE_AUDIO_SOURCE_H diff --git a/src/modules/audio/null/Audio.cpp b/src/modules/audio/null/Audio.cpp index 1269dee4a..14493718e 100644 --- a/src/modules/audio/null/Audio.cpp +++ b/src/modules/audio/null/Audio.cpp @@ -1,167 +1,168 @@ -/** -* Copyright (c) 2006-2012 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. -**/ - -#include "Audio.h" - -namespace love -{ -namespace audio -{ -namespace null -{ - Audio::Audio() : distanceModel(DISTANCE_NONE) - { - } - - Audio::~Audio() - { - } - - const char * Audio::getName() const - { - return "love.audio.null"; - } - - love::audio::Source * Audio::newSource(love::sound::Decoder *) - { - return new Source(); - } - - love::audio::Source * Audio::newSource(love::sound::SoundData *) - { - return new Source(); - } - - int Audio::getNumSources() const - { - return 0; - } - - int Audio::getMaxSources() const - { - return 0; - } - - void Audio::play(love::audio::Source *) - { - } - - void Audio::play() - { - } - - void Audio::stop(love::audio::Source *) - { - } - - void Audio::stop() - { - } - - void Audio::pause(love::audio::Source *) - { - } - - void Audio::pause() - { - } - - void Audio::resume(love::audio::Source *) - { - } - - void Audio::resume() - { - } - - void Audio::rewind(love::audio::Source *) - { - } - - void Audio::rewind() - { - } - - void Audio::setVolume(float volume) - { - this->volume = volume; - } - - float Audio::getVolume() const - { - return volume; - } - - void Audio::getPosition(float *) const - { - } - - void Audio::setPosition(float *) - { - } - - void Audio::getOrientation(float *) const - { - } - - void Audio::setOrientation(float *) - { - } - - void Audio::getVelocity(float *) const - { - } - - void Audio::setVelocity(float *) - { - } - - void Audio::record() - { - } - - love::sound::SoundData * Audio::getRecordedData() - { - return NULL; - } - - love::sound::SoundData * Audio::stopRecording(bool) - { - return NULL; - } - - bool Audio::canRecord() - { - return false; - } - - Audio::DistanceModel Audio::getDistanceModel() const - { - return this->distanceModel; - } - - void Audio::setDistanceModel(DistanceModel distanceModel) - { - this->distanceModel = distanceModel; - } - -} // null -} // audio -} // love +/** + * Copyright (c) 2006-2012 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. + **/ + +#include "Audio.h" + +namespace love +{ +namespace audio +{ +namespace null +{ + +Audio::Audio() : distanceModel(DISTANCE_NONE) +{ +} + +Audio::~Audio() +{ +} + +const char *Audio::getName() const +{ + return "love.audio.null"; +} + +love::audio::Source *Audio::newSource(love::sound::Decoder *) +{ + return new Source(); +} + +love::audio::Source *Audio::newSource(love::sound::SoundData *) +{ + return new Source(); +} + +int Audio::getNumSources() const +{ + return 0; +} + +int Audio::getMaxSources() const +{ + return 0; +} + +void Audio::play(love::audio::Source *) +{ +} + +void Audio::play() +{ +} + +void Audio::stop(love::audio::Source *) +{ +} + +void Audio::stop() +{ +} + +void Audio::pause(love::audio::Source *) +{ +} + +void Audio::pause() +{ +} + +void Audio::resume(love::audio::Source *) +{ +} + +void Audio::resume() +{ +} + +void Audio::rewind(love::audio::Source *) +{ +} + +void Audio::rewind() +{ +} + +void Audio::setVolume(float volume) +{ + this->volume = volume; +} + +float Audio::getVolume() const +{ + return volume; +} + +void Audio::getPosition(float *) const +{ +} + +void Audio::setPosition(float *) +{ +} + +void Audio::getOrientation(float *) const +{ +} + +void Audio::setOrientation(float *) +{ +} + +void Audio::getVelocity(float *) const +{ +} + +void Audio::setVelocity(float *) +{ +} + +void Audio::record() +{ +} + +love::sound::SoundData *Audio::getRecordedData() +{ + return NULL; +} + +love::sound::SoundData *Audio::stopRecording(bool) +{ + return NULL; +} + +bool Audio::canRecord() +{ + return false; +} + +Audio::DistanceModel Audio::getDistanceModel() const +{ + return this->distanceModel; +} + +void Audio::setDistanceModel(DistanceModel distanceModel) +{ + this->distanceModel = distanceModel; +} + +} // null +} // audio +} // love diff --git a/src/modules/audio/null/Audio.h b/src/modules/audio/null/Audio.h index 6048aa98e..7dc6119f7 100644 --- a/src/modules/audio/null/Audio.h +++ b/src/modules/audio/null/Audio.h @@ -1,87 +1,89 @@ -/** -* Copyright (c) 2006-2012 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_AUDIO_NULL_AUDIO_H -#define LOVE_AUDIO_NULL_AUDIO_H - -// LOVE -#include