ruby2d 0.9.0 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (132) hide show
  1. checksums.yaml +4 -4
  2. data/assets/README.md +8 -8
  3. data/assets/Rakefile +85 -0
  4. data/assets/include/SDL2/SDL.h +1 -1
  5. data/assets/include/SDL2/SDL_assert.h +1 -1
  6. data/assets/include/SDL2/SDL_atomic.h +20 -2
  7. data/assets/include/SDL2/SDL_audio.h +47 -14
  8. data/assets/include/SDL2/SDL_bits.h +10 -1
  9. data/assets/include/SDL2/SDL_blendmode.h +7 -7
  10. data/assets/include/SDL2/SDL_clipboard.h +1 -1
  11. data/assets/include/SDL2/SDL_config.h +24 -390
  12. data/assets/include/SDL2/SDL_config_android.h +178 -0
  13. data/assets/include/SDL2/SDL_config_iphoneos.h +201 -0
  14. data/assets/include/SDL2/SDL_config_macosx.h +240 -0
  15. data/assets/include/SDL2/SDL_config_minimal.h +85 -0
  16. data/assets/include/SDL2/SDL_config_os2.h +170 -0
  17. data/assets/include/SDL2/SDL_config_pandora.h +133 -0
  18. data/assets/include/SDL2/SDL_config_psp.h +164 -0
  19. data/assets/include/SDL2/SDL_config_windows.h +257 -0
  20. data/assets/include/SDL2/SDL_config_winrt.h +240 -0
  21. data/assets/include/SDL2/SDL_config_wiz.h +146 -0
  22. data/assets/include/SDL2/SDL_copying.h +20 -0
  23. data/assets/include/SDL2/SDL_cpuinfo.h +90 -8
  24. data/assets/include/SDL2/SDL_egl.h +1 -1
  25. data/assets/include/SDL2/SDL_endian.h +1 -1
  26. data/assets/include/SDL2/SDL_error.h +1 -1
  27. data/assets/include/SDL2/SDL_events.h +4 -1
  28. data/assets/include/SDL2/SDL_filesystem.h +1 -1
  29. data/assets/include/SDL2/SDL_gamecontroller.h +1 -1
  30. data/assets/include/SDL2/SDL_gesture.h +1 -1
  31. data/assets/include/SDL2/SDL_haptic.h +1 -1
  32. data/assets/include/SDL2/SDL_hints.h +145 -14
  33. data/assets/include/SDL2/SDL_image.h +2 -2
  34. data/assets/include/SDL2/SDL_joystick.h +1 -1
  35. data/assets/include/SDL2/SDL_keyboard.h +1 -1
  36. data/assets/include/SDL2/SDL_keycode.h +1 -1
  37. data/assets/include/SDL2/SDL_loadso.h +1 -1
  38. data/assets/include/SDL2/SDL_log.h +1 -1
  39. data/assets/include/SDL2/SDL_main.h +28 -16
  40. data/assets/include/SDL2/SDL_messagebox.h +1 -1
  41. data/assets/include/SDL2/SDL_mouse.h +1 -1
  42. data/assets/include/SDL2/SDL_mutex.h +1 -1
  43. data/assets/include/SDL2/SDL_name.h +1 -1
  44. data/assets/include/SDL2/SDL_opengl.h +1 -1
  45. data/assets/include/SDL2/SDL_opengles.h +1 -1
  46. data/assets/include/SDL2/SDL_opengles2.h +1 -1
  47. data/assets/include/SDL2/SDL_pixels.h +3 -3
  48. data/assets/include/SDL2/SDL_platform.h +1 -1
  49. data/assets/include/SDL2/SDL_power.h +1 -1
  50. data/assets/include/SDL2/SDL_quit.h +1 -1
  51. data/assets/include/SDL2/SDL_rect.h +29 -3
  52. data/assets/include/SDL2/SDL_render.h +168 -1
  53. data/assets/include/SDL2/SDL_revision.h +2 -2
  54. data/assets/include/SDL2/SDL_rwops.h +50 -13
  55. data/assets/include/SDL2/SDL_scancode.h +2 -2
  56. data/assets/include/SDL2/SDL_sensor.h +4 -4
  57. data/assets/include/SDL2/SDL_shape.h +1 -1
  58. data/assets/include/SDL2/SDL_stdinc.h +1 -1
  59. data/assets/include/SDL2/SDL_surface.h +2 -1
  60. data/assets/include/SDL2/SDL_system.h +1 -1
  61. data/assets/include/SDL2/SDL_syswm.h +11 -8
  62. data/assets/include/SDL2/SDL_test.h +1 -1
  63. data/assets/include/SDL2/SDL_test_assert.h +1 -1
  64. data/assets/include/SDL2/SDL_test_common.h +22 -5
  65. data/assets/include/SDL2/SDL_test_compare.h +1 -1
  66. data/assets/include/SDL2/SDL_test_crc32.h +1 -1
  67. data/assets/include/SDL2/SDL_test_font.h +1 -1
  68. data/assets/include/SDL2/SDL_test_fuzzer.h +1 -1
  69. data/assets/include/SDL2/SDL_test_harness.h +1 -1
  70. data/assets/include/SDL2/SDL_test_images.h +1 -1
  71. data/assets/include/SDL2/SDL_test_log.h +1 -1
  72. data/assets/include/SDL2/SDL_test_md5.h +1 -1
  73. data/assets/include/SDL2/SDL_test_memory.h +3 -3
  74. data/assets/include/SDL2/SDL_test_random.h +1 -1
  75. data/assets/include/SDL2/SDL_thread.h +1 -1
  76. data/assets/include/SDL2/SDL_timer.h +1 -1
  77. data/assets/include/SDL2/SDL_touch.h +17 -1
  78. data/assets/include/SDL2/SDL_types.h +1 -1
  79. data/assets/include/SDL2/SDL_version.h +2 -2
  80. data/assets/include/SDL2/SDL_video.h +1 -1
  81. data/assets/include/SDL2/SDL_vulkan.h +3 -3
  82. data/assets/include/SDL2/begin_code.h +4 -1
  83. data/assets/include/SDL2/close_code.h +4 -1
  84. data/assets/macos/lib/libFLAC.a +0 -0
  85. data/assets/macos/lib/libSDL2.a +0 -0
  86. data/assets/macos/lib/libSDL2_image.a +0 -0
  87. data/assets/macos/lib/libSDL2_mixer.a +0 -0
  88. data/assets/macos/lib/libSDL2_ttf.a +0 -0
  89. data/assets/macos/lib/libfreetype.a +0 -0
  90. data/assets/macos/lib/libjpeg.a +0 -0
  91. data/assets/macos/lib/libmpg123.a +0 -0
  92. data/assets/macos/lib/libogg.a +0 -0
  93. data/assets/macos/lib/libpng16.a +0 -0
  94. data/assets/macos/lib/libtiff.a +0 -0
  95. data/assets/macos/lib/libwebp.a +0 -0
  96. data/assets/mingw/bin/SDL2.dll +0 -0
  97. data/assets/mingw/bin/SDL2_image.dll +0 -0
  98. data/assets/mingw/bin/libpng16-16.dll +0 -0
  99. data/assets/mingw/bin/libtiff-5.dll +0 -0
  100. data/assets/mingw/bin/libwebp-7.dll +0 -0
  101. data/assets/mingw/lib/libSDL2.a +0 -0
  102. data/assets/mingw/lib/libSDL2.dll.a +0 -0
  103. data/assets/mingw/lib/libSDL2_image.a +0 -0
  104. data/assets/mingw/lib/libSDL2_image.dll.a +0 -0
  105. data/assets/mingw/lib/libSDL2_test.a +0 -0
  106. data/assets/mingw/lib/libSDL2main.a +0 -0
  107. data/bin/ruby2d +2 -1
  108. data/ext/ruby2d/common.c +185 -0
  109. data/ext/ruby2d/controllers.c +110 -0
  110. data/ext/ruby2d/extconf.rb +97 -46
  111. data/ext/ruby2d/gl.c +430 -0
  112. data/ext/ruby2d/gl2.c +146 -0
  113. data/ext/ruby2d/gl3.c +348 -0
  114. data/ext/ruby2d/gles.c +308 -0
  115. data/ext/ruby2d/image.c +138 -0
  116. data/ext/ruby2d/input.c +48 -0
  117. data/ext/ruby2d/music.c +114 -0
  118. data/ext/ruby2d/ruby2d.c +125 -126
  119. data/{assets/include/simple2d.h → ext/ruby2d/ruby2d.h} +223 -201
  120. data/ext/ruby2d/shapes.c +154 -0
  121. data/ext/ruby2d/sound.c +93 -0
  122. data/ext/ruby2d/sprite.c +147 -0
  123. data/ext/ruby2d/text.c +129 -0
  124. data/ext/ruby2d/window.c +414 -0
  125. data/lib/ruby2d.rb +1 -1
  126. data/lib/ruby2d/cli/build.rb +3 -8
  127. data/lib/ruby2d/cli/colorize.rb +10 -0
  128. data/lib/ruby2d/version.rb +1 -1
  129. metadata +34 -10
  130. data/assets/macos/lib/libsimple2d.a +0 -0
  131. data/assets/mingw/lib/libsimple2d.a +0 -0
  132. data/lib/ruby2d/colorize.rb +0 -10
@@ -1,8 +1,25 @@
1
1
  require 'mkmf'
2
- require_relative '../../lib/ruby2d/colorize'
2
+ require_relative '../../lib/ruby2d/cli/colorize'
3
+
4
+ $errors = [] # Holds errors
5
+
6
+ # Set the OS platform
7
+ case RUBY_PLATFORM
8
+ when /darwin/
9
+ $platform = :macos
10
+ when /linux/
11
+ $platform = :linux
12
+ if `cat /etc/os-release` =~ /raspbian/
13
+ $platform = :linux_rpi
14
+ end
15
+ when /mingw/
16
+ $platform = :windows
17
+ else
18
+ $platform = nil
19
+ end
20
+
3
21
 
4
- S2D_VERSION = '1.1.0' # Simple 2D minimum version required
5
- $errors = [] # Array to capture errors
22
+ # Helper functions #############################################################
6
23
 
7
24
  # Print installation errors
8
25
  def print_errors
@@ -12,42 +29,71 @@ def print_errors
12
29
  #{"======================================================================"}"
13
30
  end
14
31
 
15
- # Check that Simple 2D is installed and meets minimum version requirements
16
- def check_s2d
17
32
 
18
- # Simple 2D not installed
19
- if `which simple2d`.empty?
20
- $errors << "Ruby 2D uses a native library called Simple 2D, which was not found." <<
21
- "To install, follow the instructions at #{"ruby2d.com/learn".bold}"
22
- print_errors
23
- exit
33
+ # Add compiler and linker flags
34
+ def add_flags(type, flags)
35
+ case type
36
+ when :c
37
+ $CFLAGS << " #{flags} "
38
+ when :ld
39
+ $LDFLAGS << " #{flags} "
40
+ end
41
+ end
24
42
 
25
- # Simple 2D installed, checking version
26
- else
27
- unless Gem::Version.new(`bash simple2d --version`) >= Gem::Version.new(S2D_VERSION)
28
- $errors << "Simple 2D needs to be updated for this version of Ruby 2D." <<
29
- "Run the following, then try reinstalling this gem:\n" <<
30
- " simple2d update".bold
31
- print_errors
32
- exit
43
+
44
+ # Check SDL libraries on Linux
45
+ def check_sdl_linux
46
+ unless have_library('SDL2') && have_library('SDL2_image') && have_library('SDL2_mixer') && have_library('SDL2_ttf')
47
+
48
+ $errors << "Couldn't find packages needed by Ruby 2D."
49
+
50
+ # Fedora and CentOS
51
+ if system('which yum')
52
+ $errors << "Install the following packages using `yum` (or `dnf`) and try again:\n" <<
53
+ " SDL2-devel SDL2_image-devel SDL2_mixer-devel SDL2_ttf-devel".bold
54
+
55
+ # Arch
56
+ elsif system('which pacman')
57
+ $errors << "Install the following packages using `pacman` and try again:\n" <<
58
+ " sdl2 sdl2_image sdl2_mixer sdl2_ttf".bold
59
+
60
+ # openSUSE
61
+ elsif system('which zypper')
62
+ $errors << "Install the following packages using `zypper` and try again:\n" <<
63
+ " libSDL2-devel libSDL2_image-devel libSDL2_mixer-devel libSDL2_ttf-devel".bold
64
+
65
+ # Ubuntu, Debian, and Mint
66
+ # `apt` must be last because openSUSE has it aliased to `zypper`
67
+ elsif system('which apt')
68
+ $errors << "Install the following packages using `apt` and try again:\n" <<
69
+ " libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev".bold
33
70
  end
71
+
72
+ $errors << "" << "See #{"ruby2d.com".bold} for additional help."
73
+ print_errors; exit
34
74
  end
35
75
  end
36
76
 
37
- # Use the Simple 2D, SDL, and other libraries installed by the user (not those bundled with the gem)
38
- def use_usr_libs
39
- check_s2d
40
77
 
41
- # Add flags
42
- $CFLAGS << ' -std=c11 -I/usr/local/include'
43
- if `cat /etc/os-release` =~ /raspbian/ # Raspberry Pi
44
- $CFLAGS << ' -I/opt/vc/include'
78
+ # Set Raspberry Pi flags
79
+ def set_rpi_flags
80
+ if $platform == :linux_rpi
81
+ add_flags(:c, '-I/opt/vc/include')
82
+ add_flags(:ld, '-L/opt/vc/lib -lbrcmGLESv2')
45
83
  end
46
- $LDFLAGS << ' ' << `bash simple2d --libs`
47
- $LDFLAGS.gsub!(/\n/, ' ') # remove newlines in flags, they cause problems
48
84
  end
49
85
 
50
86
 
87
+ # Use SDL and other libraries installed by the user (not those bundled with the gem)
88
+ def use_usr_libs
89
+ # Add flags
90
+ set_rpi_flags
91
+ add_flags(:c, '-I/usr/local/include')
92
+ end
93
+
94
+
95
+ # Configure native extension ###################################################
96
+
51
97
  # Build Ruby 2D native extention using libraries installed by user
52
98
  # To use install flag: `gem install ruby2d -- libs`
53
99
  if ARGV.include? 'libs'
@@ -55,27 +101,31 @@ if ARGV.include? 'libs'
55
101
 
56
102
  # Use libraries provided by the gem (default)
57
103
  else
58
- $CFLAGS << ' -std=c11 -I../../assets/include'
59
- case RUBY_PLATFORM
104
+ add_flags(:c, '-std=c11')
60
105
 
61
- # macOS
62
- when /darwin/
63
- # $LDFLAGS << " -L../../assets/macos/lib -lsimple2d -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf -ljpeg -lpng16 -ltiff -lwebp -lmpg123 -logg -lflac -lvorbis -lvorbisfile -lfreetype -Wl,-framework,Cocoa -Wl,-framework,ForceFeedback"
106
+ case $platform
64
107
 
108
+ when :macos
109
+ add_flags(:c, '-I../../assets/include')
65
110
  ldir = "#{Dir.pwd}/../../assets/macos/lib"
66
- $LDFLAGS << " #{ldir}/libsimple2d.a #{ldir}/libSDL2.a #{ldir}/libSDL2_image.a #{ldir}/libSDL2_mixer.a #{ldir}/libSDL2_ttf.a \
67
- #{ldir}/libjpeg.a #{ldir}/libpng16.a #{ldir}/libtiff.a #{ldir}/libwebp.a \
68
- #{ldir}/libmpg123.a #{ldir}/libogg.a #{ldir}/libflac.a #{ldir}/libvorbis.a #{ldir}/libvorbisfile.a \
69
- #{ldir}/libfreetype.a -Wl,-framework,Cocoa -Wl,-framework,ForceFeedback"
70
-
71
- # Linux
72
- when /linux/
73
- # TODO: Implement static compilation for Linux
74
- use_usr_libs
75
111
 
76
- # Windows / MinGW
77
- when /mingw/
78
- $LDFLAGS << " -L../../assets/mingw/lib -lsimple2d -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf -lmingw32 -lopengl32 -lglew32"
112
+ add_flags(:ld, "#{ldir}/libSDL2.a #{ldir}/libSDL2_image.a #{ldir}/libSDL2_mixer.a #{ldir}/libSDL2_ttf.a")
113
+ add_flags(:ld, "#{ldir}/libjpeg.a #{ldir}/libpng16.a #{ldir}/libtiff.a #{ldir}/libwebp.a")
114
+ add_flags(:ld, "#{ldir}/libmpg123.a #{ldir}/libogg.a #{ldir}/libFLAC.a #{ldir}/libvorbis.a #{ldir}/libvorbisfile.a")
115
+ add_flags(:ld, "#{ldir}/libfreetype.a")
116
+ add_flags(:ld, "-Wl,-framework,Cocoa -Wl,-framework,ForceFeedback")
117
+
118
+ when :linux, :linux_rpi
119
+ check_sdl_linux
120
+
121
+ set_rpi_flags
122
+ add_flags(:ld, "-lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf -lm")
123
+ if $platform == :linux then add_flags(:ld, '-lGL') end
124
+
125
+ when :windows
126
+ add_flags(:c, '-I../../assets/include')
127
+ add_flags(:ld, '-L../../assets/mingw/lib -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf')
128
+ add_flags(:ld, '-lmingw32 -lopengl32 -lglew32')
79
129
 
80
130
  # If can't detect the platform, use libraries installed by the user
81
131
  else
@@ -83,6 +133,7 @@ else
83
133
  end
84
134
  end
85
135
 
136
+ $LDFLAGS.gsub!(/\n/, ' ') # remove newlines in flags, they can cause problems
86
137
 
87
- # Create the Makefile
138
+ # Create Makefile
88
139
  create_makefile('ruby2d/ruby2d')
data/ext/ruby2d/gl.c ADDED
@@ -0,0 +1,430 @@
1
+ // Ruby 2D OpenGL Functions
2
+
3
+ #include "ruby2d.h"
4
+
5
+ // Set to `true` to force OpenGL 2.1 (for testing)
6
+ static bool FORCE_GL2 = false;
7
+
8
+ // Flag set if using OpenGL 2.1
9
+ static bool R2D_GL2 = false;
10
+
11
+ // The orthographic projection matrix for 2D rendering.
12
+ // Elements 0 and 5 are set in R2D_GL_SetViewport.
13
+ static GLfloat orthoMatrix[16] =
14
+ { 0, 0, 0, 0,
15
+ 0, 0, 0, 0,
16
+ 0, 0, 0, 0,
17
+ -1.0f, 1.0f, -1.0f, 1.0f };
18
+
19
+
20
+ /*
21
+ * Prints current GL error
22
+ */
23
+ void R2D_GL_PrintError(char *error) {
24
+ R2D_Log(R2D_ERROR, "%s (%d)", error, glGetError());
25
+ }
26
+
27
+
28
+ /*
29
+ * Print info about the current OpenGL context
30
+ */
31
+ void R2D_GL_PrintContextInfo(R2D_Window *window) {
32
+ R2D_Log(R2D_INFO,
33
+ "OpenGL Context\n"
34
+ " GL_VENDOR: %s\n"
35
+ " GL_RENDERER: %s\n"
36
+ " GL_VERSION: %s\n"
37
+ " GL_SHADING_LANGUAGE_VERSION: %s",
38
+ window->R2D_GL_VENDOR,
39
+ window->R2D_GL_RENDERER,
40
+ window->R2D_GL_VERSION,
41
+ window->R2D_GL_SHADING_LANGUAGE_VERSION
42
+ );
43
+ }
44
+
45
+
46
+ /*
47
+ * Store info about the current OpenGL context
48
+ */
49
+ void R2D_GL_StoreContextInfo(R2D_Window *window) {
50
+
51
+ window->R2D_GL_VENDOR = glGetString(GL_VENDOR);
52
+ window->R2D_GL_RENDERER = glGetString(GL_RENDERER);
53
+ window->R2D_GL_VERSION = glGetString(GL_VERSION);
54
+
55
+ // These are not defined in GLES
56
+ #if GLES
57
+ window->R2D_GL_MAJOR_VERSION = 0;
58
+ window->R2D_GL_MINOR_VERSION = 0;
59
+ #else
60
+ glGetIntegerv(GL_MAJOR_VERSION, &window->R2D_GL_MAJOR_VERSION);
61
+ glGetIntegerv(GL_MINOR_VERSION, &window->R2D_GL_MINOR_VERSION);
62
+ #endif
63
+
64
+ window->R2D_GL_SHADING_LANGUAGE_VERSION = glGetString(GL_SHADING_LANGUAGE_VERSION);
65
+ };
66
+
67
+
68
+ /*
69
+ * Creates a shader object, loads shader string, and compiles.
70
+ * Returns 0 if shader could not be compiled.
71
+ */
72
+ GLuint R2D_GL_LoadShader(GLenum type, const GLchar *shaderSrc, char *shaderName) {
73
+
74
+ // Create the shader object
75
+ GLuint shader = glCreateShader(type);
76
+
77
+ if (shader == 0) {
78
+ R2D_GL_PrintError("Failed to create shader program");
79
+ return 0;
80
+ }
81
+
82
+ // Load the shader source
83
+ glShaderSource(shader, 1, &shaderSrc, NULL);
84
+
85
+ // Compile the shader
86
+ glCompileShader(shader);
87
+
88
+ // Check the compile status
89
+ GLint compiled;
90
+ glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
91
+
92
+ if (!compiled) {
93
+ GLint infoLen = 0;
94
+ glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
95
+
96
+ if (infoLen > 1) {
97
+ char *infoLog = malloc(sizeof(char) * infoLen);
98
+ glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
99
+ printf("Error compiling shader \"%s\":\n%s\n", shaderName, infoLog);
100
+ free(infoLog);
101
+ }
102
+
103
+ glDeleteShader(shader);
104
+ return 0;
105
+ }
106
+
107
+ return shader;
108
+ }
109
+
110
+
111
+ /*
112
+ * Check if shader program was linked
113
+ */
114
+ int R2D_GL_CheckLinked(GLuint program, char *name) {
115
+
116
+ GLint linked;
117
+ glGetProgramiv(program, GL_LINK_STATUS, &linked);
118
+
119
+ if (!linked) {
120
+ GLint infoLen = 0;
121
+ glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLen);
122
+
123
+ if (infoLen > 1) {
124
+ char *infoLog = malloc(sizeof(char) * infoLen);
125
+ glGetProgramInfoLog(program, infoLen, NULL, infoLog);
126
+ printf("Error linking program `%s`: %s\n", name, infoLog);
127
+ free(infoLog);
128
+ }
129
+
130
+ glDeleteProgram(program);
131
+ return GL_FALSE;
132
+ }
133
+
134
+ return GL_TRUE;
135
+ }
136
+
137
+
138
+ /*
139
+ * Calculate the viewport's scaled width and height
140
+ */
141
+ void R2D_GL_GetViewportScale(R2D_Window *window, int *w, int *h, double *scale) {
142
+
143
+ double s = fmin(
144
+ window->width / (double)window->viewport.width,
145
+ window->height / (double)window->viewport.height
146
+ );
147
+
148
+ *w = window->viewport.width * s;
149
+ *h = window->viewport.height * s;
150
+
151
+ if (scale) *scale = s;
152
+ }
153
+
154
+
155
+ /*
156
+ * Sets the viewport and matrix projection
157
+ */
158
+ void R2D_GL_SetViewport(R2D_Window *window) {
159
+
160
+ int ortho_w = window->viewport.width;
161
+ int ortho_h = window->viewport.height;
162
+ int x, y, w, h; // calculated GL viewport values
163
+
164
+ x = 0; y = 0; w = window->width; h = window->height;
165
+
166
+ switch (window->viewport.mode) {
167
+
168
+ case R2D_FIXED:
169
+ w = window->orig_width;
170
+ h = window->orig_height;
171
+ y = window->height - h;
172
+ break;
173
+
174
+ case R2D_EXPAND:
175
+ ortho_w = w;
176
+ ortho_h = h;
177
+ break;
178
+
179
+ case R2D_SCALE:
180
+ R2D_GL_GetViewportScale(window, &w, &h, NULL);
181
+ // Center the viewport
182
+ x = window->width / 2.0 - w/2.0;
183
+ y = window->height / 2.0 - h/2.0;
184
+ break;
185
+
186
+ case R2D_STRETCH:
187
+ break;
188
+ }
189
+
190
+ glViewport(x, y, w, h);
191
+
192
+ // Set orthographic projection matrix
193
+ orthoMatrix[0] = 2.0f / (GLfloat)ortho_w;
194
+ orthoMatrix[5] = -2.0f / (GLfloat)ortho_h;
195
+
196
+ #if GLES
197
+ R2D_GLES_ApplyProjection(orthoMatrix);
198
+ #else
199
+ if (R2D_GL2) {
200
+ R2D_GL2_ApplyProjection(ortho_w, ortho_h);
201
+ } else {
202
+ R2D_GL3_ApplyProjection(orthoMatrix);
203
+ }
204
+ #endif
205
+ }
206
+
207
+
208
+ /*
209
+ * Initialize OpenGL
210
+ */
211
+ int R2D_GL_Init(R2D_Window *window) {
212
+
213
+ // Specify OpenGL contexts and set attributes
214
+ #if GLES
215
+ SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
216
+ SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
217
+ SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
218
+ SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
219
+ #else
220
+ // Use legacy OpenGL 2.1
221
+ if (FORCE_GL2) {
222
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
223
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
224
+
225
+ // Request an OpenGL 3.3 forward-compatible core profile
226
+ } else {
227
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
228
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
229
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
230
+ }
231
+ #endif
232
+
233
+ // Create and store the OpenGL context
234
+ if (FORCE_GL2) {
235
+ window->glcontext = NULL;
236
+ } else {
237
+ // Ask SDL to create an OpenGL context
238
+ window->glcontext = SDL_GL_CreateContext(window->sdl);
239
+ }
240
+
241
+ // Check if a valid OpenGL context was created
242
+ if (window->glcontext) {
243
+ // Valid context found
244
+
245
+ // Initialize OpenGL ES 2.0
246
+ #if GLES
247
+ R2D_GLES_Init();
248
+ R2D_GL_SetViewport(window);
249
+
250
+ // Initialize OpenGL 3.3+
251
+ #else
252
+ // Initialize GLEW on Windows
253
+ #if WINDOWS
254
+ GLenum err = glewInit();
255
+ if (GLEW_OK != err) R2D_Error("GLEW", glewGetErrorString(err));
256
+ #endif
257
+ R2D_GL3_Init();
258
+ R2D_GL_SetViewport(window);
259
+ #endif
260
+
261
+ // Context could not be created
262
+ } else {
263
+
264
+ #if GLES
265
+ R2D_Error("GLES / SDL_GL_CreateContext", SDL_GetError());
266
+
267
+ #else
268
+ // Try to fallback using an OpenGL 2.1 context
269
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
270
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
271
+
272
+ // Try creating the context again
273
+ window->glcontext = SDL_GL_CreateContext(window->sdl);
274
+
275
+ // Check if this context was created
276
+ if (window->glcontext) {
277
+ // Valid context found
278
+ R2D_GL2 = true;
279
+ R2D_GL2_Init();
280
+ R2D_GL_SetViewport(window);
281
+
282
+ // Could not create any OpenGL contexts, hard failure
283
+ } else {
284
+ R2D_Error("GL2 / SDL_GL_CreateContext", SDL_GetError());
285
+ R2D_Log(R2D_ERROR, "An OpenGL context could not be created");
286
+ return -1;
287
+ }
288
+ #endif
289
+ }
290
+
291
+ // Store the context and print it if diagnostics is enabled
292
+ R2D_GL_StoreContextInfo(window);
293
+ if (R2D_diagnostics) R2D_GL_PrintContextInfo(window);
294
+
295
+ return 0;
296
+ }
297
+
298
+
299
+ /*
300
+ * Creates a texture for rendering
301
+ */
302
+ void R2D_GL_CreateTexture(GLuint *id, GLint format,
303
+ int w, int h,
304
+ const GLvoid *data, GLint filter) {
305
+
306
+ // If 0, then a new texture; generate name
307
+ if (*id == 0) glGenTextures(1, id);
308
+
309
+ // Bind the named texture to a texturing target
310
+ glBindTexture(GL_TEXTURE_2D, *id);
311
+
312
+ // Specifies the 2D texture image
313
+ glTexImage2D(
314
+ GL_TEXTURE_2D, 0, format, w, h,
315
+ 0, format, GL_UNSIGNED_BYTE, data
316
+ );
317
+
318
+ // Set the filtering mode
319
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
320
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
321
+ }
322
+
323
+
324
+ /*
325
+ * Free a texture
326
+ */
327
+ void R2D_GL_FreeTexture(GLuint *id) {
328
+ if (*id != 0) {
329
+ glDeleteTextures(1, id);
330
+ *id = 0;
331
+ }
332
+ }
333
+
334
+
335
+ /*
336
+ * Draw a triangle
337
+ */
338
+ void R2D_GL_DrawTriangle(GLfloat x1, GLfloat y1,
339
+ GLfloat r1, GLfloat g1, GLfloat b1, GLfloat a1,
340
+ GLfloat x2, GLfloat y2,
341
+ GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2,
342
+ GLfloat x3, GLfloat y3,
343
+ GLfloat r3, GLfloat g3, GLfloat b3, GLfloat a3) {
344
+
345
+ #if GLES
346
+ R2D_GLES_DrawTriangle(x1, y1, r1, g1, b1, a1,
347
+ x2, y2, r2, g2, b2, a2,
348
+ x3, y3, r3, g3, b3, a3);
349
+ #else
350
+ if (R2D_GL2) {
351
+ R2D_GL2_DrawTriangle(x1, y1, r1, g1, b1, a1,
352
+ x2, y2, r2, g2, b2, a2,
353
+ x3, y3, r3, g3, b3, a3);
354
+ } else {
355
+ R2D_GL3_DrawTriangle(x1, y1, r1, g1, b1, a1,
356
+ x2, y2, r2, g2, b2, a2,
357
+ x3, y3, r3, g3, b3, a3);
358
+ }
359
+ #endif
360
+ }
361
+
362
+
363
+ /*
364
+ * Draw an image
365
+ */
366
+ void R2D_GL_DrawImage(R2D_Image *img) {
367
+ #if GLES
368
+ R2D_GLES_DrawImage(img);
369
+ #else
370
+ if (R2D_GL2) {
371
+ R2D_GL2_DrawImage(img);
372
+ } else {
373
+ R2D_GL3_DrawImage(img);
374
+ }
375
+ #endif
376
+ }
377
+
378
+
379
+ /*
380
+ * Draw sprite
381
+ */
382
+ void R2D_GL_DrawSprite(R2D_Sprite *spr) {
383
+ #if GLES
384
+ R2D_GLES_DrawSprite(spr);
385
+ #else
386
+ if (R2D_GL2) {
387
+ R2D_GL2_DrawSprite(spr);
388
+ } else {
389
+ R2D_GL3_DrawSprite(spr);
390
+ }
391
+ #endif
392
+ }
393
+
394
+
395
+ /*
396
+ * Draw text
397
+ */
398
+ void R2D_GL_DrawText(R2D_Text *txt) {
399
+ #if GLES
400
+ R2D_GLES_DrawText(txt);
401
+ #else
402
+ if (R2D_GL2) {
403
+ R2D_GL2_DrawText(txt);
404
+ } else {
405
+ R2D_GL3_DrawText(txt);
406
+ }
407
+ #endif
408
+ }
409
+
410
+
411
+ /*
412
+ * Render and flush OpenGL buffers
413
+ */
414
+ void R2D_GL_FlushBuffers() {
415
+ // Only implemented in our OpenGL 3.3+ and ES 2.0 renderers
416
+ #if GLES
417
+ // TODO: R2D_GLES_FlushBuffers();
418
+ #else
419
+ if (!R2D_GL2) R2D_GL3_FlushBuffers();
420
+ #endif
421
+ }
422
+
423
+
424
+ /*
425
+ * Clear buffers to given color values
426
+ */
427
+ void R2D_GL_Clear(R2D_Color clr) {
428
+ glClearColor(clr.r, clr.g, clr.b, clr.a);
429
+ glClear(GL_COLOR_BUFFER_BIT);
430
+ }