-
Notifications
You must be signed in to change notification settings - Fork 16
/
hint.c
99 lines (88 loc) · 2.79 KB
/
hint.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "rubysdl2_internal.h"
#include <SDL_hints.h>
static VALUE sym_priority;
/*
* Document-module: SDL2::Hints
*
* This module enables you to get and set configuration hints.
*
*/
/*
* Clear all hints set by {.[]=}.
*
* @return [nil]
*/
static VALUE Hints_s_clear(VALUE self)
{
SDL_ClearHints();
return Qnil;
}
/*
* @overload [](hint)
* Get the value of a hint.
*
* @param hint [String] the name of the hint to query
*
* @return [String] the string value of a hint
* @return [nil] if the hint isn't set
*/
static VALUE Hints_s_aref(VALUE self, VALUE name)
{
const char* value = SDL_GetHint(StringValueCStr(name));
if (value)
return utf8str_new_cstr(value);
else
return Qnil;
}
/*
* Set the value of a hint.
*
* @overload []=(hint, value)
* Set a hint with normal priority.
*
* @param hint [String] the name of the hint to query
* @param value [String] the value of the hint varaible
*
* @overload []=(hint, priority: , value)
* Set a hint with given priority.
*
* @param hint [String] the name of the hint to query
* @param priority [Integer] the priority, one of the
* {DEFAULT}, {NORMAL}, or {OVERRIDE}.
* @param value [String] the value of the hint varaible
*
* @return [Boolean] return true if the hint was set
*
* @example
* SDL2::Hints["SDL_HINT_XINPUT_ENABLED", priority: SDL2::Hints::OVERRIDE] = "0"
*
*/
static VALUE Hints_s_aset(int argc, VALUE* argv, VALUE self)
{
VALUE name, pri, value;
rb_scan_args(argc, argv, "21", &name, &pri, &value);
if (argc == 2) {
value = pri;
return INT2BOOL(SDL_SetHint(StringValueCStr(name), StringValueCStr(value)));
} else {
Check_Type(pri, T_HASH);
return INT2BOOL(SDL_SetHintWithPriority(StringValueCStr(name),
StringValueCStr(value),
NUM2INT(rb_hash_aref(pri, sym_priority))));
}
return Qnil;
}
void rubysdl2_init_hints(void)
{
VALUE mHints = rb_define_module_under(mSDL2, "Hints");
rb_define_singleton_method(mHints, "clear", Hints_s_clear, 0);
rb_define_singleton_method(mHints, "get", Hints_s_aref, 1);
rb_define_singleton_method(mHints, "[]=", Hints_s_aset, -1);
/* @return [Integer] index for low priority, used fro default values */
rb_define_const(mHints, "DEFAULT", INT2NUM(SDL_HINT_DEFAULT));
/* @return [Integer] index for medium priority, overrided by an environment variable */
rb_define_const(mHints, "NORMAL", INT2NUM(SDL_HINT_NORMAL));
/* @return [Integer] index for high priority, this priority overrides the value by environment variables */
rb_define_const(mHints, "OVERRIDE", INT2NUM(SDL_HINT_OVERRIDE));
sym_priority = ID2SYM(rb_intern("priority"));
}