Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting and README #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,20 @@ more samples, and the necessary corrections!
- SDL2 is required;
- Optional bindings for `SDL2_Mixer`, `SDL2_Image` and `SDL2_TTF`;
- Crystal > 0.22.0 is required for `SDL2_Mixer` to work correctly.

## How to use as shard

Add this to your shard.yml file
```yaml
dependencies:
sdl:
github: ysbaddaden/sdl.cr
```

You should then run `shards update`

Make sure to
```crystal
require "sdl"
```
in your project.
8 changes: 4 additions & 4 deletions samples/04_keyboard.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ window = SDL::Window.new("SDL tutorial", 640, 480)

surfaces = {
default: SDL.load_bmp(File.join(__DIR__, "data", "press.bmp")),
up: SDL.load_bmp(File.join(__DIR__, "data", "up.bmp")),
down: SDL.load_bmp(File.join(__DIR__, "data", "down.bmp")),
left: SDL.load_bmp(File.join(__DIR__, "data", "left.bmp")),
right: SDL.load_bmp(File.join(__DIR__, "data", "right.bmp")),
up: SDL.load_bmp(File.join(__DIR__, "data", "up.bmp")),
down: SDL.load_bmp(File.join(__DIR__, "data", "down.bmp")),
left: SDL.load_bmp(File.join(__DIR__, "data", "left.bmp")),
right: SDL.load_bmp(File.join(__DIR__, "data", "right.bmp")),
}

bmp = surfaces[:default]
Expand Down
2 changes: 1 addition & 1 deletion samples/10_colorkey.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ image.color_key = {0, 255, 255}
foo = SDL::Texture.from(image, renderer)

# image has a transparent background color (SDL_image sets the colorkey):
#foo = IMG.load(File.join(__DIR__, "data", "foo2.png"), renderer)
# foo = IMG.load(File.join(__DIR__, "data", "foo2.png"), renderer)

loop do
case event = SDL::Event.poll
Expand Down
7 changes: 0 additions & 7 deletions src/events.cr
Original file line number Diff line number Diff line change
Expand Up @@ -280,21 +280,18 @@ module SDL
case event.type
when .window_event?
Window.new(event.window)

when .keydown?, .keyup?
Keyboard.new(event.key)
when .text_editing?
TextEditing.new(event.edit)
when .text_input?
TextInput.new(event.text)

when .mouse_motion?
MouseMotion.new(event.motion)
when .mouse_button_down?, .mouse_button_up?
MouseButton.new(event.button)
when .mouse_wheel?
MouseWheel.new(event.wheel)

when .joy_axis_motion?
JoyAxis.new(event.jaxis)
when .joy_ball_motion?
Expand All @@ -305,24 +302,20 @@ module SDL
JoyButton.new(event.jbutton)
when .joy_device_added?, .joy_device_removed?
JoyDevice.new(event.jdevice)

when .controller_axis_motion?
ControllerAxis.new(event.caxis)
when .controller_button_down?, .controller_button_up?
ControllerButton.new(event.cbutton)
when .controller_device_added?, .controller_device_removed?, .controller_device_remapped?
ControllerDevice.new(event.cdevice)

when .finger_down?, .finger_up?, .finger_motion?
TouchFinger.new(event.tfinger)
when .dollar_gesture?, .dollar_record?
DollarGesture.new(event.dgesture)
when .multi_gesture?
MultiGesture.new(event.mgesture)

when .drop_file?
Drop.new(event.drop)

when .quit?
Quit.new(event.quit)
when .sys_wm_event?
Expand Down
8 changes: 4 additions & 4 deletions src/image.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module SDL
# `SDL::Surface`.
def self.load(path, type : Type? = nil)
SDL::RWops.open(path, "rb") do |rwops|
if type
if type
surface = LibIMG.load_typed_rw(rwops, 1, type.to_s)
raise Error.new("IMG_LoadTyped_RW") unless surface
else
Expand All @@ -56,7 +56,7 @@ module SDL
# `SDL::Texture` for *renderer*.
def self.load(path, renderer : SDL::Renderer, type : Type? = nil)
SDL::RWops.open(path, "rb") do |rwops|
if type
if type
texture = LibIMG.load_texture_typed_rw(renderer, rwops, 1, type.to_s)
raise Error.new("IMG_LoadTextureTyped_RW") unless texture
else
Expand Down Expand Up @@ -85,7 +85,7 @@ module SDL
end
{% end %}

#class File
# class File
# def initialize(path)
# @rwops = SDL::RWops.new(path, "rb")
# end
Expand All @@ -110,6 +110,6 @@ module SDL
# LibIMG.is_{{type.downcase.id}}(@rwops) == 1
# end
# {% end %}
#end
# end
end
end
8 changes: 4 additions & 4 deletions src/lib_img.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ lib LibIMG
alias RWops = LibSDL::RWops

VERSION = {% `pkg-config SDL2_image --modversion`.strip %}
MAJOR = {% VERSION.split('.')[0] %}
MINOR = {% VERSION.split('.')[1] %}
PATCH = {% VERSION.split('.')[2] %}
MAJOR = {% VERSION.split('.')[0] %}
MINOR = {% VERSION.split('.')[1] %}
PATCH = {% VERSION.split('.')[2] %}

@[Flags]
enum Init
Expand All @@ -27,7 +27,7 @@ lib LibIMG
end

fun init = IMG_Init(flags : Init) : Int
fun quit = IMG_Quit()
fun quit = IMG_Quit

fun load = IMG_Load(file : Char*) : Surface*
fun load_rw = IMG_Load_RW(src : RWops*, freesrc : Int) : Surface*
Expand Down
44 changes: 23 additions & 21 deletions src/lib_mix.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ lib LibMix
alias Char = LibC::Char
alias RWops = LibSDL::RWops

VERSION = {% `pkg-config SDL2_mixer --modversion`.strip %}
MAJOR = {% VERSION.split('.')[0] %}
MINOR = {% VERSION.split('.')[1] %}
PATCH = {% VERSION.split('.')[2] %}
MIN_MAX_VOLUME = 128
VERSION = {% `pkg-config SDL2_mixer --modversion`.strip %}
MAJOR = {% VERSION.split('.')[0] %}
MINOR = {% VERSION.split('.')[1] %}
PATCH = {% VERSION.split('.')[2] %}
MIN_MAX_VOLUME = 128
Mix_DEFAULT_FORMAT = LibSDL::AUDIO_S16LSB

@[Flags]
enum Init
FLAC = 0x00000001
Expand Down Expand Up @@ -53,6 +53,7 @@ lib LibMix
alen : UInt32
volume : UInt8
end

alias Chunk = Mix_Chunk

struct WAVStream
Expand All @@ -64,23 +65,24 @@ lib LibMix
union Data
cmd : MusicCMD*
wave : WAVStream*
#mp3 : SMPEG*
#ogg : OGG_music*
# mp3 : SMPEG*
# ogg : OGG_music*
end

struct Mix_Music
type : MusicType
data : Data
end

alias Music = Mix_Music

fun init = Mix_Init(flags : Init) : Int
fun quit = Mix_Quit()
fun quit = Mix_Quit

fun open_audio = Mix_OpenAudio(frequency : Int, format : UInt16, channels : Int, chunksize : Int) : Int
fun close_audio = Mix_CloseAudio()
fun close_audio = Mix_CloseAudio
fun open_audio_device = Mix_OpenAudioDevice(frequency : Int, format : UInt16) : Int

fun allocate_channels = Mix_AllocateChannels(numchans : Int) : Int
fun query_spec = Mix_QuerySpec(frequency : Int*, format : UInt16*, channels : Int*) : Int
fun load_wav_rw = Mix_LoadWAV_RW(src : RWops*, freesrc : Int) : Chunk*
Expand All @@ -91,12 +93,12 @@ lib LibMix
fun quick_load_raw = Mix_QuickLoad_RAW(mem : UInt8*, len : UInt32) : Chunk*
fun free_chunk = Mix_FreeChunk(chunk : Chunk*)
fun free_music = Mix_FreeMusic(music : Music*)
fun get_num_chunk_decoders = Mix_GetNumChunkDecoders() : Int
fun get_num_chunk_decoders = Mix_GetNumChunkDecoders : Int
fun get_chunk_decoder = Mix_GetChunkDecoder(index : Int) : Char*
fun get_num_music_decoders = Mix_GetNumMusicDecoders() : Int
fun get_num_music_decoders = Mix_GetNumMusicDecoders : Int
fun get_music_decoder = Mix_GetMusicDecoder(index : Int) : Char*
fun get_music_type = Mix_GetMusicType(music : Music*) : MusicType
fun get_music_hook_data = Mix_GetMusicHookData()
fun get_music_hook_data = Mix_GetMusicHookData
fun play_channel = Mix_PlayChannel(channel : Int, chunk : Chunk*, loops : Int) : Int
fun play_channel_timed = Mix_PlayChannelTimed(channel : Int, chunk : Chunk*, loops : Int, ticks : Int) : Int
fun fade_in_channel = Mix_FadeInChannelTimed(channel : Int, chunk : Chunk*, loops : Int, ticks : Int, dur : Int) : Int
Expand All @@ -107,14 +109,14 @@ lib LibMix
fun channel_volume = Mix_Volume(channel : Int, volume : Int) : Int
fun channel_paused = Mix_Paused(channel : Int) : Int
fun play_music = Mix_PlayMusic(music : Music*, loops : Int) : Int
fun music_playing = Mix_PlayingMusic() : Int
fun music_paused = Mix_PausedMusic() : Int
fun rewind_music = Mix_RewindMusic()
fun resume_music = Mix_ResumeMusic()
fun pause_music = Mix_PauseMusic()
fun halt_music = Mix_HaltMusic() : Int
fun music_playing = Mix_PlayingMusic : Int
fun music_paused = Mix_PausedMusic : Int
fun rewind_music = Mix_RewindMusic
fun resume_music = Mix_ResumeMusic
fun pause_music = Mix_PauseMusic
fun halt_music = Mix_HaltMusic : Int
fun fade_in_music = Mix_FadeInMusic(music : Music*, loops : Int, ms : Int)
fun fade_out_music = Mix_FadeOutMusic(ms : Int)
fun music_volume = Mix_VolumeMusic(volume : Int) : Int
fun get_error = Mix_GetError() : Char*
fun get_error = Mix_GetError : Char*
end
14 changes: 7 additions & 7 deletions src/lib_sdl.cr
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require "./lib_sdl/main"
require "./lib_sdl/audio"
require "./lib_sdl/clipboard"
#require "./lib_sdl/cpuinfo"
# require "./lib_sdl/cpuinfo"
require "./lib_sdl/error"
require "./lib_sdl/events"
require "./lib_sdl/joystick"
require "./lib_sdl/gamecontroller"
#require "./lib_sdl/haptic"
# require "./lib_sdl/haptic"
require "./lib_sdl/hints"
require "./lib_sdl/messagebox"
require "./lib_sdl/mouse"
Expand All @@ -15,7 +15,7 @@ require "./lib_sdl/power"
require "./lib_sdl/render"
require "./lib_sdl/rwops"
require "./lib_sdl/shape"
#require "./lib_sdl/system"
# require "./lib_sdl/system"
require "./lib_sdl/video"

@[Link("SDL2")]
Expand All @@ -31,9 +31,9 @@ lib LibSDL
alias SizeT = LibC::SizeT

VERSION = {% `pkg-config sdl2 --modversion`.strip %}
MAJOR = {% VERSION.split('.')[0] %}
MINOR = {% VERSION.split('.')[1] %}
PATCH = {% VERSION.split('.')[2] %}
MAJOR = {% VERSION.split('.')[0] %}
MINOR = {% VERSION.split('.')[1] %}
PATCH = {% VERSION.split('.')[2] %}

INIT_TIMER = 0x00000001
INIT_AUDIO = 0x00000010
Expand All @@ -49,5 +49,5 @@ lib LibSDL
fun init_sub_system = SDL_InitSubSystem(flags : UInt32) : Int
fun quit_sub_system = SDL_QuitSubSystem(flags : UInt32)
fun was_init = SDL_WasInit(flags : UInt32) : UInt32
fun quit = SDL_Quit()
fun quit = SDL_Quit
end
Loading