|
1096
|
1 /**
|
|
|
2 * Copyright (c) 2006-2012 LOVE Development Team
|
|
|
3 *
|
|
|
4 * This software is provided 'as-is', without any express or implied
|
|
|
5 * warranty. In no event will the authors be held liable for any damages
|
|
|
6 * arising from the use of this software.
|
|
|
7 *
|
|
|
8 * Permission is granted to anyone to use this software for any purpose,
|
|
|
9 * including commercial applications, and to alter it and redistribute it
|
|
|
10 * freely, subject to the following restrictions:
|
|
|
11 *
|
|
|
12 * 1. The origin of this software must not be misrepresented; you must not
|
|
|
13 * claim that you wrote the original software. If you use this software
|
|
|
14 * in a product, an acknowledgment in the product documentation would be
|
|
|
15 * appreciated but is not required.
|
|
|
16 * 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
17 * misrepresented as being the original software.
|
|
|
18 * 3. This notice may not be removed or altered from any source distribution.
|
|
|
19 **/
|
|
|
20
|
|
|
21 #include "wrap_Image.h"
|
|
|
22
|
|
|
23 #include "common/Data.h"
|
|
|
24 #include "common/StringMap.h"
|
|
|
25
|
|
|
26 #include "devil/Image.h"
|
|
|
27
|
|
|
28 namespace love
|
|
|
29 {
|
|
|
30 namespace image
|
|
|
31 {
|
|
|
32
|
|
|
33 static Image *instance = 0;
|
|
|
34
|
|
|
35 int w_newImageData(lua_State *L)
|
|
|
36 {
|
|
|
37 // Case 1: Integers.
|
|
|
38 if (lua_isnumber(L, 1))
|
|
|
39 {
|
|
|
40 int w = luaL_checkint(L, 1);
|
|
|
41 int h = luaL_checkint(L, 2);
|
|
|
42 if (w <= 0 || h <= 0)
|
|
|
43 return luaL_error(L, "Invalid image size.");
|
|
|
44
|
|
|
45 ImageData *t = 0;
|
|
|
46 try
|
|
|
47 {
|
|
|
48 t = instance->newImageData(w, h);
|
|
|
49 }
|
|
|
50 catch(love::Exception &e)
|
|
|
51 {
|
|
|
52 return luaL_error(L, "%s", e.what());
|
|
|
53 }
|
|
|
54 luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *)t);
|
|
|
55 return 1;
|
|
|
56 }
|
|
|
57
|
|
|
58 // Case 2: Data
|
|
|
59 if (luax_istype(L, 1, DATA_T))
|
|
|
60 {
|
|
|
61 Data *d = luax_checktype<Data>(L, 1, "Data", DATA_T);
|
|
|
62 ImageData *t = 0;
|
|
|
63 try
|
|
|
64 {
|
|
|
65 t = instance->newImageData(d);
|
|
|
66 }
|
|
|
67 catch(love::Exception &e)
|
|
|
68 {
|
|
|
69 return luaL_error(L, "%s", e.what());
|
|
|
70 }
|
|
|
71 luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *)t);
|
|
|
72 return 1;
|
|
|
73 }
|
|
|
74
|
|
|
75 // Case 3: String/File.
|
|
|
76
|
|
|
77 // Convert to File, if necessary.
|
|
|
78 if (lua_isstring(L, 1))
|
|
|
79 luax_convobj(L, 1, "filesystem", "newFile");
|
|
|
80
|
|
|
81 love::filesystem::File *file = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T);
|
|
|
82
|
|
|
83 ImageData *t = 0;
|
|
|
84 try
|
|
|
85 {
|
|
|
86 t = instance->newImageData(file);
|
|
|
87 }
|
|
|
88 catch(love::Exception &e)
|
|
|
89 {
|
|
|
90 return luaL_error(L, "%s", e.what());
|
|
|
91 }
|
|
|
92 luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *)t);
|
|
|
93 return 1;
|
|
|
94 }
|
|
|
95
|
|
|
96 // List of functions to wrap.
|
|
|
97 static const luaL_Reg functions[] =
|
|
|
98 {
|
|
|
99 { "newImageData", w_newImageData },
|
|
|
100 { 0, 0 }
|
|
|
101 };
|
|
|
102
|
|
|
103 static const lua_CFunction types[] =
|
|
|
104 {
|
|
|
105 luaopen_imagedata,
|
|
|
106 0
|
|
|
107 };
|
|
|
108
|
|
|
109 extern "C" int luaopen_love_image(lua_State *L)
|
|
|
110 {
|
|
|
111 if (instance == 0)
|
|
|
112 {
|
|
|
113 try
|
|
|
114 {
|
|
|
115 instance = new love::image::devil::Image();
|
|
|
116 }
|
|
|
117 catch(Exception &e)
|
|
|
118 {
|
|
|
119 return luaL_error(L, "%s", e.what());
|
|
|
120 }
|
|
|
121 }
|
|
|
122 else
|
|
|
123 instance->retain();
|
|
|
124
|
|
|
125 WrappedModule w;
|
|
|
126 w.module = instance;
|
|
|
127 w.name = "image";
|
|
|
128 w.flags = MODULE_IMAGE_T;
|
|
|
129 w.functions = functions;
|
|
|
130 w.types = types;
|
|
|
131
|
|
|
132 return luax_register_module(L, w);
|
|
|
133 }
|
|
|
134
|
|
|
135 } // image
|
|
|
136 } // love
|