Class: SDL::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/graphics/simulation.rb,
ext/sdl/sdl.c

Overview

Renderer is the workhorse of the graphics gem. Everything graphical gets done through the renderer.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#formatObject (readonly)

The PixelFormat for the current renderer.



18
19
20
# File 'lib/graphics/simulation.rb', line 18

def format
  @format
end

#surfaceObject (readonly)

The surface used by this renderer.



23
24
25
# File 'lib/graphics/simulation.rb', line 23

def surface
  @surface
end

#windowObject (readonly)

The window for this renderer



28
29
30
# File 'lib/graphics/simulation.rb', line 28

def window
  @window
end

Instance Method Details

#[](x, y) ⇒ Object

// SDL::Renderer methods:



821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
# File 'ext/sdl/sdl.c', line 821

static VALUE Renderer_index(VALUE self, VALUE x, VALUE y) {
  DEFINE_SELF(Renderer, renderer, self);

  SDL_Rect pixel_rect = { NUM2SINT16(x), NUM2SINT16(y), 1, 1 };

  if (!pixel)
    pixel = SDL_CreateRGBSurfaceWithFormat(0, 1, 1, 32,
                                           SDL_PIXELFORMAT_RGBA32);

  if (SDL_RenderReadPixels(renderer, &pixel_rect, SDL_PIXELFORMAT_RGBA32,
                           pixel->pixels,
                           pixel->pitch))
    FAILURE("Renderer#[]");

  return UINT2NUM(((Uint32*)pixel->pixels)[0]);
}

#[]=(x, y, color) ⇒ Object



811
812
813
814
815
816
817
# File 'ext/sdl/sdl.c', line 811

static VALUE Renderer_index_eq(VALUE self, VALUE x, VALUE y, VALUE color) {
  DEFINE_SELF(Renderer, renderer, self);

  pixelColor(renderer, NUM2SINT16(x), NUM2SINT16(y), VALUE2COLOR(color));

  return Qnil;
}

#blit(src_, x_, y_, a_, ws_, hs_, center_) ⇒ Object



872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
# File 'ext/sdl/sdl.c', line 872

static VALUE Renderer_blit(VALUE self, VALUE src_,
                           VALUE x_, VALUE y_,
                           VALUE a_,
                           VALUE ws_, VALUE hs_,
                           VALUE center_) {
  DEFINE_SELF(Renderer, renderer, self);
  DEFINE_SELF(Surface, src, src_);

  int x    = NUM2SINT16(x_);
  int y    = NUM2SINT16(y_);
  double a = RTEST(a_)  ? -NUM2DBL(a_) : 0.0;
  float ws = RTEST(ws_) ? NUM2FLT(ws_) : 1.0;
  float hs = RTEST(hs_) ? NUM2FLT(hs_) : 1.0;

  SDL_Texture* texture;
  VALUE vtexture = rb_attr_get(src_, id_iv_texture);

  if (RTEST(vtexture)) {
    SET_SELF(Texture, texture, vtexture);
  } else {
    texture = SDL_CreateTextureFromSurface(renderer, src);
    if (!texture)
      FAILURE("_blit(SDL_CreateTextureFromSurface)");

    VALUE vtexture = TypedData_Wrap_Struct(cTexture, &_Texture_type, texture);

    rb_ivar_set(src_, id_iv_texture,  vtexture);
  }

  int w, h;
  if (SDL_QueryTexture(texture, NULL, NULL, &w, &h))
    FAILURE("_blit(SDL_QueryTexture)");

  SDL_Rect dst_rect = { x, y, w*ws, h*hs };

  if (SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND))
    FAILURE("Renderer#blit(SetTextureBlendMode)");

  if (RTEST(a_) || RTEST(ws_) || RTEST(hs_)) {
    SDL_Point bottom_left  = { 0, h-1 };
    SDL_Point *point = (RTEST(center_) ? NULL : &bottom_left);

    if (SDL_RenderCopyEx(renderer, texture, NULL, &dst_rect,
                         a, point, SDL_FLIP_NONE))
      FAILURE("_blit(SDL_RenderCopyEx)");
  } else {
    if (SDL_RenderCopy(renderer, texture, NULL, &dst_rect))
      FAILURE("_blit(SDL_RenderCopyEx)");
  }

  return Qnil;
}

#clear(color) ⇒ Object



723
724
725
726
727
728
729
730
731
732
733
734
735
736
# File 'ext/sdl/sdl.c', line 723

static VALUE Renderer_clear(VALUE self, VALUE color) {
  DEFINE_SELF(Renderer, renderer, self);
  DEFINE_SELF(PixelFormat, format, rb_ivar_get(self, id_iv_format));

  Uint8 r, g, b, a;
  SDL_GetRGBA(NUM2UINT(color), format, &r, &g, &b, &a);

  SDL_SetRenderDrawColor(renderer, r, g, b, a);

  if (SDL_RenderClear(renderer))
    FAILURE("Renderer#clear");

  return Qnil;
}

#copy_texture(texture_) ⇒ Object



738
739
740
741
742
743
744
745
746
# File 'ext/sdl/sdl.c', line 738

static VALUE Renderer_copy_texture(VALUE self, VALUE texture_) {
  DEFINE_SELF(Renderer, renderer, self);
  DEFINE_SELF(Texture,  texture,  texture_);

  if (SDL_RenderCopy(renderer, texture, NULL, NULL))
    FAILURE("Renderer#copy_texture");

  return Qnil;
}

#draw_bezier(xs_, ys_, steps_, color_) ⇒ Object

int stringColor (SDL_Renderer *renderer, Sint16 x, Sint16 y, const char *s, Uint32 color)



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'ext/sdl/sdl.c', line 544

static VALUE Renderer_draw_bezier(VALUE self,
                                  VALUE xs_,
                                  VALUE ys_,
                                  VALUE steps_,
                                  VALUE color_) {
  DEFINE_SELF(Renderer, renderer, self);

  int xlen = RARRAY_LENINT(xs_);
  int ylen = RARRAY_LENINT(ys_);

  if (xlen != ylen)
    rb_raise(rb_eArgError, "xs & ys are different length");

  Sint16 *xs = malloc(xlen*sizeof(Sint16));

  for (int i = 0; i < xlen; i++) {
    xs[i] = NUM2SINT16(RARRAY_AREF(xs_, i));
  }

  Sint16 *ys = malloc(ylen*sizeof(Sint16));
  for (int i = 0; i < ylen; i++) {
    ys[i] = NUM2SINT16(RARRAY_AREF(ys_, i));
  }

  int steps = NUM2INT(steps_);
  Uint32 color = VALUE2COLOR(color_);

  if (bezierColor(renderer, xs, ys, xlen, steps, color))
    FAILURE("draw_bezier");

  free(xs);
  free(ys);

  return Qnil;
}

#draw_circle(x, y, r, c, aa, f) ⇒ Object



592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'ext/sdl/sdl.c', line 592

static VALUE Renderer_draw_circle(VALUE self,
                                  VALUE x,  VALUE y,
                                  VALUE r,
                                  VALUE c,
                                  VALUE aa, VALUE f) {
  DEFINE_SELF(Renderer, renderer, self);

  Uint8 idx = IDX2(RTEST(aa), RTEST(f));

  f_circle[idx](renderer,
                NUM2SINT16(x), NUM2SINT16(y),
                NUM2SINT16(r),
                NUM2UINT(c));

  return Qnil;
}

#draw_ellipse(x, y, rx, ry, c, aa, f) ⇒ Object



621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
# File 'ext/sdl/sdl.c', line 621

static VALUE Renderer_draw_ellipse(VALUE self,
                                   VALUE x,  VALUE y,
                                   VALUE rx, VALUE ry,
                                   VALUE c,
                                   VALUE aa, VALUE f) {
  DEFINE_SELF(Renderer, renderer, self);

  Uint8 idx = IDX2(RTEST(aa), RTEST(f));

  f_ellipse[idx](renderer,
                 NUM2SINT16(x),
                 NUM2SINT16(y),
                 NUM2SINT16(rx),
                 NUM2SINT16(ry),
                 NUM2UINT(c));

  return Qnil;
}

#draw_line(x1, y1, x2, y2, c, aa) ⇒ Object



643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# File 'ext/sdl/sdl.c', line 643

static VALUE Renderer_draw_line(VALUE self,
                                VALUE x1, VALUE y1,
                                VALUE x2, VALUE y2,
                                VALUE c,
                                VALUE aa) {
  DEFINE_SELF(Renderer, renderer, self);

  Uint8 idx = IDX1(RTEST(aa));

  f_line[idx](renderer,
              NUM2SINT16(x1),
              NUM2SINT16(y1),
              NUM2SINT16(x2),
              NUM2SINT16(y2),
              VALUE2COLOR(c));

  return Qnil;
}

#draw_polygon(xs_, ys_, c, aa) ⇒ Object



668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# File 'ext/sdl/sdl.c', line 668

static VALUE Renderer_draw_polygon(VALUE self,
                                   VALUE xs_,
                                   VALUE ys_,
                                   VALUE c,
                                   VALUE aa) {
  DEFINE_SELF(Renderer, renderer, self);

  int xlen = RARRAY_LENINT(xs_);
  int ylen = RARRAY_LENINT(ys_);

  if (xlen != ylen)
    rb_raise(rb_eArgError, "xs & ys are different length");

  Sint16 *xs = malloc(xlen*sizeof(Sint16));
  for (int i = 0; i < xlen; i++) {
    xs[i] = NUM2SINT16(RARRAY_AREF(xs_, i));
  }

  Sint16 *ys = malloc(ylen*sizeof(Sint16));
  for (int i = 0; i < ylen; i++) {
    ys[i] = NUM2SINT16(RARRAY_AREF(ys_, i));
  }

  Uint8 idx = IDX1(RTEST(aa));

  if (f_polygon[idx](renderer, xs, ys, xlen, VALUE2COLOR(c)))
    FAILURE("draw_polygon");

  free(xs);
  free(ys);

  return Qnil;
}

#draw_rect(x_, y_, w_, h_, c, f) ⇒ Object



705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'ext/sdl/sdl.c', line 705

static VALUE Renderer_draw_rect(VALUE self,
                                VALUE x_, VALUE y_,
                                VALUE w_, VALUE h_,
                                VALUE c,
                                VALUE f) {
  DEFINE_SELF(Renderer, renderer, self);

  Sint16 x1 = NUM2SINT16(x_);
  Sint16 y1 = NUM2SINT16(y_);
  Sint16 x2 = NUM2SINT16(w_) + x1;
  Sint16 y2 = NUM2SINT16(h_) + y1;
  Uint8 idx = IDX1(RTEST(f));

  f_rect[idx](renderer, x1, y1, x2, y2, NUM2UINT(c));

  return Qnil;
}

#fast_rect(x, y, w, h, color) ⇒ Object



748
749
750
751
752
753
# File 'ext/sdl/sdl.c', line 748

static VALUE Renderer_fast_rect(VALUE self,
                               VALUE x, VALUE y,
                               VALUE w, VALUE h,
                               VALUE color) {
  return Renderer_draw_rect(self, x, y, w, h, color, Qtrue);
}

#hObject



780
781
782
783
784
785
786
787
788
# File 'ext/sdl/sdl.c', line 780

static VALUE Renderer_h(VALUE self) {
  DEFINE_SELF(Renderer, renderer, self);

  int w, h;

  SDL_GetRendererOutputSize(renderer, &w, &h);

  return INT2NUM(h);
}

#new_textureObject



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'ext/sdl/sdl.c', line 438

static VALUE Renderer_new_texture(VALUE self) {
  DEFINE_SELF(Renderer, renderer, self);

  int w, h;
  if (SDL_GetRendererOutputSize(renderer, &w, &h))
    FAILURE("Renderer#new_texture(GetRendererOutputSize");

  SDL_Texture *texture = SDL_CreateTexture(renderer,
                                           SDL_PIXELFORMAT_RGBA32,
                                           SDL_TEXTUREACCESS_TARGET,
                                           w, h);
  if (!texture)
    FAILURE("Renderer#new_texture(CreateTexture)");

  return TypedData_Wrap_Struct(cTexture, &_Texture_type, texture);
}

#presentObject



456
457
458
459
460
461
462
# File 'ext/sdl/sdl.c', line 456

static VALUE Renderer_present(VALUE self) {
  DEFINE_SELF(Renderer, renderer, self);

  SDL_RenderPresent(renderer);

  return Qnil;
}

#save(path) ⇒ Object



956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
# File 'ext/sdl/sdl.c', line 956

static VALUE Renderer_save(VALUE self, VALUE path) {
  DEFINE_SELF(Renderer, renderer, self);

  int w, h;
  SDL_GetRendererOutputSize(renderer, &w, &h);

  SDL_Surface *sshot = SDL_CreateRGBSurfaceWithFormat(0, w, h, 32,
                                                      SDL_PIXELFORMAT_RGBA32);
  if (SDL_RenderReadPixels(renderer, NULL, SDL_PIXELFORMAT_RGBA32,
                           sshot->pixels,
                           sshot->pitch))
    FAILURE("Renderer#save");
  int ret = IMG_SavePNG(sshot, RSTRING_PTR(path));
  SDL_FreeSurface(sshot);

  return INT2NUM(ret);
}

#sprite(w_, h_) ⇒ Object



925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
# File 'ext/sdl/sdl.c', line 925

static VALUE Renderer_sprite(VALUE self, VALUE w_, VALUE h_) {
  UNUSED(self);

  int w   = NUM2INT(w_);
  int h   = NUM2INT(h_);
  int bpp = 32;

  SDL_Surface *surface =
    SDL_CreateRGBSurfaceWithFormat(0, w, h, bpp, SDL_PIXELFORMAT_RGBA32);
  if (!surface) FAILURE("Surface#sprite(CreateRGBSurfaceWithFormat)");

  SDL_Renderer *renderer = SDL_CreateSoftwareRenderer(surface);
  if (!renderer) FAILURE("Surface#sprite(CreateSoftwareRenderer)");

  // bumps the refcount and returns the same thing
  SDL_PixelFormat *format = SDL_AllocFormat(surface->format->format);
  if (format != surface->format)
    rb_raise(eSDLError, "SDL_AllocFormat freaked out. %p vs %p",
             format,
             surface->format);

  VALUE vrenderer = TypedData_Wrap_Struct(cRenderer,    &_Renderer_type,    renderer);
  VALUE vsurface  = TypedData_Wrap_Struct(cSurface,     &_Surface_type,     surface);
  VALUE vformat   = TypedData_Wrap_Struct(cPixelFormat, &_PixelFormat_type, format);

  rb_ivar_set(vrenderer, id_iv_surface, vsurface);
  rb_ivar_set(vrenderer, id_iv_format,  vformat);

  return vrenderer;
}

#targetObject



985
986
987
988
989
990
991
992
993
994
# File 'ext/sdl/sdl.c', line 985

static VALUE Renderer_target(VALUE self) {
  DEFINE_SELF(Renderer, renderer, self);

  SDL_Texture* texture = SDL_GetRenderTarget(renderer);

  if (!texture)
    FAILURE("Renderer#target");

  return TypedData_Wrap_Struct(cTexture, &_Texture_type, texture);
}

#target=(texture_) ⇒ Object



996
997
998
999
1000
1001
1002
1003
1004
# File 'ext/sdl/sdl.c', line 996

static VALUE Renderer_target_eq(VALUE self, VALUE texture_) {
  DEFINE_SELF(Renderer, renderer, self);
  DEFINE_SELF0(Texture, texture, texture_);

  if (SDL_SetRenderTarget(renderer, texture))
    FAILURE("Renderer#target=");

  return texture_;
}

#titleObject

The title of the window.



33
34
35
# File 'lib/graphics/simulation.rb', line 33

def title
  @window.title
end

#title=(s) ⇒ Object

Sets the title of the window.



40
41
42
# File 'lib/graphics/simulation.rb', line 40

def title= s
  @window.title = s
end

#wObject



790
791
792
793
794
795
796
797
798
# File 'ext/sdl/sdl.c', line 790

static VALUE Renderer_w(VALUE self) {
  DEFINE_SELF(Renderer, renderer, self);

  int w, h;

  SDL_GetRendererOutputSize(renderer, &w, &h);

  return INT2NUM(w);
}