Class: SDL::Renderer

Inherits:
Data
  • 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:



770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
# File 'ext/sdl/sdl.c', line 770

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



760
761
762
763
764
765
766
# File 'ext/sdl/sdl.c', line 760

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



821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
# File 'ext/sdl/sdl.c', line 821

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



672
673
674
675
676
677
678
679
680
681
682
683
684
685
# File 'ext/sdl/sdl.c', line 672

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



687
688
689
690
691
692
693
694
695
# File 'ext/sdl/sdl.c', line 687

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)



533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'ext/sdl/sdl.c', line 533

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



581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'ext/sdl/sdl.c', line 581

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



610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# File 'ext/sdl/sdl.c', line 610

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



632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'ext/sdl/sdl.c', line 632

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_rect(x_, y_, w_, h_, c, f) ⇒ Object



654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# File 'ext/sdl/sdl.c', line 654

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



697
698
699
700
701
702
# File 'ext/sdl/sdl.c', line 697

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



729
730
731
732
733
734
735
736
737
# File 'ext/sdl/sdl.c', line 729

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

  int w, h;

  SDL_GetRendererOutputSize(renderer, &w, &h);

  return INT2NUM(h);
}

#new_textureObject



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'ext/sdl/sdl.c', line 429

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



447
448
449
450
451
452
453
# File 'ext/sdl/sdl.c', line 447

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

  SDL_RenderPresent(renderer);

  return Qnil;
}

#save(path) ⇒ Object



905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
# File 'ext/sdl/sdl.c', line 905

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



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
# File 'ext/sdl/sdl.c', line 874

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



934
935
936
937
938
939
940
941
942
943
# File 'ext/sdl/sdl.c', line 934

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



945
946
947
948
949
950
951
952
953
# File 'ext/sdl/sdl.c', line 945

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



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

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

  int w, h;

  SDL_GetRendererOutputSize(renderer, &w, &h);

  return INT2NUM(w);
}