static mrb_value mrb_sdl2_video_surface_initialize(mrb_state *mrb, mrb_value self) { mrb_sdl2_video_surface_data_t *data = (mrb_sdl2_video_surface_data_t*)DATA_PTR(self); uint32_t flags, rmask, gmask, bmask, amask; mrb_int width, height, depth; mrb_get_args(mrb, "iiiiiiii", &flags, &width, &height, &depth, &rmask, &gmask, &bmask, &amask); if (NULL == data) { data = (mrb_sdl2_video_surface_data_t*)mrb_malloc(mrb, sizeof(mrb_sdl2_video_surface_data_t)); if (NULL == data) { mrb_raise(mrb, E_RUNTIME_ERROR, "insufficient memory."); } data->surface = NULL; } else { if (NULL != data->surface) { SDL_FreeSurface(data->surface); } } data->surface = SDL_CreateRGBSurface(flags, width, height, depth, rmask, gmask, bmask, amask); if (NULL == data->surface) { mrb_free(mrb, data); mruby_sdl2_raise_error(mrb); } DATA_PTR(self) = data; DATA_TYPE(self) = &mrb_sdl2_video_surface_data_type; return self; }
static mrb_value mrb_sdl2_joystick_joystick_initialize(mrb_state *mrb, mrb_value self) { SDL_Joystick *joystick = NULL; mrb_sdl2_joystick_joystick_data_t *data = (mrb_sdl2_joystick_joystick_data_t*)DATA_PTR(self); if (NULL == data) { data = (mrb_sdl2_joystick_joystick_data_t*)mrb_malloc(mrb, sizeof(mrb_sdl2_joystick_joystick_data_t)); if (NULL == data) { mrb_raise(mrb, E_RUNTIME_ERROR, "insufficient memory."); } data->is_associated = false; data->joystick = NULL; } if (1 == mrb->c->ci->argc) { mrb_int device_index; mrb_get_args(mrb, "i", &device_index); joystick = SDL_JoystickOpen((int) device_index); } else { mrb_free(mrb, data); mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments."); } if (NULL == joystick) { mrb_free(mrb, data); mruby_sdl2_raise_error(mrb); } data->joystick = joystick; DATA_PTR(self) = data; DATA_TYPE(self) = &mrb_sdl2_joystick_joystick_data_type; return self; }
static mrb_value mrb_sdl2_video_surface_fill_rects(mrb_state *mrb, mrb_value self) { uint32_t color; mrb_value rects; mrb_get_args(mrb, "io", &color, &rects); if (!mrb_array_p(rects)) { mrb_raise(mrb, E_TYPE_ERROR, "given 2nd argument is unexpected type (expected Array)."); } mrb_int const n = mrb_ary_len(mrb, rects); SDL_Surface *s = mrb_sdl2_video_surface_get_ptr(mrb, self); SDL_Rect r[n]; mrb_int i; for (i = 0; i < n; ++i) { SDL_Rect const * const ptr = mrb_sdl2_rect_get_ptr(mrb, mrb_ary_ref(mrb, rects, i)); if (NULL != ptr) { r[i] = *ptr; } else { r[i] = (SDL_Rect) { 0, 0, 0, 0 }; } } if (0 != SDL_FillRects(s, r, n, color)) { mruby_sdl2_raise_error(mrb); } return self; }
static mrb_value mrb_sdl2_ttf_font_get_glyph_metric(mrb_state *mrb, mrb_value self) { int minx, maxx, miny, maxy, advance; mrb_value character; Uint16 ch; mrb_value result; mrb_get_args(mrb, "b", &character); if (mrb_obj_is_kind_of(mrb, character, mrb_class_get_under(mrb, mrb->object_class, "String"))) { ch = *RSTRING_PTR(character); } else if (mrb_obj_is_kind_of(mrb, character, mrb_class_get_under(mrb, mrb->object_class, "Integer"))) { ch = (Uint16) character.value.i; } result = mrb_hash_new_capa(mrb, 5); if (TTF_GlyphMetrics(mrb_sdl2_font_get_ptr(mrb, self), ch, &minx, &maxx, &miny, &maxy, &advance) == -1) { mruby_sdl2_raise_error(mrb); return mrb_false_value(); } mrb_hash_set(mrb, result, mrb_str_new_cstr(mrb, "minx"), mrb_fixnum_value(minx)); mrb_hash_set(mrb, result, mrb_str_new_cstr(mrb, "maxx"), mrb_fixnum_value(maxx)); mrb_hash_set(mrb, result, mrb_str_new_cstr(mrb, "miny"), mrb_fixnum_value(miny)); mrb_hash_set(mrb, result, mrb_str_new_cstr(mrb, "maxy"), mrb_fixnum_value(maxy)); mrb_hash_set(mrb, result, mrb_str_new_cstr(mrb, "advance"), mrb_fixnum_value(advance)); return result; }
static mrb_value mrb_sdl2_input_wait(mrb_state *mrb, mrb_value mod) { SDL_Event event; if (0 == SDL_WaitEvent(&event)) { mruby_sdl2_raise_error(mrb); } return mrb_sdl2_input_event(mrb, &event); }
static mrb_value mrb_sdl2_video_surface_lock(mrb_state *mrb, mrb_value self) { SDL_Surface *s = mrb_sdl2_video_surface_get_ptr(mrb, self); if (0 != SDL_LockSurface(s)) { mruby_sdl2_raise_error(mrb); } return self; }
static mrb_value mrb_sdl2_joystick_joystick_get_id(mrb_state *mrb, mrb_value self) { SDL_Joystick * joystick_p = mrb_sdl2_joystick_joystick_get_ptr(mrb, self); SDL_JoystickID result = SDL_JoystickInstanceID(joystick_p); if (result < 1) { mruby_sdl2_raise_error(mrb); } return mrb_fixnum_value(result); }
static mrb_value mrb_sdl2_video_surface_get_blend_mode(mrb_state *mrb, mrb_value self) { SDL_BlendMode mode; SDL_Surface *s = mrb_sdl2_video_surface_get_ptr(mrb, self); if (0 != SDL_GetSurfaceBlendMode(s, &mode)) { mruby_sdl2_raise_error(mrb); } return mrb_fixnum_value(mode); }
static mrb_value mrb_sdl2_video_surface_get_alpha_mod(mrb_state *mrb, mrb_value self) { uint8_t alpha; SDL_Surface *s = mrb_sdl2_video_surface_get_ptr(mrb, self); if (0 != SDL_GetSurfaceAlphaMod(s, &alpha)) { mruby_sdl2_raise_error(mrb); } return mrb_fixnum_value(alpha); }
static mrb_value mrb_sdl2_video_surface_get_color_key(mrb_state *mrb, mrb_value self) { uint32_t key; SDL_Surface *s = mrb_sdl2_video_surface_get_ptr(mrb, self); if (0 != SDL_GetColorKey(s, &key)) { mruby_sdl2_raise_error(mrb); } return mrb_fixnum_value(key); }
static mrb_value mrb_sdl2_video_surface_set_alpha_mod(mrb_state *mrb, mrb_value self) { mrb_int alpha; mrb_get_args(mrb, "i", &alpha); SDL_Surface *s = mrb_sdl2_video_surface_get_ptr(mrb, self); if (0 != SDL_SetSurfaceAlphaMod(s, (uint8_t)(alpha & 0xff))) { mruby_sdl2_raise_error(mrb); } return self; }
static mrb_value mrb_sdl2_video_surface_set_rle(mrb_state *mrb, mrb_value self) { SDL_Surface *s = mrb_sdl2_video_surface_get_ptr(mrb, self); mrb_bool is_rle_enabled; mrb_get_args(mrb, "b", &is_rle_enabled); if (0 != SDL_SetSurfaceRLE(s, is_rle_enabled ? 1 : 0)) { mruby_sdl2_raise_error(mrb); } return self; }
/* * SDL2::Video::Surface::save_bmp */ static mrb_value mrb_sdl2_video_surface_save_bmp(mrb_state *mrb, mrb_value self) { mrb_value surface, file; mrb_get_args(mrb, "oo", &surface, &file); SDL_Surface * const s = mrb_sdl2_video_surface_get_ptr(mrb, surface); if (0 != SDL_SaveBMP(s, RSTRING_PTR(file))) { mruby_sdl2_raise_error(mrb); } return mrb_nil_value(); }
static mrb_value mrb_sdl2_joystick_joystick_get_axis_y(mrb_state *mrb, mrb_value self) { SDL_Joystick * joystick_p = mrb_sdl2_joystick_joystick_get_ptr(mrb, self); Sint16 result = SDL_JoystickGetAxis(joystick_p, 1); if (result == 0) { mruby_sdl2_raise_error(mrb); } return mrb_fixnum_value(result); }
static mrb_value mrb_sdl2_video_surface_set_blend_mode(mrb_state *mrb, mrb_value self) { mrb_int mode; mrb_get_args(mrb, "i", &mode); SDL_Surface *s = mrb_sdl2_video_surface_get_ptr(mrb, self); if (0 != SDL_SetSurfaceBlendMode(s, (SDL_BlendMode)mode)) { mruby_sdl2_raise_error(mrb); } return self; }
/* * SDL2::Video::Surface::load_bmp */ static mrb_value mrb_sdl2_video_surface_load_bmp(mrb_state *mrb, mrb_value self) { mrb_value file; mrb_get_args(mrb, "S", &file); SDL_Surface *surface = SDL_LoadBMP(RSTRING_PTR(file)); if (NULL == surface) { mruby_sdl2_raise_error(mrb); } return mrb_sdl2_video_surface(mrb, surface, false); }
static mrb_value mrb_sdl2_joystick_joystick_get_num_buttons(mrb_state *mrb, mrb_value self) { SDL_Joystick * joystick_p = mrb_sdl2_joystick_joystick_get_ptr(mrb, self); int result = SDL_JoystickNumBalls(joystick_p); if (result < 1) { mruby_sdl2_raise_error(mrb); } return mrb_fixnum_value(result); }
static mrb_value mrb_sdl2_video_surface_get_solor_key(mrb_state *mrb, mrb_value self) { uint32_t key; int flag; SDL_Surface *s = mrb_sdl2_video_surface_get_ptr(mrb, self); mrb_get_args(mrb, "ii", &flag, &key); if (0 != SDL_SetColorKey(s, flag, key)) { mruby_sdl2_raise_error(mrb); } return self; }
static mrb_value mrb_sdl2_ttf_font_initialize(mrb_state *mrb, mrb_value self) { TTF_Font *font = NULL; mrb_sdl2_ttf_font_data_t *data = (mrb_sdl2_ttf_font_data_t*)DATA_PTR(self); if (NULL == data) { data = (mrb_sdl2_ttf_font_data_t*)mrb_malloc(mrb, sizeof(mrb_sdl2_ttf_font_data_t)); if (NULL == data) { mrb_raise(mrb, E_RUNTIME_ERROR, "insufficient memory."); } data->font = NULL; } if (2 == mrb->c->ci->argc) { SDL_RWops *rwops; mrb_value object; mrb_int size; mrb_get_args(mrb, "oi", &object, &size); if (mrb_obj_is_kind_of(mrb, object, mrb_class_get_under(mrb, mrb->object_class, "String"))) { font = TTF_OpenFont(RSTRING_PTR(object), size); } else if (mrb_obj_is_kind_of(mrb, object, mrb_class_get_under(mrb, mod_SDL2, "RWops"))) { rwops = mrb_sdl2_rwops_get_ptr(mrb, object); font = TTF_OpenFontRW(rwops, 0, size); } } else if (3 == mrb->c->ci->argc) { SDL_RWops *rwops; mrb_value object; mrb_int size, index; mrb_get_args(mrb, "oii", &object, &size, &index); if (mrb_obj_is_kind_of(mrb, object, mrb_class_get_under(mrb, mrb->object_class, "String"))) { font = TTF_OpenFontIndex(RSTRING_PTR(object), size, index); } else if (mrb_obj_is_kind_of(mrb, object, mrb_class_get_under(mrb, mod_SDL2, "RWops"))) { rwops = mrb_sdl2_rwops_get_ptr(mrb, object); font = TTF_OpenFontIndexRW(rwops, 0, size, index); } } else { mrb_free(mrb, data); mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments."); } if (NULL == font) { mrb_free(mrb, data); mruby_sdl2_raise_error(mrb); } data->font = font; DATA_PTR(self) = data; DATA_TYPE(self) = &mrb_sdl2_ttf_font_data_type; return self; }
static mrb_value mrb_sdl2_mutex_unlock(mrb_state *mrb, mrb_value self) { mrb_sdl2_mutex_data_t *data = (mrb_sdl2_mutex_data_t*)mrb_data_get_ptr(mrb, self, &mrb_sdl2_mutex_data_type); if (NULL != data->mutex) { if (0 > SDL_UnlockMutex(data->mutex)) { mruby_sdl2_raise_error(mrb); } } return self; }
static mrb_value mrb_sdl2_mixer_music_play(mrb_state *mrb, mrb_value self) { Mix_Music *c; mrb_int loops; int result; mrb_get_args(mrb, "i", &loops); c = mrb_sdl2_music_get_ptr(mrb, self); if ((result = Mix_PlayMusic(c, loops)) == -1) mruby_sdl2_raise_error(mrb); return mrb_fixnum_value(result); }
static mrb_value mrb_sdl2_video_surface_set_clip_rect(mrb_state *mrb, mrb_value self) { SDL_Surface *s = mrb_sdl2_video_surface_get_ptr(mrb, self); mrb_value arg; mrb_get_args(mrb, "o", &arg); SDL_Rect const * const rect = mrb_sdl2_rect_get_ptr(mrb, arg); if (SDL_FALSE == SDL_SetClipRect(s, rect)) { mruby_sdl2_raise_error(mrb); } return self; }
static mrb_value mrb_sdl2_input_push(mrb_state *mrb, mrb_value self) { mrb_value event; mrb_get_args(mrb, "o", &event); mrb_sdl2_input_event_data_t *data = (mrb_sdl2_input_event_data_t*)mrb_data_get_ptr(mrb, event, &mrb_sdl2_input_event_data_type); int const ret = SDL_PushEvent(&data->event); if (0 > ret) { mruby_sdl2_raise_error(mrb); } return mrb_fixnum_value(ret); }
static mrb_value mrb_sdl2_cond_signal(mrb_state *mrb, mrb_value self) { mrb_sdl2_cond_data_t *data = (mrb_sdl2_cond_data_t*)mrb_data_get_ptr(mrb, self, &mrb_sdl2_cond_data_type); if (NULL == data->cond) { return mrb_nil_value(); } if (0 > SDL_CondSignal(data->cond)) { mruby_sdl2_raise_error(mrb); } return self; }
static mrb_value mrb_sdl2_semaphore_wait(mrb_state *mrb, mrb_value self) { mrb_sdl2_semaphore_data_t *data = (mrb_sdl2_semaphore_data_t*)mrb_data_get_ptr(mrb, self, &mrb_sdl2_semaphore_data_type); if (NULL == data->semaphore) { return mrb_nil_value(); } if (0 > SDL_SemWait(data->semaphore)) { mruby_sdl2_raise_error(mrb); } return self; }
static mrb_value mrb_sdl2_joystick_joystick_event_state(mrb_state *mrb, mrb_value self) { int result; mrb_int state; mrb_get_args(mrb, "i", &state); result = SDL_JoystickEventState((int) state); if (0 > result) { mruby_sdl2_raise_error(mrb); } return (0 == result) ? mrb_false_value() : mrb_true_value(); }
static mrb_value mrb_sdl2_video_surface_fill_rect(mrb_state *mrb, mrb_value self) { uint32_t color; mrb_value rect; mrb_get_args(mrb, "i|o", &color, &rect); SDL_Surface *s = mrb_sdl2_video_surface_get_ptr(mrb, self); SDL_Rect const * const r = mrb_sdl2_rect_get_ptr(mrb, rect); if (0 != SDL_FillRect(s, r, color)) { mruby_sdl2_raise_error(mrb); } return self; }
static mrb_value mrb_sdl2_mixer_music_initialize(mrb_state *mrb, mrb_value self) { Mix_Music *music = NULL; mrb_sdl2_mixer_music_data_t *data = (mrb_sdl2_mixer_music_data_t*)DATA_PTR(self); if (NULL == data) { data = (mrb_sdl2_mixer_music_data_t*)mrb_malloc(mrb, sizeof(mrb_sdl2_mixer_music_data_t)); if (NULL == data) { mrb_raise(mrb, E_RUNTIME_ERROR, "insufficient memory."); } data->music = NULL; } if (1 == mrb->c->ci->argc) { mrb_value obj; SDL_RWops *rwops; mrb_get_args(mrb, "o", &obj); if (mrb_obj_is_kind_of(mrb, obj, mrb_class_get_under(mrb, mrb->object_class, "String"))) { music = Mix_LoadMUS(RSTRING_PTR(obj)); } else if (mrb_obj_is_kind_of(mrb, obj, mrb_class_get_under(mrb, mod_SDL2, "RWops"))) { rwops = mrb_sdl2_rwops_get_ptr(mrb, obj); music = Mix_LoadMUS_RW(rwops, 0); } else { mrb_free(mrb, data); mrb_raise(mrb, E_RUNTIME_ERROR, "unexpected type."); } } else if (2 == mrb->c->ci->argc) { mrb_value rw; mrb_int type; SDL_RWops *rwops; mrb_get_args(mrb, "oi", &rw, &type); rwops = mrb_sdl2_rwops_get_ptr(mrb, rw); music = Mix_LoadMUSType_RW(rwops, (Mix_MusicType) type, 0); } else { mrb_free(mrb, data); mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments."); } if (NULL == music) { mrb_free(mrb, data); mruby_sdl2_raise_error(mrb); } data->music = music; DATA_PTR(self) = data; DATA_TYPE(self) = &mrb_sdl2_mixer_music_data_type; return self; }
static mrb_value mrb_sdl2_joystick_joystick_get_axis(mrb_state *mrb, mrb_value self) { Sint16 result; mrb_int axis; SDL_Joystick * joystick_p = mrb_sdl2_joystick_joystick_get_ptr(mrb, self); mrb_get_args(mrb, "i", &axis); result = SDL_JoystickGetAxis(joystick_p, (int) axis); if (result == 0) { mruby_sdl2_raise_error(mrb); } return mrb_fixnum_value(result); }
static mrb_value mrb_sdl2_video_surface_get_color_mod(mrb_state *mrb, mrb_value self) { uint8_t r, g, b; SDL_Surface *s = mrb_sdl2_video_surface_get_ptr(mrb, self); if (0 != SDL_GetSurfaceColorMod(s, &r, &g, &b)) { mruby_sdl2_raise_error(mrb); } mrb_value rgb[3] = { mrb_fixnum_value(r), mrb_fixnum_value(g), mrb_fixnum_value(b) }; return mrb_obj_new(mrb, mrb_class_get_under(mrb, mod_SDL2, "RGB"), 3, rgb); }