3 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * A copy of the license is also included in the source distribution
12 * of Jim, as a TXT file name called LICENSE.
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
26 #include <SDL/SDL_gfxPrimitives.h>
29 #include "jimautoconf.h"
31 #define AIO_CMD_LEN 128
33 typedef struct JimSdlSurface
38 static void JimSdlSetError(Jim_Interp
*interp
)
40 Jim_SetResultString(interp
, SDL_GetError(), -1);
43 static void JimSdlDelProc(Jim_Interp
*interp
, void *privData
)
45 JimSdlSurface
*jss
= privData
;
49 SDL_FreeSurface(jss
->screen
);
53 /* Calls to commands created via [sdl.surface] are implemented by this
55 static int JimSdlHandlerCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
57 JimSdlSurface
*jss
= Jim_CmdPrivData(interp
);
59 static const char * const options
[] = {
60 "free", "flip", "pixel", "rectangle", "box", "line", "aaline",
61 "circle", "aacircle", "fcircle", NULL
64 { OPT_FREE
, OPT_FLIP
, OPT_PIXEL
, OPT_RECTANGLE
, OPT_BOX
, OPT_LINE
,
65 OPT_AALINE
, OPT_CIRCLE
, OPT_AACIRCLE
, OPT_FCIRCLE
69 Jim_WrongNumArgs(interp
, 1, argv
, "method ?args ...?");
72 if (Jim_GetEnum(interp
, argv
[1], options
, &option
, "SDL surface method", JIM_ERRMSG
) != JIM_OK
)
74 if (option
== OPT_PIXEL
) {
76 long x
, y
, red
, green
, blue
, alpha
= 255;
78 if (argc
!= 7 && argc
!= 8) {
79 Jim_WrongNumArgs(interp
, 2, argv
, "x y red green blue ?alpha?");
82 if (Jim_GetLong(interp
, argv
[2], &x
) != JIM_OK
||
83 Jim_GetLong(interp
, argv
[3], &y
) != JIM_OK
||
84 Jim_GetLong(interp
, argv
[4], &red
) != JIM_OK
||
85 Jim_GetLong(interp
, argv
[5], &green
) != JIM_OK
||
86 Jim_GetLong(interp
, argv
[6], &blue
) != JIM_OK
) {
89 if (argc
== 8 && Jim_GetLong(interp
, argv
[7], &alpha
) != JIM_OK
)
91 pixelRGBA(jss
->screen
, x
, y
, red
, green
, blue
, alpha
);
94 else if (option
== OPT_RECTANGLE
|| option
== OPT_BOX
||
95 option
== OPT_LINE
|| option
== OPT_AALINE
) {
96 /* RECTANGLE, BOX, LINE, AALINE */
97 long x1
, y1
, x2
, y2
, red
, green
, blue
, alpha
= 255;
99 if (argc
!= 9 && argc
!= 10) {
100 Jim_WrongNumArgs(interp
, 2, argv
, "x y red green blue ?alpha?");
103 if (Jim_GetLong(interp
, argv
[2], &x1
) != JIM_OK
||
104 Jim_GetLong(interp
, argv
[3], &y1
) != JIM_OK
||
105 Jim_GetLong(interp
, argv
[4], &x2
) != JIM_OK
||
106 Jim_GetLong(interp
, argv
[5], &y2
) != JIM_OK
||
107 Jim_GetLong(interp
, argv
[6], &red
) != JIM_OK
||
108 Jim_GetLong(interp
, argv
[7], &green
) != JIM_OK
||
109 Jim_GetLong(interp
, argv
[8], &blue
) != JIM_OK
) {
112 if (argc
== 10 && Jim_GetLong(interp
, argv
[9], &alpha
) != JIM_OK
)
116 rectangleRGBA(jss
->screen
, x1
, y1
, x2
, y2
, red
, green
, blue
, alpha
);
119 boxRGBA(jss
->screen
, x1
, y1
, x2
, y2
, red
, green
, blue
, alpha
);
122 lineRGBA(jss
->screen
, x1
, y1
, x2
, y2
, red
, green
, blue
, alpha
);
125 aalineRGBA(jss
->screen
, x1
, y1
, x2
, y2
, red
, green
, blue
, alpha
);
130 else if (option
== OPT_CIRCLE
|| option
== OPT_AACIRCLE
|| option
== OPT_FCIRCLE
) {
131 /* CIRCLE, AACIRCLE, FCIRCLE */
132 long x
, y
, radius
, red
, green
, blue
, alpha
= 255;
134 if (argc
!= 8 && argc
!= 9) {
135 Jim_WrongNumArgs(interp
, 2, argv
, "x y radius red green blue ?alpha?");
138 if (Jim_GetLong(interp
, argv
[2], &x
) != JIM_OK
||
139 Jim_GetLong(interp
, argv
[3], &y
) != JIM_OK
||
140 Jim_GetLong(interp
, argv
[4], &radius
) != JIM_OK
||
141 Jim_GetLong(interp
, argv
[5], &red
) != JIM_OK
||
142 Jim_GetLong(interp
, argv
[6], &green
) != JIM_OK
||
143 Jim_GetLong(interp
, argv
[7], &blue
) != JIM_OK
) {
146 if (argc
== 9 && Jim_GetLong(interp
, argv
[8], &alpha
) != JIM_OK
)
150 circleRGBA(jss
->screen
, x
, y
, radius
, red
, green
, blue
, alpha
);
153 aacircleRGBA(jss
->screen
, x
, y
, radius
, red
, green
, blue
, alpha
);
156 filledCircleRGBA(jss
->screen
, x
, y
, radius
, red
, green
, blue
, alpha
);
161 else if (option
== OPT_FREE
) {
164 Jim_WrongNumArgs(interp
, 2, argv
, "");
167 Jim_DeleteCommand(interp
, Jim_String(argv
[0]));
170 else if (option
== OPT_FLIP
) {
173 Jim_WrongNumArgs(interp
, 2, argv
, "");
176 SDL_Flip(jss
->screen
);
182 static int JimSdlSurfaceCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
185 char buf
[AIO_CMD_LEN
];
187 long screenId
, xres
, yres
;
191 Jim_WrongNumArgs(interp
, 1, argv
, "xres yres");
194 if (Jim_GetLong(interp
, argv
[1], &xres
) != JIM_OK
||
195 Jim_GetLong(interp
, argv
[2], &yres
) != JIM_OK
)
198 /* Try to create the surface */
199 screen
= SDL_SetVideoMode(xres
, yres
, 32, SDL_SWSURFACE
| SDL_ANYFORMAT
);
200 if (screen
== NULL
) {
201 JimSdlSetError(interp
);
204 /* Get the next file id */
205 if (Jim_EvalGlobal(interp
, "if {[catch {incr sdl.surfaceId}]} {set sdl.surfaceId 0}") != JIM_OK
)
207 objPtr
= Jim_GetVariableStr(interp
, "sdl.surfaceId", JIM_ERRMSG
);
210 if (Jim_GetLong(interp
, objPtr
, &screenId
) != JIM_OK
)
213 /* Create the SDL screen command */
214 jss
= Jim_Alloc(sizeof(*jss
));
215 jss
->screen
= screen
;
216 sprintf(buf
, "sdl.surface%ld", screenId
);
217 Jim_CreateCommand(interp
, buf
, JimSdlHandlerCommand
, jss
, JimSdlDelProc
);
218 Jim_SetResultString(interp
, buf
, -1);
222 int Jim_sdlInit(Jim_Interp
*interp
)
224 if (Jim_PackageProvide(interp
, "sdl", "1.0", JIM_ERRMSG
))
227 if (SDL_Init(SDL_INIT_VIDEO
) < 0) {
228 JimSdlSetError(interp
);
232 Jim_CreateCommand(interp
, "sdl.screen", JimSdlSurfaceCommand
, NULL
, NULL
);