4 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and documentation
31 * are those of the authors and should not be interpreted as representing
32 * official policies, either expressed or implied, of the Jim Tcl Project.
40 #include <SDL_gfxPrimitives.h>
44 #define AIO_CMD_LEN 128
46 typedef struct JimSdlSurface
51 static void JimSdlSetError(Jim_Interp
*interp
)
53 Jim_SetResultString(interp
, SDL_GetError(), -1);
56 static void JimSdlDelProc(Jim_Interp
*interp
, void *privData
)
58 JimSdlSurface
*jss
= privData
;
62 SDL_FreeSurface(jss
->screen
);
66 /* Calls to commands created via [sdl.surface] are implemented by this
68 static int JimSdlHandlerCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
70 JimSdlSurface
*jss
= Jim_CmdPrivData(interp
);
72 static const char * const options
[] = {
73 "free", "flip", "pixel", "rectangle", "box", "line", "aaline",
74 "circle", "aacircle", "fcircle", NULL
77 { OPT_FREE
, OPT_FLIP
, OPT_PIXEL
, OPT_RECTANGLE
, OPT_BOX
, OPT_LINE
,
78 OPT_AALINE
, OPT_CIRCLE
, OPT_AACIRCLE
, OPT_FCIRCLE
82 Jim_WrongNumArgs(interp
, 1, argv
, "method ?args ...?");
85 if (Jim_GetEnum(interp
, argv
[1], options
, &option
, "SDL surface method", JIM_ERRMSG
) != JIM_OK
)
87 if (option
== OPT_PIXEL
) {
89 long x
, y
, red
, green
, blue
, alpha
= 255;
91 if (argc
!= 7 && argc
!= 8) {
92 Jim_WrongNumArgs(interp
, 2, argv
, "x y red green blue ?alpha?");
95 if (Jim_GetLong(interp
, argv
[2], &x
) != JIM_OK
||
96 Jim_GetLong(interp
, argv
[3], &y
) != JIM_OK
||
97 Jim_GetLong(interp
, argv
[4], &red
) != JIM_OK
||
98 Jim_GetLong(interp
, argv
[5], &green
) != JIM_OK
||
99 Jim_GetLong(interp
, argv
[6], &blue
) != JIM_OK
) {
102 if (argc
== 8 && Jim_GetLong(interp
, argv
[7], &alpha
) != JIM_OK
)
104 pixelRGBA(jss
->screen
, x
, y
, red
, green
, blue
, alpha
);
107 else if (option
== OPT_RECTANGLE
|| option
== OPT_BOX
||
108 option
== OPT_LINE
|| option
== OPT_AALINE
) {
109 /* RECTANGLE, BOX, LINE, AALINE */
110 long x1
, y1
, x2
, y2
, red
, green
, blue
, alpha
= 255;
112 if (argc
!= 9 && argc
!= 10) {
113 Jim_WrongNumArgs(interp
, 2, argv
, "x y red green blue ?alpha?");
116 if (Jim_GetLong(interp
, argv
[2], &x1
) != JIM_OK
||
117 Jim_GetLong(interp
, argv
[3], &y1
) != JIM_OK
||
118 Jim_GetLong(interp
, argv
[4], &x2
) != JIM_OK
||
119 Jim_GetLong(interp
, argv
[5], &y2
) != JIM_OK
||
120 Jim_GetLong(interp
, argv
[6], &red
) != JIM_OK
||
121 Jim_GetLong(interp
, argv
[7], &green
) != JIM_OK
||
122 Jim_GetLong(interp
, argv
[8], &blue
) != JIM_OK
) {
125 if (argc
== 10 && Jim_GetLong(interp
, argv
[9], &alpha
) != JIM_OK
)
129 rectangleRGBA(jss
->screen
, x1
, y1
, x2
, y2
, red
, green
, blue
, alpha
);
132 boxRGBA(jss
->screen
, x1
, y1
, x2
, y2
, red
, green
, blue
, alpha
);
135 lineRGBA(jss
->screen
, x1
, y1
, x2
, y2
, red
, green
, blue
, alpha
);
138 aalineRGBA(jss
->screen
, x1
, y1
, x2
, y2
, red
, green
, blue
, alpha
);
143 else if (option
== OPT_CIRCLE
|| option
== OPT_AACIRCLE
|| option
== OPT_FCIRCLE
) {
144 /* CIRCLE, AACIRCLE, FCIRCLE */
145 long x
, y
, radius
, red
, green
, blue
, alpha
= 255;
147 if (argc
!= 8 && argc
!= 9) {
148 Jim_WrongNumArgs(interp
, 2, argv
, "x y radius red green blue ?alpha?");
151 if (Jim_GetLong(interp
, argv
[2], &x
) != JIM_OK
||
152 Jim_GetLong(interp
, argv
[3], &y
) != JIM_OK
||
153 Jim_GetLong(interp
, argv
[4], &radius
) != JIM_OK
||
154 Jim_GetLong(interp
, argv
[5], &red
) != JIM_OK
||
155 Jim_GetLong(interp
, argv
[6], &green
) != JIM_OK
||
156 Jim_GetLong(interp
, argv
[7], &blue
) != JIM_OK
) {
159 if (argc
== 9 && Jim_GetLong(interp
, argv
[8], &alpha
) != JIM_OK
)
163 circleRGBA(jss
->screen
, x
, y
, radius
, red
, green
, blue
, alpha
);
166 aacircleRGBA(jss
->screen
, x
, y
, radius
, red
, green
, blue
, alpha
);
169 filledCircleRGBA(jss
->screen
, x
, y
, radius
, red
, green
, blue
, alpha
);
174 else if (option
== OPT_FREE
) {
177 Jim_WrongNumArgs(interp
, 2, argv
, "");
180 Jim_DeleteCommand(interp
, Jim_String(argv
[0]));
183 else if (option
== OPT_FLIP
) {
186 Jim_WrongNumArgs(interp
, 2, argv
, "");
189 SDL_Flip(jss
->screen
);
195 static int JimSdlSurfaceCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
198 char buf
[AIO_CMD_LEN
];
200 long screenId
, xres
, yres
;
204 Jim_WrongNumArgs(interp
, 1, argv
, "xres yres");
207 if (Jim_GetLong(interp
, argv
[1], &xres
) != JIM_OK
||
208 Jim_GetLong(interp
, argv
[2], &yres
) != JIM_OK
)
211 /* Try to create the surface */
212 screen
= SDL_SetVideoMode(xres
, yres
, 32, SDL_SWSURFACE
| SDL_ANYFORMAT
);
213 if (screen
== NULL
) {
214 JimSdlSetError(interp
);
217 /* Get the next file id */
218 if (Jim_EvalGlobal(interp
, "if {[catch {incr sdl.surfaceId}]} {set sdl.surfaceId 0}") != JIM_OK
)
220 objPtr
= Jim_GetVariableStr(interp
, "sdl.surfaceId", JIM_ERRMSG
);
223 if (Jim_GetLong(interp
, objPtr
, &screenId
) != JIM_OK
)
226 /* Create the SDL screen command */
227 jss
= Jim_Alloc(sizeof(*jss
));
228 jss
->screen
= screen
;
229 sprintf(buf
, "sdl.surface%ld", screenId
);
230 Jim_CreateCommand(interp
, buf
, JimSdlHandlerCommand
, jss
, JimSdlDelProc
);
231 Jim_SetResult(interp
, Jim_MakeGlobalNamespaceName(interp
, Jim_NewStringObj(interp
, buf
, -1)));
235 int Jim_sdlInit(Jim_Interp
*interp
)
237 if (Jim_PackageProvide(interp
, "sdl", "1.0", JIM_ERRMSG
))
240 if (SDL_Init(SDL_INIT_VIDEO
) < 0) {
241 JimSdlSetError(interp
);
245 Jim_CreateCommand(interp
, "sdl.screen", JimSdlSurfaceCommand
, NULL
, NULL
);