# See the License for the specific language governing permissions and
# limitations under the License.
-# SDL display support (used in Linux for windows and inputes only)
+# Simple DirectMedia Layer
module sdl is
cflags exec("sdl-config", "--cflags")
ldflags(exec("sdl-config", "--libs"), "-lSDL_image -lSDL_ttf")
end
-import mnit_display
+import mnit::display
import c
in "C header" `{
SDL_FillRect(self, NULL, SDL_MapRGB(self->format,r,g,b));
`}
- redef fun width: Int `{ return self->w; `}
- redef fun height: Int `{ return self->h; `}
+ redef fun width `{ return self->w; `}
+ redef fun height `{ return self->h; `}
# Fill a rectangle with given color
fun fill_rect(rect: SDLRectangle, r, g, b: Int) `{
SDL_FillRect(self, rect, SDL_MapRGB(self->format,r,g,b));
`}
- redef fun clear(r, g, b: Float) `{
+ redef fun clear(r, g, b) `{
Uint8 ri, gi, bi;
ri = (Uint8)r*255;
gi = (Uint8)g*255;
return events
end
- private fun poll_event: nullable SDLInputEvent import SDLKeyEvent, SDLMouseButtonEvent, SDLMouseMotionEvent, SDLQuitEvent, NativeString.to_s, SDLMouseButtonEvent.as(nullable SDLInputEvent), SDLMouseMotionEvent.as(nullable SDLInputEvent), SDLKeyEvent.as(nullable SDLInputEvent), SDLQuitEvent.as(nullable SDLInputEvent) `{
- SDL_Event event;
-
+ private fun poll_event: nullable SDLInputEvent
+ import new_key_event, new_mouse_motion_event, new_mouse_button_event, new_quit_event `{
SDL_PumpEvents();
+ SDL_Event event;
if (SDL_PollEvent(&event))
{
switch (event.type) {
case SDL_KEYDOWN:
case SDL_KEYUP:
- #ifdef DEBUG
- printf("The \"%s\" key was pressed!\n",
- SDL_GetKeyName(event.key.keysym.sym));
- #endif
-
- return SDLKeyEvent_as_nullable_SDLInputEvent(
- new_SDLKeyEvent(NativeString_to_s(
- SDL_GetKeyName(event.key.keysym.sym)),
- event.type==SDL_KEYDOWN));
+ return SDLDisplay_new_key_event(self,
+ SDL_GetKeyName(event.key.keysym.sym),
+ event.type==SDL_KEYDOWN);
case SDL_MOUSEMOTION:
- #ifdef DEBUG
- printf("Mouse moved by %d,%d to (%d,%d)\n",
- event.motion.xrel, event.motion.yrel,
- event.motion.x, event.motion.y);
- #endif
-
- return SDLMouseMotionEvent_as_nullable_SDLInputEvent(
- new_SDLMouseMotionEvent(event.motion.x, event.motion.y,
- event.motion.xrel, event.motion.yrel, SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1)));
+ return SDLDisplay_new_mouse_motion_event(self,
+ event.motion.x, event.motion.y,
+ event.motion.xrel, event.motion.yrel,
+ SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1));
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
- #ifdef DEBUG
- printf("Mouse button \"%d\" pressed at (%d,%d)\n",
- event.button.button, event.button.x, event.button.y);
- #endif
- return SDLMouseButtonEvent_as_nullable_SDLInputEvent(
- new_SDLMouseButtonEvent(event.button.x, event.button.y,
- event.button.button, event.type == SDL_MOUSEBUTTONDOWN));
+ return SDLDisplay_new_mouse_button_event(self,
+ event.button.x, event.button.y,
+ event.button.button,
+ event.type == SDL_MOUSEBUTTONDOWN);
case SDL_QUIT:
- #ifdef DEBUG
- printf("Quit event\n");
- #endif
- return SDLQuitEvent_as_nullable_SDLInputEvent(new_SDLQuitEvent());
+ return SDLDisplay_new_quit_event(self);
}
}
return null_SDLInputEvent();
`}
+ private fun new_key_event(name: NativeString, down: Bool): nullable SDLInputEvent
+ do return new SDLKeyEvent(name.to_s, down)
+
+ private fun new_mouse_motion_event(x, y, xr, yr: Float, down: Bool): nullable SDLInputEvent
+ do return new SDLMouseMotionEvent(x, y, xr, yr, down)
+
+ private fun new_mouse_button_event(x, y: Float, id: Int, down: Bool): nullable SDLInputEvent
+ do return new SDLMouseButtonEvent(x, y, id, down)
+
+ private fun new_quit_event: nullable SDLInputEvent
+ do return new SDLQuitEvent
+
# Set the position of the cursor to x,y
fun warp_mouse(x,y: Int) `{ SDL_WarpMouse(x, y); `}
fun show_cursor=(val: Bool) `{ SDL_ShowCursor(val? SDL_ENABLE: SDL_DISABLE); `}
# Is the cursor visible?
- fun show_cursor: Bool `{ SDL_ShowCursor(SDL_QUERY); `}
+ fun show_cursor: Bool `{ return SDL_ShowCursor(SDL_QUERY); `}
# Grab or release the input
fun grab_input=(val: Bool) `{ SDL_WM_GrabInput(val? SDL_GRAB_ON: SDL_GRAB_OFF); `}
# Is the input grabbed?
- fun grab_input: Bool `{ SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON; `}
+ fun grab_input: Bool `{ return SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON; `}
# Are instances of `SDLMouseMotionEvent` ignored?
fun ignore_mouse_motion_events: Bool `{
# Destroy the image and free the memory
redef fun destroy `{ SDL_FreeSurface(self); `}
- redef fun width: Int `{ return self->w; `}
- redef fun height: Int `{ return self->h; `}
+ redef fun width `{ return self->w; `}
+ redef fun height `{ return self->h; `}
fun is_ok: Bool do return not address_is_null
# Returns a reference to the pixels of the texture
fun pixels: NativeCByteArray `{ return self->pixels; `}
- # Does this texture has an alpha mask?
- fun amask: Bool `{ return self->format->Amask; `}
+ # Mask for the alpha value of each pixel
+ fun amask: Int `{ return self->format->Amask; `}
end
# A simple rectangle
super PointerEvent
super SDLInputEvent
- redef var x: Float
- redef var y: Float
+ redef var x
+ redef var y
- private init (x, y: Float)
- do
- self.x = x
- self.y = y
- end
+ redef fun is_move do return false
end
# MouseButtonEvent used to get information when a button is pressed/depressed
var button: Int
- redef var pressed: Bool
- redef fun depressed: Bool do return not pressed
+ redef var pressed
# Is this event raised by the left button?
fun is_left_button: Bool do return button == 1
var rel_x: Float
var rel_y: Float
- redef var pressed: Bool
- redef fun depressed: Bool do return not pressed
+ redef var pressed
+
+ redef fun is_move do return true
init (x, y, rel_x, rel_y: Float, pressed: Bool)
do
super SDLInputEvent
redef var name
- var down: Bool
- init (key_name: String, down: Bool)
- do
- self.name = key_name
- self.down = down
- end
+ var down: Bool
- redef fun to_c: nullable Char
+ redef fun to_c
do
if name.length == 1 then return name.chars.first
return null
# Return true if the key is the up arrow
redef fun is_arrow_up do return name == "up"
+
# Return true if the key is the left arrow
redef fun is_arrow_left do return name == "left"
+
# Return true if the key is the down arrow
redef fun is_arrow_down do return name == "down"
+
# Return true if the key is the right arrow
redef fun is_arrow_right do return name == "right"
end