ruby2d 0.10.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
data/ext/ruby2d/ruby2d.h CHANGED
@@ -272,64 +272,6 @@ typedef struct {
272
272
  bool close;
273
273
  } R2D_Window;
274
274
 
275
- // R2D_Image
276
- typedef struct {
277
- const char *path;
278
- SDL_Surface *surface;
279
- int format;
280
- GLuint texture_id;
281
- R2D_Color color;
282
- int x;
283
- int y;
284
- int width;
285
- int height;
286
- int orig_width;
287
- int orig_height;
288
- GLfloat rotate; // Rotation angle in degrees
289
- GLfloat rx; // X coordinate to be rotated around
290
- GLfloat ry; // Y coordinate to be rotated around
291
- } R2D_Image;
292
-
293
- // R2D_Sprite
294
- typedef struct {
295
- const char *path;
296
- R2D_Image *img;
297
- R2D_Color color;
298
- int x;
299
- int y;
300
- int width;
301
- int height;
302
- int clip_width;
303
- int clip_height;
304
- GLfloat rotate; // Rotation angle in degrees
305
- GLfloat rx; // X coordinate to be rotated around
306
- GLfloat ry; // Y coordinate to be rotated around
307
- GLfloat tx1;
308
- GLfloat ty1;
309
- GLfloat tx2;
310
- GLfloat ty2;
311
- GLfloat tx3;
312
- GLfloat ty3;
313
- GLfloat tx4;
314
- GLfloat ty4;
315
- } R2D_Sprite;
316
-
317
- // R2D_Text
318
- typedef struct {
319
- const char *font;
320
- SDL_Surface *surface;
321
- GLuint texture_id;
322
- TTF_Font *font_data;
323
- R2D_Color color;
324
- char *msg;
325
- int x;
326
- int y;
327
- int width;
328
- int height;
329
- GLfloat rotate; // Rotation angle in degrees
330
- GLfloat rx; // X coordinate to be rotated around
331
- GLfloat ry; // Y coordinate to be rotated around
332
- } R2D_Text;
333
275
 
334
276
  // R2D_Sound
335
277
  typedef struct {
@@ -452,85 +394,28 @@ void R2D_DrawCircle(
452
394
  // Image ///////////////////////////////////////////////////////////////////////
453
395
 
454
396
  /*
455
- * Create an image, given a file path
456
- */
457
- R2D_Image *R2D_CreateImage(const char *path);
458
-
459
- /*
460
- * Rotate an image
461
- */
462
- void R2D_RotateImage(R2D_Image *img, GLfloat angle, int position);
463
-
464
- /*
465
- * Draw an image
466
- */
467
- void R2D_DrawImage(R2D_Image *img);
468
-
469
- /*
470
- * Free an image
471
- */
472
- void R2D_FreeImage(R2D_Image *img);
473
-
474
- // Sprite //////////////////////////////////////////////////////////////////////
475
-
476
- /*
477
- * Create a sprite, given an image file path
478
- */
479
- R2D_Sprite *R2D_CreateSprite(const char *path);
480
-
481
- /*
482
- * Clip a sprite
397
+ * Create a surface with image pixel data, given a file path
483
398
  */
484
- void R2D_ClipSprite(R2D_Sprite *spr, int x, int y, int w, int h);
399
+ SDL_Surface *R2D_CreateImageSurface(const char *path);
485
400
 
486
401
  /*
487
- * Rotate a sprite
402
+ * Convert images to RGB format if they are in a different (BGR for example) format.
488
403
  */
489
- void R2D_RotateSprite(R2D_Sprite *spr, GLfloat angle, int position);
404
+ void R2D_ImageConvertToRGB(SDL_Surface *surface);
490
405
 
491
- /*
492
- * Draw a sprite
493
- */
494
- void R2D_DrawSprite(R2D_Sprite *spr);
406
+ // Font ////////////////////////////////////////////////////////////////////////
495
407
 
496
408
  /*
497
- * Free a sprite
409
+ * Create a TTF_Font object given a path to a font and a size
498
410
  */
499
- void R2D_FreeSprite(R2D_Sprite *spr);
500
-
501
- // Tile ////////////////////////////////////////////////////////////////////////
502
-
503
- /*
504
- * Draw a tile
505
- */
506
- void R2D_DrawTile(R2D_Image *img, int tw, int th, int padding, int spacing, int tx, int ty, int x, int y);
411
+ TTF_Font *R2D_FontCreateTTFFont(const char *path, int size, const char *style);
507
412
 
508
413
  // Text ////////////////////////////////////////////////////////////////////////
509
414
 
510
415
  /*
511
- * Create text, given a font file path, the message, and size
512
- */
513
- R2D_Text *R2D_CreateText(const char *font, const char *msg, int size);
514
-
515
- /*
516
- * Set the text message
517
- */
518
- void R2D_SetText(R2D_Text *txt, const char *msg, ...);
519
-
520
- /*
521
- * Rotate text
522
- */
523
- void R2D_RotateText(R2D_Text *txt, GLfloat angle, int position);
524
-
525
- /*
526
- * Draw text
527
- */
528
- void R2D_DrawText(R2D_Text *txt);
529
-
530
- /*
531
- * Free the text
416
+ * Create a SDL_Surface that contains the pixel data to render text, given a font and message
532
417
  */
533
- void R2D_FreeText(R2D_Text *txt);
418
+ SDL_Surface *R2D_TextCreateSurface(TTF_Font *font, const char *message);
534
419
 
535
420
  // Sound ///////////////////////////////////////////////////////////////////////
536
421
 
@@ -702,11 +587,11 @@ int R2D_FreeWindow(R2D_Window *window);
702
587
  // Ruby 2D OpenGL Functions ////////////////////////////////////////////////////
703
588
 
704
589
  int R2D_GL_Init(R2D_Window *window);
705
- void R2D_GL_PrintError(char *error);
590
+ void R2D_GL_PrintError(const char *error);
706
591
  void R2D_GL_PrintContextInfo(R2D_Window *window);
707
592
  void R2D_GL_StoreContextInfo(R2D_Window *window);
708
- GLuint R2D_GL_LoadShader(GLenum type, const GLchar *shaderSrc, char *shaderName);
709
- int R2D_GL_CheckLinked(GLuint program, char *name);
593
+ GLuint R2D_GL_LoadShader(GLenum type, const GLchar *shaderSrc, const char *shaderName);
594
+ int R2D_GL_CheckLinked(GLuint program, const char *name);
710
595
  void R2D_GL_GetViewportScale(R2D_Window *window, int *w, int *h, double *scale);
711
596
  void R2D_GL_SetViewport(R2D_Window *window);
712
597
  void R2D_GL_CreateTexture(
@@ -720,11 +605,7 @@ void R2D_GL_DrawTriangle(
720
605
  GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2,
721
606
  GLfloat x3, GLfloat y3,
722
607
  GLfloat r3, GLfloat g3, GLfloat b3, GLfloat a3);
723
- void R2D_GL_DrawImage(R2D_Image *img);
724
- void R2D_GL_DrawSprite(R2D_Sprite *spr);
725
- void R2D_GL_DrawTile(R2D_Image *img, int x, int y, int tw, int th, GLfloat tx1, GLfloat ty1, GLfloat tx2,
726
- GLfloat ty2, GLfloat tx3, GLfloat ty3, GLfloat tx4, GLfloat ty4);
727
- void R2D_GL_DrawText(R2D_Text *txt);
608
+ void R2D_GL_DrawTexture(GLfloat coordinates[], GLfloat texture_coordinates[], GLfloat color[], int texture_id);
728
609
  void R2D_GL_FreeTexture(GLuint *id);
729
610
  void R2D_GL_Clear(R2D_Color clr);
730
611
  void R2D_GL_FlushBuffers();
@@ -741,13 +622,7 @@ void R2D_GL_FlushBuffers();
741
622
  GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2,
742
623
  GLfloat x3, GLfloat y3,
743
624
  GLfloat r3, GLfloat g3, GLfloat b3, GLfloat a3);
744
- void R2D_GLES_DrawImage(R2D_Image *img);
745
- void R2D_GLES_DrawSprite(R2D_Sprite *spr);
746
- void R2D_GLES_DrawTile(R2D_Image *img, int x, int y,
747
- int tw, int th,
748
- GLfloat tx1, GLfloat ty1, GLfloat tx2, GLfloat ty2,
749
- GLfloat tx3, GLfloat ty3, GLfloat tx4, GLfloat ty4);
750
- void R2D_GLES_DrawText(R2D_Text *txt);
625
+ void R2D_GLES_DrawTexture(GLfloat coordinates[], GLfloat texture_coordinates[], GLfloat color[], int texture_id);
751
626
  #else
752
627
  int R2D_GL2_Init();
753
628
  int R2D_GL3_Init();
@@ -767,20 +642,8 @@ void R2D_GL_FlushBuffers();
767
642
  GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2,
768
643
  GLfloat x3, GLfloat y3,
769
644
  GLfloat r3, GLfloat g3, GLfloat b3, GLfloat a3);
770
- void R2D_GL2_DrawImage(R2D_Image *img);
771
- void R2D_GL3_DrawImage(R2D_Image *img);
772
- void R2D_GL2_DrawSprite(R2D_Sprite *spr);
773
- void R2D_GL3_DrawSprite(R2D_Sprite *spr);
774
- void R2D_GL2_DrawTile(R2D_Image *img, int x, int y,
775
- int tw, int th,
776
- GLfloat tx1, GLfloat ty1, GLfloat tx2, GLfloat ty2,
777
- GLfloat tx3, GLfloat ty3, GLfloat tx4, GLfloat ty4);
778
- void R2D_GL3_DrawTile(R2D_Image *img, int x, int y,
779
- int tw, int th,
780
- GLfloat tx1, GLfloat ty1, GLfloat tx2, GLfloat ty2,
781
- GLfloat tx3, GLfloat ty3, GLfloat tx4, GLfloat ty4);
782
- void R2D_GL2_DrawText(R2D_Text *txt);
783
- void R2D_GL3_DrawText(R2D_Text *txt);
645
+ void R2D_GL2_DrawTexture(GLfloat coordinates[], GLfloat texture_coordinates[], GLfloat color[], int texture_id);
646
+ void R2D_GL3_DrawTexture(GLfloat coordinates[], GLfloat texture_coordinates[], GLfloat color[], int texture_id);
784
647
  void R2D_GL3_FlushBuffers();
785
648
  #endif
786
649
 
data/ext/ruby2d/sound.c CHANGED
@@ -57,7 +57,7 @@ int R2D_GetSoundLength(R2D_Sound *snd) {
57
57
  int channels = 0;
58
58
 
59
59
  // Populate the frequency, format and channel variables
60
- if (!Mix_QuerySpec(&frequency, &format, &channels)) return -1; // Querying audio deails failed
60
+ if (!Mix_QuerySpec(&frequency, &format, &channels)) return -1; // Querying audio details failed
61
61
  if (!snd) return -1;
62
62
 
63
63
  // points = bytes / samplesize
data/ext/ruby2d/text.c CHANGED
@@ -4,126 +4,19 @@
4
4
 
5
5
 
6
6
  /*
7
- * Create text, given a font file path, the message, and size
7
+ * Create a SDL_Surface that contains the pixel data to render text, given a font and message
8
8
  */
9
- R2D_Text *R2D_CreateText(const char *font, const char *msg, int size) {
10
- R2D_Init();
11
-
12
- // Check if font file exists
13
- if (!R2D_FileExists(font)) {
14
- R2D_Error("R2D_CreateText", "Font file `%s` not found", font);
15
- return NULL;
16
- }
17
-
9
+ SDL_Surface *R2D_TextCreateSurface(TTF_Font *font, const char *message) {
18
10
  // `msg` cannot be an empty string or NULL for TTF_SizeText
19
- if (msg == NULL || strlen(msg) == 0) msg = " ";
11
+ if (message == NULL || strlen(message) == 0) message = " ";
20
12
 
21
- // Allocate the text structure
22
- R2D_Text *txt = (R2D_Text *) malloc(sizeof(R2D_Text));
23
- if (!txt) {
24
- R2D_Error("R2D_CreateText", "Out of memory!");
13
+ SDL_Color color = {255, 255, 255};
14
+ SDL_Surface *surface = TTF_RenderUTF8_Blended(font, message, color);
15
+ if (!surface)
16
+ {
17
+ R2D_Error("TTF_RenderUTF8_Blended", TTF_GetError());
25
18
  return NULL;
26
19
  }
27
20
 
28
- // Open the font
29
- txt->font_data = TTF_OpenFont(font, size);
30
- if (!txt->font_data) {
31
- R2D_Error("TTF_OpenFont", TTF_GetError());
32
- free(txt);
33
- return NULL;
34
- }
35
-
36
- // Initialize values
37
- txt->font = font;
38
- txt->msg = (char *) malloc(strlen(msg) + 1 * sizeof(char));
39
- strcpy(txt->msg, msg);
40
- txt->x = 0;
41
- txt->y = 0;
42
- txt->color.r = 1.f;
43
- txt->color.g = 1.f;
44
- txt->color.b = 1.f;
45
- txt->color.a = 1.f;
46
- txt->rotate = 0;
47
- txt->rx = 0;
48
- txt->ry = 0;
49
- txt->texture_id = 0;
50
-
51
- // Save the width and height of the text
52
- TTF_SizeUTF8(txt->font_data, txt->msg, &txt->width, &txt->height);
53
-
54
- return txt;
55
- }
56
-
57
-
58
- /*
59
- * Set the text message
60
- */
61
- void R2D_SetText(R2D_Text *txt, const char *msg, ...) {
62
- if (!txt) return;
63
-
64
- // `msg` cannot be an empty string or NULL for TTF_SizeUTF8
65
- if (msg == NULL || strlen(msg) == 0) msg = " ";
66
-
67
- // Format and store new text string
68
- va_list args;
69
- va_start(args, msg);
70
- free(txt->msg);
71
- vasprintf(&txt->msg, msg, args);
72
- va_end(args);
73
-
74
- // Save the width and height of the text
75
- TTF_SizeUTF8(txt->font_data, txt->msg, &txt->width, &txt->height);
76
-
77
- // Delete the current texture so a new one can be generated
78
- R2D_GL_FreeTexture(&txt->texture_id);
79
- }
80
-
81
-
82
- /*
83
- * Rotate text
84
- */
85
- void R2D_RotateText(R2D_Text *txt, GLfloat angle, int position) {
86
-
87
- R2D_GL_Point p = R2D_GetRectRotationPoint(
88
- txt->x, txt->y, txt->width, txt->height, position
89
- );
90
-
91
- txt->rotate = angle;
92
- txt->rx = p.x;
93
- txt->ry = p.y;
94
- }
95
-
96
-
97
- /*
98
- * Draw text
99
- */
100
- void R2D_DrawText(R2D_Text *txt) {
101
- if (!txt) return;
102
-
103
- if (txt->texture_id == 0) {
104
- SDL_Color color = { 255, 255, 255 };
105
- txt->surface = TTF_RenderUTF8_Blended(txt->font_data, txt->msg, color);
106
- if (!txt->surface) {
107
- R2D_Error("TTF_RenderUTF8_Blended", TTF_GetError());
108
- return;
109
- }
110
- R2D_GL_CreateTexture(&txt->texture_id, GL_RGBA,
111
- txt->width, txt->height,
112
- txt->surface->pixels, GL_NEAREST);
113
- SDL_FreeSurface(txt->surface);
114
- }
115
-
116
- R2D_GL_DrawText(txt);
117
- }
118
-
119
-
120
- /*
121
- * Free the text
122
- */
123
- void R2D_FreeText(R2D_Text *txt) {
124
- if (!txt) return;
125
- free(txt->msg);
126
- R2D_GL_FreeTexture(&txt->texture_id);
127
- TTF_CloseFont(txt->font_data);
128
- free(txt);
129
- }
21
+ return surface;
22
+ }
data/ext/ruby2d/window.c CHANGED
@@ -335,11 +335,11 @@ int R2D_Show(R2D_Window *window) {
335
335
  * Set the icon for the window
336
336
  */
337
337
  void R2D_SetIcon(R2D_Window *window, const char *icon) {
338
- R2D_Image *img = R2D_CreateImage(icon);
339
- if (img) {
338
+ SDL_Surface *iconSurface = R2D_CreateImageSurface(icon);
339
+ if (iconSurface) {
340
340
  window->icon = icon;
341
- SDL_SetWindowIcon(window->sdl, img->surface);
342
- R2D_FreeImage(img);
341
+ SDL_SetWindowIcon(window->sdl, iconSurface);
342
+ SDL_FreeSurface(iconSurface);
343
343
  } else {
344
344
  R2D_Log(R2D_WARN, "Could not set window icon");
345
345
  }
data/lib/ruby2d/circle.rb CHANGED
@@ -13,7 +13,7 @@ module Ruby2D
13
13
  @radius = opts[:radius] || 50
14
14
  @sectors = opts[:sectors] || 30
15
15
  self.color = opts[:color] || 'white'
16
- self.opacity = opts[:opacity] if opts[:opacity]
16
+ self.color.opacity = opts[:opacity] if opts[:opacity]
17
17
  add
18
18
  end
19
19
 
@@ -22,6 +22,8 @@ module Ruby2D
22
22
  end
23
23
 
24
24
  def self.draw(opts = {})
25
+ Window.render_ready_check
26
+
25
27
  ext_draw([
26
28
  opts[:x], opts[:y], opts[:radius], opts[:sectors],
27
29
  opts[:color][0], opts[:color][1], opts[:color][2], opts[:color][3]
@@ -25,6 +25,8 @@ require 'fileutils'
25
25
  'text',
26
26
  'sound',
27
27
  'music',
28
+ 'texture',
29
+ 'vertices',
28
30
  '../ruby2d'
29
31
  ]
30
32
 
data/lib/ruby2d/font.rb CHANGED
@@ -2,8 +2,22 @@
2
2
 
3
3
  module Ruby2D
4
4
  class Font
5
+ @@loaded_fonts = {}
6
+
7
+ attr_reader :ttf_font
8
+
9
+ def initialize(path, size, style=nil)
10
+ @ttf_font = Font.ext_load(path, size, style.to_s)
11
+ end
5
12
 
6
13
  class << self
14
+ def load(path, size, style=nil)
15
+ unless File.exist? path
16
+ raise Error, "Cannot find font file `#{path}`"
17
+ end
18
+
19
+ @@loaded_fonts[[path, size, style]] ||= Font.new(path, size, style)
20
+ end
7
21
 
8
22
  # List all fonts, names only
9
23
  def all
@@ -50,6 +64,7 @@ module Ruby2D
50
64
  macos_font_path = '/Library/Fonts'
51
65
  linux_font_path = '/usr/share/fonts'
52
66
  windows_font_path = 'C:/Windows/Fonts'
67
+ openbsd_font_path = '/usr/X11R6/lib/X11/fonts'
53
68
 
54
69
  # If MRI and/or non-Bash shell (like cmd.exe)
55
70
  if Object.const_defined? :RUBY_PLATFORM
@@ -60,16 +75,20 @@ module Ruby2D
60
75
  linux_font_path
61
76
  when /mingw/
62
77
  windows_font_path
78
+ when /openbsd/
79
+ openbsd_font_path
63
80
  end
64
81
  # If MRuby
65
82
  else
66
83
  uname = `uname`
67
- if uname.include? 'Darwin' # macOS
84
+ if uname.include? 'Darwin' # macOS
68
85
  macos_font_path
69
86
  elsif uname.include? 'Linux'
70
87
  linux_font_path
71
88
  elsif uname.include? 'MINGW'
72
89
  windows_font_path
90
+ elsif uname.include? 'OpenBSD'
91
+ openbsd_font_path
73
92
  end
74
93
  end
75
94
  end