X-Git-Url: http://nitlanguage.org diff --git a/lib/sdl.nit b/lib/sdl.nit index 318b4a6..6de6d2d 100644 --- a/lib/sdl.nit +++ b/lib/sdl.nit @@ -16,11 +16,12 @@ # SDL display support (used in Linux for windows and inputes only) module sdl is - c_compiler_option(exec("sdl-config", "--cflags")) - c_linker_option(exec("sdl-config", "--libs"), "-lSDL_image -lSDL_ttf") + cflags exec("sdl-config", "--cflags") + ldflags(exec("sdl-config", "--libs"), "-lSDL_image -lSDL_ttf") end import mnit_display +import c in "C header" `{ #include @@ -92,22 +93,19 @@ extern class SDLDisplay `{SDL_Surface *`} SDL_FillRect(recv, NULL, SDL_MapRGB(recv->format,ri,gi,bi)); `} - fun events: Sequence[IE] + # SDL events since the last call to this method + fun events: Sequence[SDLInputEvent] do - var new_event: nullable Object = null - var events = new List[IE] + var events = new Array[SDLInputEvent] loop - new_event = poll_event - if new_event != null then # new_event isa Event then # - events.add(new_event) - else - break - end + var new_event = poll_event + if new_event == null then break + events.add new_event end return events end - private fun poll_event: nullable IE import SDLKeyEvent, SDLMouseButtonEvent, SDLMouseMotionEvent, SDLQuitEvent, NativeString.to_s, SDLMouseButtonEvent.as(nullable IE), SDLMouseMotionEvent.as(nullable IE), SDLKeyEvent.as(nullable IE), SDLQuitEvent.as(nullable IE) `{ + 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; SDL_PumpEvents(); @@ -122,7 +120,7 @@ extern class SDLDisplay `{SDL_Surface *`} SDL_GetKeyName(event.key.keysym.sym)); #endif - return SDLKeyEvent_as_nullable_IE( + return SDLKeyEvent_as_nullable_SDLInputEvent( new_SDLKeyEvent(NativeString_to_s( SDL_GetKeyName(event.key.keysym.sym)), event.type==SDL_KEYDOWN)); @@ -134,7 +132,7 @@ extern class SDLDisplay `{SDL_Surface *`} event.motion.x, event.motion.y); #endif - return SDLMouseMotionEvent_as_nullable_IE( + 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))); @@ -144,7 +142,7 @@ extern class SDLDisplay `{SDL_Surface *`} printf("Mouse button \"%d\" pressed at (%d,%d)\n", event.button.button, event.button.x, event.button.y); #endif - return SDLMouseButtonEvent_as_nullable_IE( + return SDLMouseButtonEvent_as_nullable_SDLInputEvent( new_SDLMouseButtonEvent(event.button.x, event.button.y, event.button.button, event.type == SDL_MOUSEBUTTONDOWN)); @@ -152,18 +150,43 @@ extern class SDLDisplay `{SDL_Surface *`} #ifdef DEBUG printf("Quit event\n"); #endif - return SDLQuitEvent_as_nullable_IE(new_SDLQuitEvent()); + return SDLQuitEvent_as_nullable_SDLInputEvent(new_SDLQuitEvent()); } } - return null_InputEvent(); + return null_SDLInputEvent(); `} # Set the position of the cursor to x,y fun warp_mouse(x,y: Int) `{ SDL_WarpMouse(x, y); `} # Show or hide the cursor - fun show_cursor(show: Bool) `{ SDL_ShowCursor(show); `} + fun show_cursor=(val: Bool) `{ SDL_ShowCursor(val? SDL_ENABLE: SDL_DISABLE); `} + + # Is the cursor visible? + fun show_cursor: Bool `{ 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; `} + + # Are instances of `SDLMouseMotionEvent` ignored? + fun ignore_mouse_motion_events: Bool `{ + return SDL_EventState(SDL_MOUSEMOTION, SDL_QUERY); + `} + + # Do not raise instances of `SDLMouseMotionEvent` if `val` + fun ignore_mouse_motion_events=(val: Bool) `{ + SDL_EventState(SDL_MOUSEMOTION, val? SDL_IGNORE: SDL_ENABLE); + `} + + # Does `self` has the mouse focus? + fun mouse_focus: Bool `{ return SDL_GetAppState() & SDL_APPMOUSEFOCUS; `} + + # Does `self` has the input focus? + fun input_focus: Bool `{ return SDL_GetAppState() & SDL_APPINPUTFOCUS; `} end # Basic Drawing figures @@ -172,7 +195,8 @@ extern class SDLDrawable `{SDL_Surface*`} redef type I: SDLImage - redef fun blit(img, x, y) `{ + redef fun blit(img, x, y) do native_blit(img, x.to_i, y.to_i) + private fun native_blit(img: I, x, y: Int) `{ SDL_Rect dst; dst.x = x; dst.y = y; @@ -227,6 +251,12 @@ extern class SDLImage redef fun height: Int `{ return recv->h; `} fun is_ok: Bool do return not address_is_null + + # Returns a reference to the pixels of the texture + fun pixels: NativeCByteArray `{ return recv->pixels; `} + + # Does this texture has an alpha mask? + fun amask: Bool `{ return recv->format->Amask; `} end # A simple rectangle @@ -282,6 +312,24 @@ class SDLMouseButtonEvent redef var pressed: Bool redef fun depressed: Bool do return not pressed + # Is this event raised by the left button? + fun is_left_button: Bool do return button == 1 + + # Is this event raised by the right button? + fun is_right_button: Bool do return button == 3 + + # Is this event raised by the middle button? + fun is_middle_button: Bool do return button == 2 + + # Is this event raised by the wheel going down? + fun is_down_wheel: Bool do return button == 4 + + # Is this event raised by the wheel going up? + fun is_up_wheel: Bool do return button == 5 + + # Is this event raised by the wheel? + fun is_wheel: Bool do return is_down_wheel or is_up_wheel + init (x, y: Float, button: Int, pressed: Bool) do super(x, y) @@ -327,27 +375,27 @@ class SDLKeyEvent super KeyEvent super SDLInputEvent - var key_name: String + redef var name var down: Bool init (key_name: String, down: Bool) do - self.key_name = key_name + self.name = key_name self.down = down end redef fun to_c: nullable Char do - if key_name.length == 1 then return key_name.chars.first + if name.length == 1 then return name.chars.first return null end redef fun to_s do if down then - return "KeyboardEvent key {key_name} down" + return "KeyboardEvent key {name} down" else - return "KeyboardEvent key {key_name} up" + return "KeyboardEvent key {name} up" end end @@ -355,13 +403,13 @@ class SDLKeyEvent redef fun is_down do return down # Return true if the key is the up arrow - redef fun is_arrow_up do return key_name == "up" + 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 key_name == "left" + 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 key_name == "down" + 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 key_name == "right" + redef fun is_arrow_right do return name == "right" end class SDLQuitEvent